2002-07-09 12:24:59 +00:00
|
|
|
/* Contains code to load dev module and bootstrap it */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
|
*/
|
|
|
|
|
#include <kernel.h>
|
|
|
|
|
#include <stage2.h>
|
|
|
|
|
#include <dev.h>
|
|
|
|
|
#include <vfs.h>
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
#include <elf.h>
|
|
|
|
|
#include <Errors.h>
|
|
|
|
|
#include <devfs.h>
|
2002-07-12 23:52:01 +00:00
|
|
|
#include <Drivers.h>
|
2002-07-09 12:24:59 +00:00
|
|
|
#include <memheap.h>
|
|
|
|
|
|
2002-07-12 23:52:01 +00:00
|
|
|
#ifdef ARCH_x86
|
|
|
|
|
#include <arch/x86/console_dev.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <fb_console.h>
|
|
|
|
|
|
2002-07-09 12:24:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2002-07-16 07:54:56 +00:00
|
|
|
/* TODO: move this into devfs.c, to add full dynamicly loaded device drivers support
|
|
|
|
|
These are mainly here to allow testing and this needs to be revisited
|
|
|
|
|
*/
|
2002-07-09 12:24:59 +00:00
|
|
|
const char *device_paths[] = {
|
2002-07-12 19:24:26 +00:00
|
|
|
"/boot/addons/drivers/dev",
|
|
|
|
|
"/boot/addons/drivers/dev/audio",
|
2002-07-09 12:24:59 +00:00
|
|
|
"/boot/addons/drivers/dev/misc",
|
|
|
|
|
"/boot/addons/drivers/dev/net",
|
|
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int dev_init(kernel_args *ka)
|
|
|
|
|
{
|
|
|
|
|
int fd;
|
|
|
|
|
const char **ptr;
|
|
|
|
|
|
|
|
|
|
dprintf("dev_init: entry\n");
|
|
|
|
|
|
|
|
|
|
for (ptr = device_paths; (*ptr); ptr++) {
|
2002-07-14 05:18:21 +00:00
|
|
|
fd = sys_open_dir(*ptr);
|
|
|
|
|
if (fd >= 0) {
|
2002-07-09 12:24:59 +00:00
|
|
|
ssize_t len;
|
2002-07-10 21:54:34 +00:00
|
|
|
char buf[SYS_MAX_NAME_LEN + sizeof(struct dirent) + 1];
|
|
|
|
|
struct dirent *dirent = (struct dirent *)buf;
|
2002-07-09 12:24:59 +00:00
|
|
|
|
2002-07-10 21:54:34 +00:00
|
|
|
while ((len = sys_read_dir(fd, dirent, sizeof(buf), 1)) > 0) {
|
|
|
|
|
dprintf("loading '%s' dev module\n", dirent->d_name);
|
|
|
|
|
dev_load_dev_module(dirent->d_name, *ptr);
|
2002-07-09 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
sys_close(fd);
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-07-10 21:54:34 +00:00
|
|
|
|
2002-07-12 23:52:01 +00:00
|
|
|
/* now run the oddballs we have, consoles at present */
|
|
|
|
|
/* XXX - these are x86 and may not be applicable to all platforms */
|
|
|
|
|
#ifdef ARCH_x86
|
|
|
|
|
console_dev_init(ka);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
fb_console_dev_init(ka);
|
|
|
|
|
|
2002-07-09 12:24:59 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
image_id dev_load_dev_module(const char *name, const char *dirpath)
|
|
|
|
|
{
|
|
|
|
|
image_id id;
|
2002-07-16 07:54:56 +00:00
|
|
|
char path[SYS_MAX_PATH_LEN];
|
|
|
|
|
// uint32 * api_version;
|
2002-07-09 12:24:59 +00:00
|
|
|
status_t (*init_hardware)(void);
|
2002-07-16 07:54:56 +00:00
|
|
|
status_t (*init_driver)(void);
|
|
|
|
|
status_t (*uninit_driver)(void);
|
2002-07-09 12:24:59 +00:00
|
|
|
device_hooks *(*find_device)(const char *);
|
2002-07-16 07:54:56 +00:00
|
|
|
const char **(*publish_devices)(void);
|
2002-07-09 12:24:59 +00:00
|
|
|
const char **devfs_paths;
|
2002-07-16 07:54:56 +00:00
|
|
|
bool keep_loaded;
|
2002-07-09 12:24:59 +00:00
|
|
|
|
|
|
|
|
sprintf(path, "%s/%s", dirpath, name);
|
|
|
|
|
|
|
|
|
|
id = elf_load_kspace(path, "");
|
|
|
|
|
if(id < 0)
|
|
|
|
|
return id;
|
2002-07-16 07:54:56 +00:00
|
|
|
|
|
|
|
|
// get device driver API symbols
|
|
|
|
|
// api_version = (uint32 *) elf_lookup_symbol(id, "api_version");
|
|
|
|
|
init_hardware = (void *) elf_lookup_symbol(id, "init_hardware");
|
|
|
|
|
init_driver = (void *) elf_lookup_symbol(id, "init_driver");
|
|
|
|
|
uninit_driver = (void *) elf_lookup_symbol(id, "uninit_driver");
|
|
|
|
|
publish_devices = (void *) elf_lookup_symbol(id, "publish_devices");
|
|
|
|
|
find_device = (void *) elf_lookup_symbol(id, "find_device");
|
|
|
|
|
|
|
|
|
|
if (publish_devices == NULL || find_device == NULL) {
|
|
|
|
|
dprintf("DEV: %s: mandatory driver symbol(s) missing! :(\n", name);
|
|
|
|
|
id = EINVAL;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (init_hardware && init_hardware() != 0) {
|
|
|
|
|
dprintf("DEV: %s: init_hardware failed :(\n", name);
|
|
|
|
|
id = ENXIO;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (init_driver && init_driver() != 0) {
|
|
|
|
|
dprintf("DEV: %s: init_driver failed :(\n", name);
|
|
|
|
|
id = ENXIO;
|
|
|
|
|
goto error;
|
2002-07-09 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
2002-07-16 07:54:56 +00:00
|
|
|
// dprintf("DEV: Well alright - %s init'd OK!\n", name);
|
|
|
|
|
|
|
|
|
|
/* Should really cycle through these... */
|
|
|
|
|
devfs_paths = publish_devices();
|
|
|
|
|
keep_loaded = false;
|
|
|
|
|
for (; *devfs_paths; devfs_paths++) {
|
|
|
|
|
device_hooks * hooks;
|
|
|
|
|
|
|
|
|
|
hooks = find_device(*devfs_paths);
|
|
|
|
|
dprintf("DEV: %s: publishing %s\n", name, *devfs_paths);
|
|
|
|
|
if (hooks) {
|
|
|
|
|
if (devfs_publish_device(*devfs_paths, NULL, hooks) == 0)
|
|
|
|
|
keep_loaded = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (keep_loaded)
|
|
|
|
|
// okay, keep this driver image in memory, as he publish *something*
|
|
|
|
|
return id;
|
|
|
|
|
|
|
|
|
|
// no need to keep this driver image loaded...
|
|
|
|
|
if (uninit_driver)
|
|
|
|
|
uninit_driver();
|
|
|
|
|
id = 0;
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
elf_unload_kspace(path);
|
2002-07-09 12:24:59 +00:00
|
|
|
return id;
|
|
|
|
|
}
|
2002-07-16 07:54:56 +00:00
|
|
|
|