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 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_transfer_completion(struct usbi_transfer *);
78 static int netbsd_clock_gettime(int, struct timespec *);
83 static int _errno_to_libusb(int);
84 static int _cache_active_config_descriptor(struct libusb_device *, int);
85 static int _sync_control_transfer(struct usbi_transfer *);
86 static int _sync_gen_transfer(struct usbi_transfer *);
87 static int _access_endpoint(struct libusb_transfer *);
89 const struct usbi_os_backend netbsd_backend = {
90 "Synchronous NetBSD backend",
94 netbsd_get_device_list,
95 NULL, /* hotplug_poll */
99 netbsd_get_device_descriptor,
100 netbsd_get_active_config_descriptor,
101 netbsd_get_config_descriptor,
102 NULL, /* get_config_descriptor_by_value() */
104 netbsd_get_configuration,
105 netbsd_set_configuration,
107 netbsd_claim_interface,
108 netbsd_release_interface,
110 netbsd_set_interface_altsetting,
114 NULL, /* alloc_streams */
115 NULL, /* free_streams */
117 NULL, /* kernel_driver_active() */
118 NULL, /* detach_kernel_driver() */
119 NULL, /* attach_kernel_driver() */
121 netbsd_destroy_device,
123 netbsd_submit_transfer,
124 netbsd_cancel_transfer,
125 netbsd_clear_transfer_priv,
127 NULL, /* handle_events() */
128 netbsd_handle_transfer_completion,
130 netbsd_clock_gettime,
131 sizeof(struct device_priv),
132 sizeof(struct handle_priv),
133 0, /* transfer_priv_size */
137 netbsd_get_device_list(struct libusb_context * ctx,
138 struct discovered_devs **discdevs)
140 struct libusb_device *dev;
141 struct device_priv *dpriv;
142 struct usb_device_info di;
143 unsigned long session_id;
149 /* Only ugen(4) is supported */
150 for (i = 0; i < USB_MAX_DEVICES; i++) {
151 /* Control endpoint is always .00 */
152 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
154 if ((fd = open(devnode, O_RDONLY)) < 0) {
155 if (errno != ENOENT && errno != ENXIO)
156 usbi_err(ctx, "could not open %s", devnode);
160 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
163 session_id = (di.udi_bus << 8 | di.udi_addr);
164 dev = usbi_get_device_by_session_id(ctx, session_id);
167 dev = usbi_alloc_device(ctx, session_id);
169 return (LIBUSB_ERROR_NO_MEM);
171 dev->bus_number = di.udi_bus;
172 dev->device_address = di.udi_addr;
173 dev->speed = di.udi_speed;
175 dpriv = (struct device_priv *)dev->os_priv;
176 strlcpy(dpriv->devnode, devnode, sizeof(devnode));
179 if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
185 if (_cache_active_config_descriptor(dev, fd)) {
190 if ((err = usbi_sanitize_device(dev)))
195 if (discovered_devs_append(*discdevs, dev) == NULL)
196 return (LIBUSB_ERROR_NO_MEM);
198 libusb_unref_device(dev);
201 return (LIBUSB_SUCCESS);
205 libusb_unref_device(dev);
206 return _errno_to_libusb(err);
210 netbsd_open(struct libusb_device_handle *handle)
212 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
213 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
215 dpriv->fd = open(dpriv->devnode, O_RDWR);
217 dpriv->fd = open(dpriv->devnode, O_RDONLY);
219 return _errno_to_libusb(errno);
222 usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
224 return (LIBUSB_SUCCESS);
228 netbsd_close(struct libusb_device_handle *handle)
230 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
231 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
233 usbi_dbg("close: fd %d", dpriv->fd);
240 netbsd_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 netbsd_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);
274 netbsd_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);
312 netbsd_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 netbsd_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 netbsd_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 netbsd_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 netbsd_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 netbsd_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 netbsd_reset_device(struct libusb_device_handle *handle)
409 return (LIBUSB_ERROR_NOT_SUPPORTED);
413 netbsd_destroy_device(struct libusb_device *dev)
415 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
423 netbsd_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 (IS_XFEROUT(transfer)) {
440 /* Isochronous write is not supported */
441 err = LIBUSB_ERROR_NOT_SUPPORTED;
444 err = _sync_gen_transfer(itransfer);
446 case LIBUSB_TRANSFER_TYPE_BULK:
447 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
448 if (IS_XFEROUT(transfer) &&
449 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
450 err = LIBUSB_ERROR_NOT_SUPPORTED;
453 err = _sync_gen_transfer(itransfer);
455 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
456 err = LIBUSB_ERROR_NOT_SUPPORTED;
463 usbi_signal_transfer_completion(itransfer);
465 return (LIBUSB_SUCCESS);
469 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
473 return (LIBUSB_ERROR_NOT_SUPPORTED);
477 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
485 netbsd_handle_transfer_completion(struct usbi_transfer *itransfer)
487 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
491 netbsd_clock_gettime(int clkid, struct timespec *tp)
493 usbi_dbg("clock %d", clkid);
495 if (clkid == USBI_CLOCK_REALTIME)
496 return clock_gettime(CLOCK_REALTIME, tp);
498 if (clkid == USBI_CLOCK_MONOTONIC)
499 return clock_gettime(CLOCK_MONOTONIC, tp);
501 return (LIBUSB_ERROR_INVALID_PARAM);
505 _errno_to_libusb(int err)
509 return (LIBUSB_ERROR_IO);
511 return (LIBUSB_ERROR_ACCESS);
513 return (LIBUSB_ERROR_NO_DEVICE);
515 return (LIBUSB_ERROR_NO_MEM);
518 usbi_dbg("error: %s", strerror(err));
520 return (LIBUSB_ERROR_OTHER);
524 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
526 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
527 struct usb_config_desc ucd;
528 struct usb_full_desc ufd;
532 usbi_dbg("fd %d", fd);
534 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
536 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
537 return _errno_to_libusb(errno);
539 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
541 len = UGETW(ucd.ucd_desc.wTotalLength);
544 return (LIBUSB_ERROR_NO_MEM);
546 ufd.ufd_config_index = ucd.ucd_config_index;
550 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
552 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
554 return _errno_to_libusb(errno);
565 _sync_control_transfer(struct usbi_transfer *itransfer)
567 struct libusb_transfer *transfer;
568 struct libusb_control_setup *setup;
569 struct device_priv *dpriv;
570 struct usb_ctl_request req;
572 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
573 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
574 setup = (struct libusb_control_setup *)transfer->buffer;
576 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
577 setup->bmRequestType, setup->bRequest,
578 libusb_le16_to_cpu(setup->wValue),
579 libusb_le16_to_cpu(setup->wIndex),
580 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
582 req.ucr_request.bmRequestType = setup->bmRequestType;
583 req.ucr_request.bRequest = setup->bRequest;
584 /* Don't use USETW, libusb already deals with the endianness */
585 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
586 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
587 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
588 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
590 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
591 req.ucr_flags = USBD_SHORT_XFER_OK;
593 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
594 return _errno_to_libusb(errno);
596 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
597 return _errno_to_libusb(errno);
599 itransfer->transferred = req.ucr_actlen;
601 usbi_dbg("transferred %d", itransfer->transferred);
607 _access_endpoint(struct libusb_transfer *transfer)
609 struct handle_priv *hpriv;
610 struct device_priv *dpriv;
611 char *s, devnode[16];
615 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
616 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
618 endpt = UE_GET_ADDR(transfer->endpoint);
619 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
621 usbi_dbg("endpoint %d mode %d", endpt, mode);
623 if (hpriv->endpoints[endpt] < 0) {
624 /* Pick the right node given the control one */
625 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
626 s = strchr(devnode, '.');
627 snprintf(s, 4, ".%02d", endpt);
629 /* We may need to read/write to the same endpoint later. */
630 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
631 if ((fd = open(devnode, mode)) < 0)
634 hpriv->endpoints[endpt] = fd;
637 return (hpriv->endpoints[endpt]);
641 _sync_gen_transfer(struct usbi_transfer *itransfer)
643 struct libusb_transfer *transfer;
646 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
649 * Bulk, Interrupt or Isochronous transfer depends on the
650 * endpoint and thus the node to open.
652 if ((fd = _access_endpoint(transfer)) < 0)
653 return _errno_to_libusb(errno);
655 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
656 return _errno_to_libusb(errno);
658 if (IS_XFERIN(transfer)) {
659 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
660 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
661 return _errno_to_libusb(errno);
663 nr = read(fd, transfer->buffer, transfer->length);
665 nr = write(fd, transfer->buffer, transfer->length);
669 return _errno_to_libusb(errno);
671 itransfer->transferred = nr;