2 * Copyright © 2011-2013 Martin Pieuchot <mpi@openbsd.org>
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.
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.
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
20 #include <sys/types.h>
29 #include <dev/usb/usb.h>
35 char *devname; /* name of the ugen(4) node */
36 int fd; /* device file descriptor */
38 unsigned char *cdesc; /* active config descriptor */
39 usb_device_descriptor_t ddesc; /* usb device descriptor */
43 int pipe[2]; /* for event notification */
44 int endpoints[USB_MAX_ENDPOINTS];
50 static int obsd_get_device_list(struct libusb_context *,
51 struct discovered_devs **);
52 static int obsd_open(struct libusb_device_handle *);
53 static void obsd_close(struct libusb_device_handle *);
55 static int obsd_get_device_descriptor(struct libusb_device *, unsigned char *,
57 static int obsd_get_active_config_descriptor(struct libusb_device *,
58 unsigned char *, size_t, int *);
59 static int obsd_get_config_descriptor(struct libusb_device *, uint8_t,
60 unsigned char *, size_t, int *);
62 static int obsd_get_configuration(struct libusb_device_handle *, int *);
63 static int obsd_set_configuration(struct libusb_device_handle *, int);
65 static int obsd_claim_interface(struct libusb_device_handle *, int);
66 static int obsd_release_interface(struct libusb_device_handle *, int);
68 static int obsd_set_interface_altsetting(struct libusb_device_handle *, int,
70 static int obsd_clear_halt(struct libusb_device_handle *, unsigned char);
71 static int obsd_reset_device(struct libusb_device_handle *);
72 static void obsd_destroy_device(struct libusb_device *);
74 static int obsd_submit_transfer(struct usbi_transfer *);
75 static int obsd_cancel_transfer(struct usbi_transfer *);
76 static void obsd_clear_transfer_priv(struct usbi_transfer *);
77 static int obsd_handle_events(struct libusb_context *ctx, struct pollfd *,
79 static int obsd_clock_gettime(int, struct timespec *);
84 static int _errno_to_libusb(int);
85 static int _cache_active_config_descriptor(struct libusb_device *);
86 static int _sync_control_transfer(struct usbi_transfer *);
87 static int _sync_gen_transfer(struct usbi_transfer *);
88 static int _access_endpoint(struct libusb_transfer *);
90 static int _bus_open(int);
93 const struct usbi_os_backend openbsd_backend = {
94 "Synchronous OpenBSD backend",
99 NULL, /* hotplug_poll */
103 obsd_get_device_descriptor,
104 obsd_get_active_config_descriptor,
105 obsd_get_config_descriptor,
106 NULL, /* get_config_descriptor_by_value() */
108 obsd_get_configuration,
109 obsd_set_configuration,
111 obsd_claim_interface,
112 obsd_release_interface,
114 obsd_set_interface_altsetting,
118 NULL, /* kernel_driver_active() */
119 NULL, /* detach_kernel_driver() */
120 NULL, /* attach_kernel_driver() */
124 obsd_submit_transfer,
125 obsd_cancel_transfer,
126 obsd_clear_transfer_priv,
131 sizeof(struct device_priv),
132 sizeof(struct handle_priv),
133 0, /* transfer_priv_size */
134 0, /* add_iso_packet_size */
137 #define DEVPATH "/dev/"
138 #define USBDEV DEVPATH "usb"
141 obsd_get_device_list(struct libusb_context * ctx,
142 struct discovered_devs **discdevs)
144 struct discovered_devs *ddd;
145 struct libusb_device *dev;
146 struct device_priv *dpriv;
147 struct usb_device_info di;
148 struct usb_device_ddesc dd;
149 unsigned long session_id;
150 char devices[USB_MAX_DEVICES];
157 for (i = 0; i < 8; i++) {
158 snprintf(busnode, sizeof(busnode), USBDEV "%d", i);
160 if ((fd = open(busnode, O_RDWR)) < 0) {
161 if (errno != ENOENT && errno != ENXIO)
162 usbi_err(ctx, "could not open %s", busnode);
166 bzero(devices, sizeof(devices));
167 for (addr = 1; addr < USB_MAX_DEVICES; addr++) {
172 if (ioctl(fd, USB_DEVICEINFO, &di) < 0)
176 * XXX If ugen(4) is attached to the USB device
180 for (j = 0; j < USB_MAX_DEVNAMES; j++)
181 if (!strncmp("ugen", di.udi_devnames[j], 4)) {
182 udevname = strdup(di.udi_devnames[j]);
186 session_id = (di.udi_bus << 8 | di.udi_addr);
187 dev = usbi_get_device_by_session_id(ctx, session_id);
190 dev = usbi_alloc_device(ctx, session_id);
193 return (LIBUSB_ERROR_NO_MEM);
196 dev->bus_number = di.udi_bus;
197 dev->device_address = di.udi_addr;
198 dev->speed = di.udi_speed;
200 dpriv = (struct device_priv *)dev->os_priv;
203 dpriv->devname = udevname;
205 dd.udd_bus = di.udi_bus;
206 dd.udd_addr = di.udi_addr;
207 if (ioctl(fd, USB_DEVICE_GET_DDESC, &dd) < 0) {
208 libusb_unref_device(dev);
211 dpriv->ddesc = dd.udd_desc;
213 if (_cache_active_config_descriptor(dev)) {
214 libusb_unref_device(dev);
218 if (usbi_sanitize_device(dev))
219 libusb_unref_device(dev);
222 ddd = discovered_devs_append(*discdevs, dev);
225 return (LIBUSB_ERROR_NO_MEM);
235 return (LIBUSB_SUCCESS);
239 obsd_open(struct libusb_device_handle *handle)
241 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
242 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
245 if (dpriv->devname) {
247 * Only open ugen(4) attached devices read-write, all
248 * read-only operations are done through the bus node.
250 snprintf(devnode, sizeof(devnode), DEVPATH "%s.00",
252 dpriv->fd = open(devnode, O_RDWR);
254 return _errno_to_libusb(errno);
256 usbi_dbg("open %s: fd %d", devnode, dpriv->fd);
259 if (pipe(hpriv->pipe) < 0)
260 return _errno_to_libusb(errno);
262 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
266 obsd_close(struct libusb_device_handle *handle)
268 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
269 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
271 if (dpriv->devname) {
272 usbi_dbg("close: fd %d", dpriv->fd);
278 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
280 close(hpriv->pipe[0]);
281 close(hpriv->pipe[1]);
285 obsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
288 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
292 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
296 return (LIBUSB_SUCCESS);
300 obsd_get_active_config_descriptor(struct libusb_device *dev,
301 unsigned char *buf, size_t len, int *host_endian)
303 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
304 usb_config_descriptor_t *ucd = (usb_config_descriptor_t *)dpriv->cdesc;
306 len = MIN(len, UGETW(ucd->wTotalLength));
308 usbi_dbg("len %d", len);
310 memcpy(buf, dpriv->cdesc, len);
318 obsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
319 unsigned char *buf, size_t len, int *host_endian)
321 struct usb_device_fdesc udf;
324 if ((fd = _bus_open(dev->bus_number)) < 0)
325 return _errno_to_libusb(errno);
327 udf.udf_bus = dev->bus_number;
328 udf.udf_addr = dev->device_address;
329 udf.udf_config_index = idx;
333 usbi_dbg("index %d, len %d", udf.udf_config_index, len);
335 if (ioctl(fd, USB_DEVICE_GET_FDESC, &udf) < 0) {
338 return _errno_to_libusb(err);
348 obsd_get_configuration(struct libusb_device_handle *handle, int *config)
350 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
351 usb_config_descriptor_t *ucd = (usb_config_descriptor_t *)dpriv->cdesc;
353 *config = ucd->bConfigurationValue;
355 usbi_dbg("bConfigurationValue %d", *config);
357 return (LIBUSB_SUCCESS);
361 obsd_set_configuration(struct libusb_device_handle *handle, int config)
363 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
365 if (dpriv->devname == NULL)
366 return (LIBUSB_ERROR_NOT_SUPPORTED);
368 usbi_dbg("bConfigurationValue %d", config);
370 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
371 return _errno_to_libusb(errno);
373 return _cache_active_config_descriptor(handle->dev);
377 obsd_claim_interface(struct libusb_device_handle *handle, int iface)
379 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
382 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
383 hpriv->endpoints[i] = -1;
385 return (LIBUSB_SUCCESS);
389 obsd_release_interface(struct libusb_device_handle *handle, int iface)
391 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
394 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
395 if (hpriv->endpoints[i] >= 0)
396 close(hpriv->endpoints[i]);
398 return (LIBUSB_SUCCESS);
402 obsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
405 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
406 struct usb_alt_interface intf;
408 if (dpriv->devname == NULL)
409 return (LIBUSB_ERROR_NOT_SUPPORTED);
411 usbi_dbg("iface %d, setting %d", iface, altsetting);
413 memset(&intf, 0, sizeof(intf));
415 intf.uai_interface_index = iface;
416 intf.uai_alt_no = altsetting;
418 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
419 return _errno_to_libusb(errno);
421 return (LIBUSB_SUCCESS);
425 obsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
427 struct usb_ctl_request req;
430 if ((fd = _bus_open(handle->dev->bus_number)) < 0)
431 return _errno_to_libusb(errno);
435 req.ucr_addr = handle->dev->device_address;
436 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
437 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
438 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
439 USETW(req.ucr_request.wIndex, endpoint);
440 USETW(req.ucr_request.wLength, 0);
442 if (ioctl(fd, USB_REQUEST, &req) < 0) {
445 return _errno_to_libusb(err);
449 return (LIBUSB_SUCCESS);
453 obsd_reset_device(struct libusb_device_handle *handle)
457 return (LIBUSB_ERROR_NOT_SUPPORTED);
461 obsd_destroy_device(struct libusb_device *dev)
463 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
468 free(dpriv->devname);
472 obsd_submit_transfer(struct usbi_transfer *itransfer)
474 struct libusb_transfer *transfer;
475 struct handle_priv *hpriv;
480 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
481 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
483 switch (transfer->type) {
484 case LIBUSB_TRANSFER_TYPE_CONTROL:
485 err = _sync_control_transfer(itransfer);
487 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
488 if (IS_XFEROUT(transfer)) {
489 /* Isochronous write is not supported */
490 err = LIBUSB_ERROR_NOT_SUPPORTED;
493 err = _sync_gen_transfer(itransfer);
495 case LIBUSB_TRANSFER_TYPE_BULK:
496 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
497 if (IS_XFEROUT(transfer) &&
498 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
499 err = LIBUSB_ERROR_NOT_SUPPORTED;
502 err = _sync_gen_transfer(itransfer);
509 if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
510 return _errno_to_libusb(errno);
512 return (LIBUSB_SUCCESS);
516 obsd_cancel_transfer(struct usbi_transfer *itransfer)
520 return (LIBUSB_ERROR_NOT_SUPPORTED);
524 obsd_clear_transfer_priv(struct usbi_transfer *itransfer)
532 obsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
535 struct libusb_device_handle *handle;
536 struct handle_priv *hpriv = NULL;
537 struct usbi_transfer *itransfer;
538 struct pollfd *pollfd;
543 pthread_mutex_lock(&ctx->open_devs_lock);
544 for (i = 0; i < nfds && num_ready > 0; i++) {
547 if (!pollfd->revents)
552 list_for_each_entry(handle, &ctx->open_devs, list,
553 struct libusb_device_handle) {
554 hpriv = (struct handle_priv *)handle->os_priv;
556 if (hpriv->pipe[0] == pollfd->fd)
563 usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
568 if (pollfd->revents & POLLERR) {
569 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
570 usbi_handle_disconnect(handle);
574 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
579 if ((err = usbi_handle_transfer_completion(itransfer,
580 LIBUSB_TRANSFER_COMPLETED)))
583 pthread_mutex_unlock(&ctx->open_devs_lock);
586 return _errno_to_libusb(err);
588 return (LIBUSB_SUCCESS);
592 obsd_clock_gettime(int clkid, struct timespec *tp)
594 usbi_dbg("clock %d", clkid);
596 if (clkid == USBI_CLOCK_REALTIME)
597 return clock_gettime(CLOCK_REALTIME, tp);
599 if (clkid == USBI_CLOCK_MONOTONIC)
600 return clock_gettime(CLOCK_MONOTONIC, tp);
602 return (LIBUSB_ERROR_INVALID_PARAM);
606 _errno_to_libusb(int err)
608 usbi_dbg("error: %s (%d)", strerror(err), err);
612 return (LIBUSB_ERROR_IO);
614 return (LIBUSB_ERROR_ACCESS);
616 return (LIBUSB_ERROR_NO_DEVICE);
618 return (LIBUSB_ERROR_NO_MEM);
620 return (LIBUSB_ERROR_TIMEOUT);
623 return (LIBUSB_ERROR_OTHER);
627 _cache_active_config_descriptor(struct libusb_device *dev)
629 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
630 struct usb_device_cdesc udc;
631 struct usb_device_fdesc udf;
635 if ((fd = _bus_open(dev->bus_number)) < 0)
636 return _errno_to_libusb(errno);
638 usbi_dbg("fd %d, addr %d", fd, dev->device_address);
640 udc.udc_bus = dev->bus_number;
641 udc.udc_addr = dev->device_address;
642 udc.udc_config_index = USB_CURRENT_CONFIG_INDEX;
643 if (ioctl(fd, USB_DEVICE_GET_CDESC, &udc) < 0) {
646 return _errno_to_libusb(errno);
649 usbi_dbg("active bLength %d", udc.udc_desc.bLength);
651 len = UGETW(udc.udc_desc.wTotalLength);
654 return (LIBUSB_ERROR_NO_MEM);
656 udf.udf_bus = dev->bus_number;
657 udf.udf_addr = dev->device_address;
658 udf.udf_config_index = udc.udc_config_index;
662 usbi_dbg("index %d, len %d", udf.udf_config_index, len);
664 if (ioctl(fd, USB_DEVICE_GET_FDESC, &udf) < 0) {
668 return _errno_to_libusb(err);
676 return (LIBUSB_SUCCESS);
680 _sync_control_transfer(struct usbi_transfer *itransfer)
682 struct libusb_transfer *transfer;
683 struct libusb_control_setup *setup;
684 struct device_priv *dpriv;
685 struct usb_ctl_request req;
687 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
688 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
689 setup = (struct libusb_control_setup *)transfer->buffer;
691 usbi_dbg("type %x request %x value %x index %d length %d timeout %d",
692 setup->bmRequestType, setup->bRequest,
693 libusb_le16_to_cpu(setup->wValue),
694 libusb_le16_to_cpu(setup->wIndex),
695 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
697 req.ucr_addr = transfer->dev_handle->dev->device_address;
698 req.ucr_request.bmRequestType = setup->bmRequestType;
699 req.ucr_request.bRequest = setup->bRequest;
700 /* Don't use USETW, libusbx already deals with the endianness */
701 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
702 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
703 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
704 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
706 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
707 req.ucr_flags = USBD_SHORT_XFER_OK;
709 if (dpriv->devname == NULL) {
711 * XXX If the device is not attached to ugen(4) it is
712 * XXX still possible to submit a control transfer but
713 * XXX with the default timeout only.
717 if ((fd = _bus_open(transfer->dev_handle->dev->bus_number)) < 0)
718 return _errno_to_libusb(errno);
720 if ((ioctl(fd, USB_REQUEST, &req)) < 0) {
723 return _errno_to_libusb(err);
727 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
728 return _errno_to_libusb(errno);
730 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
731 return _errno_to_libusb(errno);
734 itransfer->transferred = req.ucr_actlen;
736 usbi_dbg("transferred %d", itransfer->transferred);
742 _access_endpoint(struct libusb_transfer *transfer)
744 struct handle_priv *hpriv;
745 struct device_priv *dpriv;
750 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
751 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
753 endpt = UE_GET_ADDR(transfer->endpoint);
754 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
756 usbi_dbg("endpoint %d mode %d", endpt, mode);
758 if (hpriv->endpoints[endpt] < 0) {
759 /* Pick the right endpoint node */
760 snprintf(devnode, sizeof(devnode), DEVPATH "%s.%02d",
761 dpriv->devname, endpt);
763 /* We may need to read/write to the same endpoint later. */
764 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
765 if ((fd = open(devnode, mode)) < 0)
768 hpriv->endpoints[endpt] = fd;
771 return (hpriv->endpoints[endpt]);
775 _sync_gen_transfer(struct usbi_transfer *itransfer)
777 struct libusb_transfer *transfer;
778 struct device_priv *dpriv;
781 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
782 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
784 if (dpriv->devname == NULL)
785 return (LIBUSB_ERROR_NOT_SUPPORTED);
788 * Bulk, Interrupt or Isochronous transfer depends on the
789 * endpoint and thus the node to open.
791 if ((fd = _access_endpoint(transfer)) < 0)
792 return _errno_to_libusb(errno);
794 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
795 return _errno_to_libusb(errno);
797 if (IS_XFERIN(transfer)) {
798 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
799 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
800 return _errno_to_libusb(errno);
802 nr = read(fd, transfer->buffer, transfer->length);
804 nr = write(fd, transfer->buffer, transfer->length);
808 return _errno_to_libusb(errno);
810 itransfer->transferred = nr;
816 _bus_open(int number)
820 snprintf(busnode, sizeof(busnode), USBDEV "%d", number);
822 return open(busnode, O_RDWR);