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 extern void _cfunc32flat_xhci_poll_intr(void);
83 return call32_params(_cfunc32flat_xhci_poll_intr, (u32)pipe_fl
84 , (u32)MAKE_FLATPTR(GET_SEG(SS), (u32)data), 0, -1);
88 int usb_32bit_pipe(struct usb_pipe *pipe_fl)
90 return (CONFIG_USB_XHCI && GET_LOWFLAT(pipe_fl->type) == USB_TYPE_XHCI)
91 || (CONFIG_USB_OHCI && GET_LOWFLAT(pipe_fl->type) == USB_TYPE_OHCI);
95 /****************************************************************
97 ****************************************************************/
99 // Allocate a usb pipe.
101 usb_alloc_pipe(struct usbdevice_s *usbdev
102 , struct usb_endpoint_descriptor *epdesc)
104 return usb_realloc_pipe(usbdev, NULL, epdesc);
107 // Free an allocated control or bulk pipe.
109 usb_free_pipe(struct usbdevice_s *usbdev, struct usb_pipe *pipe)
113 usb_realloc_pipe(usbdev, pipe, NULL);
116 // Send a message to the default control pipe of a device.
118 usb_send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
121 return usb_send_pipe(pipe, req->bRequestType & USB_DIR_IN, req
122 , data, req->wLength);
125 // Send a message to a bulk endpoint
127 usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
129 return usb_send_pipe(pipe_fl, dir, NULL, data, datasize);
132 // Check if a pipe for a given controller is on the freelist
134 usb_is_freelist(struct usb_s *cntl, struct usb_pipe *pipe)
136 return pipe->cntl != cntl;
139 // Add a pipe to the controller's freelist
141 usb_add_freelist(struct usb_pipe *pipe)
145 struct usb_s *cntl = pipe->cntl;
146 pipe->freenext = cntl->freelist;
147 cntl->freelist = pipe;
150 // Check for an available pipe on the freelist.
152 usb_get_freelist(struct usb_s *cntl, u8 eptype)
154 struct usb_pipe **pfree = &cntl->freelist;
156 struct usb_pipe *pipe = *pfree;
159 if (pipe->eptype == eptype) {
160 *pfree = pipe->freenext;
163 pfree = &pipe->freenext;
167 // Fill "pipe" endpoint info from an endpoint descriptor.
169 usb_desc2pipe(struct usb_pipe *pipe, struct usbdevice_s *usbdev
170 , struct usb_endpoint_descriptor *epdesc)
172 pipe->cntl = usbdev->hub->cntl;
173 pipe->type = usbdev->hub->cntl->type;
174 pipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
175 pipe->devaddr = usbdev->devaddr;
176 pipe->speed = usbdev->speed;
177 pipe->maxpacket = epdesc->wMaxPacketSize;
178 pipe->eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
181 // Find the exponential period of the requested interrupt end point.
183 usb_get_period(struct usbdevice_s *usbdev
184 , struct usb_endpoint_descriptor *epdesc)
186 int period = epdesc->bInterval;
187 if (usbdev->speed != USB_HIGHSPEED)
188 return (period <= 0) ? 0 : __fls(period);
189 return (period <= 4) ? 0 : period - 4;
192 // Maximum time (in ms) a data transfer should take
194 usb_xfer_time(struct usb_pipe *pipe, int datalen)
196 // Use the maximum command time (5 seconds), except for
197 // set_address commands where we don't want to stall the boot if
198 // the device doesn't actually exist. Add 100ms to account for
199 // any controller delays.
200 if (!GET_LOWFLAT(pipe->devaddr))
201 return USB_TIME_STATUS + 100;
202 return USB_TIME_COMMAND + 100;
205 // Find the first endpoint of a given type in an interface description.
206 struct usb_endpoint_descriptor *
207 usb_find_desc(struct usbdevice_s *usbdev, int type, int dir)
209 struct usb_endpoint_descriptor *epdesc = (void*)&usbdev->iface[1];
211 if ((void*)epdesc >= (void*)usbdev->iface + usbdev->imax
212 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
215 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
216 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
217 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
219 epdesc = (void*)epdesc + epdesc->bLength;
223 // Get the first 8 bytes of the device descriptor.
225 get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
227 struct usb_ctrlrequest req;
228 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
229 req.bRequest = USB_REQ_GET_DESCRIPTOR;
230 req.wValue = USB_DT_DEVICE<<8;
233 return usb_send_default_control(pipe, &req, dinfo);
236 static struct usb_config_descriptor *
237 get_device_config(struct usb_pipe *pipe)
239 struct usb_config_descriptor cfg;
241 struct usb_ctrlrequest req;
242 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
243 req.bRequest = USB_REQ_GET_DESCRIPTOR;
244 req.wValue = USB_DT_CONFIG<<8;
246 req.wLength = sizeof(cfg);
247 int ret = usb_send_default_control(pipe, &req, &cfg);
251 void *config = malloc_tmphigh(cfg.wTotalLength);
254 req.wLength = cfg.wTotalLength;
255 ret = usb_send_default_control(pipe, &req, config);
260 //hexdump(config, cfg.wTotalLength);
265 set_configuration(struct usb_pipe *pipe, u16 val)
267 struct usb_ctrlrequest req;
268 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
269 req.bRequest = USB_REQ_SET_CONFIGURATION;
273 return usb_send_default_control(pipe, &req, NULL);
277 /****************************************************************
278 * Initialization and enumeration
279 ****************************************************************/
281 static const int speed_to_ctlsize[] = {
282 [ USB_FULLSPEED ] = 8,
283 [ USB_LOWSPEED ] = 8,
284 [ USB_HIGHSPEED ] = 64,
285 [ USB_SUPERSPEED ] = 512,
288 // Assign an address to a device in the default state on the given
291 usb_set_address(struct usbdevice_s *usbdev)
294 struct usb_s *cntl = usbdev->hub->cntl;
295 dprintf(3, "set_address %p\n", cntl);
296 if (cntl->maxaddr >= USB_MAXADDR)
299 msleep(USB_TIME_RSTRCY);
301 // Create a pipe for the default address.
302 struct usb_endpoint_descriptor epdesc = {
303 .wMaxPacketSize = speed_to_ctlsize[usbdev->speed],
304 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
306 usbdev->defpipe = usb_alloc_pipe(usbdev, &epdesc);
307 if (!usbdev->defpipe)
310 // Send set_address command.
311 struct usb_ctrlrequest req;
312 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
313 req.bRequest = USB_REQ_SET_ADDRESS;
314 req.wValue = cntl->maxaddr + 1;
317 int ret = usb_send_default_control(usbdev->defpipe, &req, NULL);
319 usb_free_pipe(usbdev, usbdev->defpipe);
323 msleep(USB_TIME_SETADDR_RECOVERY);
326 usbdev->devaddr = cntl->maxaddr;
327 usbdev->defpipe = usb_realloc_pipe(usbdev, usbdev->defpipe, &epdesc);
328 if (!usbdev->defpipe)
333 // Called for every found device - see if a driver is available for
334 // this device and do setup if so.
336 configure_usb_device(struct usbdevice_s *usbdev)
339 dprintf(3, "config_usb: %p\n", usbdev->defpipe);
341 // Set the max packet size for endpoint 0 of this device.
342 struct usb_device_descriptor dinfo;
343 int ret = get_device_info8(usbdev->defpipe, &dinfo);
346 u16 maxpacket = dinfo.bMaxPacketSize0;
347 if (dinfo.bcdUSB >= 0x0300)
348 maxpacket = 1 << dinfo.bMaxPacketSize0;
349 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%d\n"
350 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
351 , dinfo.bDeviceProtocol, maxpacket);
354 struct usb_endpoint_descriptor epdesc = {
355 .wMaxPacketSize = maxpacket,
356 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
358 usbdev->defpipe = usb_realloc_pipe(usbdev, usbdev->defpipe, &epdesc);
359 if (!usbdev->defpipe)
363 struct usb_config_descriptor *config = get_device_config(usbdev->defpipe);
367 // Determine if a driver exists for this device - only look at the
368 // first interface of the first configuration.
369 struct usb_interface_descriptor *iface = (void*)(&config[1]);
370 if (iface->bInterfaceClass != USB_CLASS_HID
371 && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
372 && iface->bInterfaceClass != USB_CLASS_HUB)
373 // Not a supported device.
376 // Set the configuration.
377 ret = set_configuration(usbdev->defpipe, config->bConfigurationValue);
382 usbdev->config = config;
383 usbdev->iface = iface;
384 usbdev->imax = (void*)config + config->wTotalLength - (void*)iface;
385 if (iface->bInterfaceClass == USB_CLASS_HUB)
386 ret = usb_hub_setup(usbdev);
387 else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE) {
388 if (iface->bInterfaceProtocol == US_PR_BULK)
389 ret = usb_msc_setup(usbdev);
390 if (iface->bInterfaceProtocol == US_PR_UAS)
391 ret = usb_uas_setup(usbdev);
393 ret = usb_hid_setup(usbdev);
405 usb_hub_port_setup(void *data)
407 struct usbdevice_s *usbdev = data;
408 struct usbhub_s *hub = usbdev->hub;
409 u32 port = usbdev->port;
412 // Detect if device present (and possibly start reset)
413 int ret = hub->op->detect(hub, port);
417 if (ret < 0 || timer_check(hub->detectend))
423 // XXX - wait USB_TIME_ATTDB time?
425 // Reset port and determine device speed
426 mutex_lock(&hub->cntl->resetlock);
427 int ret = hub->op->reset(hub, port);
433 // Set address of port
434 ret = usb_set_address(usbdev);
436 hub->op->disconnect(hub, port);
439 mutex_unlock(&hub->cntl->resetlock);
441 // Configure the device
442 int count = configure_usb_device(usbdev);
443 usb_free_pipe(usbdev, usbdev->defpipe);
445 hub->op->disconnect(hub, port);
446 hub->devcount += count;
453 mutex_unlock(&hub->cntl->resetlock);
458 usb_enumerate(struct usbhub_s *hub)
460 u32 portcount = hub->portcount;
461 hub->threads = portcount;
462 hub->detectend = timer_calc(USB_TIME_SIGATT);
464 // Launch a thread for every port.
466 for (i=0; i<portcount; i++) {
467 struct usbdevice_s *usbdev = malloc_tmphigh(sizeof(*usbdev));
472 memset(usbdev, 0, sizeof(*usbdev));
475 run_thread(usb_hub_port_setup, usbdev);
478 // Wait for threads to complete.
484 __usb_setup(void *data)
486 dprintf(3, "init usb\n");
499 run_thread(__usb_setup, NULL);