2
0

usb-linux.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  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. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. * THE SOFTWARE.
  31. */
  32. #include "qemu-common.h"
  33. #include "qemu-timer.h"
  34. #include "console.h"
  35. #include <dirent.h>
  36. #include <sys/ioctl.h>
  37. #include <signal.h>
  38. #include <linux/usbdevice_fs.h>
  39. #include <linux/version.h>
  40. #include "hw/usb.h"
  41. /* We redefine it to avoid version problems */
  42. struct usb_ctrltransfer {
  43. uint8_t bRequestType;
  44. uint8_t bRequest;
  45. uint16_t wValue;
  46. uint16_t wIndex;
  47. uint16_t wLength;
  48. uint32_t timeout;
  49. void *data;
  50. };
  51. struct usb_ctrlrequest {
  52. uint8_t bRequestType;
  53. uint8_t bRequest;
  54. uint16_t wValue;
  55. uint16_t wIndex;
  56. uint16_t wLength;
  57. };
  58. typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
  59. int vendor_id, int product_id,
  60. const char *product_name, int speed);
  61. static int usb_host_find_device(int *pbus_num, int *paddr,
  62. char *product_name, int product_name_size,
  63. const char *devname);
  64. //#define DEBUG
  65. #ifdef DEBUG
  66. #define dprintf printf
  67. #else
  68. #define dprintf(...)
  69. #endif
  70. #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
  71. #define USBPROCBUS_PATH "/proc/bus/usb"
  72. #define PRODUCT_NAME_SZ 32
  73. #define MAX_ENDPOINTS 16
  74. #define USBDEVBUS_PATH "/dev/bus/usb"
  75. #define USBSYSBUS_PATH "/sys/bus/usb"
  76. static char *usb_host_device_path;
  77. #define USB_FS_NONE 0
  78. #define USB_FS_PROC 1
  79. #define USB_FS_DEV 2
  80. #define USB_FS_SYS 3
  81. static int usb_fs_type;
  82. /* endpoint association data */
  83. struct endp_data {
  84. uint8_t type;
  85. uint8_t halted;
  86. };
  87. enum {
  88. CTRL_STATE_IDLE = 0,
  89. CTRL_STATE_SETUP,
  90. CTRL_STATE_DATA,
  91. CTRL_STATE_ACK
  92. };
  93. /*
  94. * Control transfer state.
  95. * Note that 'buffer' _must_ follow 'req' field because
  96. * we need contigious buffer when we submit control URB.
  97. */
  98. struct ctrl_struct {
  99. uint16_t len;
  100. uint16_t offset;
  101. uint8_t state;
  102. struct usb_ctrlrequest req;
  103. uint8_t buffer[1024];
  104. };
  105. typedef struct USBHostDevice {
  106. USBDevice dev;
  107. int fd;
  108. uint8_t descr[1024];
  109. int descr_len;
  110. int configuration;
  111. int ninterfaces;
  112. int closing;
  113. struct ctrl_struct ctrl;
  114. struct endp_data endp_table[MAX_ENDPOINTS];
  115. /* Host side address */
  116. int bus_num;
  117. int addr;
  118. struct USBHostDevice *next;
  119. } USBHostDevice;
  120. static int is_isoc(USBHostDevice *s, int ep)
  121. {
  122. return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
  123. }
  124. static int is_halted(USBHostDevice *s, int ep)
  125. {
  126. return s->endp_table[ep - 1].halted;
  127. }
  128. static void clear_halt(USBHostDevice *s, int ep)
  129. {
  130. s->endp_table[ep - 1].halted = 0;
  131. }
  132. static void set_halt(USBHostDevice *s, int ep)
  133. {
  134. s->endp_table[ep - 1].halted = 1;
  135. }
  136. static USBHostDevice *hostdev_list;
  137. static void hostdev_link(USBHostDevice *dev)
  138. {
  139. dev->next = hostdev_list;
  140. hostdev_list = dev;
  141. }
  142. static void hostdev_unlink(USBHostDevice *dev)
  143. {
  144. USBHostDevice *pdev = hostdev_list;
  145. USBHostDevice **prev = &hostdev_list;
  146. while (pdev) {
  147. if (pdev == dev) {
  148. *prev = dev->next;
  149. return;
  150. }
  151. prev = &pdev->next;
  152. pdev = pdev->next;
  153. }
  154. }
  155. static USBHostDevice *hostdev_find(int bus_num, int addr)
  156. {
  157. USBHostDevice *s = hostdev_list;
  158. while (s) {
  159. if (s->bus_num == bus_num && s->addr == addr)
  160. return s;
  161. s = s->next;
  162. }
  163. return NULL;
  164. }
  165. /*
  166. * Async URB state.
  167. * We always allocate one isoc descriptor even for bulk transfers
  168. * to simplify allocation and casts.
  169. */
  170. typedef struct AsyncURB
  171. {
  172. struct usbdevfs_urb urb;
  173. struct usbdevfs_iso_packet_desc isocpd;
  174. USBPacket *packet;
  175. USBHostDevice *hdev;
  176. } AsyncURB;
  177. static AsyncURB *async_alloc(void)
  178. {
  179. return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
  180. }
  181. static void async_free(AsyncURB *aurb)
  182. {
  183. qemu_free(aurb);
  184. }
  185. static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
  186. {
  187. switch(s->ctrl.state) {
  188. case CTRL_STATE_SETUP:
  189. if (p->len < s->ctrl.len)
  190. s->ctrl.len = p->len;
  191. s->ctrl.state = CTRL_STATE_DATA;
  192. p->len = 8;
  193. break;
  194. case CTRL_STATE_ACK:
  195. s->ctrl.state = CTRL_STATE_IDLE;
  196. p->len = 0;
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. static void async_complete(void *opaque)
  203. {
  204. USBHostDevice *s = opaque;
  205. AsyncURB *aurb;
  206. while (1) {
  207. USBPacket *p;
  208. int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
  209. if (r < 0) {
  210. if (errno == EAGAIN)
  211. return;
  212. if (errno == ENODEV && !s->closing) {
  213. printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
  214. usb_device_del_addr(0, s->dev.addr);
  215. return;
  216. }
  217. dprintf("husb: async. reap urb failed errno %d\n", errno);
  218. return;
  219. }
  220. p = aurb->packet;
  221. dprintf("husb: async completed. aurb %p status %d alen %d\n",
  222. aurb, aurb->urb.status, aurb->urb.actual_length);
  223. if (p) {
  224. switch (aurb->urb.status) {
  225. case 0:
  226. p->len = aurb->urb.actual_length;
  227. if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL)
  228. async_complete_ctrl(s, p);
  229. break;
  230. case -EPIPE:
  231. set_halt(s, p->devep);
  232. /* fall through */
  233. default:
  234. p->len = USB_RET_NAK;
  235. break;
  236. }
  237. usb_packet_complete(p);
  238. }
  239. async_free(aurb);
  240. }
  241. }
  242. static void async_cancel(USBPacket *unused, void *opaque)
  243. {
  244. AsyncURB *aurb = opaque;
  245. USBHostDevice *s = aurb->hdev;
  246. dprintf("husb: async cancel. aurb %p\n", aurb);
  247. /* Mark it as dead (see async_complete above) */
  248. aurb->packet = NULL;
  249. int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
  250. if (r < 0) {
  251. dprintf("husb: async. discard urb failed errno %d\n", errno);
  252. }
  253. }
  254. static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
  255. {
  256. int dev_descr_len, config_descr_len;
  257. int interface, nb_interfaces, nb_configurations;
  258. int ret, i;
  259. if (configuration == 0) /* address state - ignore */
  260. return 1;
  261. dprintf("husb: claiming interfaces. config %d\n", configuration);
  262. i = 0;
  263. dev_descr_len = dev->descr[0];
  264. if (dev_descr_len > dev->descr_len)
  265. goto fail;
  266. nb_configurations = dev->descr[17];
  267. i += dev_descr_len;
  268. while (i < dev->descr_len) {
  269. dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
  270. dev->descr[i], dev->descr[i+1]);
  271. if (dev->descr[i+1] != USB_DT_CONFIG) {
  272. i += dev->descr[i];
  273. continue;
  274. }
  275. config_descr_len = dev->descr[i];
  276. printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
  277. if (configuration < 0 || configuration == dev->descr[i + 5]) {
  278. configuration = dev->descr[i + 5];
  279. break;
  280. }
  281. i += config_descr_len;
  282. }
  283. if (i >= dev->descr_len) {
  284. fprintf(stderr, "husb: update iface failed. no matching configuration\n");
  285. goto fail;
  286. }
  287. nb_interfaces = dev->descr[i + 4];
  288. #ifdef USBDEVFS_DISCONNECT
  289. /* earlier Linux 2.4 do not support that */
  290. {
  291. struct usbdevfs_ioctl ctrl;
  292. for (interface = 0; interface < nb_interfaces; interface++) {
  293. ctrl.ioctl_code = USBDEVFS_DISCONNECT;
  294. ctrl.ifno = interface;
  295. ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
  296. if (ret < 0 && errno != ENODATA) {
  297. perror("USBDEVFS_DISCONNECT");
  298. goto fail;
  299. }
  300. }
  301. }
  302. #endif
  303. /* XXX: only grab if all interfaces are free */
  304. for (interface = 0; interface < nb_interfaces; interface++) {
  305. ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
  306. if (ret < 0) {
  307. if (errno == EBUSY) {
  308. printf("husb: update iface. device already grabbed\n");
  309. } else {
  310. perror("husb: failed to claim interface");
  311. }
  312. fail:
  313. return 0;
  314. }
  315. }
  316. printf("husb: %d interfaces claimed for configuration %d\n",
  317. nb_interfaces, configuration);
  318. dev->ninterfaces = nb_interfaces;
  319. dev->configuration = configuration;
  320. return 1;
  321. }
  322. static int usb_host_release_interfaces(USBHostDevice *s)
  323. {
  324. int ret, i;
  325. dprintf("husb: releasing interfaces\n");
  326. for (i = 0; i < s->ninterfaces; i++) {
  327. ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
  328. if (ret < 0) {
  329. perror("husb: failed to release interface");
  330. return 0;
  331. }
  332. }
  333. return 1;
  334. }
  335. static void usb_host_handle_reset(USBDevice *dev)
  336. {
  337. USBHostDevice *s = (USBHostDevice *) dev;
  338. dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
  339. ioctl(s->fd, USBDEVFS_RESET);
  340. usb_host_claim_interfaces(s, s->configuration);
  341. }
  342. static void usb_host_handle_destroy(USBDevice *dev)
  343. {
  344. USBHostDevice *s = (USBHostDevice *)dev;
  345. s->closing = 1;
  346. qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
  347. hostdev_unlink(s);
  348. async_complete(s);
  349. if (s->fd >= 0)
  350. close(s->fd);
  351. qemu_free(s);
  352. }
  353. static int usb_linux_update_endp_table(USBHostDevice *s);
  354. static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
  355. {
  356. struct usbdevfs_urb *urb;
  357. AsyncURB *aurb;
  358. int ret;
  359. aurb = async_alloc();
  360. aurb->hdev = s;
  361. aurb->packet = p;
  362. urb = &aurb->urb;
  363. if (p->pid == USB_TOKEN_IN)
  364. urb->endpoint = p->devep | 0x80;
  365. else
  366. urb->endpoint = p->devep;
  367. if (is_halted(s, p->devep)) {
  368. ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
  369. if (ret < 0) {
  370. dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
  371. urb->endpoint, errno);
  372. return USB_RET_NAK;
  373. }
  374. clear_halt(s, p->devep);
  375. }
  376. urb->buffer = p->data;
  377. urb->buffer_length = p->len;
  378. if (is_isoc(s, p->devep)) {
  379. /* Setup ISOC transfer */
  380. urb->type = USBDEVFS_URB_TYPE_ISO;
  381. urb->flags = USBDEVFS_URB_ISO_ASAP;
  382. urb->number_of_packets = 1;
  383. urb->iso_frame_desc[0].length = p->len;
  384. } else {
  385. /* Setup bulk transfer */
  386. urb->type = USBDEVFS_URB_TYPE_BULK;
  387. }
  388. urb->usercontext = s;
  389. ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
  390. dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
  391. if (ret < 0) {
  392. dprintf("husb: submit failed. errno %d\n", errno);
  393. async_free(aurb);
  394. switch(errno) {
  395. case ETIMEDOUT:
  396. return USB_RET_NAK;
  397. case EPIPE:
  398. default:
  399. return USB_RET_STALL;
  400. }
  401. }
  402. usb_defer_packet(p, async_cancel, aurb);
  403. return USB_RET_ASYNC;
  404. }
  405. static int ctrl_error(void)
  406. {
  407. if (errno == ETIMEDOUT)
  408. return USB_RET_NAK;
  409. else
  410. return USB_RET_STALL;
  411. }
  412. static int usb_host_set_address(USBHostDevice *s, int addr)
  413. {
  414. dprintf("husb: ctrl set addr %u\n", addr);
  415. s->dev.addr = addr;
  416. return 0;
  417. }
  418. static int usb_host_set_config(USBHostDevice *s, int config)
  419. {
  420. usb_host_release_interfaces(s);
  421. int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
  422. dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
  423. if (ret < 0)
  424. return ctrl_error();
  425. usb_host_claim_interfaces(s, config);
  426. return 0;
  427. }
  428. static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
  429. {
  430. struct usbdevfs_setinterface si;
  431. int ret;
  432. si.interface = iface;
  433. si.altsetting = alt;
  434. ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
  435. dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
  436. iface, alt, ret, errno);
  437. if (ret < 0)
  438. return ctrl_error();
  439. usb_linux_update_endp_table(s);
  440. return 0;
  441. }
  442. static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
  443. {
  444. struct usbdevfs_urb *urb;
  445. AsyncURB *aurb;
  446. int ret, value, index;
  447. /*
  448. * Process certain standard device requests.
  449. * These are infrequent and are processed synchronously.
  450. */
  451. value = le16_to_cpu(s->ctrl.req.wValue);
  452. index = le16_to_cpu(s->ctrl.req.wIndex);
  453. dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
  454. s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
  455. s->ctrl.len);
  456. if (s->ctrl.req.bRequestType == 0) {
  457. switch (s->ctrl.req.bRequest) {
  458. case USB_REQ_SET_ADDRESS:
  459. return usb_host_set_address(s, value);
  460. case USB_REQ_SET_CONFIGURATION:
  461. return usb_host_set_config(s, value & 0xff);
  462. }
  463. }
  464. if (s->ctrl.req.bRequestType == 1 &&
  465. s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
  466. return usb_host_set_interface(s, index, value);
  467. /* The rest are asynchronous */
  468. aurb = async_alloc();
  469. aurb->hdev = s;
  470. aurb->packet = p;
  471. /*
  472. * Setup ctrl transfer.
  473. *
  474. * s->ctrl is layed out such that data buffer immediately follows
  475. * 'req' struct which is exactly what usbdevfs expects.
  476. */
  477. urb = &aurb->urb;
  478. urb->type = USBDEVFS_URB_TYPE_CONTROL;
  479. urb->endpoint = p->devep;
  480. urb->buffer = &s->ctrl.req;
  481. urb->buffer_length = 8 + s->ctrl.len;
  482. urb->usercontext = s;
  483. ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
  484. dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
  485. if (ret < 0) {
  486. dprintf("husb: submit failed. errno %d\n", errno);
  487. async_free(aurb);
  488. switch(errno) {
  489. case ETIMEDOUT:
  490. return USB_RET_NAK;
  491. case EPIPE:
  492. default:
  493. return USB_RET_STALL;
  494. }
  495. }
  496. usb_defer_packet(p, async_cancel, aurb);
  497. return USB_RET_ASYNC;
  498. }
  499. static int do_token_setup(USBDevice *dev, USBPacket *p)
  500. {
  501. USBHostDevice *s = (USBHostDevice *) dev;
  502. int ret = 0;
  503. if (p->len != 8)
  504. return USB_RET_STALL;
  505. memcpy(&s->ctrl.req, p->data, 8);
  506. s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
  507. s->ctrl.offset = 0;
  508. s->ctrl.state = CTRL_STATE_SETUP;
  509. if (s->ctrl.req.bRequestType & USB_DIR_IN) {
  510. ret = usb_host_handle_control(s, p);
  511. if (ret < 0)
  512. return ret;
  513. if (ret < s->ctrl.len)
  514. s->ctrl.len = ret;
  515. s->ctrl.state = CTRL_STATE_DATA;
  516. } else {
  517. if (s->ctrl.len == 0)
  518. s->ctrl.state = CTRL_STATE_ACK;
  519. else
  520. s->ctrl.state = CTRL_STATE_DATA;
  521. }
  522. return ret;
  523. }
  524. static int do_token_in(USBDevice *dev, USBPacket *p)
  525. {
  526. USBHostDevice *s = (USBHostDevice *) dev;
  527. int ret = 0;
  528. if (p->devep != 0)
  529. return usb_host_handle_data(s, p);
  530. switch(s->ctrl.state) {
  531. case CTRL_STATE_ACK:
  532. if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
  533. ret = usb_host_handle_control(s, p);
  534. if (ret == USB_RET_ASYNC)
  535. return USB_RET_ASYNC;
  536. s->ctrl.state = CTRL_STATE_IDLE;
  537. return ret > 0 ? 0 : ret;
  538. }
  539. return 0;
  540. case CTRL_STATE_DATA:
  541. if (s->ctrl.req.bRequestType & USB_DIR_IN) {
  542. int len = s->ctrl.len - s->ctrl.offset;
  543. if (len > p->len)
  544. len = p->len;
  545. memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
  546. s->ctrl.offset += len;
  547. if (s->ctrl.offset >= s->ctrl.len)
  548. s->ctrl.state = CTRL_STATE_ACK;
  549. return len;
  550. }
  551. s->ctrl.state = CTRL_STATE_IDLE;
  552. return USB_RET_STALL;
  553. default:
  554. return USB_RET_STALL;
  555. }
  556. }
  557. static int do_token_out(USBDevice *dev, USBPacket *p)
  558. {
  559. USBHostDevice *s = (USBHostDevice *) dev;
  560. if (p->devep != 0)
  561. return usb_host_handle_data(s, p);
  562. switch(s->ctrl.state) {
  563. case CTRL_STATE_ACK:
  564. if (s->ctrl.req.bRequestType & USB_DIR_IN) {
  565. s->ctrl.state = CTRL_STATE_IDLE;
  566. /* transfer OK */
  567. } else {
  568. /* ignore additional output */
  569. }
  570. return 0;
  571. case CTRL_STATE_DATA:
  572. if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
  573. int len = s->ctrl.len - s->ctrl.offset;
  574. if (len > p->len)
  575. len = p->len;
  576. memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
  577. s->ctrl.offset += len;
  578. if (s->ctrl.offset >= s->ctrl.len)
  579. s->ctrl.state = CTRL_STATE_ACK;
  580. return len;
  581. }
  582. s->ctrl.state = CTRL_STATE_IDLE;
  583. return USB_RET_STALL;
  584. default:
  585. return USB_RET_STALL;
  586. }
  587. }
  588. /*
  589. * Packet handler.
  590. * Called by the HC (host controller).
  591. *
  592. * Returns length of the transaction or one of the USB_RET_XXX codes.
  593. */
  594. static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
  595. {
  596. switch(p->pid) {
  597. case USB_MSG_ATTACH:
  598. s->state = USB_STATE_ATTACHED;
  599. return 0;
  600. case USB_MSG_DETACH:
  601. s->state = USB_STATE_NOTATTACHED;
  602. return 0;
  603. case USB_MSG_RESET:
  604. s->remote_wakeup = 0;
  605. s->addr = 0;
  606. s->state = USB_STATE_DEFAULT;
  607. s->handle_reset(s);
  608. return 0;
  609. }
  610. /* Rest of the PIDs must match our address */
  611. if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
  612. return USB_RET_NODEV;
  613. switch (p->pid) {
  614. case USB_TOKEN_SETUP:
  615. return do_token_setup(s, p);
  616. case USB_TOKEN_IN:
  617. return do_token_in(s, p);
  618. case USB_TOKEN_OUT:
  619. return do_token_out(s, p);
  620. default:
  621. return USB_RET_STALL;
  622. }
  623. }
  624. /* returns 1 on problem encountered or 0 for success */
  625. static int usb_linux_update_endp_table(USBHostDevice *s)
  626. {
  627. uint8_t *descriptors;
  628. uint8_t devep, type, configuration, alt_interface;
  629. struct usb_ctrltransfer ct;
  630. int interface, ret, length, i;
  631. ct.bRequestType = USB_DIR_IN;
  632. ct.bRequest = USB_REQ_GET_CONFIGURATION;
  633. ct.wValue = 0;
  634. ct.wIndex = 0;
  635. ct.wLength = 1;
  636. ct.data = &configuration;
  637. ct.timeout = 50;
  638. ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
  639. if (ret < 0) {
  640. perror("usb_linux_update_endp_table");
  641. return 1;
  642. }
  643. /* in address state */
  644. if (configuration == 0)
  645. return 1;
  646. /* get the desired configuration, interface, and endpoint descriptors
  647. * from device description */
  648. descriptors = &s->descr[18];
  649. length = s->descr_len - 18;
  650. i = 0;
  651. if (descriptors[i + 1] != USB_DT_CONFIG ||
  652. descriptors[i + 5] != configuration) {
  653. dprintf("invalid descriptor data - configuration\n");
  654. return 1;
  655. }
  656. i += descriptors[i];
  657. while (i < length) {
  658. if (descriptors[i + 1] != USB_DT_INTERFACE ||
  659. (descriptors[i + 1] == USB_DT_INTERFACE &&
  660. descriptors[i + 4] == 0)) {
  661. i += descriptors[i];
  662. continue;
  663. }
  664. interface = descriptors[i + 2];
  665. ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
  666. ct.bRequest = USB_REQ_GET_INTERFACE;
  667. ct.wValue = 0;
  668. ct.wIndex = interface;
  669. ct.wLength = 1;
  670. ct.data = &alt_interface;
  671. ct.timeout = 50;
  672. ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
  673. if (ret < 0) {
  674. perror("usb_linux_update_endp_table");
  675. return 1;
  676. }
  677. /* the current interface descriptor is the active interface
  678. * and has endpoints */
  679. if (descriptors[i + 3] != alt_interface) {
  680. i += descriptors[i];
  681. continue;
  682. }
  683. /* advance to the endpoints */
  684. while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
  685. i += descriptors[i];
  686. if (i >= length)
  687. break;
  688. while (i < length) {
  689. if (descriptors[i + 1] != USB_DT_ENDPOINT)
  690. break;
  691. devep = descriptors[i + 2];
  692. switch (descriptors[i + 3] & 0x3) {
  693. case 0x00:
  694. type = USBDEVFS_URB_TYPE_CONTROL;
  695. break;
  696. case 0x01:
  697. type = USBDEVFS_URB_TYPE_ISO;
  698. break;
  699. case 0x02:
  700. type = USBDEVFS_URB_TYPE_BULK;
  701. break;
  702. case 0x03:
  703. type = USBDEVFS_URB_TYPE_INTERRUPT;
  704. break;
  705. default:
  706. dprintf("usb_host: malformed endpoint type\n");
  707. type = USBDEVFS_URB_TYPE_BULK;
  708. }
  709. s->endp_table[(devep & 0xf) - 1].type = type;
  710. s->endp_table[(devep & 0xf) - 1].halted = 0;
  711. i += descriptors[i];
  712. }
  713. }
  714. return 0;
  715. }
  716. static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name)
  717. {
  718. int fd = -1, ret;
  719. USBHostDevice *dev = NULL;
  720. struct usbdevfs_connectinfo ci;
  721. char buf[1024];
  722. dev = qemu_mallocz(sizeof(USBHostDevice));
  723. dev->bus_num = bus_num;
  724. dev->addr = addr;
  725. printf("husb: open device %d.%d\n", bus_num, addr);
  726. if (!usb_host_device_path) {
  727. perror("husb: USB Host Device Path not set");
  728. goto fail;
  729. }
  730. snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
  731. bus_num, addr);
  732. fd = open(buf, O_RDWR | O_NONBLOCK);
  733. if (fd < 0) {
  734. perror(buf);
  735. goto fail;
  736. }
  737. dprintf("husb: opened %s\n", buf);
  738. /* read the device description */
  739. dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
  740. if (dev->descr_len <= 0) {
  741. perror("husb: reading device data failed");
  742. goto fail;
  743. }
  744. #ifdef DEBUG
  745. {
  746. int x;
  747. printf("=== begin dumping device descriptor data ===\n");
  748. for (x = 0; x < dev->descr_len; x++)
  749. printf("%02x ", dev->descr[x]);
  750. printf("\n=== end dumping device descriptor data ===\n");
  751. }
  752. #endif
  753. dev->fd = fd;
  754. /*
  755. * Initial configuration is -1 which makes us claim first
  756. * available config. We used to start with 1, which does not
  757. * always work. I've seen devices where first config starts
  758. * with 2.
  759. */
  760. if (!usb_host_claim_interfaces(dev, -1))
  761. goto fail;
  762. ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
  763. if (ret < 0) {
  764. perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
  765. goto fail;
  766. }
  767. printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
  768. ret = usb_linux_update_endp_table(dev);
  769. if (ret)
  770. goto fail;
  771. if (ci.slow)
  772. dev->dev.speed = USB_SPEED_LOW;
  773. else
  774. dev->dev.speed = USB_SPEED_HIGH;
  775. dev->dev.handle_packet = usb_host_handle_packet;
  776. dev->dev.handle_reset = usb_host_handle_reset;
  777. dev->dev.handle_destroy = usb_host_handle_destroy;
  778. if (!prod_name || prod_name[0] == '\0')
  779. snprintf(dev->dev.devname, sizeof(dev->dev.devname),
  780. "host:%d.%d", bus_num, addr);
  781. else
  782. pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
  783. prod_name);
  784. /* USB devio uses 'write' flag to check for async completions */
  785. qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
  786. hostdev_link(dev);
  787. return (USBDevice *) dev;
  788. fail:
  789. if (dev)
  790. qemu_free(dev);
  791. close(fd);
  792. return NULL;
  793. }
  794. static int usb_host_auto_add(const char *spec);
  795. static int usb_host_auto_del(const char *spec);
  796. USBDevice *usb_host_device_open(const char *devname)
  797. {
  798. int bus_num, addr;
  799. char product_name[PRODUCT_NAME_SZ];
  800. if (strstr(devname, "auto:")) {
  801. usb_host_auto_add(devname);
  802. return NULL;
  803. }
  804. if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
  805. devname) < 0)
  806. return NULL;
  807. if (hostdev_find(bus_num, addr)) {
  808. term_printf("husb: host usb device %d.%d is already open\n", bus_num, addr);
  809. return NULL;
  810. }
  811. return usb_host_device_open_addr(bus_num, addr, product_name);
  812. }
  813. int usb_host_device_close(const char *devname)
  814. {
  815. char product_name[PRODUCT_NAME_SZ];
  816. int bus_num, addr;
  817. USBHostDevice *s;
  818. if (strstr(devname, "auto:"))
  819. return usb_host_auto_del(devname);
  820. if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
  821. devname) < 0)
  822. return -1;
  823. s = hostdev_find(bus_num, addr);
  824. if (s) {
  825. usb_device_del_addr(0, s->dev.addr);
  826. return 0;
  827. }
  828. return -1;
  829. }
  830. static int get_tag_value(char *buf, int buf_size,
  831. const char *str, const char *tag,
  832. const char *stopchars)
  833. {
  834. const char *p;
  835. char *q;
  836. p = strstr(str, tag);
  837. if (!p)
  838. return -1;
  839. p += strlen(tag);
  840. while (qemu_isspace(*p))
  841. p++;
  842. q = buf;
  843. while (*p != '\0' && !strchr(stopchars, *p)) {
  844. if ((q - buf) < (buf_size - 1))
  845. *q++ = *p;
  846. p++;
  847. }
  848. *q = '\0';
  849. return q - buf;
  850. }
  851. /*
  852. * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
  853. * host's USB devices. This is legacy support since many distributions
  854. * are moving to /sys/bus/usb
  855. */
  856. static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
  857. {
  858. FILE *f = 0;
  859. char line[1024];
  860. char buf[1024];
  861. int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
  862. char product_name[512];
  863. int ret = 0;
  864. if (!usb_host_device_path) {
  865. perror("husb: USB Host Device Path not set");
  866. goto the_end;
  867. }
  868. snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
  869. f = fopen(line, "r");
  870. if (!f) {
  871. perror("husb: cannot open devices file");
  872. goto the_end;
  873. }
  874. device_count = 0;
  875. bus_num = addr = speed = class_id = product_id = vendor_id = 0;
  876. for(;;) {
  877. if (fgets(line, sizeof(line), f) == NULL)
  878. break;
  879. if (strlen(line) > 0)
  880. line[strlen(line) - 1] = '\0';
  881. if (line[0] == 'T' && line[1] == ':') {
  882. if (device_count && (vendor_id || product_id)) {
  883. /* New device. Add the previously discovered device. */
  884. ret = func(opaque, bus_num, addr, class_id, vendor_id,
  885. product_id, product_name, speed);
  886. if (ret)
  887. goto the_end;
  888. }
  889. if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
  890. goto fail;
  891. bus_num = atoi(buf);
  892. if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
  893. goto fail;
  894. addr = atoi(buf);
  895. if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
  896. goto fail;
  897. if (!strcmp(buf, "480"))
  898. speed = USB_SPEED_HIGH;
  899. else if (!strcmp(buf, "1.5"))
  900. speed = USB_SPEED_LOW;
  901. else
  902. speed = USB_SPEED_FULL;
  903. product_name[0] = '\0';
  904. class_id = 0xff;
  905. device_count++;
  906. product_id = 0;
  907. vendor_id = 0;
  908. } else if (line[0] == 'P' && line[1] == ':') {
  909. if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
  910. goto fail;
  911. vendor_id = strtoul(buf, NULL, 16);
  912. if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
  913. goto fail;
  914. product_id = strtoul(buf, NULL, 16);
  915. } else if (line[0] == 'S' && line[1] == ':') {
  916. if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
  917. goto fail;
  918. pstrcpy(product_name, sizeof(product_name), buf);
  919. } else if (line[0] == 'D' && line[1] == ':') {
  920. if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
  921. goto fail;
  922. class_id = strtoul(buf, NULL, 16);
  923. }
  924. fail: ;
  925. }
  926. if (device_count && (vendor_id || product_id)) {
  927. /* Add the last device. */
  928. ret = func(opaque, bus_num, addr, class_id, vendor_id,
  929. product_id, product_name, speed);
  930. }
  931. the_end:
  932. if (f)
  933. fclose(f);
  934. return ret;
  935. }
  936. /*
  937. * Read sys file-system device file
  938. *
  939. * @line address of buffer to put file contents in
  940. * @line_size size of line
  941. * @device_file path to device file (printf format string)
  942. * @device_name device being opened (inserted into device_file)
  943. *
  944. * @return 0 failed, 1 succeeded ('line' contains data)
  945. */
  946. static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
  947. {
  948. FILE *f;
  949. int ret = 0;
  950. char filename[PATH_MAX];
  951. snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
  952. device_file);
  953. f = fopen(filename, "r");
  954. if (f) {
  955. fgets(line, line_size, f);
  956. fclose(f);
  957. ret = 1;
  958. } else {
  959. term_printf("husb: could not open %s\n", filename);
  960. }
  961. return ret;
  962. }
  963. /*
  964. * Use /sys/bus/usb/devices/ directory to determine host's USB
  965. * devices.
  966. *
  967. * This code is based on Robert Schiele's original patches posted to
  968. * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
  969. */
  970. static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
  971. {
  972. DIR *dir = 0;
  973. char line[1024];
  974. int bus_num, addr, speed, class_id, product_id, vendor_id;
  975. int ret = 0;
  976. char product_name[512];
  977. struct dirent *de;
  978. dir = opendir(USBSYSBUS_PATH "/devices");
  979. if (!dir) {
  980. perror("husb: cannot open devices directory");
  981. goto the_end;
  982. }
  983. while ((de = readdir(dir))) {
  984. if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
  985. char *tmpstr = de->d_name;
  986. if (!strncmp(de->d_name, "usb", 3))
  987. tmpstr += 3;
  988. bus_num = atoi(tmpstr);
  989. if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name))
  990. goto the_end;
  991. if (sscanf(line, "%d", &addr) != 1)
  992. goto the_end;
  993. if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
  994. de->d_name))
  995. goto the_end;
  996. if (sscanf(line, "%x", &class_id) != 1)
  997. goto the_end;
  998. if (!usb_host_read_file(line, sizeof(line), "idVendor", de->d_name))
  999. goto the_end;
  1000. if (sscanf(line, "%x", &vendor_id) != 1)
  1001. goto the_end;
  1002. if (!usb_host_read_file(line, sizeof(line), "idProduct",
  1003. de->d_name))
  1004. goto the_end;
  1005. if (sscanf(line, "%x", &product_id) != 1)
  1006. goto the_end;
  1007. if (!usb_host_read_file(line, sizeof(line), "product",
  1008. de->d_name)) {
  1009. *product_name = 0;
  1010. } else {
  1011. if (strlen(line) > 0)
  1012. line[strlen(line) - 1] = '\0';
  1013. pstrcpy(product_name, sizeof(product_name), line);
  1014. }
  1015. if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name))
  1016. goto the_end;
  1017. if (!strcmp(line, "480\n"))
  1018. speed = USB_SPEED_HIGH;
  1019. else if (!strcmp(line, "1.5\n"))
  1020. speed = USB_SPEED_LOW;
  1021. else
  1022. speed = USB_SPEED_FULL;
  1023. ret = func(opaque, bus_num, addr, class_id, vendor_id,
  1024. product_id, product_name, speed);
  1025. if (ret)
  1026. goto the_end;
  1027. }
  1028. }
  1029. the_end:
  1030. if (dir)
  1031. closedir(dir);
  1032. return ret;
  1033. }
  1034. /*
  1035. * Determine how to access the host's USB devices and call the
  1036. * specific support function.
  1037. */
  1038. static int usb_host_scan(void *opaque, USBScanFunc *func)
  1039. {
  1040. FILE *f = 0;
  1041. DIR *dir = 0;
  1042. int ret = 0;
  1043. const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
  1044. char devpath[PATH_MAX];
  1045. /* only check the host once */
  1046. if (!usb_fs_type) {
  1047. f = fopen(USBPROCBUS_PATH "/devices", "r");
  1048. if (f) {
  1049. /* devices found in /proc/bus/usb/ */
  1050. strcpy(devpath, USBPROCBUS_PATH);
  1051. usb_fs_type = USB_FS_PROC;
  1052. fclose(f);
  1053. dprintf(USBDBG_DEVOPENED, USBPROCBUS_PATH);
  1054. goto found_devices;
  1055. }
  1056. /* try additional methods if an access method hasn't been found yet */
  1057. f = fopen(USBDEVBUS_PATH "/devices", "r");
  1058. if (f) {
  1059. /* devices found in /dev/bus/usb/ */
  1060. strcpy(devpath, USBDEVBUS_PATH);
  1061. usb_fs_type = USB_FS_DEV;
  1062. fclose(f);
  1063. dprintf(USBDBG_DEVOPENED, USBDEVBUS_PATH);
  1064. goto found_devices;
  1065. }
  1066. dir = opendir(USBSYSBUS_PATH "/devices");
  1067. if (dir) {
  1068. /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
  1069. strcpy(devpath, USBDEVBUS_PATH);
  1070. usb_fs_type = USB_FS_SYS;
  1071. closedir(dir);
  1072. dprintf(USBDBG_DEVOPENED, USBSYSBUS_PATH);
  1073. goto found_devices;
  1074. }
  1075. found_devices:
  1076. if (!usb_fs_type) {
  1077. term_printf("husb: unable to access USB devices\n");
  1078. return -ENOENT;
  1079. }
  1080. /* the module setting (used later for opening devices) */
  1081. usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
  1082. strcpy(usb_host_device_path, devpath);
  1083. term_printf("husb: using %s file-system with %s\n", fs_type[usb_fs_type], usb_host_device_path);
  1084. }
  1085. switch (usb_fs_type) {
  1086. case USB_FS_PROC:
  1087. case USB_FS_DEV:
  1088. ret = usb_host_scan_dev(opaque, func);
  1089. break;
  1090. case USB_FS_SYS:
  1091. ret = usb_host_scan_sys(opaque, func);
  1092. break;
  1093. default:
  1094. ret = -EINVAL;
  1095. break;
  1096. }
  1097. return ret;
  1098. }
  1099. struct USBAutoFilter {
  1100. struct USBAutoFilter *next;
  1101. int bus_num;
  1102. int addr;
  1103. int vendor_id;
  1104. int product_id;
  1105. };
  1106. static QEMUTimer *usb_auto_timer;
  1107. static struct USBAutoFilter *usb_auto_filter;
  1108. static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
  1109. int class_id, int vendor_id, int product_id,
  1110. const char *product_name, int speed)
  1111. {
  1112. struct USBAutoFilter *f;
  1113. struct USBDevice *dev;
  1114. /* Ignore hubs */
  1115. if (class_id == 9)
  1116. return 0;
  1117. for (f = usb_auto_filter; f; f = f->next) {
  1118. if (f->bus_num >= 0 && f->bus_num != bus_num)
  1119. continue;
  1120. if (f->addr >= 0 && f->addr != addr)
  1121. continue;
  1122. if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
  1123. continue;
  1124. if (f->product_id >= 0 && f->product_id != product_id)
  1125. continue;
  1126. /* We got a match */
  1127. /* Allredy attached ? */
  1128. if (hostdev_find(bus_num, addr))
  1129. return 0;
  1130. dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
  1131. dev = usb_host_device_open_addr(bus_num, addr, product_name);
  1132. if (dev)
  1133. usb_device_add_dev(dev);
  1134. }
  1135. return 0;
  1136. }
  1137. static void usb_host_auto_timer(void *unused)
  1138. {
  1139. usb_host_scan(NULL, usb_host_auto_scan);
  1140. qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
  1141. }
  1142. /*
  1143. * Autoconnect filter
  1144. * Format:
  1145. * auto:bus:dev[:vid:pid]
  1146. * auto:bus.dev[:vid:pid]
  1147. *
  1148. * bus - bus number (dec, * means any)
  1149. * dev - device number (dec, * means any)
  1150. * vid - vendor id (hex, * means any)
  1151. * pid - product id (hex, * means any)
  1152. *
  1153. * See 'lsusb' output.
  1154. */
  1155. static int parse_filter(const char *spec, struct USBAutoFilter *f)
  1156. {
  1157. enum { BUS, DEV, VID, PID, DONE };
  1158. const char *p = spec;
  1159. int i;
  1160. f->bus_num = -1;
  1161. f->addr = -1;
  1162. f->vendor_id = -1;
  1163. f->product_id = -1;
  1164. for (i = BUS; i < DONE; i++) {
  1165. p = strpbrk(p, ":.");
  1166. if (!p) break;
  1167. p++;
  1168. if (*p == '*')
  1169. continue;
  1170. switch(i) {
  1171. case BUS: f->bus_num = strtol(p, NULL, 10); break;
  1172. case DEV: f->addr = strtol(p, NULL, 10); break;
  1173. case VID: f->vendor_id = strtol(p, NULL, 16); break;
  1174. case PID: f->product_id = strtol(p, NULL, 16); break;
  1175. }
  1176. }
  1177. if (i < DEV) {
  1178. fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
  1179. return -1;
  1180. }
  1181. return 0;
  1182. }
  1183. static int match_filter(const struct USBAutoFilter *f1,
  1184. const struct USBAutoFilter *f2)
  1185. {
  1186. return f1->bus_num == f2->bus_num &&
  1187. f1->addr == f2->addr &&
  1188. f1->vendor_id == f2->vendor_id &&
  1189. f1->product_id == f2->product_id;
  1190. }
  1191. static int usb_host_auto_add(const char *spec)
  1192. {
  1193. struct USBAutoFilter filter, *f;
  1194. if (parse_filter(spec, &filter) < 0)
  1195. return -1;
  1196. f = qemu_mallocz(sizeof(*f));
  1197. *f = filter;
  1198. if (!usb_auto_filter) {
  1199. /*
  1200. * First entry. Init and start the monitor.
  1201. * Right now we're using timer to check for new devices.
  1202. * If this turns out to be too expensive we can move that into a
  1203. * separate thread.
  1204. */
  1205. usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
  1206. if (!usb_auto_timer) {
  1207. fprintf(stderr, "husb: failed to allocate auto scan timer\n");
  1208. qemu_free(f);
  1209. return -1;
  1210. }
  1211. /* Check for new devices every two seconds */
  1212. qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
  1213. }
  1214. dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
  1215. f->bus_num, f->addr, f->vendor_id, f->product_id);
  1216. f->next = usb_auto_filter;
  1217. usb_auto_filter = f;
  1218. return 0;
  1219. }
  1220. static int usb_host_auto_del(const char *spec)
  1221. {
  1222. struct USBAutoFilter *pf = usb_auto_filter;
  1223. struct USBAutoFilter **prev = &usb_auto_filter;
  1224. struct USBAutoFilter filter;
  1225. if (parse_filter(spec, &filter) < 0)
  1226. return -1;
  1227. while (pf) {
  1228. if (match_filter(pf, &filter)) {
  1229. dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
  1230. pf->bus_num, pf->addr, pf->vendor_id, pf->product_id);
  1231. *prev = pf->next;
  1232. if (!usb_auto_filter) {
  1233. /* No more filters. Stop scanning. */
  1234. qemu_del_timer(usb_auto_timer);
  1235. qemu_free_timer(usb_auto_timer);
  1236. }
  1237. return 0;
  1238. }
  1239. prev = &pf->next;
  1240. pf = pf->next;
  1241. }
  1242. return -1;
  1243. }
  1244. typedef struct FindDeviceState {
  1245. int vendor_id;
  1246. int product_id;
  1247. int bus_num;
  1248. int addr;
  1249. char product_name[PRODUCT_NAME_SZ];
  1250. } FindDeviceState;
  1251. static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
  1252. int class_id,
  1253. int vendor_id, int product_id,
  1254. const char *product_name, int speed)
  1255. {
  1256. FindDeviceState *s = opaque;
  1257. if ((vendor_id == s->vendor_id &&
  1258. product_id == s->product_id) ||
  1259. (bus_num == s->bus_num &&
  1260. addr == s->addr)) {
  1261. pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
  1262. s->bus_num = bus_num;
  1263. s->addr = addr;
  1264. return 1;
  1265. } else {
  1266. return 0;
  1267. }
  1268. }
  1269. /* the syntax is :
  1270. 'bus.addr' (decimal numbers) or
  1271. 'vendor_id:product_id' (hexa numbers) */
  1272. static int usb_host_find_device(int *pbus_num, int *paddr,
  1273. char *product_name, int product_name_size,
  1274. const char *devname)
  1275. {
  1276. const char *p;
  1277. int ret;
  1278. FindDeviceState fs;
  1279. p = strchr(devname, '.');
  1280. if (p) {
  1281. *pbus_num = strtoul(devname, NULL, 0);
  1282. *paddr = strtoul(p + 1, NULL, 0);
  1283. fs.bus_num = *pbus_num;
  1284. fs.addr = *paddr;
  1285. ret = usb_host_scan(&fs, usb_host_find_device_scan);
  1286. if (ret)
  1287. pstrcpy(product_name, product_name_size, fs.product_name);
  1288. return 0;
  1289. }
  1290. p = strchr(devname, ':');
  1291. if (p) {
  1292. fs.vendor_id = strtoul(devname, NULL, 16);
  1293. fs.product_id = strtoul(p + 1, NULL, 16);
  1294. ret = usb_host_scan(&fs, usb_host_find_device_scan);
  1295. if (ret) {
  1296. *pbus_num = fs.bus_num;
  1297. *paddr = fs.addr;
  1298. pstrcpy(product_name, product_name_size, fs.product_name);
  1299. return 0;
  1300. }
  1301. }
  1302. return -1;
  1303. }
  1304. /**********************/
  1305. /* USB host device info */
  1306. struct usb_class_info {
  1307. int class;
  1308. const char *class_name;
  1309. };
  1310. static const struct usb_class_info usb_class_info[] = {
  1311. { USB_CLASS_AUDIO, "Audio"},
  1312. { USB_CLASS_COMM, "Communication"},
  1313. { USB_CLASS_HID, "HID"},
  1314. { USB_CLASS_HUB, "Hub" },
  1315. { USB_CLASS_PHYSICAL, "Physical" },
  1316. { USB_CLASS_PRINTER, "Printer" },
  1317. { USB_CLASS_MASS_STORAGE, "Storage" },
  1318. { USB_CLASS_CDC_DATA, "Data" },
  1319. { USB_CLASS_APP_SPEC, "Application Specific" },
  1320. { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
  1321. { USB_CLASS_STILL_IMAGE, "Still Image" },
  1322. { USB_CLASS_CSCID, "Smart Card" },
  1323. { USB_CLASS_CONTENT_SEC, "Content Security" },
  1324. { -1, NULL }
  1325. };
  1326. static const char *usb_class_str(uint8_t class)
  1327. {
  1328. const struct usb_class_info *p;
  1329. for(p = usb_class_info; p->class != -1; p++) {
  1330. if (p->class == class)
  1331. break;
  1332. }
  1333. return p->class_name;
  1334. }
  1335. static void usb_info_device(int bus_num, int addr, int class_id,
  1336. int vendor_id, int product_id,
  1337. const char *product_name,
  1338. int speed)
  1339. {
  1340. const char *class_str, *speed_str;
  1341. switch(speed) {
  1342. case USB_SPEED_LOW:
  1343. speed_str = "1.5";
  1344. break;
  1345. case USB_SPEED_FULL:
  1346. speed_str = "12";
  1347. break;
  1348. case USB_SPEED_HIGH:
  1349. speed_str = "480";
  1350. break;
  1351. default:
  1352. speed_str = "?";
  1353. break;
  1354. }
  1355. term_printf(" Device %d.%d, speed %s Mb/s\n",
  1356. bus_num, addr, speed_str);
  1357. class_str = usb_class_str(class_id);
  1358. if (class_str)
  1359. term_printf(" %s:", class_str);
  1360. else
  1361. term_printf(" Class %02x:", class_id);
  1362. term_printf(" USB device %04x:%04x", vendor_id, product_id);
  1363. if (product_name[0] != '\0')
  1364. term_printf(", %s", product_name);
  1365. term_printf("\n");
  1366. }
  1367. static int usb_host_info_device(void *opaque, int bus_num, int addr,
  1368. int class_id,
  1369. int vendor_id, int product_id,
  1370. const char *product_name,
  1371. int speed)
  1372. {
  1373. usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
  1374. product_name, speed);
  1375. return 0;
  1376. }
  1377. static void dec2str(int val, char *str, size_t size)
  1378. {
  1379. if (val == -1)
  1380. snprintf(str, size, "*");
  1381. else
  1382. snprintf(str, size, "%d", val);
  1383. }
  1384. static void hex2str(int val, char *str, size_t size)
  1385. {
  1386. if (val == -1)
  1387. snprintf(str, size, "*");
  1388. else
  1389. snprintf(str, size, "%x", val);
  1390. }
  1391. void usb_host_info(void)
  1392. {
  1393. struct USBAutoFilter *f;
  1394. usb_host_scan(NULL, usb_host_info_device);
  1395. if (usb_auto_filter)
  1396. term_printf(" Auto filters:\n");
  1397. for (f = usb_auto_filter; f; f = f->next) {
  1398. char bus[10], addr[10], vid[10], pid[10];
  1399. dec2str(f->bus_num, bus, sizeof(bus));
  1400. dec2str(f->addr, addr, sizeof(addr));
  1401. hex2str(f->vendor_id, vid, sizeof(vid));
  1402. hex2str(f->product_id, pid, sizeof(pid));
  1403. term_printf(" Device %s.%s ID %s:%s\n", bus, addr, vid, pid);
  1404. }
  1405. }