2
0

host-libusb.c 56 KB

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