host-libusb.c 58 KB

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