blob: 454e2d45d47727411bbcf127a525df8d8803e3ab [file] [log] [blame]
Kevin O'Connor114592f2009-09-28 21:32:08 -04001// Main code for handling USB controllers and devices.
2//
3// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7#include "util.h" // dprintf
Kevin O'Connorbaac6b62011-06-19 10:46:28 -04008#include "pci.h" // foreachbdf
Kevin O'Connor114592f2009-09-28 21:32:08 -04009#include "config.h" // CONFIG_*
10#include "pci_regs.h" // PCI_CLASS_REVISION
11#include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
12#include "usb-uhci.h" // uhci_init
Kevin O'Connor59f02832009-10-12 10:09:15 -040013#include "usb-ohci.h" // ohci_init
Kevin O'Connor190cc622010-03-09 19:43:52 -050014#include "usb-ehci.h" // ehci_init
Kevin O'Connor114592f2009-09-28 21:32:08 -040015#include "usb-hid.h" // usb_keyboard_setup
Kevin O'Connor54671c12010-02-15 18:58:12 -050016#include "usb-hub.h" // usb_hub_init
Kevin O'Connor7149fc82010-02-17 23:24:42 -050017#include "usb-msc.h" // usb_msc_init
Kevin O'Connor114592f2009-09-28 21:32:08 -040018#include "usb.h" // struct usb_s
Kevin O'Connor1c46a542009-10-17 23:53:32 -040019#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor114592f2009-09-28 21:32:08 -040020
Kevin O'Connor3c160dd2010-02-17 23:06:52 -050021
22/****************************************************************
23 * Controller function wrappers
24 ****************************************************************/
25
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050026// Free an allocated control or bulk pipe.
27void
28free_pipe(struct usb_pipe *pipe)
29{
30 ASSERT32FLAT();
31 if (!pipe)
32 return;
Kevin O'Connor4547eb92010-02-28 02:17:28 -050033 switch (pipe->type) {
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050034 default:
35 case USB_TYPE_UHCI:
36 return uhci_free_pipe(pipe);
37 case USB_TYPE_OHCI:
38 return ohci_free_pipe(pipe);
Kevin O'Connor190cc622010-03-09 19:43:52 -050039 case USB_TYPE_EHCI:
40 return ehci_free_pipe(pipe);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050041 }
42}
43
Kevin O'Connor4547eb92010-02-28 02:17:28 -050044// Allocate a control pipe to a default endpoint (which can only be
45// used by 32bit code)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050046static struct usb_pipe *
Kevin O'Connor4547eb92010-02-28 02:17:28 -050047alloc_default_control_pipe(struct usb_pipe *dummy)
Kevin O'Connor114592f2009-09-28 21:32:08 -040048{
Kevin O'Connor4547eb92010-02-28 02:17:28 -050049 switch (dummy->type) {
Kevin O'Connor59f02832009-10-12 10:09:15 -040050 default:
51 case USB_TYPE_UHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -050052 return uhci_alloc_control_pipe(dummy);
Kevin O'Connor59f02832009-10-12 10:09:15 -040053 case USB_TYPE_OHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -050054 return ohci_alloc_control_pipe(dummy);
Kevin O'Connor190cc622010-03-09 19:43:52 -050055 case USB_TYPE_EHCI:
56 return ehci_alloc_control_pipe(dummy);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050057 }
58}
59
60// Send a message on a control pipe using the default control descriptor.
61static int
62send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
63 , void *data, int datasize)
64{
65 ASSERT32FLAT();
Kevin O'Connor4547eb92010-02-28 02:17:28 -050066 switch (pipe->type) {
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050067 default:
68 case USB_TYPE_UHCI:
69 return uhci_control(pipe, dir, cmd, cmdsize, data, datasize);
70 case USB_TYPE_OHCI:
71 return ohci_control(pipe, dir, cmd, cmdsize, data, datasize);
Kevin O'Connor190cc622010-03-09 19:43:52 -050072 case USB_TYPE_EHCI:
73 return ehci_control(pipe, dir, cmd, cmdsize, data, datasize);
Kevin O'Connor59f02832009-10-12 10:09:15 -040074 }
Kevin O'Connor114592f2009-09-28 21:32:08 -040075}
76
Kevin O'Connor4547eb92010-02-28 02:17:28 -050077// Fill "pipe" endpoint info from an endpoint descriptor.
78static void
79desc2pipe(struct usb_pipe *newpipe, struct usb_pipe *origpipe
80 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor7149fc82010-02-17 23:24:42 -050081{
Kevin O'Connor4547eb92010-02-28 02:17:28 -050082 memcpy(newpipe, origpipe, sizeof(*newpipe));
83 newpipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
84 newpipe->maxpacket = epdesc->wMaxPacketSize;
85}
86
87struct usb_pipe *
88alloc_bulk_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
89{
90 struct usb_pipe dummy;
91 desc2pipe(&dummy, pipe, epdesc);
92 switch (pipe->type) {
Kevin O'Connor7149fc82010-02-17 23:24:42 -050093 default:
94 case USB_TYPE_UHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -050095 return uhci_alloc_bulk_pipe(&dummy);
Kevin O'Connor7149fc82010-02-17 23:24:42 -050096 case USB_TYPE_OHCI:
Kevin O'Connor2f968002010-09-19 22:10:08 -040097 return ohci_alloc_bulk_pipe(&dummy);
Kevin O'Connor190cc622010-03-09 19:43:52 -050098 case USB_TYPE_EHCI:
99 return ehci_alloc_bulk_pipe(&dummy);
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500100 }
101}
102
103int
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500104usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500105{
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500106 switch (GET_FLATPTR(pipe_fl->type)) {
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500107 default:
108 case USB_TYPE_UHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500109 return uhci_send_bulk(pipe_fl, dir, data, datasize);
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500110 case USB_TYPE_OHCI:
Kevin O'Connor2f968002010-09-19 22:10:08 -0400111 return ohci_send_bulk(pipe_fl, dir, data, datasize);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500112 case USB_TYPE_EHCI:
113 return ehci_send_bulk(pipe_fl, dir, data, datasize);
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500114 }
115}
116
117struct usb_pipe *
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500118alloc_intr_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400119{
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500120 struct usb_pipe dummy;
121 desc2pipe(&dummy, pipe, epdesc);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500122 // Find the exponential period of the requested time.
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500123 int period = epdesc->bInterval;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500124 int frameexp;
125 if (pipe->speed != USB_HIGHSPEED)
126 frameexp = (period <= 0) ? 0 : __fls(period);
127 else
128 frameexp = (period <= 4) ? 0 : period - 4;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500129 switch (pipe->type) {
Kevin O'Connor59f02832009-10-12 10:09:15 -0400130 default:
131 case USB_TYPE_UHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500132 return uhci_alloc_intr_pipe(&dummy, frameexp);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400133 case USB_TYPE_OHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500134 return ohci_alloc_intr_pipe(&dummy, frameexp);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500135 case USB_TYPE_EHCI:
136 return ehci_alloc_intr_pipe(&dummy, frameexp);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400137 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400138}
139
Kevin O'Connor84a4d4b2010-02-11 22:32:12 -0500140int noinline
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500141usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400142{
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500143 switch (GET_FLATPTR(pipe_fl->type)) {
Kevin O'Connor59f02832009-10-12 10:09:15 -0400144 default:
145 case USB_TYPE_UHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500146 return uhci_poll_intr(pipe_fl, data);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400147 case USB_TYPE_OHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500148 return ohci_poll_intr(pipe_fl, data);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500149 case USB_TYPE_EHCI:
150 return ehci_poll_intr(pipe_fl, data);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400151 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400152}
153
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500154
155/****************************************************************
156 * Helper functions
157 ****************************************************************/
158
159// Find the first endpoing of a given type in an interface description.
160struct usb_endpoint_descriptor *
161findEndPointDesc(struct usb_interface_descriptor *iface, int imax
162 , int type, int dir)
163{
164 struct usb_endpoint_descriptor *epdesc = (void*)&iface[1];
165 for (;;) {
166 if ((void*)epdesc >= (void*)iface + imax
167 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
168 return NULL;
169 }
170 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
171 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
172 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
173 return epdesc;
174 epdesc = (void*)epdesc + epdesc->bLength;
175 }
176}
177
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500178// Send a message to the default control pipe of a device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400179int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500180send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
181 , void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400182{
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500183 return send_control(pipe, req->bRequestType & USB_DIR_IN
Kevin O'Connor114592f2009-09-28 21:32:08 -0400184 , req, sizeof(*req), data, req->wLength);
185}
186
187// Get the first 8 bytes of the device descriptor.
188static int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500189get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400190{
191 struct usb_ctrlrequest req;
192 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
193 req.bRequest = USB_REQ_GET_DESCRIPTOR;
194 req.wValue = USB_DT_DEVICE<<8;
195 req.wIndex = 0;
196 req.wLength = 8;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500197 return send_default_control(pipe, &req, dinfo);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400198}
199
200static struct usb_config_descriptor *
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500201get_device_config(struct usb_pipe *pipe)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400202{
203 struct usb_config_descriptor cfg;
204
205 struct usb_ctrlrequest req;
206 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
207 req.bRequest = USB_REQ_GET_DESCRIPTOR;
208 req.wValue = USB_DT_CONFIG<<8;
209 req.wIndex = 0;
210 req.wLength = sizeof(cfg);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500211 int ret = send_default_control(pipe, &req, &cfg);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400212 if (ret)
213 return NULL;
214
215 void *config = malloc_tmphigh(cfg.wTotalLength);
216 if (!config)
217 return NULL;
218 req.wLength = cfg.wTotalLength;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500219 ret = send_default_control(pipe, &req, config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400220 if (ret)
221 return NULL;
222 //hexdump(config, cfg.wTotalLength);
223 return config;
224}
225
Kevin O'Connor114592f2009-09-28 21:32:08 -0400226static int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500227set_configuration(struct usb_pipe *pipe, u16 val)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400228{
229 struct usb_ctrlrequest req;
230 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
231 req.bRequest = USB_REQ_SET_CONFIGURATION;
232 req.wValue = val;
233 req.wIndex = 0;
234 req.wLength = 0;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500235 return send_default_control(pipe, &req, NULL);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400236}
237
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500238
239/****************************************************************
240 * Initialization and enumeration
241 ****************************************************************/
242
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500243// Assign an address to a device in the default state on the given
244// controller.
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400245static struct usb_pipe *
Kevin O'Connor190cc622010-03-09 19:43:52 -0500246usb_set_address(struct usbhub_s *hub, int port, int speed)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400247{
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500248 ASSERT32FLAT();
Kevin O'Connor190cc622010-03-09 19:43:52 -0500249 struct usb_s *cntl = hub->cntl;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500250 dprintf(3, "set_address %p\n", cntl);
251 if (cntl->maxaddr >= USB_MAXADDR)
252 return NULL;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400253
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500254 struct usb_pipe *defpipe = cntl->defaultpipe;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500255 if (!defpipe) {
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500256 // Create a pipe for the default address.
257 struct usb_pipe dummy;
258 memset(&dummy, 0, sizeof(dummy));
259 dummy.cntl = cntl;
260 dummy.type = cntl->type;
261 dummy.maxpacket = 8;
Kevin O'Connora5f2b912010-12-31 14:35:26 -0500262 dummy.path = (u64)-1;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500263 cntl->defaultpipe = defpipe = alloc_default_control_pipe(&dummy);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500264 if (!defpipe)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500265 return NULL;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500266 }
Kevin O'Connor190cc622010-03-09 19:43:52 -0500267 defpipe->speed = speed;
268 if (hub->pipe) {
269 if (hub->pipe->speed == USB_HIGHSPEED) {
270 defpipe->tt_devaddr = hub->pipe->devaddr;
271 defpipe->tt_port = port;
272 } else {
273 defpipe->tt_devaddr = hub->pipe->tt_devaddr;
274 defpipe->tt_port = hub->pipe->tt_port;
275 }
276 } else {
277 defpipe->tt_devaddr = defpipe->tt_port = 0;
278 }
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500279
280 msleep(USB_TIME_RSTRCY);
281
282 struct usb_ctrlrequest req;
283 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
284 req.bRequest = USB_REQ_SET_ADDRESS;
285 req.wValue = cntl->maxaddr + 1;
286 req.wIndex = 0;
287 req.wLength = 0;
288 int ret = send_default_control(defpipe, &req, NULL);
289 if (ret)
290 return NULL;
291
292 msleep(USB_TIME_SETADDR_RECOVERY);
293
294 cntl->maxaddr++;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500295 defpipe->devaddr = cntl->maxaddr;
296 struct usb_pipe *pipe = alloc_default_control_pipe(defpipe);
297 defpipe->devaddr = 0;
Kevin O'Connora5f2b912010-12-31 14:35:26 -0500298 if (hub->pipe)
299 pipe->path = hub->pipe->path;
300 pipe->path = (pipe->path << 8) | port;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500301 return pipe;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500302}
303
304// Called for every found device - see if a driver is available for
305// this device and do setup if so.
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400306static int
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500307configure_usb_device(struct usb_pipe *pipe)
308{
309 ASSERT32FLAT();
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500310 dprintf(3, "config_usb: %p\n", pipe);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500311
312 // Set the max packet size for endpoint 0 of this device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400313 struct usb_device_descriptor dinfo;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500314 int ret = get_device_info8(pipe, &dinfo);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400315 if (ret)
316 return 0;
317 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
318 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
319 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
320 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
321 return 0;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500322 pipe->maxpacket = dinfo.bMaxPacketSize0;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400323
324 // Get configuration
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500325 struct usb_config_descriptor *config = get_device_config(pipe);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400326 if (!config)
327 return 0;
328
329 // Determine if a driver exists for this device - only look at the
330 // first interface of the first configuration.
331 struct usb_interface_descriptor *iface = (void*)(&config[1]);
Kevin O'Connor0e885762010-05-01 22:14:40 -0400332 if (iface->bInterfaceClass != USB_CLASS_HID
333 && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
334 && iface->bInterfaceClass != USB_CLASS_HUB)
Kevin O'Connor54671c12010-02-15 18:58:12 -0500335 // Not a supported device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400336 goto fail;
337
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500338 // Set the configuration.
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500339 ret = set_configuration(pipe, config->bConfigurationValue);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400340 if (ret)
341 goto fail;
342
343 // Configure driver.
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500344 int imax = (void*)config + config->wTotalLength - (void*)iface;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500345 if (iface->bInterfaceClass == USB_CLASS_HUB)
346 ret = usb_hub_init(pipe);
347 else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE)
348 ret = usb_msc_init(pipe, iface, imax);
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500349 else
Kevin O'Connor0e885762010-05-01 22:14:40 -0400350 ret = usb_hid_init(pipe, iface, imax);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400351 if (ret)
352 goto fail;
353
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400354 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400355 return 1;
356fail:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400357 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400358 return 0;
359}
360
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400361static void
362usb_init_hub_port(void *data)
363{
364 struct usbhub_s *hub = data;
365 u32 port = hub->port; // XXX - find better way to pass port
366
367 // Detect if device present (and possibly start reset)
368 int ret = hub->op->detect(hub, port);
369 if (ret)
370 // No device present
371 goto done;
372
373 // Reset port and determine device speed
374 mutex_lock(&hub->cntl->resetlock);
375 ret = hub->op->reset(hub, port);
376 if (ret < 0)
377 // Reset failed
378 goto resetfail;
379
380 // Set address of port
381 struct usb_pipe *pipe = usb_set_address(hub, port, ret);
382 if (!pipe) {
383 hub->op->disconnect(hub, port);
384 goto resetfail;
385 }
386 mutex_unlock(&hub->cntl->resetlock);
387
388 // Configure the device
389 int count = configure_usb_device(pipe);
390 free_pipe(pipe);
391 if (!count)
392 hub->op->disconnect(hub, port);
393 hub->devcount += count;
394done:
395 hub->threads--;
396 return;
397
398resetfail:
399 mutex_unlock(&hub->cntl->resetlock);
400 goto done;
401}
402
403void
404usb_enumerate(struct usbhub_s *hub)
405{
406 u32 portcount = hub->portcount;
407 hub->threads = portcount;
408
409 // Launch a thread for every port.
410 int i;
411 for (i=0; i<portcount; i++) {
412 hub->port = i;
413 run_thread(usb_init_hub_port, hub);
414 }
415
416 // Wait for threads to complete.
417 while (hub->threads)
418 yield();
419}
420
Kevin O'Connor114592f2009-09-28 21:32:08 -0400421void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500422usb_setup(void)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400423{
Kevin O'Connor59c75742010-02-13 18:49:24 -0500424 ASSERT32FLAT();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400425 if (! CONFIG_USB)
426 return;
427
428 dprintf(3, "init usb\n");
429
Kevin O'Connor114592f2009-09-28 21:32:08 -0400430 // Look for USB controllers
Kevin O'Connor190cc622010-03-09 19:43:52 -0500431 int ehcibdf = -1;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400432 int count = 0;
433 int bdf, max;
Kevin O'Connorbaac6b62011-06-19 10:46:28 -0400434 foreachbdf(bdf, max) {
Kevin O'Connor114592f2009-09-28 21:32:08 -0400435 u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
436
437 if (code >> 8 != PCI_CLASS_SERIAL_USB)
438 continue;
439
Kevin O'Connor190cc622010-03-09 19:43:52 -0500440 if (bdf > ehcibdf) {
441 // Check to see if this device has an ehci controller
442 ehcibdf = bdf;
443 u32 ehcicode = code;
444 int found = 0;
445 for (;;) {
446 if (ehcicode == PCI_CLASS_SERIAL_USB_EHCI) {
447 // Found an ehci controller.
448 int ret = ehci_init(ehcibdf, count++, bdf);
449 if (ret)
450 // Error
451 break;
452 count += found;
453 bdf = ehcibdf;
454 code = 0;
455 break;
456 }
457 if (ehcicode >> 8 == PCI_CLASS_SERIAL_USB)
458 found++;
459 ehcibdf = pci_next(ehcibdf+1, &max);
460 if (ehcibdf < 0
461 || pci_bdf_to_busdev(ehcibdf) != pci_bdf_to_busdev(bdf))
462 // No ehci controller found.
463 break;
464 ehcicode = pci_config_readl(ehcibdf, PCI_CLASS_REVISION) >> 8;
465 }
466 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400467
Kevin O'Connor190cc622010-03-09 19:43:52 -0500468 if (code == PCI_CLASS_SERIAL_USB_UHCI)
469 uhci_init(bdf, count++);
470 else if (code == PCI_CLASS_SERIAL_USB_OHCI)
471 ohci_init(bdf, count++);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400472 }
473}