2 * Copyright © 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 netbsd_get_device_list(struct libusb_context *,
51 struct discovered_devs **);
52 static int netbsd_open(struct libusb_device_handle *);
53 static void netbsd_close(struct libusb_device_handle *);
55 static int netbsd_get_device_descriptor(struct libusb_device *, unsigned char *,
57 static int netbsd_get_active_config_descriptor(struct libusb_device *,
58 unsigned char *, size_t, int *);
59 static int netbsd_get_config_descriptor(struct libusb_device *, uint8_t,
60 unsigned char *, size_t, int *);
62 static int netbsd_get_configuration(struct libusb_device_handle *, int *);
63 static int netbsd_set_configuration(struct libusb_device_handle *, int);
65 static int netbsd_claim_interface(struct libusb_device_handle *, int);
66 static int netbsd_release_interface(struct libusb_device_handle *, int);
68 static int netbsd_set_interface_altsetting(struct libusb_device_handle *, int,
70 static int netbsd_clear_halt(struct libusb_device_handle *, unsigned char);
71 static int netbsd_reset_device(struct libusb_device_handle *);
72 static void netbsd_destroy_device(struct libusb_device *);
74 static int netbsd_submit_transfer(struct usbi_transfer *);
75 static int netbsd_cancel_transfer(struct usbi_transfer *);
76 static void netbsd_clear_transfer_priv(struct usbi_transfer *);
77 static int netbsd_handle_events(struct libusb_context *ctx, struct pollfd *,
79 static int netbsd_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 netbsd_backend = {
91 "Synchronous NetBSD backend",
95 netbsd_get_device_list,
96 NULL, /* hotplug_poll */
100 netbsd_get_device_descriptor,
101 netbsd_get_active_config_descriptor,
102 netbsd_get_config_descriptor,
103 NULL, /* get_config_descriptor_by_value() */
105 netbsd_get_configuration,
106 netbsd_set_configuration,
108 netbsd_claim_interface,
109 netbsd_release_interface,
111 netbsd_set_interface_altsetting,
115 NULL, /* alloc_streams */
116 NULL, /* free_streams */
118 NULL, /* kernel_driver_active() */
119 NULL, /* detach_kernel_driver() */
120 NULL, /* attach_kernel_driver() */
122 netbsd_destroy_device,
124 netbsd_submit_transfer,
125 netbsd_cancel_transfer,
126 netbsd_clear_transfer_priv,
128 netbsd_handle_events,
130 netbsd_clock_gettime,
131 sizeof(struct device_priv),
132 sizeof(struct handle_priv),
133 0, /* transfer_priv_size */
134 0, /* add_iso_packet_size */
138 netbsd_get_device_list(struct libusb_context * ctx,
139 struct discovered_devs **discdevs)
141 struct libusb_device *dev;
142 struct device_priv *dpriv;
143 struct usb_device_info di;
144 unsigned long session_id;
150 /* Only ugen(4) is supported */
151 for (i = 0; i < USB_MAX_DEVICES; i++) {
152 /* Control endpoint is always .00 */
153 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
155 if ((fd = open(devnode, O_RDONLY)) < 0) {
156 if (errno != ENOENT && errno != ENXIO)
157 usbi_err(ctx, "could not open %s", devnode);
161 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
164 session_id = (di.udi_bus << 8 | di.udi_addr);
165 dev = usbi_get_device_by_session_id(ctx, session_id);
168 dev = usbi_alloc_device(ctx, session_id);
170 return (LIBUSB_ERROR_NO_MEM);
172 dev->bus_number = di.udi_bus;
173 dev->device_address = di.udi_addr;
174 dev->speed = di.udi_speed;
176 dpriv = (struct device_priv *)dev->os_priv;
177 strlcpy(dpriv->devnode, devnode, sizeof(devnode));
180 if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
186 if (_cache_active_config_descriptor(dev, fd)) {
191 if ((err = usbi_sanitize_device(dev)))
196 if (discovered_devs_append(*discdevs, dev) == NULL)
197 return (LIBUSB_ERROR_NO_MEM);
199 libusb_unref_device(dev);
202 return (LIBUSB_SUCCESS);
206 libusb_unref_device(dev);
207 return _errno_to_libusb(err);
211 netbsd_open(struct libusb_device_handle *handle)
213 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
214 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
216 dpriv->fd = open(dpriv->devnode, O_RDWR);
218 dpriv->fd = open(dpriv->devnode, O_RDONLY);
220 return _errno_to_libusb(errno);
223 usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
225 if (pipe(hpriv->pipe) < 0)
226 return _errno_to_libusb(errno);
228 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
232 netbsd_close(struct libusb_device_handle *handle)
234 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
235 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
237 usbi_dbg("close: fd %d", dpriv->fd);
242 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
244 close(hpriv->pipe[0]);
245 close(hpriv->pipe[1]);
249 netbsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
252 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
256 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
260 return (LIBUSB_SUCCESS);
264 netbsd_get_active_config_descriptor(struct libusb_device *dev,
265 unsigned char *buf, size_t len, int *host_endian)
267 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
268 usb_config_descriptor_t *ucd;
270 ucd = (usb_config_descriptor_t *) dpriv->cdesc;
271 len = MIN(len, UGETW(ucd->wTotalLength));
273 usbi_dbg("len %d", len);
275 memcpy(buf, dpriv->cdesc, len);
283 netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
284 unsigned char *buf, size_t len, int *host_endian)
286 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
287 struct usb_full_desc ufd;
290 usbi_dbg("index %d, len %d", idx, len);
292 /* A config descriptor may be requested before opening the device */
293 if (dpriv->fd >= 0) {
296 fd = open(dpriv->devnode, O_RDONLY);
298 return _errno_to_libusb(errno);
301 ufd.ufd_config_index = idx;
305 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
309 return _errno_to_libusb(err);
321 netbsd_get_configuration(struct libusb_device_handle *handle, int *config)
323 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
327 if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
328 return _errno_to_libusb(errno);
330 usbi_dbg("configuration %d", *config);
332 return (LIBUSB_SUCCESS);
336 netbsd_set_configuration(struct libusb_device_handle *handle, int config)
338 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
340 usbi_dbg("configuration %d", config);
342 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
343 return _errno_to_libusb(errno);
345 return _cache_active_config_descriptor(handle->dev, dpriv->fd);
349 netbsd_claim_interface(struct libusb_device_handle *handle, int iface)
351 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
354 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
355 hpriv->endpoints[i] = -1;
357 return (LIBUSB_SUCCESS);
361 netbsd_release_interface(struct libusb_device_handle *handle, int iface)
363 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
366 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
367 if (hpriv->endpoints[i] >= 0)
368 close(hpriv->endpoints[i]);
370 return (LIBUSB_SUCCESS);
374 netbsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
377 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
378 struct usb_alt_interface intf;
380 usbi_dbg("iface %d, setting %d", iface, altsetting);
382 memset(&intf, 0, sizeof(intf));
384 intf.uai_interface_index = iface;
385 intf.uai_alt_no = altsetting;
387 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
388 return _errno_to_libusb(errno);
390 return (LIBUSB_SUCCESS);
394 netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
396 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
397 struct usb_ctl_request req;
401 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
402 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
403 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
404 USETW(req.ucr_request.wIndex, endpoint);
405 USETW(req.ucr_request.wLength, 0);
407 if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
408 return _errno_to_libusb(errno);
410 return (LIBUSB_SUCCESS);
414 netbsd_reset_device(struct libusb_device_handle *handle)
418 return (LIBUSB_ERROR_NOT_SUPPORTED);
422 netbsd_destroy_device(struct libusb_device *dev)
424 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
432 netbsd_submit_transfer(struct usbi_transfer *itransfer)
434 struct libusb_transfer *transfer;
435 struct handle_priv *hpriv;
440 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
441 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
443 switch (transfer->type) {
444 case LIBUSB_TRANSFER_TYPE_CONTROL:
445 err = _sync_control_transfer(itransfer);
447 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
448 if (IS_XFEROUT(transfer)) {
449 /* Isochronous write is not supported */
450 err = LIBUSB_ERROR_NOT_SUPPORTED;
453 err = _sync_gen_transfer(itransfer);
455 case LIBUSB_TRANSFER_TYPE_BULK:
456 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
457 if (IS_XFEROUT(transfer) &&
458 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
459 err = LIBUSB_ERROR_NOT_SUPPORTED;
462 err = _sync_gen_transfer(itransfer);
464 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
465 err = LIBUSB_ERROR_NOT_SUPPORTED;
472 if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
473 return _errno_to_libusb(errno);
475 return (LIBUSB_SUCCESS);
479 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
483 return (LIBUSB_ERROR_NOT_SUPPORTED);
487 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
495 netbsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
498 struct libusb_device_handle *handle;
499 struct handle_priv *hpriv = NULL;
500 struct usbi_transfer *itransfer;
501 struct pollfd *pollfd;
506 pthread_mutex_lock(&ctx->open_devs_lock);
507 for (i = 0; i < nfds && num_ready > 0; i++) {
510 if (!pollfd->revents)
515 list_for_each_entry(handle, &ctx->open_devs, list,
516 struct libusb_device_handle) {
517 hpriv = (struct handle_priv *)handle->os_priv;
519 if (hpriv->pipe[0] == pollfd->fd)
526 usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
531 if (pollfd->revents & POLLERR) {
532 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
533 usbi_handle_disconnect(handle);
537 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
542 if ((err = usbi_handle_transfer_completion(itransfer,
543 LIBUSB_TRANSFER_COMPLETED)))
546 pthread_mutex_unlock(&ctx->open_devs_lock);
549 return _errno_to_libusb(err);
551 return (LIBUSB_SUCCESS);
555 netbsd_clock_gettime(int clkid, struct timespec *tp)
557 usbi_dbg("clock %d", clkid);
559 if (clkid == USBI_CLOCK_REALTIME)
560 return clock_gettime(CLOCK_REALTIME, tp);
562 if (clkid == USBI_CLOCK_MONOTONIC)
563 return clock_gettime(CLOCK_MONOTONIC, tp);
565 return (LIBUSB_ERROR_INVALID_PARAM);
569 _errno_to_libusb(int err)
573 return (LIBUSB_ERROR_IO);
575 return (LIBUSB_ERROR_ACCESS);
577 return (LIBUSB_ERROR_NO_DEVICE);
579 return (LIBUSB_ERROR_NO_MEM);
582 usbi_dbg("error: %s", strerror(err));
584 return (LIBUSB_ERROR_OTHER);
588 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
590 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
591 struct usb_config_desc ucd;
592 struct usb_full_desc ufd;
596 usbi_dbg("fd %d", fd);
598 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
600 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
601 return _errno_to_libusb(errno);
603 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
605 len = UGETW(ucd.ucd_desc.wTotalLength);
608 return (LIBUSB_ERROR_NO_MEM);
610 ufd.ufd_config_index = ucd.ucd_config_index;
614 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
616 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
618 return _errno_to_libusb(errno);
629 _sync_control_transfer(struct usbi_transfer *itransfer)
631 struct libusb_transfer *transfer;
632 struct libusb_control_setup *setup;
633 struct device_priv *dpriv;
634 struct usb_ctl_request req;
636 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
637 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
638 setup = (struct libusb_control_setup *)transfer->buffer;
640 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
641 setup->bmRequestType, setup->bRequest,
642 libusb_le16_to_cpu(setup->wValue),
643 libusb_le16_to_cpu(setup->wIndex),
644 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
646 req.ucr_request.bmRequestType = setup->bmRequestType;
647 req.ucr_request.bRequest = setup->bRequest;
648 /* Don't use USETW, libusb already deals with the endianness */
649 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
650 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
651 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
652 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
654 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
655 req.ucr_flags = USBD_SHORT_XFER_OK;
657 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
658 return _errno_to_libusb(errno);
660 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
661 return _errno_to_libusb(errno);
663 itransfer->transferred = req.ucr_actlen;
665 usbi_dbg("transferred %d", itransfer->transferred);
671 _access_endpoint(struct libusb_transfer *transfer)
673 struct handle_priv *hpriv;
674 struct device_priv *dpriv;
675 char *s, devnode[16];
679 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
680 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
682 endpt = UE_GET_ADDR(transfer->endpoint);
683 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
685 usbi_dbg("endpoint %d mode %d", endpt, mode);
687 if (hpriv->endpoints[endpt] < 0) {
688 /* Pick the right node given the control one */
689 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
690 s = strchr(devnode, '.');
691 snprintf(s, 4, ".%02d", endpt);
693 /* We may need to read/write to the same endpoint later. */
694 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
695 if ((fd = open(devnode, mode)) < 0)
698 hpriv->endpoints[endpt] = fd;
701 return (hpriv->endpoints[endpt]);
705 _sync_gen_transfer(struct usbi_transfer *itransfer)
707 struct libusb_transfer *transfer;
710 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
713 * Bulk, Interrupt or Isochronous transfer depends on the
714 * endpoint and thus the node to open.
716 if ((fd = _access_endpoint(transfer)) < 0)
717 return _errno_to_libusb(errno);
719 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
720 return _errno_to_libusb(errno);
722 if (IS_XFERIN(transfer)) {
723 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
724 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
725 return _errno_to_libusb(errno);
727 nr = read(fd, transfer->buffer, transfer->length);
729 nr = write(fd, transfer->buffer, transfer->length);
733 return _errno_to_libusb(errno);
735 itransfer->transferred = nr;