2
0

host-libusb.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  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. * (c) 2012 Gerd Hoffmann <kraxel@redhat.com>
  15. * Completely rewritten to use libusb instead of usbfs ioctls.
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining a copy
  18. * of this software and associated documentation files (the "Software"), to deal
  19. * in the Software without restriction, including without limitation the rights
  20. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. * copies of the Software, and to permit persons to whom the Software is
  22. * furnished to do so, subject to the following conditions:
  23. *
  24. * The above copyright notice and this permission notice shall be included in
  25. * all copies or substantial portions of the Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  33. * THE SOFTWARE.
  34. */
  35. #include "qemu/osdep.h"
  36. #include "qom/object.h"
  37. #ifndef CONFIG_WIN32
  38. #include <poll.h>
  39. #endif
  40. #include <libusb.h>
  41. #ifdef CONFIG_LINUX
  42. #include <sys/ioctl.h>
  43. #include <linux/usbdevice_fs.h>
  44. #endif
  45. #include "qapi/error.h"
  46. #include "migration/vmstate.h"
  47. #include "monitor/monitor.h"
  48. #include "qemu/error-report.h"
  49. #include "qemu/main-loop.h"
  50. #include "qemu/module.h"
  51. #include "sysemu/runstate.h"
  52. #include "sysemu/sysemu.h"
  53. #include "trace.h"
  54. #include "hw/qdev-properties.h"
  55. #include "hw/usb.h"
  56. /* ------------------------------------------------------------------------ */
  57. #define TYPE_USB_HOST_DEVICE "usb-host"
  58. OBJECT_DECLARE_SIMPLE_TYPE(USBHostDevice, USB_HOST_DEVICE)
  59. typedef struct USBHostRequest USBHostRequest;
  60. typedef struct USBHostIsoXfer USBHostIsoXfer;
  61. typedef struct USBHostIsoRing USBHostIsoRing;
  62. struct USBAutoFilter {
  63. uint32_t bus_num;
  64. uint32_t addr;
  65. char *port;
  66. uint32_t vendor_id;
  67. uint32_t product_id;
  68. };
  69. enum USBHostDeviceOptions {
  70. USB_HOST_OPT_PIPELINE,
  71. };
  72. struct USBHostDevice {
  73. USBDevice parent_obj;
  74. /* properties */
  75. struct USBAutoFilter match;
  76. char *hostdevice;
  77. int32_t bootindex;
  78. uint32_t iso_urb_count;
  79. uint32_t iso_urb_frames;
  80. uint32_t options;
  81. uint32_t loglevel;
  82. bool needs_autoscan;
  83. bool allow_one_guest_reset;
  84. bool allow_all_guest_resets;
  85. bool suppress_remote_wake;
  86. /* state */
  87. QTAILQ_ENTRY(USBHostDevice) next;
  88. int seen, errcount;
  89. int bus_num;
  90. int addr;
  91. char port[16];
  92. int hostfd;
  93. libusb_device *dev;
  94. libusb_device_handle *dh;
  95. struct libusb_device_descriptor ddesc;
  96. struct {
  97. bool detached;
  98. bool claimed;
  99. } ifs[USB_MAX_INTERFACES];
  100. /* callbacks & friends */
  101. QEMUBH *bh_nodev;
  102. QEMUBH *bh_postld;
  103. bool bh_postld_pending;
  104. Notifier exit;
  105. /* request queues */
  106. QTAILQ_HEAD(, USBHostRequest) requests;
  107. QTAILQ_HEAD(, USBHostIsoRing) isorings;
  108. };
  109. struct USBHostRequest {
  110. USBHostDevice *host;
  111. USBPacket *p;
  112. bool in;
  113. struct libusb_transfer *xfer;
  114. unsigned char *buffer;
  115. unsigned char *cbuf;
  116. unsigned int clen;
  117. bool usb3ep0quirk;
  118. QTAILQ_ENTRY(USBHostRequest) next;
  119. };
  120. struct USBHostIsoXfer {
  121. USBHostIsoRing *ring;
  122. struct libusb_transfer *xfer;
  123. bool copy_complete;
  124. unsigned int packet;
  125. QTAILQ_ENTRY(USBHostIsoXfer) next;
  126. };
  127. struct USBHostIsoRing {
  128. USBHostDevice *host;
  129. USBEndpoint *ep;
  130. QTAILQ_HEAD(, USBHostIsoXfer) unused;
  131. QTAILQ_HEAD(, USBHostIsoXfer) inflight;
  132. QTAILQ_HEAD(, USBHostIsoXfer) copy;
  133. QTAILQ_ENTRY(USBHostIsoRing) next;
  134. };
  135. static QTAILQ_HEAD(, USBHostDevice) hostdevs =
  136. QTAILQ_HEAD_INITIALIZER(hostdevs);
  137. static void usb_host_auto_check(void *unused);
  138. static void usb_host_release_interfaces(USBHostDevice *s);
  139. static void usb_host_nodev(USBHostDevice *s);
  140. static void usb_host_detach_kernel(USBHostDevice *s);
  141. static void usb_host_attach_kernel(USBHostDevice *s);
  142. /* ------------------------------------------------------------------------ */
  143. #ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */
  144. #define LIBUSB_LOG_LEVEL_WARNING 2
  145. #endif
  146. /* ------------------------------------------------------------------------ */
  147. #define CONTROL_TIMEOUT 10000 /* 10 sec */
  148. #define BULK_TIMEOUT 0 /* unlimited */
  149. #define INTR_TIMEOUT 0 /* unlimited */
  150. #ifndef LIBUSB_API_VERSION
  151. # define LIBUSB_API_VERSION LIBUSBX_API_VERSION
  152. #endif
  153. #if LIBUSB_API_VERSION >= 0x01000103
  154. # define HAVE_STREAMS 1
  155. #endif
  156. static const char *speed_name[] = {
  157. [LIBUSB_SPEED_UNKNOWN] = "?",
  158. [LIBUSB_SPEED_LOW] = "1.5",
  159. [LIBUSB_SPEED_FULL] = "12",
  160. [LIBUSB_SPEED_HIGH] = "480",
  161. [LIBUSB_SPEED_SUPER] = "5000",
  162. };
  163. static const unsigned int speed_map[] = {
  164. [LIBUSB_SPEED_LOW] = USB_SPEED_LOW,
  165. [LIBUSB_SPEED_FULL] = USB_SPEED_FULL,
  166. [LIBUSB_SPEED_HIGH] = USB_SPEED_HIGH,
  167. [LIBUSB_SPEED_SUPER] = USB_SPEED_SUPER,
  168. };
  169. static const unsigned int status_map[] = {
  170. [LIBUSB_TRANSFER_COMPLETED] = USB_RET_SUCCESS,
  171. [LIBUSB_TRANSFER_ERROR] = USB_RET_IOERROR,
  172. [LIBUSB_TRANSFER_TIMED_OUT] = USB_RET_IOERROR,
  173. [LIBUSB_TRANSFER_CANCELLED] = USB_RET_IOERROR,
  174. [LIBUSB_TRANSFER_STALL] = USB_RET_STALL,
  175. [LIBUSB_TRANSFER_NO_DEVICE] = USB_RET_NODEV,
  176. [LIBUSB_TRANSFER_OVERFLOW] = USB_RET_BABBLE,
  177. };
  178. static const char *err_names[] = {
  179. [-LIBUSB_ERROR_IO] = "IO",
  180. [-LIBUSB_ERROR_INVALID_PARAM] = "INVALID_PARAM",
  181. [-LIBUSB_ERROR_ACCESS] = "ACCESS",
  182. [-LIBUSB_ERROR_NO_DEVICE] = "NO_DEVICE",
  183. [-LIBUSB_ERROR_NOT_FOUND] = "NOT_FOUND",
  184. [-LIBUSB_ERROR_BUSY] = "BUSY",
  185. [-LIBUSB_ERROR_TIMEOUT] = "TIMEOUT",
  186. [-LIBUSB_ERROR_OVERFLOW] = "OVERFLOW",
  187. [-LIBUSB_ERROR_PIPE] = "PIPE",
  188. [-LIBUSB_ERROR_INTERRUPTED] = "INTERRUPTED",
  189. [-LIBUSB_ERROR_NO_MEM] = "NO_MEM",
  190. [-LIBUSB_ERROR_NOT_SUPPORTED] = "NOT_SUPPORTED",
  191. [-LIBUSB_ERROR_OTHER] = "OTHER",
  192. };
  193. static libusb_context *ctx;
  194. static uint32_t loglevel;
  195. #ifndef CONFIG_WIN32
  196. static void usb_host_handle_fd(void *opaque)
  197. {
  198. struct timeval tv = { 0, 0 };
  199. libusb_handle_events_timeout(ctx, &tv);
  200. }
  201. static void usb_host_add_fd(int fd, short events, void *user_data)
  202. {
  203. qemu_set_fd_handler(fd,
  204. (events & POLLIN) ? usb_host_handle_fd : NULL,
  205. (events & POLLOUT) ? usb_host_handle_fd : NULL,
  206. ctx);
  207. }
  208. static void usb_host_del_fd(int fd, void *user_data)
  209. {
  210. qemu_set_fd_handler(fd, NULL, NULL, NULL);
  211. }
  212. #endif /* !CONFIG_WIN32 */
  213. static int usb_host_init(void)
  214. {
  215. #ifndef CONFIG_WIN32
  216. const struct libusb_pollfd **poll;
  217. #endif
  218. int rc;
  219. if (ctx) {
  220. return 0;
  221. }
  222. rc = libusb_init(&ctx);
  223. if (rc != 0) {
  224. return -1;
  225. }
  226. #if LIBUSB_API_VERSION >= 0x01000106
  227. libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, loglevel);
  228. #else
  229. libusb_set_debug(ctx, loglevel);
  230. #endif
  231. #ifdef CONFIG_WIN32
  232. /* FIXME: add support for Windows. */
  233. #else
  234. libusb_set_pollfd_notifiers(ctx, usb_host_add_fd,
  235. usb_host_del_fd,
  236. ctx);
  237. poll = libusb_get_pollfds(ctx);
  238. if (poll) {
  239. int i;
  240. for (i = 0; poll[i] != NULL; i++) {
  241. usb_host_add_fd(poll[i]->fd, poll[i]->events, ctx);
  242. }
  243. }
  244. free(poll);
  245. #endif
  246. return 0;
  247. }
  248. static int usb_host_get_port(libusb_device *dev, char *port, size_t len)
  249. {
  250. uint8_t path[7];
  251. size_t off;
  252. int rc, i;
  253. #if LIBUSB_API_VERSION >= 0x01000102
  254. rc = libusb_get_port_numbers(dev, path, 7);
  255. #else
  256. rc = libusb_get_port_path(ctx, dev, path, 7);
  257. #endif
  258. if (rc < 0) {
  259. return 0;
  260. }
  261. off = snprintf(port, len, "%d", path[0]);
  262. for (i = 1; i < rc; i++) {
  263. off += snprintf(port+off, len-off, ".%d", path[i]);
  264. }
  265. return off;
  266. }
  267. static void usb_host_libusb_error(const char *func, int rc)
  268. {
  269. const char *errname;
  270. if (rc >= 0) {
  271. return;
  272. }
  273. if (-rc < ARRAY_SIZE(err_names) && err_names[-rc]) {
  274. errname = err_names[-rc];
  275. } else {
  276. errname = "?";
  277. }
  278. error_report("%s: %d [%s]", func, rc, errname);
  279. }
  280. /* ------------------------------------------------------------------------ */
  281. static bool usb_host_use_combining(USBEndpoint *ep)
  282. {
  283. int type;
  284. if (!ep->pipeline) {
  285. return false;
  286. }
  287. if (ep->pid != USB_TOKEN_IN) {
  288. return false;
  289. }
  290. type = usb_ep_get_type(ep->dev, ep->pid, ep->nr);
  291. if (type != USB_ENDPOINT_XFER_BULK) {
  292. return false;
  293. }
  294. return true;
  295. }
  296. /* ------------------------------------------------------------------------ */
  297. static USBHostRequest *usb_host_req_alloc(USBHostDevice *s, USBPacket *p,
  298. bool in, size_t bufsize)
  299. {
  300. USBHostRequest *r = g_new0(USBHostRequest, 1);
  301. r->host = s;
  302. r->p = p;
  303. r->in = in;
  304. r->xfer = libusb_alloc_transfer(0);
  305. if (bufsize) {
  306. r->buffer = g_malloc(bufsize);
  307. }
  308. QTAILQ_INSERT_TAIL(&s->requests, r, next);
  309. return r;
  310. }
  311. static void usb_host_req_free(USBHostRequest *r)
  312. {
  313. QTAILQ_REMOVE(&r->host->requests, r, next);
  314. libusb_free_transfer(r->xfer);
  315. g_free(r->buffer);
  316. g_free(r);
  317. }
  318. static USBHostRequest *usb_host_req_find(USBHostDevice *s, USBPacket *p)
  319. {
  320. USBHostRequest *r;
  321. QTAILQ_FOREACH(r, &s->requests, next) {
  322. if (r->p == p) {
  323. return r;
  324. }
  325. }
  326. return NULL;
  327. }
  328. static void LIBUSB_CALL usb_host_req_complete_ctrl(struct libusb_transfer *xfer)
  329. {
  330. USBHostRequest *r = xfer->user_data;
  331. USBHostDevice *s = r->host;
  332. bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
  333. if (r->p == NULL) {
  334. goto out; /* request was canceled */
  335. }
  336. r->p->status = status_map[xfer->status];
  337. r->p->actual_length = xfer->actual_length;
  338. if (r->in && xfer->actual_length) {
  339. USBDevice *udev = USB_DEVICE(s);
  340. struct libusb_config_descriptor *conf = (void *)r->cbuf;
  341. memcpy(r->cbuf, r->buffer + 8, xfer->actual_length);
  342. /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
  343. * to work redirected to a not superspeed capable hcd */
  344. if (r->usb3ep0quirk && xfer->actual_length >= 18 &&
  345. r->cbuf[7] == 9) {
  346. r->cbuf[7] = 64;
  347. }
  348. /*
  349. *If this is GET_DESCRIPTOR request for configuration descriptor,
  350. * remove 'remote wakeup' flag from it to prevent idle power down
  351. * in Windows guest
  352. */
  353. if (s->suppress_remote_wake &&
  354. udev->setup_buf[0] == USB_DIR_IN &&
  355. udev->setup_buf[1] == USB_REQ_GET_DESCRIPTOR &&
  356. udev->setup_buf[3] == USB_DT_CONFIG && udev->setup_buf[2] == 0 &&
  357. xfer->actual_length >
  358. offsetof(struct libusb_config_descriptor, bmAttributes) &&
  359. (conf->bmAttributes & USB_CFG_ATT_WAKEUP)) {
  360. trace_usb_host_remote_wakeup_removed(s->bus_num, s->addr);
  361. conf->bmAttributes &= ~USB_CFG_ATT_WAKEUP;
  362. }
  363. }
  364. trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
  365. r->p->status, r->p->actual_length);
  366. usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
  367. out:
  368. usb_host_req_free(r);
  369. if (disconnect) {
  370. usb_host_nodev(s);
  371. }
  372. }
  373. static void LIBUSB_CALL usb_host_req_complete_data(struct libusb_transfer *xfer)
  374. {
  375. USBHostRequest *r = xfer->user_data;
  376. USBHostDevice *s = r->host;
  377. bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
  378. if (r->p == NULL) {
  379. goto out; /* request was canceled */
  380. }
  381. r->p->status = status_map[xfer->status];
  382. if (r->in && xfer->actual_length) {
  383. usb_packet_copy(r->p, r->buffer, xfer->actual_length);
  384. }
  385. trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
  386. r->p->status, r->p->actual_length);
  387. if (usb_host_use_combining(r->p->ep)) {
  388. usb_combined_input_packet_complete(USB_DEVICE(s), r->p);
  389. } else {
  390. usb_packet_complete(USB_DEVICE(s), r->p);
  391. }
  392. out:
  393. usb_host_req_free(r);
  394. if (disconnect) {
  395. usb_host_nodev(s);
  396. }
  397. }
  398. static void usb_host_req_abort(USBHostRequest *r)
  399. {
  400. USBHostDevice *s = r->host;
  401. bool inflight = (r->p && r->p->state == USB_PACKET_ASYNC);
  402. if (inflight) {
  403. r->p->status = USB_RET_NODEV;
  404. trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
  405. r->p->status, r->p->actual_length);
  406. if (r->p->ep->nr == 0) {
  407. usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
  408. } else {
  409. usb_packet_complete(USB_DEVICE(s), r->p);
  410. }
  411. r->p = NULL;
  412. libusb_cancel_transfer(r->xfer);
  413. }
  414. }
  415. /* ------------------------------------------------------------------------ */
  416. static void LIBUSB_CALL
  417. usb_host_req_complete_iso(struct libusb_transfer *transfer)
  418. {
  419. USBHostIsoXfer *xfer = transfer->user_data;
  420. if (!xfer) {
  421. /* USBHostIsoXfer released while inflight */
  422. g_free(transfer->buffer);
  423. libusb_free_transfer(transfer);
  424. return;
  425. }
  426. QTAILQ_REMOVE(&xfer->ring->inflight, xfer, next);
  427. if (QTAILQ_EMPTY(&xfer->ring->inflight)) {
  428. USBHostDevice *s = xfer->ring->host;
  429. trace_usb_host_iso_stop(s->bus_num, s->addr, xfer->ring->ep->nr);
  430. }
  431. if (xfer->ring->ep->pid == USB_TOKEN_IN) {
  432. QTAILQ_INSERT_TAIL(&xfer->ring->copy, xfer, next);
  433. usb_wakeup(xfer->ring->ep, 0);
  434. } else {
  435. QTAILQ_INSERT_TAIL(&xfer->ring->unused, xfer, next);
  436. }
  437. }
  438. static USBHostIsoRing *usb_host_iso_alloc(USBHostDevice *s, USBEndpoint *ep)
  439. {
  440. USBHostIsoRing *ring = g_new0(USBHostIsoRing, 1);
  441. USBHostIsoXfer *xfer;
  442. /* FIXME: check interval (for now assume one xfer per frame) */
  443. int packets = s->iso_urb_frames;
  444. int i;
  445. ring->host = s;
  446. ring->ep = ep;
  447. QTAILQ_INIT(&ring->unused);
  448. QTAILQ_INIT(&ring->inflight);
  449. QTAILQ_INIT(&ring->copy);
  450. QTAILQ_INSERT_TAIL(&s->isorings, ring, next);
  451. for (i = 0; i < s->iso_urb_count; i++) {
  452. xfer = g_new0(USBHostIsoXfer, 1);
  453. xfer->ring = ring;
  454. xfer->xfer = libusb_alloc_transfer(packets);
  455. xfer->xfer->dev_handle = s->dh;
  456. xfer->xfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
  457. xfer->xfer->endpoint = ring->ep->nr;
  458. if (ring->ep->pid == USB_TOKEN_IN) {
  459. xfer->xfer->endpoint |= USB_DIR_IN;
  460. }
  461. xfer->xfer->callback = usb_host_req_complete_iso;
  462. xfer->xfer->user_data = xfer;
  463. xfer->xfer->num_iso_packets = packets;
  464. xfer->xfer->length = ring->ep->max_packet_size * packets;
  465. xfer->xfer->buffer = g_malloc0(xfer->xfer->length);
  466. QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
  467. }
  468. return ring;
  469. }
  470. static USBHostIsoRing *usb_host_iso_find(USBHostDevice *s, USBEndpoint *ep)
  471. {
  472. USBHostIsoRing *ring;
  473. QTAILQ_FOREACH(ring, &s->isorings, next) {
  474. if (ring->ep == ep) {
  475. return ring;
  476. }
  477. }
  478. return NULL;
  479. }
  480. static void usb_host_iso_reset_xfer(USBHostIsoXfer *xfer)
  481. {
  482. libusb_set_iso_packet_lengths(xfer->xfer,
  483. xfer->ring->ep->max_packet_size);
  484. xfer->packet = 0;
  485. xfer->copy_complete = false;
  486. }
  487. static void usb_host_iso_free_xfer(USBHostIsoXfer *xfer, bool inflight)
  488. {
  489. if (inflight) {
  490. xfer->xfer->user_data = NULL;
  491. } else {
  492. g_free(xfer->xfer->buffer);
  493. libusb_free_transfer(xfer->xfer);
  494. }
  495. g_free(xfer);
  496. }
  497. static void usb_host_iso_free(USBHostIsoRing *ring)
  498. {
  499. USBHostIsoXfer *xfer;
  500. while ((xfer = QTAILQ_FIRST(&ring->inflight)) != NULL) {
  501. QTAILQ_REMOVE(&ring->inflight, xfer, next);
  502. usb_host_iso_free_xfer(xfer, true);
  503. }
  504. while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
  505. QTAILQ_REMOVE(&ring->unused, xfer, next);
  506. usb_host_iso_free_xfer(xfer, false);
  507. }
  508. while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL) {
  509. QTAILQ_REMOVE(&ring->copy, xfer, next);
  510. usb_host_iso_free_xfer(xfer, false);
  511. }
  512. QTAILQ_REMOVE(&ring->host->isorings, ring, next);
  513. g_free(ring);
  514. }
  515. static void usb_host_iso_free_all(USBHostDevice *s)
  516. {
  517. USBHostIsoRing *ring;
  518. while ((ring = QTAILQ_FIRST(&s->isorings)) != NULL) {
  519. usb_host_iso_free(ring);
  520. }
  521. }
  522. static bool usb_host_iso_data_copy(USBHostIsoXfer *xfer, USBPacket *p)
  523. {
  524. unsigned int psize;
  525. unsigned char *buf;
  526. buf = libusb_get_iso_packet_buffer_simple(xfer->xfer, xfer->packet);
  527. if (p->pid == USB_TOKEN_OUT) {
  528. psize = p->iov.size;
  529. if (psize > xfer->ring->ep->max_packet_size) {
  530. /* should not happen (guest bug) */
  531. psize = xfer->ring->ep->max_packet_size;
  532. }
  533. xfer->xfer->iso_packet_desc[xfer->packet].length = psize;
  534. } else {
  535. psize = xfer->xfer->iso_packet_desc[xfer->packet].actual_length;
  536. if (psize > p->iov.size) {
  537. /* should not happen (guest bug) */
  538. psize = p->iov.size;
  539. }
  540. }
  541. usb_packet_copy(p, buf, psize);
  542. xfer->packet++;
  543. xfer->copy_complete = (xfer->packet == xfer->xfer->num_iso_packets);
  544. return xfer->copy_complete;
  545. }
  546. static void usb_host_iso_data_in(USBHostDevice *s, USBPacket *p)
  547. {
  548. USBHostIsoRing *ring;
  549. USBHostIsoXfer *xfer;
  550. bool disconnect = false;
  551. int rc;
  552. ring = usb_host_iso_find(s, p->ep);
  553. if (ring == NULL) {
  554. ring = usb_host_iso_alloc(s, p->ep);
  555. }
  556. /* copy data to guest */
  557. xfer = QTAILQ_FIRST(&ring->copy);
  558. if (xfer != NULL) {
  559. if (usb_host_iso_data_copy(xfer, p)) {
  560. QTAILQ_REMOVE(&ring->copy, xfer, next);
  561. QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
  562. }
  563. }
  564. /* submit empty bufs to host */
  565. while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
  566. QTAILQ_REMOVE(&ring->unused, xfer, next);
  567. usb_host_iso_reset_xfer(xfer);
  568. rc = libusb_submit_transfer(xfer->xfer);
  569. if (rc != 0) {
  570. usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
  571. QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
  572. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  573. disconnect = true;
  574. }
  575. break;
  576. }
  577. if (QTAILQ_EMPTY(&ring->inflight)) {
  578. trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
  579. }
  580. QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
  581. }
  582. if (disconnect) {
  583. usb_host_nodev(s);
  584. }
  585. }
  586. static void usb_host_iso_data_out(USBHostDevice *s, USBPacket *p)
  587. {
  588. USBHostIsoRing *ring;
  589. USBHostIsoXfer *xfer;
  590. bool disconnect = false;
  591. int rc, filled = 0;
  592. ring = usb_host_iso_find(s, p->ep);
  593. if (ring == NULL) {
  594. ring = usb_host_iso_alloc(s, p->ep);
  595. }
  596. /* copy data from guest */
  597. xfer = QTAILQ_FIRST(&ring->copy);
  598. while (xfer != NULL && xfer->copy_complete) {
  599. filled++;
  600. xfer = QTAILQ_NEXT(xfer, next);
  601. }
  602. if (xfer == NULL) {
  603. xfer = QTAILQ_FIRST(&ring->unused);
  604. if (xfer == NULL) {
  605. trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, p->ep->nr);
  606. return;
  607. }
  608. QTAILQ_REMOVE(&ring->unused, xfer, next);
  609. usb_host_iso_reset_xfer(xfer);
  610. QTAILQ_INSERT_TAIL(&ring->copy, xfer, next);
  611. }
  612. usb_host_iso_data_copy(xfer, p);
  613. if (QTAILQ_EMPTY(&ring->inflight)) {
  614. /* wait until half of our buffers are filled
  615. before kicking the iso out stream */
  616. if (filled*2 < s->iso_urb_count) {
  617. return;
  618. }
  619. }
  620. /* submit filled bufs to host */
  621. while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL &&
  622. xfer->copy_complete) {
  623. QTAILQ_REMOVE(&ring->copy, xfer, next);
  624. rc = libusb_submit_transfer(xfer->xfer);
  625. if (rc != 0) {
  626. usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
  627. QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
  628. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  629. disconnect = true;
  630. }
  631. break;
  632. }
  633. if (QTAILQ_EMPTY(&ring->inflight)) {
  634. trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
  635. }
  636. QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
  637. }
  638. if (disconnect) {
  639. usb_host_nodev(s);
  640. }
  641. }
  642. /* ------------------------------------------------------------------------ */
  643. static void usb_host_speed_compat(USBHostDevice *s)
  644. {
  645. USBDevice *udev = USB_DEVICE(s);
  646. struct libusb_config_descriptor *conf;
  647. const struct libusb_interface_descriptor *intf;
  648. const struct libusb_endpoint_descriptor *endp;
  649. #ifdef HAVE_STREAMS
  650. struct libusb_ss_endpoint_companion_descriptor *endp_ss_comp;
  651. #endif
  652. bool compat_high = true;
  653. bool compat_full = true;
  654. uint8_t type;
  655. int rc, c, i, a, e;
  656. for (c = 0;; c++) {
  657. rc = libusb_get_config_descriptor(s->dev, c, &conf);
  658. if (rc != 0) {
  659. break;
  660. }
  661. for (i = 0; i < conf->bNumInterfaces; i++) {
  662. for (a = 0; a < conf->interface[i].num_altsetting; a++) {
  663. intf = &conf->interface[i].altsetting[a];
  664. for (e = 0; e < intf->bNumEndpoints; e++) {
  665. endp = &intf->endpoint[e];
  666. type = endp->bmAttributes & 0x3;
  667. switch (type) {
  668. case 0x01: /* ISO */
  669. compat_full = false;
  670. compat_high = false;
  671. break;
  672. case 0x02: /* BULK */
  673. #ifdef HAVE_STREAMS
  674. rc = libusb_get_ss_endpoint_companion_descriptor
  675. (ctx, endp, &endp_ss_comp);
  676. if (rc == LIBUSB_SUCCESS) {
  677. int streams = endp_ss_comp->bmAttributes & 0x1f;
  678. if (streams) {
  679. compat_full = false;
  680. compat_high = false;
  681. }
  682. libusb_free_ss_endpoint_companion_descriptor
  683. (endp_ss_comp);
  684. }
  685. #endif
  686. break;
  687. case 0x03: /* INTERRUPT */
  688. if (endp->wMaxPacketSize > 64) {
  689. compat_full = false;
  690. }
  691. if (endp->wMaxPacketSize > 1024) {
  692. compat_high = false;
  693. }
  694. break;
  695. }
  696. }
  697. }
  698. }
  699. libusb_free_config_descriptor(conf);
  700. }
  701. udev->speedmask = (1 << udev->speed);
  702. if (udev->speed == USB_SPEED_SUPER && compat_high) {
  703. udev->speedmask |= USB_SPEED_MASK_HIGH;
  704. }
  705. if (udev->speed == USB_SPEED_SUPER && compat_full) {
  706. udev->speedmask |= USB_SPEED_MASK_FULL;
  707. }
  708. if (udev->speed == USB_SPEED_HIGH && compat_full) {
  709. udev->speedmask |= USB_SPEED_MASK_FULL;
  710. }
  711. }
  712. static void usb_host_ep_update(USBHostDevice *s)
  713. {
  714. static const char *tname[] = {
  715. [USB_ENDPOINT_XFER_CONTROL] = "control",
  716. [USB_ENDPOINT_XFER_ISOC] = "isoc",
  717. [USB_ENDPOINT_XFER_BULK] = "bulk",
  718. [USB_ENDPOINT_XFER_INT] = "int",
  719. };
  720. USBDevice *udev = USB_DEVICE(s);
  721. struct libusb_config_descriptor *conf;
  722. const struct libusb_interface_descriptor *intf;
  723. const struct libusb_endpoint_descriptor *endp;
  724. #ifdef HAVE_STREAMS
  725. struct libusb_ss_endpoint_companion_descriptor *endp_ss_comp;
  726. #endif
  727. uint8_t devep, type;
  728. int pid, ep;
  729. int rc, i, e;
  730. usb_ep_reset(udev);
  731. rc = libusb_get_active_config_descriptor(s->dev, &conf);
  732. if (rc != 0) {
  733. return;
  734. }
  735. trace_usb_host_parse_config(s->bus_num, s->addr,
  736. conf->bConfigurationValue, true);
  737. for (i = 0; i < conf->bNumInterfaces; i++) {
  738. assert(udev->altsetting[i] < conf->interface[i].num_altsetting);
  739. intf = &conf->interface[i].altsetting[udev->altsetting[i]];
  740. trace_usb_host_parse_interface(s->bus_num, s->addr,
  741. intf->bInterfaceNumber,
  742. intf->bAlternateSetting, true);
  743. for (e = 0; e < intf->bNumEndpoints; e++) {
  744. endp = &intf->endpoint[e];
  745. devep = endp->bEndpointAddress;
  746. pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
  747. ep = devep & 0xf;
  748. type = endp->bmAttributes & 0x3;
  749. if (ep == 0) {
  750. trace_usb_host_parse_error(s->bus_num, s->addr,
  751. "invalid endpoint address");
  752. return;
  753. }
  754. if (usb_ep_get_type(udev, pid, ep) != USB_ENDPOINT_XFER_INVALID) {
  755. trace_usb_host_parse_error(s->bus_num, s->addr,
  756. "duplicate endpoint address");
  757. return;
  758. }
  759. trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep,
  760. (devep & USB_DIR_IN) ? "in" : "out",
  761. tname[type], true);
  762. usb_ep_set_max_packet_size(udev, pid, ep,
  763. endp->wMaxPacketSize);
  764. usb_ep_set_type(udev, pid, ep, type);
  765. usb_ep_set_ifnum(udev, pid, ep, i);
  766. usb_ep_set_halted(udev, pid, ep, 0);
  767. #ifdef HAVE_STREAMS
  768. if (type == LIBUSB_TRANSFER_TYPE_BULK &&
  769. libusb_get_ss_endpoint_companion_descriptor(ctx, endp,
  770. &endp_ss_comp) == LIBUSB_SUCCESS) {
  771. usb_ep_set_max_streams(udev, pid, ep,
  772. endp_ss_comp->bmAttributes);
  773. libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp);
  774. }
  775. #endif
  776. }
  777. }
  778. libusb_free_config_descriptor(conf);
  779. }
  780. static int usb_host_open(USBHostDevice *s, libusb_device *dev, int hostfd)
  781. {
  782. USBDevice *udev = USB_DEVICE(s);
  783. int libusb_speed;
  784. int bus_num = 0;
  785. int addr = 0;
  786. int rc;
  787. Error *local_err = NULL;
  788. if (s->bh_postld_pending) {
  789. return -1;
  790. }
  791. if (s->dh != NULL) {
  792. goto fail;
  793. }
  794. if (dev) {
  795. bus_num = libusb_get_bus_number(dev);
  796. addr = libusb_get_device_address(dev);
  797. trace_usb_host_open_started(bus_num, addr);
  798. rc = libusb_open(dev, &s->dh);
  799. if (rc != 0) {
  800. goto fail;
  801. }
  802. } else {
  803. #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
  804. trace_usb_host_open_hostfd(hostfd);
  805. rc = libusb_wrap_sys_device(ctx, hostfd, &s->dh);
  806. if (rc != 0) {
  807. goto fail;
  808. }
  809. s->hostfd = hostfd;
  810. dev = libusb_get_device(s->dh);
  811. bus_num = libusb_get_bus_number(dev);
  812. addr = libusb_get_device_address(dev);
  813. #else
  814. g_assert_not_reached();
  815. #endif
  816. }
  817. s->dev = dev;
  818. s->bus_num = bus_num;
  819. s->addr = addr;
  820. usb_host_detach_kernel(s);
  821. libusb_get_device_descriptor(dev, &s->ddesc);
  822. usb_host_get_port(s->dev, s->port, sizeof(s->port));
  823. usb_ep_init(udev);
  824. usb_host_ep_update(s);
  825. libusb_speed = libusb_get_device_speed(dev);
  826. #if LIBUSB_API_VERSION >= 0x01000107 && defined(CONFIG_LINUX)
  827. if (hostfd && libusb_speed == 0) {
  828. /*
  829. * Workaround libusb bug: libusb_get_device_speed() does not
  830. * work for libusb_wrap_sys_device() devices in v1.0.23.
  831. *
  832. * Speeds are defined in linux/usb/ch9.h, file not included
  833. * due to name conflicts.
  834. */
  835. int rc = ioctl(hostfd, USBDEVFS_GET_SPEED, NULL);
  836. switch (rc) {
  837. case 1: /* low */
  838. libusb_speed = LIBUSB_SPEED_LOW;
  839. break;
  840. case 2: /* full */
  841. libusb_speed = LIBUSB_SPEED_FULL;
  842. break;
  843. case 3: /* high */
  844. case 4: /* wireless */
  845. libusb_speed = LIBUSB_SPEED_HIGH;
  846. break;
  847. case 5: /* super */
  848. case 6: /* super plus */
  849. libusb_speed = LIBUSB_SPEED_SUPER;
  850. break;
  851. }
  852. }
  853. #endif
  854. udev->speed = speed_map[libusb_speed];
  855. usb_host_speed_compat(s);
  856. if (s->ddesc.iProduct) {
  857. libusb_get_string_descriptor_ascii(s->dh, s->ddesc.iProduct,
  858. (unsigned char *)udev->product_desc,
  859. sizeof(udev->product_desc));
  860. } else {
  861. snprintf(udev->product_desc, sizeof(udev->product_desc),
  862. "host:%d.%d", bus_num, addr);
  863. }
  864. usb_device_attach(udev, &local_err);
  865. if (local_err) {
  866. error_report_err(local_err);
  867. goto fail;
  868. }
  869. trace_usb_host_open_success(bus_num, addr);
  870. return 0;
  871. fail:
  872. trace_usb_host_open_failure(bus_num, addr);
  873. if (s->dh != NULL) {
  874. usb_host_release_interfaces(s);
  875. libusb_reset_device(s->dh);
  876. usb_host_attach_kernel(s);
  877. libusb_close(s->dh);
  878. s->dh = NULL;
  879. s->dev = NULL;
  880. }
  881. return -1;
  882. }
  883. static void usb_host_abort_xfers(USBHostDevice *s)
  884. {
  885. USBHostRequest *r, *rtmp;
  886. int limit = 100;
  887. QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) {
  888. usb_host_req_abort(r);
  889. }
  890. while (QTAILQ_FIRST(&s->requests) != NULL) {
  891. struct timeval tv;
  892. memset(&tv, 0, sizeof(tv));
  893. tv.tv_usec = 2500;
  894. libusb_handle_events_timeout(ctx, &tv);
  895. if (--limit == 0) {
  896. /*
  897. * Don't wait forever for libusb calling the complete
  898. * callback (which will unlink and free the request).
  899. *
  900. * Leaking memory here, to make sure libusb will not
  901. * access memory which we have released already.
  902. */
  903. QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) {
  904. QTAILQ_REMOVE(&s->requests, r, next);
  905. }
  906. return;
  907. }
  908. }
  909. }
  910. static int usb_host_close(USBHostDevice *s)
  911. {
  912. USBDevice *udev = USB_DEVICE(s);
  913. if (s->dh == NULL) {
  914. return -1;
  915. }
  916. trace_usb_host_close(s->bus_num, s->addr);
  917. usb_host_abort_xfers(s);
  918. usb_host_iso_free_all(s);
  919. if (udev->attached) {
  920. usb_device_detach(udev);
  921. }
  922. usb_host_release_interfaces(s);
  923. libusb_reset_device(s->dh);
  924. usb_host_attach_kernel(s);
  925. libusb_close(s->dh);
  926. s->dh = NULL;
  927. s->dev = NULL;
  928. if (s->hostfd != -1) {
  929. close(s->hostfd);
  930. s->hostfd = -1;
  931. }
  932. usb_host_auto_check(NULL);
  933. return 0;
  934. }
  935. static void usb_host_nodev_bh(void *opaque)
  936. {
  937. USBHostDevice *s = opaque;
  938. usb_host_close(s);
  939. }
  940. static void usb_host_nodev(USBHostDevice *s)
  941. {
  942. if (!s->bh_nodev) {
  943. s->bh_nodev = qemu_bh_new(usb_host_nodev_bh, s);
  944. }
  945. qemu_bh_schedule(s->bh_nodev);
  946. }
  947. static void usb_host_exit_notifier(struct Notifier *n, void *data)
  948. {
  949. USBHostDevice *s = container_of(n, USBHostDevice, exit);
  950. if (s->dh) {
  951. usb_host_abort_xfers(s);
  952. usb_host_release_interfaces(s);
  953. libusb_reset_device(s->dh);
  954. usb_host_attach_kernel(s);
  955. libusb_close(s->dh);
  956. }
  957. }
  958. static libusb_device *usb_host_find_ref(int bus, int addr)
  959. {
  960. libusb_device **devs = NULL;
  961. libusb_device *ret = NULL;
  962. int i, n;
  963. n = libusb_get_device_list(ctx, &devs);
  964. for (i = 0; i < n; i++) {
  965. if (libusb_get_bus_number(devs[i]) == bus &&
  966. libusb_get_device_address(devs[i]) == addr) {
  967. ret = libusb_ref_device(devs[i]);
  968. break;
  969. }
  970. }
  971. libusb_free_device_list(devs, 1);
  972. return ret;
  973. }
  974. static void usb_host_realize(USBDevice *udev, Error **errp)
  975. {
  976. USBHostDevice *s = USB_HOST_DEVICE(udev);
  977. libusb_device *ldev;
  978. int rc;
  979. if (usb_host_init() != 0) {
  980. error_setg(errp, "failed to init libusb");
  981. return;
  982. }
  983. if (s->match.vendor_id > 0xffff) {
  984. error_setg(errp, "vendorid out of range");
  985. return;
  986. }
  987. if (s->match.product_id > 0xffff) {
  988. error_setg(errp, "productid out of range");
  989. return;
  990. }
  991. if (s->match.addr > 127) {
  992. error_setg(errp, "hostaddr out of range");
  993. return;
  994. }
  995. loglevel = s->loglevel;
  996. udev->flags |= (1 << USB_DEV_FLAG_IS_HOST);
  997. udev->auto_attach = 0;
  998. QTAILQ_INIT(&s->requests);
  999. QTAILQ_INIT(&s->isorings);
  1000. s->hostfd = -1;
  1001. #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
  1002. if (s->hostdevice) {
  1003. int fd;
  1004. s->needs_autoscan = false;
  1005. fd = qemu_open_old(s->hostdevice, O_RDWR);
  1006. if (fd < 0) {
  1007. error_setg_errno(errp, errno, "failed to open %s", s->hostdevice);
  1008. return;
  1009. }
  1010. rc = usb_host_open(s, NULL, fd);
  1011. if (rc < 0) {
  1012. error_setg(errp, "failed to open host usb device %s", s->hostdevice);
  1013. return;
  1014. }
  1015. } else
  1016. #endif
  1017. if (s->match.addr && s->match.bus_num &&
  1018. !s->match.vendor_id &&
  1019. !s->match.product_id &&
  1020. !s->match.port) {
  1021. s->needs_autoscan = false;
  1022. ldev = usb_host_find_ref(s->match.bus_num,
  1023. s->match.addr);
  1024. if (!ldev) {
  1025. error_setg(errp, "failed to find host usb device %d:%d",
  1026. s->match.bus_num, s->match.addr);
  1027. return;
  1028. }
  1029. rc = usb_host_open(s, ldev, 0);
  1030. libusb_unref_device(ldev);
  1031. if (rc < 0) {
  1032. error_setg(errp, "failed to open host usb device %d:%d",
  1033. s->match.bus_num, s->match.addr);
  1034. return;
  1035. }
  1036. } else {
  1037. s->needs_autoscan = true;
  1038. QTAILQ_INSERT_TAIL(&hostdevs, s, next);
  1039. usb_host_auto_check(NULL);
  1040. }
  1041. s->exit.notify = usb_host_exit_notifier;
  1042. qemu_add_exit_notifier(&s->exit);
  1043. }
  1044. static void usb_host_instance_init(Object *obj)
  1045. {
  1046. USBDevice *udev = USB_DEVICE(obj);
  1047. USBHostDevice *s = USB_HOST_DEVICE(udev);
  1048. device_add_bootindex_property(obj, &s->bootindex,
  1049. "bootindex", NULL,
  1050. &udev->qdev);
  1051. }
  1052. static void usb_host_unrealize(USBDevice *udev)
  1053. {
  1054. USBHostDevice *s = USB_HOST_DEVICE(udev);
  1055. qemu_remove_exit_notifier(&s->exit);
  1056. if (s->needs_autoscan) {
  1057. QTAILQ_REMOVE(&hostdevs, s, next);
  1058. }
  1059. usb_host_close(s);
  1060. }
  1061. static void usb_host_cancel_packet(USBDevice *udev, USBPacket *p)
  1062. {
  1063. USBHostDevice *s = USB_HOST_DEVICE(udev);
  1064. USBHostRequest *r;
  1065. if (p->combined) {
  1066. usb_combined_packet_cancel(udev, p);
  1067. return;
  1068. }
  1069. trace_usb_host_req_canceled(s->bus_num, s->addr, p);
  1070. r = usb_host_req_find(s, p);
  1071. if (r && r->p) {
  1072. r->p = NULL; /* mark as dead */
  1073. libusb_cancel_transfer(r->xfer);
  1074. }
  1075. }
  1076. static void usb_host_detach_kernel(USBHostDevice *s)
  1077. {
  1078. struct libusb_config_descriptor *conf;
  1079. int rc, i;
  1080. rc = libusb_get_active_config_descriptor(s->dev, &conf);
  1081. if (rc != 0) {
  1082. return;
  1083. }
  1084. for (i = 0; i < USB_MAX_INTERFACES; i++) {
  1085. rc = libusb_kernel_driver_active(s->dh, i);
  1086. usb_host_libusb_error("libusb_kernel_driver_active", rc);
  1087. if (rc != 1) {
  1088. if (rc == 0) {
  1089. s->ifs[i].detached = true;
  1090. }
  1091. continue;
  1092. }
  1093. trace_usb_host_detach_kernel(s->bus_num, s->addr, i);
  1094. rc = libusb_detach_kernel_driver(s->dh, i);
  1095. usb_host_libusb_error("libusb_detach_kernel_driver", rc);
  1096. s->ifs[i].detached = true;
  1097. }
  1098. libusb_free_config_descriptor(conf);
  1099. }
  1100. static void usb_host_attach_kernel(USBHostDevice *s)
  1101. {
  1102. struct libusb_config_descriptor *conf;
  1103. int rc, i;
  1104. rc = libusb_get_active_config_descriptor(s->dev, &conf);
  1105. if (rc != 0) {
  1106. return;
  1107. }
  1108. for (i = 0; i < USB_MAX_INTERFACES; i++) {
  1109. if (!s->ifs[i].detached) {
  1110. continue;
  1111. }
  1112. trace_usb_host_attach_kernel(s->bus_num, s->addr, i);
  1113. libusb_attach_kernel_driver(s->dh, i);
  1114. s->ifs[i].detached = false;
  1115. }
  1116. libusb_free_config_descriptor(conf);
  1117. }
  1118. static int usb_host_claim_interfaces(USBHostDevice *s, int configuration)
  1119. {
  1120. USBDevice *udev = USB_DEVICE(s);
  1121. struct libusb_config_descriptor *conf;
  1122. int rc, i, claimed;
  1123. for (i = 0; i < USB_MAX_INTERFACES; i++) {
  1124. udev->altsetting[i] = 0;
  1125. }
  1126. udev->ninterfaces = 0;
  1127. udev->configuration = 0;
  1128. usb_host_detach_kernel(s);
  1129. rc = libusb_get_active_config_descriptor(s->dev, &conf);
  1130. if (rc != 0) {
  1131. if (rc == LIBUSB_ERROR_NOT_FOUND) {
  1132. /* address state - ignore */
  1133. return USB_RET_SUCCESS;
  1134. }
  1135. return USB_RET_STALL;
  1136. }
  1137. claimed = 0;
  1138. for (i = 0; i < USB_MAX_INTERFACES; i++) {
  1139. trace_usb_host_claim_interface(s->bus_num, s->addr, configuration, i);
  1140. rc = libusb_claim_interface(s->dh, i);
  1141. if (rc == 0) {
  1142. s->ifs[i].claimed = true;
  1143. if (++claimed == conf->bNumInterfaces) {
  1144. break;
  1145. }
  1146. }
  1147. }
  1148. if (claimed != conf->bNumInterfaces) {
  1149. return USB_RET_STALL;
  1150. }
  1151. udev->ninterfaces = conf->bNumInterfaces;
  1152. udev->configuration = configuration;
  1153. libusb_free_config_descriptor(conf);
  1154. return USB_RET_SUCCESS;
  1155. }
  1156. static void usb_host_release_interfaces(USBHostDevice *s)
  1157. {
  1158. int i, rc;
  1159. for (i = 0; i < USB_MAX_INTERFACES; i++) {
  1160. if (!s->ifs[i].claimed) {
  1161. continue;
  1162. }
  1163. trace_usb_host_release_interface(s->bus_num, s->addr, i);
  1164. rc = libusb_release_interface(s->dh, i);
  1165. usb_host_libusb_error("libusb_release_interface", rc);
  1166. s->ifs[i].claimed = false;
  1167. }
  1168. }
  1169. static void usb_host_set_address(USBHostDevice *s, int addr)
  1170. {
  1171. USBDevice *udev = USB_DEVICE(s);
  1172. trace_usb_host_set_address(s->bus_num, s->addr, addr);
  1173. udev->addr = addr;
  1174. }
  1175. static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
  1176. {
  1177. int rc = 0;
  1178. trace_usb_host_set_config(s->bus_num, s->addr, config);
  1179. usb_host_release_interfaces(s);
  1180. if (s->ddesc.bNumConfigurations != 1) {
  1181. rc = libusb_set_configuration(s->dh, config);
  1182. if (rc != 0) {
  1183. usb_host_libusb_error("libusb_set_configuration", rc);
  1184. p->status = USB_RET_STALL;
  1185. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  1186. usb_host_nodev(s);
  1187. }
  1188. return;
  1189. }
  1190. }
  1191. p->status = usb_host_claim_interfaces(s, config);
  1192. if (p->status != USB_RET_SUCCESS) {
  1193. return;
  1194. }
  1195. usb_host_ep_update(s);
  1196. }
  1197. static void usb_host_set_interface(USBHostDevice *s, int iface, int alt,
  1198. USBPacket *p)
  1199. {
  1200. USBDevice *udev = USB_DEVICE(s);
  1201. int rc;
  1202. trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
  1203. usb_host_iso_free_all(s);
  1204. if (iface >= USB_MAX_INTERFACES) {
  1205. p->status = USB_RET_STALL;
  1206. return;
  1207. }
  1208. rc = libusb_set_interface_alt_setting(s->dh, iface, alt);
  1209. if (rc != 0) {
  1210. usb_host_libusb_error("libusb_set_interface_alt_setting", rc);
  1211. p->status = USB_RET_STALL;
  1212. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  1213. usb_host_nodev(s);
  1214. }
  1215. return;
  1216. }
  1217. udev->altsetting[iface] = alt;
  1218. usb_host_ep_update(s);
  1219. }
  1220. static void usb_host_handle_control(USBDevice *udev, USBPacket *p,
  1221. int request, int value, int index,
  1222. int length, uint8_t *data)
  1223. {
  1224. USBHostDevice *s = USB_HOST_DEVICE(udev);
  1225. USBHostRequest *r;
  1226. int rc;
  1227. trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index);
  1228. if (s->dh == NULL) {
  1229. p->status = USB_RET_NODEV;
  1230. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  1231. return;
  1232. }
  1233. switch (request) {
  1234. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  1235. usb_host_set_address(s, value);
  1236. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  1237. return;
  1238. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  1239. usb_host_set_config(s, value & 0xff, p);
  1240. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  1241. return;
  1242. case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
  1243. usb_host_set_interface(s, index, value, p);
  1244. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  1245. return;
  1246. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  1247. if (value == 0) { /* clear halt */
  1248. int pid = (index & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
  1249. libusb_clear_halt(s->dh, index);
  1250. usb_ep_set_halted(udev, pid, index & 0x0f, 0);
  1251. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  1252. return;
  1253. }
  1254. }
  1255. r = usb_host_req_alloc(s, p, (request >> 8) & USB_DIR_IN, length + 8);
  1256. r->cbuf = data;
  1257. r->clen = length;
  1258. memcpy(r->buffer, udev->setup_buf, 8);
  1259. if (!r->in) {
  1260. memcpy(r->buffer + 8, r->cbuf, r->clen);
  1261. }
  1262. /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
  1263. * to work redirected to a not superspeed capable hcd */
  1264. if ((udev->speedmask & USB_SPEED_MASK_SUPER) &&
  1265. !(udev->port->speedmask & USB_SPEED_MASK_SUPER) &&
  1266. request == 0x8006 && value == 0x100 && index == 0) {
  1267. r->usb3ep0quirk = true;
  1268. }
  1269. libusb_fill_control_transfer(r->xfer, s->dh, r->buffer,
  1270. usb_host_req_complete_ctrl, r,
  1271. CONTROL_TIMEOUT);
  1272. rc = libusb_submit_transfer(r->xfer);
  1273. if (rc != 0) {
  1274. p->status = USB_RET_NODEV;
  1275. trace_usb_host_req_complete(s->bus_num, s->addr, p,
  1276. p->status, p->actual_length);
  1277. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  1278. usb_host_nodev(s);
  1279. }
  1280. return;
  1281. }
  1282. p->status = USB_RET_ASYNC;
  1283. }
  1284. static void usb_host_handle_data(USBDevice *udev, USBPacket *p)
  1285. {
  1286. USBHostDevice *s = USB_HOST_DEVICE(udev);
  1287. USBHostRequest *r;
  1288. size_t size;
  1289. int ep, rc;
  1290. if (usb_host_use_combining(p->ep) && p->state == USB_PACKET_SETUP) {
  1291. p->status = USB_RET_ADD_TO_QUEUE;
  1292. return;
  1293. }
  1294. trace_usb_host_req_data(s->bus_num, s->addr, p,
  1295. p->pid == USB_TOKEN_IN,
  1296. p->ep->nr, p->iov.size);
  1297. if (s->dh == NULL) {
  1298. p->status = USB_RET_NODEV;
  1299. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  1300. return;
  1301. }
  1302. if (p->ep->halted) {
  1303. p->status = USB_RET_STALL;
  1304. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  1305. return;
  1306. }
  1307. switch (usb_ep_get_type(udev, p->pid, p->ep->nr)) {
  1308. case USB_ENDPOINT_XFER_BULK:
  1309. size = usb_packet_size(p);
  1310. r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, size);
  1311. if (!r->in) {
  1312. usb_packet_copy(p, r->buffer, size);
  1313. }
  1314. ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
  1315. if (p->stream) {
  1316. #ifdef HAVE_STREAMS
  1317. libusb_fill_bulk_stream_transfer(r->xfer, s->dh, ep, p->stream,
  1318. r->buffer, size,
  1319. usb_host_req_complete_data, r,
  1320. BULK_TIMEOUT);
  1321. #else
  1322. usb_host_req_free(r);
  1323. p->status = USB_RET_STALL;
  1324. return;
  1325. #endif
  1326. } else {
  1327. libusb_fill_bulk_transfer(r->xfer, s->dh, ep,
  1328. r->buffer, size,
  1329. usb_host_req_complete_data, r,
  1330. BULK_TIMEOUT);
  1331. }
  1332. break;
  1333. case USB_ENDPOINT_XFER_INT:
  1334. r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, p->iov.size);
  1335. if (!r->in) {
  1336. usb_packet_copy(p, r->buffer, p->iov.size);
  1337. }
  1338. ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
  1339. libusb_fill_interrupt_transfer(r->xfer, s->dh, ep,
  1340. r->buffer, p->iov.size,
  1341. usb_host_req_complete_data, r,
  1342. INTR_TIMEOUT);
  1343. break;
  1344. case USB_ENDPOINT_XFER_ISOC:
  1345. if (p->pid == USB_TOKEN_IN) {
  1346. usb_host_iso_data_in(s, p);
  1347. } else {
  1348. usb_host_iso_data_out(s, p);
  1349. }
  1350. trace_usb_host_req_complete(s->bus_num, s->addr, p,
  1351. p->status, p->actual_length);
  1352. return;
  1353. default:
  1354. p->status = USB_RET_STALL;
  1355. trace_usb_host_req_complete(s->bus_num, s->addr, p,
  1356. p->status, p->actual_length);
  1357. return;
  1358. }
  1359. rc = libusb_submit_transfer(r->xfer);
  1360. if (rc != 0) {
  1361. p->status = USB_RET_NODEV;
  1362. trace_usb_host_req_complete(s->bus_num, s->addr, p,
  1363. p->status, p->actual_length);
  1364. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  1365. usb_host_nodev(s);
  1366. }
  1367. return;
  1368. }
  1369. p->status = USB_RET_ASYNC;
  1370. }
  1371. static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
  1372. {
  1373. if (usb_host_use_combining(ep)) {
  1374. usb_ep_combine_input_packets(ep);
  1375. }
  1376. }
  1377. static void usb_host_handle_reset(USBDevice *udev)
  1378. {
  1379. USBHostDevice *s = USB_HOST_DEVICE(udev);
  1380. int rc;
  1381. if (!s->allow_one_guest_reset && !s->allow_all_guest_resets) {
  1382. return;
  1383. }
  1384. if (!s->allow_all_guest_resets && udev->addr == 0) {
  1385. return;
  1386. }
  1387. trace_usb_host_reset(s->bus_num, s->addr);
  1388. rc = libusb_reset_device(s->dh);
  1389. if (rc != 0) {
  1390. usb_host_nodev(s);
  1391. }
  1392. }
  1393. static int usb_host_alloc_streams(USBDevice *udev, USBEndpoint **eps,
  1394. int nr_eps, int streams)
  1395. {
  1396. #ifdef HAVE_STREAMS
  1397. USBHostDevice *s = USB_HOST_DEVICE(udev);
  1398. unsigned char endpoints[30];
  1399. int i, rc;
  1400. for (i = 0; i < nr_eps; i++) {
  1401. endpoints[i] = eps[i]->nr;
  1402. if (eps[i]->pid == USB_TOKEN_IN) {
  1403. endpoints[i] |= 0x80;
  1404. }
  1405. }
  1406. rc = libusb_alloc_streams(s->dh, streams, endpoints, nr_eps);
  1407. if (rc < 0) {
  1408. usb_host_libusb_error("libusb_alloc_streams", rc);
  1409. } else if (rc != streams) {
  1410. error_report("libusb_alloc_streams: got less streams "
  1411. "then requested %d < %d", rc, streams);
  1412. }
  1413. return (rc == streams) ? 0 : -1;
  1414. #else
  1415. error_report("libusb_alloc_streams: error not implemented");
  1416. return -1;
  1417. #endif
  1418. }
  1419. static void usb_host_free_streams(USBDevice *udev, USBEndpoint **eps,
  1420. int nr_eps)
  1421. {
  1422. #ifdef HAVE_STREAMS
  1423. USBHostDevice *s = USB_HOST_DEVICE(udev);
  1424. unsigned char endpoints[30];
  1425. int i;
  1426. for (i = 0; i < nr_eps; i++) {
  1427. endpoints[i] = eps[i]->nr;
  1428. if (eps[i]->pid == USB_TOKEN_IN) {
  1429. endpoints[i] |= 0x80;
  1430. }
  1431. }
  1432. libusb_free_streams(s->dh, endpoints, nr_eps);
  1433. #endif
  1434. }
  1435. /*
  1436. * This is *NOT* about restoring state. We have absolutely no idea
  1437. * what state the host device is in at the moment and whenever it is
  1438. * still present in the first place. Attemping to contine where we
  1439. * left off is impossible.
  1440. *
  1441. * What we are going to do here is emulate a surprise removal of
  1442. * the usb device passed through, then kick host scan so the device
  1443. * will get re-attached (and re-initialized by the guest) in case it
  1444. * is still present.
  1445. *
  1446. * As the device removal will change the state of other devices (usb
  1447. * host controller, most likely interrupt controller too) we have to
  1448. * wait with it until *all* vmstate is loaded. Thus post_load just
  1449. * kicks a bottom half which then does the actual work.
  1450. */
  1451. static void usb_host_post_load_bh(void *opaque)
  1452. {
  1453. USBHostDevice *dev = opaque;
  1454. USBDevice *udev = USB_DEVICE(dev);
  1455. if (dev->dh != NULL) {
  1456. usb_host_close(dev);
  1457. }
  1458. if (udev->attached) {
  1459. usb_device_detach(udev);
  1460. }
  1461. dev->bh_postld_pending = false;
  1462. usb_host_auto_check(NULL);
  1463. }
  1464. static int usb_host_post_load(void *opaque, int version_id)
  1465. {
  1466. USBHostDevice *dev = opaque;
  1467. if (!dev->bh_postld) {
  1468. dev->bh_postld = qemu_bh_new(usb_host_post_load_bh, dev);
  1469. }
  1470. qemu_bh_schedule(dev->bh_postld);
  1471. dev->bh_postld_pending = true;
  1472. return 0;
  1473. }
  1474. static const VMStateDescription vmstate_usb_host = {
  1475. .name = "usb-host",
  1476. .version_id = 1,
  1477. .minimum_version_id = 1,
  1478. .post_load = usb_host_post_load,
  1479. .fields = (VMStateField[]) {
  1480. VMSTATE_USB_DEVICE(parent_obj, USBHostDevice),
  1481. VMSTATE_END_OF_LIST()
  1482. }
  1483. };
  1484. static Property usb_host_dev_properties[] = {
  1485. DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
  1486. DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
  1487. DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
  1488. DEFINE_PROP_UINT32("vendorid", USBHostDevice, match.vendor_id, 0),
  1489. DEFINE_PROP_UINT32("productid", USBHostDevice, match.product_id, 0),
  1490. #if LIBUSB_API_VERSION >= 0x01000107
  1491. DEFINE_PROP_STRING("hostdevice", USBHostDevice, hostdevice),
  1492. #endif
  1493. DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
  1494. DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32),
  1495. DEFINE_PROP_BOOL("guest-reset", USBHostDevice,
  1496. allow_one_guest_reset, true),
  1497. DEFINE_PROP_BOOL("guest-resets-all", USBHostDevice,
  1498. allow_all_guest_resets, false),
  1499. DEFINE_PROP_UINT32("loglevel", USBHostDevice, loglevel,
  1500. LIBUSB_LOG_LEVEL_WARNING),
  1501. DEFINE_PROP_BIT("pipeline", USBHostDevice, options,
  1502. USB_HOST_OPT_PIPELINE, true),
  1503. DEFINE_PROP_BOOL("suppress-remote-wake", USBHostDevice,
  1504. suppress_remote_wake, true),
  1505. DEFINE_PROP_END_OF_LIST(),
  1506. };
  1507. static void usb_host_class_initfn(ObjectClass *klass, void *data)
  1508. {
  1509. DeviceClass *dc = DEVICE_CLASS(klass);
  1510. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  1511. uc->realize = usb_host_realize;
  1512. uc->product_desc = "USB Host Device";
  1513. uc->cancel_packet = usb_host_cancel_packet;
  1514. uc->handle_data = usb_host_handle_data;
  1515. uc->handle_control = usb_host_handle_control;
  1516. uc->handle_reset = usb_host_handle_reset;
  1517. uc->unrealize = usb_host_unrealize;
  1518. uc->flush_ep_queue = usb_host_flush_ep_queue;
  1519. uc->alloc_streams = usb_host_alloc_streams;
  1520. uc->free_streams = usb_host_free_streams;
  1521. dc->vmsd = &vmstate_usb_host;
  1522. device_class_set_props(dc, usb_host_dev_properties);
  1523. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  1524. }
  1525. static TypeInfo usb_host_dev_info = {
  1526. .name = TYPE_USB_HOST_DEVICE,
  1527. .parent = TYPE_USB_DEVICE,
  1528. .instance_size = sizeof(USBHostDevice),
  1529. .class_init = usb_host_class_initfn,
  1530. .instance_init = usb_host_instance_init,
  1531. };
  1532. static void usb_host_register_types(void)
  1533. {
  1534. type_register_static(&usb_host_dev_info);
  1535. }
  1536. type_init(usb_host_register_types)
  1537. /* ------------------------------------------------------------------------ */
  1538. static QEMUTimer *usb_auto_timer;
  1539. static VMChangeStateEntry *usb_vmstate;
  1540. static void usb_host_vm_state(void *unused, int running, RunState state)
  1541. {
  1542. if (running) {
  1543. usb_host_auto_check(unused);
  1544. }
  1545. }
  1546. static void usb_host_auto_check(void *unused)
  1547. {
  1548. struct USBHostDevice *s;
  1549. struct USBAutoFilter *f;
  1550. libusb_device **devs = NULL;
  1551. struct libusb_device_descriptor ddesc;
  1552. int unconnected = 0;
  1553. int i, n;
  1554. if (usb_host_init() != 0) {
  1555. return;
  1556. }
  1557. if (runstate_is_running()) {
  1558. n = libusb_get_device_list(ctx, &devs);
  1559. for (i = 0; i < n; i++) {
  1560. if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
  1561. continue;
  1562. }
  1563. if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
  1564. continue;
  1565. }
  1566. QTAILQ_FOREACH(s, &hostdevs, next) {
  1567. f = &s->match;
  1568. if (f->bus_num > 0 &&
  1569. f->bus_num != libusb_get_bus_number(devs[i])) {
  1570. continue;
  1571. }
  1572. if (f->addr > 0 &&
  1573. f->addr != libusb_get_device_address(devs[i])) {
  1574. continue;
  1575. }
  1576. if (f->port != NULL) {
  1577. char port[16] = "-";
  1578. usb_host_get_port(devs[i], port, sizeof(port));
  1579. if (strcmp(f->port, port) != 0) {
  1580. continue;
  1581. }
  1582. }
  1583. if (f->vendor_id > 0 &&
  1584. f->vendor_id != ddesc.idVendor) {
  1585. continue;
  1586. }
  1587. if (f->product_id > 0 &&
  1588. f->product_id != ddesc.idProduct) {
  1589. continue;
  1590. }
  1591. /* We got a match */
  1592. s->seen++;
  1593. if (s->errcount >= 3) {
  1594. continue;
  1595. }
  1596. if (s->dh != NULL) {
  1597. continue;
  1598. }
  1599. if (usb_host_open(s, devs[i], 0) < 0) {
  1600. s->errcount++;
  1601. continue;
  1602. }
  1603. break;
  1604. }
  1605. }
  1606. libusb_free_device_list(devs, 1);
  1607. QTAILQ_FOREACH(s, &hostdevs, next) {
  1608. if (s->dh == NULL) {
  1609. unconnected++;
  1610. }
  1611. if (s->seen == 0) {
  1612. if (s->dh) {
  1613. usb_host_close(s);
  1614. }
  1615. s->errcount = 0;
  1616. }
  1617. s->seen = 0;
  1618. }
  1619. #if 0
  1620. if (unconnected == 0) {
  1621. /* nothing to watch */
  1622. if (usb_auto_timer) {
  1623. timer_del(usb_auto_timer);
  1624. trace_usb_host_auto_scan_disabled();
  1625. }
  1626. return;
  1627. }
  1628. #endif
  1629. }
  1630. if (!usb_vmstate) {
  1631. usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);
  1632. }
  1633. if (!usb_auto_timer) {
  1634. usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, usb_host_auto_check, NULL);
  1635. if (!usb_auto_timer) {
  1636. return;
  1637. }
  1638. trace_usb_host_auto_scan_enabled();
  1639. }
  1640. timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000);
  1641. }
  1642. /**
  1643. * Check whether USB host device has a USB mass storage SCSI interface
  1644. */
  1645. bool usb_host_dev_is_scsi_storage(USBDevice *ud)
  1646. {
  1647. USBHostDevice *uhd = USB_HOST_DEVICE(ud);
  1648. struct libusb_config_descriptor *conf;
  1649. const struct libusb_interface_descriptor *intf;
  1650. bool is_scsi_storage = false;
  1651. int i;
  1652. if (!uhd || libusb_get_active_config_descriptor(uhd->dev, &conf) != 0) {
  1653. return false;
  1654. }
  1655. for (i = 0; i < conf->bNumInterfaces; i++) {
  1656. intf = &conf->interface[i].altsetting[ud->altsetting[i]];
  1657. if (intf->bInterfaceClass == LIBUSB_CLASS_MASS_STORAGE &&
  1658. intf->bInterfaceSubClass == 6) { /* 6 means SCSI */
  1659. is_scsi_storage = true;
  1660. break;
  1661. }
  1662. }
  1663. libusb_free_config_descriptor(conf);
  1664. return is_scsi_storage;
  1665. }
  1666. void hmp_info_usbhost(Monitor *mon, const QDict *qdict)
  1667. {
  1668. libusb_device **devs = NULL;
  1669. struct libusb_device_descriptor ddesc;
  1670. char port[16];
  1671. int i, n;
  1672. if (usb_host_init() != 0) {
  1673. return;
  1674. }
  1675. n = libusb_get_device_list(ctx, &devs);
  1676. for (i = 0; i < n; i++) {
  1677. if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
  1678. continue;
  1679. }
  1680. if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
  1681. continue;
  1682. }
  1683. usb_host_get_port(devs[i], port, sizeof(port));
  1684. monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
  1685. libusb_get_bus_number(devs[i]),
  1686. libusb_get_device_address(devs[i]),
  1687. port,
  1688. speed_name[libusb_get_device_speed(devs[i])]);
  1689. monitor_printf(mon, " Class %02x:", ddesc.bDeviceClass);
  1690. monitor_printf(mon, " USB device %04x:%04x",
  1691. ddesc.idVendor, ddesc.idProduct);
  1692. if (ddesc.iProduct) {
  1693. libusb_device_handle *handle;
  1694. if (libusb_open(devs[i], &handle) == 0) {
  1695. unsigned char name[64] = "";
  1696. libusb_get_string_descriptor_ascii(handle,
  1697. ddesc.iProduct,
  1698. name, sizeof(name));
  1699. libusb_close(handle);
  1700. monitor_printf(mon, ", %s", name);
  1701. }
  1702. }
  1703. monitor_printf(mon, "\n");
  1704. }
  1705. libusb_free_device_list(devs, 1);
  1706. }