2
0

usb-bsd.c 17 KB

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