usb-bsd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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.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 int 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. return 0;
  124. } else if ((request >> 8) == UT_WRITE_DEVICE &&
  125. (request & 0xff) == UR_SET_CONFIG) {
  126. ensure_eps_closed(s); /* can't do this without all eps closed */
  127. ret = ioctl(s->devfd, USB_SET_CONFIG, &value);
  128. if (ret < 0) {
  129. #ifdef DEBUG
  130. printf("handle_control: failed to set configuration - %s\n",
  131. strerror(errno));
  132. #endif
  133. return USB_RET_STALL;
  134. }
  135. return 0;
  136. } else if ((request >> 8) == UT_WRITE_INTERFACE &&
  137. (request & 0xff) == UR_SET_INTERFACE) {
  138. aiface.uai_interface_index = index;
  139. aiface.uai_alt_no = value;
  140. ensure_eps_closed(s); /* can't do this without all eps closed */
  141. ret = ioctl(s->devfd, USB_SET_ALTINTERFACE, &aiface);
  142. if (ret < 0) {
  143. #ifdef DEBUG
  144. printf("handle_control: failed to set alternate interface - %s\n",
  145. strerror(errno));
  146. #endif
  147. return USB_RET_STALL;
  148. }
  149. return 0;
  150. } else {
  151. req.ucr_request.bmRequestType = request >> 8;
  152. req.ucr_request.bRequest = request & 0xff;
  153. USETW(req.ucr_request.wValue, value);
  154. USETW(req.ucr_request.wIndex, index);
  155. USETW(req.ucr_request.wLength, length);
  156. req.ucr_data = data;
  157. req.ucr_flags = USBD_SHORT_XFER_OK;
  158. ret = ioctl(s->devfd, USB_SET_TIMEOUT, &timeout);
  159. #if defined(__NetBSD__) || defined(__OpenBSD__)
  160. if (ret < 0 && errno != EINVAL) {
  161. #else
  162. if (ret < 0) {
  163. #endif
  164. #ifdef DEBUG
  165. printf("handle_control: setting timeout failed - %s\n",
  166. strerror(errno));
  167. #endif
  168. }
  169. ret = ioctl(s->devfd, USB_DO_REQUEST, &req);
  170. /* ugen returns EIO for usbd_do_request_ no matter what
  171. * happens with the transfer */
  172. if (ret < 0) {
  173. #ifdef DEBUG
  174. printf("handle_control: error after request - %s\n",
  175. strerror(errno));
  176. #endif
  177. return USB_RET_NAK; // STALL
  178. } else {
  179. return req.ucr_actlen;
  180. }
  181. }
  182. }
  183. static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
  184. {
  185. USBHostDevice *s = (USBHostDevice *)dev;
  186. int ret, fd, mode;
  187. int one = 1, shortpacket = 0, timeout = 50;
  188. sigset_t new_mask, old_mask;
  189. uint8_t devep = p->devep;
  190. /* protect data transfers from SIGALRM signal */
  191. sigemptyset(&new_mask);
  192. sigaddset(&new_mask, SIGALRM);
  193. sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
  194. if (p->pid == USB_TOKEN_IN) {
  195. devep |= 0x80;
  196. mode = O_RDONLY;
  197. shortpacket = 1;
  198. } else {
  199. mode = O_WRONLY;
  200. }
  201. fd = ensure_ep_open(s, devep, mode);
  202. if (fd < 0) {
  203. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  204. return USB_RET_NODEV;
  205. }
  206. if (ioctl(fd, USB_SET_TIMEOUT, &timeout) < 0) {
  207. #ifdef DEBUG
  208. printf("handle_data: failed to set timeout - %s\n",
  209. strerror(errno));
  210. #endif
  211. }
  212. if (shortpacket) {
  213. if (ioctl(fd, USB_SET_SHORT_XFER, &one) < 0) {
  214. #ifdef DEBUG
  215. printf("handle_data: failed to set short xfer mode - %s\n",
  216. strerror(errno));
  217. #endif
  218. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  219. }
  220. }
  221. if (p->pid == USB_TOKEN_IN)
  222. ret = readv(fd, p->iov.iov, p->iov.niov);
  223. else
  224. ret = writev(fd, p->iov.iov, p->iov.niov);
  225. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  226. if (ret < 0) {
  227. #ifdef DEBUG
  228. printf("handle_data: error after %s data - %s\n",
  229. pid == USB_TOKEN_IN ? "reading" : "writing", strerror(errno));
  230. #endif
  231. switch(errno) {
  232. case ETIMEDOUT:
  233. case EINTR:
  234. return USB_RET_NAK;
  235. default:
  236. return USB_RET_STALL;
  237. }
  238. } else {
  239. return ret;
  240. }
  241. }
  242. static void usb_host_handle_destroy(USBDevice *opaque)
  243. {
  244. USBHostDevice *s = (USBHostDevice *)opaque;
  245. int i;
  246. for (i = 0; i < USB_MAX_ENDPOINTS; i++)
  247. if (s->ep_fd[i] >= 0)
  248. close(s->ep_fd[i]);
  249. if (s->devfd < 0)
  250. return;
  251. close(s->devfd);
  252. g_free(s);
  253. }
  254. static int usb_host_initfn(USBDevice *dev)
  255. {
  256. return 0;
  257. }
  258. USBDevice *usb_host_device_open(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(NULL /* FIXME */, "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 struct USBDeviceInfo usb_host_dev_info = {
  342. .product_desc = "USB Host Device",
  343. .qdev.name = "usb-host",
  344. .qdev.size = sizeof(USBHostDevice),
  345. .init = usb_host_initfn,
  346. .handle_packet = usb_generic_handle_packet,
  347. .handle_reset = usb_host_handle_reset,
  348. .handle_control = usb_host_handle_control,
  349. .handle_data = usb_host_handle_data,
  350. .handle_destroy = usb_host_handle_destroy,
  351. };
  352. static void usb_host_register_devices(void)
  353. {
  354. usb_qdev_register(&usb_host_dev_info);
  355. }
  356. device_init(usb_host_register_devices)
  357. static int usb_host_scan(void *opaque, USBScanFunc *func)
  358. {
  359. struct usb_device_info bus_info;
  360. struct usb_device_info dev_info;
  361. uint16_t vendor_id, product_id, class_id, speed;
  362. int bfd, dfd, bus, address;
  363. char busbuf[20], devbuf[20], product_name[256];
  364. int ret = 0;
  365. for (bus = 0; bus < 10; bus++) {
  366. snprintf(busbuf, sizeof(busbuf) - 1, "/dev/usb%d", bus);
  367. bfd = open(busbuf, O_RDWR);
  368. if (bfd < 0)
  369. continue;
  370. for (address = 1; address < 127; address++) {
  371. bus_info.udi_addr = address;
  372. if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0)
  373. continue;
  374. /* only list devices that can be used by generic layer */
  375. if (strncmp(bus_info.udi_devnames[0], "ugen", 4) != 0)
  376. continue;
  377. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  378. snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s", bus_info.udi_devnames[0]);
  379. #else
  380. snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s.00", bus_info.udi_devnames[0]);
  381. #endif
  382. dfd = open(devbuf, O_RDONLY);
  383. if (dfd < 0) {
  384. #ifdef DEBUG
  385. printf("usb_host_scan: couldn't open device %s - %s\n", devbuf,
  386. strerror(errno));
  387. #endif
  388. continue;
  389. }
  390. if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0)
  391. printf("usb_host_scan: couldn't get device information for %s - %s\n",
  392. devbuf, strerror(errno));
  393. /* XXX: might need to fixup endianness of word values before copying over */
  394. vendor_id = dev_info.udi_vendorNo;
  395. product_id = dev_info.udi_productNo;
  396. class_id = dev_info.udi_class;
  397. speed = dev_info.udi_speed;
  398. if (strncmp(dev_info.udi_product, "product", 7) != 0)
  399. pstrcpy(product_name, sizeof(product_name),
  400. dev_info.udi_product);
  401. else
  402. product_name[0] = '\0';
  403. ret = func(opaque, bus, address, class_id, vendor_id,
  404. product_id, product_name, speed);
  405. close(dfd);
  406. if (ret)
  407. goto the_end;
  408. }
  409. close(bfd);
  410. }
  411. the_end:
  412. return ret;
  413. }
  414. typedef struct FindDeviceState {
  415. int vendor_id;
  416. int product_id;
  417. int bus_num;
  418. int addr;
  419. } FindDeviceState;
  420. static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
  421. int class_id,
  422. int vendor_id, int product_id,
  423. const char *product_name, int speed)
  424. {
  425. FindDeviceState *s = opaque;
  426. if (vendor_id == s->vendor_id &&
  427. product_id == s->product_id) {
  428. s->bus_num = bus_num;
  429. s->addr = addr;
  430. return 1;
  431. } else {
  432. return 0;
  433. }
  434. }
  435. /* the syntax is :
  436. 'bus.addr' (decimal numbers) or
  437. 'vendor_id:product_id' (hexa numbers) */
  438. static int usb_host_find_device(int *pbus_num, int *paddr,
  439. const char *devname)
  440. {
  441. const char *p;
  442. int ret;
  443. FindDeviceState fs;
  444. p = strchr(devname, '.');
  445. if (p) {
  446. *pbus_num = strtoul(devname, NULL, 0);
  447. *paddr = strtoul(p + 1, NULL, 0);
  448. return 0;
  449. }
  450. p = strchr(devname, ':');
  451. if (p) {
  452. fs.vendor_id = strtoul(devname, NULL, 16);
  453. fs.product_id = strtoul(p + 1, NULL, 16);
  454. ret = usb_host_scan(&fs, usb_host_find_device_scan);
  455. if (ret) {
  456. *pbus_num = fs.bus_num;
  457. *paddr = fs.addr;
  458. return 0;
  459. }
  460. }
  461. return -1;
  462. }
  463. /**********************/
  464. /* USB host device info */
  465. struct usb_class_info {
  466. int class;
  467. const char *class_name;
  468. };
  469. static const struct usb_class_info usb_class_info[] = {
  470. { USB_CLASS_AUDIO, "Audio"},
  471. { USB_CLASS_COMM, "Communication"},
  472. { USB_CLASS_HID, "HID"},
  473. { USB_CLASS_HUB, "Hub" },
  474. { USB_CLASS_PHYSICAL, "Physical" },
  475. { USB_CLASS_PRINTER, "Printer" },
  476. { USB_CLASS_MASS_STORAGE, "Storage" },
  477. { USB_CLASS_CDC_DATA, "Data" },
  478. { USB_CLASS_APP_SPEC, "Application Specific" },
  479. { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
  480. { USB_CLASS_STILL_IMAGE, "Still Image" },
  481. { USB_CLASS_CSCID, "Smart Card" },
  482. { USB_CLASS_CONTENT_SEC, "Content Security" },
  483. { -1, NULL }
  484. };
  485. static const char *usb_class_str(uint8_t class)
  486. {
  487. const struct usb_class_info *p;
  488. for (p = usb_class_info; p->class != -1; p++) {
  489. if (p->class == class)
  490. break;
  491. }
  492. return p->class_name;
  493. }
  494. static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
  495. int vendor_id, int product_id,
  496. const char *product_name,
  497. int speed)
  498. {
  499. const char *class_str, *speed_str;
  500. switch(speed) {
  501. case USB_SPEED_LOW:
  502. speed_str = "1.5";
  503. break;
  504. case USB_SPEED_FULL:
  505. speed_str = "12";
  506. break;
  507. case USB_SPEED_HIGH:
  508. speed_str = "480";
  509. break;
  510. default:
  511. speed_str = "?";
  512. break;
  513. }
  514. monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
  515. bus_num, addr, speed_str);
  516. class_str = usb_class_str(class_id);
  517. if (class_str)
  518. monitor_printf(mon, " %s:", class_str);
  519. else
  520. monitor_printf(mon, " Class %02x:", class_id);
  521. monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
  522. if (product_name[0] != '\0')
  523. monitor_printf(mon, ", %s", product_name);
  524. monitor_printf(mon, "\n");
  525. }
  526. static int usb_host_info_device(void *opaque,
  527. int bus_num, int addr,
  528. int class_id,
  529. int vendor_id, int product_id,
  530. const char *product_name,
  531. int speed)
  532. {
  533. Monitor *mon = opaque;
  534. usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
  535. product_name, speed);
  536. return 0;
  537. }
  538. void usb_host_info(Monitor *mon)
  539. {
  540. usb_host_scan(mon, usb_host_info_device);
  541. }
  542. /* XXX add this */
  543. int usb_host_device_close(const char *devname)
  544. {
  545. return 0;
  546. }