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.h" // struct usb_s
16 #include "biosvar.h" // GET_GLOBAL
18 struct usb_s USBControllers[16] VAR16VISIBLE;
21 send_control(u32 endp, int dir, const void *cmd, int cmdsize
22 , void *data, int datasize)
24 struct usb_s *cntl = endp2cntl(endp);
28 return uhci_control(endp, dir, cmd, cmdsize, data, datasize);
30 return ohci_control(endp, dir, cmd, cmdsize, data, datasize);
35 alloc_intr_pipe(u32 endp, int period)
37 struct usb_s *cntl = endp2cntl(endp);
41 return uhci_alloc_intr_pipe(endp, period);
43 return ohci_alloc_intr_pipe(endp, period);
48 usb_poll_intr(struct usb_pipe *pipe, void *data)
50 u32 endp = GET_FLATPTR(pipe->endp);
51 struct usb_s *cntl = endp2cntl(endp);
52 switch (GET_GLOBAL(cntl->type)) {
55 return uhci_poll_intr(pipe, data);
57 return ohci_poll_intr(pipe, data);
62 send_default_control(u32 endp, const struct usb_ctrlrequest *req, void *data)
64 return send_control(endp, req->bRequestType & USB_DIR_IN
65 , req, sizeof(*req), data, req->wLength);
68 // Get the first 8 bytes of the device descriptor.
70 get_device_info8(struct usb_device_descriptor *dinfo, u32 endp)
72 struct usb_ctrlrequest req;
73 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
74 req.bRequest = USB_REQ_GET_DESCRIPTOR;
75 req.wValue = USB_DT_DEVICE<<8;
78 return send_default_control(endp, &req, dinfo);
81 static struct usb_config_descriptor *
82 get_device_config(u32 endp)
84 struct usb_config_descriptor cfg;
86 struct usb_ctrlrequest req;
87 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
88 req.bRequest = USB_REQ_GET_DESCRIPTOR;
89 req.wValue = USB_DT_CONFIG<<8;
91 req.wLength = sizeof(cfg);
92 int ret = send_default_control(endp, &req, &cfg);
96 void *config = malloc_tmphigh(cfg.wTotalLength);
99 req.wLength = cfg.wTotalLength;
100 ret = send_default_control(endp, &req, config);
103 //hexdump(config, cfg.wTotalLength);
108 set_address(u32 endp)
110 dprintf(3, "set_address %x\n", endp);
111 struct usb_s *cntl = endp2cntl(endp);
112 if (cntl->maxaddr >= USB_MAXADDR)
115 struct usb_ctrlrequest req;
116 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
117 req.bRequest = USB_REQ_SET_ADDRESS;
118 req.wValue = cntl->maxaddr + 1;
121 int ret = send_default_control(endp, &req, NULL);
127 return mkendp(cntl, cntl->maxaddr, 0, endp2speed(endp), endp2maxsize(endp));
131 set_configuration(u32 endp, u16 val)
133 struct usb_ctrlrequest req;
134 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
135 req.bRequest = USB_REQ_SET_CONFIGURATION;
139 return send_default_control(endp, &req, NULL);
142 // Called for every found device - see if a driver is available for
143 // this device and do setup if so.
145 configure_usb_device(struct usb_s *cntl, int lowspeed)
147 dprintf(1, "config_usb: %p %d\n", cntl, lowspeed);
150 u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
151 struct usb_device_descriptor dinfo;
152 int ret = get_device_info8(&dinfo, endp);
155 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
156 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
157 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
158 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
160 endp = mkendp(cntl, 0, 0, lowspeed, dinfo.bMaxPacketSize0);
163 struct usb_config_descriptor *config = get_device_config(endp);
167 // Determine if a driver exists for this device - only look at the
168 // first interface of the first configuration.
169 struct usb_interface_descriptor *iface = (void*)(&config[1]);
170 if (iface->bInterfaceClass != USB_CLASS_HID
171 || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
172 || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
173 // Not a "boot" keyboard
176 // Set the address and configure device.
177 endp = set_address(endp);
180 ret = set_configuration(endp, config->bConfigurationValue);
185 ret = usb_keyboard_init(endp, iface, ((void*)config + config->wTotalLength
203 dprintf(3, "init usb\n");
205 usb_keyboard_setup();
207 // Look for USB controllers
210 foreachpci(bdf, max) {
211 u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
213 if (code >> 8 != PCI_CLASS_SERIAL_USB)
216 struct usb_s *cntl = &USBControllers[count];
219 if (code == PCI_CLASS_SERIAL_USB_UHCI)
220 run_thread(uhci_init, cntl);
221 else if (code == PCI_CLASS_SERIAL_USB_OHCI)
222 run_thread(ohci_init, cntl);
227 if (count >= ARRAY_SIZE(USBControllers))