blob: cc3b2012c0fb62cb6c66598c04409f3c73464f1a [file] [log] [blame]
Kevin O'Connor114592f2009-09-28 21:32:08 -04001// Main code for handling USB controllers and devices.
2//
3// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
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
Kevin O'Connor59f02832009-10-12 10:09:15 -040013#include "usb-ohci.h" // ohci_init
Kevin O'Connor114592f2009-09-28 21:32:08 -040014#include "usb-hid.h" // usb_keyboard_setup
Kevin O'Connor54671c12010-02-15 18:58:12 -050015#include "usb-hub.h" // usb_hub_init
Kevin O'Connor7149fc82010-02-17 23:24:42 -050016#include "usb-msc.h" // usb_msc_init
Kevin O'Connor114592f2009-09-28 21:32:08 -040017#include "usb.h" // struct usb_s
Kevin O'Connor1c46a542009-10-17 23:53:32 -040018#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor114592f2009-09-28 21:32:08 -040019
20struct usb_s USBControllers[16] VAR16VISIBLE;
21
Kevin O'Connor3c160dd2010-02-17 23:06:52 -050022
23/****************************************************************
24 * Controller function wrappers
25 ****************************************************************/
26
27// Send a message on a control pipe using the default control descriptor.
Kevin O'Connor114592f2009-09-28 21:32:08 -040028static int
29send_control(u32 endp, int dir, const void *cmd, int cmdsize
30 , void *data, int datasize)
31{
Kevin O'Connor59f02832009-10-12 10:09:15 -040032 struct usb_s *cntl = endp2cntl(endp);
33 switch (cntl->type) {
34 default:
35 case USB_TYPE_UHCI:
36 return uhci_control(endp, dir, cmd, cmdsize, data, datasize);
37 case USB_TYPE_OHCI:
38 return ohci_control(endp, dir, cmd, cmdsize, data, datasize);
39 }
Kevin O'Connor114592f2009-09-28 21:32:08 -040040}
41
Kevin O'Connor59f02832009-10-12 10:09:15 -040042struct usb_pipe *
Kevin O'Connor7149fc82010-02-17 23:24:42 -050043alloc_bulk_pipe(u32 endp)
44{
45 struct usb_s *cntl = endp2cntl(endp);
46 switch (cntl->type) {
47 default:
48 case USB_TYPE_UHCI:
49 return uhci_alloc_bulk_pipe(endp);
50 case USB_TYPE_OHCI:
51 return NULL;
52 }
53}
54
55int
56usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize)
57{
58 u32 endp = GET_FLATPTR(pipe->endp);
59 struct usb_s *cntl = endp2cntl(endp);
60 switch (cntl->type) {
61 default:
62 case USB_TYPE_UHCI:
63 return uhci_send_bulk(pipe, dir, data, datasize);
64 case USB_TYPE_OHCI:
65 return -1;
66 }
67}
68
69struct usb_pipe *
Kevin O'Connor114592f2009-09-28 21:32:08 -040070alloc_intr_pipe(u32 endp, int period)
71{
Kevin O'Connor59f02832009-10-12 10:09:15 -040072 struct usb_s *cntl = endp2cntl(endp);
Kevin O'Connor991eaff2010-02-13 21:51:47 -050073 // Find the exponential period of the requested time.
74 if (period <= 0)
75 period = 1;
76 int frameexp = __fls(period);
Kevin O'Connor59f02832009-10-12 10:09:15 -040077 switch (cntl->type) {
78 default:
79 case USB_TYPE_UHCI:
Kevin O'Connor991eaff2010-02-13 21:51:47 -050080 return uhci_alloc_intr_pipe(endp, frameexp);
Kevin O'Connor59f02832009-10-12 10:09:15 -040081 case USB_TYPE_OHCI:
Kevin O'Connor991eaff2010-02-13 21:51:47 -050082 return ohci_alloc_intr_pipe(endp, frameexp);
Kevin O'Connor59f02832009-10-12 10:09:15 -040083 }
Kevin O'Connor114592f2009-09-28 21:32:08 -040084}
85
Kevin O'Connor84a4d4b2010-02-11 22:32:12 -050086int noinline
Kevin O'Connor59f02832009-10-12 10:09:15 -040087usb_poll_intr(struct usb_pipe *pipe, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -040088{
Kevin O'Connor1c46a542009-10-17 23:53:32 -040089 u32 endp = GET_FLATPTR(pipe->endp);
90 struct usb_s *cntl = endp2cntl(endp);
91 switch (GET_GLOBAL(cntl->type)) {
Kevin O'Connor59f02832009-10-12 10:09:15 -040092 default:
93 case USB_TYPE_UHCI:
94 return uhci_poll_intr(pipe, data);
95 case USB_TYPE_OHCI:
96 return ohci_poll_intr(pipe, data);
97 }
Kevin O'Connor114592f2009-09-28 21:32:08 -040098}
99
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500100
101/****************************************************************
102 * Helper functions
103 ****************************************************************/
104
105// Find the first endpoing of a given type in an interface description.
106struct usb_endpoint_descriptor *
107findEndPointDesc(struct usb_interface_descriptor *iface, int imax
108 , int type, int dir)
109{
110 struct usb_endpoint_descriptor *epdesc = (void*)&iface[1];
111 for (;;) {
112 if ((void*)epdesc >= (void*)iface + imax
113 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
114 return NULL;
115 }
116 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
117 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
118 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
119 return epdesc;
120 epdesc = (void*)epdesc + epdesc->bLength;
121 }
122}
123
124// Build an encoded "endp" from an endpoint descriptor.
125u32
126mkendpFromDesc(u32 endp, struct usb_endpoint_descriptor *epdesc)
127{
128 return mkendp(endp2cntl(endp), endp2devaddr(endp)
129 , epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK
130 , endp2speed(endp), epdesc->wMaxPacketSize);
131}
132
133// Send a message to the default control pipe of a device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400134int
135send_default_control(u32 endp, const struct usb_ctrlrequest *req, void *data)
136{
137 return send_control(endp, req->bRequestType & USB_DIR_IN
138 , req, sizeof(*req), data, req->wLength);
139}
140
141// Get the first 8 bytes of the device descriptor.
142static int
143get_device_info8(struct usb_device_descriptor *dinfo, u32 endp)
144{
145 struct usb_ctrlrequest req;
146 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
147 req.bRequest = USB_REQ_GET_DESCRIPTOR;
148 req.wValue = USB_DT_DEVICE<<8;
149 req.wIndex = 0;
150 req.wLength = 8;
151 return send_default_control(endp, &req, dinfo);
152}
153
154static struct usb_config_descriptor *
155get_device_config(u32 endp)
156{
157 struct usb_config_descriptor cfg;
158
159 struct usb_ctrlrequest req;
160 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
161 req.bRequest = USB_REQ_GET_DESCRIPTOR;
162 req.wValue = USB_DT_CONFIG<<8;
163 req.wIndex = 0;
164 req.wLength = sizeof(cfg);
165 int ret = send_default_control(endp, &req, &cfg);
166 if (ret)
167 return NULL;
168
169 void *config = malloc_tmphigh(cfg.wTotalLength);
170 if (!config)
171 return NULL;
172 req.wLength = cfg.wTotalLength;
173 ret = send_default_control(endp, &req, config);
174 if (ret)
175 return NULL;
176 //hexdump(config, cfg.wTotalLength);
177 return config;
178}
179
180static u32
181set_address(u32 endp)
182{
183 dprintf(3, "set_address %x\n", endp);
184 struct usb_s *cntl = endp2cntl(endp);
185 if (cntl->maxaddr >= USB_MAXADDR)
186 return 0;
187
188 struct usb_ctrlrequest req;
189 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
190 req.bRequest = USB_REQ_SET_ADDRESS;
191 req.wValue = cntl->maxaddr + 1;
192 req.wIndex = 0;
193 req.wLength = 0;
194 int ret = send_default_control(endp, &req, NULL);
195 if (ret)
196 return 0;
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -0500197 msleep(USB_TIME_SETADDR_RECOVERY);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400198
199 cntl->maxaddr++;
200 return mkendp(cntl, cntl->maxaddr, 0, endp2speed(endp), endp2maxsize(endp));
201}
202
203static int
204set_configuration(u32 endp, u16 val)
205{
206 struct usb_ctrlrequest req;
207 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
208 req.bRequest = USB_REQ_SET_CONFIGURATION;
209 req.wValue = val;
210 req.wIndex = 0;
211 req.wLength = 0;
212 return send_default_control(endp, &req, NULL);
213}
214
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500215
216/****************************************************************
217 * Initialization and enumeration
218 ****************************************************************/
219
Kevin O'Connor114592f2009-09-28 21:32:08 -0400220// Called for every found device - see if a driver is available for
221// this device and do setup if so.
222int
223configure_usb_device(struct usb_s *cntl, int lowspeed)
224{
Kevin O'Connor78523312010-02-14 19:07:43 -0500225 dprintf(3, "config_usb: %p %d\n", cntl, lowspeed);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400226
227 // Get device info
228 u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
229 struct usb_device_descriptor dinfo;
230 int ret = get_device_info8(&dinfo, endp);
231 if (ret)
232 return 0;
233 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
234 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
235 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
236 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
237 return 0;
238 endp = mkendp(cntl, 0, 0, lowspeed, dinfo.bMaxPacketSize0);
239
240 // Get configuration
241 struct usb_config_descriptor *config = get_device_config(endp);
242 if (!config)
243 return 0;
244
245 // Determine if a driver exists for this device - only look at the
246 // first interface of the first configuration.
247 struct usb_interface_descriptor *iface = (void*)(&config[1]);
Kevin O'Connor54671c12010-02-15 18:58:12 -0500248 if ((iface->bInterfaceClass != USB_CLASS_HID
249 || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
250 || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500251 && (iface->bInterfaceClass != USB_CLASS_MASS_STORAGE)
Kevin O'Connor54671c12010-02-15 18:58:12 -0500252 && (iface->bInterfaceClass != USB_CLASS_HUB))
253 // Not a supported device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400254 goto fail;
255
256 // Set the address and configure device.
257 endp = set_address(endp);
258 if (!endp)
259 goto fail;
260 ret = set_configuration(endp, config->bConfigurationValue);
261 if (ret)
262 goto fail;
263
264 // Configure driver.
Kevin O'Connor54671c12010-02-15 18:58:12 -0500265 if (iface->bInterfaceClass == USB_CLASS_HUB) {
266 free(config);
267 return usb_hub_init(endp);
268 }
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500269 int imax = (void*)config + config->wTotalLength - (void*)iface;
270 if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE)
271 ret = usb_msc_init(endp, iface, imax);
272 else
273 ret = usb_keyboard_init(endp, iface, imax);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400274 if (ret)
275 goto fail;
276
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400277 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400278 return 1;
279fail:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400280 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400281 return 0;
282}
283
284void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500285usb_setup(void)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400286{
Kevin O'Connor59c75742010-02-13 18:49:24 -0500287 ASSERT32FLAT();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400288 if (! CONFIG_USB)
289 return;
290
291 dprintf(3, "init usb\n");
292
293 usb_keyboard_setup();
294
295 // Look for USB controllers
296 int count = 0;
297 int bdf, max;
298 foreachpci(bdf, max) {
299 u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
300
301 if (code >> 8 != PCI_CLASS_SERIAL_USB)
302 continue;
303
304 struct usb_s *cntl = &USBControllers[count];
305 cntl->bdf = bdf;
306
Kevin O'Connor114592f2009-09-28 21:32:08 -0400307 if (code == PCI_CLASS_SERIAL_USB_UHCI)
Kevin O'Connora5826b52009-10-24 17:57:29 -0400308 run_thread(uhci_init, cntl);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400309 else if (code == PCI_CLASS_SERIAL_USB_OHCI)
Kevin O'Connora5826b52009-10-24 17:57:29 -0400310 run_thread(ohci_init, cntl);
311 else
312 continue;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400313
Kevin O'Connora5826b52009-10-24 17:57:29 -0400314 count++;
315 if (count >= ARRAY_SIZE(USBControllers))
316 break;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400317 }
318}