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
26 #include "qemu-common.h"
30 void usb_attach(USBPort *port)
32 USBDevice *dev = port->dev;
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;
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;
59 usb_device_reset(dev);
62 void usb_device_reset(USBDevice *dev)
64 if (dev == NULL || !dev->attached) {
67 dev->remote_wakeup = 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
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;
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];
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;
125 if (ret < s->setup_len)
127 s->setup_state = SETUP_STATE_DATA;
129 if (s->setup_len > sizeof(s->data_buf)) {
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;
138 s->setup_state = SETUP_STATE_DATA;
144 static int do_token_in(USBDevice *s, USBPacket *p)
146 int request, value, index;
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;
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) {
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;
185 s->setup_state = SETUP_STATE_IDLE;
186 return USB_RET_STALL;
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;
203 /* ignore additional output */
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) {
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;
220 s->setup_state = SETUP_STATE_IDLE;
221 return USB_RET_STALL;
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)
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;
247 case SETUP_STATE_ACK:
248 s->setup_state = SETUP_STATE_IDLE;
255 usb_packet_complete(s, p);
258 /* XXX: fix overflow */
259 int set_usb_string(uint8_t *buf, const char *str)
268 for(i = 0; i < len; i++) {
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) {
282 if (dev->addr == addr) {
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) {
295 case USB_TOKEN_SETUP:
296 return do_token_setup(dev, p);
298 return do_token_in(dev, p);
300 return do_token_out(dev, p);
302 return USB_RET_STALL;
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)
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);
332 usb_packet_set_state(p, USB_PACKET_COMPLETE);
336 usb_packet_set_state(p, USB_PACKET_QUEUED);
337 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
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
345 void usb_packet_complete(USBDevice *dev, USBPacket *p)
347 USBEndpoint *ep = p->ep;
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);
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
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);
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)
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",
411 if (state == USB_PACKET_COMPLETE) {
413 snprintf(add, sizeof(add), " - %s", rets[-p->result]);
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);
427 void usb_packet_setup(USBPacket *p, int pid, USBEndpoint *ep)
429 assert(!usb_packet_is_inflight(p));
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);
447 case USB_TOKEN_SETUP:
449 iov_to_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
452 iov_from_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
455 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
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);
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)
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++) {
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) {
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) {
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;
551 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
555 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
556 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
560 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
562 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
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);
572 uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep)
574 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
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);
584 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
587 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
588 int size, microframes;
591 switch ((raw >> 11) & 3) {
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;