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