2
0

host-legacy.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Linux host USB redirector
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. *
  6. * Copyright (c) 2008 Max Krasnyansky
  7. * Support for host device auto connect & disconnect
  8. * Major rewrite to support fully async operation
  9. *
  10. * Copyright 2008 TJ <linux@tjworld.net>
  11. * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
  12. * to the legacy /proc/bus/usb USB device discovery and handling
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. * THE SOFTWARE.
  31. */
  32. #include "qemu-common.h"
  33. #include "hw/usb.h"
  34. #include "hw/usb/host.h"
  35. /*
  36. * Autoconnect filter
  37. * Format:
  38. * auto:bus:dev[:vid:pid]
  39. * auto:bus.dev[:vid:pid]
  40. *
  41. * bus - bus number (dec, * means any)
  42. * dev - device number (dec, * means any)
  43. * vid - vendor id (hex, * means any)
  44. * pid - product id (hex, * means any)
  45. *
  46. * See 'lsusb' output.
  47. */
  48. static int parse_filter(const char *spec, struct USBAutoFilter *f)
  49. {
  50. enum { BUS, DEV, VID, PID, DONE };
  51. const char *p = spec;
  52. int i;
  53. f->bus_num = 0;
  54. f->addr = 0;
  55. f->vendor_id = 0;
  56. f->product_id = 0;
  57. for (i = BUS; i < DONE; i++) {
  58. p = strpbrk(p, ":.");
  59. if (!p) {
  60. break;
  61. }
  62. p++;
  63. if (*p == '*') {
  64. continue;
  65. }
  66. switch (i) {
  67. case BUS:
  68. f->bus_num = strtol(p, NULL, 10);
  69. break;
  70. case DEV:
  71. f->addr = strtol(p, NULL, 10);
  72. break;
  73. case VID:
  74. f->vendor_id = strtol(p, NULL, 16);
  75. break;
  76. case PID:
  77. f->product_id = strtol(p, NULL, 16);
  78. break;
  79. }
  80. }
  81. if (i < DEV) {
  82. fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
  83. return -1;
  84. }
  85. return 0;
  86. }
  87. USBDevice *usb_host_device_open(USBBus *bus, const char *devname)
  88. {
  89. struct USBAutoFilter filter;
  90. USBDevice *dev;
  91. char *p;
  92. dev = usb_create(bus, "usb-host");
  93. if (strstr(devname, "auto:")) {
  94. if (parse_filter(devname, &filter) < 0) {
  95. goto fail;
  96. }
  97. } else {
  98. p = strchr(devname, '.');
  99. if (p) {
  100. filter.bus_num = strtoul(devname, NULL, 0);
  101. filter.addr = strtoul(p + 1, NULL, 0);
  102. filter.vendor_id = 0;
  103. filter.product_id = 0;
  104. } else {
  105. p = strchr(devname, ':');
  106. if (p) {
  107. filter.bus_num = 0;
  108. filter.addr = 0;
  109. filter.vendor_id = strtoul(devname, NULL, 16);
  110. filter.product_id = strtoul(p + 1, NULL, 16);
  111. } else {
  112. goto fail;
  113. }
  114. }
  115. }
  116. qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
  117. qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
  118. qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
  119. qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
  120. qdev_init_nofail(&dev->qdev);
  121. return dev;
  122. fail:
  123. object_unparent(OBJECT(dev));
  124. return NULL;
  125. }
  126. static void usb_host_register_types(void)
  127. {
  128. usb_legacy_register("usb-host", "host", usb_host_device_open);
  129. }
  130. type_init(usb_host_register_types)