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 // Free an allocated control or bulk pipe.
29 free_pipe(struct usb_pipe *pipe)
34 struct usb_s *cntl = endp2cntl(pipe->endp);
38 return uhci_free_pipe(pipe);
40 return ohci_free_pipe(pipe);
44 // Allocate a control pipe (which can only be used by 32bit code)
45 static struct usb_pipe *
46 alloc_control_pipe(u32 endp)
48 struct usb_s *cntl = endp2cntl(endp);
52 return uhci_alloc_control_pipe(endp);
54 return ohci_alloc_control_pipe(endp);
58 // Send a message on a control pipe using the default control descriptor.
60 send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
61 , void *data, int datasize)
64 struct usb_s *cntl = endp2cntl(pipe->endp);
68 return uhci_control(pipe, dir, cmd, cmdsize, data, datasize);
70 return ohci_control(pipe, dir, cmd, cmdsize, data, datasize);
75 alloc_bulk_pipe(u32 endp)
77 struct usb_s *cntl = endp2cntl(endp);
81 return uhci_alloc_bulk_pipe(endp);
88 usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize)
90 u32 endp = GET_FLATPTR(pipe->endp);
91 struct usb_s *cntl = endp2cntl(endp);
95 return uhci_send_bulk(pipe, dir, data, datasize);
102 alloc_intr_pipe(u32 endp, int period)
104 struct usb_s *cntl = endp2cntl(endp);
105 // Find the exponential period of the requested time.
108 int frameexp = __fls(period);
109 switch (cntl->type) {
112 return uhci_alloc_intr_pipe(endp, frameexp);
114 return ohci_alloc_intr_pipe(endp, frameexp);
119 usb_poll_intr(struct usb_pipe *pipe, void *data)
121 u32 endp = GET_FLATPTR(pipe->endp);
122 struct usb_s *cntl = endp2cntl(endp);
123 switch (GET_GLOBAL(cntl->type)) {
126 return uhci_poll_intr(pipe, data);
128 return ohci_poll_intr(pipe, data);
133 /****************************************************************
135 ****************************************************************/
137 // Find the first endpoing of a given type in an interface description.
138 struct usb_endpoint_descriptor *
139 findEndPointDesc(struct usb_interface_descriptor *iface, int imax
142 struct usb_endpoint_descriptor *epdesc = (void*)&iface[1];
144 if ((void*)epdesc >= (void*)iface + imax
145 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
148 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
149 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
150 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
152 epdesc = (void*)epdesc + epdesc->bLength;
156 // Change endpoint characteristics of the default control pipe.
158 usb_alter_control(struct usb_pipe *pipe, u32 endp)
163 // Build an encoded "endp" from an endpoint descriptor.
165 mkendpFromDesc(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
167 u32 endp = pipe->endp;
168 return mkendp(endp2cntl(endp), endp2devaddr(endp)
169 , epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK
170 , endp2speed(endp), epdesc->wMaxPacketSize);
173 // Send a message to the default control pipe of a device.
175 send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
178 return send_control(pipe, req->bRequestType & USB_DIR_IN
179 , req, sizeof(*req), data, req->wLength);
182 // Get the first 8 bytes of the device descriptor.
184 get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
186 struct usb_ctrlrequest req;
187 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
188 req.bRequest = USB_REQ_GET_DESCRIPTOR;
189 req.wValue = USB_DT_DEVICE<<8;
192 return send_default_control(pipe, &req, dinfo);
195 static struct usb_config_descriptor *
196 get_device_config(struct usb_pipe *pipe)
198 struct usb_config_descriptor cfg;
200 struct usb_ctrlrequest req;
201 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
202 req.bRequest = USB_REQ_GET_DESCRIPTOR;
203 req.wValue = USB_DT_CONFIG<<8;
205 req.wLength = sizeof(cfg);
206 int ret = send_default_control(pipe, &req, &cfg);
210 void *config = malloc_tmphigh(cfg.wTotalLength);
213 req.wLength = cfg.wTotalLength;
214 ret = send_default_control(pipe, &req, config);
217 //hexdump(config, cfg.wTotalLength);
222 set_configuration(struct usb_pipe *pipe, u16 val)
224 struct usb_ctrlrequest req;
225 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
226 req.bRequest = USB_REQ_SET_CONFIGURATION;
230 return send_default_control(pipe, &req, NULL);
234 /****************************************************************
235 * Initialization and enumeration
236 ****************************************************************/
238 // Assign an address to a device in the default state on the given
241 usb_set_address(struct usb_s *cntl, int lowspeed)
244 dprintf(3, "set_address %p\n", cntl);
245 if (cntl->maxaddr >= USB_MAXADDR)
248 struct usb_pipe *defpipe = cntl->defaultpipe;
249 u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
251 cntl->defaultpipe = defpipe = alloc_control_pipe(endp);
255 usb_alter_control(defpipe, endp);
257 msleep(USB_TIME_RSTRCY);
259 struct usb_ctrlrequest req;
260 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
261 req.bRequest = USB_REQ_SET_ADDRESS;
262 req.wValue = cntl->maxaddr + 1;
265 int ret = send_default_control(defpipe, &req, NULL);
269 msleep(USB_TIME_SETADDR_RECOVERY);
272 endp = mkendp(cntl, cntl->maxaddr, 0, lowspeed, 8);
273 return alloc_control_pipe(endp);
276 // Called for every found device - see if a driver is available for
277 // this device and do setup if so.
279 configure_usb_device(struct usb_pipe *pipe)
282 struct usb_s *cntl = endp2cntl(pipe->endp);
283 dprintf(3, "config_usb: %p\n", cntl);
285 // Set the max packet size for endpoint 0 of this device.
286 struct usb_device_descriptor dinfo;
287 int ret = get_device_info8(pipe, &dinfo);
290 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
291 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
292 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
293 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
295 u32 endp = mkendp(cntl, endp2devaddr(pipe->endp), 0
296 , endp2speed(pipe->endp), dinfo.bMaxPacketSize0);
297 usb_alter_control(pipe, endp);
300 struct usb_config_descriptor *config = get_device_config(pipe);
304 // Determine if a driver exists for this device - only look at the
305 // first interface of the first configuration.
306 struct usb_interface_descriptor *iface = (void*)(&config[1]);
307 if ((iface->bInterfaceClass != USB_CLASS_HID
308 || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
309 || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
310 && (iface->bInterfaceClass != USB_CLASS_MASS_STORAGE)
311 && (iface->bInterfaceClass != USB_CLASS_HUB))
312 // Not a supported device.
315 // Set the configuration.
316 ret = set_configuration(pipe, config->bConfigurationValue);
321 int imax = (void*)config + config->wTotalLength - (void*)iface;
322 if (iface->bInterfaceClass == USB_CLASS_HUB)
323 ret = usb_hub_init(pipe);
324 else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE)
325 ret = usb_msc_init(pipe, iface, imax);
327 ret = usb_keyboard_init(pipe, iface, imax);
345 dprintf(3, "init usb\n");
347 memset(&USBControllers, 0, sizeof(USBControllers));
348 usb_keyboard_setup();
350 // Look for USB controllers
353 foreachpci(bdf, max) {
354 u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
356 if (code >> 8 != PCI_CLASS_SERIAL_USB)
359 struct usb_s *cntl = &USBControllers[count];
362 if (code == PCI_CLASS_SERIAL_USB_UHCI)
363 run_thread(uhci_init, cntl);
364 else if (code == PCI_CLASS_SERIAL_USB_OHCI)
365 run_thread(ohci_init, cntl);
370 if (count >= ARRAY_SIZE(USBControllers))