From e6a2f50042580796de2df38e7eb7a907f4305839 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Mon, 22 Aug 2011 14:13:20 +0200 Subject: [PATCH 01/18] usb-host: start tracing support Add a bunch of trace points to usb-linux.c Drop a bunch of DPRINTK's in favor of the trace points. Also cleanup error reporting a bit while being at it. Signed-off-by: Gerd Hoffmann --- trace-events | 24 +++++++++++++++++ usb-linux.c | 73 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 72 insertions(+), 25 deletions(-) diff --git a/trace-events b/trace-events index 08ffedfdd1..e473aef829 100644 --- a/trace-events +++ b/trace-events @@ -240,6 +240,30 @@ usb_set_config(int addr, int config, int ret) "dev %d, config %d, ret %d" usb_clear_device_feature(int addr, int feature, int ret) "dev %d, feature %d, ret %d" usb_set_device_feature(int addr, int feature, int ret) "dev %d, feature %d, ret %d" +# usb-linux.c +usb_host_open_started(int bus, int addr) "dev %d:%d" +usb_host_open_success(int bus, int addr) "dev %d:%d" +usb_host_open_failure(int bus, int addr) "dev %d:%d" +usb_host_disconnect(int bus, int addr) "dev %d:%d" +usb_host_close(int bus, int addr) "dev %d:%d" +usb_host_set_address(int bus, int addr, int config) "dev %d:%d, address %d" +usb_host_set_config(int bus, int addr, int config) "dev %d:%d, config %d" +usb_host_set_interface(int bus, int addr, int interface, int alt) "dev %d:%d, interface %d, alt %d" +usb_host_claim_interfaces(int bus, int addr, int config, int nif) "dev %d:%d, config %d, nif %d" +usb_host_release_interfaces(int bus, int addr) "dev %d:%d" +usb_host_req_control(int bus, int addr, int req, int value, int index) "dev %d:%d, req 0x%x, value %d, index %d" +usb_host_req_data(int bus, int addr, int in, int ep, int size) "dev %d:%d, in %d, ep %d, size %d" +usb_host_req_complete(int bus, int addr, int status) "dev %d:%d, status %d" +usb_host_urb_submit(int bus, int addr, void *aurb, int length, int more) "dev %d:%d, aurb %p, length %d, more %d" +usb_host_urb_complete(int bus, int addr, void *aurb, int status, int length, int more) "dev %d:%d, aurb %p, status %d, length %d, more %d" +usb_host_ep_set_halt(int bus, int addr, int ep) "dev %d:%d, ep %d" +usb_host_ep_clear_halt(int bus, int addr, int ep) "dev %d:%d, ep %d" +usb_host_ep_start_iso(int bus, int addr, int ep) "dev %d:%d, ep %d" +usb_host_ep_stop_iso(int bus, int addr, int ep) "dev %d:%d, ep %d" +usb_host_reset(int bus, int addr) "dev %d:%d" +usb_host_auto_scan_enabled(void) +usb_host_auto_scan_disabled(void) + # hw/scsi-bus.c scsi_req_alloc(int target, int lun, int tag) "target %d lun %d tag %d" scsi_req_data(int target, int lun, int tag, int len) "target %d lun %d tag %d len %d" diff --git a/usb-linux.c b/usb-linux.c index 2e20f8e935..4e4df61c9f 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -34,6 +34,7 @@ #include "qemu-timer.h" #include "monitor.h" #include "sysemu.h" +#include "trace.h" #include #include @@ -165,11 +166,13 @@ static int is_halted(USBHostDevice *s, int ep) static void clear_halt(USBHostDevice *s, int ep) { + trace_usb_host_ep_clear_halt(s->bus_num, s->addr, ep); get_endp(s, ep)->halted = 0; } static void set_halt(USBHostDevice *s, int ep) { + trace_usb_host_ep_set_halt(s->bus_num, s->addr, ep); get_endp(s, ep)->halted = 1; } @@ -180,12 +183,15 @@ static int is_iso_started(USBHostDevice *s, int ep) static void clear_iso_started(USBHostDevice *s, int ep) { + trace_usb_host_ep_stop_iso(s->bus_num, s->addr, ep); get_endp(s, ep)->iso_started = 0; } static void set_iso_started(USBHostDevice *s, int ep) { struct endp_data *e = get_endp(s, ep); + + trace_usb_host_ep_start_iso(s->bus_num, s->addr, ep); if (!e->iso_started) { e->iso_started = 1; e->inflight = 0; @@ -285,8 +291,6 @@ static void async_free(AsyncURB *aurb) static void do_disconnect(USBHostDevice *s) { - printf("husb: device %d.%d disconnected\n", - s->bus_num, s->addr); usb_host_close(s); usb_host_auto_check(NULL); } @@ -309,11 +313,12 @@ static void async_complete(void *opaque) return; } if (errno == ENODEV && !s->closing) { + trace_usb_host_disconnect(s->bus_num, s->addr); do_disconnect(s); return; } - DPRINTF("husb: async. reap urb failed errno %d\n", errno); + perror("USBDEVFS_REAPURBNDELAY"); return; } @@ -337,6 +342,8 @@ static void async_complete(void *opaque) } p = aurb->packet; + trace_usb_host_urb_complete(s->bus_num, s->addr, aurb, aurb->urb.status, + aurb->urb.actual_length, aurb->more); if (p) { switch (aurb->urb.status) { @@ -355,8 +362,10 @@ static void async_complete(void *opaque) } if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) { + trace_usb_host_req_complete(s->bus_num, s->addr, p->result); usb_generic_async_ctrl_complete(&s->dev, p); } else if (!aurb->more) { + trace_usb_host_req_complete(s->bus_num, s->addr, p->result); usb_packet_complete(&s->dev, p); } } @@ -418,7 +427,7 @@ static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) } config_descr_len = dev->descr[i]; - printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration); + DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration); if (configuration < 0 || configuration == dev->descr[i + 5]) { configuration = dev->descr[i + 5]; @@ -457,17 +466,12 @@ static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) op = "USBDEVFS_CLAIMINTERFACE"; ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface); if (ret < 0) { - if (errno == EBUSY) { - printf("husb: update iface. device already grabbed\n"); - } else { - perror("husb: failed to claim interface"); - } goto fail; } } - printf("husb: %d interfaces claimed for configuration %d\n", - nb_interfaces, configuration); + trace_usb_host_claim_interfaces(dev->bus_num, dev->addr, + nb_interfaces, configuration); dev->ninterfaces = nb_interfaces; dev->configuration = configuration; @@ -485,16 +489,15 @@ static int usb_host_release_interfaces(USBHostDevice *s) { int ret, i; - DPRINTF("husb: releasing interfaces\n"); + trace_usb_host_release_interfaces(s->bus_num, s->addr); for (i = 0; i < s->ninterfaces; i++) { ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i); if (ret < 0) { - perror("husb: failed to release interface"); + perror("USBDEVFS_RELEASEINTERFACE"); return 0; } } - return 1; } @@ -502,7 +505,7 @@ static void usb_host_handle_reset(USBDevice *dev) { USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); - DPRINTF("husb: reset device %u.%u\n", s->bus_num, s->addr); + trace_usb_host_reset(s->bus_num, s->addr); ioctl(s->fd, USBDEVFS_RESET); @@ -564,7 +567,7 @@ static void usb_host_stop_n_free_iso(USBHostDevice *s, uint8_t ep) if (aurb[i].iso_frame_idx == -1) { ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]); if (ret < 0) { - printf("husb: discard isoc in urb failed errno %d\n", errno); + perror("USBDEVFS_DISCARDURB"); free = 0; continue; } @@ -680,7 +683,7 @@ static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in) if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) { ret = ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]); if (ret < 0) { - printf("husb error submitting iso urb %d: %d\n", i, errno); + perror("USBDEVFS_SUBMITURB"); if (!in || len == 0) { switch(errno) { case ETIMEDOUT: @@ -711,7 +714,12 @@ static int usb_host_handle_data(USBDevice *dev, USBPacket *p) uint8_t *pbuf; uint8_t ep; + trace_usb_host_req_data(s->bus_num, s->addr, + p->pid == USB_TOKEN_IN, + p->devep, p->iov.size); + if (!is_valid(s, p->devep)) { + trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK); return USB_RET_NAK; } @@ -724,8 +732,8 @@ static int usb_host_handle_data(USBDevice *dev, USBPacket *p) if (is_halted(s, p->devep)) { ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &ep); if (ret < 0) { - DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n", - ep, errno); + perror("USBDEVFS_CLEAR_HALT"); + trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK); return USB_RET_NAK; } clear_halt(s, p->devep); @@ -767,20 +775,24 @@ static int usb_host_handle_data(USBDevice *dev, USBPacket *p) aurb->more = 1; } + trace_usb_host_urb_submit(s->bus_num, s->addr, aurb, + urb->buffer_length, aurb->more); ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n", urb->endpoint, urb->buffer_length, aurb->more, p, aurb); if (ret < 0) { - DPRINTF("husb: submit failed. errno %d\n", errno); + perror("USBDEVFS_SUBMITURB"); async_free(aurb); switch(errno) { case ETIMEDOUT: + trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK); return USB_RET_NAK; case EPIPE: default: + trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_STALL); return USB_RET_STALL; } } @@ -800,13 +812,15 @@ static int ctrl_error(void) static int usb_host_set_address(USBHostDevice *s, int addr) { - DPRINTF("husb: ctrl set addr %u\n", addr); + trace_usb_host_set_address(s->bus_num, s->addr, addr); s->dev.addr = addr; return 0; } static int usb_host_set_config(USBHostDevice *s, int config) { + trace_usb_host_set_config(s->bus_num, s->addr, config); + usb_host_release_interfaces(s); int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config); @@ -825,6 +839,8 @@ static int usb_host_set_interface(USBHostDevice *s, int iface, int alt) struct usbdevfs_setinterface si; int i, ret; + trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt); + for (i = 1; i <= MAX_ENDPOINTS; i++) { if (is_isoc(s, i)) { usb_host_stop_n_free_iso(s, i); @@ -859,8 +875,7 @@ static int usb_host_handle_control(USBDevice *dev, USBPacket *p, */ /* Note request is (bRequestType << 8) | bRequest */ - DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n", - request >> 8, request & 0xff, value, index, length); + trace_usb_host_req_control(s->bus_num, s->addr, request, value, index); switch (request) { case DeviceOutRequest | USB_REQ_SET_ADDRESS: @@ -900,6 +915,8 @@ static int usb_host_handle_control(USBDevice *dev, USBPacket *p, urb->usercontext = s; + trace_usb_host_urb_submit(s->bus_num, s->addr, aurb, + urb->buffer_length, aurb->more); ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb); @@ -1140,10 +1157,11 @@ static int usb_host_open(USBHostDevice *dev, int bus_num, int fd = -1, ret; char buf[1024]; + trace_usb_host_open_started(bus_num, addr); + if (dev->fd != -1) { goto fail; } - printf("husb: open device %d.%d\n", bus_num, addr); if (!usb_host_device_path) { perror("husb: USB Host Device Path not set"); @@ -1218,7 +1236,7 @@ static int usb_host_open(USBHostDevice *dev, int bus_num, dev->dev.speedmask |= USB_SPEED_MASK_FULL; } - printf("husb: grabbed usb device %d.%d\n", bus_num, addr); + trace_usb_host_open_success(bus_num, addr); if (!prod_name || prod_name[0] == '\0') { snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc), @@ -1239,6 +1257,7 @@ static int usb_host_open(USBHostDevice *dev, int bus_num, return 0; fail: + trace_usb_host_open_failure(bus_num, addr); if (dev->fd != -1) { close(dev->fd); dev->fd = -1; @@ -1254,6 +1273,8 @@ static int usb_host_close(USBHostDevice *dev) return -1; } + trace_usb_host_close(dev->bus_num, dev->addr); + qemu_set_fd_handler(dev->fd, NULL, NULL, NULL); dev->closing = 1; for (i = 1; i <= MAX_ENDPOINTS; i++) { @@ -1776,6 +1797,7 @@ static void usb_host_auto_check(void *unused) /* nothing to watch */ if (usb_auto_timer) { qemu_del_timer(usb_auto_timer); + trace_usb_host_auto_scan_disabled(); } return; } @@ -1785,6 +1807,7 @@ static void usb_host_auto_check(void *unused) if (!usb_auto_timer) { return; } + trace_usb_host_auto_scan_enabled(); } qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000); } From 40197c359b1f06cbaae5460336bca241c35f266e Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Mon, 22 Aug 2011 14:18:21 +0200 Subject: [PATCH 02/18] usb-host: reapurb error report fix Don't report errors on devices which are in disconnected and closing state. --- usb-linux.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index 4e4df61c9f..12e8772a4f 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -312,9 +312,11 @@ static void async_complete(void *opaque) } return; } - if (errno == ENODEV && !s->closing) { - trace_usb_host_disconnect(s->bus_num, s->addr); - do_disconnect(s); + if (errno == ENODEV) { + if (!s->closing) { + trace_usb_host_disconnect(s->bus_num, s->addr); + do_disconnect(s); + } return; } From 9b87e19bc7adea69bd55ae9c64b8a7fb81214726 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Wed, 24 Aug 2011 10:55:40 +0200 Subject: [PATCH 03/18] usb-host: fix halted endpoints Two fixes for the price of one ;) First, reinitialize the endpoint table after device reset. This is needed anyway as the reset might have switched interfaces. It also clears the endpoint halted state. Second the CLEAR_HALT ioctl wants a unsigned int passed in as argument, not uint8_t. This gets my usb sd card reader (sandisk micromate) going. Signed-off-by: Gerd Hoffmann --- usb-linux.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index 12e8772a4f..344af22405 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -143,6 +143,7 @@ static int parse_filter(const char *spec, struct USBAutoFilter *f); static void usb_host_auto_check(void *unused); static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name); +static int usb_linux_update_endp_table(USBHostDevice *s); static struct endp_data *get_endp(USBHostDevice *s, int ep) { @@ -512,6 +513,7 @@ static void usb_host_handle_reset(USBDevice *dev) ioctl(s->fd, USBDEVFS_RESET); usb_host_claim_interfaces(s, s->configuration); + usb_linux_update_endp_table(s); } static void usb_host_handle_destroy(USBDevice *dev) @@ -523,8 +525,6 @@ static void usb_host_handle_destroy(USBDevice *dev) qemu_remove_exit_notifier(&s->exit); } -static int usb_linux_update_endp_table(USBHostDevice *s); - /* iso data is special, we need to keep enough urbs in flight to make sure that the controller never runs out of them, otherwise the device will likely suffer a buffer underrun / overrun. */ @@ -732,7 +732,8 @@ static int usb_host_handle_data(USBDevice *dev, USBPacket *p) } if (is_halted(s, p->devep)) { - ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &ep); + unsigned int arg = ep; + ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &arg); if (ret < 0) { perror("USBDEVFS_CLEAR_HALT"); trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK); From 3ee886c5ba77a65d6b2c2a372a091d6796ed502b Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Wed, 24 Aug 2011 13:45:06 +0200 Subject: [PATCH 04/18] usb-host: limit open retries Limit the number of times qemu tries to open host devices to three. Reset error counter when the device goes away, after un-plugging and re-plugging the device qemu will try again three times. Signed-off-by: Gerd Hoffmann --- usb-linux.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/usb-linux.c b/usb-linux.c index 344af22405..36d25d7328 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -132,6 +132,7 @@ typedef struct USBHostDevice { int addr; char port[MAX_PORTLEN]; struct USBAutoFilter match; + int seen, errcount; QTAILQ_ENTRY(USBHostDevice) next; } USBHostDevice; @@ -1769,6 +1770,10 @@ static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port, continue; } /* We got a match */ + s->seen++; + if (s->errcount >= 3) { + return 0; + } /* Already attached ? */ if (s->fd != -1) { @@ -1776,7 +1781,9 @@ static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port, } DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr); - usb_host_open(s, bus_num, addr, port, product_name, speed); + if (usb_host_open(s, bus_num, addr, port, product_name, speed) < 0) { + s->errcount++; + } break; } @@ -1794,6 +1801,10 @@ static void usb_host_auto_check(void *unused) if (s->fd == -1) { unconnected++; } + if (s->seen == 0) { + s->errcount = 0; + } + s->seen = 0; } if (unconnected == 0) { From eb7700bb999544151d6e4350362b9b6d9d20abe8 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Wed, 24 Aug 2011 14:45:07 +0200 Subject: [PATCH 05/18] usb-host: fix configuration tracking. It is perfectly fine to leave the usb device in unconfigured state (USBHostDevice->configuration == 0). Just do that and wait for the guest to explicitly set a configuration. This is closer to what real hardware does and it also simplifies the device initialization. There is no need to figure how the device is configured on the host. Signed-off-by: Gerd Hoffmann --- usb-linux.c | 82 +++++++++++++---------------------------------------- 1 file changed, 19 insertions(+), 63 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index 36d25d7328..a903023847 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -407,8 +407,11 @@ static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) int interface, nb_interfaces; int ret, i; - if (configuration == 0) /* address state - ignore */ + if (configuration == 0) { /* address state - ignore */ + dev->ninterfaces = 0; + dev->configuration = 0; return 1; + } DPRINTF("husb: claiming interfaces. config %d\n", configuration); @@ -433,7 +436,7 @@ static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration); - if (configuration < 0 || configuration == dev->descr[i + 5]) { + if (configuration == dev->descr[i + 5]) { configuration = dev->descr[i + 5]; break; } @@ -513,7 +516,7 @@ static void usb_host_handle_reset(USBDevice *dev) ioctl(s->fd, USBDEVFS_RESET); - usb_host_claim_interfaces(s, s->configuration); + usb_host_claim_interfaces(s, 0); usb_linux_update_endp_table(s); } @@ -835,6 +838,7 @@ static int usb_host_set_config(USBHostDevice *s, int config) return ctrl_error(); } usb_host_claim_interfaces(s, config); + usb_linux_update_endp_table(s); return 0; } @@ -941,51 +945,6 @@ static int usb_host_handle_control(USBDevice *dev, USBPacket *p, return USB_RET_ASYNC; } -static int usb_linux_get_configuration(USBHostDevice *s) -{ - uint8_t configuration; - struct usb_ctrltransfer ct; - int ret; - - if (usb_fs_type == USB_FS_SYS) { - char device_name[32], line[1024]; - int configuration; - - sprintf(device_name, "%d-%s", s->bus_num, s->port); - - if (!usb_host_read_file(line, sizeof(line), "bConfigurationValue", - device_name)) { - goto usbdevfs; - } - if (sscanf(line, "%d", &configuration) != 1) { - goto usbdevfs; - } - return configuration; - } - -usbdevfs: - ct.bRequestType = USB_DIR_IN; - ct.bRequest = USB_REQ_GET_CONFIGURATION; - ct.wValue = 0; - ct.wIndex = 0; - ct.wLength = 1; - ct.data = &configuration; - ct.timeout = 50; - - ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); - if (ret < 0) { - perror("usb_linux_get_configuration"); - return -1; - } - - /* in address state */ - if (configuration == 0) { - return -1; - } - - return configuration; -} - static uint8_t usb_linux_get_alt_setting(USBHostDevice *s, uint8_t configuration, uint8_t interface) { @@ -1031,16 +990,16 @@ usbdevfs: static int usb_linux_update_endp_table(USBHostDevice *s) { uint8_t *descriptors; - uint8_t devep, type, configuration, alt_interface; + uint8_t devep, type, alt_interface; int interface, length, i; for (i = 0; i < MAX_ENDPOINTS; i++) s->endp_table[i].type = INVALID_EP_TYPE; - i = usb_linux_get_configuration(s); - if (i < 0) - return 1; - configuration = i; + if (s->configuration == 0) { + /* not configured yet -- leave all endpoints disabled */ + return 0; + } /* get the desired configuration, interface, and endpoint descriptors * from device description */ @@ -1049,8 +1008,9 @@ static int usb_linux_update_endp_table(USBHostDevice *s) i = 0; if (descriptors[i + 1] != USB_DT_CONFIG || - descriptors[i + 5] != configuration) { - DPRINTF("invalid descriptor data - configuration\n"); + descriptors[i + 5] != s->configuration) { + fprintf(stderr, "invalid descriptor data - configuration %d\n", + s->configuration); return 1; } i += descriptors[i]; @@ -1064,7 +1024,8 @@ static int usb_linux_update_endp_table(USBHostDevice *s) } interface = descriptors[i + 2]; - alt_interface = usb_linux_get_alt_setting(s, configuration, interface); + alt_interface = usb_linux_get_alt_setting(s, s->configuration, + interface); /* the current interface descriptor is the active interface * and has endpoints */ @@ -1204,13 +1165,8 @@ static int usb_host_open(USBHostDevice *dev, int bus_num, #endif - /* - * Initial configuration is -1 which makes us claim first - * available config. We used to start with 1, which does not - * always work. I've seen devices where first config starts - * with 2. - */ - if (!usb_host_claim_interfaces(dev, -1)) { + /* start unconfigured -- we'll wait for the guest to set a configuration */ + if (!usb_host_claim_interfaces(dev, 0)) { goto fail; } From 9516bb4772815f3bbf94d4779905681a7d254850 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Wed, 24 Aug 2011 13:34:17 +0200 Subject: [PATCH 06/18] usb-host: claim port When configured to pass through a specific host port (using hostbus and hostport properties), try to claim the port if supported by the kernel. That will avoid any kernel drivers binding to devices plugged into that port. It will not stop any userspace apps (such as usb_modeswitch) access the device via usbfs though. Signed-off-by: Gerd Hoffmann --- trace-events | 1 + usb-linux.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/trace-events b/trace-events index e473aef829..a6e0cb4c48 100644 --- a/trace-events +++ b/trace-events @@ -263,6 +263,7 @@ usb_host_ep_stop_iso(int bus, int addr, int ep) "dev %d:%d, ep %d" usb_host_reset(int bus, int addr) "dev %d:%d" usb_host_auto_scan_enabled(void) usb_host_auto_scan_disabled(void) +usb_host_claim_port(int bus, int hub, int port) "bus %d, hub addr %d, port %d" # hw/scsi-bus.c scsi_req_alloc(int target, int lun, int tag) "target %d lun %d tag %d" diff --git a/usb-linux.c b/usb-linux.c index a903023847..ce0eaddefb 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -115,6 +115,7 @@ struct USBAutoFilter { typedef struct USBHostDevice { USBDevice dev; int fd; + int hub_fd; uint8_t descr[8192]; int descr_len; @@ -525,6 +526,9 @@ static void usb_host_handle_destroy(USBDevice *dev) USBHostDevice *s = (USBHostDevice *)dev; usb_host_close(s); + if (s->hub_fd != -1) { + close(s->hub_fd); + } QTAILQ_REMOVE(&hostdevs, s, next); qemu_remove_exit_notifier(&s->exit); } @@ -1266,10 +1270,63 @@ static int usb_host_initfn(USBDevice *dev) dev->auto_attach = 0; s->fd = -1; + s->hub_fd = -1; + QTAILQ_INSERT_TAIL(&hostdevs, s, next); s->exit.notify = usb_host_exit_notifier; qemu_add_exit_notifier(&s->exit); usb_host_auto_check(NULL); + +#ifdef USBDEVFS_CLAIM_PORT + if (s->match.bus_num != 0 && s->match.port != NULL) { + char *h, hub_name[64], line[1024]; + int hub_addr, portnr, ret; + + snprintf(hub_name, sizeof(hub_name), "%d-%s", + s->match.bus_num, s->match.port); + + /* try strip off last ".$portnr" to get hub */ + h = strrchr(hub_name, '.'); + if (h != NULL) { + portnr = atoi(h+1); + *h = '\0'; + } else { + /* no dot in there -> it is the root hub */ + snprintf(hub_name, sizeof(hub_name), "usb%d", + s->match.bus_num); + portnr = atoi(s->match.port); + } + + if (!usb_host_read_file(line, sizeof(line), "devnum", + hub_name)) { + goto out; + } + if (sscanf(line, "%d", &hub_addr) != 1) { + goto out; + } + + if (!usb_host_device_path) { + goto out; + } + snprintf(line, sizeof(line), "%s/%03d/%03d", + usb_host_device_path, s->match.bus_num, hub_addr); + s->hub_fd = open(line, O_RDWR | O_NONBLOCK); + if (s->hub_fd < 0) { + goto out; + } + + ret = ioctl(s->hub_fd, USBDEVFS_CLAIM_PORT, &portnr); + if (ret < 0) { + close(s->hub_fd); + s->hub_fd = -1; + goto out; + } + + trace_usb_host_claim_port(s->match.bus_num, hub_addr, portnr); + } +out: +#endif + return 0; } From c0e5750bc35366b7231693d9ae433bea9a485ef0 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Fri, 26 Aug 2011 16:27:41 +0200 Subject: [PATCH 07/18] usb-host: endpoint table fixup USB Devices can have up to 15 IN and 15 OUT endpoints, not 15 endpoints total. Move from one array to two arrays (one IN, one OUT) to maintain the endpoint state. --- usb-linux.c | 179 +++++++++++++++++++++++++++++----------------------- 1 file changed, 101 insertions(+), 78 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index ce0eaddefb..6490582232 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -125,7 +125,8 @@ typedef struct USBHostDevice { uint32_t iso_urb_count; Notifier exit; - struct endp_data endp_table[MAX_ENDPOINTS]; + struct endp_data ep_in[MAX_ENDPOINTS]; + struct endp_data ep_out[MAX_ENDPOINTS]; QLIST_HEAD(, AsyncURB) aurbs; /* Host side address */ @@ -147,52 +148,57 @@ static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name); static int usb_linux_update_endp_table(USBHostDevice *s); -static struct endp_data *get_endp(USBHostDevice *s, int ep) +static struct endp_data *get_endp(USBHostDevice *s, int pid, int ep) { - return s->endp_table + ep - 1; + struct endp_data *eps = pid == USB_TOKEN_IN ? s->ep_in : s->ep_out; + assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT); + assert(ep > 0 && ep <= MAX_ENDPOINTS); + return eps + ep - 1; } -static int is_isoc(USBHostDevice *s, int ep) +static int is_isoc(USBHostDevice *s, int pid, int ep) { - return get_endp(s, ep)->type == USBDEVFS_URB_TYPE_ISO; + return get_endp(s, pid, ep)->type == USBDEVFS_URB_TYPE_ISO; } -static int is_valid(USBHostDevice *s, int ep) +static int is_valid(USBHostDevice *s, int pid, int ep) { - return get_endp(s, ep)->type != INVALID_EP_TYPE; + return get_endp(s, pid, ep)->type != INVALID_EP_TYPE; } -static int is_halted(USBHostDevice *s, int ep) +static int is_halted(USBHostDevice *s, int pid, int ep) { - return get_endp(s, ep)->halted; + return get_endp(s, pid, ep)->halted; } -static void clear_halt(USBHostDevice *s, int ep) +static void clear_halt(USBHostDevice *s, int pid, int ep) { trace_usb_host_ep_clear_halt(s->bus_num, s->addr, ep); - get_endp(s, ep)->halted = 0; + get_endp(s, pid, ep)->halted = 0; } -static void set_halt(USBHostDevice *s, int ep) +static void set_halt(USBHostDevice *s, int pid, int ep) { - trace_usb_host_ep_set_halt(s->bus_num, s->addr, ep); - get_endp(s, ep)->halted = 1; + if (ep != 0) { + trace_usb_host_ep_set_halt(s->bus_num, s->addr, ep); + get_endp(s, pid, ep)->halted = 1; + } } -static int is_iso_started(USBHostDevice *s, int ep) +static int is_iso_started(USBHostDevice *s, int pid, int ep) { - return get_endp(s, ep)->iso_started; + return get_endp(s, pid, ep)->iso_started; } -static void clear_iso_started(USBHostDevice *s, int ep) +static void clear_iso_started(USBHostDevice *s, int pid, int ep) { trace_usb_host_ep_stop_iso(s->bus_num, s->addr, ep); - get_endp(s, ep)->iso_started = 0; + get_endp(s, pid, ep)->iso_started = 0; } -static void set_iso_started(USBHostDevice *s, int ep) +static void set_iso_started(USBHostDevice *s, int pid, int ep) { - struct endp_data *e = get_endp(s, ep); + struct endp_data *e = get_endp(s, pid, ep); trace_usb_host_ep_start_iso(s->bus_num, s->addr, ep); if (!e->iso_started) { @@ -201,45 +207,46 @@ static void set_iso_started(USBHostDevice *s, int ep) } } -static int change_iso_inflight(USBHostDevice *s, int ep, int value) +static int change_iso_inflight(USBHostDevice *s, int pid, int ep, int value) { - struct endp_data *e = get_endp(s, ep); + struct endp_data *e = get_endp(s, pid, ep); e->inflight += value; return e->inflight; } -static void set_iso_urb(USBHostDevice *s, int ep, AsyncURB *iso_urb) +static void set_iso_urb(USBHostDevice *s, int pid, int ep, AsyncURB *iso_urb) { - get_endp(s, ep)->iso_urb = iso_urb; + get_endp(s, pid, ep)->iso_urb = iso_urb; } -static AsyncURB *get_iso_urb(USBHostDevice *s, int ep) +static AsyncURB *get_iso_urb(USBHostDevice *s, int pid, int ep) { - return get_endp(s, ep)->iso_urb; + return get_endp(s, pid, ep)->iso_urb; } -static void set_iso_urb_idx(USBHostDevice *s, int ep, int i) +static void set_iso_urb_idx(USBHostDevice *s, int pid, int ep, int i) { - get_endp(s, ep)->iso_urb_idx = i; + get_endp(s, pid, ep)->iso_urb_idx = i; } -static int get_iso_urb_idx(USBHostDevice *s, int ep) +static int get_iso_urb_idx(USBHostDevice *s, int pid, int ep) { - return get_endp(s, ep)->iso_urb_idx; + return get_endp(s, pid, ep)->iso_urb_idx; } -static void set_iso_buffer_used(USBHostDevice *s, int ep, int i) +static void set_iso_buffer_used(USBHostDevice *s, int pid, int ep, int i) { - get_endp(s, ep)->iso_buffer_used = i; + get_endp(s, pid, ep)->iso_buffer_used = i; } -static int get_iso_buffer_used(USBHostDevice *s, int ep) +static int get_iso_buffer_used(USBHostDevice *s, int pid, int ep) { - return get_endp(s, ep)->iso_buffer_used; + return get_endp(s, pid, ep)->iso_buffer_used; } -static void set_max_packet_size(USBHostDevice *s, int ep, uint8_t *descriptor) +static void set_max_packet_size(USBHostDevice *s, int pid, int ep, + uint8_t *descriptor) { int raw = descriptor[4] + (descriptor[5] << 8); int size, microframes; @@ -250,12 +257,12 @@ static void set_max_packet_size(USBHostDevice *s, int ep, uint8_t *descriptor) case 2: microframes = 3; break; default: microframes = 1; break; } - get_endp(s, ep)->max_packet_size = size * microframes; + get_endp(s, pid, ep)->max_packet_size = size * microframes; } -static int get_max_packet_size(USBHostDevice *s, int ep) +static int get_max_packet_size(USBHostDevice *s, int pid, int ep) { - return get_endp(s, ep)->max_packet_size; + return get_endp(s, pid, ep)->max_packet_size; } /* @@ -334,13 +341,16 @@ static void async_complete(void *opaque) anything else (it is handled further in usb_host_handle_iso_data) */ if (aurb->iso_frame_idx == -1) { int inflight; + int pid = (aurb->urb.endpoint & USB_DIR_IN) ? + USB_TOKEN_IN : USB_TOKEN_OUT; + int ep = aurb->urb.endpoint & 0xf; if (aurb->urb.status == -EPIPE) { - set_halt(s, aurb->urb.endpoint & 0xf); + set_halt(s, pid, ep); } aurb->iso_frame_idx = 0; urbs++; - inflight = change_iso_inflight(s, aurb->urb.endpoint & 0xf, -1); - if (inflight == 0 && is_iso_started(s, aurb->urb.endpoint & 0xf)) { + inflight = change_iso_inflight(s, pid, ep, -1); + if (inflight == 0 && is_iso_started(s, pid, ep)) { fprintf(stderr, "husb: out of buffers for iso stream\n"); } continue; @@ -357,7 +367,7 @@ static void async_complete(void *opaque) break; case -EPIPE: - set_halt(s, p->devep); + set_halt(s, p->pid, p->devep); p->result = USB_RET_STALL; break; @@ -536,10 +546,10 @@ static void usb_host_handle_destroy(USBDevice *dev) /* iso data is special, we need to keep enough urbs in flight to make sure that the controller never runs out of them, otherwise the device will likely suffer a buffer underrun / overrun. */ -static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, uint8_t ep, int in) +static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, int pid, uint8_t ep) { AsyncURB *aurb; - int i, j, len = get_max_packet_size(s, ep); + int i, j, len = get_max_packet_size(s, pid, ep); aurb = g_malloc0(s->iso_urb_count * sizeof(*aurb)); for (i = 0; i < s->iso_urb_count; i++) { @@ -551,23 +561,23 @@ static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, uint8_t ep, int in) aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB; for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++) aurb[i].urb.iso_frame_desc[j].length = len; - if (in) { + if (pid == USB_TOKEN_IN) { aurb[i].urb.endpoint |= 0x80; /* Mark as fully consumed (idle) */ aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB; } } - set_iso_urb(s, ep, aurb); + set_iso_urb(s, pid, ep, aurb); return aurb; } -static void usb_host_stop_n_free_iso(USBHostDevice *s, uint8_t ep) +static void usb_host_stop_n_free_iso(USBHostDevice *s, int pid, uint8_t ep) { AsyncURB *aurb; int i, ret, killed = 0, free = 1; - aurb = get_iso_urb(s, ep); + aurb = get_iso_urb(s, pid, ep); if (!aurb) { return; } @@ -598,9 +608,9 @@ static void usb_host_stop_n_free_iso(USBHostDevice *s, uint8_t ep) g_free(aurb); else printf("husb: leaking iso urbs because of discard failure\n"); - set_iso_urb(s, ep, NULL); - set_iso_urb_idx(s, ep, 0); - clear_iso_started(s, ep); + set_iso_urb(s, pid, ep, NULL); + set_iso_urb_idx(s, pid, ep, 0); + clear_iso_started(s, pid, ep); } static int urb_status_to_usb_ret(int status) @@ -619,16 +629,16 @@ static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in) int i, j, ret, max_packet_size, offset, len = 0; uint8_t *buf; - max_packet_size = get_max_packet_size(s, p->devep); + max_packet_size = get_max_packet_size(s, p->pid, p->devep); if (max_packet_size == 0) return USB_RET_NAK; - aurb = get_iso_urb(s, p->devep); + aurb = get_iso_urb(s, p->pid, p->devep); if (!aurb) { - aurb = usb_host_alloc_iso(s, p->devep, in); + aurb = usb_host_alloc_iso(s, p->pid, p->devep); } - i = get_iso_urb_idx(s, p->devep); + i = get_iso_urb_idx(s, p->pid, p->devep); j = aurb[i].iso_frame_idx; if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) { if (in) { @@ -655,7 +665,7 @@ static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in) } } else { len = p->iov.size; - offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->devep); + offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->pid, p->devep); /* Check the frame fits */ if (len > max_packet_size) { @@ -667,27 +677,27 @@ static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in) usb_packet_copy(p, aurb[i].urb.buffer + offset, len); aurb[i].urb.iso_frame_desc[j].length = len; offset += len; - set_iso_buffer_used(s, p->devep, offset); + set_iso_buffer_used(s, p->pid, p->devep, offset); /* Start the stream once we have buffered enough data */ - if (!is_iso_started(s, p->devep) && i == 1 && j == 8) { - set_iso_started(s, p->devep); + if (!is_iso_started(s, p->pid, p->devep) && i == 1 && j == 8) { + set_iso_started(s, p->pid, p->devep); } } aurb[i].iso_frame_idx++; if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) { i = (i + 1) % s->iso_urb_count; - set_iso_urb_idx(s, p->devep, i); + set_iso_urb_idx(s, p->pid, p->devep, i); } } else { if (in) { - set_iso_started(s, p->devep); + set_iso_started(s, p->pid, p->devep); } else { DPRINTF("hubs: iso out error no free buffer, dropping packet\n"); } } - if (is_iso_started(s, p->devep)) { + if (is_iso_started(s, p->pid, p->devep)) { /* (Re)-submit all fully consumed / filled urbs */ for (i = 0; i < s->iso_urb_count; i++) { if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) { @@ -707,7 +717,7 @@ static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in) break; } aurb[i].iso_frame_idx = -1; - change_iso_inflight(s, p->devep, +1); + change_iso_inflight(s, p->pid, p->devep, 1); } } } @@ -728,7 +738,7 @@ static int usb_host_handle_data(USBDevice *dev, USBPacket *p) p->pid == USB_TOKEN_IN, p->devep, p->iov.size); - if (!is_valid(s, p->devep)) { + if (!is_valid(s, p->pid, p->devep)) { trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK); return USB_RET_NAK; } @@ -739,7 +749,7 @@ static int usb_host_handle_data(USBDevice *dev, USBPacket *p) ep = p->devep; } - if (is_halted(s, p->devep)) { + if (is_halted(s, p->pid, p->devep)) { unsigned int arg = ep; ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &arg); if (ret < 0) { @@ -747,10 +757,10 @@ static int usb_host_handle_data(USBDevice *dev, USBPacket *p) trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK); return USB_RET_NAK; } - clear_halt(s, p->devep); + clear_halt(s, p->pid, p->devep); } - if (is_isoc(s, p->devep)) { + if (is_isoc(s, p->pid, p->devep)) { return usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN); } @@ -854,8 +864,11 @@ static int usb_host_set_interface(USBHostDevice *s, int iface, int alt) trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt); for (i = 1; i <= MAX_ENDPOINTS; i++) { - if (is_isoc(s, i)) { - usb_host_stop_n_free_iso(s, i); + if (is_isoc(s, USB_TOKEN_IN, i)) { + usb_host_stop_n_free_iso(s, USB_TOKEN_IN, i); + } + if (is_isoc(s, USB_TOKEN_OUT, i)) { + usb_host_stop_n_free_iso(s, USB_TOKEN_OUT, i); } } @@ -995,10 +1008,13 @@ static int usb_linux_update_endp_table(USBHostDevice *s) { uint8_t *descriptors; uint8_t devep, type, alt_interface; - int interface, length, i; + int interface, length, i, ep, pid; + struct endp_data *epd; - for (i = 0; i < MAX_ENDPOINTS; i++) - s->endp_table[i].type = INVALID_EP_TYPE; + for (i = 0; i < MAX_ENDPOINTS; i++) { + s->ep_in[i].type = INVALID_EP_TYPE; + s->ep_out[i].type = INVALID_EP_TYPE; + } if (s->configuration == 0) { /* not configured yet -- leave all endpoints disabled */ @@ -1052,7 +1068,9 @@ static int usb_linux_update_endp_table(USBHostDevice *s) } devep = descriptors[i + 2]; - if ((devep & 0x0f) == 0) { + pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT; + ep = devep & 0xf; + if (ep == 0) { fprintf(stderr, "usb-linux: invalid ep descriptor, ep == 0\n"); return 1; } @@ -1063,7 +1081,7 @@ static int usb_linux_update_endp_table(USBHostDevice *s) break; case 0x01: type = USBDEVFS_URB_TYPE_ISO; - set_max_packet_size(s, (devep & 0xf), descriptors + i); + set_max_packet_size(s, pid, ep, descriptors + i); break; case 0x02: type = USBDEVFS_URB_TYPE_BULK; @@ -1075,8 +1093,10 @@ static int usb_linux_update_endp_table(USBHostDevice *s) DPRINTF("usb_host: malformed endpoint type\n"); type = USBDEVFS_URB_TYPE_BULK; } - s->endp_table[(devep & 0xf) - 1].type = type; - s->endp_table[(devep & 0xf) - 1].halted = 0; + epd = get_endp(s, pid, ep); + assert(epd->type == INVALID_EP_TYPE); + epd->type = type; + epd->halted = 0; i += descriptors[i]; } @@ -1242,8 +1262,11 @@ static int usb_host_close(USBHostDevice *dev) qemu_set_fd_handler(dev->fd, NULL, NULL, NULL); dev->closing = 1; for (i = 1; i <= MAX_ENDPOINTS; i++) { - if (is_isoc(dev, i)) { - usb_host_stop_n_free_iso(dev, i); + if (is_isoc(dev, USB_TOKEN_IN, i)) { + usb_host_stop_n_free_iso(dev, USB_TOKEN_IN, i); + } + if (is_isoc(dev, USB_TOKEN_OUT, i)) { + usb_host_stop_n_free_iso(dev, USB_TOKEN_OUT, i); } } async_complete(dev); From 2fe80192ba82fe298fe656c80d7e5981929393a6 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Fri, 26 Aug 2011 14:13:48 +0200 Subject: [PATCH 08/18] usb-ehci: handle siTDs This patch adds code to do minimal siTD handling, which is basically just following the next pointer. This is good enougth to handle the inactive siTDs used by FreeBSD. Active siTDs are skipped too as we don't have split transfer support in qemu, additionally a warning is printed. Signed-off-by: Gerd Hoffmann --- hw/usb-ehci.c | 43 ++++++++++++++++++++++++++++++++++++++++++- trace-events | 1 + 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/hw/usb-ehci.c b/hw/usb-ehci.c index 47a7fb9de4..d8ef0cb422 100644 --- a/hw/usb-ehci.c +++ b/hw/usb-ehci.c @@ -149,6 +149,7 @@ typedef enum { EST_FETCHENTRY, EST_FETCHQH, EST_FETCHITD, + EST_FETCHSITD, EST_ADVANCEQUEUE, EST_FETCHQTD, EST_EXECUTE, @@ -646,6 +647,13 @@ static void ehci_trace_itd(EHCIState *s, target_phys_addr_t addr, EHCIitd *itd) get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR)); } +static void ehci_trace_sitd(EHCIState *s, target_phys_addr_t addr, + EHCIsitd *sitd) +{ + trace_usb_ehci_sitd(addr, sitd->next, + (bool)(sitd->results & SITD_RESULTS_ACTIVE)); +} + /* queue management */ static EHCIQueue *ehci_alloc_queue(EHCIState *ehci, int async) @@ -1584,8 +1592,13 @@ static int ehci_state_fetchentry(EHCIState *ehci, int async) again = 1; break; + case NLPTR_TYPE_STITD: + ehci_set_state(ehci, async, EST_FETCHSITD); + again = 1; + break; + default: - // TODO: handle siTD and FSTN types + /* TODO: handle FSTN type */ fprintf(stderr, "FETCHENTRY: entry at %X is of type %d " "which is not supported yet\n", entry, NLPTR_TYPE_GET(entry)); return -1; @@ -1701,6 +1714,30 @@ static int ehci_state_fetchitd(EHCIState *ehci, int async) return 1; } +static int ehci_state_fetchsitd(EHCIState *ehci, int async) +{ + uint32_t entry; + EHCIsitd sitd; + + assert(!async); + entry = ehci_get_fetch_addr(ehci, async); + + get_dwords(NLPTR_GET(entry), (uint32_t *)&sitd, + sizeof(EHCIsitd) >> 2); + ehci_trace_sitd(ehci, entry, &sitd); + + if (!(sitd.results & SITD_RESULTS_ACTIVE)) { + /* siTD is not active, nothing to do */; + } else { + /* TODO: split transfers are not implemented */ + fprintf(stderr, "WARNING: Skipping active siTD\n"); + } + + ehci_set_fetch_addr(ehci, async, sitd.next); + ehci_set_state(ehci, async, EST_FETCHENTRY); + return 1; +} + /* Section 4.10.2 - paragraph 3 */ static int ehci_state_advqueue(EHCIQueue *q, int async) { @@ -1976,6 +2013,10 @@ static void ehci_advance_state(EHCIState *ehci, again = ehci_state_fetchitd(ehci, async); break; + case EST_FETCHSITD: + again = ehci_state_fetchsitd(ehci, async); + break; + case EST_ADVANCEQUEUE: again = ehci_state_advqueue(q, async); break; diff --git a/trace-events b/trace-events index a6e0cb4c48..5936192289 100644 --- a/trace-events +++ b/trace-events @@ -223,6 +223,7 @@ usb_ehci_qtd_ptrs(void *q, uint32_t addr, uint32_t nxt, uint32_t altnext) "q %p usb_ehci_qtd_fields(uint32_t addr, int tbytes, int cpage, int cerr, int pid) "QTD @ %08x - tbytes %d, cpage %d, cerr %d, pid %d" usb_ehci_qtd_bits(uint32_t addr, int ioc, int active, int halt, int babble, int xacterr) "QTD @ %08x - ioc %d, active %d, halt %d, babble %d, xacterr %d" usb_ehci_itd(uint32_t addr, uint32_t nxt, uint32_t mplen, uint32_t mult, uint32_t ep, uint32_t devaddr) "ITD @ %08x: next %08x - mplen %d, mult %d, ep %d, dev %d" +usb_ehci_sitd(uint32_t addr, uint32_t nxt, uint32_t active) "ITD @ %08x: next %08x - active %d" usb_ehci_port_attach(uint32_t port, const char *device) "attach port #%d - %s" usb_ehci_port_detach(uint32_t port) "detach port #%d" usb_ehci_port_reset(uint32_t port, int enable) "reset port #%d - %d" From ba9acab9bfc23bedbf639217af7c68142f022a07 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Wed, 17 Aug 2011 23:35:45 +0200 Subject: [PATCH 09/18] usb-host: constify port Signed-off-by: Gerd Hoffmann --- usb-linux.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index 6490582232..ef29a76ce5 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -54,7 +54,7 @@ struct usb_ctrltransfer { void *data; }; -typedef int USBScanFunc(void *opaque, int bus_num, int addr, char *port, +typedef int USBScanFunc(void *opaque, int bus_num, int addr, const char *port, int class_id, int vendor_id, int product_id, const char *product_name, int speed); @@ -1141,7 +1141,8 @@ static int usb_linux_full_speed_compat(USBHostDevice *dev) } static int usb_host_open(USBHostDevice *dev, int bus_num, - int addr, char *port, const char *prod_name, int speed) + int addr, const char *port, + const char *prod_name, int speed) { int fd = -1, ret; char buf[1024]; @@ -1774,7 +1775,8 @@ static int usb_host_scan(void *opaque, USBScanFunc *func) static QEMUTimer *usb_auto_timer; -static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port, +static int usb_host_auto_scan(void *opaque, int bus_num, + int addr, const char *port, int class_id, int vendor_id, int product_id, const char *product_name, int speed) { @@ -1948,7 +1950,8 @@ static const char *usb_class_str(uint8_t class) return p->class_name; } -static void usb_info_device(Monitor *mon, int bus_num, int addr, char *port, +static void usb_info_device(Monitor *mon, int bus_num, + int addr, const char *port, int class_id, int vendor_id, int product_id, const char *product_name, int speed) @@ -1989,7 +1992,7 @@ static void usb_info_device(Monitor *mon, int bus_num, int addr, char *port, } static int usb_host_info_device(void *opaque, int bus_num, int addr, - char *path, int class_id, + const char *path, int class_id, int vendor_id, int product_id, const char *product_name, int speed) From 0c402e5abb8c2755390eee864b43a98280fc2453 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Wed, 17 Aug 2011 23:36:46 +0200 Subject: [PATCH 10/18] usb-host: parse port in /proc/bus/usb/devices scan Unfortunaly this is limited to root ports. Signed-off-by: Gerd Hoffmann --- usb-linux.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/usb-linux.c b/usb-linux.c index ef29a76ce5..67639f3bc1 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -1483,7 +1483,8 @@ static int usb_host_scan_dev(void *opaque, USBScanFunc *func) FILE *f = NULL; char line[1024]; char buf[1024]; - int bus_num, addr, speed, device_count, class_id, product_id, vendor_id; + int bus_num, addr, speed, device_count; + int class_id, product_id, vendor_id, port; char product_name[512]; int ret = 0; @@ -1499,7 +1500,7 @@ static int usb_host_scan_dev(void *opaque, USBScanFunc *func) } device_count = 0; - bus_num = addr = class_id = product_id = vendor_id = 0; + bus_num = addr = class_id = product_id = vendor_id = port = 0; speed = -1; /* Can't get the speed from /[proc|dev]/bus/usb/devices */ for(;;) { if (fgets(line, sizeof(line), f) == NULL) { @@ -1521,6 +1522,10 @@ static int usb_host_scan_dev(void *opaque, USBScanFunc *func) goto fail; } bus_num = atoi(buf); + if (get_tag_value(buf, sizeof(buf), line, "Port=", " ") < 0) { + goto fail; + } + port = atoi(buf); if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) { goto fail; } @@ -1566,7 +1571,12 @@ static int usb_host_scan_dev(void *opaque, USBScanFunc *func) } if (device_count && (vendor_id || product_id)) { /* Add the last device. */ - ret = func(opaque, bus_num, addr, 0, class_id, vendor_id, + if (port > 0) { + snprintf(buf, sizeof(buf), "%d", port); + } else { + snprintf(buf, sizeof(buf), "?"); + } + ret = func(opaque, bus_num, addr, buf, class_id, vendor_id, product_id, product_name, speed); } the_end: From 4d8debba766265d70cb7bf11570e3622512641d6 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Thu, 25 Aug 2011 16:43:15 +0200 Subject: [PATCH 11/18] usb: fix use after free The ->complete() callback might have released the USBPacket (uhci actually does), so we must not touch it after the callback returns. Signed-off-by: Gerd Hoffmann --- hw/usb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/usb.c b/hw/usb.c index 685e775a00..a091e4eafc 100644 --- a/hw/usb.c +++ b/hw/usb.c @@ -338,8 +338,8 @@ void usb_packet_complete(USBDevice *dev, USBPacket *p) { /* Note: p->owner != dev is possible in case dev is a hub */ assert(p->owner != NULL); - dev->port->ops->complete(dev->port, p); p->owner = NULL; + dev->port->ops->complete(dev->port, p); } /* Cancel an active packet. The packed must have been deferred by From 97237e0a5a95123a072a49eddd3bc5e20534f7fa Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 30 Aug 2011 11:49:05 +0200 Subject: [PATCH 12/18] usb-ccid: switch to USBDesc* Switch the smard card emulation to use the USBDesc* structs for the usb descriptors. Signed-off-by: Gerd Hoffmann --- hw/usb-ccid.c | 243 +++++++++++++++++--------------------------------- hw/usb-desc.h | 2 +- 2 files changed, 82 insertions(+), 163 deletions(-) diff --git a/hw/usb-ccid.c b/hw/usb-ccid.c index c2f9241014..d40977be8d 100644 --- a/hw/usb-ccid.c +++ b/hw/usb-ccid.c @@ -37,6 +37,7 @@ #include "qemu-common.h" #include "qemu-error.h" #include "usb.h" +#include "usb-desc.h" #include "monitor.h" #include "hw/ccid.h" @@ -306,56 +307,7 @@ typedef struct USBCCIDState { * 0dc3:1004 Athena Smartcard Solutions, Inc. */ -static const uint8_t qemu_ccid_dev_descriptor[] = { - 0x12, /* u8 bLength; */ - USB_DT_DEVICE, /* u8 bDescriptorType; Device */ - 0x10, 0x01, /* u16 bcdUSB; v1.1 */ - - 0x00, /* u8 bDeviceClass; */ - 0x00, /* u8 bDeviceSubClass; */ - 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */ - 0x40, /* u8 bMaxPacketSize0; 8 Bytes (valid: 8,16,32,64) */ - - /* Vendor and product id are arbitrary. */ - /* u16 idVendor */ - CCID_VENDOR_ID & 0xff, CCID_VENDOR_ID >> 8, - /* u16 idProduct */ - CCID_PRODUCT_ID & 0xff, CCID_PRODUCT_ID >> 8, - /* u16 bcdDevice */ - CCID_DEVICE_VERSION & 0xff, CCID_DEVICE_VERSION >> 8, - 0x01, /* u8 iManufacturer; */ - 0x02, /* u8 iProduct; */ - 0x03, /* u8 iSerialNumber; */ - 0x01, /* u8 bNumConfigurations; */ -}; - -static const uint8_t qemu_ccid_config_descriptor[] = { - - /* one configuration */ - 0x09, /* u8 bLength; */ - USB_DT_CONFIG, /* u8 bDescriptorType; Configuration */ - 0x5d, 0x00, /* u16 wTotalLength; 9+9+54+7+7+7 */ - 0x01, /* u8 bNumInterfaces; (1) */ - 0x01, /* u8 bConfigurationValue; */ - 0x00, /* u8 iConfiguration; */ - 0xe0, /* u8 bmAttributes; - Bit 7: must be set, - 6: Self-powered, - 5: Remote wakeup, - 4..0: resvd */ - 100/2, /* u8 MaxPower; 50 == 100mA */ - - /* one interface */ - 0x09, /* u8 if_bLength; */ - USB_DT_INTERFACE, /* u8 if_bDescriptorType; Interface */ - 0x00, /* u8 if_bInterfaceNumber; */ - 0x00, /* u8 if_bAlternateSetting; */ - 0x03, /* u8 if_bNumEndpoints; */ - 0x0b, /* u8 if_bInterfaceClass; Smart Card Device Class */ - 0x00, /* u8 if_bInterfaceSubClass; Subclass code */ - 0x00, /* u8 if_bInterfaceProtocol; Protocol code */ - 0x04, /* u8 if_iInterface; Index of string descriptor */ - +static const uint8_t qemu_ccid_descriptor[] = { /* Smart Card Device Class Descriptor */ 0x36, /* u8 bLength; */ 0x21, /* u8 bDescriptorType; Functional */ @@ -439,38 +391,81 @@ static const uint8_t qemu_ccid_config_descriptor[] = { * 02h PIN Modification */ 0x01, /* u8 bMaxCCIDBusySlots; */ +}; - /* Interrupt-IN endpoint */ - 0x07, /* u8 ep_bLength; */ - /* u8 ep_bDescriptorType; Endpoint */ - USB_DT_ENDPOINT, - /* u8 ep_bEndpointAddress; IN Endpoint 1 */ - 0x80 | CCID_INT_IN_EP, - 0x03, /* u8 ep_bmAttributes; Interrupt */ - /* u16 ep_wMaxPacketSize; */ - CCID_MAX_PACKET_SIZE & 0xff, (CCID_MAX_PACKET_SIZE >> 8), - 0xff, /* u8 ep_bInterval; */ +enum { + STR_MANUFACTURER = 1, + STR_PRODUCT, + STR_SERIALNUMBER, + STR_INTERFACE, +}; - /* Bulk-In endpoint */ - 0x07, /* u8 ep_bLength; */ - /* u8 ep_bDescriptorType; Endpoint */ - USB_DT_ENDPOINT, - /* u8 ep_bEndpointAddress; IN Endpoint 2 */ - 0x80 | CCID_BULK_IN_EP, - 0x02, /* u8 ep_bmAttributes; Bulk */ - 0x40, 0x00, /* u16 ep_wMaxPacketSize; */ - 0x00, /* u8 ep_bInterval; */ +static const USBDescStrings desc_strings = { + [STR_MANUFACTURER] = "QEMU " QEMU_VERSION, + [STR_PRODUCT] = "QEMU USB CCID", + [STR_SERIALNUMBER] = "1", + [STR_INTERFACE] = "CCID Interface", +}; - /* Bulk-Out endpoint */ - 0x07, /* u8 ep_bLength; */ - /* u8 ep_bDescriptorType; Endpoint */ - USB_DT_ENDPOINT, - /* u8 ep_bEndpointAddress; OUT Endpoint 3 */ - CCID_BULK_OUT_EP, - 0x02, /* u8 ep_bmAttributes; Bulk */ - 0x40, 0x00, /* u16 ep_wMaxPacketSize; */ - 0x00, /* u8 ep_bInterval; */ +static const USBDescIface desc_iface0 = { + .bInterfaceNumber = 0, + .bNumEndpoints = 3, + .bInterfaceClass = 0x0b, + .bInterfaceSubClass = 0x00, + .bInterfaceProtocol = 0x00, + .iInterface = STR_INTERFACE, + .ndesc = 1, + .descs = (USBDescOther[]) { + { + /* smartcard descriptor */ + .data = qemu_ccid_descriptor, + }, + }, + .eps = (USBDescEndpoint[]) { + { + .bEndpointAddress = USB_DIR_IN | CCID_INT_IN_EP, + .bmAttributes = USB_ENDPOINT_XFER_INT, + .bInterval = 255, + .wMaxPacketSize = 64, + },{ + .bEndpointAddress = USB_DIR_IN | CCID_BULK_IN_EP, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = 64, + },{ + .bEndpointAddress = USB_DIR_OUT | CCID_BULK_OUT_EP, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = 64, + }, + } +}; +static const USBDescDevice desc_device = { + .bcdUSB = 0x0110, + .bMaxPacketSize0 = 64, + .bNumConfigurations = 1, + .confs = (USBDescConfig[]) { + { + .bNumInterfaces = 1, + .bConfigurationValue = 1, + .bmAttributes = 0x80, + .bMaxPower = 50, + .nif = 1, + .ifs = &desc_iface0, + }, + }, +}; + +static const USBDesc desc_ccid = { + .id = { + .idVendor = CCID_VENDOR_ID, + .idProduct = CCID_PRODUCT_ID, + .bcdDevice = CCID_DEVICE_VERSION, + .iManufacturer = STR_MANUFACTURER, + .iProduct = STR_PRODUCT, + .iSerialNumber = STR_SERIALNUMBER, + }, + .full = &desc_device, + .str = desc_strings, }; static bool ccid_has_pending_answers(USBCCIDState *s) @@ -610,87 +605,12 @@ static int ccid_handle_control(USBDevice *dev, USBPacket *p, int request, int ret = 0; DPRINTF(s, 1, "got control %x, value %x\n", request, value); + ret = usb_desc_handle_control(dev, p, request, value, index, length, data); + if (ret >= 0) { + return ret; + } + switch (request) { - case DeviceRequest | USB_REQ_GET_STATUS: - data[0] = (1 << USB_DEVICE_SELF_POWERED) | - (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP); - data[1] = 0x00; - ret = 2; - break; - case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: - if (value == USB_DEVICE_REMOTE_WAKEUP) { - dev->remote_wakeup = 0; - } else { - goto fail; - } - ret = 0; - break; - case DeviceOutRequest | USB_REQ_SET_FEATURE: - if (value == USB_DEVICE_REMOTE_WAKEUP) { - dev->remote_wakeup = 1; - } else { - goto fail; - } - ret = 0; - break; - case DeviceOutRequest | USB_REQ_SET_ADDRESS: - dev->addr = value; - ret = 0; - break; - case DeviceRequest | USB_REQ_GET_DESCRIPTOR: - switch (value >> 8) { - case USB_DT_DEVICE: - memcpy(data, qemu_ccid_dev_descriptor, - sizeof(qemu_ccid_dev_descriptor)); - ret = sizeof(qemu_ccid_dev_descriptor); - break; - case USB_DT_CONFIG: - memcpy(data, qemu_ccid_config_descriptor, - sizeof(qemu_ccid_config_descriptor)); - ret = sizeof(qemu_ccid_config_descriptor); - break; - case USB_DT_STRING: - switch (value & 0xff) { - case 0: - /* language ids */ - data[0] = 4; - data[1] = 3; - data[2] = 0x09; - data[3] = 0x04; - ret = 4; - break; - case 1: - /* vendor description */ - ret = set_usb_string(data, CCID_VENDOR_DESCRIPTION); - break; - case 2: - /* product description */ - ret = set_usb_string(data, CCID_PRODUCT_DESCRIPTION); - break; - case 3: - /* serial number */ - ret = set_usb_string(data, CCID_SERIAL_NUMBER_STRING); - break; - case 4: - /* interface name */ - ret = set_usb_string(data, CCID_INTERFACE_NAME); - break; - default: - goto fail; - } - break; - default: - goto fail; - } - break; - case DeviceRequest | USB_REQ_GET_CONFIGURATION: - data[0] = 1; - ret = 1; - break; - case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: - /* Only one configuration - we just ignore the request */ - ret = 0; - break; case DeviceRequest | USB_REQ_GET_INTERFACE: data[0] = 0; ret = 1; @@ -698,9 +618,6 @@ static int ccid_handle_control(USBDevice *dev, USBPacket *p, int request, case InterfaceOutRequest | USB_REQ_SET_INTERFACE: ret = 0; break; - case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: - ret = 0; - break; /* Class specific requests. */ case InterfaceOutClass | CCID_CONTROL_ABORT: @@ -716,7 +633,6 @@ static int ccid_handle_control(USBDevice *dev, USBPacket *p, int request, ret = USB_RET_STALL; break; default: -fail: DPRINTF(s, 1, "got unsupported/bogus control %x, value %x\n", request, value); ret = USB_RET_STALL; @@ -1075,6 +991,7 @@ static int ccid_handle_data(USBDevice *dev, USBPacket *p) break; default: DPRINTF(s, 1, "Bad endpoint\n"); + ret = USB_RET_STALL; break; } break; @@ -1256,6 +1173,7 @@ static int ccid_initfn(USBDevice *dev) { USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev); + usb_desc_init(dev); qbus_create_inplace(&s->bus.qbus, &ccid_bus_info, &dev->qdev, NULL); s->bus.qbus.allow_hotplug = 1; s->card = NULL; @@ -1381,6 +1299,7 @@ static struct USBDeviceInfo ccid_info = { .qdev.desc = "CCID Rev 1.1 smartcard reader", .qdev.size = sizeof(USBCCIDState), .init = ccid_initfn, + .usb_desc = &desc_ccid, .handle_packet = usb_generic_handle_packet, .handle_reset = ccid_handle_reset, .handle_control = ccid_handle_control, diff --git a/hw/usb-desc.h b/hw/usb-desc.h index 9d7ed599ce..5c14e4abdc 100644 --- a/hw/usb-desc.h +++ b/hw/usb-desc.h @@ -75,7 +75,7 @@ struct USBDescEndpoint { struct USBDescOther { uint8_t length; - uint8_t *data; + const uint8_t *data; }; typedef const char *USBDescStrings[256]; From 7755260f015dcbb8fa47bcc6f1e2ca317b2205aa Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 30 Aug 2011 12:15:53 +0200 Subject: [PATCH 13/18] usb-ccid: remote wakeup support Signed-off-by: Gerd Hoffmann --- hw/usb-ccid.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hw/usb-ccid.c b/hw/usb-ccid.c index d40977be8d..cd349f3f17 100644 --- a/hw/usb-ccid.c +++ b/hw/usb-ccid.c @@ -447,7 +447,7 @@ static const USBDescDevice desc_device = { { .bNumInterfaces = 1, .bConfigurationValue = 1, - .bmAttributes = 0x80, + .bmAttributes = 0xa0, .bMaxPower = 50, .nif = 1, .ifs = &desc_iface0, @@ -811,6 +811,7 @@ static void ccid_on_slot_change(USBCCIDState *s, bool full) s->bmSlotICCState |= SLOT_0_CHANGED_MASK; } s->notify_slot_change = true; + usb_wakeup(&s->dev); } static void ccid_write_data_block_error( From 891fb2cd4592b6fe76106a69e0ca40efbf82726a Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Thu, 1 Sep 2011 13:56:37 +0200 Subject: [PATCH 14/18] usb: claim port at device initialization time. This patch makes qemu assign a port when creating the device, not when attaching it. For most usb devices this isn't a noticable difference because they are in attached state all the time. The change affects usb-host devices which live in detached state while the real device is unplugged from the host. They have a fixed port assigned all the time now instead of getting grabbing one on attach and releasing it at detach, i.e. they stop floating around at the usb bus. The change also allows to simplify usb-hub. It doesn't need the handle_attach() callback any more to configure the downstream ports. This can be done at device initialitation time now. The changed initialization order (first grab upstream port, then register downstream ports) also fixes some icky corner cases. For example it is not possible any more to plug the hub into one of its own downstream ports. The usb host adapters must care too. USBPort->dev being non-NULL doesn't imply any more the device is in attached state. The host adapters must additionally check the USBPort->dev->attached flag. Signed-off-by: Gerd Hoffmann --- hw/usb-bus.c | 112 +++++++++++++++++++++++++++++--------------------- hw/usb-ehci.c | 22 +++++----- hw/usb-hub.c | 12 +----- hw/usb-ohci.c | 4 +- hw/usb-uhci.c | 11 ++--- hw/usb.c | 35 ++++++++-------- hw/usb.h | 5 ++- trace-events | 6 +++ 8 files changed, 111 insertions(+), 96 deletions(-) diff --git a/hw/usb-bus.c b/hw/usb-bus.c index c0bbc7c6b0..93f640d370 100644 --- a/hw/usb-bus.c +++ b/hw/usb-bus.c @@ -3,6 +3,7 @@ #include "qdev.h" #include "sysemu.h" #include "monitor.h" +#include "trace.h" static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); @@ -73,9 +74,13 @@ static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base) dev->info = info; dev->auto_attach = 1; QLIST_INIT(&dev->strings); - rc = dev->info->init(dev); - if (rc == 0 && dev->auto_attach) + rc = usb_claim_port(dev); + if (rc == 0) { + rc = dev->info->init(dev); + } + if (rc == 0 && dev->auto_attach) { rc = usb_device_attach(dev); + } return rc; } @@ -89,6 +94,9 @@ static int usb_qdev_exit(DeviceState *qdev) if (dev->info->handle_destroy) { dev->info->handle_destroy(dev); } + if (dev->port) { + usb_release_port(dev); + } return 0; } @@ -205,21 +213,13 @@ void usb_unregister_port(USBBus *bus, USBPort *port) bus->nfree--; } -static int do_attach(USBDevice *dev) +int usb_claim_port(USBDevice *dev) { USBBus *bus = usb_bus_from_device(dev); USBPort *port; - if (dev->attached) { - error_report("Error: tried to attach usb device %s twice\n", - dev->product_desc); - return -1; - } - if (bus->nfree == 0) { - error_report("Error: tried to attach usb device %s to a bus with no free ports\n", - dev->product_desc); - return -1; - } + assert(dev->port == NULL); + if (dev->port_path) { QTAILQ_FOREACH(port, &bus->free, next) { if (strcmp(port->path, dev->port_path) == 0) { @@ -227,68 +227,86 @@ static int do_attach(USBDevice *dev) } } if (port == NULL) { - error_report("Error: usb port %s (bus %s) not found\n", - dev->port_path, bus->qbus.name); + error_report("Error: usb port %s (bus %s) not found (in use?)\n", + dev->port_path, bus->qbus.name); return -1; } } else { + if (bus->nfree == 1 && strcmp(dev->qdev.info->name, "usb-hub") != 0) { + /* Create a new hub and chain it on */ + usb_create_simple(bus, "usb-hub"); + } + if (bus->nfree == 0) { + error_report("Error: tried to attach usb device %s to a bus " + "with no free ports\n", dev->product_desc); + return -1; + } port = QTAILQ_FIRST(&bus->free); } - if (!(port->speedmask & dev->speedmask)) { - error_report("Warning: speed mismatch trying to attach usb device %s to bus %s\n", - dev->product_desc, bus->qbus.name); - return -1; - } + trace_usb_port_claim(bus->busnr, port->path); - dev->attached++; QTAILQ_REMOVE(&bus->free, port, next); bus->nfree--; - usb_attach(port, dev); + dev->port = port; + port->dev = dev; QTAILQ_INSERT_TAIL(&bus->used, port, next); bus->nused++; - return 0; } +void usb_release_port(USBDevice *dev) +{ + USBBus *bus = usb_bus_from_device(dev); + USBPort *port = dev->port; + + assert(port != NULL); + trace_usb_port_release(bus->busnr, port->path); + + QTAILQ_REMOVE(&bus->used, port, next); + bus->nused--; + + dev->port = NULL; + port->dev = NULL; + + QTAILQ_INSERT_TAIL(&bus->free, port, next); + bus->nfree++; +} + int usb_device_attach(USBDevice *dev) { USBBus *bus = usb_bus_from_device(dev); + USBPort *port = dev->port; - if (bus->nfree == 1 && dev->port_path == NULL) { - /* Create a new hub and chain it on - (unless a physical port location is specified). */ - usb_create_simple(bus, "usb-hub"); + assert(port != NULL); + assert(!dev->attached); + trace_usb_port_attach(bus->busnr, port->path); + + if (!(port->speedmask & dev->speedmask)) { + error_report("Warning: speed mismatch trying to attach " + "usb device %s to bus %s\n", + dev->product_desc, bus->qbus.name); + return -1; } - return do_attach(dev); + + dev->attached++; + usb_attach(port); + + return 0; } int usb_device_detach(USBDevice *dev) { USBBus *bus = usb_bus_from_device(dev); - USBPort *port; + USBPort *port = dev->port; - if (!dev->attached) { - error_report("Error: tried to detach unattached usb device %s\n", - dev->product_desc); - return -1; - } - dev->attached--; - - QTAILQ_FOREACH(port, &bus->used, next) { - if (port->dev == dev) - break; - } assert(port != NULL); + assert(dev->attached); + trace_usb_port_detach(bus->busnr, port->path); - QTAILQ_REMOVE(&bus->used, port, next); - bus->nused--; - - usb_attach(port, NULL); - - QTAILQ_INSERT_TAIL(&bus->free, port, next); - bus->nfree++; + usb_detach(port); + dev->attached--; return 0; } diff --git a/hw/usb-ehci.c b/hw/usb-ehci.c index d8ef0cb422..e9e0789795 100644 --- a/hw/usb-ehci.c +++ b/hw/usb-ehci.c @@ -857,8 +857,8 @@ static void ehci_reset(void *opaque) */ for(i = 0; i < NB_PORTS; i++) { devs[i] = s->ports[i].dev; - if (devs[i]) { - usb_attach(&s->ports[i], NULL); + if (devs[i] && devs[i]->attached) { + usb_detach(&s->ports[i]); } } @@ -878,8 +878,8 @@ static void ehci_reset(void *opaque) } else { s->portsc[i] = PORTSC_PPOWER; } - if (devs[i]) { - usb_attach(&s->ports[i], devs[i]); + if (devs[i] && devs[i]->attached) { + usb_attach(&s->ports[i]); } } ehci_queues_rip_all(s); @@ -945,15 +945,15 @@ static void handle_port_owner_write(EHCIState *s, int port, uint32_t owner) return; } - if (dev) { - usb_attach(&s->ports[port], NULL); + if (dev && dev->attached) { + usb_detach(&s->ports[port]); } *portsc &= ~PORTSC_POWNER; *portsc |= owner; - if (dev) { - usb_attach(&s->ports[port], dev); + if (dev && dev->attached) { + usb_attach(&s->ports[port]); } } @@ -977,8 +977,8 @@ static void handle_port_status_write(EHCIState *s, int port, uint32_t val) if (!(val & PORTSC_PRESET) &&(*portsc & PORTSC_PRESET)) { trace_usb_ehci_port_reset(port, 0); - if (dev) { - usb_attach(&s->ports[port], dev); + if (dev && dev->attached) { + usb_attach(&s->ports[port]); usb_send_msg(dev, USB_MSG_RESET); *portsc &= ~PORTSC_CSC; } @@ -987,7 +987,7 @@ static void handle_port_status_write(EHCIState *s, int port, uint32_t val) * Table 2.16 Set the enable bit(and enable bit change) to indicate * to SW that this port has a high speed device attached */ - if (dev && (dev->speedmask & USB_SPEED_MASK_HIGH)) { + if (dev && dev->attached && (dev->speedmask & USB_SPEED_MASK_HIGH)) { val |= PORTSC_PED; } } diff --git a/hw/usb-hub.c b/hw/usb-hub.c index c49c547d0c..286e3ad85d 100644 --- a/hw/usb-hub.c +++ b/hw/usb-hub.c @@ -213,16 +213,6 @@ static void usb_hub_complete(USBPort *port, USBPacket *packet) usb_packet_complete(&s->dev, packet); } -static void usb_hub_handle_attach(USBDevice *dev) -{ - USBHubState *s = DO_UPCAST(USBHubState, dev, dev); - int i; - - for (i = 0; i < NUM_PORTS; i++) { - usb_port_location(&s->ports[i].port, dev->port, i+1); - } -} - static void usb_hub_handle_reset(USBDevice *dev) { /* XXX: do it */ @@ -499,6 +489,7 @@ static int usb_hub_initfn(USBDevice *dev) usb_register_port(usb_bus_from_device(dev), &port->port, s, i, &usb_hub_port_ops, USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL); + usb_port_location(&port->port, dev->port, i+1); port->wPortStatus = PORT_STAT_POWER; port->wPortChange = 0; } @@ -537,7 +528,6 @@ static struct USBDeviceInfo hub_info = { .usb_desc = &desc_hub, .init = usb_hub_initfn, .handle_packet = usb_hub_handle_packet, - .handle_attach = usb_hub_handle_attach, .handle_reset = usb_hub_handle_reset, .handle_control = usb_hub_handle_control, .handle_data = usb_hub_handle_data, diff --git a/hw/usb-ohci.c b/hw/usb-ohci.c index d30db3f92f..503ca2d31f 100644 --- a/hw/usb-ohci.c +++ b/hw/usb-ohci.c @@ -448,8 +448,8 @@ static void ohci_reset(void *opaque) { port = &ohci->rhport[i]; port->ctrl = 0; - if (port->port.dev) { - usb_attach(&port->port, port->port.dev); + if (port->port.dev && port->port.dev->attached) { + usb_attach(&port->port); } } if (ohci->async_td) { diff --git a/hw/usb-uhci.c b/hw/usb-uhci.c index 6ca7ca81eb..64f7b36c00 100644 --- a/hw/usb-uhci.c +++ b/hw/usb-uhci.c @@ -340,8 +340,8 @@ static void uhci_reset(void *opaque) for(i = 0; i < NB_PORTS; i++) { port = &s->ports[i]; port->ctrl = 0x0080; - if (port->port.dev) { - usb_attach(&port->port, port->port.dev); + if (port->port.dev && port->port.dev->attached) { + usb_attach(&port->port); } } @@ -446,7 +446,7 @@ static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val) for(i = 0; i < NB_PORTS; i++) { port = &s->ports[i]; dev = port->port.dev; - if (dev) { + if (dev && dev->attached) { usb_send_msg(dev, USB_MSG_RESET); } } @@ -486,7 +486,7 @@ static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val) return; port = &s->ports[n]; dev = port->port.dev; - if (dev) { + if (dev && dev->attached) { /* port reset */ if ( (val & UHCI_PORT_RESET) && !(port->ctrl & UHCI_PORT_RESET) ) { @@ -660,8 +660,9 @@ static int uhci_broadcast_packet(UHCIState *s, USBPacket *p) UHCIPort *port = &s->ports[i]; USBDevice *dev = port->port.dev; - if (dev && (port->ctrl & UHCI_PORT_EN)) + if (dev && dev->attached && (port->ctrl & UHCI_PORT_EN)) { ret = usb_handle_packet(dev, p); + } } DPRINTF("uhci: packet exit. ret %d len %zd\n", ret, p->iov.size); diff --git a/hw/usb.c b/hw/usb.c index a091e4eafc..fa90204c5e 100644 --- a/hw/usb.c +++ b/hw/usb.c @@ -27,26 +27,23 @@ #include "usb.h" #include "iov.h" -void usb_attach(USBPort *port, USBDevice *dev) +void usb_attach(USBPort *port) { - if (dev != NULL) { - /* attach */ - if (port->dev) { - usb_attach(port, NULL); - } - dev->port = port; - port->dev = dev; - port->ops->attach(port); - usb_send_msg(dev, USB_MSG_ATTACH); - } else { - /* detach */ - dev = port->dev; - assert(dev); - port->ops->detach(port); - usb_send_msg(dev, USB_MSG_DETACH); - dev->port = NULL; - port->dev = NULL; - } + USBDevice *dev = port->dev; + + assert(dev != NULL); + assert(dev->attached); + port->ops->attach(port); + usb_send_msg(dev, USB_MSG_ATTACH); +} + +void usb_detach(USBPort *port) +{ + USBDevice *dev = port->dev; + + assert(dev != NULL); + port->ops->detach(port); + usb_send_msg(dev, USB_MSG_DETACH); } void usb_wakeup(USBDevice *dev) diff --git a/hw/usb.h b/hw/usb.h index d784448290..73479d66c9 100644 --- a/hw/usb.h +++ b/hw/usb.h @@ -304,7 +304,8 @@ int usb_handle_packet(USBDevice *dev, USBPacket *p); void usb_packet_complete(USBDevice *dev, USBPacket *p); void usb_cancel_packet(USBPacket * p); -void usb_attach(USBPort *port, USBDevice *dev); +void usb_attach(USBPort *port); +void usb_detach(USBPort *port); void usb_wakeup(USBDevice *dev); int usb_generic_handle_packet(USBDevice *s, USBPacket *p); void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p); @@ -378,6 +379,8 @@ int usb_register_companion(const char *masterbus, USBPort *ports[], void *opaque, USBPortOps *ops, int speedmask); void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr); void usb_unregister_port(USBBus *bus, USBPort *port); +int usb_claim_port(USBDevice *dev); +void usb_release_port(USBDevice *dev); int usb_device_attach(USBDevice *dev); int usb_device_detach(USBDevice *dev); int usb_device_delete_addr(int busnr, int addr); diff --git a/trace-events b/trace-events index 5936192289..3fdd60faa4 100644 --- a/trace-events +++ b/trace-events @@ -209,6 +209,12 @@ sun4m_iommu_page_get_flags(uint64_t pa, uint64_t iopte, uint32_t ret) "get flags sun4m_iommu_translate_pa(uint64_t addr, uint64_t pa, uint32_t iopte) "xlate dva %"PRIx64" => pa %"PRIx64" iopte = %x" sun4m_iommu_bad_addr(uint64_t addr) "bad addr %"PRIx64"" +# hw/usb-bus.c +usb_port_claim(int bus, const char *port) "bus %d, port %s" +usb_port_attach(int bus, const char *port) "bus %d, port %s" +usb_port_detach(int bus, const char *port) "bus %d, port %s" +usb_port_release(int bus, const char *port) "bus %d, port %s" + # hw/usb-ehci.c usb_ehci_reset(void) "=== RESET ===" usb_ehci_mmio_readl(uint32_t addr, const char *str, uint32_t val) "rd mmio %04x [%s] = %x" From d67915786822b42094ce99768e595994cf6de0bf Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Wed, 31 Aug 2011 11:44:24 +0200 Subject: [PATCH 15/18] usb-host: tag as unmigratable Signed-off-by: Gerd Hoffmann --- usb-linux.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/usb-linux.c b/usb-linux.c index 67639f3bc1..2075c4c67a 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -1354,10 +1354,16 @@ out: return 0; } +static const VMStateDescription vmstate_usb_host = { + .name = "usb-host", + .unmigratable = 1, +}; + static struct USBDeviceInfo usb_host_dev_info = { .product_desc = "USB Host Device", .qdev.name = "usb-host", .qdev.size = sizeof(USBHostDevice), + .qdev.vmsd = &vmstate_usb_host, .init = usb_host_initfn, .handle_packet = usb_generic_handle_packet, .cancel_packet = usb_host_async_cancel, From 9147b752887d65dfe2431f0fbff1d4a2545344d0 Mon Sep 17 00:00:00 2001 From: Peter Maydell Date: Wed, 31 Aug 2011 21:49:39 +0100 Subject: [PATCH 16/18] usb: Remove leading underscores from __musb_irq_max Identifiers with double leading underscore are reserved, so rename __musb_irq_max so we don't encroach on reserved namespace. Signed-off-by: Peter Maydell Signed-off-by: Gerd Hoffmann --- hw/tusb6010.c | 6 +++--- hw/usb.h | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/hw/tusb6010.c b/hw/tusb6010.c index de6ffc6133..78814f132c 100644 --- a/hw/tusb6010.c +++ b/hw/tusb6010.c @@ -785,9 +785,9 @@ static int tusb6010_init(SysBusDevice *dev) sysbus_init_mmio_region(dev, &s->iomem[0]); sysbus_init_mmio_region(dev, &s->iomem[1]); sysbus_init_irq(dev, &s->irq); - qdev_init_gpio_in(&dev->qdev, tusb6010_irq, __musb_irq_max + 1); - musb_irqs = g_new0(qemu_irq, __musb_irq_max); - for (i = 0; i < __musb_irq_max; i++) { + qdev_init_gpio_in(&dev->qdev, tusb6010_irq, musb_irq_max + 1); + musb_irqs = g_new0(qemu_irq, musb_irq_max); + for (i = 0; i < musb_irq_max; i++) { musb_irqs[i] = qdev_get_gpio_in(&dev->qdev, i + 1); } s->musb = musb_init(musb_irqs); diff --git a/hw/usb.h b/hw/usb.h index 73479d66c9..e251e616f4 100644 --- a/hw/usb.h +++ b/hw/usb.h @@ -338,7 +338,8 @@ enum musb_irq_source_e { musb_irq_tx, musb_set_vbus, musb_set_session, - __musb_irq_max, + /* Add new interrupts here */ + musb_irq_max, /* total number of interrupts defined */ }; typedef struct MUSBState MUSBState; From 406c20754a29586f6dc1fccacbca3792be24922c Mon Sep 17 00:00:00 2001 From: Peter Maydell Date: Wed, 31 Aug 2011 21:49:40 +0100 Subject: [PATCH 17/18] usb-musb: Take a DeviceState* in init function Initialise usb-musb by passing it a DeviceState* and the offset of the IRQs in its gpio array, rather than a plain pointer to an irq array. This is simpler for callers and also allows us to pass in a valid parent to usb_bus_new(), so the USB bus actually appears in the qdev tree. Signed-off-by: Peter Maydell Signed-off-by: Gerd Hoffmann --- hw/tusb6010.c | 8 +------- hw/usb-musb.c | 10 ++++++---- hw/usb.h | 2 +- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/hw/tusb6010.c b/hw/tusb6010.c index 78814f132c..57fe8045b8 100644 --- a/hw/tusb6010.c +++ b/hw/tusb6010.c @@ -776,8 +776,6 @@ static void tusb6010_reset(DeviceState *dev) static int tusb6010_init(SysBusDevice *dev) { TUSBState *s = FROM_SYSBUS(TUSBState, dev); - qemu_irq *musb_irqs; - int i; s->otg_timer = qemu_new_timer_ns(vm_clock, tusb_otg_tick, s); s->pwr_timer = qemu_new_timer_ns(vm_clock, tusb_power_tick, s); memory_region_init_io(&s->iomem[1], &tusb_async_ops, s, "tusb-async", @@ -786,11 +784,7 @@ static int tusb6010_init(SysBusDevice *dev) sysbus_init_mmio_region(dev, &s->iomem[1]); sysbus_init_irq(dev, &s->irq); qdev_init_gpio_in(&dev->qdev, tusb6010_irq, musb_irq_max + 1); - musb_irqs = g_new0(qemu_irq, musb_irq_max); - for (i = 0; i < musb_irq_max; i++) { - musb_irqs[i] = qdev_get_gpio_in(&dev->qdev, i + 1); - } - s->musb = musb_init(musb_irqs); + s->musb = musb_init(&dev->qdev, 1); return 0; } diff --git a/hw/usb-musb.c b/hw/usb-musb.c index 799fa6e187..640037fab6 100644 --- a/hw/usb-musb.c +++ b/hw/usb-musb.c @@ -314,7 +314,7 @@ struct MUSBEndPoint { }; struct MUSBState { - qemu_irq *irqs; + qemu_irq irqs[musb_irq_max]; USBBus bus; USBPort port; @@ -340,12 +340,14 @@ struct MUSBState { MUSBEndPoint ep[16]; }; -struct MUSBState *musb_init(qemu_irq *irqs) +struct MUSBState *musb_init(DeviceState *parent_device, int gpio_base) { MUSBState *s = g_malloc0(sizeof(*s)); int i; - s->irqs = irqs; + for (i = 0; i < musb_irq_max; i++) { + s->irqs[i] = qdev_get_gpio_in(parent_device, gpio_base + i); + } s->faddr = 0x00; s->power = MGC_M_POWER_HSENAB; @@ -369,7 +371,7 @@ struct MUSBState *musb_init(qemu_irq *irqs) usb_packet_init(&s->ep[i].packey[1].p); } - usb_bus_new(&s->bus, &musb_bus_ops, NULL /* FIXME */); + usb_bus_new(&s->bus, &musb_bus_ops, parent_device); usb_register_port(&s->bus, &s->port, s, 0, &musb_port_ops, USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL); diff --git a/hw/usb.h b/hw/usb.h index e251e616f4..55c061e3f9 100644 --- a/hw/usb.h +++ b/hw/usb.h @@ -343,7 +343,7 @@ enum musb_irq_source_e { }; typedef struct MUSBState MUSBState; -MUSBState *musb_init(qemu_irq *irqs); +MUSBState *musb_init(DeviceState *parent_device, int gpio_base); uint32_t musb_core_intr_get(MUSBState *s); void musb_core_intr_clear(MUSBState *s, uint32_t mask); void musb_set_size(MUSBState *s, int epnum, int size, int is_tx); From 5b1cdb4ead0b3d2190492fce0a17e809ef915f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juha=20Riihim=C3=A4ki?= Date: Wed, 31 Aug 2011 21:49:41 +0100 Subject: [PATCH 18/18] usb-musb: Add reset function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a separate reset function musb_reset() to the usb-musb interface, so that users who implement a reset function can also reset usb-musb. Use this in tusb6010. Signed-off-by: Juha Riihimäki [Riku Voipio: Fixes and restructuring patchset] Signed-off-by: Riku Voipio [Peter Maydell: More fixes and cleanups for upstream submission] Signed-off-by: Peter Maydell Signed-off-by: Gerd Hoffmann --- hw/tusb6010.c | 1 + hw/usb-musb.c | 24 ++++++++++++++++++------ hw/usb.h | 1 + 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/hw/tusb6010.c b/hw/tusb6010.c index 57fe8045b8..ce7c81f8f2 100644 --- a/hw/tusb6010.c +++ b/hw/tusb6010.c @@ -771,6 +771,7 @@ static void tusb6010_reset(DeviceState *dev) for (i = 0; i < 15; i++) { s->rx_config[i] = s->tx_config[i] = 0; } + musb_reset(s->musb); } static int tusb6010_init(SysBusDevice *dev) diff --git a/hw/usb-musb.c b/hw/usb-musb.c index 640037fab6..01e2e7c389 100644 --- a/hw/usb-musb.c +++ b/hw/usb-musb.c @@ -340,16 +340,12 @@ struct MUSBState { MUSBEndPoint ep[16]; }; -struct MUSBState *musb_init(DeviceState *parent_device, int gpio_base) +void musb_reset(MUSBState *s) { - MUSBState *s = g_malloc0(sizeof(*s)); int i; - for (i = 0; i < musb_irq_max; i++) { - s->irqs[i] = qdev_get_gpio_in(parent_device, gpio_base + i); - } - s->faddr = 0x00; + s->devctl = 0; s->power = MGC_M_POWER_HSENAB; s->tx_intr = 0x0000; s->rx_intr = 0x0000; @@ -359,6 +355,10 @@ struct MUSBState *musb_init(DeviceState *parent_device, int gpio_base) s->mask = 0x06; s->idx = 0; + s->setup_len = 0; + s->session = 0; + memset(s->buf, 0, sizeof(s->buf)); + /* TODO: _DW */ s->ep[0].config = MGC_M_CONFIGDATA_SOFTCONE | MGC_M_CONFIGDATA_DYNFIFO; for (i = 0; i < 16; i ++) { @@ -370,6 +370,18 @@ struct MUSBState *musb_init(DeviceState *parent_device, int gpio_base) usb_packet_init(&s->ep[i].packey[0].p); usb_packet_init(&s->ep[i].packey[1].p); } +} + +struct MUSBState *musb_init(DeviceState *parent_device, int gpio_base) +{ + MUSBState *s = g_malloc0(sizeof(*s)); + int i; + + for (i = 0; i < musb_irq_max; i++) { + s->irqs[i] = qdev_get_gpio_in(parent_device, gpio_base + i); + } + + musb_reset(s); usb_bus_new(&s->bus, &musb_bus_ops, parent_device); usb_register_port(&s->bus, &s->port, s, 0, &musb_port_ops, diff --git a/hw/usb.h b/hw/usb.h index 55c061e3f9..c08d469496 100644 --- a/hw/usb.h +++ b/hw/usb.h @@ -344,6 +344,7 @@ enum musb_irq_source_e { typedef struct MUSBState MUSBState; MUSBState *musb_init(DeviceState *parent_device, int gpio_base); +void musb_reset(MUSBState *s); uint32_t musb_core_intr_get(MUSBState *s); void musb_core_intr_clear(MUSBState *s, uint32_t mask); void musb_set_size(MUSBState *s, int epnum, int size, int is_tx);