These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / seabios / src / hw / usb.c
1 // Main code for handling USB controllers and devices.
2 //
3 // Copyright (C) 2009-2013  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "biosvar.h" // GET_GLOBAL
8 #include "config.h" // CONFIG_*
9 #include "malloc.h" // free
10 #include "output.h" // dprintf
11 #include "string.h" // memset
12 #include "usb.h" // struct usb_s
13 #include "usb-ehci.h" // ehci_setup
14 #include "usb-xhci.h" // xhci_setup
15 #include "usb-hid.h" // usb_keyboard_setup
16 #include "usb-hub.h" // usb_hub_setup
17 #include "usb-msc.h" // usb_msc_setup
18 #include "usb-ohci.h" // ohci_setup
19 #include "usb-uas.h" // usb_uas_setup
20 #include "usb-uhci.h" // uhci_setup
21 #include "util.h" // msleep
22 #include "x86.h" // __fls
23
24
25 /****************************************************************
26  * Controller function wrappers
27  ****************************************************************/
28
29 // Allocate, update, or free a usb pipe.
30 static struct usb_pipe *
31 usb_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *pipe
32                  , struct usb_endpoint_descriptor *epdesc)
33 {
34     switch (usbdev->hub->cntl->type) {
35     default:
36     case USB_TYPE_UHCI:
37         return uhci_realloc_pipe(usbdev, pipe, epdesc);
38     case USB_TYPE_OHCI:
39         return ohci_realloc_pipe(usbdev, pipe, epdesc);
40     case USB_TYPE_EHCI:
41         return ehci_realloc_pipe(usbdev, pipe, epdesc);
42     case USB_TYPE_XHCI:
43         return xhci_realloc_pipe(usbdev, pipe, epdesc);
44     }
45 }
46
47 // Send a message on a control pipe using the default control descriptor.
48 static int
49 usb_send_pipe(struct usb_pipe *pipe_fl, int dir, const void *cmd
50               , void *data, int datasize)
51 {
52     switch (GET_LOWFLAT(pipe_fl->type)) {
53     default:
54     case USB_TYPE_UHCI:
55         return uhci_send_pipe(pipe_fl, dir, cmd, data, datasize);
56     case USB_TYPE_OHCI:
57         if (MODESEGMENT)
58             return -1;
59         return ohci_send_pipe(pipe_fl, dir, cmd, data, datasize);
60     case USB_TYPE_EHCI:
61         return ehci_send_pipe(pipe_fl, dir, cmd, data, datasize);
62     case USB_TYPE_XHCI:
63         if (MODESEGMENT)
64             return -1;
65         return xhci_send_pipe(pipe_fl, dir, cmd, data, datasize);
66     }
67 }
68
69 int
70 usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
71 {
72     ASSERT16();
73     switch (GET_LOWFLAT(pipe_fl->type)) {
74     default:
75     case USB_TYPE_UHCI:
76         return uhci_poll_intr(pipe_fl, data);
77     case USB_TYPE_OHCI:
78         return ohci_poll_intr(pipe_fl, data);
79     case USB_TYPE_EHCI:
80         return ehci_poll_intr(pipe_fl, data);
81     case USB_TYPE_XHCI: ;
82         return call32_params(xhci_poll_intr, pipe_fl
83                              , MAKE_FLATPTR(GET_SEG(SS), data), 0, -1);
84     }
85 }
86
87 int usb_32bit_pipe(struct usb_pipe *pipe_fl)
88 {
89     return (CONFIG_USB_XHCI && GET_LOWFLAT(pipe_fl->type) == USB_TYPE_XHCI)
90         || (CONFIG_USB_OHCI && GET_LOWFLAT(pipe_fl->type) == USB_TYPE_OHCI);
91 }
92
93
94 /****************************************************************
95  * Helper functions
96  ****************************************************************/
97
98 // Allocate a usb pipe.
99 struct usb_pipe *
100 usb_alloc_pipe(struct usbdevice_s *usbdev
101                , struct usb_endpoint_descriptor *epdesc)
102 {
103     return usb_realloc_pipe(usbdev, NULL, epdesc);
104 }
105
106 // Free an allocated control or bulk pipe.
107 void
108 usb_free_pipe(struct usbdevice_s *usbdev, struct usb_pipe *pipe)
109 {
110     if (!pipe)
111         return;
112     usb_realloc_pipe(usbdev, pipe, NULL);
113 }
114
115 // Send a message to the default control pipe of a device.
116 int
117 usb_send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
118                          , void *data)
119 {
120     return usb_send_pipe(pipe, req->bRequestType & USB_DIR_IN, req
121                          , data, req->wLength);
122 }
123
124 // Send a message to a bulk endpoint
125 int
126 usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
127 {
128     return usb_send_pipe(pipe_fl, dir, NULL, data, datasize);
129 }
130
131 // Check if a pipe for a given controller is on the freelist
132 int
133 usb_is_freelist(struct usb_s *cntl, struct usb_pipe *pipe)
134 {
135     return pipe->cntl != cntl;
136 }
137
138 // Add a pipe to the controller's freelist
139 void
140 usb_add_freelist(struct usb_pipe *pipe)
141 {
142     if (!pipe)
143         return;
144     struct usb_s *cntl = pipe->cntl;
145     pipe->freenext = cntl->freelist;
146     cntl->freelist = pipe;
147 }
148
149 // Check for an available pipe on the freelist.
150 struct usb_pipe *
151 usb_get_freelist(struct usb_s *cntl, u8 eptype)
152 {
153     struct usb_pipe **pfree = &cntl->freelist;
154     for (;;) {
155         struct usb_pipe *pipe = *pfree;
156         if (!pipe)
157             return NULL;
158         if (pipe->eptype == eptype) {
159             *pfree = pipe->freenext;
160             return pipe;
161         }
162         pfree = &pipe->freenext;
163     }
164 }
165
166 // Fill "pipe" endpoint info from an endpoint descriptor.
167 void
168 usb_desc2pipe(struct usb_pipe *pipe, struct usbdevice_s *usbdev
169               , struct usb_endpoint_descriptor *epdesc)
170 {
171     pipe->cntl = usbdev->hub->cntl;
172     pipe->type = usbdev->hub->cntl->type;
173     pipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
174     pipe->devaddr = usbdev->devaddr;
175     pipe->speed = usbdev->speed;
176     pipe->maxpacket = epdesc->wMaxPacketSize;
177     pipe->eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
178 }
179
180 // Find the exponential period of the requested interrupt end point.
181 int
182 usb_get_period(struct usbdevice_s *usbdev
183                , struct usb_endpoint_descriptor *epdesc)
184 {
185     int period = epdesc->bInterval;
186     if (usbdev->speed != USB_HIGHSPEED)
187         return (period <= 0) ? 0 : __fls(period);
188     return (period <= 4) ? 0 : period - 4;
189 }
190
191 // Maximum time (in ms) a data transfer should take
192 int
193 usb_xfer_time(struct usb_pipe *pipe, int datalen)
194 {
195     // Use the maximum command time (5 seconds), except for
196     // set_address commands where we don't want to stall the boot if
197     // the device doesn't actually exist.  Add 100ms to account for
198     // any controller delays.
199     if (!GET_LOWFLAT(pipe->devaddr))
200         return USB_TIME_STATUS + 100;
201     return USB_TIME_COMMAND + 100;
202 }
203
204 // Find the first endpoint of a given type in an interface description.
205 struct usb_endpoint_descriptor *
206 usb_find_desc(struct usbdevice_s *usbdev, int type, int dir)
207 {
208     struct usb_endpoint_descriptor *epdesc = (void*)&usbdev->iface[1];
209     for (;;) {
210         if ((void*)epdesc >= (void*)usbdev->iface + usbdev->imax
211             || epdesc->bDescriptorType == USB_DT_INTERFACE) {
212             return NULL;
213         }
214         if (epdesc->bDescriptorType == USB_DT_ENDPOINT
215             && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
216             && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
217             return epdesc;
218         epdesc = (void*)epdesc + epdesc->bLength;
219     }
220 }
221
222 // Get the first 8 bytes of the device descriptor.
223 static int
224 get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
225 {
226     struct usb_ctrlrequest req;
227     req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
228     req.bRequest = USB_REQ_GET_DESCRIPTOR;
229     req.wValue = USB_DT_DEVICE<<8;
230     req.wIndex = 0;
231     req.wLength = 8;
232     return usb_send_default_control(pipe, &req, dinfo);
233 }
234
235 static struct usb_config_descriptor *
236 get_device_config(struct usb_pipe *pipe)
237 {
238     struct usb_config_descriptor cfg;
239
240     struct usb_ctrlrequest req;
241     req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
242     req.bRequest = USB_REQ_GET_DESCRIPTOR;
243     req.wValue = USB_DT_CONFIG<<8;
244     req.wIndex = 0;
245     req.wLength = sizeof(cfg);
246     int ret = usb_send_default_control(pipe, &req, &cfg);
247     if (ret)
248         return NULL;
249
250     void *config = malloc_tmphigh(cfg.wTotalLength);
251     if (!config) {
252         warn_noalloc();
253         return NULL;
254     }
255     req.wLength = cfg.wTotalLength;
256     ret = usb_send_default_control(pipe, &req, config);
257     if (ret) {
258         free(config);
259         return NULL;
260     }
261     //hexdump(config, cfg.wTotalLength);
262     return config;
263 }
264
265 static int
266 set_configuration(struct usb_pipe *pipe, u16 val)
267 {
268     struct usb_ctrlrequest req;
269     req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
270     req.bRequest = USB_REQ_SET_CONFIGURATION;
271     req.wValue = val;
272     req.wIndex = 0;
273     req.wLength = 0;
274     return usb_send_default_control(pipe, &req, NULL);
275 }
276
277
278 /****************************************************************
279  * Initialization and enumeration
280  ****************************************************************/
281
282 static const int speed_to_ctlsize[] = {
283     [ USB_FULLSPEED  ] = 8,
284     [ USB_LOWSPEED   ] = 8,
285     [ USB_HIGHSPEED  ] = 64,
286     [ USB_SUPERSPEED ] = 512,
287 };
288
289 // Assign an address to a device in the default state on the given
290 // controller.
291 static int
292 usb_set_address(struct usbdevice_s *usbdev)
293 {
294     ASSERT32FLAT();
295     struct usb_s *cntl = usbdev->hub->cntl;
296     dprintf(3, "set_address %p\n", cntl);
297     if (cntl->maxaddr >= USB_MAXADDR)
298         return -1;
299
300     msleep(USB_TIME_RSTRCY);
301
302     // Create a pipe for the default address.
303     struct usb_endpoint_descriptor epdesc = {
304         .wMaxPacketSize = speed_to_ctlsize[usbdev->speed],
305         .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
306     };
307     usbdev->defpipe = usb_alloc_pipe(usbdev, &epdesc);
308     if (!usbdev->defpipe)
309         return -1;
310
311     // Send set_address command.
312     struct usb_ctrlrequest req;
313     req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
314     req.bRequest = USB_REQ_SET_ADDRESS;
315     req.wValue = cntl->maxaddr + 1;
316     req.wIndex = 0;
317     req.wLength = 0;
318     int ret = usb_send_default_control(usbdev->defpipe, &req, NULL);
319     if (ret) {
320         usb_free_pipe(usbdev, usbdev->defpipe);
321         return -1;
322     }
323
324     msleep(USB_TIME_SETADDR_RECOVERY);
325
326     cntl->maxaddr++;
327     usbdev->devaddr = cntl->maxaddr;
328     usbdev->defpipe = usb_realloc_pipe(usbdev, usbdev->defpipe, &epdesc);
329     if (!usbdev->defpipe)
330         return -1;
331     return 0;
332 }
333
334 // Called for every found device - see if a driver is available for
335 // this device and do setup if so.
336 static int
337 configure_usb_device(struct usbdevice_s *usbdev)
338 {
339     ASSERT32FLAT();
340     dprintf(3, "config_usb: %p\n", usbdev->defpipe);
341
342     // Set the max packet size for endpoint 0 of this device.
343     struct usb_device_descriptor dinfo;
344     int ret = get_device_info8(usbdev->defpipe, &dinfo);
345     if (ret)
346         return 0;
347     u16 maxpacket = dinfo.bMaxPacketSize0;
348     if (dinfo.bcdUSB >= 0x0300)
349         maxpacket = 1 << dinfo.bMaxPacketSize0;
350     dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%d\n"
351             , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
352             , dinfo.bDeviceProtocol, maxpacket);
353     if (maxpacket < 8)
354         return 0;
355     struct usb_endpoint_descriptor epdesc = {
356         .wMaxPacketSize = maxpacket,
357         .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
358     };
359     usbdev->defpipe = usb_realloc_pipe(usbdev, usbdev->defpipe, &epdesc);
360     if (!usbdev->defpipe)
361         return -1;
362
363     // Get configuration
364     struct usb_config_descriptor *config = get_device_config(usbdev->defpipe);
365     if (!config)
366         return 0;
367
368     // Determine if a driver exists for this device - only look at the
369     // first interface of the first configuration.
370     struct usb_interface_descriptor *iface = (void*)(&config[1]);
371     if (iface->bInterfaceClass != USB_CLASS_HID
372         && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
373         && iface->bInterfaceClass != USB_CLASS_HUB)
374         // Not a supported device.
375         goto fail;
376
377     // Set the configuration.
378     ret = set_configuration(usbdev->defpipe, config->bConfigurationValue);
379     if (ret)
380         goto fail;
381
382     // Configure driver.
383     usbdev->config = config;
384     usbdev->iface = iface;
385     usbdev->imax = (void*)config + config->wTotalLength - (void*)iface;
386     if (iface->bInterfaceClass == USB_CLASS_HUB)
387         ret = usb_hub_setup(usbdev);
388     else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE) {
389         if (iface->bInterfaceProtocol == US_PR_BULK)
390             ret = usb_msc_setup(usbdev);
391         if (iface->bInterfaceProtocol == US_PR_UAS)
392             ret = usb_uas_setup(usbdev);
393     } else
394         ret = usb_hid_setup(usbdev);
395     if (ret)
396         goto fail;
397
398     free(config);
399     return 1;
400 fail:
401     free(config);
402     return 0;
403 }
404
405 static void
406 usb_hub_port_setup(void *data)
407 {
408     struct usbdevice_s *usbdev = data;
409     struct usbhub_s *hub = usbdev->hub;
410     u32 port = usbdev->port;
411
412     for (;;) {
413         // Detect if device present (and possibly start reset)
414         int ret = hub->op->detect(hub, port);
415         if (ret > 0)
416             // Device connected.
417             break;
418         if (ret < 0 || timer_check(hub->detectend))
419             // No device found.
420             goto done;
421         msleep(5);
422     }
423
424     // XXX - wait USB_TIME_ATTDB time?
425
426     // Reset port and determine device speed
427     mutex_lock(&hub->cntl->resetlock);
428     int ret = hub->op->reset(hub, port);
429     if (ret < 0)
430         // Reset failed
431         goto resetfail;
432     usbdev->speed = ret;
433
434     // Set address of port
435     ret = usb_set_address(usbdev);
436     if (ret) {
437         hub->op->disconnect(hub, port);
438         goto resetfail;
439     }
440     mutex_unlock(&hub->cntl->resetlock);
441
442     // Configure the device
443     int count = configure_usb_device(usbdev);
444     usb_free_pipe(usbdev, usbdev->defpipe);
445     if (!count)
446         hub->op->disconnect(hub, port);
447     hub->devcount += count;
448 done:
449     hub->threads--;
450     free(usbdev);
451     return;
452
453 resetfail:
454     mutex_unlock(&hub->cntl->resetlock);
455     goto done;
456 }
457
458 void
459 usb_enumerate(struct usbhub_s *hub)
460 {
461     u32 portcount = hub->portcount;
462     hub->threads = portcount;
463     hub->detectend = timer_calc(USB_TIME_SIGATT);
464
465     // Launch a thread for every port.
466     int i;
467     for (i=0; i<portcount; i++) {
468         struct usbdevice_s *usbdev = malloc_tmphigh(sizeof(*usbdev));
469         if (!usbdev) {
470             warn_noalloc();
471             continue;
472         }
473         memset(usbdev, 0, sizeof(*usbdev));
474         usbdev->hub = hub;
475         usbdev->port = i;
476         run_thread(usb_hub_port_setup, usbdev);
477     }
478
479     // Wait for threads to complete.
480     while (hub->threads)
481         yield();
482 }
483
484 void
485 __usb_setup(void *data)
486 {
487     dprintf(3, "init usb\n");
488     xhci_setup();
489     ehci_setup();
490     uhci_setup();
491     ohci_setup();
492 }
493
494 void
495 usb_setup(void)
496 {
497     ASSERT32FLAT();
498     if (! CONFIG_USB)
499         return;
500     run_thread(__usb_setup, NULL);
501 }