vnc: Don't demote authentication scheme when changing password/disabling login
[qemu-kvm.git] / hw / usb.c
blobe5b8f335dc388e7ba00fb50bf9e8b03cbc5f8357
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"
30 void usb_attach(USBPort *port)
32 USBDevice *dev = port->dev;
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);
42 void usb_detach(USBPort *port)
44 USBDevice *dev = port->dev;
46 assert(dev != NULL);
47 assert(dev->state != USB_STATE_NOTATTACHED);
48 port->ops->detach(port);
49 dev->state = USB_STATE_NOTATTACHED;
52 void usb_port_reset(USBPort *port)
54 USBDevice *dev = port->dev;
56 assert(dev != NULL);
57 usb_detach(port);
58 usb_attach(port);
59 usb_device_reset(dev);
62 void usb_device_reset(USBDevice *dev)
64 if (dev == NULL || !dev->attached) {
65 return;
67 dev->remote_wakeup = 0;
68 dev->addr = 0;
69 dev->state = USB_STATE_DEFAULT;
70 usb_device_handle_reset(dev);
73 void usb_wakeup(USBEndpoint *ep)
75 USBDevice *dev = ep->dev;
76 USBBus *bus = usb_bus_from_device(dev);
78 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
79 dev->port->ops->wakeup(dev->port);
81 if (bus->ops->wakeup_endpoint) {
82 bus->ops->wakeup_endpoint(bus, ep);
86 /**********************/
88 /* generic USB device helpers (you are not forced to use them when
89 writing your USB device driver, but they help handling the
90 protocol)
93 #define SETUP_STATE_IDLE 0
94 #define SETUP_STATE_SETUP 1
95 #define SETUP_STATE_DATA 2
96 #define SETUP_STATE_ACK 3
98 static int do_token_setup(USBDevice *s, USBPacket *p)
100 int request, value, index;
101 int ret = 0;
103 if (p->iov.size != 8) {
104 return USB_RET_STALL;
107 usb_packet_copy(p, s->setup_buf, p->iov.size);
108 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
109 s->setup_index = 0;
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];
115 if (s->setup_buf[0] & USB_DIR_IN) {
116 ret = usb_device_handle_control(s, p, request, value, index,
117 s->setup_len, s->data_buf);
118 if (ret == USB_RET_ASYNC) {
119 s->setup_state = SETUP_STATE_SETUP;
120 return USB_RET_ASYNC;
122 if (ret < 0)
123 return ret;
125 if (ret < s->setup_len)
126 s->setup_len = ret;
127 s->setup_state = SETUP_STATE_DATA;
128 } else {
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;
135 if (s->setup_len == 0)
136 s->setup_state = SETUP_STATE_ACK;
137 else
138 s->setup_state = SETUP_STATE_DATA;
141 return ret;
144 static int do_token_in(USBDevice *s, USBPacket *p)
146 int request, value, index;
147 int ret = 0;
149 assert(p->ep->nr == 0);
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];
155 switch(s->setup_state) {
156 case SETUP_STATE_ACK:
157 if (!(s->setup_buf[0] & USB_DIR_IN)) {
158 ret = usb_device_handle_control(s, p, request, value, index,
159 s->setup_len, s->data_buf);
160 if (ret == USB_RET_ASYNC) {
161 return USB_RET_ASYNC;
163 s->setup_state = SETUP_STATE_IDLE;
164 if (ret > 0)
165 return 0;
166 return ret;
169 /* return 0 byte */
170 return 0;
172 case SETUP_STATE_DATA:
173 if (s->setup_buf[0] & USB_DIR_IN) {
174 int len = s->setup_len - s->setup_index;
175 if (len > p->iov.size) {
176 len = p->iov.size;
178 usb_packet_copy(p, s->data_buf + s->setup_index, len);
179 s->setup_index += len;
180 if (s->setup_index >= s->setup_len)
181 s->setup_state = SETUP_STATE_ACK;
182 return len;
185 s->setup_state = SETUP_STATE_IDLE;
186 return USB_RET_STALL;
188 default:
189 return USB_RET_STALL;
193 static int do_token_out(USBDevice *s, USBPacket *p)
195 assert(p->ep->nr == 0);
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 */
205 return 0;
207 case SETUP_STATE_DATA:
208 if (!(s->setup_buf[0] & USB_DIR_IN)) {
209 int len = s->setup_len - s->setup_index;
210 if (len > p->iov.size) {
211 len = p->iov.size;
213 usb_packet_copy(p, s->data_buf + s->setup_index, len);
214 s->setup_index += len;
215 if (s->setup_index >= s->setup_len)
216 s->setup_state = SETUP_STATE_ACK;
217 return len;
220 s->setup_state = SETUP_STATE_IDLE;
221 return USB_RET_STALL;
223 default:
224 return USB_RET_STALL;
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. */
232 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
234 if (p->result < 0) {
235 s->setup_state = SETUP_STATE_IDLE;
238 switch (s->setup_state) {
239 case SETUP_STATE_SETUP:
240 if (p->result < s->setup_len) {
241 s->setup_len = p->result;
243 s->setup_state = SETUP_STATE_DATA;
244 p->result = 8;
245 break;
247 case SETUP_STATE_ACK:
248 s->setup_state = SETUP_STATE_IDLE;
249 p->result = 0;
250 break;
252 default:
253 break;
255 usb_packet_complete(s, p);
258 /* XXX: fix overflow */
259 int set_usb_string(uint8_t *buf, const char *str)
261 int len, i;
262 uint8_t *q;
264 q = buf;
265 len = strlen(str);
266 *q++ = 2 * len + 2;
267 *q++ = 3;
268 for(i = 0; i < len; i++) {
269 *q++ = str[i];
270 *q++ = 0;
272 return q - buf;
275 USBDevice *usb_find_device(USBPort *port, uint8_t addr)
277 USBDevice *dev = port->dev;
279 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
280 return NULL;
282 if (dev->addr == addr) {
283 return dev;
285 return usb_device_find_device(dev, addr);
288 static int usb_process_one(USBPacket *p)
290 USBDevice *dev = p->ep->dev;
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;
304 } else {
305 /* data pipe */
306 return usb_device_handle_data(dev, p);
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. */
313 int usb_handle_packet(USBDevice *dev, USBPacket *p)
315 int ret;
317 if (dev == NULL) {
318 return USB_RET_NODEV;
320 assert(dev == p->ep->dev);
321 assert(dev->state == USB_STATE_DEFAULT);
322 assert(p->state == USB_PACKET_SETUP);
323 assert(p->ep != NULL);
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);
334 } else {
335 ret = USB_RET_ASYNC;
336 usb_packet_set_state(p, USB_PACKET_QUEUED);
337 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
339 return ret;
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. */
345 void usb_packet_complete(USBDevice *dev, USBPacket *p)
347 USBEndpoint *ep = p->ep;
348 int ret;
350 assert(p->state == USB_PACKET_ASYNC);
351 assert(QTAILQ_FIRST(&ep->queue) == p);
352 usb_packet_set_state(p, USB_PACKET_COMPLETE);
353 QTAILQ_REMOVE(&ep->queue, p, queue);
354 dev->port->ops->complete(dev->port, p);
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;
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);
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. */
374 void usb_cancel_packet(USBPacket * p)
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);
386 void usb_packet_init(USBPacket *p)
388 qemu_iovec_init(&p->iov, 1);
391 void usb_packet_set_state(USBPacket *p, USBPacketState state)
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",
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",
409 char add[16] = "";
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);
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;
427 void usb_packet_setup(USBPacket *p, int pid, USBEndpoint *ep)
429 assert(!usb_packet_is_inflight(p));
430 p->pid = pid;
431 p->ep = ep;
432 p->result = 0;
433 qemu_iovec_reset(&p->iov);
434 usb_packet_set_state(p, USB_PACKET_SETUP);
437 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
439 qemu_iovec_add(&p->iov, ptr, len);
442 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
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();
458 p->result += bytes;
461 void usb_packet_skip(USBPacket *p, size_t bytes)
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);
468 p->result += bytes;
471 void usb_packet_cleanup(USBPacket *p)
473 assert(!usb_packet_is_inflight(p));
474 qemu_iovec_destroy(&p->iov);
477 void usb_ep_init(USBDevice *dev)
479 int ep;
481 dev->ep_ctl.nr = 0;
482 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
483 dev->ep_ctl.ifnum = 0;
484 dev->ep_ctl.dev = dev;
485 QTAILQ_INIT(&dev->ep_ctl.queue);
486 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
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;
491 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
492 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
493 dev->ep_in[ep].ifnum = 0;
494 dev->ep_out[ep].ifnum = 0;
495 dev->ep_in[ep].dev = dev;
496 dev->ep_out[ep].dev = dev;
497 QTAILQ_INIT(&dev->ep_in[ep].queue);
498 QTAILQ_INIT(&dev->ep_out[ep].queue);
502 void usb_ep_dump(USBDevice *dev)
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",
510 int ifnum, ep, first;
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]);
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);
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]);
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);
541 fprintf(stderr, "--\n");
544 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
546 struct USBEndpoint *eps;
548 if (dev == NULL) {
549 return NULL;
551 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
552 if (ep == 0) {
553 return &dev->ep_ctl;
555 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
556 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
557 return eps + ep - 1;
560 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
562 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
563 return uep->type;
566 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
568 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
569 uep->type = type;
572 uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep)
574 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
575 return uep->ifnum;
578 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
580 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
581 uep->ifnum = ifnum;
584 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
585 uint16_t raw)
587 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
588 int size, microframes;
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;
602 uep->max_packet_size = size * microframes;
605 int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep)
607 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
608 return uep->max_packet_size;