<sys/socket.h>: turn off MSG_NOSIGNAL
[minix3.git] / drivers / usbd / base / usbd.c
blobf45929cba6ae7b90cacbef922d8dd4c1ac941d15
1 /*
2 * Entry point for USBD service, that handles USB HCDs
3 */
5 #include <ddekit/ddekit.h> /* ddekit_init */
6 #include <ddekit/thread.h> /* DDEKit threading */
8 #include <libdde/usb_server.h> /* DDEKit USB server */
10 #include <minix/devman.h> /* Initializing 'devman' */
11 #include <minix/sef.h> /* SEF handling */
13 #include <usb/usb_common.h>
14 #include <usb/usbd_interface.h>
17 /*===========================================================================*
18 * Local declarations *
19 *===========================================================================*/
20 static int usbd_sef_handler(int, sef_init_info_t *);
21 static void usbd_signal_handler(int);
22 static int usbd_start(void);
23 static void usbd_init(void);
24 static void usbd_server_thread(void *);
26 /* TODO: No headers for these... */
27 extern void ddekit_minix_wait_exit(void); /* dde.c */
28 extern void ddekit_shutdown(void); /* dde.c */
31 /*===========================================================================*
32 * main *
33 *===========================================================================*/
34 int
35 main(int UNUSED(argc), char * UNUSED(argv[]))
37 int ret_val;
39 USB_MSG("Starting USBD");
40 USB_DBG("Built: %s %s", __DATE__, __TIME__);
42 /* Basic SEF,DDE,... initialization */
43 usbd_init();
45 /* Assume failure unless usbd_start exits gracefully */
46 ret_val = EXIT_FAILURE;
48 /* USB host controllers initialization */
49 if (EXIT_SUCCESS == usbd_init_hcd()) {
51 /* Try initializing 'devman' */
52 if (EXIT_SUCCESS == devman_init()) {
54 /* Run USB driver (actually DDEKit threads)
55 * until this call returns: */
56 ret_val = usbd_start();
58 } else
59 USB_MSG("Initializing devman, failed");
61 /* Clean whatever was initialized */
62 usbd_deinit_hcd();
64 } else
65 USB_MSG("Initializing HCDs, failed");
67 return ret_val;
71 /*===========================================================================*
72 * usbd_sef_handler *
73 *===========================================================================*/
74 static int
75 usbd_sef_handler(int type, sef_init_info_t * UNUSED(info))
77 DEBUG_DUMP;
79 switch (type) {
80 case SEF_INIT_FRESH:
81 USB_MSG("Initializing");
82 return EXIT_SUCCESS;
84 case SEF_INIT_LU:
85 USB_MSG("Updating, not implemented");
86 return EXIT_FAILURE;
88 case SEF_INIT_RESTART:
89 USB_MSG("Restarting, not implemented");
90 return EXIT_FAILURE;
92 default:
93 USB_MSG("illegal SEF type");
94 return EXIT_FAILURE;
99 /*===========================================================================*
100 * usbd_signal_handler *
101 *===========================================================================*/
102 static void
103 usbd_signal_handler(int UNUSED(signo))
105 DEBUG_DUMP;
107 USB_MSG("Signal received, exiting USBD...");
109 /* Try graceful DDEKit exit */
110 ddekit_shutdown();
112 /* Unreachable, when ddekit_shutdown works correctly */
113 USB_ASSERT(0, "Calling ddekit_shutdown failed!");
117 /*===========================================================================*
118 * usbd_start *
119 *===========================================================================*/
120 static int
121 usbd_start(void)
123 ddekit_thread_t * usbd_th;
125 DEBUG_DUMP;
127 /* Driver's "main loop" is within DDEKit server thread */
128 usbd_th = ddekit_thread_create(usbd_server_thread, NULL, "USBD");
130 /* After spawning, allow server thread to work */
131 if (NULL != usbd_th) {
132 /* This will lock current thread until DDEKit exits */
133 ddekit_minix_wait_exit();
135 /* Cleanup */
136 ddekit_thread_terminate(usbd_th);
138 return EXIT_SUCCESS;
139 } else
140 return EXIT_FAILURE;
144 /*===========================================================================*
145 * usbd_init *
146 *===========================================================================*/
147 static void
148 usbd_init(void)
150 DEBUG_DUMP;
152 /* Set one handler for all messages */
153 sef_setcb_init_fresh(usbd_sef_handler);
154 sef_setcb_init_lu(usbd_sef_handler);
155 sef_setcb_init_restart(usbd_sef_handler);
156 sef_setcb_signal_handler(usbd_signal_handler);
158 /* Initialize DDEkit (involves sef_startup()) */
159 ddekit_init();
163 /*===========================================================================*
164 * usbd_server_thread *
165 *===========================================================================*/
166 static void
167 usbd_server_thread(void * UNUSED(unused))
169 DEBUG_DUMP;
171 ddekit_usb_server_init();