1 // Main code for handling USB controllers and devices.
3 // Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
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
13 #include "usb-ohci.h" // ohci_init
14 #include "usb-ehci.h" // ehci_init
15 #include "usb-hid.h" // usb_keyboard_setup
16 #include "usb-hub.h" // usb_hub_init
17 #include "usb-msc.h" // usb_msc_init
18 #include "usb.h" // struct usb_s
19 #include "biosvar.h" // GET_GLOBAL
22 /****************************************************************
23 * Controller function wrappers
24 ****************************************************************/
26 // Free an allocated control or bulk pipe.
28 free_pipe(struct usb_pipe *pipe)
36 return uhci_free_pipe(pipe);
38 return ohci_free_pipe(pipe);
40 return ehci_free_pipe(pipe);
44 // Allocate a control pipe to a default endpoint (which can only be
45 // used by 32bit code)
46 static struct usb_pipe *
47 alloc_default_control_pipe(struct usb_pipe *dummy)
49 switch (dummy->type) {
52 return uhci_alloc_control_pipe(dummy);
54 return ohci_alloc_control_pipe(dummy);
56 return ehci_alloc_control_pipe(dummy);
60 // Send a message on a control pipe using the default control descriptor.
62 send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
63 , void *data, int datasize)
69 return uhci_control(pipe, dir, cmd, cmdsize, data, datasize);
71 return ohci_control(pipe, dir, cmd, cmdsize, data, datasize);
73 return ehci_control(pipe, dir, cmd, cmdsize, data, datasize);
77 // Fill "pipe" endpoint info from an endpoint descriptor.
79 desc2pipe(struct usb_pipe *newpipe, struct usb_pipe *origpipe
80 , struct usb_endpoint_descriptor *epdesc)
82 memcpy(newpipe, origpipe, sizeof(*newpipe));
83 newpipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
84 newpipe->maxpacket = epdesc->wMaxPacketSize;
88 alloc_bulk_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
90 struct usb_pipe dummy;
91 desc2pipe(&dummy, pipe, epdesc);
95 return uhci_alloc_bulk_pipe(&dummy);
97 return ohci_alloc_bulk_pipe(&dummy);
99 return ehci_alloc_bulk_pipe(&dummy);
104 usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
106 switch (GET_FLATPTR(pipe_fl->type)) {
109 return uhci_send_bulk(pipe_fl, dir, data, datasize);
111 return ohci_send_bulk(pipe_fl, dir, data, datasize);
113 return ehci_send_bulk(pipe_fl, dir, data, datasize);
118 alloc_intr_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
120 struct usb_pipe dummy;
121 desc2pipe(&dummy, pipe, epdesc);
122 // Find the exponential period of the requested time.
123 int period = epdesc->bInterval;
125 if (pipe->speed != USB_HIGHSPEED)
126 frameexp = (period <= 0) ? 0 : __fls(period);
128 frameexp = (period <= 4) ? 0 : period - 4;
129 switch (pipe->type) {
132 return uhci_alloc_intr_pipe(&dummy, frameexp);
134 return ohci_alloc_intr_pipe(&dummy, frameexp);
136 return ehci_alloc_intr_pipe(&dummy, frameexp);
141 usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
143 switch (GET_FLATPTR(pipe_fl->type)) {
146 return uhci_poll_intr(pipe_fl, data);
148 return ohci_poll_intr(pipe_fl, data);
150 return ehci_poll_intr(pipe_fl, data);
155 /****************************************************************
157 ****************************************************************/
159 // Find the first endpoing of a given type in an interface description.
160 struct usb_endpoint_descriptor *
161 findEndPointDesc(struct usb_interface_descriptor *iface, int imax
164 struct usb_endpoint_descriptor *epdesc = (void*)&iface[1];
166 if ((void*)epdesc >= (void*)iface + imax
167 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
170 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
171 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
172 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
174 epdesc = (void*)epdesc + epdesc->bLength;
178 // Send a message to the default control pipe of a device.
180 send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
183 return send_control(pipe, req->bRequestType & USB_DIR_IN
184 , req, sizeof(*req), data, req->wLength);
187 // Get the first 8 bytes of the device descriptor.
189 get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
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;
197 return send_default_control(pipe, &req, dinfo);
200 static struct usb_config_descriptor *
201 get_device_config(struct usb_pipe *pipe)
203 struct usb_config_descriptor cfg;
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;
210 req.wLength = sizeof(cfg);
211 int ret = send_default_control(pipe, &req, &cfg);
215 void *config = malloc_tmphigh(cfg.wTotalLength);
218 req.wLength = cfg.wTotalLength;
219 ret = send_default_control(pipe, &req, config);
222 //hexdump(config, cfg.wTotalLength);
227 set_configuration(struct usb_pipe *pipe, u16 val)
229 struct usb_ctrlrequest req;
230 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
231 req.bRequest = USB_REQ_SET_CONFIGURATION;
235 return send_default_control(pipe, &req, NULL);
239 /****************************************************************
240 * Initialization and enumeration
241 ****************************************************************/
243 // Assign an address to a device in the default state on the given
245 static struct usb_pipe *
246 usb_set_address(struct usbhub_s *hub, int port, int speed)
249 struct usb_s *cntl = hub->cntl;
250 dprintf(3, "set_address %p\n", cntl);
251 if (cntl->maxaddr >= USB_MAXADDR)
254 struct usb_pipe *defpipe = cntl->defaultpipe;
256 // Create a pipe for the default address.
257 struct usb_pipe dummy;
258 memset(&dummy, 0, sizeof(dummy));
260 dummy.type = cntl->type;
262 dummy.path = (u64)-1;
263 cntl->defaultpipe = defpipe = alloc_default_control_pipe(&dummy);
267 defpipe->speed = speed;
269 if (hub->pipe->speed == USB_HIGHSPEED) {
270 defpipe->tt_devaddr = hub->pipe->devaddr;
271 defpipe->tt_port = port;
273 defpipe->tt_devaddr = hub->pipe->tt_devaddr;
274 defpipe->tt_port = hub->pipe->tt_port;
277 defpipe->tt_devaddr = defpipe->tt_port = 0;
280 msleep(USB_TIME_RSTRCY);
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;
288 int ret = send_default_control(defpipe, &req, NULL);
292 msleep(USB_TIME_SETADDR_RECOVERY);
295 defpipe->devaddr = cntl->maxaddr;
296 struct usb_pipe *pipe = alloc_default_control_pipe(defpipe);
297 defpipe->devaddr = 0;
299 pipe->path = hub->pipe->path;
300 pipe->path = (pipe->path << 8) | port;
304 // Called for every found device - see if a driver is available for
305 // this device and do setup if so.
307 configure_usb_device(struct usb_pipe *pipe)
310 dprintf(3, "config_usb: %p\n", pipe);
312 // Set the max packet size for endpoint 0 of this device.
313 struct usb_device_descriptor dinfo;
314 int ret = get_device_info8(pipe, &dinfo);
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)
322 pipe->maxpacket = dinfo.bMaxPacketSize0;
325 struct usb_config_descriptor *config = get_device_config(pipe);
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]);
332 if (iface->bInterfaceClass != USB_CLASS_HID
333 && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
334 && iface->bInterfaceClass != USB_CLASS_HUB)
335 // Not a supported device.
338 // Set the configuration.
339 ret = set_configuration(pipe, config->bConfigurationValue);
344 int imax = (void*)config + config->wTotalLength - (void*)iface;
345 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);
350 ret = usb_hid_init(pipe, iface, imax);
362 usb_init_hub_port(void *data)
364 struct usbhub_s *hub = data;
365 u32 port = hub->port; // XXX - find better way to pass port
367 // Detect if device present (and possibly start reset)
368 int ret = hub->op->detect(hub, port);
373 // Reset port and determine device speed
374 mutex_lock(&hub->cntl->resetlock);
375 ret = hub->op->reset(hub, port);
380 // Set address of port
381 struct usb_pipe *pipe = usb_set_address(hub, port, ret);
383 hub->op->disconnect(hub, port);
386 mutex_unlock(&hub->cntl->resetlock);
388 // Configure the device
389 int count = configure_usb_device(pipe);
392 hub->op->disconnect(hub, port);
393 hub->devcount += count;
399 mutex_unlock(&hub->cntl->resetlock);
404 usb_enumerate(struct usbhub_s *hub)
406 u32 portcount = hub->portcount;
407 hub->threads = portcount;
409 // Launch a thread for every port.
411 for (i=0; i<portcount; i++) {
413 run_thread(usb_init_hub_port, hub);
416 // Wait for threads to complete.
428 dprintf(3, "init usb\n");
430 // Look for USB controllers
432 struct pci_device *ehcipci = PCIDevices;
433 struct pci_device *pci;
435 if (pci->class != PCI_CLASS_SERIAL_USB)
438 if (pci->bdf >= ehcipci->bdf) {
439 // Check to see if this device has an ehci controller
443 if (pci_classprog(ehcipci) == PCI_CLASS_SERIAL_USB_EHCI) {
444 // Found an ehci controller.
445 int ret = ehci_init(ehcipci, count++, pci);
453 if (ehcipci->class == PCI_CLASS_SERIAL_USB)
455 ehcipci = ehcipci->next;
456 if (!ehcipci || (pci_bdf_to_busdev(ehcipci->bdf)
457 != pci_bdf_to_busdev(pci->bdf)))
458 // No ehci controller found.
463 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
464 uhci_init(pci, count++);
465 else if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
466 ohci_init(pci, count++);