2 * Copyright (c) 2011 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>
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 *, int);
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 const struct usbi_os_backend openbsd_backend = {
91 "Synchronous OpenBSD backend",
98 obsd_get_device_descriptor,
99 obsd_get_active_config_descriptor,
100 obsd_get_config_descriptor,
102 obsd_get_configuration,
103 obsd_set_configuration,
105 obsd_claim_interface,
106 obsd_release_interface,
108 obsd_set_interface_altsetting,
112 NULL, /* kernel_driver_active() */
113 NULL, /* detach_kernel_driver() */
114 NULL, /* attach_kernel_driver() */
118 obsd_submit_transfer,
119 obsd_cancel_transfer,
120 obsd_clear_transfer_priv,
125 sizeof(struct device_priv),
126 sizeof(struct handle_priv),
127 0, /* transfer_priv_size */
128 0, /* add_iso_packet_size */
132 obsd_get_device_list(struct libusb_context * ctx,
133 struct discovered_devs **discdevs)
135 struct libusb_device *dev;
136 struct device_priv *dpriv;
137 struct usb_device_info di;
138 unsigned long session_id;
144 /* Only ugen(4) is supported */
145 for (i = 0; i < USB_MAX_DEVICES; i++) {
146 /* Control endpoint is always .00 */
147 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
149 if ((fd = open(devnode, O_RDONLY)) < 0) {
150 if (errno != ENOENT && errno != ENXIO)
151 usbi_err(ctx, "could not open %s", devnode);
155 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
158 session_id = (di.udi_bus << 8 | di.udi_addr);
159 dev = usbi_get_device_by_session_id(ctx, session_id);
162 dev = usbi_alloc_device(ctx, session_id);
164 return (LIBUSB_ERROR_NO_MEM);
166 dev->bus_number = di.udi_bus;
167 dev->device_address = di.udi_addr;
169 dpriv = (struct device_priv *)dev->os_priv;
170 strlcpy(dpriv->devnode, devnode, sizeof(devnode));
173 if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
179 if (_cache_active_config_descriptor(dev, fd)) {
184 if ((err = usbi_sanitize_device(dev)))
189 if (discovered_devs_append(*discdevs, dev) == NULL)
190 return (LIBUSB_ERROR_NO_MEM);
193 return (LIBUSB_SUCCESS);
197 libusb_unref_device(dev);
198 return _errno_to_libusb(err);
202 obsd_open(struct libusb_device_handle *handle)
204 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
205 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
207 dpriv->fd = open(dpriv->devnode, O_RDWR);
209 dpriv->fd = open(dpriv->devnode, O_RDONLY);
211 return _errno_to_libusb(errno);
214 usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
216 if (pipe(hpriv->pipe) < 0)
217 return _errno_to_libusb(errno);
219 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
223 obsd_close(struct libusb_device_handle *handle)
225 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
226 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
228 usbi_dbg("close: fd %d", dpriv->fd);
233 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
235 close(hpriv->pipe[0]);
236 close(hpriv->pipe[1]);
240 obsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
243 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
247 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
251 return (LIBUSB_SUCCESS);
255 obsd_get_active_config_descriptor(struct libusb_device *dev,
256 unsigned char *buf, size_t len, int *host_endian)
258 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
259 usb_config_descriptor_t *ucd;
261 ucd = (usb_config_descriptor_t *) dpriv->cdesc;
262 len = MIN(len, UGETW(ucd->wTotalLength));
264 usbi_dbg("len %d", len);
266 memcpy(buf, dpriv->cdesc, len);
270 return (LIBUSB_SUCCESS);
274 obsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
275 unsigned char *buf, size_t len, int *host_endian)
277 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
278 struct usb_full_desc ufd;
281 usbi_dbg("index %d, len %d", idx, len);
283 /* A config descriptor may be requested before opening the device */
284 if (dpriv->fd >= 0) {
287 fd = open(dpriv->devnode, O_RDONLY);
289 return _errno_to_libusb(errno);
292 ufd.ufd_config_index = idx;
296 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
300 return _errno_to_libusb(err);
308 return (LIBUSB_SUCCESS);
312 obsd_get_configuration(struct libusb_device_handle *handle, int *config)
314 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
318 if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
319 return _errno_to_libusb(errno);
321 usbi_dbg("configuration %d", *config);
323 return (LIBUSB_SUCCESS);
327 obsd_set_configuration(struct libusb_device_handle *handle, int config)
329 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
331 usbi_dbg("configuration %d", config);
333 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
334 return _errno_to_libusb(errno);
336 return _cache_active_config_descriptor(handle->dev, dpriv->fd);
340 obsd_claim_interface(struct libusb_device_handle *handle, int iface)
342 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
345 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
346 hpriv->endpoints[i] = -1;
348 return (LIBUSB_SUCCESS);
352 obsd_release_interface(struct libusb_device_handle *handle, int iface)
354 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
357 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
358 if (hpriv->endpoints[i] >= 0)
359 close(hpriv->endpoints[i]);
361 return (LIBUSB_SUCCESS);
365 obsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
368 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
369 struct usb_alt_interface intf;
371 usbi_dbg("iface %d, setting %d", iface, altsetting);
373 memset(&intf, 0, sizeof(intf));
375 intf.uai_interface_index = iface;
376 intf.uai_alt_no = altsetting;
378 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
379 return _errno_to_libusb(errno);
381 return (LIBUSB_SUCCESS);
385 obsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
387 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
388 struct usb_ctl_request req;
392 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
393 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
394 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
395 USETW(req.ucr_request.wIndex, endpoint);
396 USETW(req.ucr_request.wLength, 0);
398 if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
399 return _errno_to_libusb(errno);
401 return (LIBUSB_SUCCESS);
405 obsd_reset_device(struct libusb_device_handle *handle)
409 return (LIBUSB_ERROR_NOT_SUPPORTED);
413 obsd_destroy_device(struct libusb_device *dev)
415 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
423 obsd_submit_transfer(struct usbi_transfer *itransfer)
425 struct libusb_transfer *transfer;
426 struct handle_priv *hpriv;
431 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
432 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
434 switch (transfer->type) {
435 case LIBUSB_TRANSFER_TYPE_CONTROL:
436 err = _sync_control_transfer(itransfer);
438 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
439 if (transfer->endpoint & LIBUSB_ENDPOINT_OUT) {
440 /* Isochronous write is not supported */
441 err = LIBUSB_ERROR_NOT_SUPPORTED;
445 case LIBUSB_TRANSFER_TYPE_BULK:
446 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
447 err = _sync_gen_transfer(itransfer);
454 if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
455 return _errno_to_libusb(errno);
457 return (LIBUSB_SUCCESS);
461 obsd_cancel_transfer(struct usbi_transfer *itransfer)
465 return (LIBUSB_ERROR_NOT_SUPPORTED);
469 obsd_clear_transfer_priv(struct usbi_transfer *itransfer)
477 obsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
480 struct libusb_device_handle *handle;
481 struct handle_priv *hpriv;
482 struct usbi_transfer *itransfer;
483 struct pollfd *pollfd;
488 pthread_mutex_lock(&ctx->open_devs_lock);
489 for (i = 0; i < nfds && num_ready > 0; i++) {
492 if (!pollfd->revents)
496 list_for_each_entry(handle, &ctx->open_devs, list,
497 struct libusb_device_handle) {
498 hpriv = (struct handle_priv *)handle->os_priv;
500 if (hpriv->pipe[0] == pollfd->fd)
504 if (pollfd->revents & POLLERR) {
505 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
506 usbi_handle_disconnect(handle);
510 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
515 if ((err = usbi_handle_transfer_completion(itransfer,
516 LIBUSB_TRANSFER_COMPLETED)))
519 pthread_mutex_unlock(&ctx->open_devs_lock);
522 return _errno_to_libusb(err);
524 return (LIBUSB_SUCCESS);
528 obsd_clock_gettime(int clkid, struct timespec *tp)
530 usbi_dbg("clock %d", clkid);
532 if (clkid == USBI_CLOCK_REALTIME)
533 return clock_gettime(CLOCK_REALTIME, tp);
535 if (clkid == USBI_CLOCK_MONOTONIC)
536 return clock_gettime(CLOCK_MONOTONIC, tp);
538 return (LIBUSB_ERROR_INVALID_PARAM);
542 _errno_to_libusb(int err)
546 return (LIBUSB_ERROR_IO);
548 return (LIBUSB_ERROR_ACCESS);
550 return (LIBUSB_ERROR_NO_DEVICE);
552 return (LIBUSB_ERROR_NO_MEM);
555 usbi_dbg("error: %s", strerror(err));
557 return (LIBUSB_ERROR_OTHER);
561 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
563 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
564 struct usb_config_desc ucd;
565 struct usb_full_desc ufd;
569 usbi_dbg("fd %d", fd);
571 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
573 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
574 return _errno_to_libusb(errno);
576 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
578 len = UGETW(ucd.ucd_desc.wTotalLength);
581 return (LIBUSB_ERROR_NO_MEM);
583 ufd.ufd_config_index = ucd.ucd_config_index;
587 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
589 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
591 return _errno_to_libusb(errno);
602 _sync_control_transfer(struct usbi_transfer *itransfer)
604 struct libusb_transfer *transfer;
605 struct libusb_control_setup *setup;
606 struct device_priv *dpriv;
607 struct usb_ctl_request req;
609 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
610 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
611 setup = (struct libusb_control_setup *)transfer->buffer;
613 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
614 setup->bmRequestType, setup->bRequest,
615 libusb_le16_to_cpu(setup->wValue),
616 libusb_le16_to_cpu(setup->wIndex),
617 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
619 req.ucr_request.bmRequestType = setup->bmRequestType;
620 req.ucr_request.bRequest = setup->bRequest;
621 /* Don't use USETW, libusb already deals with the endianness */
622 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
623 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
624 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
625 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
627 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
628 req.ucr_flags = USBD_SHORT_XFER_OK;
630 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
631 return _errno_to_libusb(errno);
633 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
634 return _errno_to_libusb(errno);
636 itransfer->transferred = req.ucr_actlen;
638 usbi_dbg("transferred %d", itransfer->transferred);
644 _access_endpoint(struct libusb_transfer *transfer)
646 struct handle_priv *hpriv;
647 struct device_priv *dpriv;
648 char *s, devnode[16];
652 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
653 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
655 endpt = UE_GET_ADDR(transfer->endpoint);
656 mode = (transfer->endpoint & LIBUSB_ENDPOINT_IN) ? O_RDONLY : O_WRONLY;
658 usbi_dbg("endpoint %d mode %d", endpt, mode);
660 if (hpriv->endpoints[endpt] < 0) {
661 /* Pick the right node given the control one */
662 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
663 s = strchr(devnode, '.');
664 snprintf(s, 4, ".%02d", endpt);
666 /* We may need to read/write to the same endpoint later. */
667 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
668 if ((fd = open(devnode, mode)) < 0)
671 hpriv->endpoints[endpt] = fd;
674 return (hpriv->endpoints[endpt]);
678 _sync_gen_transfer(struct usbi_transfer *itransfer)
680 struct libusb_transfer *transfer;
683 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
686 * Bulk, Interrupt or Isochronous transfer depends on the
687 * endpoint and thus the node to open.
689 if ((fd = _access_endpoint(transfer)) < 0)
690 return _errno_to_libusb(errno);
692 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
693 return _errno_to_libusb(errno);
695 if (transfer->endpoint & LIBUSB_ENDPOINT_IN) {
696 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
697 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
698 return _errno_to_libusb(errno);
700 nr = read(fd, transfer->buffer, transfer->length);
702 nr = write(fd, transfer->buffer, transfer->length);
706 return _errno_to_libusb(errno);
708 itransfer->transferred = nr;