blob: 23561c40aab132466109eb896041f5d6d55eb772 [file] [log] [blame]
Patrick Georgid21f68b2008-09-02 16:06:22 +00001/*
2 * This file is part of the libpayload project.
3 *
Stefan Reinauerb56f2d02010-03-25 22:17:36 +00004 * Copyright (C) 2008-2010 coresystems GmbH
Patrick Georgid21f68b2008-09-02 16:06:22 +00005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
Stefan Reinauerb56f2d02010-03-25 22:17:36 +000030//#define USB_DEBUG
31
Jordan Crousedb8c0ab2008-11-24 17:54:46 +000032#include <libpayload-config.h>
Jordan Crouse29061a52008-09-11 17:29:00 +000033#include <usb/usb.h>
Patrick Georgid21f68b2008-09-02 16:06:22 +000034
35hci_t *usb_hcs = 0;
36
37hci_t *
Stefan Reinauer5fe6e232009-07-31 11:39:55 +000038new_controller (void)
Patrick Georgid21f68b2008-09-02 16:06:22 +000039{
40 hci_t *controller = malloc (sizeof (hci_t));
41
Stefan Reinauer5fe6e232009-07-31 11:39:55 +000042 if (controller) {
43 /* atomic */
44 controller->next = usb_hcs;
45 usb_hcs = controller;
46 /* atomic end */
47 }
Patrick Georgid21f68b2008-09-02 16:06:22 +000048
49 return controller;
50}
51
52void
53detach_controller (hci_t *controller)
54{
Stefan Reinauer5fe6e232009-07-31 11:39:55 +000055 if (controller == NULL)
Patrick Georgid21f68b2008-09-02 16:06:22 +000056 return;
57 if (usb_hcs == controller) {
58 usb_hcs = controller->next;
59 } else {
60 hci_t *it = usb_hcs;
Stefan Reinauer5fe6e232009-07-31 11:39:55 +000061 while (it != NULL) {
Patrick Georgid21f68b2008-09-02 16:06:22 +000062 if (it->next == controller) {
63 it->next = controller->next;
64 return;
65 }
Anton Kochkov421303a2012-06-20 03:57:18 +040066 it = it->next;
Patrick Georgid21f68b2008-09-02 16:06:22 +000067 }
68 }
69}
70
71/**
Patrick Georgibbc52312011-11-04 12:06:06 +010072 * Shut down all controllers
73 */
74int
75usb_exit (void)
76{
Mathias Krause59c020a2013-03-24 19:40:02 +010077 while (usb_hcs != NULL) {
78 usb_hcs->shutdown(usb_hcs);
Patrick Georgibbc52312011-11-04 12:06:06 +010079 }
80 return 0;
81}
82
83/**
Patrick Georgid21f68b2008-09-02 16:06:22 +000084 * Polls all hubs on all USB controllers, to find out about device changes
85 */
86void
Stefan Reinauerb56f2d02010-03-25 22:17:36 +000087usb_poll (void)
Patrick Georgid21f68b2008-09-02 16:06:22 +000088{
89 if (usb_hcs == 0)
90 return;
91 hci_t *controller = usb_hcs;
Patrick Georgidb89ec92011-11-18 11:56:38 +010092 while (controller != NULL) {
Patrick Georgid21f68b2008-09-02 16:06:22 +000093 int i;
94 for (i = 0; i < 128; i++) {
Patrick Georgi4727c072008-10-16 19:20:51 +000095 if (controller->devices[i] != 0) {
Stefan Reinauerb56f2d02010-03-25 22:17:36 +000096 controller->devices[i]->poll (controller->devices[i]);
Patrick Georgid21f68b2008-09-02 16:06:22 +000097 }
98 }
99 controller = controller->next;
100 }
101}
102
103void
104init_device_entry (hci_t *controller, int i)
105{
Patrick Georgi4727c072008-10-16 19:20:51 +0000106 if (controller->devices[i] != 0)
Gabe Black93ded592012-11-01 15:44:10 -0700107 usb_debug("warning: device %d reassigned?\n", i);
Patrick Georgi4727c072008-10-16 19:20:51 +0000108 controller->devices[i] = malloc(sizeof(usbdev_t));
109 controller->devices[i]->controller = controller;
110 controller->devices[i]->address = -1;
111 controller->devices[i]->hub = -1;
112 controller->devices[i]->port = -1;
113 controller->devices[i]->init = usb_nop_init;
114 controller->devices[i]->init (controller->devices[i]);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000115}
116
117void
118set_feature (usbdev_t *dev, int endp, int feature, int rtype)
119{
120 dev_req_t dr;
121
122 dr.bmRequestType = rtype;
123 dr.data_dir = host_to_device;
124 dr.bRequest = SET_FEATURE;
125 dr.wValue = feature;
126 dr.wIndex = endp;
127 dr.wLength = 0;
128 dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0);
129}
130
131void
132get_status (usbdev_t *dev, int intf, int rtype, int len, void *data)
133{
134 dev_req_t dr;
135
136 dr.bmRequestType = rtype;
137 dr.data_dir = device_to_host;
138 dr.bRequest = GET_STATUS;
139 dr.wValue = 0;
140 dr.wIndex = intf;
141 dr.wLength = len;
142 dev->controller->control (dev, IN, sizeof (dr), &dr, len, data);
143}
144
145u8 *
146get_descriptor (usbdev_t *dev, unsigned char bmRequestType, int descType,
147 int descIdx, int langID)
148{
149 u8 buf[8];
150 u8 *result;
151 dev_req_t dr;
152 int size;
153
154 dr.bmRequestType = bmRequestType;
155 dr.data_dir = device_to_host; // always like this for descriptors
156 dr.bRequest = GET_DESCRIPTOR;
157 dr.wValue = (descType << 8) | descIdx;
158 dr.wIndex = langID;
159 dr.wLength = 8;
160 if (dev->controller->control (dev, IN, sizeof (dr), &dr, 8, buf)) {
Gabe Black93ded592012-11-01 15:44:10 -0700161 usb_debug ("getting descriptor size (type %x) failed\n",
Patrick Georgid21f68b2008-09-02 16:06:22 +0000162 descType);
163 }
164
165 if (descType == 1) {
166 device_descriptor_t *dd = (device_descriptor_t *) buf;
Gabe Black93ded592012-11-01 15:44:10 -0700167 usb_debug ("maxPacketSize0: %x\n", dd->bMaxPacketSize0);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000168 if (dd->bMaxPacketSize0 != 0)
169 dev->endpoints[0].maxpacketsize = dd->bMaxPacketSize0;
170 }
171
172 /* special case for configuration descriptors: they carry all their
173 subsequent descriptors with them, and keep the entire size at a
174 different location */
175 size = buf[0];
176 if (buf[1] == 2) {
177 int realsize = ((unsigned short *) (buf + 2))[0];
178 size = realsize;
179 }
180 result = malloc (size);
181 memset (result, 0, size);
182 dr.wLength = size;
183 if (dev->controller->
184 control (dev, IN, sizeof (dr), &dr, size, result)) {
Gabe Black93ded592012-11-01 15:44:10 -0700185 usb_debug ("getting descriptor (type %x, size %x) failed\n",
Patrick Georgid21f68b2008-09-02 16:06:22 +0000186 descType, size);
187 }
188
189 return result;
190}
191
192void
193set_configuration (usbdev_t *dev)
194{
195 dev_req_t dr;
196
197 dr.bmRequestType = 0;
198 dr.bRequest = SET_CONFIGURATION;
199 dr.wValue = dev->configuration[5];
200 dr.wIndex = 0;
201 dr.wLength = 0;
202 dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0);
203}
204
Nico Huber79e1f2f2012-06-01 09:50:11 +0200205int
Nico Huber5f595cb2012-05-21 16:19:05 +0200206clear_feature (usbdev_t *dev, int endp, int feature, int rtype)
207{
208 dev_req_t dr;
209
210 dr.bmRequestType = rtype;
211 dr.data_dir = host_to_device;
212 dr.bRequest = CLEAR_FEATURE;
213 dr.wValue = feature;
214 dr.wIndex = endp;
215 dr.wLength = 0;
Nico Huber79e1f2f2012-06-01 09:50:11 +0200216 return dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0);
Nico Huber5f595cb2012-05-21 16:19:05 +0200217}
218
Patrick Georgid21f68b2008-09-02 16:06:22 +0000219int
220clear_stall (endpoint_t *ep)
221{
222 usbdev_t *dev = ep->dev;
223 int endp = ep->endpoint;
Nico Huber5f595cb2012-05-21 16:19:05 +0200224 int rtype = gen_bmRequestType (host_to_device, standard_type,
225 endp ? endp_recp : dev_recp);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000226
Nico Huber79e1f2f2012-06-01 09:50:11 +0200227 int ret = clear_feature (dev, endp, ENDPOINT_HALT, rtype);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000228 ep->toggle = 0;
Nico Huber79e1f2f2012-06-01 09:50:11 +0200229 return ret;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000230}
231
232/* returns free address or -1 */
233static int
234get_free_address (hci_t *controller)
235{
236 int i;
237 for (i = 1; i < 128; i++) {
Patrick Georgi4727c072008-10-16 19:20:51 +0000238 if (controller->devices[i] == 0)
Patrick Georgid21f68b2008-09-02 16:06:22 +0000239 return i;
240 }
Gabe Black93ded592012-11-01 15:44:10 -0700241 usb_debug ("no free address found\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000242 return -1; // no free address
243}
244
Nico Huber1ab60752012-05-23 09:21:54 +0200245static int
246set_address (hci_t *controller, int speed, int hubport, int hubaddr)
Patrick Georgid21f68b2008-09-02 16:06:22 +0000247{
248 int adr = get_free_address (controller); // address to set
249 dev_req_t dr;
250 configuration_descriptor_t *cd;
251 device_descriptor_t *dd;
252
253 memset (&dr, 0, sizeof (dr));
254 dr.data_dir = host_to_device;
255 dr.req_type = standard_type;
256 dr.req_recp = dev_recp;
257 dr.bRequest = SET_ADDRESS;
258 dr.wValue = adr;
259 dr.wIndex = 0;
260 dr.wLength = 0;
261
Patrick Georgi4727c072008-10-16 19:20:51 +0000262 init_device_entry(controller, adr);
263 usbdev_t *dev = controller->devices[adr];
Patrick Georgid21f68b2008-09-02 16:06:22 +0000264 // dummy values for registering the address
265 dev->address = 0;
Nico Huber1ab60752012-05-23 09:21:54 +0200266 dev->hub = hubaddr;
267 dev->port = hubport;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000268 dev->speed = speed;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000269 dev->endpoints[0].dev = dev;
270 dev->endpoints[0].endpoint = 0;
271 dev->endpoints[0].maxpacketsize = 8;
272 dev->endpoints[0].toggle = 0;
273 dev->endpoints[0].direction = SETUP;
274 mdelay (50);
275 if (dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0)) {
Gabe Black93ded592012-11-01 15:44:10 -0700276 usb_debug ("set_address failed\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000277 return -1;
278 }
279 mdelay (50);
280 dev->address = adr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000281 dev->descriptor = get_descriptor (dev, gen_bmRequestType
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000282 (device_to_host, standard_type, dev_recp), 1, 0, 0);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000283 dd = (device_descriptor_t *) dev->descriptor;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000284
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700285 usb_debug ("* found device (0x%04x:0x%04x, USB %x.%x)",
Stefan Reinauer14e22772010-04-27 06:56:47 +0000286 dd->idVendor, dd->idProduct,
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000287 dd->bcdUSB >> 8, dd->bcdUSB & 0xff);
288 dev->quirks = usb_quirk_check(dd->idVendor, dd->idProduct);
289
Gabe Black93ded592012-11-01 15:44:10 -0700290 usb_debug ("\ndevice has %x configurations\n", dd->bNumConfigurations);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000291 if (dd->bNumConfigurations == 0) {
292 /* device isn't usable */
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700293 usb_debug ("... no usable configuration!\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000294 dev->address = 0;
295 return -1;
296 }
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000297
298 dev->configuration = get_descriptor (dev, gen_bmRequestType
299 (device_to_host, standard_type, dev_recp), 2, 0, 0);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000300 cd = (configuration_descriptor_t *) dev->configuration;
301 set_configuration (dev);
302 interface_descriptor_t *interface =
303 (interface_descriptor_t *) (((char *) cd) + cd->bLength);
304 {
305 int i;
306 int num = cd->bNumInterfaces;
307 interface_descriptor_t *current = interface;
Gabe Black93ded592012-11-01 15:44:10 -0700308 usb_debug ("device has %x interfaces\n", num);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000309 if (num > 1) {
310 int interfaces = usb_interface_check(dd->idVendor, dd->idProduct);
311 if (interfaces) {
312 /* Well known device, don't warn */
313 num = interfaces;
314 } else {
315
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700316 usb_debug ("\nNOTICE: This driver defaults to using the first interface.\n"
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000317 "This might be the wrong choice and lead to limited functionality\n"
318 "of the device. Please report such a case to coreboot@coreboot.org\n"
319 "as you might be the first.\n");
320 /* we limit to the first interface, as there was no need to
321 * implement something else for the time being. If you need
322 * it, see the SetInterface and GetInterface functions in
323 * the USB specification, and adapt appropriately.
324 */
325 num = (num > 1) ? 1 : num;
326 }
327 }
Patrick Georgid21f68b2008-09-02 16:06:22 +0000328 for (i = 0; i < num; i++) {
329 int j;
Gabe Black93ded592012-11-01 15:44:10 -0700330 usb_debug (" #%x has %x endpoints, interface %x:%x, protocol %x\n",
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000331 current->bInterfaceNumber, current->bNumEndpoints, current->bInterfaceClass, current->bInterfaceSubClass, current->bInterfaceProtocol);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000332 endpoint_descriptor_t *endp =
333 (endpoint_descriptor_t *) (((char *) current)
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000334 + current->bLength);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000335 if (interface->bInterfaceClass == 0x3)
336 endp = (endpoint_descriptor_t *) (((char *) endp) + ((char *) endp)[0]); // ignore HID descriptor
337 memset (dev->endpoints, 0, sizeof (dev->endpoints));
338 dev->num_endp = 1; // 0 always exists
339 dev->endpoints[0].dev = dev;
340 dev->endpoints[0].maxpacketsize = dd->bMaxPacketSize0;
341 dev->endpoints[0].direction = SETUP;
342 dev->endpoints[0].type = CONTROL;
343 for (j = 1; j <= current->bNumEndpoints; j++) {
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000344#ifdef USB_DEBUG
345 static const char *transfertypes[4] = {
346 "control", "isochronous", "bulk", "interrupt"
Patrick Georgid21f68b2008-09-02 16:06:22 +0000347 };
Gabe Black93ded592012-11-01 15:44:10 -0700348 usb_debug (" #%x: Endpoint %x (%s), max packet size %x, type %s\n", j, endp->bEndpointAddress & 0x7f, ((endp->bEndpointAddress & 0x80) != 0) ? "in" : "out", endp->wMaxPacketSize, transfertypes[endp->bmAttributes]);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000349#endif
Patrick Georgid21f68b2008-09-02 16:06:22 +0000350 endpoint_t *ep =
351 &dev->endpoints[dev->num_endp++];
352 ep->dev = dev;
353 ep->endpoint = endp->bEndpointAddress;
354 ep->toggle = 0;
355 ep->maxpacketsize = endp->wMaxPacketSize;
356 ep->direction =
357 ((endp->bEndpointAddress & 0x80) ==
358 0) ? OUT : IN;
359 ep->type = endp->bmAttributes;
360 endp = (endpoint_descriptor_t
361 *) (((char *) endp) + endp->bLength);
362 }
363 current = (interface_descriptor_t *) endp;
364 }
365 }
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000366
Patrick Georgid21f68b2008-09-02 16:06:22 +0000367 int class = dd->bDeviceClass;
368 if (class == 0)
369 class = interface->bInterfaceClass;
370
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000371 enum {
372 audio_device = 0x01,
373 comm_device = 0x02,
374 hid_device = 0x03,
375 physical_device = 0x05,
376 imaging_device = 0x06,
377 printer_device = 0x07,
378 msc_device = 0x08,
379 hub_device = 0x09,
380 cdc_device = 0x0a,
381 ccid_device = 0x0b,
382 security_device = 0x0d,
383 video_device = 0x0e,
384 healthcare_device = 0x0f,
385 diagnostic_device = 0xdc,
386 wireless_device = 0xe0,
387 misc_device = 0xef,
388 };
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700389 usb_debug(", class: ");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000390 switch (class) {
391 case audio_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700392 usb_debug("audio\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000393 break;
394 case comm_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700395 usb_debug("communication\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000396 break;
397 case hid_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700398 usb_debug ("HID\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000399#ifdef CONFIG_USB_HID
Patrick Georgi4727c072008-10-16 19:20:51 +0000400 controller->devices[adr]->init = usb_hid_init;
Gabe Blackdc9e77f2013-02-01 20:19:27 -0800401 return adr;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000402#else
Gabe Black93ded592012-11-01 15:44:10 -0700403 usb_debug ("NOTICE: USB HID support not compiled in\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000404#endif
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000405 break;
406 case physical_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700407 usb_debug("physical\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000408 break;
409 case imaging_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700410 usb_debug("camera\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000411 break;
412 case printer_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700413 usb_debug("printer\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000414 break;
415 case msc_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700416 usb_debug ("MSC\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000417#ifdef CONFIG_USB_MSC
Patrick Georgi4727c072008-10-16 19:20:51 +0000418 controller->devices[adr]->init = usb_msc_init;
Gabe Blackdc9e77f2013-02-01 20:19:27 -0800419 return adr;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000420#else
Gabe Black93ded592012-11-01 15:44:10 -0700421 usb_debug ("NOTICE: USB MSC support not compiled in\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000422#endif
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000423 break;
424 case hub_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700425 usb_debug ("hub\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000426#ifdef CONFIG_USB_HUB
427 controller->devices[adr]->init = usb_hub_init;
Gabe Blackdc9e77f2013-02-01 20:19:27 -0800428 return adr;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000429#else
Gabe Black93ded592012-11-01 15:44:10 -0700430 usb_debug ("NOTICE: USB hub support not compiled in.\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000431#endif
432 break;
433 case cdc_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700434 usb_debug("CDC\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000435 break;
436 case ccid_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700437 usb_debug("smartcard / CCID\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000438 break;
439 case security_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700440 usb_debug("content security\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000441 break;
442 case video_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700443 usb_debug("video\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000444 break;
445 case healthcare_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700446 usb_debug("healthcare\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000447 break;
448 case diagnostic_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700449 usb_debug("diagnostic\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000450 break;
451 case wireless_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700452 usb_debug("wireless\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000453 break;
454 default:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700455 usb_debug("unsupported class %x\n", class);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000456 break;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000457 }
Gabe Blackdc9e77f2013-02-01 20:19:27 -0800458 controller->devices[adr]->init = usb_generic_init;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000459 return adr;
460}
Patrick Georgi4727c072008-10-16 19:20:51 +0000461
Nico Huber79e1f2f2012-06-01 09:50:11 +0200462/*
463 * Should be called by the hub drivers whenever a physical detach occurs
464 * and can be called by usb class drivers if they are unsatisfied with a
465 * malfunctioning device.
466 */
Patrick Georgi4727c072008-10-16 19:20:51 +0000467void
468usb_detach_device(hci_t *controller, int devno)
469{
Nico Huber79e1f2f2012-06-01 09:50:11 +0200470 /* check if device exists, as we may have
471 been called yet by the usb class driver */
472 if (controller->devices[devno]) {
473 controller->devices[devno]->destroy (controller->devices[devno]);
474 free(controller->devices[devno]);
475 controller->devices[devno] = NULL;
476 }
Patrick Georgi4727c072008-10-16 19:20:51 +0000477}
478
479int
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000480usb_attach_device(hci_t *controller, int hubaddress, int port, int speed)
Patrick Georgi4727c072008-10-16 19:20:51 +0000481{
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000482 static const char* speeds[] = { "full", "low", "high" };
Gabe Black93ded592012-11-01 15:44:10 -0700483 usb_debug ("%sspeed device\n", (speed <= 2) ? speeds[speed] : "invalid value - no");
Nico Huber1ab60752012-05-23 09:21:54 +0200484 int newdev = set_address (controller, speed, port, hubaddress);
Patrick Georgi4727c072008-10-16 19:20:51 +0000485 if (newdev == -1)
486 return -1;
487 usbdev_t *newdev_t = controller->devices[newdev];
Patrick Georgi4727c072008-10-16 19:20:51 +0000488 // determine responsible driver - current done in set_address
489 newdev_t->init (newdev_t);
Nico Huberd8a66802012-06-21 11:21:23 +0200490 /* init() may have called usb_detach_device() yet, so check */
491 return controller->devices[newdev] ? newdev : -1;
Patrick Georgi4727c072008-10-16 19:20:51 +0000492}
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000493
Gabe Blackdc9e77f2013-02-01 20:19:27 -0800494static void
495usb_generic_destroy (usbdev_t *dev)
496{
497 if (usb_generic_remove)
498 usb_generic_remove(dev);
499}
500
501void
502usb_generic_init (usbdev_t *dev)
503{
504 dev->data = NULL;
505 dev->destroy = usb_generic_destroy;
506
507 if (usb_generic_create)
508 usb_generic_create(dev);
509}