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