]> Git Repo - qemu.git/blob - hw/usb.c
Merge branch 'target-arm.for-upstream' of git://git.linaro.org/people/pmaydell/qemu-arm
[qemu.git] / hw / usb.c
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     usb_send_msg(dev, USB_MSG_ATTACH);
39 }
40
41 void usb_detach(USBPort *port)
42 {
43     USBDevice *dev = port->dev;
44
45     assert(dev != NULL);
46     assert(dev->state != USB_STATE_NOTATTACHED);
47     port->ops->detach(port);
48     usb_send_msg(dev, USB_MSG_DETACH);
49 }
50
51 void usb_reset(USBPort *port)
52 {
53     USBDevice *dev = port->dev;
54
55     assert(dev != NULL);
56     usb_detach(port);
57     usb_attach(port);
58     usb_send_msg(dev, USB_MSG_RESET);
59 }
60
61 void usb_wakeup(USBDevice *dev)
62 {
63     if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
64         dev->port->ops->wakeup(dev->port);
65     }
66 }
67
68 /**********************/
69
70 /* generic USB device helpers (you are not forced to use them when
71    writing your USB device driver, but they help handling the
72    protocol)
73 */
74
75 #define SETUP_STATE_IDLE  0
76 #define SETUP_STATE_SETUP 1
77 #define SETUP_STATE_DATA  2
78 #define SETUP_STATE_ACK   3
79
80 static int do_token_setup(USBDevice *s, USBPacket *p)
81 {
82     int request, value, index;
83     int ret = 0;
84
85     if (p->iov.size != 8) {
86         return USB_RET_STALL;
87     }
88
89     usb_packet_copy(p, s->setup_buf, p->iov.size);
90     s->setup_len   = (s->setup_buf[7] << 8) | s->setup_buf[6];
91     s->setup_index = 0;
92
93     request = (s->setup_buf[0] << 8) | s->setup_buf[1];
94     value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
95     index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
96
97     if (s->setup_buf[0] & USB_DIR_IN) {
98         ret = usb_device_handle_control(s, p, request, value, index,
99                                         s->setup_len, s->data_buf);
100         if (ret == USB_RET_ASYNC) {
101              s->setup_state = SETUP_STATE_SETUP;
102              return USB_RET_ASYNC;
103         }
104         if (ret < 0)
105             return ret;
106
107         if (ret < s->setup_len)
108             s->setup_len = ret;
109         s->setup_state = SETUP_STATE_DATA;
110     } else {
111         if (s->setup_len > sizeof(s->data_buf)) {
112             fprintf(stderr,
113                 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
114                 s->setup_len, sizeof(s->data_buf));
115             return USB_RET_STALL;
116         }
117         if (s->setup_len == 0)
118             s->setup_state = SETUP_STATE_ACK;
119         else
120             s->setup_state = SETUP_STATE_DATA;
121     }
122
123     return ret;
124 }
125
126 static int do_token_in(USBDevice *s, USBPacket *p)
127 {
128     int request, value, index;
129     int ret = 0;
130
131     if (p->devep != 0)
132         return usb_device_handle_data(s, p);
133
134     request = (s->setup_buf[0] << 8) | s->setup_buf[1];
135     value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
136     index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
137  
138     switch(s->setup_state) {
139     case SETUP_STATE_ACK:
140         if (!(s->setup_buf[0] & USB_DIR_IN)) {
141             ret = usb_device_handle_control(s, p, request, value, index,
142                                             s->setup_len, s->data_buf);
143             if (ret == USB_RET_ASYNC) {
144                 return USB_RET_ASYNC;
145             }
146             s->setup_state = SETUP_STATE_IDLE;
147             if (ret > 0)
148                 return 0;
149             return ret;
150         }
151
152         /* return 0 byte */
153         return 0;
154
155     case SETUP_STATE_DATA:
156         if (s->setup_buf[0] & USB_DIR_IN) {
157             int len = s->setup_len - s->setup_index;
158             if (len > p->iov.size) {
159                 len = p->iov.size;
160             }
161             usb_packet_copy(p, s->data_buf + s->setup_index, len);
162             s->setup_index += len;
163             if (s->setup_index >= s->setup_len)
164                 s->setup_state = SETUP_STATE_ACK;
165             return len;
166         }
167
168         s->setup_state = SETUP_STATE_IDLE;
169         return USB_RET_STALL;
170
171     default:
172         return USB_RET_STALL;
173     }
174 }
175
176 static int do_token_out(USBDevice *s, USBPacket *p)
177 {
178     if (p->devep != 0)
179         return usb_device_handle_data(s, p);
180
181     switch(s->setup_state) {
182     case SETUP_STATE_ACK:
183         if (s->setup_buf[0] & USB_DIR_IN) {
184             s->setup_state = SETUP_STATE_IDLE;
185             /* transfer OK */
186         } else {
187             /* ignore additional output */
188         }
189         return 0;
190
191     case SETUP_STATE_DATA:
192         if (!(s->setup_buf[0] & USB_DIR_IN)) {
193             int len = s->setup_len - s->setup_index;
194             if (len > p->iov.size) {
195                 len = p->iov.size;
196             }
197             usb_packet_copy(p, s->data_buf + s->setup_index, len);
198             s->setup_index += len;
199             if (s->setup_index >= s->setup_len)
200                 s->setup_state = SETUP_STATE_ACK;
201             return len;
202         }
203
204         s->setup_state = SETUP_STATE_IDLE;
205         return USB_RET_STALL;
206
207     default:
208         return USB_RET_STALL;
209     }
210 }
211
212 /*
213  * Generic packet handler.
214  * Called by the HC (host controller).
215  *
216  * Returns length of the transaction or one of the USB_RET_XXX codes.
217  */
218 int usb_generic_handle_packet(USBDevice *s, USBPacket *p)
219 {
220     switch(p->pid) {
221     case USB_MSG_ATTACH:
222         s->state = USB_STATE_ATTACHED;
223         usb_device_handle_attach(s);
224         return 0;
225
226     case USB_MSG_DETACH:
227         s->state = USB_STATE_NOTATTACHED;
228         return 0;
229
230     case USB_MSG_RESET:
231         s->remote_wakeup = 0;
232         s->addr = 0;
233         s->state = USB_STATE_DEFAULT;
234         usb_device_handle_reset(s);
235         return 0;
236     }
237
238     /* Rest of the PIDs must match our address */
239     if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
240         return USB_RET_NODEV;
241
242     switch (p->pid) {
243     case USB_TOKEN_SETUP:
244         return do_token_setup(s, p);
245
246     case USB_TOKEN_IN:
247         return do_token_in(s, p);
248
249     case USB_TOKEN_OUT:
250         return do_token_out(s, p);
251  
252     default:
253         return USB_RET_STALL;
254     }
255 }
256
257 /* ctrl complete function for devices which use usb_generic_handle_packet and
258    may return USB_RET_ASYNC from their handle_control callback. Device code
259    which does this *must* call this function instead of the normal
260    usb_packet_complete to complete their async control packets. */
261 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
262 {
263     if (p->result < 0) {
264         s->setup_state = SETUP_STATE_IDLE;
265     }
266
267     switch (s->setup_state) {
268     case SETUP_STATE_SETUP:
269         if (p->result < s->setup_len) {
270             s->setup_len = p->result;
271         }
272         s->setup_state = SETUP_STATE_DATA;
273         p->result = 8;
274         break;
275
276     case SETUP_STATE_ACK:
277         s->setup_state = SETUP_STATE_IDLE;
278         p->result = 0;
279         break;
280
281     default:
282         break;
283     }
284     usb_packet_complete(s, p);
285 }
286
287 /* XXX: fix overflow */
288 int set_usb_string(uint8_t *buf, const char *str)
289 {
290     int len, i;
291     uint8_t *q;
292
293     q = buf;
294     len = strlen(str);
295     *q++ = 2 * len + 2;
296     *q++ = 3;
297     for(i = 0; i < len; i++) {
298         *q++ = str[i];
299         *q++ = 0;
300     }
301     return q - buf;
302 }
303
304 /* Send an internal message to a USB device.  */
305 void usb_send_msg(USBDevice *dev, int msg)
306 {
307     USBPacket p;
308     int ret;
309
310     memset(&p, 0, sizeof(p));
311     p.pid = msg;
312     ret = usb_handle_packet(dev, &p);
313     /* This _must_ be synchronous */
314     assert(ret != USB_RET_ASYNC);
315 }
316
317 /* Hand over a packet to a device for processing.  Return value
318    USB_RET_ASYNC indicates the processing isn't finished yet, the
319    driver will call usb_packet_complete() when done processing it. */
320 int usb_handle_packet(USBDevice *dev, USBPacket *p)
321 {
322     int ret;
323
324     assert(p->owner == NULL);
325     ret = usb_device_handle_packet(dev, p);
326     if (ret == USB_RET_ASYNC) {
327         if (p->owner == NULL) {
328             p->owner = usb_ep_get(dev, p->pid, p->devep);
329         } else {
330             /* We'll end up here when usb_handle_packet is called
331              * recursively due to a hub being in the chain.  Nothing
332              * to do.  Leave p->owner pointing to the device, not the
333              * hub. */;
334         }
335     }
336     return ret;
337 }
338
339 /* Notify the controller that an async packet is complete.  This should only
340    be called for packets previously deferred by returning USB_RET_ASYNC from
341    handle_packet. */
342 void usb_packet_complete(USBDevice *dev, USBPacket *p)
343 {
344     /* Note: p->owner != dev is possible in case dev is a hub */
345     assert(p->owner != NULL);
346     p->owner = NULL;
347     dev->port->ops->complete(dev->port, p);
348 }
349
350 /* Cancel an active packet.  The packed must have been deferred by
351    returning USB_RET_ASYNC from handle_packet, and not yet
352    completed.  */
353 void usb_cancel_packet(USBPacket * p)
354 {
355     assert(p->owner != NULL);
356     usb_device_cancel_packet(p->owner->dev, p);
357     p->owner = NULL;
358 }
359
360
361 void usb_packet_init(USBPacket *p)
362 {
363     qemu_iovec_init(&p->iov, 1);
364 }
365
366 void usb_packet_setup(USBPacket *p, int pid, uint8_t addr, uint8_t ep)
367 {
368     p->pid = pid;
369     p->devaddr = addr;
370     p->devep = ep;
371     p->result = 0;
372     qemu_iovec_reset(&p->iov);
373 }
374
375 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
376 {
377     qemu_iovec_add(&p->iov, ptr, len);
378 }
379
380 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
381 {
382     assert(p->result >= 0);
383     assert(p->result + bytes <= p->iov.size);
384     switch (p->pid) {
385     case USB_TOKEN_SETUP:
386     case USB_TOKEN_OUT:
387         iov_to_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
388         break;
389     case USB_TOKEN_IN:
390         iov_from_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
391         break;
392     default:
393         fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
394         abort();
395     }
396     p->result += bytes;
397 }
398
399 void usb_packet_skip(USBPacket *p, size_t bytes)
400 {
401     assert(p->result >= 0);
402     assert(p->result + bytes <= p->iov.size);
403     if (p->pid == USB_TOKEN_IN) {
404         iov_clear(p->iov.iov, p->iov.niov, p->result, bytes);
405     }
406     p->result += bytes;
407 }
408
409 void usb_packet_cleanup(USBPacket *p)
410 {
411     qemu_iovec_destroy(&p->iov);
412 }
413
414 void usb_ep_init(USBDevice *dev)
415 {
416     int ep;
417
418     dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
419     dev->ep_ctl.ifnum = 0;
420     dev->ep_ctl.dev = dev;
421     for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
422         dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
423         dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
424         dev->ep_in[ep].ifnum = 0;
425         dev->ep_out[ep].ifnum = 0;
426         dev->ep_in[ep].dev = dev;
427         dev->ep_out[ep].dev = dev;
428     }
429 }
430
431 void usb_ep_dump(USBDevice *dev)
432 {
433     static const char *tname[] = {
434         [USB_ENDPOINT_XFER_CONTROL] = "control",
435         [USB_ENDPOINT_XFER_ISOC]    = "isoc",
436         [USB_ENDPOINT_XFER_BULK]    = "bulk",
437         [USB_ENDPOINT_XFER_INT]     = "int",
438     };
439     int ifnum, ep, first;
440
441     fprintf(stderr, "Device \"%s\", config %d\n",
442             dev->product_desc, dev->configuration);
443     for (ifnum = 0; ifnum < 16; ifnum++) {
444         first = 1;
445         for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
446             if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
447                 dev->ep_in[ep].ifnum == ifnum) {
448                 if (first) {
449                     first = 0;
450                     fprintf(stderr, "  Interface %d, alternative %d\n",
451                             ifnum, dev->altsetting[ifnum]);
452                 }
453                 fprintf(stderr, "    Endpoint %d, IN, %s, %d max\n", ep,
454                         tname[dev->ep_in[ep].type],
455                         dev->ep_in[ep].max_packet_size);
456             }
457             if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
458                 dev->ep_out[ep].ifnum == ifnum) {
459                 if (first) {
460                     first = 0;
461                     fprintf(stderr, "  Interface %d, alternative %d\n",
462                             ifnum, dev->altsetting[ifnum]);
463                 }
464                 fprintf(stderr, "    Endpoint %d, OUT, %s, %d max\n", ep,
465                         tname[dev->ep_out[ep].type],
466                         dev->ep_out[ep].max_packet_size);
467             }
468         }
469     }
470     fprintf(stderr, "--\n");
471 }
472
473 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
474 {
475     struct USBEndpoint *eps = pid == USB_TOKEN_IN ? dev->ep_in : dev->ep_out;
476     if (ep == 0) {
477         return &dev->ep_ctl;
478     }
479     assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
480     assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
481     return eps + ep - 1;
482 }
483
484 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
485 {
486     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
487     return uep->type;
488 }
489
490 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
491 {
492     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
493     uep->type = type;
494 }
495
496 uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep)
497 {
498     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
499     return uep->ifnum;
500 }
501
502 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
503 {
504     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
505     uep->ifnum = ifnum;
506 }
507
508 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
509                                 uint16_t raw)
510 {
511     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
512     int size, microframes;
513
514     size = raw & 0x7ff;
515     switch ((raw >> 11) & 3) {
516     case 1:
517         microframes = 2;
518         break;
519     case 2:
520         microframes = 3;
521         break;
522     default:
523         microframes = 1;
524         break;
525     }
526     uep->max_packet_size = size * microframes;
527 }
528
529 int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep)
530 {
531     struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
532     return uep->max_packet_size;
533 }
This page took 0.060418 seconds and 4 git commands to generate.