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.
|
|
|
|
|
*/
|
2002-09-30 21:59:09 +00:00
|
|
|
|
2002-07-09 12:24:59 +00:00
|
|
|
#include <kernel.h>
|
2003-05-03 16:33:09 +00:00
|
|
|
#include <boot/stage2.h>
|
2002-07-09 12:24:59 +00:00
|
|
|
#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
|
2002-09-30 21:59:09 +00:00
|
|
|
# include <arch/x86/console_dev.h>
|
2002-07-12 23:52:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <fb_console.h>
|
|
|
|
|
|
2002-07-09 12:24:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2002-07-25 02:01:01 +00:00
|
|
|
/* XXX - move this into the core
|
|
|
|
|
* 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-25 02:01:01 +00:00
|
|
|
"/boot/drivers/dev",
|
|
|
|
|
"/boot/drivers/dev/audio",
|
|
|
|
|
"/boot/drivers/dev/misc",
|
|
|
|
|
"/boot/drivers/dev/net",
|
2002-07-09 12:24:59 +00:00
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int dev_init(kernel_args *ka)
|
|
|
|
|
{
|
|
|
|
|
int fd;
|
|
|
|
|
const char **ptr;
|
|
|
|
|
|
|
|
|
|
dprintf("dev_init: entry\n");
|
|
|
|
|
|
2002-09-30 21:59:09 +00:00
|
|
|
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-09-30 21:59:09 +00:00
|
|
|
dprintf("loaded dev module!\n");
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-30 21:59:09 +00:00
|
|
|
|
|
|
|
|
image_id
|
|
|
|
|
dev_load_dev_module(const char *name, const char *dirpath)
|
2002-07-09 12:24:59 +00:00
|
|
|
{
|
2004-05-11 19:53:37 +00:00
|
|
|
image_id image;
|
2002-07-16 07:54:56 +00:00
|
|
|
char path[SYS_MAX_PATH_LEN];
|
2004-05-11 19:53:37 +00:00
|
|
|
uint32 *api_version = NULL;
|
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;
|
2004-05-11 19:53:37 +00:00
|
|
|
status_t status;
|
|
|
|
|
|
2002-07-09 12:24:59 +00:00
|
|
|
sprintf(path, "%s/%s", dirpath, name);
|
|
|
|
|
|
2002-07-17 12:10:38 +00:00
|
|
|
/* Load the module, return error if unable */
|
2004-05-11 19:53:37 +00:00
|
|
|
image = load_kernel_add_on(path);
|
|
|
|
|
if (image < 0)
|
|
|
|
|
return image;
|
2002-07-16 07:54:56 +00:00
|
|
|
|
2002-07-17 12:10:38 +00:00
|
|
|
/* Don't get all the symbols we will require initially. Just get the
|
|
|
|
|
* ones we need to check we have a valid module.
|
|
|
|
|
*/
|
2002-07-16 07:54:56 +00:00
|
|
|
|
2004-05-11 19:53:37 +00:00
|
|
|
// For a valid device driver the following exports are required
|
|
|
|
|
if (get_image_symbol(image, "publish_devices", B_SYMBOL_TYPE_TEXT, (void **)&publish_devices) != B_OK
|
|
|
|
|
|| get_image_symbol(image, "api_version", B_SYMBOL_TYPE_DATA, (void **)&api_version) != B_OK
|
|
|
|
|
|| get_image_symbol(image, "find_device", B_SYMBOL_TYPE_TEXT, (void **)&find_device) != B_OK) {
|
2002-07-16 07:54:56 +00:00
|
|
|
dprintf("DEV: %s: mandatory driver symbol(s) missing! :(\n", name);
|
2004-05-11 19:53:37 +00:00
|
|
|
status = EINVAL;
|
2002-07-16 07:54:56 +00:00
|
|
|
goto error;
|
2002-09-30 21:59:09 +00:00
|
|
|
}
|
2002-07-16 07:54:56 +00:00
|
|
|
|
2002-07-17 12:10:38 +00:00
|
|
|
/* Next, do we have an init_hardware function? If we do run it and
|
|
|
|
|
* if it fails simply fall through to the exit...
|
|
|
|
|
*/
|
2004-05-11 19:53:37 +00:00
|
|
|
if (get_image_symbol(image, "init_hardware", B_SYMBOL_TYPE_TEXT,
|
|
|
|
|
(void **)&init_hardware) == B_OK
|
|
|
|
|
&& init_hardware() != B_OK) {
|
2002-07-16 07:54:56 +00:00
|
|
|
dprintf("DEV: %s: init_hardware failed :(\n", name);
|
2004-05-11 19:53:37 +00:00
|
|
|
status = ENXIO;
|
2002-07-16 07:54:56 +00:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2002-07-17 12:10:38 +00:00
|
|
|
/* OK, so we now have what appears to be a valid module that has
|
|
|
|
|
* completed init_hardware and thus thinks it should be used.
|
2004-05-11 19:53:37 +00:00
|
|
|
* ToDo:
|
|
|
|
|
* - this is bogus!
|
|
|
|
|
* - the driver init routines should be called by devfs and
|
2002-07-17 12:10:38 +00:00
|
|
|
* only when the driver is first needed. However, that level
|
|
|
|
|
* level of support is not yet in devfs, so we have a hack
|
|
|
|
|
* here that calls the init_driver function at this point.
|
|
|
|
|
* As a result we will check to see if we actually manage to
|
|
|
|
|
* publish the device, and if we do we will keep the module
|
|
|
|
|
* loaded.
|
2004-05-11 19:53:37 +00:00
|
|
|
* - remove this when devfs is fixed!
|
2002-07-17 12:10:38 +00:00
|
|
|
*/
|
2004-05-11 19:53:37 +00:00
|
|
|
if (get_image_symbol(image, "init_driver", B_SYMBOL_TYPE_TEXT,
|
|
|
|
|
(void **)&init_driver) == B_OK
|
|
|
|
|
&& init_driver() != B_OK) {
|
2002-07-16 07:54:56 +00:00
|
|
|
dprintf("DEV: %s: init_driver failed :(\n", name);
|
2004-05-11 19:53:37 +00:00
|
|
|
status = ENXIO;
|
2002-07-16 07:54:56 +00:00
|
|
|
goto error;
|
2002-07-17 12:10:38 +00:00
|
|
|
}
|
2002-07-16 07:54:56 +00:00
|
|
|
|
2002-07-17 12:10:38 +00:00
|
|
|
/* Start of with the keep_loaded as false. If we have a successful
|
|
|
|
|
* call to devfs_publish_device we will set this to true.
|
|
|
|
|
*/
|
2002-07-16 07:54:56 +00:00
|
|
|
keep_loaded = false;
|
2002-07-17 12:10:38 +00:00
|
|
|
devfs_paths = publish_devices();
|
2002-07-16 07:54:56 +00:00
|
|
|
for (; *devfs_paths; devfs_paths++) {
|
2002-07-17 12:10:38 +00:00
|
|
|
device_hooks *hooks = find_device(*devfs_paths);
|
2002-07-16 07:54:56 +00:00
|
|
|
if (hooks) {
|
2002-07-17 12:10:38 +00:00
|
|
|
dprintf("DEV: %s: publishing %s with hooks %p\n",
|
|
|
|
|
name, *devfs_paths, hooks);
|
2002-07-16 07:54:56 +00:00
|
|
|
if (devfs_publish_device(*devfs_paths, NULL, hooks) == 0)
|
|
|
|
|
keep_loaded = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-09-30 21:59:09 +00:00
|
|
|
|
2002-07-17 12:10:38 +00:00
|
|
|
/* If we've managed to publish at least one of the device entry
|
|
|
|
|
* points that the driver wished us to then we MUST keep the driver in
|
|
|
|
|
* memory or the pointer that devfs has will become invalid.
|
|
|
|
|
* XXX - This is bogus, see above
|
|
|
|
|
*/
|
2002-07-16 07:54:56 +00:00
|
|
|
if (keep_loaded)
|
2004-05-11 19:53:37 +00:00
|
|
|
return image;
|
2002-09-30 21:59:09 +00:00
|
|
|
|
2002-07-17 22:07:37 +00:00
|
|
|
/* If the function gets here then the following has happenned...
|
2002-07-17 12:10:38 +00:00
|
|
|
* - the driver has been loaded
|
|
|
|
|
* - it has appeared valid
|
|
|
|
|
* - init_hardware has returned saying it should be used
|
|
|
|
|
* - init_driver has been run OK
|
2002-07-17 22:07:37 +00:00
|
|
|
* - publish_devices return empty paths list or
|
|
|
|
|
* devfs_publish_device has for some reason failed on each path.
|
2002-07-17 12:10:38 +00:00
|
|
|
*/
|
|
|
|
|
|
2004-05-11 19:53:37 +00:00
|
|
|
if (get_image_symbol(image, "uninit_driver", B_SYMBOL_TYPE_TEXT,
|
|
|
|
|
(void **)&uninit_driver) == B_OK)
|
2002-07-16 07:54:56 +00:00
|
|
|
uninit_driver();
|
2002-09-30 21:59:09 +00:00
|
|
|
|
|
|
|
|
// ToDo: That might not be the best error code
|
2004-05-11 19:53:37 +00:00
|
|
|
status = ENXIO;
|
2002-07-16 07:54:56 +00:00
|
|
|
|
|
|
|
|
error:
|
2002-07-17 12:10:38 +00:00
|
|
|
/* If we've gotten here then the driver will be unloaded and an
|
|
|
|
|
* error code returned.
|
|
|
|
|
*/
|
2004-05-11 19:53:37 +00:00
|
|
|
unload_kernel_add_on(image);
|
|
|
|
|
return status;
|
2002-07-09 12:24:59 +00:00
|
|
|
}
|
2002-07-16 07:54:56 +00:00
|
|
|
|
2002-12-07 00:25:50 +00:00
|
|
|
|
2003-01-27 13:51:46 +00:00
|
|
|
/* This is no longer part of the public kernel API, so we just export the symbol */
|
2003-02-11 19:34:12 +00:00
|
|
|
status_t load_driver_symbols(const char *driver_name);
|
|
|
|
|
status_t
|
2003-01-27 13:51:46 +00:00
|
|
|
load_driver_symbols(const char *driver_name)
|
2002-12-07 00:25:50 +00:00
|
|
|
{
|
2003-01-27 13:51:46 +00:00
|
|
|
// This will be globally done for the whole kernel via the settings file.
|
|
|
|
|
// We don't have to do anything here.
|
2002-12-07 00:25:50 +00:00
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|