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
22 #include <sys/types.h>
31 #include <dev/usb/usb.h>
36 char *devname; /* name of the ugen(4) node */
37 int fd; /* device file descriptor */
39 unsigned char *cdesc; /* active config descriptor */
40 usb_device_descriptor_t ddesc; /* usb device descriptor */
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_transfer_completion(struct usbi_transfer *);
78 static int obsd_clock_gettime(int, struct timespec *);
83 static int _errno_to_libusb(int);
84 static int _cache_active_config_descriptor(struct libusb_device *);
85 static int _sync_control_transfer(struct usbi_transfer *);
86 static int _sync_gen_transfer(struct usbi_transfer *);
87 static int _access_endpoint(struct libusb_transfer *);
89 static int _bus_open(int);
92 const struct usbi_os_backend usbi_backend = {
93 "Synchronous OpenBSD backend",
97 NULL, /* set_option() */
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, /* alloc_streams */
119 NULL, /* free_streams */
121 NULL, /* dev_mem_alloc() */
122 NULL, /* dev_mem_free() */
124 NULL, /* kernel_driver_active() */
125 NULL, /* detach_kernel_driver() */
126 NULL, /* attach_kernel_driver() */
130 obsd_submit_transfer,
131 obsd_cancel_transfer,
132 obsd_clear_transfer_priv,
134 NULL, /* handle_events() */
135 obsd_handle_transfer_completion,
138 0, /* context_priv_size */
139 sizeof(struct device_priv),
140 sizeof(struct handle_priv),
141 0, /* transfer_priv_size */
144 #define DEVPATH "/dev/"
145 #define USBDEV DEVPATH "usb"
148 obsd_get_device_list(struct libusb_context * ctx,
149 struct discovered_devs **discdevs)
151 struct discovered_devs *ddd;
152 struct libusb_device *dev;
153 struct device_priv *dpriv;
154 struct usb_device_info di;
155 struct usb_device_ddesc dd;
156 unsigned long session_id;
157 char devices[USB_MAX_DEVICES];
164 for (i = 0; i < 8; i++) {
165 snprintf(busnode, sizeof(busnode), USBDEV "%d", i);
167 if ((fd = open(busnode, O_RDWR)) < 0) {
168 if (errno != ENOENT && errno != ENXIO)
169 usbi_err(ctx, "could not open %s", busnode);
173 bzero(devices, sizeof(devices));
174 for (addr = 1; addr < USB_MAX_DEVICES; addr++) {
179 if (ioctl(fd, USB_DEVICEINFO, &di) < 0)
183 * XXX If ugen(4) is attached to the USB device
187 for (j = 0; j < USB_MAX_DEVNAMES; j++)
188 if (!strncmp("ugen", di.udi_devnames[j], 4)) {
189 udevname = strdup(di.udi_devnames[j]);
193 session_id = (di.udi_bus << 8 | di.udi_addr);
194 dev = usbi_get_device_by_session_id(ctx, session_id);
197 dev = usbi_alloc_device(ctx, session_id);
200 return (LIBUSB_ERROR_NO_MEM);
203 dev->bus_number = di.udi_bus;
204 dev->device_address = di.udi_addr;
205 dev->speed = di.udi_speed;
207 dpriv = (struct device_priv *)dev->os_priv;
210 dpriv->devname = udevname;
212 dd.udd_bus = di.udi_bus;
213 dd.udd_addr = di.udi_addr;
214 if (ioctl(fd, USB_DEVICE_GET_DDESC, &dd) < 0) {
215 libusb_unref_device(dev);
218 dpriv->ddesc = dd.udd_desc;
220 if (_cache_active_config_descriptor(dev)) {
221 libusb_unref_device(dev);
225 if (usbi_sanitize_device(dev)) {
226 libusb_unref_device(dev);
231 ddd = discovered_devs_append(*discdevs, dev);
234 return (LIBUSB_ERROR_NO_MEM);
236 libusb_unref_device(dev);
245 return (LIBUSB_SUCCESS);
249 obsd_open(struct libusb_device_handle *handle)
251 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
252 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
255 if (dpriv->devname) {
257 * Only open ugen(4) attached devices read-write, all
258 * read-only operations are done through the bus node.
260 snprintf(devnode, sizeof(devnode), DEVPATH "%s.00",
262 dpriv->fd = open(devnode, O_RDWR);
264 return _errno_to_libusb(errno);
266 usbi_dbg("open %s: fd %d", devnode, dpriv->fd);
269 return (LIBUSB_SUCCESS);
273 obsd_close(struct libusb_device_handle *handle)
275 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
276 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
278 if (dpriv->devname) {
279 usbi_dbg("close: fd %d", dpriv->fd);
287 obsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
290 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
294 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
298 return (LIBUSB_SUCCESS);
302 obsd_get_active_config_descriptor(struct libusb_device *dev,
303 unsigned char *buf, size_t len, int *host_endian)
305 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
306 usb_config_descriptor_t *ucd = (usb_config_descriptor_t *)dpriv->cdesc;
308 len = MIN(len, UGETW(ucd->wTotalLength));
310 usbi_dbg("len %d", len);
312 memcpy(buf, dpriv->cdesc, len);
320 obsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
321 unsigned char *buf, size_t len, int *host_endian)
323 struct usb_device_fdesc udf;
326 if ((fd = _bus_open(dev->bus_number)) < 0)
327 return _errno_to_libusb(errno);
329 udf.udf_bus = dev->bus_number;
330 udf.udf_addr = dev->device_address;
331 udf.udf_config_index = idx;
335 usbi_dbg("index %d, len %d", udf.udf_config_index, len);
337 if (ioctl(fd, USB_DEVICE_GET_FDESC, &udf) < 0) {
340 return _errno_to_libusb(err);
350 obsd_get_configuration(struct libusb_device_handle *handle, int *config)
352 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
353 usb_config_descriptor_t *ucd = (usb_config_descriptor_t *)dpriv->cdesc;
355 *config = ucd->bConfigurationValue;
357 usbi_dbg("bConfigurationValue %d", *config);
359 return (LIBUSB_SUCCESS);
363 obsd_set_configuration(struct libusb_device_handle *handle, int config)
365 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
367 if (dpriv->devname == NULL)
368 return (LIBUSB_ERROR_NOT_SUPPORTED);
370 usbi_dbg("bConfigurationValue %d", config);
372 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
373 return _errno_to_libusb(errno);
375 return _cache_active_config_descriptor(handle->dev);
379 obsd_claim_interface(struct libusb_device_handle *handle, int iface)
381 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
384 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
385 hpriv->endpoints[i] = -1;
387 return (LIBUSB_SUCCESS);
391 obsd_release_interface(struct libusb_device_handle *handle, int iface)
393 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
396 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
397 if (hpriv->endpoints[i] >= 0)
398 close(hpriv->endpoints[i]);
400 return (LIBUSB_SUCCESS);
404 obsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
407 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
408 struct usb_alt_interface intf;
410 if (dpriv->devname == NULL)
411 return (LIBUSB_ERROR_NOT_SUPPORTED);
413 usbi_dbg("iface %d, setting %d", iface, altsetting);
415 memset(&intf, 0, sizeof(intf));
417 intf.uai_interface_index = iface;
418 intf.uai_alt_no = altsetting;
420 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
421 return _errno_to_libusb(errno);
423 return (LIBUSB_SUCCESS);
427 obsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
429 struct usb_ctl_request req;
432 if ((fd = _bus_open(handle->dev->bus_number)) < 0)
433 return _errno_to_libusb(errno);
437 req.ucr_addr = handle->dev->device_address;
438 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
439 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
440 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
441 USETW(req.ucr_request.wIndex, endpoint);
442 USETW(req.ucr_request.wLength, 0);
444 if (ioctl(fd, USB_REQUEST, &req) < 0) {
447 return _errno_to_libusb(err);
451 return (LIBUSB_SUCCESS);
455 obsd_reset_device(struct libusb_device_handle *handle)
459 return (LIBUSB_ERROR_NOT_SUPPORTED);
463 obsd_destroy_device(struct libusb_device *dev)
465 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
470 free(dpriv->devname);
474 obsd_submit_transfer(struct usbi_transfer *itransfer)
476 struct libusb_transfer *transfer;
477 struct handle_priv *hpriv;
482 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
483 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
485 switch (transfer->type) {
486 case LIBUSB_TRANSFER_TYPE_CONTROL:
487 err = _sync_control_transfer(itransfer);
489 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
490 if (IS_XFEROUT(transfer)) {
491 /* Isochronous write is not supported */
492 err = LIBUSB_ERROR_NOT_SUPPORTED;
495 err = _sync_gen_transfer(itransfer);
497 case LIBUSB_TRANSFER_TYPE_BULK:
498 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
499 if (IS_XFEROUT(transfer) &&
500 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
501 err = LIBUSB_ERROR_NOT_SUPPORTED;
504 err = _sync_gen_transfer(itransfer);
506 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
507 err = LIBUSB_ERROR_NOT_SUPPORTED;
514 usbi_signal_transfer_completion(itransfer);
516 return (LIBUSB_SUCCESS);
520 obsd_cancel_transfer(struct usbi_transfer *itransfer)
524 return (LIBUSB_ERROR_NOT_SUPPORTED);
528 obsd_clear_transfer_priv(struct usbi_transfer *itransfer)
536 obsd_handle_transfer_completion(struct usbi_transfer *itransfer)
538 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
542 obsd_clock_gettime(int clkid, struct timespec *tp)
544 usbi_dbg("clock %d", clkid);
546 if (clkid == USBI_CLOCK_REALTIME)
547 return clock_gettime(CLOCK_REALTIME, tp);
549 if (clkid == USBI_CLOCK_MONOTONIC)
550 return clock_gettime(CLOCK_MONOTONIC, tp);
552 return (LIBUSB_ERROR_INVALID_PARAM);
556 _errno_to_libusb(int err)
558 usbi_dbg("error: %s (%d)", strerror(err), err);
562 return (LIBUSB_ERROR_IO);
564 return (LIBUSB_ERROR_ACCESS);
566 return (LIBUSB_ERROR_NO_DEVICE);
568 return (LIBUSB_ERROR_NO_MEM);
570 return (LIBUSB_ERROR_TIMEOUT);
573 return (LIBUSB_ERROR_OTHER);
577 _cache_active_config_descriptor(struct libusb_device *dev)
579 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
580 struct usb_device_cdesc udc;
581 struct usb_device_fdesc udf;
585 if ((fd = _bus_open(dev->bus_number)) < 0)
586 return _errno_to_libusb(errno);
588 usbi_dbg("fd %d, addr %d", fd, dev->device_address);
590 udc.udc_bus = dev->bus_number;
591 udc.udc_addr = dev->device_address;
592 udc.udc_config_index = USB_CURRENT_CONFIG_INDEX;
593 if (ioctl(fd, USB_DEVICE_GET_CDESC, &udc) < 0) {
596 return _errno_to_libusb(errno);
599 usbi_dbg("active bLength %d", udc.udc_desc.bLength);
601 len = UGETW(udc.udc_desc.wTotalLength);
604 return (LIBUSB_ERROR_NO_MEM);
606 udf.udf_bus = dev->bus_number;
607 udf.udf_addr = dev->device_address;
608 udf.udf_config_index = udc.udc_config_index;
612 usbi_dbg("index %d, len %d", udf.udf_config_index, len);
614 if (ioctl(fd, USB_DEVICE_GET_FDESC, &udf) < 0) {
618 return _errno_to_libusb(err);
626 return (LIBUSB_SUCCESS);
630 _sync_control_transfer(struct usbi_transfer *itransfer)
632 struct libusb_transfer *transfer;
633 struct libusb_control_setup *setup;
634 struct device_priv *dpriv;
635 struct usb_ctl_request req;
637 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
638 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
639 setup = (struct libusb_control_setup *)transfer->buffer;
641 usbi_dbg("type %x request %x value %x index %d length %d timeout %d",
642 setup->bmRequestType, setup->bRequest,
643 libusb_le16_to_cpu(setup->wValue),
644 libusb_le16_to_cpu(setup->wIndex),
645 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
647 req.ucr_addr = transfer->dev_handle->dev->device_address;
648 req.ucr_request.bmRequestType = setup->bmRequestType;
649 req.ucr_request.bRequest = setup->bRequest;
650 /* Don't use USETW, libusb already deals with the endianness */
651 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
652 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
653 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
654 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
656 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
657 req.ucr_flags = USBD_SHORT_XFER_OK;
659 if (dpriv->devname == NULL) {
661 * XXX If the device is not attached to ugen(4) it is
662 * XXX still possible to submit a control transfer but
663 * XXX with the default timeout only.
667 if ((fd = _bus_open(transfer->dev_handle->dev->bus_number)) < 0)
668 return _errno_to_libusb(errno);
670 if ((ioctl(fd, USB_REQUEST, &req)) < 0) {
673 return _errno_to_libusb(err);
677 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
678 return _errno_to_libusb(errno);
680 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
681 return _errno_to_libusb(errno);
684 itransfer->transferred = req.ucr_actlen;
686 usbi_dbg("transferred %d", itransfer->transferred);
692 _access_endpoint(struct libusb_transfer *transfer)
694 struct handle_priv *hpriv;
695 struct device_priv *dpriv;
700 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
701 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
703 endpt = UE_GET_ADDR(transfer->endpoint);
704 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
706 usbi_dbg("endpoint %d mode %d", endpt, mode);
708 if (hpriv->endpoints[endpt] < 0) {
709 /* Pick the right endpoint node */
710 snprintf(devnode, sizeof(devnode), DEVPATH "%s.%02d",
711 dpriv->devname, endpt);
713 /* We may need to read/write to the same endpoint later. */
714 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
715 if ((fd = open(devnode, mode)) < 0)
718 hpriv->endpoints[endpt] = fd;
721 return (hpriv->endpoints[endpt]);
725 _sync_gen_transfer(struct usbi_transfer *itransfer)
727 struct libusb_transfer *transfer;
728 struct device_priv *dpriv;
731 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
732 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
734 if (dpriv->devname == NULL)
735 return (LIBUSB_ERROR_NOT_SUPPORTED);
738 * Bulk, Interrupt or Isochronous transfer depends on the
739 * endpoint and thus the node to open.
741 if ((fd = _access_endpoint(transfer)) < 0)
742 return _errno_to_libusb(errno);
744 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
745 return _errno_to_libusb(errno);
747 if (IS_XFERIN(transfer)) {
748 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
749 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
750 return _errno_to_libusb(errno);
752 nr = read(fd, transfer->buffer, transfer->length);
754 nr = write(fd, transfer->buffer, transfer->length);
758 return _errno_to_libusb(errno);
760 itransfer->transferred = nr;
766 _bus_open(int number)
770 snprintf(busnode, sizeof(busnode), USBDEV "%d", number);
772 return open(busnode, O_RDWR);