usb-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.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. #if 0
  57. static int ensure_ep_open(USBHostDevice *dev, int ep, int mode)
  58. {
  59. char buf[32];
  60. int fd;
  61. /* Get the address for this endpoint */
  62. ep = UE_GET_ADDR(ep);
  63. if (dev->ep_fd[ep] < 0) {
  64. #if defined(__FreeBSD__) || defined(__DragonFly__)
  65. snprintf(buf, sizeof(buf) - 1, "%s.%d", dev->devpath, ep);
  66. #else
  67. snprintf(buf, sizeof(buf) - 1, "%s.%02d", dev->devpath, ep);
  68. #endif
  69. /* Try to open it O_RDWR first for those devices which have in and out
  70. * endpoints with the same address (eg 0x02 and 0x82)
  71. */
  72. fd = open(buf, O_RDWR);
  73. if (fd < 0 && errno == ENXIO)
  74. fd = open(buf, mode);
  75. if (fd < 0) {
  76. #ifdef DEBUG
  77. printf("ensure_ep_open: failed to open device endpoint %s: %s\n",
  78. buf, strerror(errno));
  79. #endif
  80. }
  81. dev->ep_fd[ep] = fd;
  82. }
  83. return dev->ep_fd[ep];
  84. }
  85. static void ensure_eps_closed(USBHostDevice *dev)
  86. {
  87. int epnum = 1;
  88. if (!dev)
  89. return;
  90. while (epnum < USB_MAX_ENDPOINTS) {
  91. if (dev->ep_fd[epnum] >= 0) {
  92. close(dev->ep_fd[epnum]);
  93. dev->ep_fd[epnum] = -1;
  94. }
  95. epnum++;
  96. }
  97. }
  98. #endif
  99. static void usb_host_handle_reset(USBDevice *dev)
  100. {
  101. #if 0
  102. USBHostDevice *s = (USBHostDevice *)dev;
  103. #endif
  104. }
  105. #if 0
  106. /* XXX:
  107. * -check device states against transfer requests
  108. * and return appropriate response
  109. */
  110. static int usb_host_handle_control(USBDevice *dev,
  111. USBPacket *p,
  112. int request,
  113. int value,
  114. int index,
  115. int length,
  116. uint8_t *data)
  117. {
  118. USBHostDevice *s = (USBHostDevice *)dev;
  119. struct usb_ctl_request req;
  120. struct usb_alt_interface aiface;
  121. int ret, timeout = 50;
  122. if ((request >> 8) == UT_WRITE_DEVICE &&
  123. (request & 0xff) == UR_SET_ADDRESS) {
  124. /* specific SET_ADDRESS support */
  125. dev->addr = value;
  126. return 0;
  127. } else if ((request >> 8) == UT_WRITE_DEVICE &&
  128. (request & 0xff) == UR_SET_CONFIG) {
  129. ensure_eps_closed(s); /* can't do this without all eps closed */
  130. ret = ioctl(s->devfd, USB_SET_CONFIG, &value);
  131. if (ret < 0) {
  132. #ifdef DEBUG
  133. printf("handle_control: failed to set configuration - %s\n",
  134. strerror(errno));
  135. #endif
  136. return USB_RET_STALL;
  137. }
  138. return 0;
  139. } else if ((request >> 8) == UT_WRITE_INTERFACE &&
  140. (request & 0xff) == UR_SET_INTERFACE) {
  141. aiface.uai_interface_index = index;
  142. aiface.uai_alt_no = value;
  143. ensure_eps_closed(s); /* can't do this without all eps closed */
  144. ret = ioctl(s->devfd, USB_SET_ALTINTERFACE, &aiface);
  145. if (ret < 0) {
  146. #ifdef DEBUG
  147. printf("handle_control: failed to set alternate interface - %s\n",
  148. strerror(errno));
  149. #endif
  150. return USB_RET_STALL;
  151. }
  152. return 0;
  153. } else {
  154. req.ucr_request.bmRequestType = request >> 8;
  155. req.ucr_request.bRequest = request & 0xff;
  156. USETW(req.ucr_request.wValue, value);
  157. USETW(req.ucr_request.wIndex, index);
  158. USETW(req.ucr_request.wLength, length);
  159. req.ucr_data = data;
  160. req.ucr_flags = USBD_SHORT_XFER_OK;
  161. ret = ioctl(s->devfd, USB_SET_TIMEOUT, &timeout);
  162. #if defined(__NetBSD__) || defined(__OpenBSD__)
  163. if (ret < 0 && errno != EINVAL) {
  164. #else
  165. if (ret < 0) {
  166. #endif
  167. #ifdef DEBUG
  168. printf("handle_control: setting timeout failed - %s\n",
  169. strerror(errno));
  170. #endif
  171. }
  172. ret = ioctl(s->devfd, USB_DO_REQUEST, &req);
  173. /* ugen returns EIO for usbd_do_request_ no matter what
  174. * happens with the transfer */
  175. if (ret < 0) {
  176. #ifdef DEBUG
  177. printf("handle_control: error after request - %s\n",
  178. strerror(errno));
  179. #endif
  180. return USB_RET_NAK; // STALL
  181. } else {
  182. return req.ucr_actlen;
  183. }
  184. }
  185. }
  186. static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
  187. {
  188. USBHostDevice *s = (USBHostDevice *)dev;
  189. int ret, fd, mode;
  190. int one = 1, shortpacket = 0, timeout = 50;
  191. sigset_t new_mask, old_mask;
  192. uint8_t devep = p->devep;
  193. /* protect data transfers from SIGALRM signal */
  194. sigemptyset(&new_mask);
  195. sigaddset(&new_mask, SIGALRM);
  196. sigprocmask(SIG_BLOCK, &new_mask, &old_mask);
  197. if (p->pid == USB_TOKEN_IN) {
  198. devep |= 0x80;
  199. mode = O_RDONLY;
  200. shortpacket = 1;
  201. } else {
  202. mode = O_WRONLY;
  203. }
  204. fd = ensure_ep_open(s, devep, mode);
  205. if (fd < 0) {
  206. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  207. return USB_RET_NODEV;
  208. }
  209. if (ioctl(fd, USB_SET_TIMEOUT, &timeout) < 0) {
  210. #ifdef DEBUG
  211. printf("handle_data: failed to set timeout - %s\n",
  212. strerror(errno));
  213. #endif
  214. }
  215. if (shortpacket) {
  216. if (ioctl(fd, USB_SET_SHORT_XFER, &one) < 0) {
  217. #ifdef DEBUG
  218. printf("handle_data: failed to set short xfer mode - %s\n",
  219. strerror(errno));
  220. #endif
  221. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  222. }
  223. }
  224. if (p->pid == USB_TOKEN_IN)
  225. ret = read(fd, p->data, p->len);
  226. else
  227. ret = write(fd, p->data, p->len);
  228. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  229. if (ret < 0) {
  230. #ifdef DEBUG
  231. printf("handle_data: error after %s data - %s\n",
  232. pid == USB_TOKEN_IN ? "reading" : "writing", strerror(errno));
  233. #endif
  234. switch(errno) {
  235. case ETIMEDOUT:
  236. case EINTR:
  237. return USB_RET_NAK;
  238. default:
  239. return USB_RET_STALL;
  240. }
  241. } else {
  242. return ret;
  243. }
  244. }
  245. #endif
  246. static void usb_host_handle_destroy(USBDevice *opaque)
  247. {
  248. USBHostDevice *s = (USBHostDevice *)opaque;
  249. int i;
  250. for (i = 0; i < USB_MAX_ENDPOINTS; i++)
  251. if (s->ep_fd[i] >= 0)
  252. close(s->ep_fd[i]);
  253. if (s->devfd < 0)
  254. return;
  255. close(s->devfd);
  256. qemu_free(s);
  257. }
  258. static int usb_host_initfn(USBDevice *dev)
  259. {
  260. return 0;
  261. }
  262. USBDevice *usb_host_device_open(const char *devname)
  263. {
  264. struct usb_device_info bus_info, dev_info;
  265. USBDevice *d = NULL;
  266. USBHostDevice *dev, *ret = NULL;
  267. char ctlpath[PATH_MAX + 1];
  268. char buspath[PATH_MAX + 1];
  269. int bfd, dfd, bus, address, i;
  270. int ugendebug = UGEN_DEBUG_LEVEL;
  271. if (usb_host_find_device(&bus, &address, devname) < 0) {
  272. goto fail;
  273. }
  274. snprintf(buspath, PATH_MAX, "/dev/usb%d", bus);
  275. bfd = open(buspath, O_RDWR);
  276. if (bfd < 0) {
  277. #ifdef DEBUG
  278. printf("usb_host_device_open: failed to open usb bus - %s\n",
  279. strerror(errno));
  280. #endif
  281. goto fail;
  282. }
  283. bus_info.udi_addr = address;
  284. if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0) {
  285. #ifdef DEBUG
  286. printf("usb_host_device_open: failed to grab bus information - %s\n",
  287. strerror(errno));
  288. #endif
  289. goto fail_bfd;
  290. }
  291. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  292. snprintf(ctlpath, PATH_MAX, "/dev/%s", bus_info.udi_devnames[0]);
  293. #else
  294. snprintf(ctlpath, PATH_MAX, "/dev/%s.00", bus_info.udi_devnames[0]);
  295. #endif
  296. dfd = open(ctlpath, O_RDWR);
  297. if (dfd < 0) {
  298. dfd = open(ctlpath, O_RDONLY);
  299. if (dfd < 0) {
  300. #ifdef DEBUG
  301. printf("usb_host_device_open: failed to open usb device %s - %s\n",
  302. ctlpath, strerror(errno));
  303. #endif
  304. }
  305. goto fail_dfd;
  306. }
  307. if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) {
  308. #ifdef DEBUG
  309. printf("usb_host_device_open: failed to grab device info - %s\n",
  310. strerror(errno));
  311. #endif
  312. goto fail_dfd;
  313. }
  314. d = usb_create(NULL /* FIXME */, "usb-host");
  315. dev = DO_UPCAST(USBHostDevice, dev, d);
  316. if (dev_info.udi_speed == 1) {
  317. dev->dev.speed = USB_SPEED_LOW - 1;
  318. dev->dev.speedmask = USB_SPEED_MASK_LOW;
  319. } else {
  320. dev->dev.speed = USB_SPEED_FULL - 1;
  321. dev->dev.speedmask = USB_SPEED_MASK_FULL;
  322. }
  323. if (strncmp(dev_info.udi_product, "product", 7) != 0) {
  324. pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
  325. dev_info.udi_product);
  326. } else {
  327. snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
  328. "host:%s", devname);
  329. }
  330. pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/");
  331. pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]);
  332. /* Mark the endpoints as not yet open */
  333. for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
  334. dev->ep_fd[i] = -1;
  335. }
  336. ioctl(dfd, USB_SETDEBUG, &ugendebug);
  337. ret = (USBDevice *)dev;
  338. fail_dfd:
  339. close(dfd);
  340. fail_bfd:
  341. close(bfd);
  342. fail:
  343. return ret;
  344. }
  345. static struct USBDeviceInfo usb_host_dev_info = {
  346. .product_desc = "USB Host Device",
  347. .qdev.name = "usb-host",
  348. .qdev.size = sizeof(USBHostDevice),
  349. .init = usb_host_initfn,
  350. .handle_packet = usb_generic_handle_packet,
  351. .handle_reset = usb_host_handle_reset,
  352. #if 0
  353. .handle_control = usb_host_handle_control,
  354. .handle_data = usb_host_handle_data,
  355. #endif
  356. .handle_destroy = usb_host_handle_destroy,
  357. };
  358. static void usb_host_register_devices(void)
  359. {
  360. usb_qdev_register(&usb_host_dev_info);
  361. }
  362. device_init(usb_host_register_devices)
  363. static int usb_host_scan(void *opaque, USBScanFunc *func)
  364. {
  365. struct usb_device_info bus_info;
  366. struct usb_device_info dev_info;
  367. uint16_t vendor_id, product_id, class_id, speed;
  368. int bfd, dfd, bus, address;
  369. char busbuf[20], devbuf[20], product_name[256];
  370. int ret = 0;
  371. for (bus = 0; bus < 10; bus++) {
  372. snprintf(busbuf, sizeof(busbuf) - 1, "/dev/usb%d", bus);
  373. bfd = open(busbuf, O_RDWR);
  374. if (bfd < 0)
  375. continue;
  376. for (address = 1; address < 127; address++) {
  377. bus_info.udi_addr = address;
  378. if (ioctl(bfd, USB_DEVICEINFO, &bus_info) < 0)
  379. continue;
  380. /* only list devices that can be used by generic layer */
  381. if (strncmp(bus_info.udi_devnames[0], "ugen", 4) != 0)
  382. continue;
  383. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  384. snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s", bus_info.udi_devnames[0]);
  385. #else
  386. snprintf(devbuf, sizeof(devbuf) - 1, "/dev/%s.00", bus_info.udi_devnames[0]);
  387. #endif
  388. dfd = open(devbuf, O_RDONLY);
  389. if (dfd < 0) {
  390. #ifdef DEBUG
  391. printf("usb_host_scan: couldn't open device %s - %s\n", devbuf,
  392. strerror(errno));
  393. #endif
  394. continue;
  395. }
  396. if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0)
  397. printf("usb_host_scan: couldn't get device information for %s - %s\n",
  398. devbuf, strerror(errno));
  399. /* XXX: might need to fixup endianness of word values before copying over */
  400. vendor_id = dev_info.udi_vendorNo;
  401. product_id = dev_info.udi_productNo;
  402. class_id = dev_info.udi_class;
  403. speed = dev_info.udi_speed;
  404. if (strncmp(dev_info.udi_product, "product", 7) != 0)
  405. pstrcpy(product_name, sizeof(product_name),
  406. dev_info.udi_product);
  407. else
  408. product_name[0] = '\0';
  409. ret = func(opaque, bus, address, class_id, vendor_id,
  410. product_id, product_name, speed);
  411. close(dfd);
  412. if (ret)
  413. goto the_end;
  414. }
  415. close(bfd);
  416. }
  417. the_end:
  418. return ret;
  419. }
  420. typedef struct FindDeviceState {
  421. int vendor_id;
  422. int product_id;
  423. int bus_num;
  424. int addr;
  425. } FindDeviceState;
  426. static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
  427. int class_id,
  428. int vendor_id, int product_id,
  429. const char *product_name, int speed)
  430. {
  431. FindDeviceState *s = opaque;
  432. if (vendor_id == s->vendor_id &&
  433. product_id == s->product_id) {
  434. s->bus_num = bus_num;
  435. s->addr = addr;
  436. return 1;
  437. } else {
  438. return 0;
  439. }
  440. }
  441. /* the syntax is :
  442. 'bus.addr' (decimal numbers) or
  443. 'vendor_id:product_id' (hexa numbers) */
  444. static int usb_host_find_device(int *pbus_num, int *paddr,
  445. const char *devname)
  446. {
  447. const char *p;
  448. int ret;
  449. FindDeviceState fs;
  450. p = strchr(devname, '.');
  451. if (p) {
  452. *pbus_num = strtoul(devname, NULL, 0);
  453. *paddr = strtoul(p + 1, NULL, 0);
  454. return 0;
  455. }
  456. p = strchr(devname, ':');
  457. if (p) {
  458. fs.vendor_id = strtoul(devname, NULL, 16);
  459. fs.product_id = strtoul(p + 1, NULL, 16);
  460. ret = usb_host_scan(&fs, usb_host_find_device_scan);
  461. if (ret) {
  462. *pbus_num = fs.bus_num;
  463. *paddr = fs.addr;
  464. return 0;
  465. }
  466. }
  467. return -1;
  468. }
  469. /**********************/
  470. /* USB host device info */
  471. struct usb_class_info {
  472. int class;
  473. const char *class_name;
  474. };
  475. static const struct usb_class_info usb_class_info[] = {
  476. { USB_CLASS_AUDIO, "Audio"},
  477. { USB_CLASS_COMM, "Communication"},
  478. { USB_CLASS_HID, "HID"},
  479. { USB_CLASS_HUB, "Hub" },
  480. { USB_CLASS_PHYSICAL, "Physical" },
  481. { USB_CLASS_PRINTER, "Printer" },
  482. { USB_CLASS_MASS_STORAGE, "Storage" },
  483. { USB_CLASS_CDC_DATA, "Data" },
  484. { USB_CLASS_APP_SPEC, "Application Specific" },
  485. { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
  486. { USB_CLASS_STILL_IMAGE, "Still Image" },
  487. { USB_CLASS_CSCID, "Smart Card" },
  488. { USB_CLASS_CONTENT_SEC, "Content Security" },
  489. { -1, NULL }
  490. };
  491. static const char *usb_class_str(uint8_t class)
  492. {
  493. const struct usb_class_info *p;
  494. for (p = usb_class_info; p->class != -1; p++) {
  495. if (p->class == class)
  496. break;
  497. }
  498. return p->class_name;
  499. }
  500. static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
  501. int vendor_id, int product_id,
  502. const char *product_name,
  503. int speed)
  504. {
  505. const char *class_str, *speed_str;
  506. switch(speed) {
  507. case USB_SPEED_LOW:
  508. speed_str = "1.5";
  509. break;
  510. case USB_SPEED_FULL:
  511. speed_str = "12";
  512. break;
  513. case USB_SPEED_HIGH:
  514. speed_str = "480";
  515. break;
  516. default:
  517. speed_str = "?";
  518. break;
  519. }
  520. monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
  521. bus_num, addr, speed_str);
  522. class_str = usb_class_str(class_id);
  523. if (class_str)
  524. monitor_printf(mon, " %s:", class_str);
  525. else
  526. monitor_printf(mon, " Class %02x:", class_id);
  527. monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
  528. if (product_name[0] != '\0')
  529. monitor_printf(mon, ", %s", product_name);
  530. monitor_printf(mon, "\n");
  531. }
  532. static int usb_host_info_device(void *opaque,
  533. int bus_num, int addr,
  534. int class_id,
  535. int vendor_id, int product_id,
  536. const char *product_name,
  537. int speed)
  538. {
  539. Monitor *mon = opaque;
  540. usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
  541. product_name, speed);
  542. return 0;
  543. }
  544. void usb_host_info(Monitor *mon)
  545. {
  546. usb_host_scan(mon, usb_host_info_device);
  547. }
  548. /* XXX add this */
  549. int usb_host_device_close(const char *devname)
  550. {
  551. return 0;
  552. }