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);
469 if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
470 return _errno_to_libusb(errno);
472 return (LIBUSB_SUCCESS);
476 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
480 return (LIBUSB_ERROR_NOT_SUPPORTED);
484 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
492 netbsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
495 struct libusb_device_handle *handle;
496 struct handle_priv *hpriv = NULL;
497 struct usbi_transfer *itransfer;
498 struct pollfd *pollfd;
503 pthread_mutex_lock(&ctx->open_devs_lock);
504 for (i = 0; i < nfds && num_ready > 0; i++) {
507 if (!pollfd->revents)
512 list_for_each_entry(handle, &ctx->open_devs, list,
513 struct libusb_device_handle) {
514 hpriv = (struct handle_priv *)handle->os_priv;
516 if (hpriv->pipe[0] == pollfd->fd)
523 usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
528 if (pollfd->revents & POLLERR) {
529 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
530 usbi_handle_disconnect(handle);
534 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
539 if ((err = usbi_handle_transfer_completion(itransfer,
540 LIBUSB_TRANSFER_COMPLETED)))
543 pthread_mutex_unlock(&ctx->open_devs_lock);
546 return _errno_to_libusb(err);
548 return (LIBUSB_SUCCESS);
552 netbsd_clock_gettime(int clkid, struct timespec *tp)
554 usbi_dbg("clock %d", clkid);
556 if (clkid == USBI_CLOCK_REALTIME)
557 return clock_gettime(CLOCK_REALTIME, tp);
559 if (clkid == USBI_CLOCK_MONOTONIC)
560 return clock_gettime(CLOCK_MONOTONIC, tp);
562 return (LIBUSB_ERROR_INVALID_PARAM);
566 _errno_to_libusb(int err)
570 return (LIBUSB_ERROR_IO);
572 return (LIBUSB_ERROR_ACCESS);
574 return (LIBUSB_ERROR_NO_DEVICE);
576 return (LIBUSB_ERROR_NO_MEM);
579 usbi_dbg("error: %s", strerror(err));
581 return (LIBUSB_ERROR_OTHER);
585 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
587 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
588 struct usb_config_desc ucd;
589 struct usb_full_desc ufd;
593 usbi_dbg("fd %d", fd);
595 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
597 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
598 return _errno_to_libusb(errno);
600 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
602 len = UGETW(ucd.ucd_desc.wTotalLength);
605 return (LIBUSB_ERROR_NO_MEM);
607 ufd.ufd_config_index = ucd.ucd_config_index;
611 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
613 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
615 return _errno_to_libusb(errno);
626 _sync_control_transfer(struct usbi_transfer *itransfer)
628 struct libusb_transfer *transfer;
629 struct libusb_control_setup *setup;
630 struct device_priv *dpriv;
631 struct usb_ctl_request req;
633 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
634 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
635 setup = (struct libusb_control_setup *)transfer->buffer;
637 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
638 setup->bmRequestType, setup->bRequest,
639 libusb_le16_to_cpu(setup->wValue),
640 libusb_le16_to_cpu(setup->wIndex),
641 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
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 ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
655 return _errno_to_libusb(errno);
657 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
658 return _errno_to_libusb(errno);
660 itransfer->transferred = req.ucr_actlen;
662 usbi_dbg("transferred %d", itransfer->transferred);
668 _access_endpoint(struct libusb_transfer *transfer)
670 struct handle_priv *hpriv;
671 struct device_priv *dpriv;
672 char *s, devnode[16];
676 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
677 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
679 endpt = UE_GET_ADDR(transfer->endpoint);
680 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
682 usbi_dbg("endpoint %d mode %d", endpt, mode);
684 if (hpriv->endpoints[endpt] < 0) {
685 /* Pick the right node given the control one */
686 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
687 s = strchr(devnode, '.');
688 snprintf(s, 4, ".%02d", endpt);
690 /* We may need to read/write to the same endpoint later. */
691 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
692 if ((fd = open(devnode, mode)) < 0)
695 hpriv->endpoints[endpt] = fd;
698 return (hpriv->endpoints[endpt]);
702 _sync_gen_transfer(struct usbi_transfer *itransfer)
704 struct libusb_transfer *transfer;
707 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
710 * Bulk, Interrupt or Isochronous transfer depends on the
711 * endpoint and thus the node to open.
713 if ((fd = _access_endpoint(transfer)) < 0)
714 return _errno_to_libusb(errno);
716 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
717 return _errno_to_libusb(errno);
719 if (IS_XFERIN(transfer)) {
720 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
721 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
722 return _errno_to_libusb(errno);
724 nr = read(fd, transfer->buffer, transfer->length);
726 nr = write(fd, transfer->buffer, transfer->length);
730 return _errno_to_libusb(errno);
732 itransfer->transferred = nr;