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 openbsd_backend = {
93 "Synchronous OpenBSD backend",
98 NULL, /* hotplug_poll */
102 obsd_get_device_descriptor,
103 obsd_get_active_config_descriptor,
104 obsd_get_config_descriptor,
105 NULL, /* get_config_descriptor_by_value() */
107 obsd_get_configuration,
108 obsd_set_configuration,
110 obsd_claim_interface,
111 obsd_release_interface,
113 obsd_set_interface_altsetting,
117 NULL, /* alloc_streams */
118 NULL, /* free_streams */
120 NULL, /* kernel_driver_active() */
121 NULL, /* detach_kernel_driver() */
122 NULL, /* attach_kernel_driver() */
126 obsd_submit_transfer,
127 obsd_cancel_transfer,
128 obsd_clear_transfer_priv,
130 NULL, /* handle_events() */
131 obsd_handle_transfer_completion,
134 sizeof(struct device_priv),
135 sizeof(struct handle_priv),
136 0, /* transfer_priv_size */
139 #define DEVPATH "/dev/"
140 #define USBDEV DEVPATH "usb"
143 obsd_get_device_list(struct libusb_context * ctx,
144 struct discovered_devs **discdevs)
146 struct discovered_devs *ddd;
147 struct libusb_device *dev;
148 struct device_priv *dpriv;
149 struct usb_device_info di;
150 struct usb_device_ddesc dd;
151 unsigned long session_id;
152 char devices[USB_MAX_DEVICES];
159 for (i = 0; i < 8; i++) {
160 snprintf(busnode, sizeof(busnode), USBDEV "%d", i);
162 if ((fd = open(busnode, O_RDWR)) < 0) {
163 if (errno != ENOENT && errno != ENXIO)
164 usbi_err(ctx, "could not open %s", busnode);
168 bzero(devices, sizeof(devices));
169 for (addr = 1; addr < USB_MAX_DEVICES; addr++) {
174 if (ioctl(fd, USB_DEVICEINFO, &di) < 0)
178 * XXX If ugen(4) is attached to the USB device
182 for (j = 0; j < USB_MAX_DEVNAMES; j++)
183 if (!strncmp("ugen", di.udi_devnames[j], 4)) {
184 udevname = strdup(di.udi_devnames[j]);
188 session_id = (di.udi_bus << 8 | di.udi_addr);
189 dev = usbi_get_device_by_session_id(ctx, session_id);
192 dev = usbi_alloc_device(ctx, session_id);
195 return (LIBUSB_ERROR_NO_MEM);
198 dev->bus_number = di.udi_bus;
199 dev->device_address = di.udi_addr;
200 dev->speed = di.udi_speed;
202 dpriv = (struct device_priv *)dev->os_priv;
205 dpriv->devname = udevname;
207 dd.udd_bus = di.udi_bus;
208 dd.udd_addr = di.udi_addr;
209 if (ioctl(fd, USB_DEVICE_GET_DDESC, &dd) < 0) {
210 libusb_unref_device(dev);
213 dpriv->ddesc = dd.udd_desc;
215 if (_cache_active_config_descriptor(dev)) {
216 libusb_unref_device(dev);
220 if (usbi_sanitize_device(dev)) {
221 libusb_unref_device(dev);
226 ddd = discovered_devs_append(*discdevs, dev);
229 return (LIBUSB_ERROR_NO_MEM);
231 libusb_unref_device(dev);
240 return (LIBUSB_SUCCESS);
244 obsd_open(struct libusb_device_handle *handle)
246 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
247 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
250 if (dpriv->devname) {
252 * Only open ugen(4) attached devices read-write, all
253 * read-only operations are done through the bus node.
255 snprintf(devnode, sizeof(devnode), DEVPATH "%s.00",
257 dpriv->fd = open(devnode, O_RDWR);
259 return _errno_to_libusb(errno);
261 usbi_dbg("open %s: fd %d", devnode, dpriv->fd);
264 return (LIBUSB_SUCCESS);
268 obsd_close(struct libusb_device_handle *handle)
270 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
271 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
273 if (dpriv->devname) {
274 usbi_dbg("close: fd %d", dpriv->fd);
282 obsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
285 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
289 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
293 return (LIBUSB_SUCCESS);
297 obsd_get_active_config_descriptor(struct libusb_device *dev,
298 unsigned char *buf, size_t len, int *host_endian)
300 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
301 usb_config_descriptor_t *ucd = (usb_config_descriptor_t *)dpriv->cdesc;
303 len = MIN(len, UGETW(ucd->wTotalLength));
305 usbi_dbg("len %d", len);
307 memcpy(buf, dpriv->cdesc, len);
315 obsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
316 unsigned char *buf, size_t len, int *host_endian)
318 struct usb_device_fdesc udf;
321 if ((fd = _bus_open(dev->bus_number)) < 0)
322 return _errno_to_libusb(errno);
324 udf.udf_bus = dev->bus_number;
325 udf.udf_addr = dev->device_address;
326 udf.udf_config_index = idx;
330 usbi_dbg("index %d, len %d", udf.udf_config_index, len);
332 if (ioctl(fd, USB_DEVICE_GET_FDESC, &udf) < 0) {
335 return _errno_to_libusb(err);
345 obsd_get_configuration(struct libusb_device_handle *handle, int *config)
347 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
348 usb_config_descriptor_t *ucd = (usb_config_descriptor_t *)dpriv->cdesc;
350 *config = ucd->bConfigurationValue;
352 usbi_dbg("bConfigurationValue %d", *config);
354 return (LIBUSB_SUCCESS);
358 obsd_set_configuration(struct libusb_device_handle *handle, int config)
360 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
362 if (dpriv->devname == NULL)
363 return (LIBUSB_ERROR_NOT_SUPPORTED);
365 usbi_dbg("bConfigurationValue %d", config);
367 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
368 return _errno_to_libusb(errno);
370 return _cache_active_config_descriptor(handle->dev);
374 obsd_claim_interface(struct libusb_device_handle *handle, int iface)
376 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
379 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
380 hpriv->endpoints[i] = -1;
382 return (LIBUSB_SUCCESS);
386 obsd_release_interface(struct libusb_device_handle *handle, int iface)
388 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
391 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
392 if (hpriv->endpoints[i] >= 0)
393 close(hpriv->endpoints[i]);
395 return (LIBUSB_SUCCESS);
399 obsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
402 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
403 struct usb_alt_interface intf;
405 if (dpriv->devname == NULL)
406 return (LIBUSB_ERROR_NOT_SUPPORTED);
408 usbi_dbg("iface %d, setting %d", iface, altsetting);
410 memset(&intf, 0, sizeof(intf));
412 intf.uai_interface_index = iface;
413 intf.uai_alt_no = altsetting;
415 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
416 return _errno_to_libusb(errno);
418 return (LIBUSB_SUCCESS);
422 obsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
424 struct usb_ctl_request req;
427 if ((fd = _bus_open(handle->dev->bus_number)) < 0)
428 return _errno_to_libusb(errno);
432 req.ucr_addr = handle->dev->device_address;
433 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
434 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
435 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
436 USETW(req.ucr_request.wIndex, endpoint);
437 USETW(req.ucr_request.wLength, 0);
439 if (ioctl(fd, USB_REQUEST, &req) < 0) {
442 return _errno_to_libusb(err);
446 return (LIBUSB_SUCCESS);
450 obsd_reset_device(struct libusb_device_handle *handle)
454 return (LIBUSB_ERROR_NOT_SUPPORTED);
458 obsd_destroy_device(struct libusb_device *dev)
460 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
465 free(dpriv->devname);
469 obsd_submit_transfer(struct usbi_transfer *itransfer)
471 struct libusb_transfer *transfer;
472 struct handle_priv *hpriv;
477 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
478 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
480 switch (transfer->type) {
481 case LIBUSB_TRANSFER_TYPE_CONTROL:
482 err = _sync_control_transfer(itransfer);
484 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
485 if (IS_XFEROUT(transfer)) {
486 /* Isochronous write is not supported */
487 err = LIBUSB_ERROR_NOT_SUPPORTED;
490 err = _sync_gen_transfer(itransfer);
492 case LIBUSB_TRANSFER_TYPE_BULK:
493 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
494 if (IS_XFEROUT(transfer) &&
495 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
496 err = LIBUSB_ERROR_NOT_SUPPORTED;
499 err = _sync_gen_transfer(itransfer);
501 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
502 err = LIBUSB_ERROR_NOT_SUPPORTED;
509 usbi_signal_transfer_completion(itransfer);
511 return (LIBUSB_SUCCESS);
515 obsd_cancel_transfer(struct usbi_transfer *itransfer)
519 return (LIBUSB_ERROR_NOT_SUPPORTED);
523 obsd_clear_transfer_priv(struct usbi_transfer *itransfer)
531 obsd_handle_transfer_completion(struct usbi_transfer *itransfer)
533 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
537 obsd_clock_gettime(int clkid, struct timespec *tp)
539 usbi_dbg("clock %d", clkid);
541 if (clkid == USBI_CLOCK_REALTIME)
542 return clock_gettime(CLOCK_REALTIME, tp);
544 if (clkid == USBI_CLOCK_MONOTONIC)
545 return clock_gettime(CLOCK_MONOTONIC, tp);
547 return (LIBUSB_ERROR_INVALID_PARAM);
551 _errno_to_libusb(int err)
553 usbi_dbg("error: %s (%d)", strerror(err), err);
557 return (LIBUSB_ERROR_IO);
559 return (LIBUSB_ERROR_ACCESS);
561 return (LIBUSB_ERROR_NO_DEVICE);
563 return (LIBUSB_ERROR_NO_MEM);
565 return (LIBUSB_ERROR_TIMEOUT);
568 return (LIBUSB_ERROR_OTHER);
572 _cache_active_config_descriptor(struct libusb_device *dev)
574 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
575 struct usb_device_cdesc udc;
576 struct usb_device_fdesc udf;
580 if ((fd = _bus_open(dev->bus_number)) < 0)
581 return _errno_to_libusb(errno);
583 usbi_dbg("fd %d, addr %d", fd, dev->device_address);
585 udc.udc_bus = dev->bus_number;
586 udc.udc_addr = dev->device_address;
587 udc.udc_config_index = USB_CURRENT_CONFIG_INDEX;
588 if (ioctl(fd, USB_DEVICE_GET_CDESC, &udc) < 0) {
591 return _errno_to_libusb(errno);
594 usbi_dbg("active bLength %d", udc.udc_desc.bLength);
596 len = UGETW(udc.udc_desc.wTotalLength);
599 return (LIBUSB_ERROR_NO_MEM);
601 udf.udf_bus = dev->bus_number;
602 udf.udf_addr = dev->device_address;
603 udf.udf_config_index = udc.udc_config_index;
607 usbi_dbg("index %d, len %d", udf.udf_config_index, len);
609 if (ioctl(fd, USB_DEVICE_GET_FDESC, &udf) < 0) {
613 return _errno_to_libusb(err);
621 return (LIBUSB_SUCCESS);
625 _sync_control_transfer(struct usbi_transfer *itransfer)
627 struct libusb_transfer *transfer;
628 struct libusb_control_setup *setup;
629 struct device_priv *dpriv;
630 struct usb_ctl_request req;
632 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
633 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
634 setup = (struct libusb_control_setup *)transfer->buffer;
636 usbi_dbg("type %x request %x value %x index %d length %d timeout %d",
637 setup->bmRequestType, setup->bRequest,
638 libusb_le16_to_cpu(setup->wValue),
639 libusb_le16_to_cpu(setup->wIndex),
640 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
642 req.ucr_addr = transfer->dev_handle->dev->device_address;
643 req.ucr_request.bmRequestType = setup->bmRequestType;
644 req.ucr_request.bRequest = setup->bRequest;
645 /* Don't use USETW, libusb already deals with the endianness */
646 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
647 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
648 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
649 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
651 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
652 req.ucr_flags = USBD_SHORT_XFER_OK;
654 if (dpriv->devname == NULL) {
656 * XXX If the device is not attached to ugen(4) it is
657 * XXX still possible to submit a control transfer but
658 * XXX with the default timeout only.
662 if ((fd = _bus_open(transfer->dev_handle->dev->bus_number)) < 0)
663 return _errno_to_libusb(errno);
665 if ((ioctl(fd, USB_REQUEST, &req)) < 0) {
668 return _errno_to_libusb(err);
672 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
673 return _errno_to_libusb(errno);
675 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
676 return _errno_to_libusb(errno);
679 itransfer->transferred = req.ucr_actlen;
681 usbi_dbg("transferred %d", itransfer->transferred);
687 _access_endpoint(struct libusb_transfer *transfer)
689 struct handle_priv *hpriv;
690 struct device_priv *dpriv;
695 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
696 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
698 endpt = UE_GET_ADDR(transfer->endpoint);
699 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
701 usbi_dbg("endpoint %d mode %d", endpt, mode);
703 if (hpriv->endpoints[endpt] < 0) {
704 /* Pick the right endpoint node */
705 snprintf(devnode, sizeof(devnode), DEVPATH "%s.%02d",
706 dpriv->devname, endpt);
708 /* We may need to read/write to the same endpoint later. */
709 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
710 if ((fd = open(devnode, mode)) < 0)
713 hpriv->endpoints[endpt] = fd;
716 return (hpriv->endpoints[endpt]);
720 _sync_gen_transfer(struct usbi_transfer *itransfer)
722 struct libusb_transfer *transfer;
723 struct device_priv *dpriv;
726 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
727 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
729 if (dpriv->devname == NULL)
730 return (LIBUSB_ERROR_NOT_SUPPORTED);
733 * Bulk, Interrupt or Isochronous transfer depends on the
734 * endpoint and thus the node to open.
736 if ((fd = _access_endpoint(transfer)) < 0)
737 return _errno_to_libusb(errno);
739 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
740 return _errno_to_libusb(errno);
742 if (IS_XFERIN(transfer)) {
743 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
744 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
745 return _errno_to_libusb(errno);
747 nr = read(fd, transfer->buffer, transfer->length);
749 nr = write(fd, transfer->buffer, transfer->length);
753 return _errno_to_libusb(errno);
755 itransfer->transferred = nr;
761 _bus_open(int number)
765 snprintf(busnode, sizeof(busnode), USBDEV "%d", number);
767 return open(busnode, O_RDWR);