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-msc.h" // usb_msc_init
17 #include "usb.h" // struct usb_s
18 #include "biosvar.h" // GET_GLOBAL
20 struct usb_s USBControllers[16] VAR16VISIBLE;
23 /****************************************************************
24 * Controller function wrappers
25 ****************************************************************/
27 // Send a message on a control pipe using the default control descriptor.
29 send_control(u32 endp, int dir, const void *cmd, int cmdsize
30 , void *data, int datasize)
32 struct usb_s *cntl = endp2cntl(endp);
36 return uhci_control(endp, dir, cmd, cmdsize, data, datasize);
38 return ohci_control(endp, dir, cmd, cmdsize, data, datasize);
43 alloc_bulk_pipe(u32 endp)
45 struct usb_s *cntl = endp2cntl(endp);
49 return uhci_alloc_bulk_pipe(endp);
56 usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize)
58 u32 endp = GET_FLATPTR(pipe->endp);
59 struct usb_s *cntl = endp2cntl(endp);
63 return uhci_send_bulk(pipe, dir, data, datasize);
70 alloc_intr_pipe(u32 endp, int period)
72 struct usb_s *cntl = endp2cntl(endp);
73 // Find the exponential period of the requested time.
76 int frameexp = __fls(period);
80 return uhci_alloc_intr_pipe(endp, frameexp);
82 return ohci_alloc_intr_pipe(endp, frameexp);
87 usb_poll_intr(struct usb_pipe *pipe, void *data)
89 u32 endp = GET_FLATPTR(pipe->endp);
90 struct usb_s *cntl = endp2cntl(endp);
91 switch (GET_GLOBAL(cntl->type)) {
94 return uhci_poll_intr(pipe, data);
96 return ohci_poll_intr(pipe, data);
101 /****************************************************************
103 ****************************************************************/
105 // Find the first endpoing of a given type in an interface description.
106 struct usb_endpoint_descriptor *
107 findEndPointDesc(struct usb_interface_descriptor *iface, int imax
110 struct usb_endpoint_descriptor *epdesc = (void*)&iface[1];
112 if ((void*)epdesc >= (void*)iface + imax
113 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
116 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
117 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
118 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
120 epdesc = (void*)epdesc + epdesc->bLength;
124 // Build an encoded "endp" from an endpoint descriptor.
126 mkendpFromDesc(u32 endp, struct usb_endpoint_descriptor *epdesc)
128 return mkendp(endp2cntl(endp), endp2devaddr(endp)
129 , epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK
130 , endp2speed(endp), epdesc->wMaxPacketSize);
133 // Send a message to the default control pipe of a device.
135 send_default_control(u32 endp, const struct usb_ctrlrequest *req, void *data)
137 return send_control(endp, req->bRequestType & USB_DIR_IN
138 , req, sizeof(*req), data, req->wLength);
141 // Get the first 8 bytes of the device descriptor.
143 get_device_info8(struct usb_device_descriptor *dinfo, u32 endp)
145 struct usb_ctrlrequest req;
146 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
147 req.bRequest = USB_REQ_GET_DESCRIPTOR;
148 req.wValue = USB_DT_DEVICE<<8;
151 return send_default_control(endp, &req, dinfo);
154 static struct usb_config_descriptor *
155 get_device_config(u32 endp)
157 struct usb_config_descriptor cfg;
159 struct usb_ctrlrequest req;
160 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
161 req.bRequest = USB_REQ_GET_DESCRIPTOR;
162 req.wValue = USB_DT_CONFIG<<8;
164 req.wLength = sizeof(cfg);
165 int ret = send_default_control(endp, &req, &cfg);
169 void *config = malloc_tmphigh(cfg.wTotalLength);
172 req.wLength = cfg.wTotalLength;
173 ret = send_default_control(endp, &req, config);
176 //hexdump(config, cfg.wTotalLength);
181 set_address(u32 endp)
183 dprintf(3, "set_address %x\n", endp);
184 struct usb_s *cntl = endp2cntl(endp);
185 if (cntl->maxaddr >= USB_MAXADDR)
188 struct usb_ctrlrequest req;
189 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
190 req.bRequest = USB_REQ_SET_ADDRESS;
191 req.wValue = cntl->maxaddr + 1;
194 int ret = send_default_control(endp, &req, NULL);
197 msleep(USB_TIME_SETADDR_RECOVERY);
200 return mkendp(cntl, cntl->maxaddr, 0, endp2speed(endp), endp2maxsize(endp));
204 set_configuration(u32 endp, u16 val)
206 struct usb_ctrlrequest req;
207 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
208 req.bRequest = USB_REQ_SET_CONFIGURATION;
212 return send_default_control(endp, &req, NULL);
216 /****************************************************************
217 * Initialization and enumeration
218 ****************************************************************/
220 // Called for every found device - see if a driver is available for
221 // this device and do setup if so.
223 configure_usb_device(struct usb_s *cntl, int lowspeed)
225 dprintf(3, "config_usb: %p %d\n", cntl, lowspeed);
228 u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
229 struct usb_device_descriptor dinfo;
230 int ret = get_device_info8(&dinfo, endp);
233 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
234 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
235 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
236 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
238 endp = mkendp(cntl, 0, 0, lowspeed, dinfo.bMaxPacketSize0);
241 struct usb_config_descriptor *config = get_device_config(endp);
245 // Determine if a driver exists for this device - only look at the
246 // first interface of the first configuration.
247 struct usb_interface_descriptor *iface = (void*)(&config[1]);
248 if ((iface->bInterfaceClass != USB_CLASS_HID
249 || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
250 || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
251 && (iface->bInterfaceClass != USB_CLASS_MASS_STORAGE)
252 && (iface->bInterfaceClass != USB_CLASS_HUB))
253 // Not a supported device.
256 // Set the address and configure device.
257 endp = set_address(endp);
260 ret = set_configuration(endp, config->bConfigurationValue);
265 if (iface->bInterfaceClass == USB_CLASS_HUB) {
267 return usb_hub_init(endp);
269 int imax = (void*)config + config->wTotalLength - (void*)iface;
270 if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE)
271 ret = usb_msc_init(endp, iface, imax);
273 ret = usb_keyboard_init(endp, iface, imax);
291 dprintf(3, "init usb\n");
293 usb_keyboard_setup();
295 // Look for USB controllers
298 foreachpci(bdf, max) {
299 u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
301 if (code >> 8 != PCI_CLASS_SERIAL_USB)
304 struct usb_s *cntl = &USBControllers[count];
307 if (code == PCI_CLASS_SERIAL_USB_UHCI)
308 run_thread(uhci_init, cntl);
309 else if (code == PCI_CLASS_SERIAL_USB_OHCI)
310 run_thread(ohci_init, cntl);
315 if (count >= ARRAY_SIZE(USBControllers))