qapi-schema: fix typos and explain 'spice' auth
[qemu/ar7.git] / hw / usb.c
blob1ec2e90ef71ed9257b70733c19d0b5b03f688ea5
1 /*
2 * QEMU USB emulation
4 * Copyright (c) 2005 Fabrice Bellard
6 * 2008 Generic packet handler rewrite by Max Krasnyansky
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 #include "qemu-common.h"
27 #include "usb.h"
28 #include "iov.h"
29 #include "trace.h"
31 void usb_attach(USBPort *port)
33 USBDevice *dev = port->dev;
35 assert(dev != NULL);
36 assert(dev->attached);
37 assert(dev->state == USB_STATE_NOTATTACHED);
38 port->ops->attach(port);
39 dev->state = USB_STATE_ATTACHED;
40 usb_device_handle_attach(dev);
43 void usb_detach(USBPort *port)
45 USBDevice *dev = port->dev;
47 assert(dev != NULL);
48 assert(dev->state != USB_STATE_NOTATTACHED);
49 port->ops->detach(port);
50 dev->state = USB_STATE_NOTATTACHED;
53 void usb_port_reset(USBPort *port)
55 USBDevice *dev = port->dev;
57 assert(dev != NULL);
58 usb_detach(port);
59 usb_attach(port);
60 usb_device_reset(dev);
63 void usb_device_reset(USBDevice *dev)
65 if (dev == NULL || !dev->attached) {
66 return;
68 dev->remote_wakeup = 0;
69 dev->addr = 0;
70 dev->state = USB_STATE_DEFAULT;
71 usb_device_handle_reset(dev);
74 void usb_wakeup(USBEndpoint *ep)
76 USBDevice *dev = ep->dev;
77 USBBus *bus = usb_bus_from_device(dev);
79 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
80 dev->port->ops->wakeup(dev->port);
82 if (bus->ops->wakeup_endpoint) {
83 bus->ops->wakeup_endpoint(bus, ep);
87 /**********************/
89 /* generic USB device helpers (you are not forced to use them when
90 writing your USB device driver, but they help handling the
91 protocol)
94 #define SETUP_STATE_IDLE 0
95 #define SETUP_STATE_SETUP 1
96 #define SETUP_STATE_DATA 2
97 #define SETUP_STATE_ACK 3
98 #define SETUP_STATE_PARAM 4
100 static int do_token_setup(USBDevice *s, USBPacket *p)
102 int request, value, index;
103 int ret = 0;
105 if (p->iov.size != 8) {
106 return USB_RET_STALL;
109 usb_packet_copy(p, s->setup_buf, p->iov.size);
110 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
111 s->setup_index = 0;
113 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
114 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
115 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
117 if (s->setup_buf[0] & USB_DIR_IN) {
118 ret = usb_device_handle_control(s, p, request, value, index,
119 s->setup_len, s->data_buf);
120 if (ret == USB_RET_ASYNC) {
121 s->setup_state = SETUP_STATE_SETUP;
122 return USB_RET_ASYNC;
124 if (ret < 0)
125 return ret;
127 if (ret < s->setup_len)
128 s->setup_len = ret;
129 s->setup_state = SETUP_STATE_DATA;
130 } else {
131 if (s->setup_len > sizeof(s->data_buf)) {
132 fprintf(stderr,
133 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
134 s->setup_len, sizeof(s->data_buf));
135 return USB_RET_STALL;
137 if (s->setup_len == 0)
138 s->setup_state = SETUP_STATE_ACK;
139 else
140 s->setup_state = SETUP_STATE_DATA;
143 return ret;
146 static int do_token_in(USBDevice *s, USBPacket *p)
148 int request, value, index;
149 int ret = 0;
151 assert(p->ep->nr == 0);
153 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
154 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
155 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
157 switch(s->setup_state) {
158 case SETUP_STATE_ACK:
159 if (!(s->setup_buf[0] & USB_DIR_IN)) {
160 ret = usb_device_handle_control(s, p, request, value, index,
161 s->setup_len, s->data_buf);
162 if (ret == USB_RET_ASYNC) {
163 return USB_RET_ASYNC;
165 s->setup_state = SETUP_STATE_IDLE;
166 if (ret > 0)
167 return 0;
168 return ret;
171 /* return 0 byte */
172 return 0;
174 case SETUP_STATE_DATA:
175 if (s->setup_buf[0] & USB_DIR_IN) {
176 int len = s->setup_len - s->setup_index;
177 if (len > p->iov.size) {
178 len = p->iov.size;
180 usb_packet_copy(p, s->data_buf + s->setup_index, len);
181 s->setup_index += len;
182 if (s->setup_index >= s->setup_len)
183 s->setup_state = SETUP_STATE_ACK;
184 return len;
187 s->setup_state = SETUP_STATE_IDLE;
188 return USB_RET_STALL;
190 default:
191 return USB_RET_STALL;
195 static int do_token_out(USBDevice *s, USBPacket *p)
197 assert(p->ep->nr == 0);
199 switch(s->setup_state) {
200 case SETUP_STATE_ACK:
201 if (s->setup_buf[0] & USB_DIR_IN) {
202 s->setup_state = SETUP_STATE_IDLE;
203 /* transfer OK */
204 } else {
205 /* ignore additional output */
207 return 0;
209 case SETUP_STATE_DATA:
210 if (!(s->setup_buf[0] & USB_DIR_IN)) {
211 int len = s->setup_len - s->setup_index;
212 if (len > p->iov.size) {
213 len = p->iov.size;
215 usb_packet_copy(p, s->data_buf + s->setup_index, len);
216 s->setup_index += len;
217 if (s->setup_index >= s->setup_len)
218 s->setup_state = SETUP_STATE_ACK;
219 return len;
222 s->setup_state = SETUP_STATE_IDLE;
223 return USB_RET_STALL;
225 default:
226 return USB_RET_STALL;
230 static int do_parameter(USBDevice *s, USBPacket *p)
232 int request, value, index;
233 int i, ret = 0;
235 for (i = 0; i < 8; i++) {
236 s->setup_buf[i] = p->parameter >> (i*8);
239 s->setup_state = SETUP_STATE_PARAM;
240 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
241 s->setup_index = 0;
243 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
244 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
245 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
247 if (s->setup_len > sizeof(s->data_buf)) {
248 fprintf(stderr,
249 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
250 s->setup_len, sizeof(s->data_buf));
251 return USB_RET_STALL;
254 if (p->pid == USB_TOKEN_OUT) {
255 usb_packet_copy(p, s->data_buf, s->setup_len);
258 ret = usb_device_handle_control(s, p, request, value, index,
259 s->setup_len, s->data_buf);
260 if (ret < 0) {
261 return ret;
264 if (ret < s->setup_len) {
265 s->setup_len = ret;
267 if (p->pid == USB_TOKEN_IN) {
268 usb_packet_copy(p, s->data_buf, s->setup_len);
271 return ret;
274 /* ctrl complete function for devices which use usb_generic_handle_packet and
275 may return USB_RET_ASYNC from their handle_control callback. Device code
276 which does this *must* call this function instead of the normal
277 usb_packet_complete to complete their async control packets. */
278 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
280 if (p->result < 0) {
281 s->setup_state = SETUP_STATE_IDLE;
284 switch (s->setup_state) {
285 case SETUP_STATE_SETUP:
286 if (p->result < s->setup_len) {
287 s->setup_len = p->result;
289 s->setup_state = SETUP_STATE_DATA;
290 p->result = 8;
291 break;
293 case SETUP_STATE_ACK:
294 s->setup_state = SETUP_STATE_IDLE;
295 p->result = 0;
296 break;
298 case SETUP_STATE_PARAM:
299 if (p->result < s->setup_len) {
300 s->setup_len = p->result;
302 if (p->pid == USB_TOKEN_IN) {
303 p->result = 0;
304 usb_packet_copy(p, s->data_buf, s->setup_len);
306 break;
308 default:
309 break;
311 usb_packet_complete(s, p);
314 /* XXX: fix overflow */
315 int set_usb_string(uint8_t *buf, const char *str)
317 int len, i;
318 uint8_t *q;
320 q = buf;
321 len = strlen(str);
322 *q++ = 2 * len + 2;
323 *q++ = 3;
324 for(i = 0; i < len; i++) {
325 *q++ = str[i];
326 *q++ = 0;
328 return q - buf;
331 USBDevice *usb_find_device(USBPort *port, uint8_t addr)
333 USBDevice *dev = port->dev;
335 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
336 return NULL;
338 if (dev->addr == addr) {
339 return dev;
341 return usb_device_find_device(dev, addr);
344 static int usb_process_one(USBPacket *p)
346 USBDevice *dev = p->ep->dev;
348 if (p->ep->nr == 0) {
349 /* control pipe */
350 if (p->parameter) {
351 return do_parameter(dev, p);
353 switch (p->pid) {
354 case USB_TOKEN_SETUP:
355 return do_token_setup(dev, p);
356 case USB_TOKEN_IN:
357 return do_token_in(dev, p);
358 case USB_TOKEN_OUT:
359 return do_token_out(dev, p);
360 default:
361 return USB_RET_STALL;
363 } else {
364 /* data pipe */
365 return usb_device_handle_data(dev, p);
369 /* Hand over a packet to a device for processing. Return value
370 USB_RET_ASYNC indicates the processing isn't finished yet, the
371 driver will call usb_packet_complete() when done processing it. */
372 int usb_handle_packet(USBDevice *dev, USBPacket *p)
374 int ret;
376 if (dev == NULL) {
377 return USB_RET_NODEV;
379 assert(dev == p->ep->dev);
380 assert(dev->state == USB_STATE_DEFAULT);
381 assert(p->state == USB_PACKET_SETUP);
382 assert(p->ep != NULL);
384 if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline) {
385 ret = usb_process_one(p);
386 if (ret == USB_RET_ASYNC) {
387 usb_packet_set_state(p, USB_PACKET_ASYNC);
388 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
389 } else {
390 p->result = ret;
391 usb_packet_set_state(p, USB_PACKET_COMPLETE);
393 } else {
394 ret = USB_RET_ASYNC;
395 usb_packet_set_state(p, USB_PACKET_QUEUED);
396 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
398 return ret;
401 /* Notify the controller that an async packet is complete. This should only
402 be called for packets previously deferred by returning USB_RET_ASYNC from
403 handle_packet. */
404 void usb_packet_complete(USBDevice *dev, USBPacket *p)
406 USBEndpoint *ep = p->ep;
407 int ret;
409 assert(p->state == USB_PACKET_ASYNC);
410 assert(QTAILQ_FIRST(&ep->queue) == p);
411 usb_packet_set_state(p, USB_PACKET_COMPLETE);
412 QTAILQ_REMOVE(&ep->queue, p, queue);
413 dev->port->ops->complete(dev->port, p);
415 while (!QTAILQ_EMPTY(&ep->queue)) {
416 p = QTAILQ_FIRST(&ep->queue);
417 if (p->state == USB_PACKET_ASYNC) {
418 break;
420 assert(p->state == USB_PACKET_QUEUED);
421 ret = usb_process_one(p);
422 if (ret == USB_RET_ASYNC) {
423 usb_packet_set_state(p, USB_PACKET_ASYNC);
424 break;
426 p->result = ret;
427 usb_packet_set_state(p, USB_PACKET_COMPLETE);
428 QTAILQ_REMOVE(&ep->queue, p, queue);
429 dev->port->ops->complete(dev->port, p);
433 /* Cancel an active packet. The packed must have been deferred by
434 returning USB_RET_ASYNC from handle_packet, and not yet
435 completed. */
436 void usb_cancel_packet(USBPacket * p)
438 bool callback = (p->state == USB_PACKET_ASYNC);
439 assert(usb_packet_is_inflight(p));
440 usb_packet_set_state(p, USB_PACKET_CANCELED);
441 QTAILQ_REMOVE(&p->ep->queue, p, queue);
442 if (callback) {
443 usb_device_cancel_packet(p->ep->dev, p);
448 void usb_packet_init(USBPacket *p)
450 qemu_iovec_init(&p->iov, 1);
453 void usb_packet_set_state(USBPacket *p, USBPacketState state)
455 static const char *name[] = {
456 [USB_PACKET_UNDEFINED] = "undef",
457 [USB_PACKET_SETUP] = "setup",
458 [USB_PACKET_QUEUED] = "queued",
459 [USB_PACKET_ASYNC] = "async",
460 [USB_PACKET_COMPLETE] = "complete",
461 [USB_PACKET_CANCELED] = "canceled",
463 USBDevice *dev = p->ep->dev;
464 USBBus *bus = usb_bus_from_device(dev);
466 trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr,
467 p, name[p->state], name[state]);
468 p->state = state;
471 void usb_packet_setup(USBPacket *p, int pid, USBEndpoint *ep)
473 assert(!usb_packet_is_inflight(p));
474 p->pid = pid;
475 p->ep = ep;
476 p->result = 0;
477 p->parameter = 0;
478 qemu_iovec_reset(&p->iov);
479 usb_packet_set_state(p, USB_PACKET_SETUP);
482 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
484 qemu_iovec_add(&p->iov, ptr, len);
487 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
489 assert(p->result >= 0);
490 assert(p->result + bytes <= p->iov.size);
491 switch (p->pid) {
492 case USB_TOKEN_SETUP:
493 case USB_TOKEN_OUT:
494 iov_to_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
495 break;
496 case USB_TOKEN_IN:
497 iov_from_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
498 break;
499 default:
500 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
501 abort();
503 p->result += bytes;
506 void usb_packet_skip(USBPacket *p, size_t bytes)
508 assert(p->result >= 0);
509 assert(p->result + bytes <= p->iov.size);
510 if (p->pid == USB_TOKEN_IN) {
511 iov_clear(p->iov.iov, p->iov.niov, p->result, bytes);
513 p->result += bytes;
516 void usb_packet_cleanup(USBPacket *p)
518 assert(!usb_packet_is_inflight(p));
519 qemu_iovec_destroy(&p->iov);
522 void usb_ep_init(USBDevice *dev)
524 int ep;
526 dev->ep_ctl.nr = 0;
527 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
528 dev->ep_ctl.ifnum = 0;
529 dev->ep_ctl.dev = dev;
530 dev->ep_ctl.pipeline = false;
531 QTAILQ_INIT(&dev->ep_ctl.queue);
532 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
533 dev->ep_in[ep].nr = ep + 1;
534 dev->ep_out[ep].nr = ep + 1;
535 dev->ep_in[ep].pid = USB_TOKEN_IN;
536 dev->ep_out[ep].pid = USB_TOKEN_OUT;
537 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
538 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
539 dev->ep_in[ep].ifnum = 0;
540 dev->ep_out[ep].ifnum = 0;
541 dev->ep_in[ep].dev = dev;
542 dev->ep_out[ep].dev = dev;
543 dev->ep_in[ep].pipeline = false;
544 dev->ep_out[ep].pipeline = false;
545 QTAILQ_INIT(&dev->ep_in[ep].queue);
546 QTAILQ_INIT(&dev->ep_out[ep].queue);
550 void usb_ep_dump(USBDevice *dev)
552 static const char *tname[] = {
553 [USB_ENDPOINT_XFER_CONTROL] = "control",
554 [USB_ENDPOINT_XFER_ISOC] = "isoc",
555 [USB_ENDPOINT_XFER_BULK] = "bulk",
556 [USB_ENDPOINT_XFER_INT] = "int",
558 int ifnum, ep, first;
560 fprintf(stderr, "Device \"%s\", config %d\n",
561 dev->product_desc, dev->configuration);
562 for (ifnum = 0; ifnum < 16; ifnum++) {
563 first = 1;
564 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
565 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
566 dev->ep_in[ep].ifnum == ifnum) {
567 if (first) {
568 first = 0;
569 fprintf(stderr, " Interface %d, alternative %d\n",
570 ifnum, dev->altsetting[ifnum]);
572 fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
573 tname[dev->ep_in[ep].type],
574 dev->ep_in[ep].max_packet_size);
576 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
577 dev->ep_out[ep].ifnum == ifnum) {
578 if (first) {
579 first = 0;
580 fprintf(stderr, " Interface %d, alternative %d\n",
581 ifnum, dev->altsetting[ifnum]);
583 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
584 tname[dev->ep_out[ep].type],
585 dev->ep_out[ep].max_packet_size);
589 fprintf(stderr, "--\n");
592 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
594 struct USBEndpoint *eps;
596 if (dev == NULL) {
597 return NULL;
599 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
600 if (ep == 0) {
601 return &dev->ep_ctl;
603 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
604 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
605 return eps + ep - 1;
608 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
610 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
611 return uep->type;
614 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
616 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
617 uep->type = type;
620 uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep)
622 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
623 return uep->ifnum;
626 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
628 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
629 uep->ifnum = ifnum;
632 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
633 uint16_t raw)
635 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
636 int size, microframes;
638 size = raw & 0x7ff;
639 switch ((raw >> 11) & 3) {
640 case 1:
641 microframes = 2;
642 break;
643 case 2:
644 microframes = 3;
645 break;
646 default:
647 microframes = 1;
648 break;
650 uep->max_packet_size = size * microframes;
653 int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep)
655 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
656 return uep->max_packet_size;
659 void usb_ep_set_pipeline(USBDevice *dev, int pid, int ep, bool enabled)
661 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
662 uep->pipeline = enabled;