]> Git Repo - qemu.git/blame - hw/usb.c
qxl: make sure primary surface is saved on migration also in compat mode
[qemu.git] / hw / usb.c
CommitLineData
bb36d470
FB
1/*
2 * QEMU USB emulation
3 *
4 * Copyright (c) 2005 Fabrice Bellard
5fafdf24 5 *
89b9b79f
AL
6 * 2008 Generic packet handler rewrite by Max Krasnyansky
7 *
bb36d470
FB
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 */
87ecb68b
PB
26#include "qemu-common.h"
27#include "usb.h"
4f4321c1 28#include "iov.h"
bb36d470 29
891fb2cd 30void usb_attach(USBPort *port)
bb36d470 31{
891fb2cd
GH
32 USBDevice *dev = port->dev;
33
34 assert(dev != NULL);
35 assert(dev->attached);
e0b8e72d 36 assert(dev->state == USB_STATE_NOTATTACHED);
891fb2cd 37 port->ops->attach(port);
d1f8b536
GH
38 dev->state = USB_STATE_ATTACHED;
39 usb_device_handle_attach(dev);
891fb2cd
GH
40}
41
42void usb_detach(USBPort *port)
43{
44 USBDevice *dev = port->dev;
45
46 assert(dev != NULL);
e0b8e72d 47 assert(dev->state != USB_STATE_NOTATTACHED);
891fb2cd 48 port->ops->detach(port);
d1f8b536 49 dev->state = USB_STATE_NOTATTACHED;
bb36d470
FB
50}
51
d28f4e2d 52void usb_port_reset(USBPort *port)
e0b8e72d
GH
53{
54 USBDevice *dev = port->dev;
55
56 assert(dev != NULL);
57 usb_detach(port);
58 usb_attach(port);
d28f4e2d
GH
59 usb_device_reset(dev);
60}
61
62void 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);
e0b8e72d
GH
71}
72
7567b51f 73void usb_wakeup(USBEndpoint *ep)
01eacab6 74{
7567b51f 75 USBDevice *dev = ep->dev;
37f32f0f 76 USBBus *bus = usb_bus_from_device(dev);
7567b51f 77
01eacab6 78 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
d47e59b8 79 dev->port->ops->wakeup(dev->port);
01eacab6 80 }
37f32f0f
GH
81 if (bus->ops->wakeup_endpoint) {
82 bus->ops->wakeup_endpoint(bus, ep);
83 }
01eacab6
GH
84}
85
bb36d470 86/**********************/
89b9b79f 87
bb36d470
FB
88/* generic USB device helpers (you are not forced to use them when
89 writing your USB device driver, but they help handling the
5fafdf24 90 protocol)
bb36d470
FB
91*/
92
50b7963e
HG
93#define SETUP_STATE_IDLE 0
94#define SETUP_STATE_SETUP 1
95#define SETUP_STATE_DATA 2
96#define SETUP_STATE_ACK 3
bb36d470 97
89b9b79f
AL
98static int do_token_setup(USBDevice *s, USBPacket *p)
99{
100 int request, value, index;
101 int ret = 0;
102
4f4321c1 103 if (p->iov.size != 8) {
89b9b79f 104 return USB_RET_STALL;
4f4321c1
GH
105 }
106
107 usb_packet_copy(p, s->setup_buf, p->iov.size);
89b9b79f
AL
108 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
109 s->setup_index = 0;
110
111 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
112 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
113 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
007fd62f 114
89b9b79f 115 if (s->setup_buf[0] & USB_DIR_IN) {
62aed765
AL
116 ret = usb_device_handle_control(s, p, request, value, index,
117 s->setup_len, s->data_buf);
50b7963e
HG
118 if (ret == USB_RET_ASYNC) {
119 s->setup_state = SETUP_STATE_SETUP;
120 return USB_RET_ASYNC;
121 }
89b9b79f
AL
122 if (ret < 0)
123 return ret;
124
125 if (ret < s->setup_len)
126 s->setup_len = ret;
127 s->setup_state = SETUP_STATE_DATA;
128 } else {
19f33223
HG
129 if (s->setup_len > sizeof(s->data_buf)) {
130 fprintf(stderr,
131 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
132 s->setup_len, sizeof(s->data_buf));
133 return USB_RET_STALL;
134 }
89b9b79f
AL
135 if (s->setup_len == 0)
136 s->setup_state = SETUP_STATE_ACK;
137 else
138 s->setup_state = SETUP_STATE_DATA;
139 }
140
141 return ret;
142}
143
144static int do_token_in(USBDevice *s, USBPacket *p)
bb36d470 145{
89b9b79f
AL
146 int request, value, index;
147 int ret = 0;
148
079d0b7f 149 assert(p->ep->nr == 0);
89b9b79f
AL
150
151 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
152 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
153 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
154
155 switch(s->setup_state) {
156 case SETUP_STATE_ACK:
157 if (!(s->setup_buf[0] & USB_DIR_IN)) {
62aed765
AL
158 ret = usb_device_handle_control(s, p, request, value, index,
159 s->setup_len, s->data_buf);
007fd62f
HG
160 if (ret == USB_RET_ASYNC) {
161 return USB_RET_ASYNC;
162 }
163 s->setup_state = SETUP_STATE_IDLE;
89b9b79f
AL
164 if (ret > 0)
165 return 0;
166 return ret;
167 }
168
169 /* return 0 byte */
170 return 0;
171
172 case SETUP_STATE_DATA:
173 if (s->setup_buf[0] & USB_DIR_IN) {
174 int len = s->setup_len - s->setup_index;
4f4321c1
GH
175 if (len > p->iov.size) {
176 len = p->iov.size;
177 }
178 usb_packet_copy(p, s->data_buf + s->setup_index, len);
89b9b79f
AL
179 s->setup_index += len;
180 if (s->setup_index >= s->setup_len)
181 s->setup_state = SETUP_STATE_ACK;
182 return len;
183 }
184
185 s->setup_state = SETUP_STATE_IDLE;
186 return USB_RET_STALL;
187
188 default:
189 return USB_RET_STALL;
190 }
191}
192
193static int do_token_out(USBDevice *s, USBPacket *p)
194{
079d0b7f 195 assert(p->ep->nr == 0);
89b9b79f
AL
196
197 switch(s->setup_state) {
198 case SETUP_STATE_ACK:
199 if (s->setup_buf[0] & USB_DIR_IN) {
200 s->setup_state = SETUP_STATE_IDLE;
201 /* transfer OK */
202 } else {
203 /* ignore additional output */
204 }
205 return 0;
206
207 case SETUP_STATE_DATA:
208 if (!(s->setup_buf[0] & USB_DIR_IN)) {
209 int len = s->setup_len - s->setup_index;
4f4321c1
GH
210 if (len > p->iov.size) {
211 len = p->iov.size;
212 }
213 usb_packet_copy(p, s->data_buf + s->setup_index, len);
89b9b79f
AL
214 s->setup_index += len;
215 if (s->setup_index >= s->setup_len)
216 s->setup_state = SETUP_STATE_ACK;
217 return len;
218 }
219
220 s->setup_state = SETUP_STATE_IDLE;
221 return USB_RET_STALL;
222
223 default:
224 return USB_RET_STALL;
225 }
226}
bb36d470 227
50b7963e
HG
228/* ctrl complete function for devices which use usb_generic_handle_packet and
229 may return USB_RET_ASYNC from their handle_control callback. Device code
230 which does this *must* call this function instead of the normal
231 usb_packet_complete to complete their async control packets. */
232void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
233{
4f4321c1 234 if (p->result < 0) {
50b7963e
HG
235 s->setup_state = SETUP_STATE_IDLE;
236 }
237
238 switch (s->setup_state) {
239 case SETUP_STATE_SETUP:
4f4321c1
GH
240 if (p->result < s->setup_len) {
241 s->setup_len = p->result;
50b7963e
HG
242 }
243 s->setup_state = SETUP_STATE_DATA;
4f4321c1 244 p->result = 8;
50b7963e
HG
245 break;
246
247 case SETUP_STATE_ACK:
248 s->setup_state = SETUP_STATE_IDLE;
4f4321c1 249 p->result = 0;
50b7963e
HG
250 break;
251
252 default:
253 break;
254 }
255 usb_packet_complete(s, p);
256}
257
bb36d470
FB
258/* XXX: fix overflow */
259int set_usb_string(uint8_t *buf, const char *str)
260{
261 int len, i;
262 uint8_t *q;
263
264 q = buf;
265 len = strlen(str);
ce5c37c2 266 *q++ = 2 * len + 2;
bb36d470
FB
267 *q++ = 3;
268 for(i = 0; i < len; i++) {
269 *q++ = str[i];
270 *q++ = 0;
271 }
272 return q - buf;
273}
4d611c9a 274
73796fe6
GH
275USBDevice *usb_find_device(USBPort *port, uint8_t addr)
276{
277 USBDevice *dev = port->dev;
278
279 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
280 return NULL;
281 }
282 if (dev->addr == addr) {
283 return dev;
284 }
285 return usb_device_find_device(dev, addr);
286}
287
db4be873
GH
288static int usb_process_one(USBPacket *p)
289{
290 USBDevice *dev = p->ep->dev;
291
292 if (p->ep->nr == 0) {
293 /* control pipe */
294 switch (p->pid) {
295 case USB_TOKEN_SETUP:
296 return do_token_setup(dev, p);
297 case USB_TOKEN_IN:
298 return do_token_in(dev, p);
299 case USB_TOKEN_OUT:
300 return do_token_out(dev, p);
301 default:
302 return USB_RET_STALL;
303 }
304 } else {
305 /* data pipe */
306 return usb_device_handle_data(dev, p);
307 }
308}
309
53aa8c0e
GH
310/* Hand over a packet to a device for processing. Return value
311 USB_RET_ASYNC indicates the processing isn't finished yet, the
312 driver will call usb_packet_complete() when done processing it. */
313int usb_handle_packet(USBDevice *dev, USBPacket *p)
314{
315 int ret;
316
98861f51
GH
317 if (dev == NULL) {
318 return USB_RET_NODEV;
319 }
079d0b7f 320 assert(dev == p->ep->dev);
1977f93d 321 assert(dev->state == USB_STATE_DEFAULT);
f53c398a 322 assert(p->state == USB_PACKET_SETUP);
db4be873 323 assert(p->ep != NULL);
1977f93d 324
db4be873
GH
325 if (QTAILQ_EMPTY(&p->ep->queue)) {
326 ret = usb_process_one(p);
327 if (ret == USB_RET_ASYNC) {
328 usb_packet_set_state(p, USB_PACKET_ASYNC);
329 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
330 } else {
331 p->result = ret;
332 usb_packet_set_state(p, USB_PACKET_COMPLETE);
1977f93d
GH
333 }
334 } else {
db4be873
GH
335 ret = USB_RET_ASYNC;
336 usb_packet_set_state(p, USB_PACKET_QUEUED);
337 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
4ff658fb 338 }
53aa8c0e 339 return ret;
89b9b79f 340}
4ff658fb
GH
341
342/* Notify the controller that an async packet is complete. This should only
343 be called for packets previously deferred by returning USB_RET_ASYNC from
344 handle_packet. */
345void usb_packet_complete(USBDevice *dev, USBPacket *p)
346{
db4be873
GH
347 USBEndpoint *ep = p->ep;
348 int ret;
349
f53c398a 350 assert(p->state == USB_PACKET_ASYNC);
db4be873
GH
351 assert(QTAILQ_FIRST(&ep->queue) == p);
352 usb_packet_set_state(p, USB_PACKET_COMPLETE);
353 QTAILQ_REMOVE(&ep->queue, p, queue);
4d8debba 354 dev->port->ops->complete(dev->port, p);
db4be873
GH
355
356 while (!QTAILQ_EMPTY(&ep->queue)) {
357 p = QTAILQ_FIRST(&ep->queue);
358 assert(p->state == USB_PACKET_QUEUED);
359 ret = usb_process_one(p);
360 if (ret == USB_RET_ASYNC) {
361 usb_packet_set_state(p, USB_PACKET_ASYNC);
362 break;
363 }
364 p->result = ret;
365 usb_packet_set_state(p, USB_PACKET_COMPLETE);
366 QTAILQ_REMOVE(&ep->queue, p, queue);
367 dev->port->ops->complete(dev->port, p);
368 }
4ff658fb
GH
369}
370
371/* Cancel an active packet. The packed must have been deferred by
372 returning USB_RET_ASYNC from handle_packet, and not yet
373 completed. */
374void usb_cancel_packet(USBPacket * p)
375{
db4be873
GH
376 bool callback = (p->state == USB_PACKET_ASYNC);
377 assert(usb_packet_is_inflight(p));
378 usb_packet_set_state(p, USB_PACKET_CANCELED);
379 QTAILQ_REMOVE(&p->ep->queue, p, queue);
380 if (callback) {
381 usb_device_cancel_packet(p->ep->dev, p);
382 }
4ff658fb 383}
4f4321c1
GH
384
385
386void usb_packet_init(USBPacket *p)
387{
388 qemu_iovec_init(&p->iov, 1);
389}
390
db4be873
GH
391void usb_packet_set_state(USBPacket *p, USBPacketState state)
392{
393#ifdef DEBUG
394 static const char *name[] = {
395 [USB_PACKET_UNDEFINED] = "undef",
396 [USB_PACKET_SETUP] = "setup",
397 [USB_PACKET_QUEUED] = "queued",
398 [USB_PACKET_ASYNC] = "async",
399 [USB_PACKET_COMPLETE] = "complete",
400 [USB_PACKET_CANCELED] = "canceled",
401 };
402 static const char *rets[] = {
403 [-USB_RET_NODEV] = "NODEV",
404 [-USB_RET_NAK] = "NAK",
405 [-USB_RET_STALL] = "STALL",
406 [-USB_RET_BABBLE] = "BABBLE",
407 [-USB_RET_ASYNC] = "ASYNC",
408 };
409 char add[16] = "";
410
411 if (state == USB_PACKET_COMPLETE) {
412 if (p->result < 0) {
413 snprintf(add, sizeof(add), " - %s", rets[-p->result]);
414 } else {
415 snprintf(add, sizeof(add), " - %d", p->result);
416 }
417 }
418 fprintf(stderr, "bus %s, port %s, dev %d, ep %d: packet %p: %s -> %s%s\n",
419 p->ep->dev->qdev.parent_bus->name,
420 p->ep->dev->port->path,
421 p->ep->dev->addr, p->ep->nr,
422 p, name[p->state], name[state], add);
423#endif
424 p->state = state;
425}
426
079d0b7f 427void usb_packet_setup(USBPacket *p, int pid, USBEndpoint *ep)
4f4321c1 428{
f53c398a 429 assert(!usb_packet_is_inflight(p));
4f4321c1 430 p->pid = pid;
079d0b7f 431 p->ep = ep;
4f4321c1
GH
432 p->result = 0;
433 qemu_iovec_reset(&p->iov);
db4be873 434 usb_packet_set_state(p, USB_PACKET_SETUP);
4f4321c1
GH
435}
436
437void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
438{
439 qemu_iovec_add(&p->iov, ptr, len);
440}
441
442void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
443{
444 assert(p->result >= 0);
445 assert(p->result + bytes <= p->iov.size);
446 switch (p->pid) {
447 case USB_TOKEN_SETUP:
448 case USB_TOKEN_OUT:
449 iov_to_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
450 break;
451 case USB_TOKEN_IN:
452 iov_from_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
453 break;
454 default:
455 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
456 abort();
457 }
458 p->result += bytes;
459}
460
461void usb_packet_skip(USBPacket *p, size_t bytes)
462{
463 assert(p->result >= 0);
464 assert(p->result + bytes <= p->iov.size);
465 if (p->pid == USB_TOKEN_IN) {
466 iov_clear(p->iov.iov, p->iov.niov, p->result, bytes);
467 }
468 p->result += bytes;
469}
470
471void usb_packet_cleanup(USBPacket *p)
472{
f53c398a 473 assert(!usb_packet_is_inflight(p));
4f4321c1
GH
474 qemu_iovec_destroy(&p->iov);
475}
d8e17efd
GH
476
477void usb_ep_init(USBDevice *dev)
478{
479 int ep;
480
63095ab5 481 dev->ep_ctl.nr = 0;
25d5de7d
GH
482 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
483 dev->ep_ctl.ifnum = 0;
484 dev->ep_ctl.dev = dev;
db4be873 485 QTAILQ_INIT(&dev->ep_ctl.queue);
d8e17efd 486 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
63095ab5
GH
487 dev->ep_in[ep].nr = ep + 1;
488 dev->ep_out[ep].nr = ep + 1;
489 dev->ep_in[ep].pid = USB_TOKEN_IN;
490 dev->ep_out[ep].pid = USB_TOKEN_OUT;
d8e17efd
GH
491 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
492 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
82f02fe9
GH
493 dev->ep_in[ep].ifnum = 0;
494 dev->ep_out[ep].ifnum = 0;
25d5de7d
GH
495 dev->ep_in[ep].dev = dev;
496 dev->ep_out[ep].dev = dev;
db4be873
GH
497 QTAILQ_INIT(&dev->ep_in[ep].queue);
498 QTAILQ_INIT(&dev->ep_out[ep].queue);
d8e17efd
GH
499 }
500}
501
5b6780d0
GH
502void usb_ep_dump(USBDevice *dev)
503{
504 static const char *tname[] = {
505 [USB_ENDPOINT_XFER_CONTROL] = "control",
506 [USB_ENDPOINT_XFER_ISOC] = "isoc",
507 [USB_ENDPOINT_XFER_BULK] = "bulk",
508 [USB_ENDPOINT_XFER_INT] = "int",
509 };
510 int ifnum, ep, first;
511
512 fprintf(stderr, "Device \"%s\", config %d\n",
513 dev->product_desc, dev->configuration);
514 for (ifnum = 0; ifnum < 16; ifnum++) {
515 first = 1;
516 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
517 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
518 dev->ep_in[ep].ifnum == ifnum) {
519 if (first) {
520 first = 0;
521 fprintf(stderr, " Interface %d, alternative %d\n",
522 ifnum, dev->altsetting[ifnum]);
523 }
f003397c
GH
524 fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
525 tname[dev->ep_in[ep].type],
526 dev->ep_in[ep].max_packet_size);
5b6780d0
GH
527 }
528 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
529 dev->ep_out[ep].ifnum == ifnum) {
530 if (first) {
531 first = 0;
532 fprintf(stderr, " Interface %d, alternative %d\n",
533 ifnum, dev->altsetting[ifnum]);
534 }
f003397c
GH
535 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
536 tname[dev->ep_out[ep].type],
537 dev->ep_out[ep].max_packet_size);
5b6780d0
GH
538 }
539 }
540 }
541 fprintf(stderr, "--\n");
542}
543
d8e17efd
GH
544struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
545{
079d0b7f
GH
546 struct USBEndpoint *eps;
547
548 if (dev == NULL) {
549 return NULL;
550 }
551 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
25d5de7d
GH
552 if (ep == 0) {
553 return &dev->ep_ctl;
554 }
d8e17efd
GH
555 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
556 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
557 return eps + ep - 1;
558}
559
560uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
561{
562 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
563 return uep->type;
564}
565
566void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
567{
568 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
569 uep->type = type;
570}
82f02fe9
GH
571
572uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep)
573{
574 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
575 return uep->ifnum;
576}
577
578void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
579{
580 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
581 uep->ifnum = ifnum;
582}
f003397c
GH
583
584void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
585 uint16_t raw)
586{
587 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
588 int size, microframes;
589
590 size = raw & 0x7ff;
591 switch ((raw >> 11) & 3) {
592 case 1:
593 microframes = 2;
594 break;
595 case 2:
596 microframes = 3;
597 break;
598 default:
599 microframes = 1;
600 break;
601 }
602 uep->max_packet_size = size * microframes;
603}
604
605int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep)
606{
607 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
608 return uep->max_packet_size;
609}
This page took 0.775522 seconds and 4 git commands to generate.