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
22 #include <sys/types.h>
31 #include <dev/usb/usb.h>
39 unsigned char *cdesc; /* active config descriptor */
40 usb_device_descriptor_t ddesc; /* usb device descriptor */
44 int pipe[2]; /* for event notification */
45 int endpoints[USB_MAX_ENDPOINTS];
51 static int netbsd_get_device_list(struct libusb_context *,
52 struct discovered_devs **);
53 static int netbsd_open(struct libusb_device_handle *);
54 static void netbsd_close(struct libusb_device_handle *);
56 static int netbsd_get_device_descriptor(struct libusb_device *, unsigned char *,
58 static int netbsd_get_active_config_descriptor(struct libusb_device *,
59 unsigned char *, size_t, int *);
60 static int netbsd_get_config_descriptor(struct libusb_device *, uint8_t,
61 unsigned char *, size_t, int *);
63 static int netbsd_get_configuration(struct libusb_device_handle *, int *);
64 static int netbsd_set_configuration(struct libusb_device_handle *, int);
66 static int netbsd_claim_interface(struct libusb_device_handle *, int);
67 static int netbsd_release_interface(struct libusb_device_handle *, int);
69 static int netbsd_set_interface_altsetting(struct libusb_device_handle *, int,
71 static int netbsd_clear_halt(struct libusb_device_handle *, unsigned char);
72 static int netbsd_reset_device(struct libusb_device_handle *);
73 static void netbsd_destroy_device(struct libusb_device *);
75 static int netbsd_submit_transfer(struct usbi_transfer *);
76 static int netbsd_cancel_transfer(struct usbi_transfer *);
77 static void netbsd_clear_transfer_priv(struct usbi_transfer *);
78 static int netbsd_handle_events(struct libusb_context *ctx, struct pollfd *,
80 static int netbsd_clock_gettime(int, struct timespec *);
85 static int _errno_to_libusb(int);
86 static int _cache_active_config_descriptor(struct libusb_device *, int);
87 static int _sync_control_transfer(struct usbi_transfer *);
88 static int _sync_gen_transfer(struct usbi_transfer *);
89 static int _access_endpoint(struct libusb_transfer *);
91 const struct usbi_os_backend netbsd_backend = {
92 "Synchronous NetBSD backend",
96 netbsd_get_device_list,
97 NULL, /* hotplug_poll */
101 netbsd_get_device_descriptor,
102 netbsd_get_active_config_descriptor,
103 netbsd_get_config_descriptor,
104 NULL, /* get_config_descriptor_by_value() */
106 netbsd_get_configuration,
107 netbsd_set_configuration,
109 netbsd_claim_interface,
110 netbsd_release_interface,
112 netbsd_set_interface_altsetting,
116 NULL, /* alloc_streams */
117 NULL, /* free_streams */
119 NULL, /* kernel_driver_active() */
120 NULL, /* detach_kernel_driver() */
121 NULL, /* attach_kernel_driver() */
123 netbsd_destroy_device,
125 netbsd_submit_transfer,
126 netbsd_cancel_transfer,
127 netbsd_clear_transfer_priv,
129 netbsd_handle_events,
131 netbsd_clock_gettime,
132 sizeof(struct device_priv),
133 sizeof(struct handle_priv),
134 0, /* transfer_priv_size */
135 0, /* add_iso_packet_size */
139 netbsd_get_device_list(struct libusb_context * ctx,
140 struct discovered_devs **discdevs)
142 struct libusb_device *dev;
143 struct device_priv *dpriv;
144 struct usb_device_info di;
145 unsigned long session_id;
151 /* Only ugen(4) is supported */
152 for (i = 0; i < USB_MAX_DEVICES; i++) {
153 /* Control endpoint is always .00 */
154 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
156 if ((fd = open(devnode, O_RDONLY)) < 0) {
157 if (errno != ENOENT && errno != ENXIO)
158 usbi_err(ctx, "could not open %s", devnode);
162 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
165 session_id = (di.udi_bus << 8 | di.udi_addr);
166 dev = usbi_get_device_by_session_id(ctx, session_id);
169 dev = usbi_alloc_device(ctx, session_id);
171 return (LIBUSB_ERROR_NO_MEM);
173 dev->bus_number = di.udi_bus;
174 dev->device_address = di.udi_addr;
175 dev->speed = di.udi_speed;
177 dpriv = (struct device_priv *)dev->os_priv;
178 strlcpy(dpriv->devnode, devnode, sizeof(devnode));
181 if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
187 if (_cache_active_config_descriptor(dev, fd)) {
192 if ((err = usbi_sanitize_device(dev)))
197 if (discovered_devs_append(*discdevs, dev) == NULL)
198 return (LIBUSB_ERROR_NO_MEM);
200 libusb_unref_device(dev);
203 return (LIBUSB_SUCCESS);
207 libusb_unref_device(dev);
208 return _errno_to_libusb(err);
212 netbsd_open(struct libusb_device_handle *handle)
214 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
215 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
217 dpriv->fd = open(dpriv->devnode, O_RDWR);
219 dpriv->fd = open(dpriv->devnode, O_RDONLY);
221 return _errno_to_libusb(errno);
224 usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
226 if (pipe(hpriv->pipe) < 0)
227 return _errno_to_libusb(errno);
229 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
233 netbsd_close(struct libusb_device_handle *handle)
235 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
236 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
238 usbi_dbg("close: fd %d", dpriv->fd);
243 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
245 close(hpriv->pipe[0]);
246 close(hpriv->pipe[1]);
250 netbsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
253 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
257 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
261 return (LIBUSB_SUCCESS);
265 netbsd_get_active_config_descriptor(struct libusb_device *dev,
266 unsigned char *buf, size_t len, int *host_endian)
268 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
269 usb_config_descriptor_t *ucd;
271 ucd = (usb_config_descriptor_t *) dpriv->cdesc;
272 len = MIN(len, UGETW(ucd->wTotalLength));
274 usbi_dbg("len %d", len);
276 memcpy(buf, dpriv->cdesc, len);
284 netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
285 unsigned char *buf, size_t len, int *host_endian)
287 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
288 struct usb_full_desc ufd;
291 usbi_dbg("index %d, len %d", idx, len);
293 /* A config descriptor may be requested before opening the device */
294 if (dpriv->fd >= 0) {
297 fd = open(dpriv->devnode, O_RDONLY);
299 return _errno_to_libusb(errno);
302 ufd.ufd_config_index = idx;
306 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
310 return _errno_to_libusb(err);
322 netbsd_get_configuration(struct libusb_device_handle *handle, int *config)
324 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
328 if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
329 return _errno_to_libusb(errno);
331 usbi_dbg("configuration %d", *config);
333 return (LIBUSB_SUCCESS);
337 netbsd_set_configuration(struct libusb_device_handle *handle, int config)
339 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
341 usbi_dbg("configuration %d", config);
343 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
344 return _errno_to_libusb(errno);
346 return _cache_active_config_descriptor(handle->dev, dpriv->fd);
350 netbsd_claim_interface(struct libusb_device_handle *handle, int iface)
352 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
355 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
356 hpriv->endpoints[i] = -1;
358 return (LIBUSB_SUCCESS);
362 netbsd_release_interface(struct libusb_device_handle *handle, int iface)
364 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
367 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
368 if (hpriv->endpoints[i] >= 0)
369 close(hpriv->endpoints[i]);
371 return (LIBUSB_SUCCESS);
375 netbsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
378 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
379 struct usb_alt_interface intf;
381 usbi_dbg("iface %d, setting %d", iface, altsetting);
383 memset(&intf, 0, sizeof(intf));
385 intf.uai_interface_index = iface;
386 intf.uai_alt_no = altsetting;
388 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
389 return _errno_to_libusb(errno);
391 return (LIBUSB_SUCCESS);
395 netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
397 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
398 struct usb_ctl_request req;
402 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
403 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
404 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
405 USETW(req.ucr_request.wIndex, endpoint);
406 USETW(req.ucr_request.wLength, 0);
408 if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
409 return _errno_to_libusb(errno);
411 return (LIBUSB_SUCCESS);
415 netbsd_reset_device(struct libusb_device_handle *handle)
419 return (LIBUSB_ERROR_NOT_SUPPORTED);
423 netbsd_destroy_device(struct libusb_device *dev)
425 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
433 netbsd_submit_transfer(struct usbi_transfer *itransfer)
435 struct libusb_transfer *transfer;
436 struct handle_priv *hpriv;
441 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
442 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
444 switch (transfer->type) {
445 case LIBUSB_TRANSFER_TYPE_CONTROL:
446 err = _sync_control_transfer(itransfer);
448 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
449 if (IS_XFEROUT(transfer)) {
450 /* Isochronous write is not supported */
451 err = LIBUSB_ERROR_NOT_SUPPORTED;
454 err = _sync_gen_transfer(itransfer);
456 case LIBUSB_TRANSFER_TYPE_BULK:
457 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
458 if (IS_XFEROUT(transfer) &&
459 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
460 err = LIBUSB_ERROR_NOT_SUPPORTED;
463 err = _sync_gen_transfer(itransfer);
465 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
466 err = LIBUSB_ERROR_NOT_SUPPORTED;
473 if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
474 return _errno_to_libusb(errno);
476 return (LIBUSB_SUCCESS);
480 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
484 return (LIBUSB_ERROR_NOT_SUPPORTED);
488 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
496 netbsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
499 struct libusb_device_handle *handle;
500 struct handle_priv *hpriv = NULL;
501 struct usbi_transfer *itransfer;
502 struct pollfd *pollfd;
507 pthread_mutex_lock(&ctx->open_devs_lock);
508 for (i = 0; i < nfds && num_ready > 0; i++) {
511 if (!pollfd->revents)
516 list_for_each_entry(handle, &ctx->open_devs, list,
517 struct libusb_device_handle) {
518 hpriv = (struct handle_priv *)handle->os_priv;
520 if (hpriv->pipe[0] == pollfd->fd)
527 usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
532 if (pollfd->revents & POLLERR) {
533 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
534 usbi_handle_disconnect(handle);
538 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
543 if ((err = usbi_handle_transfer_completion(itransfer,
544 LIBUSB_TRANSFER_COMPLETED)))
547 pthread_mutex_unlock(&ctx->open_devs_lock);
550 return _errno_to_libusb(err);
552 return (LIBUSB_SUCCESS);
556 netbsd_clock_gettime(int clkid, struct timespec *tp)
558 usbi_dbg("clock %d", clkid);
560 if (clkid == USBI_CLOCK_REALTIME)
561 return clock_gettime(CLOCK_REALTIME, tp);
563 if (clkid == USBI_CLOCK_MONOTONIC)
564 return clock_gettime(CLOCK_MONOTONIC, tp);
566 return (LIBUSB_ERROR_INVALID_PARAM);
570 _errno_to_libusb(int err)
574 return (LIBUSB_ERROR_IO);
576 return (LIBUSB_ERROR_ACCESS);
578 return (LIBUSB_ERROR_NO_DEVICE);
580 return (LIBUSB_ERROR_NO_MEM);
583 usbi_dbg("error: %s", strerror(err));
585 return (LIBUSB_ERROR_OTHER);
589 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
591 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
592 struct usb_config_desc ucd;
593 struct usb_full_desc ufd;
597 usbi_dbg("fd %d", fd);
599 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
601 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
602 return _errno_to_libusb(errno);
604 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
606 len = UGETW(ucd.ucd_desc.wTotalLength);
609 return (LIBUSB_ERROR_NO_MEM);
611 ufd.ufd_config_index = ucd.ucd_config_index;
615 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
617 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
619 return _errno_to_libusb(errno);
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 %d request %d value %d 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_request.bmRequestType = setup->bmRequestType;
648 req.ucr_request.bRequest = setup->bRequest;
649 /* Don't use USETW, libusb already deals with the endianness */
650 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
651 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
652 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
653 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
655 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
656 req.ucr_flags = USBD_SHORT_XFER_OK;
658 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
659 return _errno_to_libusb(errno);
661 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
662 return _errno_to_libusb(errno);
664 itransfer->transferred = req.ucr_actlen;
666 usbi_dbg("transferred %d", itransfer->transferred);
672 _access_endpoint(struct libusb_transfer *transfer)
674 struct handle_priv *hpriv;
675 struct device_priv *dpriv;
676 char *s, devnode[16];
680 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
681 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
683 endpt = UE_GET_ADDR(transfer->endpoint);
684 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
686 usbi_dbg("endpoint %d mode %d", endpt, mode);
688 if (hpriv->endpoints[endpt] < 0) {
689 /* Pick the right node given the control one */
690 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
691 s = strchr(devnode, '.');
692 snprintf(s, 4, ".%02d", endpt);
694 /* We may need to read/write to the same endpoint later. */
695 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
696 if ((fd = open(devnode, mode)) < 0)
699 hpriv->endpoints[endpt] = fd;
702 return (hpriv->endpoints[endpt]);
706 _sync_gen_transfer(struct usbi_transfer *itransfer)
708 struct libusb_transfer *transfer;
711 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
714 * Bulk, Interrupt or Isochronous transfer depends on the
715 * endpoint and thus the node to open.
717 if ((fd = _access_endpoint(transfer)) < 0)
718 return _errno_to_libusb(errno);
720 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
721 return _errno_to_libusb(errno);
723 if (IS_XFERIN(transfer)) {
724 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
725 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
726 return _errno_to_libusb(errno);
728 nr = read(fd, transfer->buffer, transfer->length);
730 nr = write(fd, transfer->buffer, transfer->length);
734 return _errno_to_libusb(errno);
736 itransfer->transferred = nr;