blob: aa8d72cc1f7ec273e07faa1d463d24e8fba169b0 [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
8#include "pci.h" // foreachpci
9#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;
262 cntl->defaultpipe = defpipe = alloc_default_control_pipe(&dummy);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500263 if (!defpipe)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500264 return NULL;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500265 }
Kevin O'Connor190cc622010-03-09 19:43:52 -0500266 defpipe->speed = speed;
267 if (hub->pipe) {
268 if (hub->pipe->speed == USB_HIGHSPEED) {
269 defpipe->tt_devaddr = hub->pipe->devaddr;
270 defpipe->tt_port = port;
271 } else {
272 defpipe->tt_devaddr = hub->pipe->tt_devaddr;
273 defpipe->tt_port = hub->pipe->tt_port;
274 }
275 } else {
276 defpipe->tt_devaddr = defpipe->tt_port = 0;
277 }
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500278
279 msleep(USB_TIME_RSTRCY);
280
281 struct usb_ctrlrequest req;
282 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
283 req.bRequest = USB_REQ_SET_ADDRESS;
284 req.wValue = cntl->maxaddr + 1;
285 req.wIndex = 0;
286 req.wLength = 0;
287 int ret = send_default_control(defpipe, &req, NULL);
288 if (ret)
289 return NULL;
290
291 msleep(USB_TIME_SETADDR_RECOVERY);
292
293 cntl->maxaddr++;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500294 defpipe->devaddr = cntl->maxaddr;
295 struct usb_pipe *pipe = alloc_default_control_pipe(defpipe);
296 defpipe->devaddr = 0;
297 return pipe;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500298}
299
300// Called for every found device - see if a driver is available for
301// this device and do setup if so.
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400302static int
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500303configure_usb_device(struct usb_pipe *pipe)
304{
305 ASSERT32FLAT();
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500306 dprintf(3, "config_usb: %p\n", pipe);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500307
308 // Set the max packet size for endpoint 0 of this device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400309 struct usb_device_descriptor dinfo;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500310 int ret = get_device_info8(pipe, &dinfo);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400311 if (ret)
312 return 0;
313 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
314 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
315 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
316 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
317 return 0;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500318 pipe->maxpacket = dinfo.bMaxPacketSize0;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400319
320 // Get configuration
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500321 struct usb_config_descriptor *config = get_device_config(pipe);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400322 if (!config)
323 return 0;
324
325 // Determine if a driver exists for this device - only look at the
326 // first interface of the first configuration.
327 struct usb_interface_descriptor *iface = (void*)(&config[1]);
Kevin O'Connor0e885762010-05-01 22:14:40 -0400328 if (iface->bInterfaceClass != USB_CLASS_HID
329 && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
330 && iface->bInterfaceClass != USB_CLASS_HUB)
Kevin O'Connor54671c12010-02-15 18:58:12 -0500331 // Not a supported device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400332 goto fail;
333
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500334 // Set the configuration.
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500335 ret = set_configuration(pipe, config->bConfigurationValue);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400336 if (ret)
337 goto fail;
338
339 // Configure driver.
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500340 int imax = (void*)config + config->wTotalLength - (void*)iface;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500341 if (iface->bInterfaceClass == USB_CLASS_HUB)
342 ret = usb_hub_init(pipe);
343 else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE)
344 ret = usb_msc_init(pipe, iface, imax);
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500345 else
Kevin O'Connor0e885762010-05-01 22:14:40 -0400346 ret = usb_hid_init(pipe, iface, imax);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400347 if (ret)
348 goto fail;
349
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400350 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400351 return 1;
352fail:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400353 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400354 return 0;
355}
356
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400357static void
358usb_init_hub_port(void *data)
359{
360 struct usbhub_s *hub = data;
361 u32 port = hub->port; // XXX - find better way to pass port
362
363 // Detect if device present (and possibly start reset)
364 int ret = hub->op->detect(hub, port);
365 if (ret)
366 // No device present
367 goto done;
368
369 // Reset port and determine device speed
370 mutex_lock(&hub->cntl->resetlock);
371 ret = hub->op->reset(hub, port);
372 if (ret < 0)
373 // Reset failed
374 goto resetfail;
375
376 // Set address of port
377 struct usb_pipe *pipe = usb_set_address(hub, port, ret);
378 if (!pipe) {
379 hub->op->disconnect(hub, port);
380 goto resetfail;
381 }
382 mutex_unlock(&hub->cntl->resetlock);
383
384 // Configure the device
385 int count = configure_usb_device(pipe);
386 free_pipe(pipe);
387 if (!count)
388 hub->op->disconnect(hub, port);
389 hub->devcount += count;
390done:
391 hub->threads--;
392 return;
393
394resetfail:
395 mutex_unlock(&hub->cntl->resetlock);
396 goto done;
397}
398
399void
400usb_enumerate(struct usbhub_s *hub)
401{
402 u32 portcount = hub->portcount;
403 hub->threads = portcount;
404
405 // Launch a thread for every port.
406 int i;
407 for (i=0; i<portcount; i++) {
408 hub->port = i;
409 run_thread(usb_init_hub_port, hub);
410 }
411
412 // Wait for threads to complete.
413 while (hub->threads)
414 yield();
415}
416
Kevin O'Connor114592f2009-09-28 21:32:08 -0400417void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500418usb_setup(void)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400419{
Kevin O'Connor59c75742010-02-13 18:49:24 -0500420 ASSERT32FLAT();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400421 if (! CONFIG_USB)
422 return;
423
424 dprintf(3, "init usb\n");
425
Kevin O'Connor114592f2009-09-28 21:32:08 -0400426 // Look for USB controllers
Kevin O'Connor190cc622010-03-09 19:43:52 -0500427 int ehcibdf = -1;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400428 int count = 0;
429 int bdf, max;
430 foreachpci(bdf, max) {
431 u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
432
433 if (code >> 8 != PCI_CLASS_SERIAL_USB)
434 continue;
435
Kevin O'Connor190cc622010-03-09 19:43:52 -0500436 if (bdf > ehcibdf) {
437 // Check to see if this device has an ehci controller
438 ehcibdf = bdf;
439 u32 ehcicode = code;
440 int found = 0;
441 for (;;) {
442 if (ehcicode == PCI_CLASS_SERIAL_USB_EHCI) {
443 // Found an ehci controller.
444 int ret = ehci_init(ehcibdf, count++, bdf);
445 if (ret)
446 // Error
447 break;
448 count += found;
449 bdf = ehcibdf;
450 code = 0;
451 break;
452 }
453 if (ehcicode >> 8 == PCI_CLASS_SERIAL_USB)
454 found++;
455 ehcibdf = pci_next(ehcibdf+1, &max);
456 if (ehcibdf < 0
457 || pci_bdf_to_busdev(ehcibdf) != pci_bdf_to_busdev(bdf))
458 // No ehci controller found.
459 break;
460 ehcicode = pci_config_readl(ehcibdf, PCI_CLASS_REVISION) >> 8;
461 }
462 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400463
Kevin O'Connor190cc622010-03-09 19:43:52 -0500464 if (code == PCI_CLASS_SERIAL_USB_UHCI)
465 uhci_init(bdf, count++);
466 else if (code == PCI_CLASS_SERIAL_USB_OHCI)
467 ohci_init(bdf, count++);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400468 }
469}