2
0

host-libusb.c 53 KB

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