Lines Matching +full:libusb +full:- +full:dev
11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
15 * Completely rewritten to use libusb instead of usbfs ioctls.
41 #include <libusb.h>
51 #include "qemu/error-report.h"
52 #include "qemu/main-loop.h"
58 #include "hw/qdev-properties.h"
61 /* ------------------------------------------------------------------------ */
63 #define TYPE_USB_HOST_DEVICE "usb-host"
106 libusb_device *dev; member
164 /* ------------------------------------------------------------------------ */
166 #ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */
170 /* ------------------------------------------------------------------------ */
218 [-LIBUSB_ERROR_IO] = "IO",
219 [-LIBUSB_ERROR_INVALID_PARAM] = "INVALID_PARAM",
220 [-LIBUSB_ERROR_ACCESS] = "ACCESS",
221 [-LIBUSB_ERROR_NO_DEVICE] = "NO_DEVICE",
222 [-LIBUSB_ERROR_NOT_FOUND] = "NOT_FOUND",
223 [-LIBUSB_ERROR_BUSY] = "BUSY",
224 [-LIBUSB_ERROR_TIMEOUT] = "TIMEOUT",
225 [-LIBUSB_ERROR_OVERFLOW] = "OVERFLOW",
226 [-LIBUSB_ERROR_PIPE] = "PIPE",
227 [-LIBUSB_ERROR_INTERRUPTED] = "INTERRUPTED",
228 [-LIBUSB_ERROR_NO_MEM] = "NO_MEM",
229 [-LIBUSB_ERROR_NOT_SUPPORTED] = "NOT_SUPPORTED",
230 [-LIBUSB_ERROR_OTHER] = "OTHER",
294 return -1; in usb_host_init()
312 usb_host_add_fd(poll[i]->fd, poll[i]->events, ctx); in usb_host_init()
320 static int usb_host_get_port(libusb_device *dev, char *port, size_t len) in usb_host_get_port() argument
327 rc = libusb_get_port_numbers(dev, path, 7); in usb_host_get_port()
329 rc = libusb_get_port_path(ctx, dev, path, 7); in usb_host_get_port()
336 off += snprintf(port+off, len-off, ".%d", path[i]); in usb_host_get_port()
349 if (-rc < ARRAY_SIZE(err_names) && err_names[-rc]) { in usb_host_libusb_error()
350 errname = err_names[-rc]; in usb_host_libusb_error()
357 /* ------------------------------------------------------------------------ */
363 if (!ep->pipeline) { in usb_host_use_combining()
366 if (ep->pid != USB_TOKEN_IN) { in usb_host_use_combining()
369 type = usb_ep_get_type(ep->dev, ep->pid, ep->nr); in usb_host_use_combining()
376 /* ------------------------------------------------------------------------ */
383 r->host = s; in usb_host_req_alloc()
384 r->p = p; in usb_host_req_alloc()
385 r->in = in; in usb_host_req_alloc()
386 r->xfer = libusb_alloc_transfer(0); in usb_host_req_alloc()
388 r->buffer = g_malloc(bufsize); in usb_host_req_alloc()
390 QTAILQ_INSERT_TAIL(&s->requests, r, next); in usb_host_req_alloc()
401 request_count--; in usb_host_req_free()
403 QTAILQ_REMOVE(&r->host->requests, r, next); in usb_host_req_free()
404 libusb_free_transfer(r->xfer); in usb_host_req_free()
405 g_free(r->buffer); in usb_host_req_free()
413 QTAILQ_FOREACH(r, &s->requests, next) { in usb_host_req_find()
414 if (r->p == p) { in usb_host_req_find()
423 USBHostRequest *r = xfer->user_data; in usb_host_req_complete_ctrl()
424 USBHostDevice *s = r->host; in usb_host_req_complete_ctrl()
425 bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE); in usb_host_req_complete_ctrl()
427 if (r->p == NULL) { in usb_host_req_complete_ctrl()
431 r->p->status = status_map[xfer->status]; in usb_host_req_complete_ctrl()
432 r->p->actual_length = xfer->actual_length; in usb_host_req_complete_ctrl()
433 if (r->in && xfer->actual_length) { in usb_host_req_complete_ctrl()
435 struct libusb_config_descriptor *conf = (void *)r->cbuf; in usb_host_req_complete_ctrl()
436 memcpy(r->cbuf, r->buffer + 8, xfer->actual_length); in usb_host_req_complete_ctrl()
438 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices in usb_host_req_complete_ctrl()
440 if (r->usb3ep0quirk && xfer->actual_length >= 18 && in usb_host_req_complete_ctrl()
441 r->cbuf[7] == 9) { in usb_host_req_complete_ctrl()
442 r->cbuf[7] = 64; in usb_host_req_complete_ctrl()
449 if (s->suppress_remote_wake && in usb_host_req_complete_ctrl()
450 udev->setup_buf[0] == USB_DIR_IN && in usb_host_req_complete_ctrl()
451 udev->setup_buf[1] == USB_REQ_GET_DESCRIPTOR && in usb_host_req_complete_ctrl()
452 udev->setup_buf[3] == USB_DT_CONFIG && udev->setup_buf[2] == 0 && in usb_host_req_complete_ctrl()
453 xfer->actual_length > in usb_host_req_complete_ctrl()
455 (conf->bmAttributes & USB_CFG_ATT_WAKEUP)) { in usb_host_req_complete_ctrl()
456 trace_usb_host_remote_wakeup_removed(s->bus_num, s->addr); in usb_host_req_complete_ctrl()
457 conf->bmAttributes &= ~USB_CFG_ATT_WAKEUP; in usb_host_req_complete_ctrl()
460 trace_usb_host_req_complete(s->bus_num, s->addr, r->p, in usb_host_req_complete_ctrl()
461 r->p->status, r->p->actual_length); in usb_host_req_complete_ctrl()
462 usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p); in usb_host_req_complete_ctrl()
473 USBHostRequest *r = xfer->user_data; in usb_host_req_complete_data()
474 USBHostDevice *s = r->host; in usb_host_req_complete_data()
475 bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE); in usb_host_req_complete_data()
477 if (r->p == NULL) { in usb_host_req_complete_data()
481 r->p->status = status_map[xfer->status]; in usb_host_req_complete_data()
482 if (r->in && xfer->actual_length) { in usb_host_req_complete_data()
483 usb_packet_copy(r->p, r->buffer, xfer->actual_length); in usb_host_req_complete_data()
485 trace_usb_host_req_complete(s->bus_num, s->addr, r->p, in usb_host_req_complete_data()
486 r->p->status, r->p->actual_length); in usb_host_req_complete_data()
487 if (usb_host_use_combining(r->p->ep)) { in usb_host_req_complete_data()
488 usb_combined_input_packet_complete(USB_DEVICE(s), r->p); in usb_host_req_complete_data()
490 usb_packet_complete(USB_DEVICE(s), r->p); in usb_host_req_complete_data()
502 USBHostDevice *s = r->host; in usb_host_req_abort()
503 bool inflight = (r->p && r->p->state == USB_PACKET_ASYNC); in usb_host_req_abort()
506 r->p->status = USB_RET_NODEV; in usb_host_req_abort()
507 trace_usb_host_req_complete(s->bus_num, s->addr, r->p, in usb_host_req_abort()
508 r->p->status, r->p->actual_length); in usb_host_req_abort()
509 if (r->p->ep->nr == 0) { in usb_host_req_abort()
510 usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p); in usb_host_req_abort()
512 usb_packet_complete(USB_DEVICE(s), r->p); in usb_host_req_abort()
514 r->p = NULL; in usb_host_req_abort()
516 libusb_cancel_transfer(r->xfer); in usb_host_req_abort()
520 /* ------------------------------------------------------------------------ */
525 USBHostIsoXfer *xfer = transfer->user_data; in usb_host_req_complete_iso()
529 g_free(transfer->buffer); in usb_host_req_complete_iso()
534 QTAILQ_REMOVE(&xfer->ring->inflight, xfer, next); in usb_host_req_complete_iso()
535 if (QTAILQ_EMPTY(&xfer->ring->inflight)) { in usb_host_req_complete_iso()
536 USBHostDevice *s = xfer->ring->host; in usb_host_req_complete_iso()
537 trace_usb_host_iso_stop(s->bus_num, s->addr, xfer->ring->ep->nr); in usb_host_req_complete_iso()
539 if (xfer->ring->ep->pid == USB_TOKEN_IN) { in usb_host_req_complete_iso()
540 QTAILQ_INSERT_TAIL(&xfer->ring->copy, xfer, next); in usb_host_req_complete_iso()
541 usb_wakeup(xfer->ring->ep, 0); in usb_host_req_complete_iso()
543 QTAILQ_INSERT_TAIL(&xfer->ring->unused, xfer, next); in usb_host_req_complete_iso()
552 int packets = s->iso_urb_frames; in usb_host_iso_alloc()
555 ring->host = s; in usb_host_iso_alloc()
556 ring->ep = ep; in usb_host_iso_alloc()
557 QTAILQ_INIT(&ring->unused); in usb_host_iso_alloc()
558 QTAILQ_INIT(&ring->inflight); in usb_host_iso_alloc()
559 QTAILQ_INIT(&ring->copy); in usb_host_iso_alloc()
560 QTAILQ_INSERT_TAIL(&s->isorings, ring, next); in usb_host_iso_alloc()
562 for (i = 0; i < s->iso_urb_count; i++) { in usb_host_iso_alloc()
564 xfer->ring = ring; in usb_host_iso_alloc()
565 xfer->xfer = libusb_alloc_transfer(packets); in usb_host_iso_alloc()
566 xfer->xfer->dev_handle = s->dh; in usb_host_iso_alloc()
567 xfer->xfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; in usb_host_iso_alloc()
569 xfer->xfer->endpoint = ring->ep->nr; in usb_host_iso_alloc()
570 if (ring->ep->pid == USB_TOKEN_IN) { in usb_host_iso_alloc()
571 xfer->xfer->endpoint |= USB_DIR_IN; in usb_host_iso_alloc()
573 xfer->xfer->callback = usb_host_req_complete_iso; in usb_host_iso_alloc()
574 xfer->xfer->user_data = xfer; in usb_host_iso_alloc()
576 xfer->xfer->num_iso_packets = packets; in usb_host_iso_alloc()
577 xfer->xfer->length = ring->ep->max_packet_size * packets; in usb_host_iso_alloc()
578 xfer->xfer->buffer = g_malloc0(xfer->xfer->length); in usb_host_iso_alloc()
580 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next); in usb_host_iso_alloc()
590 QTAILQ_FOREACH(ring, &s->isorings, next) { in usb_host_iso_find()
591 if (ring->ep == ep) { in usb_host_iso_find()
600 libusb_set_iso_packet_lengths(xfer->xfer, in usb_host_iso_reset_xfer()
601 xfer->ring->ep->max_packet_size); in usb_host_iso_reset_xfer()
602 xfer->packet = 0; in usb_host_iso_reset_xfer()
603 xfer->copy_complete = false; in usb_host_iso_reset_xfer()
609 xfer->xfer->user_data = NULL; in usb_host_iso_free_xfer()
611 g_free(xfer->xfer->buffer); in usb_host_iso_free_xfer()
612 libusb_free_transfer(xfer->xfer); in usb_host_iso_free_xfer()
621 while ((xfer = QTAILQ_FIRST(&ring->inflight)) != NULL) { in usb_host_iso_free()
622 QTAILQ_REMOVE(&ring->inflight, xfer, next); in usb_host_iso_free()
625 while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) { in usb_host_iso_free()
626 QTAILQ_REMOVE(&ring->unused, xfer, next); in usb_host_iso_free()
629 while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL) { in usb_host_iso_free()
630 QTAILQ_REMOVE(&ring->copy, xfer, next); in usb_host_iso_free()
634 QTAILQ_REMOVE(&ring->host->isorings, ring, next); in usb_host_iso_free()
642 while ((ring = QTAILQ_FIRST(&s->isorings)) != NULL) { in usb_host_iso_free_all()
652 buf = libusb_get_iso_packet_buffer_simple(xfer->xfer, xfer->packet); in usb_host_iso_data_copy()
653 if (p->pid == USB_TOKEN_OUT) { in usb_host_iso_data_copy()
654 psize = p->iov.size; in usb_host_iso_data_copy()
655 if (psize > xfer->ring->ep->max_packet_size) { in usb_host_iso_data_copy()
657 psize = xfer->ring->ep->max_packet_size; in usb_host_iso_data_copy()
659 xfer->xfer->iso_packet_desc[xfer->packet].length = psize; in usb_host_iso_data_copy()
661 psize = xfer->xfer->iso_packet_desc[xfer->packet].actual_length; in usb_host_iso_data_copy()
662 if (psize > p->iov.size) { in usb_host_iso_data_copy()
664 psize = p->iov.size; in usb_host_iso_data_copy()
668 xfer->packet++; in usb_host_iso_data_copy()
669 xfer->copy_complete = (xfer->packet == xfer->xfer->num_iso_packets); in usb_host_iso_data_copy()
670 return xfer->copy_complete; in usb_host_iso_data_copy()
680 ring = usb_host_iso_find(s, p->ep); in usb_host_iso_data_in()
682 ring = usb_host_iso_alloc(s, p->ep); in usb_host_iso_data_in()
686 xfer = QTAILQ_FIRST(&ring->copy); in usb_host_iso_data_in()
689 QTAILQ_REMOVE(&ring->copy, xfer, next); in usb_host_iso_data_in()
690 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next); in usb_host_iso_data_in()
695 while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) { in usb_host_iso_data_in()
696 QTAILQ_REMOVE(&ring->unused, xfer, next); in usb_host_iso_data_in()
698 rc = libusb_submit_transfer(xfer->xfer); in usb_host_iso_data_in()
701 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next); in usb_host_iso_data_in()
707 if (QTAILQ_EMPTY(&ring->inflight)) { in usb_host_iso_data_in()
708 trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr); in usb_host_iso_data_in()
710 QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next); in usb_host_iso_data_in()
725 ring = usb_host_iso_find(s, p->ep); in usb_host_iso_data_out()
727 ring = usb_host_iso_alloc(s, p->ep); in usb_host_iso_data_out()
731 xfer = QTAILQ_FIRST(&ring->copy); in usb_host_iso_data_out()
732 while (xfer != NULL && xfer->copy_complete) { in usb_host_iso_data_out()
737 xfer = QTAILQ_FIRST(&ring->unused); in usb_host_iso_data_out()
739 trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, p->ep->nr); in usb_host_iso_data_out()
742 QTAILQ_REMOVE(&ring->unused, xfer, next); in usb_host_iso_data_out()
744 QTAILQ_INSERT_TAIL(&ring->copy, xfer, next); in usb_host_iso_data_out()
748 if (QTAILQ_EMPTY(&ring->inflight)) { in usb_host_iso_data_out()
751 if (filled*2 < s->iso_urb_count) { in usb_host_iso_data_out()
757 while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL && in usb_host_iso_data_out()
758 xfer->copy_complete) { in usb_host_iso_data_out()
759 QTAILQ_REMOVE(&ring->copy, xfer, next); in usb_host_iso_data_out()
760 rc = libusb_submit_transfer(xfer->xfer); in usb_host_iso_data_out()
763 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next); in usb_host_iso_data_out()
769 if (QTAILQ_EMPTY(&ring->inflight)) { in usb_host_iso_data_out()
770 trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr); in usb_host_iso_data_out()
772 QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next); in usb_host_iso_data_out()
780 /* ------------------------------------------------------------------------ */
797 rc = libusb_get_config_descriptor(s->dev, c, &conf); in usb_host_speed_compat()
801 for (i = 0; i < conf->bNumInterfaces; i++) { in usb_host_speed_compat()
802 for (a = 0; a < conf->interface[i].num_altsetting; a++) { in usb_host_speed_compat()
803 intf = &conf->interface[i].altsetting[a]; in usb_host_speed_compat()
805 if (intf->bInterfaceClass == LIBUSB_CLASS_MASS_STORAGE && in usb_host_speed_compat()
806 intf->bInterfaceSubClass == 6) { /* SCSI */ in usb_host_speed_compat()
807 udev->flags |= (1 << USB_DEV_FLAG_IS_SCSI_STORAGE); in usb_host_speed_compat()
811 for (e = 0; e < intf->bNumEndpoints; e++) { in usb_host_speed_compat()
812 endp = &intf->endpoint[e]; in usb_host_speed_compat()
813 type = endp->bmAttributes & 0x3; in usb_host_speed_compat()
824 int streams = endp_ss_comp->bmAttributes & 0x1f; in usb_host_speed_compat()
835 if (endp->wMaxPacketSize > 64) { in usb_host_speed_compat()
838 if (endp->wMaxPacketSize > 1024) { in usb_host_speed_compat()
849 udev->speedmask = (1 << udev->speed); in usb_host_speed_compat()
850 if (udev->speed == USB_SPEED_SUPER && compat_high) { in usb_host_speed_compat()
851 udev->speedmask |= USB_SPEED_MASK_HIGH; in usb_host_speed_compat()
853 if (udev->speed == USB_SPEED_SUPER && compat_full) { in usb_host_speed_compat()
854 udev->speedmask |= USB_SPEED_MASK_FULL; in usb_host_speed_compat()
856 if (udev->speed == USB_SPEED_HIGH && compat_full) { in usb_host_speed_compat()
857 udev->speedmask |= USB_SPEED_MASK_FULL; in usb_host_speed_compat()
881 rc = libusb_get_active_config_descriptor(s->dev, &conf); in usb_host_ep_update()
885 trace_usb_host_parse_config(s->bus_num, s->addr, in usb_host_ep_update()
886 conf->bConfigurationValue, true); in usb_host_ep_update()
888 for (i = 0; i < conf->bNumInterfaces; i++) { in usb_host_ep_update()
890 * The udev->altsetting array indexes alternate settings in usb_host_ep_update()
895 intf = &conf->interface[i].altsetting[0]; in usb_host_ep_update()
896 alt = udev->altsetting[intf->bInterfaceNumber]; in usb_host_ep_update()
899 assert(alt < conf->interface[i].num_altsetting); in usb_host_ep_update()
900 intf = &conf->interface[i].altsetting[alt]; in usb_host_ep_update()
903 trace_usb_host_parse_interface(s->bus_num, s->addr, in usb_host_ep_update()
904 intf->bInterfaceNumber, in usb_host_ep_update()
905 intf->bAlternateSetting, true); in usb_host_ep_update()
906 for (e = 0; e < intf->bNumEndpoints; e++) { in usb_host_ep_update()
907 endp = &intf->endpoint[e]; in usb_host_ep_update()
909 devep = endp->bEndpointAddress; in usb_host_ep_update()
912 type = endp->bmAttributes & 0x3; in usb_host_ep_update()
915 trace_usb_host_parse_error(s->bus_num, s->addr, in usb_host_ep_update()
920 trace_usb_host_parse_error(s->bus_num, s->addr, in usb_host_ep_update()
925 trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep, in usb_host_ep_update()
929 endp->wMaxPacketSize); in usb_host_ep_update()
938 endp_ss_comp->bmAttributes); in usb_host_ep_update()
948 static int usb_host_open(USBHostDevice *s, libusb_device *dev, int hostfd) in usb_host_open() argument
957 if (s->bh_postld_pending) { in usb_host_open()
958 return -1; in usb_host_open()
960 if (s->dh != NULL) { in usb_host_open()
964 if (dev) { in usb_host_open()
965 bus_num = libusb_get_bus_number(dev); in usb_host_open()
966 addr = libusb_get_device_address(dev); in usb_host_open()
969 rc = libusb_open(dev, &s->dh); in usb_host_open()
977 rc = libusb_wrap_sys_device(ctx, hostfd, &s->dh); in usb_host_open()
981 s->hostfd = hostfd; in usb_host_open()
982 dev = libusb_get_device(s->dh); in usb_host_open()
983 bus_num = libusb_get_bus_number(dev); in usb_host_open()
984 addr = libusb_get_device_address(dev); in usb_host_open()
990 s->dev = dev; in usb_host_open()
991 s->bus_num = bus_num; in usb_host_open()
992 s->addr = addr; in usb_host_open()
996 libusb_get_device_descriptor(dev, &s->ddesc); in usb_host_open()
997 usb_host_get_port(s->dev, s->port, sizeof(s->port)); in usb_host_open()
1002 libusb_speed = libusb_get_device_speed(dev); in usb_host_open()
1007 * Workaround libusb bug: libusb_get_device_speed() does not in usb_host_open()
1038 udev->speed = speed_map[libusb_speed]; in usb_host_open()
1041 if (s->ddesc.iProduct) { in usb_host_open()
1042 libusb_get_string_descriptor_ascii(s->dh, s->ddesc.iProduct, in usb_host_open()
1043 (unsigned char *)udev->product_desc, in usb_host_open()
1044 sizeof(udev->product_desc)); in usb_host_open()
1046 snprintf(udev->product_desc, sizeof(udev->product_desc), in usb_host_open()
1061 if (s->dh != NULL) { in usb_host_open()
1063 libusb_reset_device(s->dh); in usb_host_open()
1065 libusb_close(s->dh); in usb_host_open()
1066 s->dh = NULL; in usb_host_open()
1067 s->dev = NULL; in usb_host_open()
1069 return -1; in usb_host_open()
1077 QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) { in usb_host_abort_xfers()
1081 while (QTAILQ_FIRST(&s->requests) != NULL) { in usb_host_abort_xfers()
1086 if (--limit == 0) { in usb_host_abort_xfers()
1088 * Don't wait forever for libusb calling the complete in usb_host_abort_xfers()
1091 * Leaking memory here, to make sure libusb will not in usb_host_abort_xfers()
1094 QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) { in usb_host_abort_xfers()
1095 QTAILQ_REMOVE(&s->requests, r, next); in usb_host_abort_xfers()
1106 if (s->dh == NULL) { in usb_host_close()
1107 return -1; in usb_host_close()
1110 trace_usb_host_close(s->bus_num, s->addr); in usb_host_close()
1115 if (udev->attached) { in usb_host_close()
1120 libusb_reset_device(s->dh); in usb_host_close()
1122 libusb_close(s->dh); in usb_host_close()
1123 s->dh = NULL; in usb_host_close()
1124 s->dev = NULL; in usb_host_close()
1126 if (s->hostfd != -1) { in usb_host_close()
1127 close(s->hostfd); in usb_host_close()
1128 s->hostfd = -1; in usb_host_close()
1143 if (!s->bh_nodev) { in usb_host_nodev()
1144 s->bh_nodev = qemu_bh_new_guarded(usb_host_nodev_bh, s, in usb_host_nodev()
1145 &DEVICE(s)->mem_reentrancy_guard); in usb_host_nodev()
1147 qemu_bh_schedule(s->bh_nodev); in usb_host_nodev()
1154 if (s->dh) { in usb_host_exit_notifier()
1157 libusb_reset_device(s->dh); in usb_host_exit_notifier()
1159 libusb_close(s->dh); in usb_host_exit_notifier()
1188 error_setg(errp, "failed to init libusb"); in usb_host_realize()
1191 if (s->match.vendor_id > 0xffff) { in usb_host_realize()
1195 if (s->match.product_id > 0xffff) { in usb_host_realize()
1199 if (s->match.addr > 127) { in usb_host_realize()
1204 loglevel = s->loglevel; in usb_host_realize()
1205 udev->flags |= (1 << USB_DEV_FLAG_IS_HOST); in usb_host_realize()
1206 udev->auto_attach = 0; in usb_host_realize()
1207 QTAILQ_INIT(&s->requests); in usb_host_realize()
1208 QTAILQ_INIT(&s->isorings); in usb_host_realize()
1209 s->hostfd = -1; in usb_host_realize()
1212 if (s->hostdevice) { in usb_host_realize()
1214 s->needs_autoscan = false; in usb_host_realize()
1215 fd = qemu_open(s->hostdevice, O_RDWR, errp); in usb_host_realize()
1221 error_setg(errp, "failed to open host usb device %s", s->hostdevice); in usb_host_realize()
1226 if (s->match.addr && s->match.bus_num && in usb_host_realize()
1227 !s->match.vendor_id && in usb_host_realize()
1228 !s->match.product_id && in usb_host_realize()
1229 !s->match.port) { in usb_host_realize()
1230 s->needs_autoscan = false; in usb_host_realize()
1231 ldev = usb_host_find_ref(s->match.bus_num, in usb_host_realize()
1232 s->match.addr); in usb_host_realize()
1235 s->match.bus_num, s->match.addr); in usb_host_realize()
1242 s->match.bus_num, s->match.addr); in usb_host_realize()
1246 s->needs_autoscan = true; in usb_host_realize()
1251 s->exit.notify = usb_host_exit_notifier; in usb_host_realize()
1252 qemu_add_exit_notifier(&s->exit); in usb_host_realize()
1260 device_add_bootindex_property(obj, &s->bootindex, in usb_host_instance_init()
1262 &udev->qdev); in usb_host_instance_init()
1269 qemu_remove_exit_notifier(&s->exit); in usb_host_unrealize()
1270 if (s->needs_autoscan) { in usb_host_unrealize()
1281 if (p->combined) { in usb_host_cancel_packet()
1286 trace_usb_host_req_canceled(s->bus_num, s->addr, p); in usb_host_cancel_packet()
1289 if (r && r->p) { in usb_host_cancel_packet()
1290 r->p = NULL; /* mark as dead */ in usb_host_cancel_packet()
1291 libusb_cancel_transfer(r->xfer); in usb_host_cancel_packet()
1300 rc = libusb_get_active_config_descriptor(s->dev, &conf); in usb_host_detach_kernel()
1305 rc = libusb_kernel_driver_active(s->dh, i); in usb_host_detach_kernel()
1309 s->ifs[i].detached = true; in usb_host_detach_kernel()
1313 trace_usb_host_detach_kernel(s->bus_num, s->addr, i); in usb_host_detach_kernel()
1314 rc = libusb_detach_kernel_driver(s->dh, i); in usb_host_detach_kernel()
1316 s->ifs[i].detached = true; in usb_host_detach_kernel()
1326 rc = libusb_get_active_config_descriptor(s->dev, &conf); in usb_host_attach_kernel()
1331 if (!s->ifs[i].detached) { in usb_host_attach_kernel()
1334 trace_usb_host_attach_kernel(s->bus_num, s->addr, i); in usb_host_attach_kernel()
1335 libusb_attach_kernel_driver(s->dh, i); in usb_host_attach_kernel()
1336 s->ifs[i].detached = false; in usb_host_attach_kernel()
1348 udev->altsetting[i] = 0; in usb_host_claim_interfaces()
1350 udev->ninterfaces = 0; in usb_host_claim_interfaces()
1351 udev->configuration = 0; in usb_host_claim_interfaces()
1355 rc = libusb_get_active_config_descriptor(s->dev, &conf); in usb_host_claim_interfaces()
1358 /* address state - ignore */ in usb_host_claim_interfaces()
1366 trace_usb_host_claim_interface(s->bus_num, s->addr, configuration, i); in usb_host_claim_interfaces()
1367 rc = libusb_claim_interface(s->dh, i); in usb_host_claim_interfaces()
1369 s->ifs[i].claimed = true; in usb_host_claim_interfaces()
1370 if (++claimed == conf->bNumInterfaces) { in usb_host_claim_interfaces()
1375 if (claimed != conf->bNumInterfaces) { in usb_host_claim_interfaces()
1379 udev->ninterfaces = conf->bNumInterfaces; in usb_host_claim_interfaces()
1380 udev->configuration = configuration; in usb_host_claim_interfaces()
1391 if (!s->ifs[i].claimed) { in usb_host_release_interfaces()
1394 trace_usb_host_release_interface(s->bus_num, s->addr, i); in usb_host_release_interfaces()
1395 rc = libusb_release_interface(s->dh, i); in usb_host_release_interfaces()
1397 s->ifs[i].claimed = false; in usb_host_release_interfaces()
1405 trace_usb_host_set_address(s->bus_num, s->addr, addr); in usb_host_set_address()
1406 udev->addr = addr; in usb_host_set_address()
1413 trace_usb_host_set_config(s->bus_num, s->addr, config); in usb_host_set_config()
1416 if (s->ddesc.bNumConfigurations != 1) { in usb_host_set_config()
1417 rc = libusb_set_configuration(s->dh, config); in usb_host_set_config()
1420 p->status = USB_RET_STALL; in usb_host_set_config()
1427 p->status = usb_host_claim_interfaces(s, config); in usb_host_set_config()
1428 if (p->status != USB_RET_SUCCESS) { in usb_host_set_config()
1440 trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt); in usb_host_set_interface()
1445 p->status = USB_RET_STALL; in usb_host_set_interface()
1449 rc = libusb_set_interface_alt_setting(s->dh, iface, alt); in usb_host_set_interface()
1452 p->status = USB_RET_STALL; in usb_host_set_interface()
1459 udev->altsetting[iface] = alt; in usb_host_set_interface()
1471 trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index); in usb_host_handle_control()
1473 if (s->dh == NULL) { in usb_host_handle_control()
1474 p->status = USB_RET_NODEV; in usb_host_handle_control()
1475 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); in usb_host_handle_control()
1482 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); in usb_host_handle_control()
1487 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); in usb_host_handle_control()
1492 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); in usb_host_handle_control()
1498 libusb_clear_halt(s->dh, index); in usb_host_handle_control()
1500 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); in usb_host_handle_control()
1506 r->cbuf = data; in usb_host_handle_control()
1507 r->clen = length; in usb_host_handle_control()
1508 memcpy(r->buffer, udev->setup_buf, 8); in usb_host_handle_control()
1509 if (!r->in) { in usb_host_handle_control()
1510 memcpy(r->buffer + 8, r->cbuf, r->clen); in usb_host_handle_control()
1513 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices in usb_host_handle_control()
1515 if ((udev->speedmask & USB_SPEED_MASK_SUPER) && in usb_host_handle_control()
1516 !(udev->port->speedmask & USB_SPEED_MASK_SUPER) && in usb_host_handle_control()
1518 r->usb3ep0quirk = true; in usb_host_handle_control()
1521 libusb_fill_control_transfer(r->xfer, s->dh, r->buffer, in usb_host_handle_control()
1524 rc = libusb_submit_transfer(r->xfer); in usb_host_handle_control()
1526 p->status = USB_RET_NODEV; in usb_host_handle_control()
1527 trace_usb_host_req_complete(s->bus_num, s->addr, p, in usb_host_handle_control()
1528 p->status, p->actual_length); in usb_host_handle_control()
1535 p->status = USB_RET_ASYNC; in usb_host_handle_control()
1545 if (usb_host_use_combining(p->ep) && p->state == USB_PACKET_SETUP) { in usb_host_handle_data()
1546 p->status = USB_RET_ADD_TO_QUEUE; in usb_host_handle_data()
1550 trace_usb_host_req_data(s->bus_num, s->addr, p, in usb_host_handle_data()
1551 p->pid == USB_TOKEN_IN, in usb_host_handle_data()
1552 p->ep->nr, p->iov.size); in usb_host_handle_data()
1554 if (s->dh == NULL) { in usb_host_handle_data()
1555 p->status = USB_RET_NODEV; in usb_host_handle_data()
1556 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); in usb_host_handle_data()
1559 if (p->ep->halted) { in usb_host_handle_data()
1560 p->status = USB_RET_STALL; in usb_host_handle_data()
1561 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); in usb_host_handle_data()
1565 switch (usb_ep_get_type(udev, p->pid, p->ep->nr)) { in usb_host_handle_data()
1568 r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, size); in usb_host_handle_data()
1569 if (!r->in) { in usb_host_handle_data()
1570 usb_packet_copy(p, r->buffer, size); in usb_host_handle_data()
1572 ep = p->ep->nr | (r->in ? USB_DIR_IN : 0); in usb_host_handle_data()
1573 if (p->stream) { in usb_host_handle_data()
1575 libusb_fill_bulk_stream_transfer(r->xfer, s->dh, ep, p->stream, in usb_host_handle_data()
1576 r->buffer, size, in usb_host_handle_data()
1581 p->status = USB_RET_STALL; in usb_host_handle_data()
1585 libusb_fill_bulk_transfer(r->xfer, s->dh, ep, in usb_host_handle_data()
1586 r->buffer, size, in usb_host_handle_data()
1592 r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, p->iov.size); in usb_host_handle_data()
1593 if (!r->in) { in usb_host_handle_data()
1594 usb_packet_copy(p, r->buffer, p->iov.size); in usb_host_handle_data()
1596 ep = p->ep->nr | (r->in ? USB_DIR_IN : 0); in usb_host_handle_data()
1597 libusb_fill_interrupt_transfer(r->xfer, s->dh, ep, in usb_host_handle_data()
1598 r->buffer, p->iov.size, in usb_host_handle_data()
1603 if (p->pid == USB_TOKEN_IN) { in usb_host_handle_data()
1608 trace_usb_host_req_complete(s->bus_num, s->addr, p, in usb_host_handle_data()
1609 p->status, p->actual_length); in usb_host_handle_data()
1612 p->status = USB_RET_STALL; in usb_host_handle_data()
1613 trace_usb_host_req_complete(s->bus_num, s->addr, p, in usb_host_handle_data()
1614 p->status, p->actual_length); in usb_host_handle_data()
1618 rc = libusb_submit_transfer(r->xfer); in usb_host_handle_data()
1620 p->status = USB_RET_NODEV; in usb_host_handle_data()
1621 trace_usb_host_req_complete(s->bus_num, s->addr, p, in usb_host_handle_data()
1622 p->status, p->actual_length); in usb_host_handle_data()
1629 p->status = USB_RET_ASYNC; in usb_host_handle_data()
1632 static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep) in usb_host_flush_ep_queue() argument
1644 if (!s->allow_one_guest_reset && !s->allow_all_guest_resets) { in usb_host_handle_reset()
1647 if (!s->allow_all_guest_resets && udev->addr == 0) { in usb_host_handle_reset()
1651 trace_usb_host_reset(s->bus_num, s->addr); in usb_host_handle_reset()
1653 rc = libusb_reset_device(s->dh); in usb_host_handle_reset()
1668 endpoints[i] = eps[i]->nr; in usb_host_alloc_streams()
1669 if (eps[i]->pid == USB_TOKEN_IN) { in usb_host_alloc_streams()
1673 rc = libusb_alloc_streams(s->dh, streams, endpoints, nr_eps); in usb_host_alloc_streams()
1681 return (rc == streams) ? 0 : -1; in usb_host_alloc_streams()
1684 return -1; in usb_host_alloc_streams()
1697 endpoints[i] = eps[i]->nr; in usb_host_free_streams()
1698 if (eps[i]->pid == USB_TOKEN_IN) { in usb_host_free_streams()
1702 libusb_free_streams(s->dh, endpoints, nr_eps); in usb_host_free_streams()
1714 * will get re-attached (and re-initialized by the guest) in case it
1724 USBHostDevice *dev = opaque; in usb_host_post_load_bh() local
1725 USBDevice *udev = USB_DEVICE(dev); in usb_host_post_load_bh()
1727 if (dev->dh != NULL) { in usb_host_post_load_bh()
1728 usb_host_close(dev); in usb_host_post_load_bh()
1730 if (udev->attached) { in usb_host_post_load_bh()
1733 dev->bh_postld_pending = false; in usb_host_post_load_bh()
1739 USBHostDevice *dev = opaque; in usb_host_post_load() local
1741 if (!dev->bh_postld) { in usb_host_post_load()
1742 dev->bh_postld = qemu_bh_new_guarded(usb_host_post_load_bh, dev, in usb_host_post_load()
1743 &DEVICE(dev)->mem_reentrancy_guard); in usb_host_post_load()
1745 qemu_bh_schedule(dev->bh_postld); in usb_host_post_load()
1746 dev->bh_postld_pending = true; in usb_host_post_load()
1751 .name = "usb-host",
1772 DEFINE_PROP_BOOL("guest-reset", USBHostDevice,
1774 DEFINE_PROP_BOOL("guest-resets-all", USBHostDevice,
1780 DEFINE_PROP_BOOL("suppress-remote-wake", USBHostDevice,
1790 uc->realize = usb_host_realize; in usb_host_class_initfn()
1791 uc->product_desc = "USB Host Device"; in usb_host_class_initfn()
1792 uc->cancel_packet = usb_host_cancel_packet; in usb_host_class_initfn()
1793 uc->handle_data = usb_host_handle_data; in usb_host_class_initfn()
1794 uc->handle_control = usb_host_handle_control; in usb_host_class_initfn()
1795 uc->handle_reset = usb_host_handle_reset; in usb_host_class_initfn()
1796 uc->unrealize = usb_host_unrealize; in usb_host_class_initfn()
1797 uc->flush_ep_queue = usb_host_flush_ep_queue; in usb_host_class_initfn()
1798 uc->alloc_streams = usb_host_alloc_streams; in usb_host_class_initfn()
1799 uc->free_streams = usb_host_free_streams; in usb_host_class_initfn()
1800 dc->vmsd = &vmstate_usb_host; in usb_host_class_initfn()
1802 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); in usb_host_class_initfn()
1823 /* ------------------------------------------------------------------------ */
1857 f = &s->match; in usb_host_auto_check()
1858 if (f->bus_num > 0 && in usb_host_auto_check()
1859 f->bus_num != libusb_get_bus_number(devs[i])) { in usb_host_auto_check()
1862 if (f->addr > 0 && in usb_host_auto_check()
1863 f->addr != libusb_get_device_address(devs[i])) { in usb_host_auto_check()
1866 if (f->port != NULL) { in usb_host_auto_check()
1867 char port[16] = "-"; in usb_host_auto_check()
1869 if (strcmp(f->port, port) != 0) { in usb_host_auto_check()
1873 if (f->vendor_id > 0 && in usb_host_auto_check()
1874 f->vendor_id != ddesc.idVendor) { in usb_host_auto_check()
1877 if (f->product_id > 0 && in usb_host_auto_check()
1878 f->product_id != ddesc.idProduct) { in usb_host_auto_check()
1883 s->seen++; in usb_host_auto_check()
1884 if (s->errcount >= 3) { in usb_host_auto_check()
1887 if (s->dh != NULL) { in usb_host_auto_check()
1891 s->errcount++; in usb_host_auto_check()
1900 if (s->seen == 0) { in usb_host_auto_check()
1901 if (s->dh) { in usb_host_auto_check()
1904 s->errcount = 0; in usb_host_auto_check()
1906 s->seen = 0; in usb_host_auto_check()