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
21 /****************************************************************
22 * Controller function wrappers
23 ****************************************************************/
25 // Free an allocated control or bulk pipe.
27 free_pipe(struct usb_pipe *pipe)
35 return uhci_free_pipe(pipe);
37 return ohci_free_pipe(pipe);
41 // Allocate a control pipe to a default endpoint (which can only be
42 // used by 32bit code)
43 static struct usb_pipe *
44 alloc_default_control_pipe(struct usb_pipe *dummy)
46 switch (dummy->type) {
49 return uhci_alloc_control_pipe(dummy);
51 return ohci_alloc_control_pipe(dummy);
55 // Send a message on a control pipe using the default control descriptor.
57 send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
58 , void *data, int datasize)
64 return uhci_control(pipe, dir, cmd, cmdsize, data, datasize);
66 return ohci_control(pipe, dir, cmd, cmdsize, data, datasize);
70 // Fill "pipe" endpoint info from an endpoint descriptor.
72 desc2pipe(struct usb_pipe *newpipe, struct usb_pipe *origpipe
73 , struct usb_endpoint_descriptor *epdesc)
75 memcpy(newpipe, origpipe, sizeof(*newpipe));
76 newpipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
77 newpipe->maxpacket = epdesc->wMaxPacketSize;
81 alloc_bulk_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
83 struct usb_pipe dummy;
84 desc2pipe(&dummy, pipe, epdesc);
88 return uhci_alloc_bulk_pipe(&dummy);
95 usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
97 switch (GET_FLATPTR(pipe_fl->type)) {
100 return uhci_send_bulk(pipe_fl, dir, data, datasize);
107 alloc_intr_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
109 struct usb_pipe dummy;
110 desc2pipe(&dummy, pipe, epdesc);
111 // Find the exponential period of the requested time.
112 int period = epdesc->bInterval;
115 int frameexp = __fls(period);
116 switch (pipe->type) {
119 return uhci_alloc_intr_pipe(&dummy, frameexp);
121 return ohci_alloc_intr_pipe(&dummy, frameexp);
126 usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
128 switch (GET_FLATPTR(pipe_fl->type)) {
131 return uhci_poll_intr(pipe_fl, data);
133 return ohci_poll_intr(pipe_fl, data);
138 /****************************************************************
140 ****************************************************************/
142 // Find the first endpoing of a given type in an interface description.
143 struct usb_endpoint_descriptor *
144 findEndPointDesc(struct usb_interface_descriptor *iface, int imax
147 struct usb_endpoint_descriptor *epdesc = (void*)&iface[1];
149 if ((void*)epdesc >= (void*)iface + imax
150 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
153 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
154 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
155 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
157 epdesc = (void*)epdesc + epdesc->bLength;
161 // Send a message to the default control pipe of a device.
163 send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
166 return send_control(pipe, req->bRequestType & USB_DIR_IN
167 , req, sizeof(*req), data, req->wLength);
170 // Get the first 8 bytes of the device descriptor.
172 get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
174 struct usb_ctrlrequest req;
175 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
176 req.bRequest = USB_REQ_GET_DESCRIPTOR;
177 req.wValue = USB_DT_DEVICE<<8;
180 return send_default_control(pipe, &req, dinfo);
183 static struct usb_config_descriptor *
184 get_device_config(struct usb_pipe *pipe)
186 struct usb_config_descriptor cfg;
188 struct usb_ctrlrequest req;
189 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
190 req.bRequest = USB_REQ_GET_DESCRIPTOR;
191 req.wValue = USB_DT_CONFIG<<8;
193 req.wLength = sizeof(cfg);
194 int ret = send_default_control(pipe, &req, &cfg);
198 void *config = malloc_tmphigh(cfg.wTotalLength);
201 req.wLength = cfg.wTotalLength;
202 ret = send_default_control(pipe, &req, config);
205 //hexdump(config, cfg.wTotalLength);
210 set_configuration(struct usb_pipe *pipe, u16 val)
212 struct usb_ctrlrequest req;
213 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
214 req.bRequest = USB_REQ_SET_CONFIGURATION;
218 return send_default_control(pipe, &req, NULL);
222 /****************************************************************
223 * Initialization and enumeration
224 ****************************************************************/
226 // Assign an address to a device in the default state on the given
229 usb_set_address(struct usb_s *cntl, int lowspeed)
232 dprintf(3, "set_address %p\n", cntl);
233 if (cntl->maxaddr >= USB_MAXADDR)
236 struct usb_pipe *defpipe = cntl->defaultpipe;
238 // Create a pipe for the default address.
239 struct usb_pipe dummy;
240 memset(&dummy, 0, sizeof(dummy));
242 dummy.type = cntl->type;
244 cntl->defaultpipe = defpipe = alloc_default_control_pipe(&dummy);
248 defpipe->lowspeed = lowspeed;
250 msleep(USB_TIME_RSTRCY);
252 struct usb_ctrlrequest req;
253 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
254 req.bRequest = USB_REQ_SET_ADDRESS;
255 req.wValue = cntl->maxaddr + 1;
258 int ret = send_default_control(defpipe, &req, NULL);
262 msleep(USB_TIME_SETADDR_RECOVERY);
265 defpipe->devaddr = cntl->maxaddr;
266 struct usb_pipe *pipe = alloc_default_control_pipe(defpipe);
267 defpipe->devaddr = 0;
271 // Called for every found device - see if a driver is available for
272 // this device and do setup if so.
274 configure_usb_device(struct usb_pipe *pipe)
277 dprintf(3, "config_usb: %p\n", pipe);
279 // Set the max packet size for endpoint 0 of this device.
280 struct usb_device_descriptor dinfo;
281 int ret = get_device_info8(pipe, &dinfo);
284 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
285 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
286 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
287 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
289 pipe->maxpacket = dinfo.bMaxPacketSize0;
292 struct usb_config_descriptor *config = get_device_config(pipe);
296 // Determine if a driver exists for this device - only look at the
297 // first interface of the first configuration.
298 struct usb_interface_descriptor *iface = (void*)(&config[1]);
299 if ((iface->bInterfaceClass != USB_CLASS_HID
300 || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
301 || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
302 && (iface->bInterfaceClass != USB_CLASS_MASS_STORAGE)
303 && (iface->bInterfaceClass != USB_CLASS_HUB))
304 // Not a supported device.
307 // Set the configuration.
308 ret = set_configuration(pipe, config->bConfigurationValue);
313 int imax = (void*)config + config->wTotalLength - (void*)iface;
314 if (iface->bInterfaceClass == USB_CLASS_HUB)
315 ret = usb_hub_init(pipe);
316 else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE)
317 ret = usb_msc_init(pipe, iface, imax);
319 ret = usb_keyboard_init(pipe, iface, imax);
337 dprintf(3, "init usb\n");
339 usb_keyboard_setup();
341 // Look for USB controllers
344 foreachpci(bdf, max) {
345 u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
347 if (code >> 8 != PCI_CLASS_SERIAL_USB)
350 if (code == PCI_CLASS_SERIAL_USB_UHCI)
351 uhci_init(bdf, count);
352 else if (code == PCI_CLASS_SERIAL_USB_OHCI)
353 ohci_init(bdf, count);