work in progress of ochi + usb bus manager
[newos.git] / kernel / addons / modules / bus_managers / usb / usb.c
blob0ee0671b0e4047317c65dffbd64b10284bdae683
1 /*
2 ** Copyright 2003, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
6 #include <kernel/kernel.h>
7 #include <kernel/debug.h>
8 #include <kernel/lock.h>
9 #include <kernel/module.h>
10 #include <kernel/heap.h>
11 #include <kernel/thread.h>
12 #include <string.h>
14 #include <kernel/bus/usb/usb.h>
15 #include <kernel/bus/usb/usb_hc.h>
16 #include "usb_priv.h"
18 #define debug_level_flow 10
19 #define debug_level_error 10
20 #define debug_level_info 10
22 #define DEBUG_MSG_PREFIX "USB - "
24 #include <kernel/debug_ext.h>
26 usb_bus *usb;
28 static int
29 hc_init_callback(void *hooks, void *hc_cookie)
31 usb_hc *hc;
33 SHOW_FLOW(1, "cookie %p", hc_cookie);
35 hc = (usb_hc *)kmalloc(sizeof(usb_hc));
36 if(!hc)
37 return ERR_NO_MEMORY;
39 memset(hc, 0, sizeof(*hc));
41 hc->hooks = hooks;
42 hc->hc_cookie = hc_cookie;
44 // add it to the list of host controllers
45 hc->next = usb->hc_list;
46 usb->hc_list = hc;
48 // create an enumerator thread
49 hc->enumerator_thread = thread_create_kernel_thread("usb bus enumerator", &usb_enumerator_thread, hc);
50 thread_set_priority(hc->enumerator_thread, THREAD_MIN_RT_PRIORITY);
51 thread_resume_thread(hc->enumerator_thread);
53 return NO_ERROR;
56 static int
57 load_hc_modules()
59 char module_name[SYS_MAX_PATH_LEN];
60 size_t bufsize;
61 modules_cookie cookie;
62 struct usb_hc_module_hooks *hooks;
63 int err;
64 int count = 0;
66 // scan through the host controller module dir and load all of them
67 cookie = module_open_list(USB_HC_MODULE_NAME_PREFIX);
68 bufsize = sizeof(module_name);
69 while(read_next_module_name(cookie, module_name, &bufsize) >= NO_ERROR) {
70 bufsize = sizeof(module_name); // reset this for the next iteration
72 err = module_get(module_name, 0, (void **)&hooks);
73 if(err < 0)
74 continue;
76 err = hooks->init_hc(&hc_init_callback, hooks);
77 if(err < 0) {
78 module_put(module_name); // it failed, put it away
81 count++;
83 close_module_list(cookie);
85 return count;
88 static int
89 usb_module_init(void)
91 usb = kmalloc(sizeof(usb_bus));
92 if(!usb)
93 return ERR_NO_MEMORY;
95 load_hc_modules();
97 return NO_ERROR;
100 static int
101 usb_module_uninit(void)
103 return NO_ERROR;
106 static struct usb_module_hooks usb_hooks = {
107 NULL,
110 static module_header usb_module_header = {
111 USB_BUS_MODULE_NAME,
112 MODULE_CURR_VERSION,
113 MODULE_KEEP_LOADED,
114 &usb_hooks,
115 &usb_module_init,
116 &usb_module_uninit
119 module_header *modules[] = {
120 &usb_module_header,
121 NULL