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, /* kernel_driver_active() */
116 NULL, /* detach_kernel_driver() */
117 NULL, /* attach_kernel_driver() */
119 netbsd_destroy_device,
121 netbsd_submit_transfer,
122 netbsd_cancel_transfer,
123 netbsd_clear_transfer_priv,
125 netbsd_handle_events,
127 netbsd_clock_gettime,
128 sizeof(struct device_priv),
129 sizeof(struct handle_priv),
130 0, /* transfer_priv_size */
131 0, /* add_iso_packet_size */
135 netbsd_get_device_list(struct libusb_context * ctx,
136 struct discovered_devs **discdevs)
138 struct libusb_device *dev;
139 struct device_priv *dpriv;
140 struct usb_device_info di;
141 unsigned long session_id;
147 /* Only ugen(4) is supported */
148 for (i = 0; i < USB_MAX_DEVICES; i++) {
149 /* Control endpoint is always .00 */
150 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
152 if ((fd = open(devnode, O_RDONLY)) < 0) {
153 if (errno != ENOENT && errno != ENXIO)
154 usbi_err(ctx, "could not open %s", devnode);
158 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
161 session_id = (di.udi_bus << 8 | di.udi_addr);
162 dev = usbi_get_device_by_session_id(ctx, session_id);
165 dev = usbi_alloc_device(ctx, session_id);
167 return (LIBUSB_ERROR_NO_MEM);
169 dev->bus_number = di.udi_bus;
170 dev->device_address = di.udi_addr;
171 dev->speed = di.udi_speed;
173 dpriv = (struct device_priv *)dev->os_priv;
174 strlcpy(dpriv->devnode, devnode, sizeof(devnode));
177 if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
183 if (_cache_active_config_descriptor(dev, fd)) {
188 if ((err = usbi_sanitize_device(dev)))
193 if (discovered_devs_append(*discdevs, dev) == NULL)
194 return (LIBUSB_ERROR_NO_MEM);
196 libusb_unref_device(dev);
199 return (LIBUSB_SUCCESS);
203 libusb_unref_device(dev);
204 return _errno_to_libusb(err);
208 netbsd_open(struct libusb_device_handle *handle)
210 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
211 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
213 dpriv->fd = open(dpriv->devnode, O_RDWR);
215 dpriv->fd = open(dpriv->devnode, O_RDONLY);
217 return _errno_to_libusb(errno);
220 usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
222 if (pipe(hpriv->pipe) < 0)
223 return _errno_to_libusb(errno);
225 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
229 netbsd_close(struct libusb_device_handle *handle)
231 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
232 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
234 usbi_dbg("close: fd %d", dpriv->fd);
239 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
241 close(hpriv->pipe[0]);
242 close(hpriv->pipe[1]);
246 netbsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
249 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
253 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
257 return (LIBUSB_SUCCESS);
261 netbsd_get_active_config_descriptor(struct libusb_device *dev,
262 unsigned char *buf, size_t len, int *host_endian)
264 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
265 usb_config_descriptor_t *ucd;
267 ucd = (usb_config_descriptor_t *) dpriv->cdesc;
268 len = MIN(len, UGETW(ucd->wTotalLength));
270 usbi_dbg("len %d", len);
272 memcpy(buf, dpriv->cdesc, len);
280 netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
281 unsigned char *buf, size_t len, int *host_endian)
283 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
284 struct usb_full_desc ufd;
287 usbi_dbg("index %d, len %d", idx, len);
289 /* A config descriptor may be requested before opening the device */
290 if (dpriv->fd >= 0) {
293 fd = open(dpriv->devnode, O_RDONLY);
295 return _errno_to_libusb(errno);
298 ufd.ufd_config_index = idx;
302 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
306 return _errno_to_libusb(err);
318 netbsd_get_configuration(struct libusb_device_handle *handle, int *config)
320 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
324 if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
325 return _errno_to_libusb(errno);
327 usbi_dbg("configuration %d", *config);
329 return (LIBUSB_SUCCESS);
333 netbsd_set_configuration(struct libusb_device_handle *handle, int config)
335 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
337 usbi_dbg("configuration %d", config);
339 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
340 return _errno_to_libusb(errno);
342 return _cache_active_config_descriptor(handle->dev, dpriv->fd);
346 netbsd_claim_interface(struct libusb_device_handle *handle, int iface)
348 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
351 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
352 hpriv->endpoints[i] = -1;
354 return (LIBUSB_SUCCESS);
358 netbsd_release_interface(struct libusb_device_handle *handle, int iface)
360 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
363 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
364 if (hpriv->endpoints[i] >= 0)
365 close(hpriv->endpoints[i]);
367 return (LIBUSB_SUCCESS);
371 netbsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
374 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
375 struct usb_alt_interface intf;
377 usbi_dbg("iface %d, setting %d", iface, altsetting);
379 memset(&intf, 0, sizeof(intf));
381 intf.uai_interface_index = iface;
382 intf.uai_alt_no = altsetting;
384 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
385 return _errno_to_libusb(errno);
387 return (LIBUSB_SUCCESS);
391 netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
393 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
394 struct usb_ctl_request req;
398 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
399 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
400 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
401 USETW(req.ucr_request.wIndex, endpoint);
402 USETW(req.ucr_request.wLength, 0);
404 if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
405 return _errno_to_libusb(errno);
407 return (LIBUSB_SUCCESS);
411 netbsd_reset_device(struct libusb_device_handle *handle)
415 return (LIBUSB_ERROR_NOT_SUPPORTED);
419 netbsd_destroy_device(struct libusb_device *dev)
421 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
429 netbsd_submit_transfer(struct usbi_transfer *itransfer)
431 struct libusb_transfer *transfer;
432 struct handle_priv *hpriv;
437 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
438 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
440 switch (transfer->type) {
441 case LIBUSB_TRANSFER_TYPE_CONTROL:
442 err = _sync_control_transfer(itransfer);
444 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
445 if (IS_XFEROUT(transfer)) {
446 /* Isochronous write is not supported */
447 err = LIBUSB_ERROR_NOT_SUPPORTED;
450 err = _sync_gen_transfer(itransfer);
452 case LIBUSB_TRANSFER_TYPE_BULK:
453 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
454 if (IS_XFEROUT(transfer) &&
455 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
456 err = LIBUSB_ERROR_NOT_SUPPORTED;
459 err = _sync_gen_transfer(itransfer);
466 if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
467 return _errno_to_libusb(errno);
469 return (LIBUSB_SUCCESS);
473 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
477 return (LIBUSB_ERROR_NOT_SUPPORTED);
481 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
489 netbsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
492 struct libusb_device_handle *handle;
493 struct handle_priv *hpriv = NULL;
494 struct usbi_transfer *itransfer;
495 struct pollfd *pollfd;
500 pthread_mutex_lock(&ctx->open_devs_lock);
501 for (i = 0; i < nfds && num_ready > 0; i++) {
504 if (!pollfd->revents)
509 list_for_each_entry(handle, &ctx->open_devs, list,
510 struct libusb_device_handle) {
511 hpriv = (struct handle_priv *)handle->os_priv;
513 if (hpriv->pipe[0] == pollfd->fd)
520 usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
525 if (pollfd->revents & POLLERR) {
526 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
527 usbi_handle_disconnect(handle);
531 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
536 if ((err = usbi_handle_transfer_completion(itransfer,
537 LIBUSB_TRANSFER_COMPLETED)))
540 pthread_mutex_unlock(&ctx->open_devs_lock);
543 return _errno_to_libusb(err);
545 return (LIBUSB_SUCCESS);
549 netbsd_clock_gettime(int clkid, struct timespec *tp)
551 usbi_dbg("clock %d", clkid);
553 if (clkid == USBI_CLOCK_REALTIME)
554 return clock_gettime(CLOCK_REALTIME, tp);
556 if (clkid == USBI_CLOCK_MONOTONIC)
557 return clock_gettime(CLOCK_MONOTONIC, tp);
559 return (LIBUSB_ERROR_INVALID_PARAM);
563 _errno_to_libusb(int err)
567 return (LIBUSB_ERROR_IO);
569 return (LIBUSB_ERROR_ACCESS);
571 return (LIBUSB_ERROR_NO_DEVICE);
573 return (LIBUSB_ERROR_NO_MEM);
576 usbi_dbg("error: %s", strerror(err));
578 return (LIBUSB_ERROR_OTHER);
582 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
584 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
585 struct usb_config_desc ucd;
586 struct usb_full_desc ufd;
590 usbi_dbg("fd %d", fd);
592 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
594 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
595 return _errno_to_libusb(errno);
597 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
599 len = UGETW(ucd.ucd_desc.wTotalLength);
602 return (LIBUSB_ERROR_NO_MEM);
604 ufd.ufd_config_index = ucd.ucd_config_index;
608 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
610 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
612 return _errno_to_libusb(errno);
623 _sync_control_transfer(struct usbi_transfer *itransfer)
625 struct libusb_transfer *transfer;
626 struct libusb_control_setup *setup;
627 struct device_priv *dpriv;
628 struct usb_ctl_request req;
630 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
631 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
632 setup = (struct libusb_control_setup *)transfer->buffer;
634 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
635 setup->bmRequestType, setup->bRequest,
636 libusb_le16_to_cpu(setup->wValue),
637 libusb_le16_to_cpu(setup->wIndex),
638 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
640 req.ucr_request.bmRequestType = setup->bmRequestType;
641 req.ucr_request.bRequest = setup->bRequest;
642 /* Don't use USETW, libusb already deals with the endianness */
643 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
644 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
645 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
646 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
648 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
649 req.ucr_flags = USBD_SHORT_XFER_OK;
651 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
652 return _errno_to_libusb(errno);
654 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
655 return _errno_to_libusb(errno);
657 itransfer->transferred = req.ucr_actlen;
659 usbi_dbg("transferred %d", itransfer->transferred);
665 _access_endpoint(struct libusb_transfer *transfer)
667 struct handle_priv *hpriv;
668 struct device_priv *dpriv;
669 char *s, devnode[16];
673 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
674 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
676 endpt = UE_GET_ADDR(transfer->endpoint);
677 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
679 usbi_dbg("endpoint %d mode %d", endpt, mode);
681 if (hpriv->endpoints[endpt] < 0) {
682 /* Pick the right node given the control one */
683 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
684 s = strchr(devnode, '.');
685 snprintf(s, 4, ".%02d", endpt);
687 /* We may need to read/write to the same endpoint later. */
688 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
689 if ((fd = open(devnode, mode)) < 0)
692 hpriv->endpoints[endpt] = fd;
695 return (hpriv->endpoints[endpt]);
699 _sync_gen_transfer(struct usbi_transfer *itransfer)
701 struct libusb_transfer *transfer;
704 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
707 * Bulk, Interrupt or Isochronous transfer depends on the
708 * endpoint and thus the node to open.
710 if ((fd = _access_endpoint(transfer)) < 0)
711 return _errno_to_libusb(errno);
713 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
714 return _errno_to_libusb(errno);
716 if (IS_XFERIN(transfer)) {
717 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
718 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
719 return _errno_to_libusb(errno);
721 nr = read(fd, transfer->buffer, transfer->length);
723 nr = write(fd, transfer->buffer, transfer->length);
727 return _errno_to_libusb(errno);
729 itransfer->transferred = nr;