Initial support for booting from USB drives.
[seabios.git] / src / usb.c
1 // 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
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
19
20 struct usb_s USBControllers[16] VAR16VISIBLE;
21
22
23 /****************************************************************
24  * Controller function wrappers
25  ****************************************************************/
26
27 // Send a message on a control pipe using the default control descriptor.
28 static int
29 send_control(u32 endp, int dir, const void *cmd, int cmdsize
30              , void *data, int datasize)
31 {
32     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     }
40 }
41
42 struct usb_pipe *
43 alloc_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
55 int
56 usb_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
69 struct usb_pipe *
70 alloc_intr_pipe(u32 endp, int period)
71 {
72     struct usb_s *cntl = endp2cntl(endp);
73     // Find the exponential period of the requested time.
74     if (period <= 0)
75         period = 1;
76     int frameexp = __fls(period);
77     switch (cntl->type) {
78     default:
79     case USB_TYPE_UHCI:
80         return uhci_alloc_intr_pipe(endp, frameexp);
81     case USB_TYPE_OHCI:
82         return ohci_alloc_intr_pipe(endp, frameexp);
83     }
84 }
85
86 int noinline
87 usb_poll_intr(struct usb_pipe *pipe, void *data)
88 {
89     u32 endp = GET_FLATPTR(pipe->endp);
90     struct usb_s *cntl = endp2cntl(endp);
91     switch (GET_GLOBAL(cntl->type)) {
92     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     }
98 }
99
100
101 /****************************************************************
102  * Helper functions
103  ****************************************************************/
104
105 // Find the first endpoing of a given type in an interface description.
106 struct usb_endpoint_descriptor *
107 findEndPointDesc(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.
125 u32
126 mkendpFromDesc(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.
134 int
135 send_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.
142 static int
143 get_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
154 static struct usb_config_descriptor *
155 get_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
180 static u32
181 set_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;
197     msleep(USB_TIME_SETADDR_RECOVERY);
198
199     cntl->maxaddr++;
200     return mkendp(cntl, cntl->maxaddr, 0, endp2speed(endp), endp2maxsize(endp));
201 }
202
203 static int
204 set_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
215
216 /****************************************************************
217  * Initialization and enumeration
218  ****************************************************************/
219
220 // Called for every found device - see if a driver is available for
221 // this device and do setup if so.
222 int
223 configure_usb_device(struct usb_s *cntl, int lowspeed)
224 {
225     dprintf(3, "config_usb: %p %d\n", cntl, lowspeed);
226
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]);
248     if ((iface->bInterfaceClass != USB_CLASS_HID
249          || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
250          || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
251         && (iface->bInterfaceClass != USB_CLASS_MASS_STORAGE)
252         && (iface->bInterfaceClass != USB_CLASS_HUB))
253         // Not a supported device.
254         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.
265     if (iface->bInterfaceClass == USB_CLASS_HUB) {
266         free(config);
267         return usb_hub_init(endp);
268     }
269     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);
274     if (ret)
275         goto fail;
276
277     free(config);
278     return 1;
279 fail:
280     free(config);
281     return 0;
282 }
283
284 void
285 usb_setup(void)
286 {
287     ASSERT32FLAT();
288     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
307         if (code == PCI_CLASS_SERIAL_USB_UHCI)
308             run_thread(uhci_init, cntl);
309         else if (code == PCI_CLASS_SERIAL_USB_OHCI)
310             run_thread(ohci_init, cntl);
311         else
312             continue;
313
314         count++;
315         if (count >= ARRAY_SIZE(USBControllers))
316             break;
317     }
318 }