openbsd_usb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * Copyright © 2011-2013 Martin Pieuchot <mpi@openbsd.org>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <config.h>
  19. #include <sys/time.h>
  20. #include <sys/types.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <dev/usb/usb.h>
  28. #include "libusbi.h"
  29. struct device_priv {
  30. char *devname; /* name of the ugen(4) node */
  31. int fd; /* device file descriptor */
  32. usb_config_descriptor_t *cdesc; /* active config descriptor */
  33. };
  34. struct handle_priv {
  35. int endpoints[USB_MAX_ENDPOINTS];
  36. };
  37. /*
  38. * Backend functions
  39. */
  40. static int obsd_get_device_list(struct libusb_context *,
  41. struct discovered_devs **);
  42. static int obsd_open(struct libusb_device_handle *);
  43. static void obsd_close(struct libusb_device_handle *);
  44. static int obsd_get_active_config_descriptor(struct libusb_device *,
  45. void *, size_t);
  46. static int obsd_get_config_descriptor(struct libusb_device *, uint8_t,
  47. void *, size_t);
  48. static int obsd_get_configuration(struct libusb_device_handle *, uint8_t *);
  49. static int obsd_set_configuration(struct libusb_device_handle *, int);
  50. static int obsd_claim_interface(struct libusb_device_handle *, uint8_t);
  51. static int obsd_release_interface(struct libusb_device_handle *, uint8_t);
  52. static int obsd_set_interface_altsetting(struct libusb_device_handle *, uint8_t,
  53. uint8_t);
  54. static int obsd_clear_halt(struct libusb_device_handle *, unsigned char);
  55. static void obsd_destroy_device(struct libusb_device *);
  56. static int obsd_submit_transfer(struct usbi_transfer *);
  57. static int obsd_cancel_transfer(struct usbi_transfer *);
  58. static int obsd_handle_transfer_completion(struct usbi_transfer *);
  59. /*
  60. * Private functions
  61. */
  62. static int _errno_to_libusb(int);
  63. static int _cache_active_config_descriptor(struct libusb_device *);
  64. static int _sync_control_transfer(struct usbi_transfer *);
  65. static int _sync_gen_transfer(struct usbi_transfer *);
  66. static int _access_endpoint(struct libusb_transfer *);
  67. static int _bus_open(int);
  68. const struct usbi_os_backend usbi_backend = {
  69. .name = "Synchronous OpenBSD backend",
  70. .get_device_list = obsd_get_device_list,
  71. .open = obsd_open,
  72. .close = obsd_close,
  73. .get_active_config_descriptor = obsd_get_active_config_descriptor,
  74. .get_config_descriptor = obsd_get_config_descriptor,
  75. .get_configuration = obsd_get_configuration,
  76. .set_configuration = obsd_set_configuration,
  77. .claim_interface = obsd_claim_interface,
  78. .release_interface = obsd_release_interface,
  79. .set_interface_altsetting = obsd_set_interface_altsetting,
  80. .clear_halt = obsd_clear_halt,
  81. .destroy_device = obsd_destroy_device,
  82. .submit_transfer = obsd_submit_transfer,
  83. .cancel_transfer = obsd_cancel_transfer,
  84. .handle_transfer_completion = obsd_handle_transfer_completion,
  85. .device_priv_size = sizeof(struct device_priv),
  86. .device_handle_priv_size = sizeof(struct handle_priv),
  87. };
  88. #define DEVPATH "/dev/"
  89. #define USBDEV DEVPATH "usb"
  90. int
  91. obsd_get_device_list(struct libusb_context * ctx,
  92. struct discovered_devs **discdevs)
  93. {
  94. struct discovered_devs *ddd;
  95. struct libusb_device *dev;
  96. struct device_priv *dpriv;
  97. struct usb_device_info di;
  98. struct usb_device_ddesc dd;
  99. unsigned long session_id;
  100. char devices[USB_MAX_DEVICES];
  101. char busnode[16];
  102. char *udevname;
  103. int fd, addr, i, j;
  104. usbi_dbg(ctx, " ");
  105. for (i = 0; i < 8; i++) {
  106. snprintf(busnode, sizeof(busnode), USBDEV "%d", i);
  107. if ((fd = open(busnode, O_RDWR)) < 0) {
  108. if (errno != ENOENT && errno != ENXIO)
  109. usbi_err(ctx, "could not open %s", busnode);
  110. continue;
  111. }
  112. bzero(devices, sizeof(devices));
  113. for (addr = 1; addr < USB_MAX_DEVICES; addr++) {
  114. if (devices[addr])
  115. continue;
  116. di.udi_addr = addr;
  117. if (ioctl(fd, USB_DEVICEINFO, &di) < 0)
  118. continue;
  119. /*
  120. * XXX If ugen(4) is attached to the USB device
  121. * it will be used.
  122. */
  123. udevname = NULL;
  124. for (j = 0; j < USB_MAX_DEVNAMES; j++)
  125. if (!strncmp("ugen", di.udi_devnames[j], 4)) {
  126. udevname = strdup(di.udi_devnames[j]);
  127. break;
  128. }
  129. session_id = (di.udi_bus << 8 | di.udi_addr);
  130. dev = usbi_get_device_by_session_id(ctx, session_id);
  131. if (dev == NULL) {
  132. dev = usbi_alloc_device(ctx, session_id);
  133. if (dev == NULL) {
  134. close(fd);
  135. return (LIBUSB_ERROR_NO_MEM);
  136. }
  137. dev->bus_number = di.udi_bus;
  138. dev->device_address = di.udi_addr;
  139. dev->speed = di.udi_speed;
  140. dev->port_number = di.udi_port;
  141. dpriv = usbi_get_device_priv(dev);
  142. dpriv->fd = -1;
  143. dpriv->devname = udevname;
  144. dd.udd_bus = di.udi_bus;
  145. dd.udd_addr = di.udi_addr;
  146. if (ioctl(fd, USB_DEVICE_GET_DDESC, &dd) < 0) {
  147. libusb_unref_device(dev);
  148. continue;
  149. }
  150. static_assert(sizeof(dev->device_descriptor) == sizeof(dd.udd_desc),
  151. "mismatch between libusb and OS device descriptor sizes");
  152. memcpy(&dev->device_descriptor, &dd.udd_desc, LIBUSB_DT_DEVICE_SIZE);
  153. usbi_localize_device_descriptor(&dev->device_descriptor);
  154. if (_cache_active_config_descriptor(dev)) {
  155. libusb_unref_device(dev);
  156. continue;
  157. }
  158. if (usbi_sanitize_device(dev)) {
  159. libusb_unref_device(dev);
  160. continue;
  161. }
  162. }
  163. ddd = discovered_devs_append(*discdevs, dev);
  164. if (ddd == NULL) {
  165. close(fd);
  166. return (LIBUSB_ERROR_NO_MEM);
  167. }
  168. libusb_unref_device(dev);
  169. *discdevs = ddd;
  170. devices[addr] = 1;
  171. }
  172. close(fd);
  173. }
  174. return (LIBUSB_SUCCESS);
  175. }
  176. int
  177. obsd_open(struct libusb_device_handle *handle)
  178. {
  179. struct device_priv *dpriv = usbi_get_device_priv(handle->dev);
  180. char devnode[16];
  181. if (dpriv->devname) {
  182. int fd;
  183. /*
  184. * Only open ugen(4) attached devices read-write, all
  185. * read-only operations are done through the bus node.
  186. */
  187. snprintf(devnode, sizeof(devnode), DEVPATH "%s.00",
  188. dpriv->devname);
  189. fd = open(devnode, O_RDWR);
  190. if (fd < 0)
  191. return _errno_to_libusb(errno);
  192. dpriv->fd = fd;
  193. usbi_dbg(HANDLE_CTX(handle), "open %s: fd %d", devnode, dpriv->fd);
  194. }
  195. return (LIBUSB_SUCCESS);
  196. }
  197. void
  198. obsd_close(struct libusb_device_handle *handle)
  199. {
  200. struct device_priv *dpriv = usbi_get_device_priv(handle->dev);
  201. if (dpriv->devname) {
  202. usbi_dbg(HANDLE_CTX(handle), "close: fd %d", dpriv->fd);
  203. close(dpriv->fd);
  204. dpriv->fd = -1;
  205. }
  206. }
  207. int
  208. obsd_get_active_config_descriptor(struct libusb_device *dev,
  209. void *buf, size_t len)
  210. {
  211. struct device_priv *dpriv = usbi_get_device_priv(dev);
  212. len = MIN(len, (size_t)UGETW(dpriv->cdesc->wTotalLength));
  213. usbi_dbg(DEVICE_CTX(dev), "len %zu", len);
  214. memcpy(buf, dpriv->cdesc, len);
  215. return ((int)len);
  216. }
  217. int
  218. obsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
  219. void *buf, size_t len)
  220. {
  221. struct usb_device_fdesc udf;
  222. int fd, err;
  223. if ((fd = _bus_open(dev->bus_number)) < 0)
  224. return _errno_to_libusb(errno);
  225. udf.udf_bus = dev->bus_number;
  226. udf.udf_addr = dev->device_address;
  227. udf.udf_config_index = idx;
  228. udf.udf_size = len;
  229. udf.udf_data = buf;
  230. usbi_dbg(DEVICE_CTX(dev), "index %d, len %zu", udf.udf_config_index, len);
  231. if (ioctl(fd, USB_DEVICE_GET_FDESC, &udf) < 0) {
  232. err = errno;
  233. close(fd);
  234. return _errno_to_libusb(err);
  235. }
  236. close(fd);
  237. return ((int)len);
  238. }
  239. int
  240. obsd_get_configuration(struct libusb_device_handle *handle, uint8_t *config)
  241. {
  242. struct device_priv *dpriv = usbi_get_device_priv(handle->dev);
  243. *config = dpriv->cdesc->bConfigurationValue;
  244. usbi_dbg(HANDLE_CTX(handle), "bConfigurationValue %u", *config);
  245. return (LIBUSB_SUCCESS);
  246. }
  247. int
  248. obsd_set_configuration(struct libusb_device_handle *handle, int config)
  249. {
  250. struct device_priv *dpriv = usbi_get_device_priv(handle->dev);
  251. if (dpriv->devname == NULL)
  252. return (LIBUSB_ERROR_NOT_SUPPORTED);
  253. usbi_dbg(HANDLE_CTX(handle), "bConfigurationValue %d", config);
  254. if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
  255. return _errno_to_libusb(errno);
  256. return _cache_active_config_descriptor(handle->dev);
  257. }
  258. int
  259. obsd_claim_interface(struct libusb_device_handle *handle, uint8_t iface)
  260. {
  261. struct handle_priv *hpriv = usbi_get_device_handle_priv(handle);
  262. int i;
  263. UNUSED(iface);
  264. for (i = 0; i < USB_MAX_ENDPOINTS; i++)
  265. hpriv->endpoints[i] = -1;
  266. return (LIBUSB_SUCCESS);
  267. }
  268. int
  269. obsd_release_interface(struct libusb_device_handle *handle, uint8_t iface)
  270. {
  271. struct handle_priv *hpriv = usbi_get_device_handle_priv(handle);
  272. int i;
  273. UNUSED(iface);
  274. for (i = 0; i < USB_MAX_ENDPOINTS; i++)
  275. if (hpriv->endpoints[i] >= 0)
  276. close(hpriv->endpoints[i]);
  277. return (LIBUSB_SUCCESS);
  278. }
  279. int
  280. obsd_set_interface_altsetting(struct libusb_device_handle *handle, uint8_t iface,
  281. uint8_t altsetting)
  282. {
  283. struct device_priv *dpriv = usbi_get_device_priv(handle->dev);
  284. struct usb_alt_interface intf;
  285. if (dpriv->devname == NULL)
  286. return (LIBUSB_ERROR_NOT_SUPPORTED);
  287. usbi_dbg(HANDLE_CTX(handle), "iface %u, setting %u", iface, altsetting);
  288. memset(&intf, 0, sizeof(intf));
  289. intf.uai_interface_index = iface;
  290. intf.uai_alt_no = altsetting;
  291. if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
  292. return _errno_to_libusb(errno);
  293. return (LIBUSB_SUCCESS);
  294. }
  295. int
  296. obsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
  297. {
  298. struct usb_ctl_request req;
  299. int fd, err;
  300. if ((fd = _bus_open(handle->dev->bus_number)) < 0)
  301. return _errno_to_libusb(errno);
  302. usbi_dbg(HANDLE_CTX(handle), " ");
  303. req.ucr_addr = handle->dev->device_address;
  304. req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
  305. req.ucr_request.bRequest = UR_CLEAR_FEATURE;
  306. USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
  307. USETW(req.ucr_request.wIndex, endpoint);
  308. USETW(req.ucr_request.wLength, 0);
  309. if (ioctl(fd, USB_REQUEST, &req) < 0) {
  310. err = errno;
  311. close(fd);
  312. return _errno_to_libusb(err);
  313. }
  314. close(fd);
  315. return (LIBUSB_SUCCESS);
  316. }
  317. void
  318. obsd_destroy_device(struct libusb_device *dev)
  319. {
  320. struct device_priv *dpriv = usbi_get_device_priv(dev);
  321. usbi_dbg(DEVICE_CTX(dev), " ");
  322. free(dpriv->cdesc);
  323. free(dpriv->devname);
  324. }
  325. int
  326. obsd_submit_transfer(struct usbi_transfer *itransfer)
  327. {
  328. struct libusb_transfer *transfer;
  329. int err = 0;
  330. usbi_dbg(ITRANSFER_CTX(itransfer), " ");
  331. transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
  332. switch (transfer->type) {
  333. case LIBUSB_TRANSFER_TYPE_CONTROL:
  334. err = _sync_control_transfer(itransfer);
  335. break;
  336. case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
  337. if (IS_XFEROUT(transfer)) {
  338. /* Isochronous write is not supported */
  339. err = LIBUSB_ERROR_NOT_SUPPORTED;
  340. break;
  341. }
  342. err = _sync_gen_transfer(itransfer);
  343. break;
  344. case LIBUSB_TRANSFER_TYPE_BULK:
  345. case LIBUSB_TRANSFER_TYPE_INTERRUPT:
  346. if (IS_XFEROUT(transfer) &&
  347. transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
  348. err = LIBUSB_ERROR_NOT_SUPPORTED;
  349. break;
  350. }
  351. err = _sync_gen_transfer(itransfer);
  352. break;
  353. case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
  354. err = LIBUSB_ERROR_NOT_SUPPORTED;
  355. break;
  356. }
  357. if (err)
  358. return (err);
  359. usbi_signal_transfer_completion(itransfer);
  360. return (LIBUSB_SUCCESS);
  361. }
  362. int
  363. obsd_cancel_transfer(struct usbi_transfer *itransfer)
  364. {
  365. UNUSED(itransfer);
  366. usbi_dbg(ITRANSFER_CTX(itransfer), " ");
  367. return (LIBUSB_ERROR_NOT_SUPPORTED);
  368. }
  369. int
  370. obsd_handle_transfer_completion(struct usbi_transfer *itransfer)
  371. {
  372. return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
  373. }
  374. int
  375. _errno_to_libusb(int err)
  376. {
  377. usbi_dbg(NULL, "error: %s (%d)", strerror(err), err);
  378. switch (err) {
  379. case EIO:
  380. return (LIBUSB_ERROR_IO);
  381. case EACCES:
  382. return (LIBUSB_ERROR_ACCESS);
  383. case ENOENT:
  384. return (LIBUSB_ERROR_NO_DEVICE);
  385. case ENOMEM:
  386. return (LIBUSB_ERROR_NO_MEM);
  387. case ETIMEDOUT:
  388. return (LIBUSB_ERROR_TIMEOUT);
  389. }
  390. return (LIBUSB_ERROR_OTHER);
  391. }
  392. int
  393. _cache_active_config_descriptor(struct libusb_device *dev)
  394. {
  395. struct device_priv *dpriv = usbi_get_device_priv(dev);
  396. struct usb_device_cdesc udc;
  397. struct usb_device_fdesc udf;
  398. void *buf;
  399. int fd, len, err;
  400. if ((fd = _bus_open(dev->bus_number)) < 0)
  401. return _errno_to_libusb(errno);
  402. usbi_dbg(DEVICE_CTX(dev), "fd %d, addr %d", fd, dev->device_address);
  403. udc.udc_bus = dev->bus_number;
  404. udc.udc_addr = dev->device_address;
  405. udc.udc_config_index = USB_CURRENT_CONFIG_INDEX;
  406. if (ioctl(fd, USB_DEVICE_GET_CDESC, &udc) < 0) {
  407. err = errno;
  408. close(fd);
  409. return _errno_to_libusb(errno);
  410. }
  411. usbi_dbg(DEVICE_CTX(dev), "active bLength %d", udc.udc_desc.bLength);
  412. len = UGETW(udc.udc_desc.wTotalLength);
  413. buf = malloc((size_t)len);
  414. if (buf == NULL)
  415. return (LIBUSB_ERROR_NO_MEM);
  416. udf.udf_bus = dev->bus_number;
  417. udf.udf_addr = dev->device_address;
  418. udf.udf_config_index = udc.udc_config_index;
  419. udf.udf_size = len;
  420. udf.udf_data = buf;
  421. usbi_dbg(DEVICE_CTX(dev), "index %d, len %d", udf.udf_config_index, len);
  422. if (ioctl(fd, USB_DEVICE_GET_FDESC, &udf) < 0) {
  423. err = errno;
  424. close(fd);
  425. free(buf);
  426. return _errno_to_libusb(err);
  427. }
  428. close(fd);
  429. if (dpriv->cdesc)
  430. free(dpriv->cdesc);
  431. dpriv->cdesc = buf;
  432. return (LIBUSB_SUCCESS);
  433. }
  434. int
  435. _sync_control_transfer(struct usbi_transfer *itransfer)
  436. {
  437. struct libusb_transfer *transfer;
  438. struct libusb_control_setup *setup;
  439. struct device_priv *dpriv;
  440. struct usb_ctl_request req;
  441. transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
  442. dpriv = usbi_get_device_priv(transfer->dev_handle->dev);
  443. setup = (struct libusb_control_setup *)transfer->buffer;
  444. usbi_dbg(ITRANSFER_CTX(itransfer), "type 0x%x request 0x%x value 0x%x index %d length %d timeout %d",
  445. setup->bmRequestType, setup->bRequest,
  446. libusb_le16_to_cpu(setup->wValue),
  447. libusb_le16_to_cpu(setup->wIndex),
  448. libusb_le16_to_cpu(setup->wLength), transfer->timeout);
  449. req.ucr_addr = transfer->dev_handle->dev->device_address;
  450. req.ucr_request.bmRequestType = setup->bmRequestType;
  451. req.ucr_request.bRequest = setup->bRequest;
  452. /* Don't use USETW, libusb already deals with the endianness */
  453. (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
  454. (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
  455. (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
  456. req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
  457. if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
  458. req.ucr_flags = USBD_SHORT_XFER_OK;
  459. if (dpriv->devname == NULL) {
  460. /*
  461. * XXX If the device is not attached to ugen(4) it is
  462. * XXX still possible to submit a control transfer but
  463. * XXX with the default timeout only.
  464. */
  465. int fd, err;
  466. if ((fd = _bus_open(transfer->dev_handle->dev->bus_number)) < 0)
  467. return _errno_to_libusb(errno);
  468. if ((ioctl(fd, USB_REQUEST, &req)) < 0) {
  469. err = errno;
  470. close(fd);
  471. return _errno_to_libusb(err);
  472. }
  473. close(fd);
  474. } else {
  475. if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
  476. return _errno_to_libusb(errno);
  477. if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
  478. return _errno_to_libusb(errno);
  479. }
  480. itransfer->transferred = req.ucr_actlen;
  481. usbi_dbg(ITRANSFER_CTX(itransfer), "transferred %d", itransfer->transferred);
  482. return (0);
  483. }
  484. int
  485. _access_endpoint(struct libusb_transfer *transfer)
  486. {
  487. struct handle_priv *hpriv;
  488. struct device_priv *dpriv;
  489. char devnode[16];
  490. int fd, endpt;
  491. mode_t mode;
  492. hpriv = usbi_get_device_handle_priv(transfer->dev_handle);
  493. dpriv = usbi_get_device_priv(transfer->dev_handle->dev);
  494. endpt = UE_GET_ADDR(transfer->endpoint);
  495. mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
  496. usbi_dbg(TRANSFER_CTX(transfer), "endpoint %d mode %d", endpt, mode);
  497. if (hpriv->endpoints[endpt] < 0) {
  498. /* Pick the right endpoint node */
  499. snprintf(devnode, sizeof(devnode), DEVPATH "%s.%02d",
  500. dpriv->devname, endpt);
  501. /* We may need to read/write to the same endpoint later. */
  502. if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
  503. if ((fd = open(devnode, mode)) < 0)
  504. return (-1);
  505. hpriv->endpoints[endpt] = fd;
  506. }
  507. return (hpriv->endpoints[endpt]);
  508. }
  509. int
  510. _sync_gen_transfer(struct usbi_transfer *itransfer)
  511. {
  512. struct libusb_transfer *transfer;
  513. struct device_priv *dpriv;
  514. int fd, nr = 1;
  515. transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
  516. dpriv = usbi_get_device_priv(transfer->dev_handle->dev);
  517. if (dpriv->devname == NULL)
  518. return (LIBUSB_ERROR_NOT_SUPPORTED);
  519. /*
  520. * Bulk, Interrupt or Isochronous transfer depends on the
  521. * endpoint and thus the node to open.
  522. */
  523. if ((fd = _access_endpoint(transfer)) < 0)
  524. return _errno_to_libusb(errno);
  525. if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
  526. return _errno_to_libusb(errno);
  527. if (IS_XFERIN(transfer)) {
  528. if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
  529. if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
  530. return _errno_to_libusb(errno);
  531. nr = read(fd, transfer->buffer, transfer->length);
  532. } else {
  533. nr = write(fd, transfer->buffer, transfer->length);
  534. }
  535. if (nr < 0)
  536. return _errno_to_libusb(errno);
  537. itransfer->transferred = nr;
  538. return (0);
  539. }
  540. int
  541. _bus_open(int number)
  542. {
  543. char busnode[16];
  544. snprintf(busnode, sizeof(busnode), USBDEV "%d", number);
  545. return open(busnode, O_RDWR);
  546. }