1 // Main code for handling USB controllers and devices.
3 // Copyright (C) 2009-2013 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "biosvar.h" // GET_GLOBAL
8 #include "config.h" // CONFIG_*
9 #include "malloc.h" // free
10 #include "output.h" // dprintf
11 #include "string.h" // memset
12 #include "usb.h" // struct usb_s
13 #include "usb-ehci.h" // ehci_setup
14 #include "usb-xhci.h" // xhci_setup
15 #include "usb-hid.h" // usb_keyboard_setup
16 #include "usb-hub.h" // usb_hub_setup
17 #include "usb-msc.h" // usb_msc_setup
18 #include "usb-ohci.h" // ohci_setup
19 #include "usb-uas.h" // usb_uas_setup
20 #include "usb-uhci.h" // uhci_setup
21 #include "util.h" // msleep
22 #include "x86.h" // __fls
25 /****************************************************************
26 * Controller function wrappers
27 ****************************************************************/
29 // Allocate, update, or free a usb pipe.
30 static struct usb_pipe *
31 usb_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *pipe
32 , struct usb_endpoint_descriptor *epdesc)
34 switch (usbdev->hub->cntl->type) {
37 return uhci_realloc_pipe(usbdev, pipe, epdesc);
39 return ohci_realloc_pipe(usbdev, pipe, epdesc);
41 return ehci_realloc_pipe(usbdev, pipe, epdesc);
43 return xhci_realloc_pipe(usbdev, pipe, epdesc);
47 // Send a message on a control pipe using the default control descriptor.
49 usb_send_pipe(struct usb_pipe *pipe_fl, int dir, const void *cmd
50 , void *data, int datasize)
52 switch (GET_LOWFLAT(pipe_fl->type)) {
55 return uhci_send_pipe(pipe_fl, dir, cmd, data, datasize);
59 return ohci_send_pipe(pipe_fl, dir, cmd, data, datasize);
61 return ehci_send_pipe(pipe_fl, dir, cmd, data, datasize);
65 return xhci_send_pipe(pipe_fl, dir, cmd, data, datasize);
70 usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
73 switch (GET_LOWFLAT(pipe_fl->type)) {
76 return uhci_poll_intr(pipe_fl, data);
78 return ohci_poll_intr(pipe_fl, data);
80 return ehci_poll_intr(pipe_fl, data);
82 return call32_params(xhci_poll_intr, pipe_fl
83 , MAKE_FLATPTR(GET_SEG(SS), data), 0, -1);
87 int usb_32bit_pipe(struct usb_pipe *pipe_fl)
89 return (CONFIG_USB_XHCI && GET_LOWFLAT(pipe_fl->type) == USB_TYPE_XHCI)
90 || (CONFIG_USB_OHCI && GET_LOWFLAT(pipe_fl->type) == USB_TYPE_OHCI);
94 /****************************************************************
96 ****************************************************************/
98 // Allocate a usb pipe.
100 usb_alloc_pipe(struct usbdevice_s *usbdev
101 , struct usb_endpoint_descriptor *epdesc)
103 return usb_realloc_pipe(usbdev, NULL, epdesc);
106 // Free an allocated control or bulk pipe.
108 usb_free_pipe(struct usbdevice_s *usbdev, struct usb_pipe *pipe)
112 usb_realloc_pipe(usbdev, pipe, NULL);
115 // Send a message to the default control pipe of a device.
117 usb_send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
120 return usb_send_pipe(pipe, req->bRequestType & USB_DIR_IN, req
121 , data, req->wLength);
124 // Send a message to a bulk endpoint
126 usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
128 return usb_send_pipe(pipe_fl, dir, NULL, data, datasize);
131 // Check if a pipe for a given controller is on the freelist
133 usb_is_freelist(struct usb_s *cntl, struct usb_pipe *pipe)
135 return pipe->cntl != cntl;
138 // Add a pipe to the controller's freelist
140 usb_add_freelist(struct usb_pipe *pipe)
144 struct usb_s *cntl = pipe->cntl;
145 pipe->freenext = cntl->freelist;
146 cntl->freelist = pipe;
149 // Check for an available pipe on the freelist.
151 usb_get_freelist(struct usb_s *cntl, u8 eptype)
153 struct usb_pipe **pfree = &cntl->freelist;
155 struct usb_pipe *pipe = *pfree;
158 if (pipe->eptype == eptype) {
159 *pfree = pipe->freenext;
162 pfree = &pipe->freenext;
166 // Fill "pipe" endpoint info from an endpoint descriptor.
168 usb_desc2pipe(struct usb_pipe *pipe, struct usbdevice_s *usbdev
169 , struct usb_endpoint_descriptor *epdesc)
171 pipe->cntl = usbdev->hub->cntl;
172 pipe->type = usbdev->hub->cntl->type;
173 pipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
174 pipe->devaddr = usbdev->devaddr;
175 pipe->speed = usbdev->speed;
176 pipe->maxpacket = epdesc->wMaxPacketSize;
177 pipe->eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
180 // Find the exponential period of the requested interrupt end point.
182 usb_get_period(struct usbdevice_s *usbdev
183 , struct usb_endpoint_descriptor *epdesc)
185 int period = epdesc->bInterval;
186 if (usbdev->speed != USB_HIGHSPEED)
187 return (period <= 0) ? 0 : __fls(period);
188 return (period <= 4) ? 0 : period - 4;
191 // Maximum time (in ms) a data transfer should take
193 usb_xfer_time(struct usb_pipe *pipe, int datalen)
195 // Use the maximum command time (5 seconds), except for
196 // set_address commands where we don't want to stall the boot if
197 // the device doesn't actually exist. Add 100ms to account for
198 // any controller delays.
199 if (!GET_LOWFLAT(pipe->devaddr))
200 return USB_TIME_STATUS + 100;
201 return USB_TIME_COMMAND + 100;
204 // Find the first endpoint of a given type in an interface description.
205 struct usb_endpoint_descriptor *
206 usb_find_desc(struct usbdevice_s *usbdev, int type, int dir)
208 struct usb_endpoint_descriptor *epdesc = (void*)&usbdev->iface[1];
210 if ((void*)epdesc >= (void*)usbdev->iface + usbdev->imax
211 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
214 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
215 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
216 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
218 epdesc = (void*)epdesc + epdesc->bLength;
222 // Get the first 8 bytes of the device descriptor.
224 get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
226 struct usb_ctrlrequest req;
227 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
228 req.bRequest = USB_REQ_GET_DESCRIPTOR;
229 req.wValue = USB_DT_DEVICE<<8;
232 return usb_send_default_control(pipe, &req, dinfo);
235 static struct usb_config_descriptor *
236 get_device_config(struct usb_pipe *pipe)
238 struct usb_config_descriptor cfg;
240 struct usb_ctrlrequest req;
241 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
242 req.bRequest = USB_REQ_GET_DESCRIPTOR;
243 req.wValue = USB_DT_CONFIG<<8;
245 req.wLength = sizeof(cfg);
246 int ret = usb_send_default_control(pipe, &req, &cfg);
250 void *config = malloc_tmphigh(cfg.wTotalLength);
255 req.wLength = cfg.wTotalLength;
256 ret = usb_send_default_control(pipe, &req, config);
261 //hexdump(config, cfg.wTotalLength);
266 set_configuration(struct usb_pipe *pipe, u16 val)
268 struct usb_ctrlrequest req;
269 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
270 req.bRequest = USB_REQ_SET_CONFIGURATION;
274 return usb_send_default_control(pipe, &req, NULL);
278 /****************************************************************
279 * Initialization and enumeration
280 ****************************************************************/
282 static const int speed_to_ctlsize[] = {
283 [ USB_FULLSPEED ] = 8,
284 [ USB_LOWSPEED ] = 8,
285 [ USB_HIGHSPEED ] = 64,
286 [ USB_SUPERSPEED ] = 512,
289 // Assign an address to a device in the default state on the given
292 usb_set_address(struct usbdevice_s *usbdev)
295 struct usb_s *cntl = usbdev->hub->cntl;
296 dprintf(3, "set_address %p\n", cntl);
297 if (cntl->maxaddr >= USB_MAXADDR)
300 msleep(USB_TIME_RSTRCY);
302 // Create a pipe for the default address.
303 struct usb_endpoint_descriptor epdesc = {
304 .wMaxPacketSize = speed_to_ctlsize[usbdev->speed],
305 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
307 usbdev->defpipe = usb_alloc_pipe(usbdev, &epdesc);
308 if (!usbdev->defpipe)
311 // Send set_address command.
312 struct usb_ctrlrequest req;
313 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
314 req.bRequest = USB_REQ_SET_ADDRESS;
315 req.wValue = cntl->maxaddr + 1;
318 int ret = usb_send_default_control(usbdev->defpipe, &req, NULL);
320 usb_free_pipe(usbdev, usbdev->defpipe);
324 msleep(USB_TIME_SETADDR_RECOVERY);
327 usbdev->devaddr = cntl->maxaddr;
328 usbdev->defpipe = usb_realloc_pipe(usbdev, usbdev->defpipe, &epdesc);
329 if (!usbdev->defpipe)
334 // Called for every found device - see if a driver is available for
335 // this device and do setup if so.
337 configure_usb_device(struct usbdevice_s *usbdev)
340 dprintf(3, "config_usb: %p\n", usbdev->defpipe);
342 // Set the max packet size for endpoint 0 of this device.
343 struct usb_device_descriptor dinfo;
344 int ret = get_device_info8(usbdev->defpipe, &dinfo);
347 u16 maxpacket = dinfo.bMaxPacketSize0;
348 if (dinfo.bcdUSB >= 0x0300)
349 maxpacket = 1 << dinfo.bMaxPacketSize0;
350 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%d\n"
351 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
352 , dinfo.bDeviceProtocol, maxpacket);
355 struct usb_endpoint_descriptor epdesc = {
356 .wMaxPacketSize = maxpacket,
357 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
359 usbdev->defpipe = usb_realloc_pipe(usbdev, usbdev->defpipe, &epdesc);
360 if (!usbdev->defpipe)
364 struct usb_config_descriptor *config = get_device_config(usbdev->defpipe);
368 // Determine if a driver exists for this device - only look at the
369 // first interface of the first configuration.
370 struct usb_interface_descriptor *iface = (void*)(&config[1]);
371 if (iface->bInterfaceClass != USB_CLASS_HID
372 && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
373 && iface->bInterfaceClass != USB_CLASS_HUB)
374 // Not a supported device.
377 // Set the configuration.
378 ret = set_configuration(usbdev->defpipe, config->bConfigurationValue);
383 usbdev->config = config;
384 usbdev->iface = iface;
385 usbdev->imax = (void*)config + config->wTotalLength - (void*)iface;
386 if (iface->bInterfaceClass == USB_CLASS_HUB)
387 ret = usb_hub_setup(usbdev);
388 else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE) {
389 if (iface->bInterfaceProtocol == US_PR_BULK)
390 ret = usb_msc_setup(usbdev);
391 if (iface->bInterfaceProtocol == US_PR_UAS)
392 ret = usb_uas_setup(usbdev);
394 ret = usb_hid_setup(usbdev);
406 usb_hub_port_setup(void *data)
408 struct usbdevice_s *usbdev = data;
409 struct usbhub_s *hub = usbdev->hub;
410 u32 port = usbdev->port;
413 // Detect if device present (and possibly start reset)
414 int ret = hub->op->detect(hub, port);
418 if (ret < 0 || timer_check(hub->detectend))
424 // XXX - wait USB_TIME_ATTDB time?
426 // Reset port and determine device speed
427 mutex_lock(&hub->cntl->resetlock);
428 int ret = hub->op->reset(hub, port);
434 // Set address of port
435 ret = usb_set_address(usbdev);
437 hub->op->disconnect(hub, port);
440 mutex_unlock(&hub->cntl->resetlock);
442 // Configure the device
443 int count = configure_usb_device(usbdev);
444 usb_free_pipe(usbdev, usbdev->defpipe);
446 hub->op->disconnect(hub, port);
447 hub->devcount += count;
454 mutex_unlock(&hub->cntl->resetlock);
459 usb_enumerate(struct usbhub_s *hub)
461 u32 portcount = hub->portcount;
462 hub->threads = portcount;
463 hub->detectend = timer_calc(USB_TIME_SIGATT);
465 // Launch a thread for every port.
467 for (i=0; i<portcount; i++) {
468 struct usbdevice_s *usbdev = malloc_tmphigh(sizeof(*usbdev));
473 memset(usbdev, 0, sizeof(*usbdev));
476 run_thread(usb_hub_port_setup, usbdev);
479 // Wait for threads to complete.
485 __usb_setup(void *data)
487 dprintf(3, "init usb\n");
500 run_thread(__usb_setup, NULL);