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-hid.h" // usb_keyboard_setup
15 #include "usb-hub.h" // usb_hub_init
16 #include "usb.h" // struct usb_s
17 #include "biosvar.h" // GET_GLOBAL
19 struct usb_s USBControllers[16] VAR16VISIBLE;
22 /****************************************************************
23 * Controller function wrappers
24 ****************************************************************/
26 // Send a message on a control pipe using the default control descriptor.
28 send_control(u32 endp, int dir, const void *cmd, int cmdsize
29 , void *data, int datasize)
31 struct usb_s *cntl = endp2cntl(endp);
35 return uhci_control(endp, dir, cmd, cmdsize, data, datasize);
37 return ohci_control(endp, dir, cmd, cmdsize, data, datasize);
42 alloc_intr_pipe(u32 endp, int period)
44 struct usb_s *cntl = endp2cntl(endp);
45 // Find the exponential period of the requested time.
48 int frameexp = __fls(period);
52 return uhci_alloc_intr_pipe(endp, frameexp);
54 return ohci_alloc_intr_pipe(endp, frameexp);
59 usb_poll_intr(struct usb_pipe *pipe, void *data)
61 u32 endp = GET_FLATPTR(pipe->endp);
62 struct usb_s *cntl = endp2cntl(endp);
63 switch (GET_GLOBAL(cntl->type)) {
66 return uhci_poll_intr(pipe, data);
68 return ohci_poll_intr(pipe, data);
73 /****************************************************************
75 ****************************************************************/
77 // Find the first endpoing of a given type in an interface description.
78 struct usb_endpoint_descriptor *
79 findEndPointDesc(struct usb_interface_descriptor *iface, int imax
82 struct usb_endpoint_descriptor *epdesc = (void*)&iface[1];
84 if ((void*)epdesc >= (void*)iface + imax
85 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
88 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
89 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
90 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
92 epdesc = (void*)epdesc + epdesc->bLength;
96 // Build an encoded "endp" from an endpoint descriptor.
98 mkendpFromDesc(u32 endp, struct usb_endpoint_descriptor *epdesc)
100 return mkendp(endp2cntl(endp), endp2devaddr(endp)
101 , epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK
102 , endp2speed(endp), epdesc->wMaxPacketSize);
105 // Send a message to the default control pipe of a device.
107 send_default_control(u32 endp, const struct usb_ctrlrequest *req, void *data)
109 return send_control(endp, req->bRequestType & USB_DIR_IN
110 , req, sizeof(*req), data, req->wLength);
113 // Get the first 8 bytes of the device descriptor.
115 get_device_info8(struct usb_device_descriptor *dinfo, u32 endp)
117 struct usb_ctrlrequest req;
118 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
119 req.bRequest = USB_REQ_GET_DESCRIPTOR;
120 req.wValue = USB_DT_DEVICE<<8;
123 return send_default_control(endp, &req, dinfo);
126 static struct usb_config_descriptor *
127 get_device_config(u32 endp)
129 struct usb_config_descriptor cfg;
131 struct usb_ctrlrequest req;
132 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
133 req.bRequest = USB_REQ_GET_DESCRIPTOR;
134 req.wValue = USB_DT_CONFIG<<8;
136 req.wLength = sizeof(cfg);
137 int ret = send_default_control(endp, &req, &cfg);
141 void *config = malloc_tmphigh(cfg.wTotalLength);
144 req.wLength = cfg.wTotalLength;
145 ret = send_default_control(endp, &req, config);
148 //hexdump(config, cfg.wTotalLength);
153 set_address(u32 endp)
155 dprintf(3, "set_address %x\n", endp);
156 struct usb_s *cntl = endp2cntl(endp);
157 if (cntl->maxaddr >= USB_MAXADDR)
160 struct usb_ctrlrequest req;
161 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
162 req.bRequest = USB_REQ_SET_ADDRESS;
163 req.wValue = cntl->maxaddr + 1;
166 int ret = send_default_control(endp, &req, NULL);
169 msleep(USB_TIME_SETADDR_RECOVERY);
172 return mkendp(cntl, cntl->maxaddr, 0, endp2speed(endp), endp2maxsize(endp));
176 set_configuration(u32 endp, u16 val)
178 struct usb_ctrlrequest req;
179 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
180 req.bRequest = USB_REQ_SET_CONFIGURATION;
184 return send_default_control(endp, &req, NULL);
188 /****************************************************************
189 * Initialization and enumeration
190 ****************************************************************/
192 // Called for every found device - see if a driver is available for
193 // this device and do setup if so.
195 configure_usb_device(struct usb_s *cntl, int lowspeed)
197 dprintf(3, "config_usb: %p %d\n", cntl, lowspeed);
200 u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
201 struct usb_device_descriptor dinfo;
202 int ret = get_device_info8(&dinfo, endp);
205 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
206 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
207 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
208 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
210 endp = mkendp(cntl, 0, 0, lowspeed, dinfo.bMaxPacketSize0);
213 struct usb_config_descriptor *config = get_device_config(endp);
217 // Determine if a driver exists for this device - only look at the
218 // first interface of the first configuration.
219 struct usb_interface_descriptor *iface = (void*)(&config[1]);
220 if ((iface->bInterfaceClass != USB_CLASS_HID
221 || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
222 || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
223 && (iface->bInterfaceClass != USB_CLASS_HUB))
224 // Not a supported device.
227 // Set the address and configure device.
228 endp = set_address(endp);
231 ret = set_configuration(endp, config->bConfigurationValue);
236 if (iface->bInterfaceClass == USB_CLASS_HUB) {
238 return usb_hub_init(endp);
240 ret = usb_keyboard_init(endp, iface, ((void*)config + config->wTotalLength
259 dprintf(3, "init usb\n");
261 usb_keyboard_setup();
263 // Look for USB controllers
266 foreachpci(bdf, max) {
267 u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
269 if (code >> 8 != PCI_CLASS_SERIAL_USB)
272 struct usb_s *cntl = &USBControllers[count];
275 if (code == PCI_CLASS_SERIAL_USB_UHCI)
276 run_thread(uhci_init, cntl);
277 else if (code == PCI_CLASS_SERIAL_USB_OHCI)
278 run_thread(ohci_init, cntl);
283 if (count >= ARRAY_SIZE(USBControllers))