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 usbi_backend = {
90 "Synchronous NetBSD backend",
94 NULL, /* set_option() */
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, /* dev_mem_alloc() */
119 NULL, /* dev_mem_free() */
121 NULL, /* kernel_driver_active() */
122 NULL, /* detach_kernel_driver() */
123 NULL, /* attach_kernel_driver() */
125 netbsd_destroy_device,
127 netbsd_submit_transfer,
128 netbsd_cancel_transfer,
129 netbsd_clear_transfer_priv,
131 NULL, /* handle_events() */
132 netbsd_handle_transfer_completion,
134 netbsd_clock_gettime,
135 0, /* context_priv_size */
136 sizeof(struct device_priv),
137 sizeof(struct handle_priv),
138 0, /* transfer_priv_size */
142 netbsd_get_device_list(struct libusb_context * ctx,
143 struct discovered_devs **discdevs)
145 struct libusb_device *dev;
146 struct device_priv *dpriv;
147 struct usb_device_info di;
148 unsigned long session_id;
154 /* Only ugen(4) is supported */
155 for (i = 0; i < USB_MAX_DEVICES; i++) {
156 /* Control endpoint is always .00 */
157 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
159 if ((fd = open(devnode, O_RDONLY)) < 0) {
160 if (errno != ENOENT && errno != ENXIO)
161 usbi_err(ctx, "could not open %s", devnode);
165 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
168 session_id = (di.udi_bus << 8 | di.udi_addr);
169 dev = usbi_get_device_by_session_id(ctx, session_id);
172 dev = usbi_alloc_device(ctx, session_id);
174 return (LIBUSB_ERROR_NO_MEM);
176 dev->bus_number = di.udi_bus;
177 dev->device_address = di.udi_addr;
178 dev->speed = di.udi_speed;
180 dpriv = (struct device_priv *)dev->os_priv;
181 strlcpy(dpriv->devnode, devnode, sizeof(devnode));
184 if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
190 if (_cache_active_config_descriptor(dev, fd)) {
195 if ((err = usbi_sanitize_device(dev)))
200 if (discovered_devs_append(*discdevs, dev) == NULL)
201 return (LIBUSB_ERROR_NO_MEM);
203 libusb_unref_device(dev);
206 return (LIBUSB_SUCCESS);
210 libusb_unref_device(dev);
211 return _errno_to_libusb(err);
215 netbsd_open(struct libusb_device_handle *handle)
217 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
219 dpriv->fd = open(dpriv->devnode, O_RDWR);
221 dpriv->fd = open(dpriv->devnode, O_RDONLY);
223 return _errno_to_libusb(errno);
226 usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
228 return (LIBUSB_SUCCESS);
232 netbsd_close(struct libusb_device_handle *handle)
234 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
236 usbi_dbg("close: fd %d", dpriv->fd);
243 netbsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
246 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
250 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
254 return (LIBUSB_SUCCESS);
258 netbsd_get_active_config_descriptor(struct libusb_device *dev,
259 unsigned char *buf, size_t len, int *host_endian)
261 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
262 usb_config_descriptor_t *ucd;
264 ucd = (usb_config_descriptor_t *) dpriv->cdesc;
265 len = MIN(len, UGETW(ucd->wTotalLength));
267 usbi_dbg("len %d", len);
269 memcpy(buf, dpriv->cdesc, len);
277 netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
278 unsigned char *buf, size_t len, int *host_endian)
280 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
281 struct usb_full_desc ufd;
284 usbi_dbg("index %d, len %d", idx, len);
286 /* A config descriptor may be requested before opening the device */
287 if (dpriv->fd >= 0) {
290 fd = open(dpriv->devnode, O_RDONLY);
292 return _errno_to_libusb(errno);
295 ufd.ufd_config_index = idx;
299 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
303 return _errno_to_libusb(err);
315 netbsd_get_configuration(struct libusb_device_handle *handle, int *config)
317 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
321 if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
322 return _errno_to_libusb(errno);
324 usbi_dbg("configuration %d", *config);
326 return (LIBUSB_SUCCESS);
330 netbsd_set_configuration(struct libusb_device_handle *handle, int config)
332 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
334 usbi_dbg("configuration %d", config);
336 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
337 return _errno_to_libusb(errno);
339 return _cache_active_config_descriptor(handle->dev, dpriv->fd);
343 netbsd_claim_interface(struct libusb_device_handle *handle, int iface)
345 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
348 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
349 hpriv->endpoints[i] = -1;
351 return (LIBUSB_SUCCESS);
355 netbsd_release_interface(struct libusb_device_handle *handle, int iface)
357 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
360 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
361 if (hpriv->endpoints[i] >= 0)
362 close(hpriv->endpoints[i]);
364 return (LIBUSB_SUCCESS);
368 netbsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
371 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
372 struct usb_alt_interface intf;
374 usbi_dbg("iface %d, setting %d", iface, altsetting);
376 memset(&intf, 0, sizeof(intf));
378 intf.uai_interface_index = iface;
379 intf.uai_alt_no = altsetting;
381 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
382 return _errno_to_libusb(errno);
384 return (LIBUSB_SUCCESS);
388 netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
390 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
391 struct usb_ctl_request req;
395 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
396 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
397 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
398 USETW(req.ucr_request.wIndex, endpoint);
399 USETW(req.ucr_request.wLength, 0);
401 if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
402 return _errno_to_libusb(errno);
404 return (LIBUSB_SUCCESS);
408 netbsd_reset_device(struct libusb_device_handle *handle)
412 return (LIBUSB_ERROR_NOT_SUPPORTED);
416 netbsd_destroy_device(struct libusb_device *dev)
418 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
426 netbsd_submit_transfer(struct usbi_transfer *itransfer)
428 struct libusb_transfer *transfer;
429 struct handle_priv *hpriv;
434 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
435 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
437 switch (transfer->type) {
438 case LIBUSB_TRANSFER_TYPE_CONTROL:
439 err = _sync_control_transfer(itransfer);
441 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
442 if (IS_XFEROUT(transfer)) {
443 /* Isochronous write is not supported */
444 err = LIBUSB_ERROR_NOT_SUPPORTED;
447 err = _sync_gen_transfer(itransfer);
449 case LIBUSB_TRANSFER_TYPE_BULK:
450 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
451 if (IS_XFEROUT(transfer) &&
452 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
453 err = LIBUSB_ERROR_NOT_SUPPORTED;
456 err = _sync_gen_transfer(itransfer);
458 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
459 err = LIBUSB_ERROR_NOT_SUPPORTED;
466 usbi_signal_transfer_completion(itransfer);
468 return (LIBUSB_SUCCESS);
472 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
476 return (LIBUSB_ERROR_NOT_SUPPORTED);
480 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
488 netbsd_handle_transfer_completion(struct usbi_transfer *itransfer)
490 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
494 netbsd_clock_gettime(int clkid, struct timespec *tp)
496 usbi_dbg("clock %d", clkid);
498 if (clkid == USBI_CLOCK_REALTIME)
499 return clock_gettime(CLOCK_REALTIME, tp);
501 if (clkid == USBI_CLOCK_MONOTONIC)
502 return clock_gettime(CLOCK_MONOTONIC, tp);
504 return (LIBUSB_ERROR_INVALID_PARAM);
508 _errno_to_libusb(int err)
512 return (LIBUSB_ERROR_IO);
514 return (LIBUSB_ERROR_ACCESS);
516 return (LIBUSB_ERROR_NO_DEVICE);
518 return (LIBUSB_ERROR_NO_MEM);
521 usbi_dbg("error: %s", strerror(err));
523 return (LIBUSB_ERROR_OTHER);
527 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
529 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
530 struct usb_config_desc ucd;
531 struct usb_full_desc ufd;
535 usbi_dbg("fd %d", fd);
537 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
539 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
540 return _errno_to_libusb(errno);
542 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
544 len = UGETW(ucd.ucd_desc.wTotalLength);
547 return (LIBUSB_ERROR_NO_MEM);
549 ufd.ufd_config_index = ucd.ucd_config_index;
553 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
555 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
557 return _errno_to_libusb(errno);
568 _sync_control_transfer(struct usbi_transfer *itransfer)
570 struct libusb_transfer *transfer;
571 struct libusb_control_setup *setup;
572 struct device_priv *dpriv;
573 struct usb_ctl_request req;
575 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
576 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
577 setup = (struct libusb_control_setup *)transfer->buffer;
579 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
580 setup->bmRequestType, setup->bRequest,
581 libusb_le16_to_cpu(setup->wValue),
582 libusb_le16_to_cpu(setup->wIndex),
583 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
585 req.ucr_request.bmRequestType = setup->bmRequestType;
586 req.ucr_request.bRequest = setup->bRequest;
587 /* Don't use USETW, libusb already deals with the endianness */
588 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
589 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
590 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
591 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
593 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
594 req.ucr_flags = USBD_SHORT_XFER_OK;
596 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
597 return _errno_to_libusb(errno);
599 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
600 return _errno_to_libusb(errno);
602 itransfer->transferred = req.ucr_actlen;
604 usbi_dbg("transferred %d", itransfer->transferred);
610 _access_endpoint(struct libusb_transfer *transfer)
612 struct handle_priv *hpriv;
613 struct device_priv *dpriv;
614 char *s, devnode[16];
618 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
619 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
621 endpt = UE_GET_ADDR(transfer->endpoint);
622 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
624 usbi_dbg("endpoint %d mode %d", endpt, mode);
626 if (hpriv->endpoints[endpt] < 0) {
627 /* Pick the right node given the control one */
628 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
629 s = strchr(devnode, '.');
630 snprintf(s, 4, ".%02d", endpt);
632 /* We may need to read/write to the same endpoint later. */
633 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
634 if ((fd = open(devnode, mode)) < 0)
637 hpriv->endpoints[endpt] = fd;
640 return (hpriv->endpoints[endpt]);
644 _sync_gen_transfer(struct usbi_transfer *itransfer)
646 struct libusb_transfer *transfer;
649 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
652 * Bulk, Interrupt or Isochronous transfer depends on the
653 * endpoint and thus the node to open.
655 if ((fd = _access_endpoint(transfer)) < 0)
656 return _errno_to_libusb(errno);
658 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
659 return _errno_to_libusb(errno);
661 if (IS_XFERIN(transfer)) {
662 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
663 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
664 return _errno_to_libusb(errno);
666 nr = read(fd, transfer->buffer, transfer->length);
668 nr = write(fd, transfer->buffer, transfer->length);
672 return _errno_to_libusb(errno);
674 itransfer->transferred = nr;