xref: /qemu/hw/usb/core.c (revision 079d0b7f1eedcc634c371fe05b617fdc55c8b762)
1 /*
2  * QEMU USB emulation
3  *
4  * Copyright (c) 2005 Fabrice Bellard
5  *
6  * 2008 Generic packet handler rewrite by Max Krasnyansky
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 #include "qemu-common.h"
27 #include "usb.h"
28 #include "iov.h"
29 
30 void usb_attach(USBPort *port)
31 {
32     USBDevice *dev = port->dev;
33 
34     assert(dev != NULL);
35     assert(dev->attached);
36     assert(dev->state == USB_STATE_NOTATTACHED);
37     port->ops->attach(port);
38     dev->state = USB_STATE_ATTACHED;
39     usb_device_handle_attach(dev);
40 }
41 
42 void usb_detach(USBPort *port)
43 {
44     USBDevice *dev = port->dev;
45 
46     assert(dev != NULL);
47     assert(dev->state != USB_STATE_NOTATTACHED);
48     port->ops->detach(port);
49     dev->state = USB_STATE_NOTATTACHED;
50 }
51 
52 void usb_port_reset(USBPort *port)
53 {
54     USBDevice *dev = port->dev;
55 
56     assert(dev != NULL);
57     usb_detach(port);
58     usb_attach(port);
59     usb_device_reset(dev);
60 }
61 
62 void usb_device_reset(USBDevice *dev)
63 {
64     if (dev == NULL || !dev->attached) {
65         return;
66     }
67     dev->remote_wakeup = 0;
68     dev->addr = 0;
69     dev->state = USB_STATE_DEFAULT;
70     usb_device_handle_reset(dev);
71 }
72 
73 void usb_wakeup(USBDevice *dev)
74 {
75     if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
76         dev->port->ops->wakeup(dev->port);
77     }
78 }
79 
80 /**********************/
81 
82 /* generic USB device helpers (you are not forced to use them when
83    writing your USB device driver, but they help handling the
84    protocol)
85 */
86 
87 #define SETUP_STATE_IDLE  0
88 #define SETUP_STATE_SETUP 1
89 #define SETUP_STATE_DATA  2
90 #define SETUP_STATE_ACK   3
91 
92 static int do_token_setup(USBDevice *s, USBPacket *p)
93 {
94     int request, value, index;
95     int ret = 0;
96 
97     if (p->iov.size != 8) {
98         return USB_RET_STALL;
99     }
100 
101     usb_packet_copy(p, s->setup_buf, p->iov.size);
102     s->setup_len   = (s->setup_buf[7] << 8) | s->setup_buf[6];
103     s->setup_index = 0;
104 
105     request = (s->setup_buf[0] << 8) | s->setup_buf[1];
106     value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
107     index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
108 
109     if (s->setup_buf[0] & USB_DIR_IN) {
110         ret = usb_device_handle_control(s, p, request, value, index,
111                                         s->setup_len, s->data_buf);
112         if (ret == USB_RET_ASYNC) {
113              s->setup_state = SETUP_STATE_SETUP;
114              return USB_RET_ASYNC;
115         }
116         if (ret < 0)
117             return ret;
118 
119         if (ret < s->setup_len)
120             s->setup_len = ret;
121         s->setup_state = SETUP_STATE_DATA;
122     } else {
123         if (s->setup_len > sizeof(s->data_buf)) {
124             fprintf(stderr,
125                 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
126                 s->setup_len, sizeof(s->data_buf));
127             return USB_RET_STALL;
128         }
129         if (s->setup_len == 0)
130             s->setup_state = SETUP_STATE_ACK;
131         else
132             s->setup_state = SETUP_STATE_DATA;
133     }
134 
135     return ret;
136 }
137 
138 static int do_token_in(USBDevice *s, USBPacket *p)
139 {
140     int request, value, index;
141     int ret = 0;
142 
143     assert(p->ep->nr == 0);
144 
145     request = (s->setup_buf[0] << 8) | s->setup_buf[1];
146     value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
147     index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
148 
149     switch(s->setup_state) {
150     case SETUP_STATE_ACK:
151         if (!(s->setup_buf[0] & USB_DIR_IN)) {
152             ret = usb_device_handle_control(s, p, request, value, index,
153                                             s->setup_len, s->data_buf);
154             if (ret == USB_RET_ASYNC) {
155                 return USB_RET_ASYNC;
156             }
157             s->setup_state = SETUP_STATE_IDLE;
158             if (ret > 0)
159                 return 0;
160             return ret;
161         }
162 
163         /* return 0 byte */
164         return 0;
165 
166     case SETUP_STATE_DATA:
167         if (s->setup_buf[0] & USB_DIR_IN) {
168             int len = s->setup_len - s->setup_index;
169             if (len > p->iov.size) {
170                 len = p->iov.size;
171             }
172             usb_packet_copy(p, s->data_buf + s->setup_index, len);
173             s->setup_index += len;
174             if (s->setup_index >= s->setup_len)
175                 s->setup_state = SETUP_STATE_ACK;
176             return len;
177         }
178 
179         s->setup_state = SETUP_STATE_IDLE;
180         return USB_RET_STALL;
181 
182     default:
183         return USB_RET_STALL;
184     }
185 }
186 
187 static int do_token_out(USBDevice *s, USBPacket *p)
188 {
189     assert(p->ep->nr == 0);
190 
191     switch(s->setup_state) {
192     case SETUP_STATE_ACK:
193         if (s->setup_buf[0] & USB_DIR_IN) {
194             s->setup_state = SETUP_STATE_IDLE;
195             /* transfer OK */
196         } else {
197             /* ignore additional output */
198         }
199         return 0;
200 
201     case SETUP_STATE_DATA:
202         if (!(s->setup_buf[0] & USB_DIR_IN)) {
203             int len = s->setup_len - s->setup_index;
204             if (len > p->iov.size) {
205                 len = p->iov.size;
206             }
207             usb_packet_copy(p, s->data_buf + s->setup_index, len);
208             s->setup_index += len;
209             if (s->setup_index >= s->setup_len)
210                 s->setup_state = SETUP_STATE_ACK;
211             return len;
212         }
213 
214         s->setup_state = SETUP_STATE_IDLE;
215         return USB_RET_STALL;
216 
217     default:
218         return USB_RET_STALL;
219     }
220 }
221 
222 /* ctrl complete function for devices which use usb_generic_handle_packet and
223    may return USB_RET_ASYNC from their handle_control callback. Device code
224    which does this *must* call this function instead of the normal
225    usb_packet_complete to complete their async control packets. */
226 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
227 {
228     if (p->result < 0) {
229         s->setup_state = SETUP_STATE_IDLE;
230     }
231 
232     switch (s->setup_state) {
233     case SETUP_STATE_SETUP:
234         if (p->result < s->setup_len) {
235             s->setup_len = p->result;
236         }
237         s->setup_state = SETUP_STATE_DATA;
238         p->result = 8;
239         break;
240 
241     case SETUP_STATE_ACK:
242         s->setup_state = SETUP_STATE_IDLE;
243         p->result = 0;
244         break;
245 
246     default:
247         break;
248     }
249     usb_packet_complete(s, p);
250 }
251 
252 /* XXX: fix overflow */
253 int set_usb_string(uint8_t *buf, const char *str)
254 {
255     int len, i;
256     uint8_t *q;
257 
258     q = buf;
259     len = strlen(str);
260     *q++ = 2 * len + 2;
261     *q++ = 3;
262     for(i = 0; i < len; i++) {
263         *q++ = str[i];
264         *q++ = 0;
265     }
266     return q - buf;
267 }
268 
269 USBDevice *usb_find_device(USBPort *port, uint8_t addr)
270 {
271     USBDevice *dev = port->dev;
272 
273     if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
274         return NULL;
275     }
276     if (dev->addr == addr) {
277         return dev;
278     }
279     return usb_device_find_device(dev, addr);
280 }
281 
282 /* Hand over a packet to a device for processing.  Return value
283    USB_RET_ASYNC indicates the processing isn't finished yet, the
284    driver will call usb_packet_complete() when done processing it. */
285 int usb_handle_packet(USBDevice *dev, USBPacket *p)
286 {
287     int ret;
288 
289     if (dev == NULL) {
290         return USB_RET_NODEV;
291     }
292     assert(dev == p->ep->dev);
293     assert(dev->state == USB_STATE_DEFAULT);
294     assert(p->state == USB_PACKET_SETUP);
295 
296     if (p->ep->nr == 0) {
297         /* control pipe */
298         switch (p->pid) {
299         case USB_TOKEN_SETUP:
300             ret = do_token_setup(dev, p);
301             break;
302         case USB_TOKEN_IN:
303             ret = do_token_in(dev, p);
304             break;
305         case USB_TOKEN_OUT:
306             ret = do_token_out(dev, p);
307             break;
308         default:
309             ret = USB_RET_STALL;
310             break;
311         }
312     } else {
313         /* data pipe */
314         ret = usb_device_handle_data(dev, p);
315     }
316 
317     if (ret == USB_RET_ASYNC) {
318         p->state = USB_PACKET_ASYNC;
319     }
320     return ret;
321 }
322 
323 /* Notify the controller that an async packet is complete.  This should only
324    be called for packets previously deferred by returning USB_RET_ASYNC from
325    handle_packet. */
326 void usb_packet_complete(USBDevice *dev, USBPacket *p)
327 {
328     assert(p->state == USB_PACKET_ASYNC);
329     p->state = USB_PACKET_COMPLETE;
330     dev->port->ops->complete(dev->port, p);
331 }
332 
333 /* Cancel an active packet.  The packed must have been deferred by
334    returning USB_RET_ASYNC from handle_packet, and not yet
335    completed.  */
336 void usb_cancel_packet(USBPacket * p)
337 {
338     assert(p->state == USB_PACKET_ASYNC);
339     p->state = USB_PACKET_CANCELED;
340     usb_device_cancel_packet(p->ep->dev, p);
341 }
342 
343 
344 void usb_packet_init(USBPacket *p)
345 {
346     qemu_iovec_init(&p->iov, 1);
347 }
348 
349 void usb_packet_setup(USBPacket *p, int pid, USBEndpoint *ep)
350 {
351     assert(!usb_packet_is_inflight(p));
352     p->state = USB_PACKET_SETUP;
353     p->pid = pid;
354     p->ep = ep;
355     p->result = 0;
356     qemu_iovec_reset(&p->iov);
357 }
358 
359 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
360 {
361     qemu_iovec_add(&p->iov, ptr, len);
362 }
363 
364 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
365 {
366     assert(p->result >= 0);
367     assert(p->result + bytes <= p->iov.size);
368     switch (p->pid) {
369     case USB_TOKEN_SETUP:
370     case USB_TOKEN_OUT:
371         iov_to_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
372         break;
373     case USB_TOKEN_IN:
374         iov_from_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
375         break;
376     default:
377         fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
378         abort();
379     }
380     p->result += bytes;
381 }
382 
383 void usb_packet_skip(USBPacket *p, size_t bytes)
384 {
385     assert(p->result >= 0);
386     assert(p->result + bytes <= p->iov.size);
387     if (p->pid == USB_TOKEN_IN) {
388         iov_clear(p->iov.iov, p->iov.niov, p->result, bytes);
389     }
390     p->result += bytes;
391 }
392 
393 void usb_packet_cleanup(USBPacket *p)
394 {
395     assert(!usb_packet_is_inflight(p));
396     qemu_iovec_destroy(&p->iov);
397 }
398 
399 void usb_ep_init(USBDevice *dev)
400 {
401     int ep;
402 
403     dev->ep_ctl.nr = 0;
404     dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
405     dev->ep_ctl.ifnum = 0;
406     dev->ep_ctl.dev = dev;
407     for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
408         dev->ep_in[ep].nr = ep + 1;
409         dev->ep_out[ep].nr = ep + 1;
410         dev->ep_in[ep].pid = USB_TOKEN_IN;
411         dev->ep_out[ep].pid = USB_TOKEN_OUT;
412         dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
413         dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
414         dev->ep_in[ep].ifnum = 0;
415         dev->ep_out[ep].ifnum = 0;
416         dev->ep_in[ep].dev = dev;
417         dev->ep_out[ep].dev = dev;
418     }
419 }
420 
421 void usb_ep_dump(USBDevice *dev)
422 {
423     static const char *tname[] = {
424         [USB_ENDPOINT_XFER_CONTROL] = "control",
425         [USB_ENDPOINT_XFER_ISOC]    = "isoc",
426         [USB_ENDPOINT_XFER_BULK]    = "bulk",
427         [USB_ENDPOINT_XFER_INT]     = "int",
428     };
429     int ifnum, ep, first;
430 
431     fprintf(stderr, "Device \"%s\", config %d\n",
432             dev->product_desc, dev->configuration);
433     for (ifnum = 0; ifnum < 16; ifnum++) {
434         first = 1;
435         for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
436             if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
437                 dev->ep_in[ep].ifnum == ifnum) {
438                 if (first) {
439                     first = 0;
440                     fprintf(stderr, "  Interface %d, alternative %d\n",
441                             ifnum, dev->altsetting[ifnum]);
442                 }
443                 fprintf(stderr, "    Endpoint %d, IN, %s, %d max\n", ep,
444                         tname[dev->ep_in[ep].type],
445                         dev->ep_in[ep].max_packet_size);
446             }
447             if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
448                 dev->ep_out[ep].ifnum == ifnum) {
449                 if (first) {
450                     first = 0;
451                     fprintf(stderr, "  Interface %d, alternative %d\n",
452                             ifnum, dev->altsetting[ifnum]);
453                 }
454                 fprintf(stderr, "    Endpoint %d, OUT, %s, %d max\n", ep,
455                         tname[dev->ep_out[ep].type],
456                         dev->ep_out[ep].max_packet_size);
457             }
458         }
459     }
460     fprintf(stderr, "--\n");
461 }
462 
463 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
464 {
465     struct USBEndpoint *eps;
466 
467     if (dev == NULL) {
468         return NULL;
469     }
470     eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
471     if (ep == 0) {
472         return &dev->ep_ctl;
473     }
474     assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
475     assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
476     return eps + ep - 1;
477 }
478 
479 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
480 {
481     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
482     return uep->type;
483 }
484 
485 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
486 {
487     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
488     uep->type = type;
489 }
490 
491 uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep)
492 {
493     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
494     return uep->ifnum;
495 }
496 
497 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
498 {
499     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
500     uep->ifnum = ifnum;
501 }
502 
503 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
504                                 uint16_t raw)
505 {
506     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
507     int size, microframes;
508 
509     size = raw & 0x7ff;
510     switch ((raw >> 11) & 3) {
511     case 1:
512         microframes = 2;
513         break;
514     case 2:
515         microframes = 3;
516         break;
517     default:
518         microframes = 1;
519         break;
520     }
521     uep->max_packet_size = size * microframes;
522 }
523 
524 int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep)
525 {
526     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
527     return uep->max_packet_size;
528 }
529