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
17 struct usb_s USBControllers[16] VAR16VISIBLE;
20 send_control(u32 endp, int dir, const void *cmd, int cmdsize
21 , void *data, int datasize)
23 struct usb_s *cntl = endp2cntl(endp);
27 return uhci_control(endp, dir, cmd, cmdsize, data, datasize);
29 return ohci_control(endp, dir, cmd, cmdsize, data, datasize);
34 alloc_intr_pipe(u32 endp, int period)
36 struct usb_s *cntl = endp2cntl(endp);
40 return uhci_alloc_intr_pipe(endp, period);
42 return ohci_alloc_intr_pipe(endp, period);
47 usb_poll_intr(struct usb_pipe *pipe, void *data)
49 struct usb_s *cntl = endp2cntl(pipe->endp);
53 return uhci_poll_intr(pipe, data);
55 return ohci_poll_intr(pipe, data);
60 send_default_control(u32 endp, const struct usb_ctrlrequest *req, void *data)
62 return send_control(endp, req->bRequestType & USB_DIR_IN
63 , req, sizeof(*req), data, req->wLength);
66 // Get the first 8 bytes of the device descriptor.
68 get_device_info8(struct usb_device_descriptor *dinfo, u32 endp)
70 struct usb_ctrlrequest req;
71 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
72 req.bRequest = USB_REQ_GET_DESCRIPTOR;
73 req.wValue = USB_DT_DEVICE<<8;
76 return send_default_control(endp, &req, dinfo);
79 static struct usb_config_descriptor *
80 get_device_config(u32 endp)
82 struct usb_config_descriptor cfg;
84 struct usb_ctrlrequest req;
85 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
86 req.bRequest = USB_REQ_GET_DESCRIPTOR;
87 req.wValue = USB_DT_CONFIG<<8;
89 req.wLength = sizeof(cfg);
90 int ret = send_default_control(endp, &req, &cfg);
94 void *config = malloc_tmphigh(cfg.wTotalLength);
97 req.wLength = cfg.wTotalLength;
98 ret = send_default_control(endp, &req, config);
101 //hexdump(config, cfg.wTotalLength);
106 set_address(u32 endp)
108 dprintf(3, "set_address %x\n", endp);
109 struct usb_s *cntl = endp2cntl(endp);
110 if (cntl->maxaddr >= USB_MAXADDR)
113 struct usb_ctrlrequest req;
114 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
115 req.bRequest = USB_REQ_SET_ADDRESS;
116 req.wValue = cntl->maxaddr + 1;
119 int ret = send_default_control(endp, &req, NULL);
125 return mkendp(cntl, cntl->maxaddr, 0, endp2speed(endp), endp2maxsize(endp));
129 set_configuration(u32 endp, u16 val)
131 struct usb_ctrlrequest req;
132 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
133 req.bRequest = USB_REQ_SET_CONFIGURATION;
137 return send_default_control(endp, &req, NULL);
140 // Called for every found device - see if a driver is available for
141 // this device and do setup if so.
143 configure_usb_device(struct usb_s *cntl, int lowspeed)
145 dprintf(1, "config_usb: %p %d\n", cntl, lowspeed);
148 u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
149 struct usb_device_descriptor dinfo;
150 int ret = get_device_info8(&dinfo, endp);
153 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
154 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
155 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
156 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
158 endp = mkendp(cntl, 0, 0, lowspeed, dinfo.bMaxPacketSize0);
161 struct usb_config_descriptor *config = get_device_config(endp);
165 // Determine if a driver exists for this device - only look at the
166 // first interface of the first configuration.
167 struct usb_interface_descriptor *iface = (void*)(&config[1]);
168 if (iface->bInterfaceClass != USB_CLASS_HID
169 || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
170 || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
171 // Not a "boot" keyboard
174 // Set the address and configure device.
175 endp = set_address(endp);
178 ret = set_configuration(endp, config->bConfigurationValue);
183 ret = usb_keyboard_init(endp, iface, ((void*)config + config->wTotalLength
188 // XXX - free(config);
191 // XXX - free(config);
201 dprintf(3, "init usb\n");
203 usb_keyboard_setup();
205 // Look for USB controllers
208 foreachpci(bdf, max) {
209 u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
211 if (code >> 8 != PCI_CLASS_SERIAL_USB)
214 struct usb_s *cntl = &USBControllers[count];
218 if (code == PCI_CLASS_SERIAL_USB_UHCI)
219 devcount = uhci_init(cntl);
220 else if (code == PCI_CLASS_SERIAL_USB_OHCI)
221 devcount = ohci_init(cntl);
226 if (count >= ARRAY_SIZE(USBControllers))