host-bsd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * BSD host USB redirector
  3. *
  4. * Copyright (c) 2006 Lonnie Mendez
  5. * Portions of code and concepts borrowed from
  6. * usb-linux.c and libusb's bsd.c and are copyright their respective owners.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu-common.h"
  27. #include "monitor/monitor.h"
  28. #include "hw/usb.h"
  29. /* usb.h declares these */
  30. #undef USB_SPEED_HIGH
  31. #undef USB_SPEED_FULL
  32. #undef USB_SPEED_LOW
  33. #include <sys/ioctl.h>
  34. #ifndef __DragonFly__
  35. #include <dev/usb/usb.h>
  36. #else
  37. #include <bus/usb/usb.h>
  38. #endif
  39. /* This value has maximum potential at 16.
  40. * You should also set hw.usb.debug to gain
  41. * more detailed view.
  42. */
  43. //#define DEBUG
  44. #define UGEN_DEBUG_LEVEL 0
  45. typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
  46. int vendor_id, int product_id,
  47. const char *product_name, int speed);
  48. static int usb_host_find_device(int *pbus_num, int *paddr,
  49. const char *devname);
  50. typedef struct USBHostDevice {
  51. USBDevice dev;
  52. int ep_fd[USB_MAX_ENDPOINTS];
  53. int devfd;
  54. char devpath[32];
  55. } USBHostDevice;
  56. static int ensure_ep_open(USBHostDevice *dev, int ep, int mode)
  57. {
  58. char buf[32];
  59. int fd;
  60. /* Get the address for this endpoint */
  61. ep = UE_GET_ADDR(ep);
  62. if (dev->ep_fd[ep] < 0) {
  63. #if defined(__FreeBSD__) || defined(__DragonFly__)
  64. snprintf(buf, sizeof(buf) - 1, "%s.%d", dev->devpath, ep);
  65. #else
  66. snprintf(buf, sizeof(buf) - 1, "%s.%02d", dev->devpath, ep);
  67. #endif
  68. /* Try to open it O_RDWR first for those devices which have in and out
  69. * endpoints with the same address (eg 0x02 and 0x82)
  70. */
  71. fd = open(buf, O_RDWR);
  72. if (fd < 0 && errno == ENXIO)
  73. fd = open(buf, mode);
  74. if (fd < 0) {
  75. #ifdef DEBUG
  76. printf("ensure_ep_open: failed to open device endpoint %s: %s\n",
  77. buf, strerror(errno));
  78. #endif
  79. }
  80. dev->ep_fd[ep] = fd;
  81. }
  82. return dev->ep_fd[ep];
  83. }
  84. static void ensure_eps_closed(USBHostDevice *dev)
  85. {
  86. int epnum = 1;
  87. if (!dev)
  88. return;
  89. while (epnum < USB_MAX_ENDPOINTS) {
  90. if (dev->ep_fd[epnum] >= 0) {
  91. close(dev->ep_fd[epnum]);
  92. dev->ep_fd[epnum] = -1;
  93. }
  94. epnum++;
  95. }
  96. }
  97. static void usb_host_handle_reset(USBDevice *dev)
  98. {
  99. #if 0
  100. USBHostDevice *s = (USBHostDevice *)dev;
  101. #endif
  102. }
  103. /* XXX:
  104. * -check device states against transfer requests
  105. * and return appropriate response
  106. */
  107. static void usb_host_handle_control(USBDevice *dev,
  108. USBPacket *p,
  109. int request,
  110. int value,
  111. int index,
  112. int length,
  113. uint8_t *data)
  114. {
  115. USBHostDevice *s = (USBHostDevice *)dev;
  116. struct usb_ctl_request req;
  117. struct usb_alt_interface aiface;
  118. int ret, timeout = 50;
  119. if ((request >> 8) == UT_WRITE_DEVICE &&
  120. (request & 0xff) == UR_SET_ADDRESS) {
  121. /* specific SET_ADDRESS support */
  122. dev->addr = value;
  123. } else if ((request >> 8) == UT_WRITE_DEVICE &&
  124. (request & 0xff) == UR_SET_CONFIG) {
  125. ensure_eps_closed(s); /* can't do this without all eps closed */
  126. ret = ioctl(s->devfd, USB_SET_CONFIG, &value);
  127. if (ret < 0) {
  128. #ifdef DEBUG
  129. printf("handle_control: failed to set configuration - %s\n",
  130. strerror(errno));
  131. #endif
  132. p->status = USB_RET_STALL;
  133. }
  134. } else if ((request >> 8) == UT_WRITE_INTERFACE &&
  135. (request & 0xff) == UR_SET_INTERFACE) {
  136. aiface.uai_interface_index = index;
  137. aiface.uai_alt_no = value;
  138. ensure_eps_closed(s); /* can't do this without all eps closed */
  139. ret = ioctl(s->devfd, USB_SET_ALTINTERFACE, &aiface);
  140. if (ret < 0) {
  141. #ifdef DEBUG
  142. printf("handle_control: failed to set alternate interface - %s\n",
  143. strerror(errno));
  144. #endif
  145. p->status = USB_RET_STALL;
  146. }
  147. } else {
  148. req.ucr_request.bmRequestType = request >> 8;
  149. req.ucr_request.bRequest = request & 0xff;
  150. USETW(req.ucr_request.wValue, value);
  151. USETW(req.ucr_request.wIndex, index);
  152. USETW(req.ucr_request.wLength, length);
  153. req.ucr_data = data;
  154. req.ucr_flags = USBD_SHORT_XFER_OK;
  155. ret = ioctl(s->devfd, USB_SET_TIMEOUT, &timeout);
  156. #if defined(__NetBSD__) || defined(__OpenBSD__)
  157. if (ret < 0 && errno != EINVAL) {
  158. #else
  159. if (ret < 0) {
  160. #endif
  161. #ifdef DEBUG
  162. printf("handle_control: setting timeout failed - %s\n",
  163. strerror(errno));
  164. #endif
  165. }
  166. ret = ioctl(s->devfd, USB_DO_REQUEST, &req);
  167. /* ugen returns EIO for usbd_do_request_ no matter what
  168. * happens with the transfer */
  169. if (ret < 0) {
  170. #ifdef DEBUG
  171. printf("handle_control: error after request - %s\n",
  172. strerror(errno));
  173. #endif
  174. p->status = USB_RET_NAK; /* STALL */
  175. } else {
  176. p->actual_length = req.ucr_actlen;
  177. }
  178. }
  179. }
  180. static void usb_host_handle_data(USBDevice *dev, USBPacket *p)
  181. {
  182. USBHostDevice *s = (USBHostDevice *)dev;
  183. int ret, fd, mode;
  184. int one = 1, shortpacket = 0, timeout = 50;
  185. sigset_t new_mask, old_mask;
  186. uint8_t devep = p->ep->nr;
  187. /* protect data transfers from SIGALRM signal */
  188. sigemptyset(&new_mask);
  189. sigaddset(&new_mask, SIGALRM);
  190. sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
  191. if (p->pid == USB_TOKEN_IN) {
  192. devep |= 0x80;
  193. mode = O_RDONLY;
  194. shortpacket = 1;
  195. } else {
  196. mode = O_WRONLY;
  197. }
  198. fd = ensure_ep_open(s, devep, mode);
  199. if (fd < 0) {
  200. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  201. p->status = USB_RET_NODEV;
  202. return;
  203. }
  204. if (ioctl(fd, USB_SET_TIMEOUT, &timeout) < 0) {
  205. #ifdef DEBUG
  206. printf("handle_data: failed to set timeout - %s\n",
  207. strerror(errno));
  208. #endif
  209. }
  210. if (shortpacket) {
  211. if (ioctl(fd, USB_SET_SHORT_XFER, &one) < 0) {
  212. #ifdef DEBUG
  213. printf("handle_data: failed to set short xfer mode - %s\n",
  214. strerror(errno));
  215. #endif
  216. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  217. }
  218. }
  219. if (p->pid == USB_TOKEN_IN)
  220. ret = readv(fd, p->iov.iov, p->iov.niov);
  221. else
  222. ret = writev(fd, p->iov.iov, p->iov.niov);
  223. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  224. if (ret < 0) {
  225. #ifdef DEBUG
  226. printf("handle_data: error after %s data - %s\n",
  227. pid == USB_TOKEN_IN ? "reading" : "writing", strerror(errno));
  228. #endif
  229. switch(errno) {
  230. case ETIMEDOUT:
  231. case EINTR:
  232. p->status = USB_RET_NAK;
  233. break;
  234. default:
  235. p->status = USB_RET_STALL;
  236. }
  237. } else {
  238. p->actual_length = ret;
  239. }
  240. }
  241. static void usb_host_handle_destroy(USBDevice *opaque)
  242. {
  243. USBHostDevice *s = (USBHostDevice *)opaque;
  244. int i;
  245. for (i = 0; i < USB_MAX_ENDPOINTS; i++)
  246. if (s->ep_fd[i] >= 0)
  247. close(s->ep_fd[i]);
  248. if (s->devfd < 0)
  249. return;
  250. close(s->devfd);
  251. g_free(s);
  252. }
  253. static int usb_host_initfn(USBDevice *dev)
  254. {
  255. dev->flags |= (1 << USB_DEV_FLAG_IS_HOST);
  256. return 0;
  257. }
  258. USBDevice *usb_host_device_open(USBBus *guest_bus, const char *devname)
  259. {
  260. struct usb_device_info bus_info, dev_info;
  261. USBDevice *d = NULL, *ret = NULL;
  262. USBHostDevice *dev;
  263. char ctlpath[PATH_MAX + 1];
  264. char buspath[PATH_MAX + 1];
  265. int bfd, dfd, bus, address, i;
  266. int ugendebug = UGEN_DEBUG_LEVEL;
  267. if (usb_host_find_device(&bus, &address, devname) < 0) {
  268. goto fail;
  269. }
  270. snprintf(buspath, PATH_MAX, "/dev/usb%d", bus);
  271. bfd = open(buspath, O_RDWR);
  272. if (bfd < 0) {
  273. #ifdef DEBUG
  274. printf("usb_host_device_open: failed to open usb bus - %s\n",
  275. strerror(errno));
  276. #endif
  277. goto fail;
  278. }
  279. bus_info.udi_addr = address;
  280. if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) {
  281. #ifdef DEBUG
  282. printf("usb_host_device_open: failed to grab bus information - %s\n",
  283. strerror(errno));
  284. #endif
  285. goto fail_bfd;
  286. }
  287. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  288. snprintf(ctlpath, PATH_MAX, "/dev/%s", bus_info.udi_devnames[0]);
  289. #else
  290. snprintf(ctlpath, PATH_MAX, "/dev/%s.00", bus_info.udi_devnames[0]);
  291. #endif
  292. dfd = open(ctlpath, O_RDWR);
  293. if (dfd < 0) {
  294. dfd = open(ctlpath, O_RDONLY);
  295. if (dfd < 0) {
  296. #ifdef DEBUG
  297. printf("usb_host_device_open: failed to open usb device %s - %s\n",
  298. ctlpath, strerror(errno));
  299. #endif
  300. }
  301. goto fail_dfd;
  302. }
  303. if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) {
  304. #ifdef DEBUG
  305. printf("usb_host_device_open: failed to grab device info - %s\n",
  306. strerror(errno));
  307. #endif
  308. goto fail_dfd;
  309. }
  310. d = usb_create(guest_bus, "usb-host");
  311. dev = DO_UPCAST(USBHostDevice, dev, d);
  312. if (dev_info.udi_speed == 1) {
  313. dev->dev.speed = USB_SPEED_LOW - 1;
  314. dev->dev.speedmask = USB_SPEED_MASK_LOW;
  315. } else {
  316. dev->dev.speed = USB_SPEED_FULL - 1;
  317. dev->dev.speedmask = USB_SPEED_MASK_FULL;
  318. }
  319. if (strncmp(dev_info.udi_product, "product", 7) != 0) {
  320. pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
  321. dev_info.udi_product);
  322. } else {
  323. snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
  324. "host:%s", devname);
  325. }
  326. pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/");
  327. pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]);
  328. /* Mark the endpoints as not yet open */
  329. for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
  330. dev->ep_fd[i] = -1;
  331. }
  332. ioctl(dfd, USB_SETDEBUG, &ugendebug);
  333. ret = (USBDevice *)dev;
  334. fail_dfd:
  335. close(dfd);
  336. fail_bfd:
  337. close(bfd);
  338. fail:
  339. return ret;
  340. }
  341. static void usb_host_class_initfn(ObjectClass *klass, void *data)
  342. {
  343. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  344. uc->product_desc = "USB Host Device";
  345. uc->init = usb_host_initfn;
  346. uc->handle_reset = usb_host_handle_reset;
  347. uc->handle_control = usb_host_handle_control;
  348. uc->handle_data = usb_host_handle_data;
  349. uc->handle_destroy = usb_host_handle_destroy;
  350. }
  351. static const TypeInfo usb_host_dev_info = {
  352. .name = "usb-host",
  353. .parent = TYPE_USB_DEVICE,
  354. .instance_size = sizeof(USBHostDevice),
  355. .class_init = usb_host_class_initfn,
  356. };
  357. static void usb_host_register_types(void)
  358. {
  359. type_register_static(&usb_host_dev_info);
  360. }
  361. type_init(usb_host_register_types)
  362. static int usb_host_scan(void *opaque, USBScanFunc *func)
  363. {
  364. struct usb_device_info bus_info;
  365. struct usb_device_info dev_info;
  366. uint16_t vendor_id, product_id, class_id, speed;
  367. int bfd, dfd, bus, address;
  368. char busbuf[20], devbuf[20], product_name[256];
  369. int ret = 0;
  370. for (bus = 0; bus < 10; bus++) {
  371. snprintf(busbuf, sizeof(busbuf) - 1, "/dev/usb%d", bus);
  372. bfd = open(busbuf, O_RDWR);
  373. if (bfd < 0)
  374. continue;
  375. for (address = 1; address < 127; address++) {
  376. bus_info.udi_addr = address;
  377. if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0)
  378. continue;
  379. /* only list devices that can be used by generic layer */
  380. if (strncmp(bus_info.udi_devnames[0], "ugen", 4) != 0)
  381. continue;
  382. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  383. snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s", bus_info.udi_devnames[0]);
  384. #else
  385. snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s.00", bus_info.udi_devnames[0]);
  386. #endif
  387. dfd = open(devbuf, O_RDONLY);
  388. if (dfd < 0) {
  389. #ifdef DEBUG
  390. printf("usb_host_scan: couldn't open device %s - %s\n", devbuf,
  391. strerror(errno));
  392. #endif
  393. continue;
  394. }
  395. if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0)
  396. printf("usb_host_scan: couldn't get device information for %s - %s\n",
  397. devbuf, strerror(errno));
  398. /* XXX: might need to fixup endianness of word values before copying over */
  399. vendor_id = dev_info.udi_vendorNo;
  400. product_id = dev_info.udi_productNo;
  401. class_id = dev_info.udi_class;
  402. speed = dev_info.udi_speed;
  403. if (strncmp(dev_info.udi_product, "product", 7) != 0)
  404. pstrcpy(product_name, sizeof(product_name),
  405. dev_info.udi_product);
  406. else
  407. product_name[0] = '\0';
  408. ret = func(opaque, bus, address, class_id, vendor_id,
  409. product_id, product_name, speed);
  410. close(dfd);
  411. if (ret)
  412. goto the_end;
  413. }
  414. close(bfd);
  415. }
  416. the_end:
  417. return ret;
  418. }
  419. typedef struct FindDeviceState {
  420. int vendor_id;
  421. int product_id;
  422. int bus_num;
  423. int addr;
  424. } FindDeviceState;
  425. static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
  426. int class_id,
  427. int vendor_id, int product_id,
  428. const char *product_name, int speed)
  429. {
  430. FindDeviceState *s = opaque;
  431. if (vendor_id == s->vendor_id &&
  432. product_id == s->product_id) {
  433. s->bus_num = bus_num;
  434. s->addr = addr;
  435. return 1;
  436. } else {
  437. return 0;
  438. }
  439. }
  440. /* the syntax is :
  441. 'bus.addr' (decimal numbers) or
  442. 'vendor_id:product_id' (hexa numbers) */
  443. static int usb_host_find_device(int *pbus_num, int *paddr,
  444. const char *devname)
  445. {
  446. const char *p;
  447. int ret;
  448. FindDeviceState fs;
  449. p = strchr(devname, '.');
  450. if (p) {
  451. *pbus_num = strtoul(devname, NULL, 0);
  452. *paddr = strtoul(p + 1, NULL, 0);
  453. return 0;
  454. }
  455. p = strchr(devname, ':');
  456. if (p) {
  457. fs.vendor_id = strtoul(devname, NULL, 16);
  458. fs.product_id = strtoul(p + 1, NULL, 16);
  459. ret = usb_host_scan(&fs, usb_host_find_device_scan);
  460. if (ret) {
  461. *pbus_num = fs.bus_num;
  462. *paddr = fs.addr;
  463. return 0;
  464. }
  465. }
  466. return -1;
  467. }
  468. /**********************/
  469. /* USB host device info */
  470. struct usb_class_info {
  471. int class;
  472. const char *class_name;
  473. };
  474. static const struct usb_class_info usb_class_info[] = {
  475. { USB_CLASS_AUDIO, "Audio"},
  476. { USB_CLASS_COMM, "Communication"},
  477. { USB_CLASS_HID, "HID"},
  478. { USB_CLASS_HUB, "Hub" },
  479. { USB_CLASS_PHYSICAL, "Physical" },
  480. { USB_CLASS_PRINTER, "Printer" },
  481. { USB_CLASS_MASS_STORAGE, "Storage" },
  482. { USB_CLASS_CDC_DATA, "Data" },
  483. { USB_CLASS_APP_SPEC, "Application Specific" },
  484. { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
  485. { USB_CLASS_STILL_IMAGE, "Still Image" },
  486. { USB_CLASS_CSCID, "Smart Card" },
  487. { USB_CLASS_CONTENT_SEC, "Content Security" },
  488. { -1, NULL }
  489. };
  490. static const char *usb_class_str(uint8_t class)
  491. {
  492. const struct usb_class_info *p;
  493. for (p = usb_class_info; p->class != -1; p++) {
  494. if (p->class == class)
  495. break;
  496. }
  497. return p->class_name;
  498. }
  499. static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
  500. int vendor_id, int product_id,
  501. const char *product_name,
  502. int speed)
  503. {
  504. const char *class_str, *speed_str;
  505. switch(speed) {
  506. case USB_SPEED_LOW:
  507. speed_str = "1.5";
  508. break;
  509. case USB_SPEED_FULL:
  510. speed_str = "12";
  511. break;
  512. case USB_SPEED_HIGH:
  513. speed_str = "480";
  514. break;
  515. default:
  516. speed_str = "?";
  517. break;
  518. }
  519. monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
  520. bus_num, addr, speed_str);
  521. class_str = usb_class_str(class_id);
  522. if (class_str)
  523. monitor_printf(mon, " %s:", class_str);
  524. else
  525. monitor_printf(mon, " Class %02x:", class_id);
  526. monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
  527. if (product_name[0] != '\0')
  528. monitor_printf(mon, ", %s", product_name);
  529. monitor_printf(mon, "\n");
  530. }
  531. static int usb_host_info_device(void *opaque,
  532. int bus_num, int addr,
  533. int class_id,
  534. int vendor_id, int product_id,
  535. const char *product_name,
  536. int speed)
  537. {
  538. Monitor *mon = opaque;
  539. usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
  540. product_name, speed);
  541. return 0;
  542. }
  543. void usb_host_info(Monitor *mon, const QDict *qdict)
  544. {
  545. usb_host_scan(mon, usb_host_info_device);
  546. }
  547. /* XXX add this */
  548. int usb_host_device_close(const char *devname)
  549. {
  550. return 0;
  551. }