blob: 54a5935c1a2cd89489f262bd95a7e4dc5371e534 [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
Patrick Georgi482af6d2013-05-24 15:48:56 +0200245int
246generic_set_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;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000250
251 memset (&dr, 0, sizeof (dr));
252 dr.data_dir = host_to_device;
253 dr.req_type = standard_type;
254 dr.req_recp = dev_recp;
255 dr.bRequest = SET_ADDRESS;
256 dr.wValue = adr;
257 dr.wIndex = 0;
258 dr.wLength = 0;
259
Patrick Georgi4727c072008-10-16 19:20:51 +0000260 init_device_entry(controller, adr);
261 usbdev_t *dev = controller->devices[adr];
Patrick Georgid21f68b2008-09-02 16:06:22 +0000262 // dummy values for registering the address
263 dev->address = 0;
Nico Huber1ab60752012-05-23 09:21:54 +0200264 dev->hub = hubaddr;
265 dev->port = hubport;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000266 dev->speed = speed;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000267 dev->endpoints[0].dev = dev;
268 dev->endpoints[0].endpoint = 0;
269 dev->endpoints[0].maxpacketsize = 8;
270 dev->endpoints[0].toggle = 0;
271 dev->endpoints[0].direction = SETUP;
272 mdelay (50);
273 if (dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0)) {
Patrick Georgid21f68b2008-09-02 16:06:22 +0000274 return -1;
275 }
276 mdelay (50);
Patrick Georgi482af6d2013-05-24 15:48:56 +0200277
278 return adr;
279}
280
281static int
282set_address (hci_t *controller, int speed, int hubport, int hubaddr)
283{
284 int adr = controller->set_address(controller, speed, hubport, hubaddr);
285 if (adr < 0 || !controller->devices[adr]) {
286 usb_debug ("set_address failed\n");
287 return -1;
288 }
289 configuration_descriptor_t *cd;
290 device_descriptor_t *dd;
291
292 usbdev_t *dev = controller->devices[adr];
Patrick Georgid21f68b2008-09-02 16:06:22 +0000293 dev->address = adr;
Patrick Georgi482af6d2013-05-24 15:48:56 +0200294 dev->hub = hubaddr;
295 dev->port = hubport;
296 dev->speed = speed;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000297 dev->descriptor = get_descriptor (dev, gen_bmRequestType
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000298 (device_to_host, standard_type, dev_recp), 1, 0, 0);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000299 dd = (device_descriptor_t *) dev->descriptor;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000300
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700301 usb_debug ("* found device (0x%04x:0x%04x, USB %x.%x)",
Stefan Reinauer14e22772010-04-27 06:56:47 +0000302 dd->idVendor, dd->idProduct,
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000303 dd->bcdUSB >> 8, dd->bcdUSB & 0xff);
304 dev->quirks = usb_quirk_check(dd->idVendor, dd->idProduct);
305
Gabe Black93ded592012-11-01 15:44:10 -0700306 usb_debug ("\ndevice has %x configurations\n", dd->bNumConfigurations);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000307 if (dd->bNumConfigurations == 0) {
308 /* device isn't usable */
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700309 usb_debug ("... no usable configuration!\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000310 dev->address = 0;
311 return -1;
312 }
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000313
314 dev->configuration = get_descriptor (dev, gen_bmRequestType
315 (device_to_host, standard_type, dev_recp), 2, 0, 0);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000316 cd = (configuration_descriptor_t *) dev->configuration;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000317 interface_descriptor_t *interface =
318 (interface_descriptor_t *) (((char *) cd) + cd->bLength);
319 {
320 int i;
321 int num = cd->bNumInterfaces;
322 interface_descriptor_t *current = interface;
Gabe Black93ded592012-11-01 15:44:10 -0700323 usb_debug ("device has %x interfaces\n", num);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000324 if (num > 1) {
325 int interfaces = usb_interface_check(dd->idVendor, dd->idProduct);
326 if (interfaces) {
327 /* Well known device, don't warn */
328 num = interfaces;
329 } else {
330
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700331 usb_debug ("\nNOTICE: This driver defaults to using the first interface.\n"
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000332 "This might be the wrong choice and lead to limited functionality\n"
333 "of the device. Please report such a case to coreboot@coreboot.org\n"
334 "as you might be the first.\n");
335 /* we limit to the first interface, as there was no need to
336 * implement something else for the time being. If you need
337 * it, see the SetInterface and GetInterface functions in
338 * the USB specification, and adapt appropriately.
339 */
340 num = (num > 1) ? 1 : num;
341 }
342 }
Patrick Georgid21f68b2008-09-02 16:06:22 +0000343 for (i = 0; i < num; i++) {
344 int j;
Gabe Black93ded592012-11-01 15:44:10 -0700345 usb_debug (" #%x has %x endpoints, interface %x:%x, protocol %x\n",
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000346 current->bInterfaceNumber, current->bNumEndpoints, current->bInterfaceClass, current->bInterfaceSubClass, current->bInterfaceProtocol);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000347 endpoint_descriptor_t *endp =
348 (endpoint_descriptor_t *) (((char *) current)
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000349 + current->bLength);
Nico Huber735f55c2013-06-10 13:43:29 +0200350 /* Skip any non-endpoint descriptor */
351 if (endp->bDescriptorType != 0x05)
352 endp = (endpoint_descriptor_t *)(((char *)endp) + ((char *)endp)[0]);
353
Patrick Georgid21f68b2008-09-02 16:06:22 +0000354 memset (dev->endpoints, 0, sizeof (dev->endpoints));
355 dev->num_endp = 1; // 0 always exists
356 dev->endpoints[0].dev = dev;
357 dev->endpoints[0].maxpacketsize = dd->bMaxPacketSize0;
358 dev->endpoints[0].direction = SETUP;
359 dev->endpoints[0].type = CONTROL;
360 for (j = 1; j <= current->bNumEndpoints; j++) {
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000361#ifdef USB_DEBUG
362 static const char *transfertypes[4] = {
363 "control", "isochronous", "bulk", "interrupt"
Patrick Georgid21f68b2008-09-02 16:06:22 +0000364 };
Gabe Black93ded592012-11-01 15:44:10 -0700365 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 +0000366#endif
Patrick Georgid21f68b2008-09-02 16:06:22 +0000367 endpoint_t *ep =
368 &dev->endpoints[dev->num_endp++];
369 ep->dev = dev;
370 ep->endpoint = endp->bEndpointAddress;
371 ep->toggle = 0;
372 ep->maxpacketsize = endp->wMaxPacketSize;
373 ep->direction =
374 ((endp->bEndpointAddress & 0x80) ==
375 0) ? OUT : IN;
376 ep->type = endp->bmAttributes;
377 endp = (endpoint_descriptor_t
378 *) (((char *) endp) + endp->bLength);
379 }
380 current = (interface_descriptor_t *) endp;
381 }
382 }
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000383
Patrick Georgi482af6d2013-05-24 15:48:56 +0200384 if (controller->finish_device_config &&
385 controller->finish_device_config(dev))
386 return adr; /* Device isn't configured correctly,
387 only control transfers may work. */
388
389 set_configuration(dev);
390
Patrick Georgid21f68b2008-09-02 16:06:22 +0000391 int class = dd->bDeviceClass;
392 if (class == 0)
393 class = interface->bInterfaceClass;
394
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000395 enum {
396 audio_device = 0x01,
397 comm_device = 0x02,
398 hid_device = 0x03,
399 physical_device = 0x05,
400 imaging_device = 0x06,
401 printer_device = 0x07,
402 msc_device = 0x08,
403 hub_device = 0x09,
404 cdc_device = 0x0a,
405 ccid_device = 0x0b,
406 security_device = 0x0d,
407 video_device = 0x0e,
408 healthcare_device = 0x0f,
409 diagnostic_device = 0xdc,
410 wireless_device = 0xe0,
411 misc_device = 0xef,
412 };
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700413 usb_debug(", class: ");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000414 switch (class) {
415 case audio_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700416 usb_debug("audio\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000417 break;
418 case comm_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700419 usb_debug("communication\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000420 break;
421 case hid_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700422 usb_debug ("HID\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000423#ifdef CONFIG_USB_HID
Patrick Georgi4727c072008-10-16 19:20:51 +0000424 controller->devices[adr]->init = usb_hid_init;
Gabe Blackdc9e77f2013-02-01 20:19:27 -0800425 return adr;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000426#else
Gabe Black93ded592012-11-01 15:44:10 -0700427 usb_debug ("NOTICE: USB HID support not compiled in\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000428#endif
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000429 break;
430 case physical_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700431 usb_debug("physical\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000432 break;
433 case imaging_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700434 usb_debug("camera\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000435 break;
436 case printer_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700437 usb_debug("printer\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000438 break;
439 case msc_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700440 usb_debug ("MSC\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000441#ifdef CONFIG_USB_MSC
Patrick Georgi4727c072008-10-16 19:20:51 +0000442 controller->devices[adr]->init = usb_msc_init;
Gabe Blackdc9e77f2013-02-01 20:19:27 -0800443 return adr;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000444#else
Gabe Black93ded592012-11-01 15:44:10 -0700445 usb_debug ("NOTICE: USB MSC support not compiled in\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000446#endif
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000447 break;
448 case hub_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700449 usb_debug ("hub\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000450#ifdef CONFIG_USB_HUB
451 controller->devices[adr]->init = usb_hub_init;
Gabe Blackdc9e77f2013-02-01 20:19:27 -0800452 return adr;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000453#else
Gabe Black93ded592012-11-01 15:44:10 -0700454 usb_debug ("NOTICE: USB hub support not compiled in.\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000455#endif
456 break;
457 case cdc_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700458 usb_debug("CDC\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000459 break;
460 case ccid_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700461 usb_debug("smartcard / CCID\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000462 break;
463 case security_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700464 usb_debug("content security\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000465 break;
466 case video_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700467 usb_debug("video\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000468 break;
469 case healthcare_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700470 usb_debug("healthcare\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000471 break;
472 case diagnostic_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700473 usb_debug("diagnostic\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000474 break;
475 case wireless_device:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700476 usb_debug("wireless\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000477 break;
478 default:
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700479 usb_debug("unsupported class %x\n", class);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000480 break;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000481 }
Gabe Blackdc9e77f2013-02-01 20:19:27 -0800482 controller->devices[adr]->init = usb_generic_init;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000483 return adr;
484}
Patrick Georgi4727c072008-10-16 19:20:51 +0000485
Nico Huber79e1f2f2012-06-01 09:50:11 +0200486/*
487 * Should be called by the hub drivers whenever a physical detach occurs
488 * and can be called by usb class drivers if they are unsatisfied with a
489 * malfunctioning device.
490 */
Patrick Georgi4727c072008-10-16 19:20:51 +0000491void
492usb_detach_device(hci_t *controller, int devno)
493{
Nico Huber79e1f2f2012-06-01 09:50:11 +0200494 /* check if device exists, as we may have
495 been called yet by the usb class driver */
496 if (controller->devices[devno]) {
497 controller->devices[devno]->destroy (controller->devices[devno]);
498 free(controller->devices[devno]);
499 controller->devices[devno] = NULL;
Patrick Georgi482af6d2013-05-24 15:48:56 +0200500 if (controller->destroy_device)
501 controller->destroy_device(controller, devno);
Nico Huber79e1f2f2012-06-01 09:50:11 +0200502 }
Patrick Georgi4727c072008-10-16 19:20:51 +0000503}
504
505int
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000506usb_attach_device(hci_t *controller, int hubaddress, int port, int speed)
Patrick Georgi4727c072008-10-16 19:20:51 +0000507{
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000508 static const char* speeds[] = { "full", "low", "high" };
Gabe Black93ded592012-11-01 15:44:10 -0700509 usb_debug ("%sspeed device\n", (speed <= 2) ? speeds[speed] : "invalid value - no");
Nico Huber1ab60752012-05-23 09:21:54 +0200510 int newdev = set_address (controller, speed, port, hubaddress);
Patrick Georgi4727c072008-10-16 19:20:51 +0000511 if (newdev == -1)
512 return -1;
513 usbdev_t *newdev_t = controller->devices[newdev];
Patrick Georgi4727c072008-10-16 19:20:51 +0000514 // determine responsible driver - current done in set_address
515 newdev_t->init (newdev_t);
Nico Huberd8a66802012-06-21 11:21:23 +0200516 /* init() may have called usb_detach_device() yet, so check */
517 return controller->devices[newdev] ? newdev : -1;
Patrick Georgi4727c072008-10-16 19:20:51 +0000518}
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000519
Gabe Blackdc9e77f2013-02-01 20:19:27 -0800520static void
521usb_generic_destroy (usbdev_t *dev)
522{
523 if (usb_generic_remove)
524 usb_generic_remove(dev);
525}
526
527void
528usb_generic_init (usbdev_t *dev)
529{
530 dev->data = NULL;
531 dev->destroy = usb_generic_destroy;
532
533 if (usb_generic_create)
534 usb_generic_create(dev);
535}