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 send_control(u32 endp, int dir, const void *cmd, int cmdsize
23 , void *data, int datasize)
25 struct usb_s *cntl = endp2cntl(endp);
29 return uhci_control(endp, dir, cmd, cmdsize, data, datasize);
31 return ohci_control(endp, dir, cmd, cmdsize, data, datasize);
36 alloc_intr_pipe(u32 endp, int period)
38 struct usb_s *cntl = endp2cntl(endp);
39 // Find the exponential period of the requested time.
42 int frameexp = __fls(period);
46 return uhci_alloc_intr_pipe(endp, frameexp);
48 return ohci_alloc_intr_pipe(endp, frameexp);
53 usb_poll_intr(struct usb_pipe *pipe, void *data)
55 u32 endp = GET_FLATPTR(pipe->endp);
56 struct usb_s *cntl = endp2cntl(endp);
57 switch (GET_GLOBAL(cntl->type)) {
60 return uhci_poll_intr(pipe, data);
62 return ohci_poll_intr(pipe, data);
67 send_default_control(u32 endp, const struct usb_ctrlrequest *req, void *data)
69 return send_control(endp, req->bRequestType & USB_DIR_IN
70 , req, sizeof(*req), data, req->wLength);
73 // Get the first 8 bytes of the device descriptor.
75 get_device_info8(struct usb_device_descriptor *dinfo, u32 endp)
77 struct usb_ctrlrequest req;
78 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
79 req.bRequest = USB_REQ_GET_DESCRIPTOR;
80 req.wValue = USB_DT_DEVICE<<8;
83 return send_default_control(endp, &req, dinfo);
86 static struct usb_config_descriptor *
87 get_device_config(u32 endp)
89 struct usb_config_descriptor cfg;
91 struct usb_ctrlrequest req;
92 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
93 req.bRequest = USB_REQ_GET_DESCRIPTOR;
94 req.wValue = USB_DT_CONFIG<<8;
96 req.wLength = sizeof(cfg);
97 int ret = send_default_control(endp, &req, &cfg);
101 void *config = malloc_tmphigh(cfg.wTotalLength);
104 req.wLength = cfg.wTotalLength;
105 ret = send_default_control(endp, &req, config);
108 //hexdump(config, cfg.wTotalLength);
113 set_address(u32 endp)
115 dprintf(3, "set_address %x\n", endp);
116 struct usb_s *cntl = endp2cntl(endp);
117 if (cntl->maxaddr >= USB_MAXADDR)
120 struct usb_ctrlrequest req;
121 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
122 req.bRequest = USB_REQ_SET_ADDRESS;
123 req.wValue = cntl->maxaddr + 1;
126 int ret = send_default_control(endp, &req, NULL);
129 msleep(USB_TIME_SETADDR_RECOVERY);
132 return mkendp(cntl, cntl->maxaddr, 0, endp2speed(endp), endp2maxsize(endp));
136 set_configuration(u32 endp, u16 val)
138 struct usb_ctrlrequest req;
139 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
140 req.bRequest = USB_REQ_SET_CONFIGURATION;
144 return send_default_control(endp, &req, NULL);
147 // Called for every found device - see if a driver is available for
148 // this device and do setup if so.
150 configure_usb_device(struct usb_s *cntl, int lowspeed)
152 dprintf(3, "config_usb: %p %d\n", cntl, lowspeed);
155 u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
156 struct usb_device_descriptor dinfo;
157 int ret = get_device_info8(&dinfo, endp);
160 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
161 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
162 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
163 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
165 endp = mkendp(cntl, 0, 0, lowspeed, dinfo.bMaxPacketSize0);
168 struct usb_config_descriptor *config = get_device_config(endp);
172 // Determine if a driver exists for this device - only look at the
173 // first interface of the first configuration.
174 struct usb_interface_descriptor *iface = (void*)(&config[1]);
175 if ((iface->bInterfaceClass != USB_CLASS_HID
176 || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
177 || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
178 && (iface->bInterfaceClass != USB_CLASS_HUB))
179 // Not a supported device.
182 // Set the address and configure device.
183 endp = set_address(endp);
186 ret = set_configuration(endp, config->bConfigurationValue);
191 if (iface->bInterfaceClass == USB_CLASS_HUB) {
193 return usb_hub_init(endp);
195 ret = usb_keyboard_init(endp, iface, ((void*)config + config->wTotalLength
214 dprintf(3, "init usb\n");
216 usb_keyboard_setup();
218 // Look for USB controllers
221 foreachpci(bdf, max) {
222 u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
224 if (code >> 8 != PCI_CLASS_SERIAL_USB)
227 struct usb_s *cntl = &USBControllers[count];
230 if (code == PCI_CLASS_SERIAL_USB_UHCI)
231 run_thread(uhci_init, cntl);
232 else if (code == PCI_CLASS_SERIAL_USB_OHCI)
233 run_thread(ohci_init, cntl);
238 if (count >= ARRAY_SIZE(USBControllers))