2
0

host-libusb.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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 <poll.h>
  36. #include <libusb.h>
  37. #include "qemu-common.h"
  38. #include "monitor/monitor.h"
  39. #include "sysemu/sysemu.h"
  40. #include "trace.h"
  41. #include "hw/usb.h"
  42. /* ------------------------------------------------------------------------ */
  43. #define TYPE_USB_HOST_DEVICE "usb-host"
  44. #define USB_HOST_DEVICE(obj) \
  45. OBJECT_CHECK(USBHostDevice, (obj), TYPE_USB_HOST_DEVICE)
  46. typedef struct USBHostDevice USBHostDevice;
  47. typedef struct USBHostRequest USBHostRequest;
  48. typedef struct USBHostIsoXfer USBHostIsoXfer;
  49. typedef struct USBHostIsoRing USBHostIsoRing;
  50. struct USBAutoFilter {
  51. uint32_t bus_num;
  52. uint32_t addr;
  53. char *port;
  54. uint32_t vendor_id;
  55. uint32_t product_id;
  56. };
  57. enum USBHostDeviceOptions {
  58. USB_HOST_OPT_PIPELINE,
  59. };
  60. struct USBHostDevice {
  61. USBDevice parent_obj;
  62. /* properties */
  63. struct USBAutoFilter match;
  64. int32_t bootindex;
  65. uint32_t iso_urb_count;
  66. uint32_t iso_urb_frames;
  67. uint32_t options;
  68. uint32_t loglevel;
  69. /* state */
  70. QTAILQ_ENTRY(USBHostDevice) next;
  71. int seen, errcount;
  72. int bus_num;
  73. int addr;
  74. char port[16];
  75. libusb_device *dev;
  76. libusb_device_handle *dh;
  77. struct libusb_device_descriptor ddesc;
  78. struct {
  79. bool detached;
  80. bool claimed;
  81. } ifs[USB_MAX_INTERFACES];
  82. /* callbacks & friends */
  83. QEMUBH *bh_nodev;
  84. QEMUBH *bh_postld;
  85. Notifier exit;
  86. /* request queues */
  87. QTAILQ_HEAD(, USBHostRequest) requests;
  88. QTAILQ_HEAD(, USBHostIsoRing) isorings;
  89. };
  90. struct USBHostRequest {
  91. USBHostDevice *host;
  92. USBPacket *p;
  93. bool in;
  94. struct libusb_transfer *xfer;
  95. unsigned char *buffer;
  96. unsigned char *cbuf;
  97. unsigned int clen;
  98. QTAILQ_ENTRY(USBHostRequest) next;
  99. };
  100. struct USBHostIsoXfer {
  101. USBHostIsoRing *ring;
  102. struct libusb_transfer *xfer;
  103. bool copy_complete;
  104. unsigned int packet;
  105. QTAILQ_ENTRY(USBHostIsoXfer) next;
  106. };
  107. struct USBHostIsoRing {
  108. USBHostDevice *host;
  109. USBEndpoint *ep;
  110. QTAILQ_HEAD(, USBHostIsoXfer) unused;
  111. QTAILQ_HEAD(, USBHostIsoXfer) inflight;
  112. QTAILQ_HEAD(, USBHostIsoXfer) copy;
  113. QTAILQ_ENTRY(USBHostIsoRing) next;
  114. };
  115. static QTAILQ_HEAD(, USBHostDevice) hostdevs =
  116. QTAILQ_HEAD_INITIALIZER(hostdevs);
  117. static void usb_host_auto_check(void *unused);
  118. static void usb_host_release_interfaces(USBHostDevice *s);
  119. static void usb_host_nodev(USBHostDevice *s);
  120. static void usb_host_detach_kernel(USBHostDevice *s);
  121. static void usb_host_attach_kernel(USBHostDevice *s);
  122. /* ------------------------------------------------------------------------ */
  123. #define CONTROL_TIMEOUT 10000 /* 10 sec */
  124. #define BULK_TIMEOUT 0 /* unlimited */
  125. #define INTR_TIMEOUT 0 /* unlimited */
  126. static const char *speed_name[] = {
  127. [LIBUSB_SPEED_UNKNOWN] = "?",
  128. [LIBUSB_SPEED_LOW] = "1.5",
  129. [LIBUSB_SPEED_FULL] = "12",
  130. [LIBUSB_SPEED_HIGH] = "480",
  131. [LIBUSB_SPEED_SUPER] = "5000",
  132. };
  133. static const unsigned int speed_map[] = {
  134. [LIBUSB_SPEED_LOW] = USB_SPEED_LOW,
  135. [LIBUSB_SPEED_FULL] = USB_SPEED_FULL,
  136. [LIBUSB_SPEED_HIGH] = USB_SPEED_HIGH,
  137. [LIBUSB_SPEED_SUPER] = USB_SPEED_SUPER,
  138. };
  139. static const unsigned int status_map[] = {
  140. [LIBUSB_TRANSFER_COMPLETED] = USB_RET_SUCCESS,
  141. [LIBUSB_TRANSFER_ERROR] = USB_RET_IOERROR,
  142. [LIBUSB_TRANSFER_TIMED_OUT] = USB_RET_IOERROR,
  143. [LIBUSB_TRANSFER_CANCELLED] = USB_RET_IOERROR,
  144. [LIBUSB_TRANSFER_STALL] = USB_RET_STALL,
  145. [LIBUSB_TRANSFER_NO_DEVICE] = USB_RET_NODEV,
  146. [LIBUSB_TRANSFER_OVERFLOW] = USB_RET_BABBLE,
  147. };
  148. static const char *err_names[] = {
  149. [-LIBUSB_ERROR_IO] = "IO",
  150. [-LIBUSB_ERROR_INVALID_PARAM] = "INVALID_PARAM",
  151. [-LIBUSB_ERROR_ACCESS] = "ACCESS",
  152. [-LIBUSB_ERROR_NO_DEVICE] = "NO_DEVICE",
  153. [-LIBUSB_ERROR_NOT_FOUND] = "NOT_FOUND",
  154. [-LIBUSB_ERROR_BUSY] = "BUSY",
  155. [-LIBUSB_ERROR_TIMEOUT] = "TIMEOUT",
  156. [-LIBUSB_ERROR_OVERFLOW] = "OVERFLOW",
  157. [-LIBUSB_ERROR_PIPE] = "PIPE",
  158. [-LIBUSB_ERROR_INTERRUPTED] = "INTERRUPTED",
  159. [-LIBUSB_ERROR_NO_MEM] = "NO_MEM",
  160. [-LIBUSB_ERROR_NOT_SUPPORTED] = "NOT_SUPPORTED",
  161. [-LIBUSB_ERROR_OTHER] = "OTHER",
  162. };
  163. static libusb_context *ctx;
  164. static uint32_t loglevel;
  165. static void usb_host_handle_fd(void *opaque)
  166. {
  167. struct timeval tv = { 0, 0 };
  168. libusb_handle_events_timeout(ctx, &tv);
  169. }
  170. static void usb_host_add_fd(int fd, short events, void *user_data)
  171. {
  172. qemu_set_fd_handler(fd,
  173. (events & POLLIN) ? usb_host_handle_fd : NULL,
  174. (events & POLLOUT) ? usb_host_handle_fd : NULL,
  175. ctx);
  176. }
  177. static void usb_host_del_fd(int fd, void *user_data)
  178. {
  179. qemu_set_fd_handler(fd, NULL, NULL, NULL);
  180. }
  181. static int usb_host_init(void)
  182. {
  183. const struct libusb_pollfd **poll;
  184. int i, rc;
  185. if (ctx) {
  186. return 0;
  187. }
  188. rc = libusb_init(&ctx);
  189. if (rc != 0) {
  190. return -1;
  191. }
  192. libusb_set_debug(ctx, loglevel);
  193. libusb_set_pollfd_notifiers(ctx, usb_host_add_fd,
  194. usb_host_del_fd,
  195. ctx);
  196. poll = libusb_get_pollfds(ctx);
  197. if (poll) {
  198. for (i = 0; poll[i] != NULL; i++) {
  199. usb_host_add_fd(poll[i]->fd, poll[i]->events, ctx);
  200. }
  201. }
  202. free(poll);
  203. return 0;
  204. }
  205. static int usb_host_get_port(libusb_device *dev, char *port, size_t len)
  206. {
  207. uint8_t path[7];
  208. size_t off;
  209. int rc, i;
  210. #if LIBUSBX_API_VERSION >= 0x01000102
  211. rc = libusb_get_port_numbers(dev, path, 7);
  212. #else
  213. rc = libusb_get_port_path(ctx, dev, path, 7);
  214. #endif
  215. if (rc < 0) {
  216. return 0;
  217. }
  218. off = snprintf(port, len, "%d", path[0]);
  219. for (i = 1; i < rc; i++) {
  220. off += snprintf(port+off, len-off, ".%d", path[i]);
  221. }
  222. return off;
  223. }
  224. static void usb_host_libusb_error(const char *func, int rc)
  225. {
  226. const char *errname;
  227. if (rc >= 0) {
  228. return;
  229. }
  230. if (-rc < ARRAY_SIZE(err_names) && err_names[-rc]) {
  231. errname = err_names[-rc];
  232. } else {
  233. errname = "?";
  234. }
  235. fprintf(stderr, "%s: %d [%s]\n", func, rc, errname);
  236. }
  237. /* ------------------------------------------------------------------------ */
  238. static bool usb_host_use_combining(USBEndpoint *ep)
  239. {
  240. int type;
  241. if (!ep->pipeline) {
  242. return false;
  243. }
  244. if (ep->pid != USB_TOKEN_IN) {
  245. return false;
  246. }
  247. type = usb_ep_get_type(ep->dev, ep->pid, ep->nr);
  248. if (type != USB_ENDPOINT_XFER_BULK) {
  249. return false;
  250. }
  251. return true;
  252. }
  253. /* ------------------------------------------------------------------------ */
  254. static USBHostRequest *usb_host_req_alloc(USBHostDevice *s, USBPacket *p,
  255. bool in, size_t bufsize)
  256. {
  257. USBHostRequest *r = g_new0(USBHostRequest, 1);
  258. r->host = s;
  259. r->p = p;
  260. r->in = in;
  261. r->xfer = libusb_alloc_transfer(0);
  262. if (bufsize) {
  263. r->buffer = g_malloc(bufsize);
  264. }
  265. QTAILQ_INSERT_TAIL(&s->requests, r, next);
  266. return r;
  267. }
  268. static void usb_host_req_free(USBHostRequest *r)
  269. {
  270. if (r->host) {
  271. QTAILQ_REMOVE(&r->host->requests, r, next);
  272. }
  273. libusb_free_transfer(r->xfer);
  274. g_free(r->buffer);
  275. g_free(r);
  276. }
  277. static USBHostRequest *usb_host_req_find(USBHostDevice *s, USBPacket *p)
  278. {
  279. USBHostRequest *r;
  280. QTAILQ_FOREACH(r, &s->requests, next) {
  281. if (r->p == p) {
  282. return r;
  283. }
  284. }
  285. return NULL;
  286. }
  287. static void usb_host_req_complete_ctrl(struct libusb_transfer *xfer)
  288. {
  289. USBHostRequest *r = xfer->user_data;
  290. USBHostDevice *s = r->host;
  291. bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
  292. if (r->p == NULL) {
  293. goto out; /* request was canceled */
  294. }
  295. r->p->status = status_map[xfer->status];
  296. r->p->actual_length = xfer->actual_length;
  297. if (r->in && xfer->actual_length) {
  298. memcpy(r->cbuf, r->buffer + 8, xfer->actual_length);
  299. }
  300. trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
  301. r->p->status, r->p->actual_length);
  302. usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
  303. out:
  304. usb_host_req_free(r);
  305. if (disconnect) {
  306. usb_host_nodev(s);
  307. }
  308. }
  309. static void usb_host_req_complete_data(struct libusb_transfer *xfer)
  310. {
  311. USBHostRequest *r = xfer->user_data;
  312. USBHostDevice *s = r->host;
  313. bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
  314. if (r->p == NULL) {
  315. goto out; /* request was canceled */
  316. }
  317. r->p->status = status_map[xfer->status];
  318. if (r->in && xfer->actual_length) {
  319. usb_packet_copy(r->p, r->buffer, xfer->actual_length);
  320. }
  321. trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
  322. r->p->status, r->p->actual_length);
  323. if (usb_host_use_combining(r->p->ep)) {
  324. usb_combined_input_packet_complete(USB_DEVICE(s), r->p);
  325. } else {
  326. usb_packet_complete(USB_DEVICE(s), r->p);
  327. }
  328. out:
  329. usb_host_req_free(r);
  330. if (disconnect) {
  331. usb_host_nodev(s);
  332. }
  333. }
  334. static void usb_host_req_abort(USBHostRequest *r)
  335. {
  336. USBHostDevice *s = r->host;
  337. bool inflight = (r->p && r->p->state == USB_PACKET_ASYNC);
  338. if (inflight) {
  339. r->p->status = USB_RET_NODEV;
  340. trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
  341. r->p->status, r->p->actual_length);
  342. if (r->p->ep->nr == 0) {
  343. usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
  344. } else {
  345. usb_packet_complete(USB_DEVICE(s), r->p);
  346. }
  347. r->p = NULL;
  348. }
  349. QTAILQ_REMOVE(&r->host->requests, r, next);
  350. r->host = NULL;
  351. if (inflight) {
  352. libusb_cancel_transfer(r->xfer);
  353. }
  354. }
  355. /* ------------------------------------------------------------------------ */
  356. static void usb_host_req_complete_iso(struct libusb_transfer *transfer)
  357. {
  358. USBHostIsoXfer *xfer = transfer->user_data;
  359. if (!xfer) {
  360. /* USBHostIsoXfer released while inflight */
  361. g_free(transfer->buffer);
  362. libusb_free_transfer(transfer);
  363. return;
  364. }
  365. QTAILQ_REMOVE(&xfer->ring->inflight, xfer, next);
  366. if (QTAILQ_EMPTY(&xfer->ring->inflight)) {
  367. USBHostDevice *s = xfer->ring->host;
  368. trace_usb_host_iso_stop(s->bus_num, s->addr, xfer->ring->ep->nr);
  369. }
  370. if (xfer->ring->ep->pid == USB_TOKEN_IN) {
  371. QTAILQ_INSERT_TAIL(&xfer->ring->copy, xfer, next);
  372. } else {
  373. QTAILQ_INSERT_TAIL(&xfer->ring->unused, xfer, next);
  374. }
  375. }
  376. static USBHostIsoRing *usb_host_iso_alloc(USBHostDevice *s, USBEndpoint *ep)
  377. {
  378. USBHostIsoRing *ring = g_new0(USBHostIsoRing, 1);
  379. USBHostIsoXfer *xfer;
  380. /* FIXME: check interval (for now assume one xfer per frame) */
  381. int packets = s->iso_urb_frames;
  382. int i;
  383. ring->host = s;
  384. ring->ep = ep;
  385. QTAILQ_INIT(&ring->unused);
  386. QTAILQ_INIT(&ring->inflight);
  387. QTAILQ_INIT(&ring->copy);
  388. QTAILQ_INSERT_TAIL(&s->isorings, ring, next);
  389. for (i = 0; i < s->iso_urb_count; i++) {
  390. xfer = g_new0(USBHostIsoXfer, 1);
  391. xfer->ring = ring;
  392. xfer->xfer = libusb_alloc_transfer(packets);
  393. xfer->xfer->dev_handle = s->dh;
  394. xfer->xfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
  395. xfer->xfer->endpoint = ring->ep->nr;
  396. if (ring->ep->pid == USB_TOKEN_IN) {
  397. xfer->xfer->endpoint |= USB_DIR_IN;
  398. }
  399. xfer->xfer->callback = usb_host_req_complete_iso;
  400. xfer->xfer->user_data = xfer;
  401. xfer->xfer->num_iso_packets = packets;
  402. xfer->xfer->length = ring->ep->max_packet_size * packets;
  403. xfer->xfer->buffer = g_malloc0(xfer->xfer->length);
  404. QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
  405. }
  406. return ring;
  407. }
  408. static USBHostIsoRing *usb_host_iso_find(USBHostDevice *s, USBEndpoint *ep)
  409. {
  410. USBHostIsoRing *ring;
  411. QTAILQ_FOREACH(ring, &s->isorings, next) {
  412. if (ring->ep == ep) {
  413. return ring;
  414. }
  415. }
  416. return NULL;
  417. }
  418. static void usb_host_iso_reset_xfer(USBHostIsoXfer *xfer)
  419. {
  420. libusb_set_iso_packet_lengths(xfer->xfer,
  421. xfer->ring->ep->max_packet_size);
  422. xfer->packet = 0;
  423. xfer->copy_complete = false;
  424. }
  425. static void usb_host_iso_free_xfer(USBHostIsoXfer *xfer, bool inflight)
  426. {
  427. if (inflight) {
  428. xfer->xfer->user_data = NULL;
  429. } else {
  430. g_free(xfer->xfer->buffer);
  431. libusb_free_transfer(xfer->xfer);
  432. }
  433. g_free(xfer);
  434. }
  435. static void usb_host_iso_free(USBHostIsoRing *ring)
  436. {
  437. USBHostIsoXfer *xfer;
  438. while ((xfer = QTAILQ_FIRST(&ring->inflight)) != NULL) {
  439. QTAILQ_REMOVE(&ring->inflight, xfer, next);
  440. usb_host_iso_free_xfer(xfer, true);
  441. }
  442. while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
  443. QTAILQ_REMOVE(&ring->unused, xfer, next);
  444. usb_host_iso_free_xfer(xfer, false);
  445. }
  446. while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL) {
  447. QTAILQ_REMOVE(&ring->copy, xfer, next);
  448. usb_host_iso_free_xfer(xfer, false);
  449. }
  450. QTAILQ_REMOVE(&ring->host->isorings, ring, next);
  451. g_free(ring);
  452. }
  453. static void usb_host_iso_free_all(USBHostDevice *s)
  454. {
  455. USBHostIsoRing *ring;
  456. while ((ring = QTAILQ_FIRST(&s->isorings)) != NULL) {
  457. usb_host_iso_free(ring);
  458. }
  459. }
  460. static bool usb_host_iso_data_copy(USBHostIsoXfer *xfer, USBPacket *p)
  461. {
  462. unsigned int psize;
  463. unsigned char *buf;
  464. buf = libusb_get_iso_packet_buffer_simple(xfer->xfer, xfer->packet);
  465. if (p->pid == USB_TOKEN_OUT) {
  466. psize = p->iov.size;
  467. if (psize > xfer->ring->ep->max_packet_size) {
  468. /* should not happen (guest bug) */
  469. psize = xfer->ring->ep->max_packet_size;
  470. }
  471. xfer->xfer->iso_packet_desc[xfer->packet].length = psize;
  472. } else {
  473. psize = xfer->xfer->iso_packet_desc[xfer->packet].actual_length;
  474. if (psize > p->iov.size) {
  475. /* should not happen (guest bug) */
  476. psize = p->iov.size;
  477. }
  478. }
  479. usb_packet_copy(p, buf, psize);
  480. xfer->packet++;
  481. xfer->copy_complete = (xfer->packet == xfer->xfer->num_iso_packets);
  482. return xfer->copy_complete;
  483. }
  484. static void usb_host_iso_data_in(USBHostDevice *s, USBPacket *p)
  485. {
  486. USBHostIsoRing *ring;
  487. USBHostIsoXfer *xfer;
  488. bool disconnect = false;
  489. int rc;
  490. ring = usb_host_iso_find(s, p->ep);
  491. if (ring == NULL) {
  492. ring = usb_host_iso_alloc(s, p->ep);
  493. }
  494. /* copy data to guest */
  495. xfer = QTAILQ_FIRST(&ring->copy);
  496. if (xfer != NULL) {
  497. if (usb_host_iso_data_copy(xfer, p)) {
  498. QTAILQ_REMOVE(&ring->copy, xfer, next);
  499. QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
  500. }
  501. }
  502. /* submit empty bufs to host */
  503. while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
  504. QTAILQ_REMOVE(&ring->unused, xfer, next);
  505. usb_host_iso_reset_xfer(xfer);
  506. rc = libusb_submit_transfer(xfer->xfer);
  507. if (rc != 0) {
  508. usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
  509. QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
  510. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  511. disconnect = true;
  512. }
  513. break;
  514. }
  515. if (QTAILQ_EMPTY(&ring->inflight)) {
  516. trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
  517. }
  518. QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
  519. }
  520. if (disconnect) {
  521. usb_host_nodev(s);
  522. }
  523. }
  524. static void usb_host_iso_data_out(USBHostDevice *s, USBPacket *p)
  525. {
  526. USBHostIsoRing *ring;
  527. USBHostIsoXfer *xfer;
  528. bool disconnect = false;
  529. int rc, filled = 0;
  530. ring = usb_host_iso_find(s, p->ep);
  531. if (ring == NULL) {
  532. ring = usb_host_iso_alloc(s, p->ep);
  533. }
  534. /* copy data from guest */
  535. xfer = QTAILQ_FIRST(&ring->copy);
  536. while (xfer != NULL && xfer->copy_complete) {
  537. filled++;
  538. xfer = QTAILQ_NEXT(xfer, next);
  539. }
  540. if (xfer == NULL) {
  541. xfer = QTAILQ_FIRST(&ring->unused);
  542. if (xfer == NULL) {
  543. trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, p->ep->nr);
  544. return;
  545. }
  546. QTAILQ_REMOVE(&ring->unused, xfer, next);
  547. usb_host_iso_reset_xfer(xfer);
  548. QTAILQ_INSERT_TAIL(&ring->copy, xfer, next);
  549. }
  550. usb_host_iso_data_copy(xfer, p);
  551. if (QTAILQ_EMPTY(&ring->inflight)) {
  552. /* wait until half of our buffers are filled
  553. before kicking the iso out stream */
  554. if (filled*2 < s->iso_urb_count) {
  555. return;
  556. }
  557. }
  558. /* submit filled bufs to host */
  559. while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL &&
  560. xfer->copy_complete) {
  561. QTAILQ_REMOVE(&ring->copy, xfer, next);
  562. rc = libusb_submit_transfer(xfer->xfer);
  563. if (rc != 0) {
  564. usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
  565. QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
  566. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  567. disconnect = true;
  568. }
  569. break;
  570. }
  571. if (QTAILQ_EMPTY(&ring->inflight)) {
  572. trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
  573. }
  574. QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
  575. }
  576. if (disconnect) {
  577. usb_host_nodev(s);
  578. }
  579. }
  580. /* ------------------------------------------------------------------------ */
  581. static bool usb_host_full_speed_compat(USBHostDevice *s)
  582. {
  583. struct libusb_config_descriptor *conf;
  584. const struct libusb_interface_descriptor *intf;
  585. const struct libusb_endpoint_descriptor *endp;
  586. uint8_t type;
  587. int rc, c, i, a, e;
  588. for (c = 0;; c++) {
  589. rc = libusb_get_config_descriptor(s->dev, c, &conf);
  590. if (rc != 0) {
  591. break;
  592. }
  593. for (i = 0; i < conf->bNumInterfaces; i++) {
  594. for (a = 0; a < conf->interface[i].num_altsetting; a++) {
  595. intf = &conf->interface[i].altsetting[a];
  596. for (e = 0; e < intf->bNumEndpoints; e++) {
  597. endp = &intf->endpoint[e];
  598. type = endp->bmAttributes & 0x3;
  599. switch (type) {
  600. case 0x01: /* ISO */
  601. return false;
  602. case 0x03: /* INTERRUPT */
  603. if (endp->wMaxPacketSize > 64) {
  604. return false;
  605. }
  606. break;
  607. }
  608. }
  609. }
  610. }
  611. libusb_free_config_descriptor(conf);
  612. }
  613. return true;
  614. }
  615. static void usb_host_ep_update(USBHostDevice *s)
  616. {
  617. static const char *tname[] = {
  618. [USB_ENDPOINT_XFER_CONTROL] = "control",
  619. [USB_ENDPOINT_XFER_ISOC] = "isoc",
  620. [USB_ENDPOINT_XFER_BULK] = "bulk",
  621. [USB_ENDPOINT_XFER_INT] = "int",
  622. };
  623. USBDevice *udev = USB_DEVICE(s);
  624. struct libusb_config_descriptor *conf;
  625. const struct libusb_interface_descriptor *intf;
  626. const struct libusb_endpoint_descriptor *endp;
  627. uint8_t devep, type;
  628. int pid, ep;
  629. int rc, i, e;
  630. usb_ep_reset(udev);
  631. rc = libusb_get_active_config_descriptor(s->dev, &conf);
  632. if (rc != 0) {
  633. return;
  634. }
  635. trace_usb_host_parse_config(s->bus_num, s->addr,
  636. conf->bConfigurationValue, true);
  637. for (i = 0; i < conf->bNumInterfaces; i++) {
  638. assert(udev->altsetting[i] < conf->interface[i].num_altsetting);
  639. intf = &conf->interface[i].altsetting[udev->altsetting[i]];
  640. trace_usb_host_parse_interface(s->bus_num, s->addr,
  641. intf->bInterfaceNumber,
  642. intf->bAlternateSetting, true);
  643. for (e = 0; e < intf->bNumEndpoints; e++) {
  644. endp = &intf->endpoint[e];
  645. devep = endp->bEndpointAddress;
  646. pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
  647. ep = devep & 0xf;
  648. type = endp->bmAttributes & 0x3;
  649. if (ep == 0) {
  650. trace_usb_host_parse_error(s->bus_num, s->addr,
  651. "invalid endpoint address");
  652. return;
  653. }
  654. if (usb_ep_get_type(udev, pid, ep) != USB_ENDPOINT_XFER_INVALID) {
  655. trace_usb_host_parse_error(s->bus_num, s->addr,
  656. "duplicate endpoint address");
  657. return;
  658. }
  659. trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep,
  660. (devep & USB_DIR_IN) ? "in" : "out",
  661. tname[type], true);
  662. usb_ep_set_max_packet_size(udev, pid, ep,
  663. endp->wMaxPacketSize);
  664. usb_ep_set_type(udev, pid, ep, type);
  665. usb_ep_set_ifnum(udev, pid, ep, i);
  666. usb_ep_set_halted(udev, pid, ep, 0);
  667. }
  668. }
  669. libusb_free_config_descriptor(conf);
  670. }
  671. static int usb_host_open(USBHostDevice *s, libusb_device *dev)
  672. {
  673. USBDevice *udev = USB_DEVICE(s);
  674. int bus_num = libusb_get_bus_number(dev);
  675. int addr = libusb_get_device_address(dev);
  676. int rc;
  677. trace_usb_host_open_started(bus_num, addr);
  678. if (s->dh != NULL) {
  679. goto fail;
  680. }
  681. rc = libusb_open(dev, &s->dh);
  682. if (rc != 0) {
  683. goto fail;
  684. }
  685. s->dev = dev;
  686. s->bus_num = bus_num;
  687. s->addr = addr;
  688. usb_host_detach_kernel(s);
  689. libusb_get_device_descriptor(dev, &s->ddesc);
  690. usb_host_get_port(s->dev, s->port, sizeof(s->port));
  691. usb_ep_init(udev);
  692. usb_host_ep_update(s);
  693. udev->speed = speed_map[libusb_get_device_speed(dev)];
  694. udev->speedmask = (1 << udev->speed);
  695. if (udev->speed == USB_SPEED_HIGH && usb_host_full_speed_compat(s)) {
  696. udev->speedmask |= USB_SPEED_MASK_FULL;
  697. }
  698. if (s->ddesc.iProduct) {
  699. libusb_get_string_descriptor_ascii(s->dh, s->ddesc.iProduct,
  700. (unsigned char *)udev->product_desc,
  701. sizeof(udev->product_desc));
  702. } else {
  703. snprintf(udev->product_desc, sizeof(udev->product_desc),
  704. "host:%d.%d", bus_num, addr);
  705. }
  706. rc = usb_device_attach(udev);
  707. if (rc) {
  708. goto fail;
  709. }
  710. trace_usb_host_open_success(bus_num, addr);
  711. return 0;
  712. fail:
  713. trace_usb_host_open_failure(bus_num, addr);
  714. if (s->dh != NULL) {
  715. libusb_close(s->dh);
  716. s->dh = NULL;
  717. s->dev = NULL;
  718. }
  719. return -1;
  720. }
  721. static void usb_host_abort_xfers(USBHostDevice *s)
  722. {
  723. USBHostRequest *r, *rtmp;
  724. QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) {
  725. usb_host_req_abort(r);
  726. }
  727. }
  728. static int usb_host_close(USBHostDevice *s)
  729. {
  730. USBDevice *udev = USB_DEVICE(s);
  731. if (s->dh == NULL) {
  732. return -1;
  733. }
  734. trace_usb_host_close(s->bus_num, s->addr);
  735. usb_host_abort_xfers(s);
  736. usb_host_iso_free_all(s);
  737. if (udev->attached) {
  738. usb_device_detach(udev);
  739. }
  740. usb_host_release_interfaces(s);
  741. libusb_reset_device(s->dh);
  742. usb_host_attach_kernel(s);
  743. libusb_close(s->dh);
  744. s->dh = NULL;
  745. s->dev = NULL;
  746. usb_host_auto_check(NULL);
  747. return 0;
  748. }
  749. static void usb_host_nodev_bh(void *opaque)
  750. {
  751. USBHostDevice *s = opaque;
  752. usb_host_close(s);
  753. }
  754. static void usb_host_nodev(USBHostDevice *s)
  755. {
  756. if (!s->bh_nodev) {
  757. s->bh_nodev = qemu_bh_new(usb_host_nodev_bh, s);
  758. }
  759. qemu_bh_schedule(s->bh_nodev);
  760. }
  761. static void usb_host_exit_notifier(struct Notifier *n, void *data)
  762. {
  763. USBHostDevice *s = container_of(n, USBHostDevice, exit);
  764. if (s->dh) {
  765. usb_host_release_interfaces(s);
  766. usb_host_attach_kernel(s);
  767. }
  768. }
  769. static int usb_host_initfn(USBDevice *udev)
  770. {
  771. USBHostDevice *s = USB_HOST_DEVICE(udev);
  772. loglevel = s->loglevel;
  773. udev->flags |= (1 << USB_DEV_FLAG_IS_HOST);
  774. udev->auto_attach = 0;
  775. QTAILQ_INIT(&s->requests);
  776. QTAILQ_INIT(&s->isorings);
  777. s->exit.notify = usb_host_exit_notifier;
  778. qemu_add_exit_notifier(&s->exit);
  779. QTAILQ_INSERT_TAIL(&hostdevs, s, next);
  780. add_boot_device_path(s->bootindex, &udev->qdev, NULL);
  781. usb_host_auto_check(NULL);
  782. return 0;
  783. }
  784. static void usb_host_handle_destroy(USBDevice *udev)
  785. {
  786. USBHostDevice *s = USB_HOST_DEVICE(udev);
  787. qemu_remove_exit_notifier(&s->exit);
  788. QTAILQ_REMOVE(&hostdevs, s, next);
  789. usb_host_close(s);
  790. }
  791. static void usb_host_cancel_packet(USBDevice *udev, USBPacket *p)
  792. {
  793. USBHostDevice *s = USB_HOST_DEVICE(udev);
  794. USBHostRequest *r;
  795. if (p->combined) {
  796. usb_combined_packet_cancel(udev, p);
  797. return;
  798. }
  799. trace_usb_host_req_canceled(s->bus_num, s->addr, p);
  800. r = usb_host_req_find(s, p);
  801. if (r && r->p) {
  802. r->p = NULL; /* mark as dead */
  803. libusb_cancel_transfer(r->xfer);
  804. }
  805. }
  806. static void usb_host_detach_kernel(USBHostDevice *s)
  807. {
  808. struct libusb_config_descriptor *conf;
  809. int rc, i;
  810. rc = libusb_get_active_config_descriptor(s->dev, &conf);
  811. if (rc != 0) {
  812. return;
  813. }
  814. for (i = 0; i < conf->bNumInterfaces; i++) {
  815. rc = libusb_kernel_driver_active(s->dh, i);
  816. usb_host_libusb_error("libusb_kernel_driver_active", rc);
  817. if (rc != 1) {
  818. continue;
  819. }
  820. trace_usb_host_detach_kernel(s->bus_num, s->addr, i);
  821. rc = libusb_detach_kernel_driver(s->dh, i);
  822. usb_host_libusb_error("libusb_detach_kernel_driver", rc);
  823. s->ifs[i].detached = true;
  824. }
  825. libusb_free_config_descriptor(conf);
  826. }
  827. static void usb_host_attach_kernel(USBHostDevice *s)
  828. {
  829. struct libusb_config_descriptor *conf;
  830. int rc, i;
  831. rc = libusb_get_active_config_descriptor(s->dev, &conf);
  832. if (rc != 0) {
  833. return;
  834. }
  835. for (i = 0; i < conf->bNumInterfaces; i++) {
  836. if (!s->ifs[i].detached) {
  837. continue;
  838. }
  839. trace_usb_host_attach_kernel(s->bus_num, s->addr, i);
  840. libusb_attach_kernel_driver(s->dh, i);
  841. s->ifs[i].detached = false;
  842. }
  843. libusb_free_config_descriptor(conf);
  844. }
  845. static int usb_host_claim_interfaces(USBHostDevice *s, int configuration)
  846. {
  847. USBDevice *udev = USB_DEVICE(s);
  848. struct libusb_config_descriptor *conf;
  849. int rc, i;
  850. for (i = 0; i < USB_MAX_INTERFACES; i++) {
  851. udev->altsetting[i] = 0;
  852. }
  853. udev->ninterfaces = 0;
  854. udev->configuration = 0;
  855. usb_host_detach_kernel(s);
  856. rc = libusb_get_active_config_descriptor(s->dev, &conf);
  857. if (rc != 0) {
  858. if (rc == LIBUSB_ERROR_NOT_FOUND) {
  859. /* address state - ignore */
  860. return USB_RET_SUCCESS;
  861. }
  862. return USB_RET_STALL;
  863. }
  864. for (i = 0; i < conf->bNumInterfaces; i++) {
  865. trace_usb_host_claim_interface(s->bus_num, s->addr, configuration, i);
  866. rc = libusb_claim_interface(s->dh, i);
  867. usb_host_libusb_error("libusb_claim_interface", rc);
  868. if (rc != 0) {
  869. return USB_RET_STALL;
  870. }
  871. s->ifs[i].claimed = true;
  872. }
  873. udev->ninterfaces = conf->bNumInterfaces;
  874. udev->configuration = configuration;
  875. libusb_free_config_descriptor(conf);
  876. return USB_RET_SUCCESS;
  877. }
  878. static void usb_host_release_interfaces(USBHostDevice *s)
  879. {
  880. USBDevice *udev = USB_DEVICE(s);
  881. int i, rc;
  882. for (i = 0; i < udev->ninterfaces; i++) {
  883. if (!s->ifs[i].claimed) {
  884. continue;
  885. }
  886. trace_usb_host_release_interface(s->bus_num, s->addr, i);
  887. rc = libusb_release_interface(s->dh, i);
  888. usb_host_libusb_error("libusb_release_interface", rc);
  889. s->ifs[i].claimed = false;
  890. }
  891. }
  892. static void usb_host_set_address(USBHostDevice *s, int addr)
  893. {
  894. USBDevice *udev = USB_DEVICE(s);
  895. trace_usb_host_set_address(s->bus_num, s->addr, addr);
  896. udev->addr = addr;
  897. }
  898. static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
  899. {
  900. int rc;
  901. trace_usb_host_set_config(s->bus_num, s->addr, config);
  902. usb_host_release_interfaces(s);
  903. rc = libusb_set_configuration(s->dh, config);
  904. if (rc != 0) {
  905. usb_host_libusb_error("libusb_set_configuration", rc);
  906. p->status = USB_RET_STALL;
  907. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  908. usb_host_nodev(s);
  909. }
  910. return;
  911. }
  912. p->status = usb_host_claim_interfaces(s, config);
  913. if (p->status != USB_RET_SUCCESS) {
  914. return;
  915. }
  916. usb_host_ep_update(s);
  917. }
  918. static void usb_host_set_interface(USBHostDevice *s, int iface, int alt,
  919. USBPacket *p)
  920. {
  921. USBDevice *udev = USB_DEVICE(s);
  922. int rc;
  923. trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
  924. usb_host_iso_free_all(s);
  925. if (iface >= USB_MAX_INTERFACES) {
  926. p->status = USB_RET_STALL;
  927. return;
  928. }
  929. rc = libusb_set_interface_alt_setting(s->dh, iface, alt);
  930. if (rc != 0) {
  931. usb_host_libusb_error("libusb_set_interface_alt_setting", rc);
  932. p->status = USB_RET_STALL;
  933. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  934. usb_host_nodev(s);
  935. }
  936. return;
  937. }
  938. udev->altsetting[iface] = alt;
  939. usb_host_ep_update(s);
  940. }
  941. static void usb_host_handle_control(USBDevice *udev, USBPacket *p,
  942. int request, int value, int index,
  943. int length, uint8_t *data)
  944. {
  945. USBHostDevice *s = USB_HOST_DEVICE(udev);
  946. USBHostRequest *r;
  947. int rc;
  948. trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index);
  949. if (s->dh == NULL) {
  950. p->status = USB_RET_NODEV;
  951. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  952. return;
  953. }
  954. switch (request) {
  955. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  956. usb_host_set_address(s, value);
  957. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  958. return;
  959. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  960. usb_host_set_config(s, value & 0xff, p);
  961. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  962. return;
  963. case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
  964. usb_host_set_interface(s, index, value, p);
  965. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  966. return;
  967. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  968. if (value == 0) { /* clear halt */
  969. int pid = (index & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
  970. libusb_clear_halt(s->dh, index);
  971. usb_ep_set_halted(udev, pid, index & 0x0f, 0);
  972. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  973. return;
  974. }
  975. }
  976. r = usb_host_req_alloc(s, p, (request >> 8) & USB_DIR_IN, length + 8);
  977. r->cbuf = data;
  978. r->clen = length;
  979. memcpy(r->buffer, udev->setup_buf, 8);
  980. if (!r->in) {
  981. memcpy(r->buffer + 8, r->cbuf, r->clen);
  982. }
  983. libusb_fill_control_transfer(r->xfer, s->dh, r->buffer,
  984. usb_host_req_complete_ctrl, r,
  985. CONTROL_TIMEOUT);
  986. rc = libusb_submit_transfer(r->xfer);
  987. if (rc != 0) {
  988. p->status = USB_RET_NODEV;
  989. trace_usb_host_req_complete(s->bus_num, s->addr, p,
  990. p->status, p->actual_length);
  991. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  992. usb_host_nodev(s);
  993. }
  994. return;
  995. }
  996. p->status = USB_RET_ASYNC;
  997. }
  998. static void usb_host_handle_data(USBDevice *udev, USBPacket *p)
  999. {
  1000. USBHostDevice *s = USB_HOST_DEVICE(udev);
  1001. USBHostRequest *r;
  1002. size_t size;
  1003. int ep, rc;
  1004. if (usb_host_use_combining(p->ep) && p->state == USB_PACKET_SETUP) {
  1005. p->status = USB_RET_ADD_TO_QUEUE;
  1006. return;
  1007. }
  1008. trace_usb_host_req_data(s->bus_num, s->addr, p,
  1009. p->pid == USB_TOKEN_IN,
  1010. p->ep->nr, p->iov.size);
  1011. if (s->dh == NULL) {
  1012. p->status = USB_RET_NODEV;
  1013. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  1014. return;
  1015. }
  1016. if (p->ep->halted) {
  1017. p->status = USB_RET_STALL;
  1018. trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
  1019. return;
  1020. }
  1021. switch (usb_ep_get_type(udev, p->pid, p->ep->nr)) {
  1022. case USB_ENDPOINT_XFER_BULK:
  1023. size = usb_packet_size(p);
  1024. r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, size);
  1025. if (!r->in) {
  1026. usb_packet_copy(p, r->buffer, size);
  1027. }
  1028. ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
  1029. libusb_fill_bulk_transfer(r->xfer, s->dh, ep,
  1030. r->buffer, size,
  1031. usb_host_req_complete_data, r,
  1032. BULK_TIMEOUT);
  1033. break;
  1034. case USB_ENDPOINT_XFER_INT:
  1035. r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, p->iov.size);
  1036. if (!r->in) {
  1037. usb_packet_copy(p, r->buffer, p->iov.size);
  1038. }
  1039. ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
  1040. libusb_fill_interrupt_transfer(r->xfer, s->dh, ep,
  1041. r->buffer, p->iov.size,
  1042. usb_host_req_complete_data, r,
  1043. INTR_TIMEOUT);
  1044. break;
  1045. case USB_ENDPOINT_XFER_ISOC:
  1046. if (p->pid == USB_TOKEN_IN) {
  1047. usb_host_iso_data_in(s, p);
  1048. } else {
  1049. usb_host_iso_data_out(s, p);
  1050. }
  1051. trace_usb_host_req_complete(s->bus_num, s->addr, p,
  1052. p->status, p->actual_length);
  1053. return;
  1054. default:
  1055. p->status = USB_RET_STALL;
  1056. trace_usb_host_req_complete(s->bus_num, s->addr, p,
  1057. p->status, p->actual_length);
  1058. return;
  1059. }
  1060. rc = libusb_submit_transfer(r->xfer);
  1061. if (rc != 0) {
  1062. p->status = USB_RET_NODEV;
  1063. trace_usb_host_req_complete(s->bus_num, s->addr, p,
  1064. p->status, p->actual_length);
  1065. if (rc == LIBUSB_ERROR_NO_DEVICE) {
  1066. usb_host_nodev(s);
  1067. }
  1068. return;
  1069. }
  1070. p->status = USB_RET_ASYNC;
  1071. }
  1072. static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
  1073. {
  1074. if (usb_host_use_combining(ep)) {
  1075. usb_ep_combine_input_packets(ep);
  1076. }
  1077. }
  1078. static void usb_host_handle_reset(USBDevice *udev)
  1079. {
  1080. USBHostDevice *s = USB_HOST_DEVICE(udev);
  1081. int rc;
  1082. trace_usb_host_reset(s->bus_num, s->addr);
  1083. rc = libusb_reset_device(s->dh);
  1084. if (rc != 0) {
  1085. usb_host_nodev(s);
  1086. }
  1087. }
  1088. /*
  1089. * This is *NOT* about restoring state. We have absolutely no idea
  1090. * what state the host device is in at the moment and whenever it is
  1091. * still present in the first place. Attemping to contine where we
  1092. * left off is impossible.
  1093. *
  1094. * What we are going to to to here is emulate a surprise removal of
  1095. * the usb device passed through, then kick host scan so the device
  1096. * will get re-attached (and re-initialized by the guest) in case it
  1097. * is still present.
  1098. *
  1099. * As the device removal will change the state of other devices (usb
  1100. * host controller, most likely interrupt controller too) we have to
  1101. * wait with it until *all* vmstate is loaded. Thus post_load just
  1102. * kicks a bottom half which then does the actual work.
  1103. */
  1104. static void usb_host_post_load_bh(void *opaque)
  1105. {
  1106. USBHostDevice *dev = opaque;
  1107. USBDevice *udev = USB_DEVICE(dev);
  1108. if (dev->dh != NULL) {
  1109. usb_host_close(dev);
  1110. }
  1111. if (udev->attached) {
  1112. usb_device_detach(udev);
  1113. }
  1114. usb_host_auto_check(NULL);
  1115. }
  1116. static int usb_host_post_load(void *opaque, int version_id)
  1117. {
  1118. USBHostDevice *dev = opaque;
  1119. if (!dev->bh_postld) {
  1120. dev->bh_postld = qemu_bh_new(usb_host_post_load_bh, dev);
  1121. }
  1122. qemu_bh_schedule(dev->bh_postld);
  1123. return 0;
  1124. }
  1125. static const VMStateDescription vmstate_usb_host = {
  1126. .name = "usb-host",
  1127. .version_id = 1,
  1128. .minimum_version_id = 1,
  1129. .post_load = usb_host_post_load,
  1130. .fields = (VMStateField[]) {
  1131. VMSTATE_USB_DEVICE(parent_obj, USBHostDevice),
  1132. VMSTATE_END_OF_LIST()
  1133. }
  1134. };
  1135. static Property usb_host_dev_properties[] = {
  1136. DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
  1137. DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
  1138. DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
  1139. DEFINE_PROP_UINT32("vendorid", USBHostDevice, match.vendor_id, 0),
  1140. DEFINE_PROP_UINT32("productid", USBHostDevice, match.product_id, 0),
  1141. DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
  1142. DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32),
  1143. DEFINE_PROP_INT32("bootindex", USBHostDevice, bootindex, -1),
  1144. DEFINE_PROP_UINT32("loglevel", USBHostDevice, loglevel,
  1145. LIBUSB_LOG_LEVEL_WARNING),
  1146. DEFINE_PROP_BIT("pipeline", USBHostDevice, options,
  1147. USB_HOST_OPT_PIPELINE, true),
  1148. DEFINE_PROP_END_OF_LIST(),
  1149. };
  1150. static void usb_host_class_initfn(ObjectClass *klass, void *data)
  1151. {
  1152. DeviceClass *dc = DEVICE_CLASS(klass);
  1153. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  1154. uc->init = usb_host_initfn;
  1155. uc->product_desc = "USB Host Device";
  1156. uc->cancel_packet = usb_host_cancel_packet;
  1157. uc->handle_data = usb_host_handle_data;
  1158. uc->handle_control = usb_host_handle_control;
  1159. uc->handle_reset = usb_host_handle_reset;
  1160. uc->handle_destroy = usb_host_handle_destroy;
  1161. uc->flush_ep_queue = usb_host_flush_ep_queue;
  1162. dc->vmsd = &vmstate_usb_host;
  1163. dc->props = usb_host_dev_properties;
  1164. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  1165. }
  1166. static TypeInfo usb_host_dev_info = {
  1167. .name = TYPE_USB_HOST_DEVICE,
  1168. .parent = TYPE_USB_DEVICE,
  1169. .instance_size = sizeof(USBHostDevice),
  1170. .class_init = usb_host_class_initfn,
  1171. };
  1172. static void usb_host_register_types(void)
  1173. {
  1174. type_register_static(&usb_host_dev_info);
  1175. }
  1176. type_init(usb_host_register_types)
  1177. /* ------------------------------------------------------------------------ */
  1178. static QEMUTimer *usb_auto_timer;
  1179. static VMChangeStateEntry *usb_vmstate;
  1180. static void usb_host_vm_state(void *unused, int running, RunState state)
  1181. {
  1182. if (running) {
  1183. usb_host_auto_check(unused);
  1184. }
  1185. }
  1186. static void usb_host_auto_check(void *unused)
  1187. {
  1188. struct USBHostDevice *s;
  1189. struct USBAutoFilter *f;
  1190. libusb_device **devs;
  1191. struct libusb_device_descriptor ddesc;
  1192. int unconnected = 0;
  1193. int i, n;
  1194. if (usb_host_init() != 0) {
  1195. return;
  1196. }
  1197. if (runstate_is_running()) {
  1198. n = libusb_get_device_list(ctx, &devs);
  1199. for (i = 0; i < n; i++) {
  1200. if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
  1201. continue;
  1202. }
  1203. if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
  1204. continue;
  1205. }
  1206. QTAILQ_FOREACH(s, &hostdevs, next) {
  1207. f = &s->match;
  1208. if (f->bus_num > 0 &&
  1209. f->bus_num != libusb_get_bus_number(devs[i])) {
  1210. continue;
  1211. }
  1212. if (f->addr > 0 &&
  1213. f->addr != libusb_get_device_address(devs[i])) {
  1214. continue;
  1215. }
  1216. if (f->port != NULL) {
  1217. char port[16] = "-";
  1218. usb_host_get_port(devs[i], port, sizeof(port));
  1219. if (strcmp(f->port, port) != 0) {
  1220. continue;
  1221. }
  1222. }
  1223. if (f->vendor_id > 0 &&
  1224. f->vendor_id != ddesc.idVendor) {
  1225. continue;
  1226. }
  1227. if (f->product_id > 0 &&
  1228. f->product_id != ddesc.idProduct) {
  1229. continue;
  1230. }
  1231. /* We got a match */
  1232. s->seen++;
  1233. if (s->errcount >= 3) {
  1234. continue;
  1235. }
  1236. if (s->dh != NULL) {
  1237. continue;
  1238. }
  1239. if (usb_host_open(s, devs[i]) < 0) {
  1240. s->errcount++;
  1241. continue;
  1242. }
  1243. break;
  1244. }
  1245. }
  1246. libusb_free_device_list(devs, 1);
  1247. QTAILQ_FOREACH(s, &hostdevs, next) {
  1248. if (s->dh == NULL) {
  1249. unconnected++;
  1250. }
  1251. if (s->seen == 0) {
  1252. if (s->dh) {
  1253. usb_host_close(s);
  1254. }
  1255. s->errcount = 0;
  1256. }
  1257. s->seen = 0;
  1258. }
  1259. #if 0
  1260. if (unconnected == 0) {
  1261. /* nothing to watch */
  1262. if (usb_auto_timer) {
  1263. timer_del(usb_auto_timer);
  1264. trace_usb_host_auto_scan_disabled();
  1265. }
  1266. return;
  1267. }
  1268. #endif
  1269. }
  1270. if (!usb_vmstate) {
  1271. usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);
  1272. }
  1273. if (!usb_auto_timer) {
  1274. usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, usb_host_auto_check, NULL);
  1275. if (!usb_auto_timer) {
  1276. return;
  1277. }
  1278. trace_usb_host_auto_scan_enabled();
  1279. }
  1280. timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000);
  1281. }
  1282. void usb_host_info(Monitor *mon, const QDict *qdict)
  1283. {
  1284. libusb_device **devs;
  1285. struct libusb_device_descriptor ddesc;
  1286. char port[16];
  1287. int i, n;
  1288. if (usb_host_init() != 0) {
  1289. return;
  1290. }
  1291. n = libusb_get_device_list(ctx, &devs);
  1292. for (i = 0; i < n; i++) {
  1293. if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
  1294. continue;
  1295. }
  1296. if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
  1297. continue;
  1298. }
  1299. usb_host_get_port(devs[i], port, sizeof(port));
  1300. monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
  1301. libusb_get_bus_number(devs[i]),
  1302. libusb_get_device_address(devs[i]),
  1303. port,
  1304. speed_name[libusb_get_device_speed(devs[i])]);
  1305. monitor_printf(mon, " Class %02x:", ddesc.bDeviceClass);
  1306. monitor_printf(mon, " USB device %04x:%04x",
  1307. ddesc.idVendor, ddesc.idProduct);
  1308. if (ddesc.iProduct) {
  1309. libusb_device_handle *handle;
  1310. if (libusb_open(devs[i], &handle) == 0) {
  1311. unsigned char name[64] = "";
  1312. libusb_get_string_descriptor_ascii(handle,
  1313. ddesc.iProduct,
  1314. name, sizeof(name));
  1315. libusb_close(handle);
  1316. monitor_printf(mon, ", %s", name);
  1317. }
  1318. }
  1319. monitor_printf(mon, "\n");
  1320. }
  1321. libusb_free_device_list(devs, 1);
  1322. }