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 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, /* dev_mem_alloc() */
118 NULL, /* dev_mem_free() */
120 NULL, /* kernel_driver_active() */
121 NULL, /* detach_kernel_driver() */
122 NULL, /* attach_kernel_driver() */
124 netbsd_destroy_device,
126 netbsd_submit_transfer,
127 netbsd_cancel_transfer,
128 netbsd_clear_transfer_priv,
130 NULL, /* handle_events() */
131 netbsd_handle_transfer_completion,
133 netbsd_clock_gettime,
134 0, /* context_priv_size */
135 sizeof(struct device_priv),
136 sizeof(struct handle_priv),
137 0, /* transfer_priv_size */
141 netbsd_get_device_list(struct libusb_context * ctx,
142 struct discovered_devs **discdevs)
144 struct libusb_device *dev;
145 struct device_priv *dpriv;
146 struct usb_device_info di;
147 unsigned long session_id;
153 /* Only ugen(4) is supported */
154 for (i = 0; i < USB_MAX_DEVICES; i++) {
155 /* Control endpoint is always .00 */
156 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
158 if ((fd = open(devnode, O_RDONLY)) < 0) {
159 if (errno != ENOENT && errno != ENXIO)
160 usbi_err(ctx, "could not open %s", devnode);
164 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
167 session_id = (di.udi_bus << 8 | di.udi_addr);
168 dev = usbi_get_device_by_session_id(ctx, session_id);
171 dev = usbi_alloc_device(ctx, session_id);
173 return (LIBUSB_ERROR_NO_MEM);
175 dev->bus_number = di.udi_bus;
176 dev->device_address = di.udi_addr;
177 dev->speed = di.udi_speed;
179 dpriv = (struct device_priv *)dev->os_priv;
180 strlcpy(dpriv->devnode, devnode, sizeof(devnode));
183 if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
189 if (_cache_active_config_descriptor(dev, fd)) {
194 if ((err = usbi_sanitize_device(dev)))
199 if (discovered_devs_append(*discdevs, dev) == NULL)
200 return (LIBUSB_ERROR_NO_MEM);
202 libusb_unref_device(dev);
205 return (LIBUSB_SUCCESS);
209 libusb_unref_device(dev);
210 return _errno_to_libusb(err);
214 netbsd_open(struct libusb_device_handle *handle)
216 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
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 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);
244 netbsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
247 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
251 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
255 return (LIBUSB_SUCCESS);
259 netbsd_get_active_config_descriptor(struct libusb_device *dev,
260 unsigned char *buf, size_t len, int *host_endian)
262 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
263 usb_config_descriptor_t *ucd;
265 ucd = (usb_config_descriptor_t *) dpriv->cdesc;
266 len = MIN(len, UGETW(ucd->wTotalLength));
268 usbi_dbg("len %d", len);
270 memcpy(buf, dpriv->cdesc, len);
278 netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
279 unsigned char *buf, size_t len, int *host_endian)
281 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
282 struct usb_full_desc ufd;
285 usbi_dbg("index %d, len %d", idx, len);
287 /* A config descriptor may be requested before opening the device */
288 if (dpriv->fd >= 0) {
291 fd = open(dpriv->devnode, O_RDONLY);
293 return _errno_to_libusb(errno);
296 ufd.ufd_config_index = idx;
300 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
304 return _errno_to_libusb(err);
316 netbsd_get_configuration(struct libusb_device_handle *handle, int *config)
318 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
322 if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
323 return _errno_to_libusb(errno);
325 usbi_dbg("configuration %d", *config);
327 return (LIBUSB_SUCCESS);
331 netbsd_set_configuration(struct libusb_device_handle *handle, int config)
333 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
335 usbi_dbg("configuration %d", config);
337 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
338 return _errno_to_libusb(errno);
340 return _cache_active_config_descriptor(handle->dev, dpriv->fd);
344 netbsd_claim_interface(struct libusb_device_handle *handle, int iface)
346 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
349 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
350 hpriv->endpoints[i] = -1;
352 return (LIBUSB_SUCCESS);
356 netbsd_release_interface(struct libusb_device_handle *handle, int iface)
358 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
361 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
362 if (hpriv->endpoints[i] >= 0)
363 close(hpriv->endpoints[i]);
365 return (LIBUSB_SUCCESS);
369 netbsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
372 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
373 struct usb_alt_interface intf;
375 usbi_dbg("iface %d, setting %d", iface, altsetting);
377 memset(&intf, 0, sizeof(intf));
379 intf.uai_interface_index = iface;
380 intf.uai_alt_no = altsetting;
382 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
383 return _errno_to_libusb(errno);
385 return (LIBUSB_SUCCESS);
389 netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
391 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
392 struct usb_ctl_request req;
396 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
397 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
398 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
399 USETW(req.ucr_request.wIndex, endpoint);
400 USETW(req.ucr_request.wLength, 0);
402 if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
403 return _errno_to_libusb(errno);
405 return (LIBUSB_SUCCESS);
409 netbsd_reset_device(struct libusb_device_handle *handle)
413 return (LIBUSB_ERROR_NOT_SUPPORTED);
417 netbsd_destroy_device(struct libusb_device *dev)
419 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
427 netbsd_submit_transfer(struct usbi_transfer *itransfer)
429 struct libusb_transfer *transfer;
430 struct handle_priv *hpriv;
435 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
436 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
438 switch (transfer->type) {
439 case LIBUSB_TRANSFER_TYPE_CONTROL:
440 err = _sync_control_transfer(itransfer);
442 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
443 if (IS_XFEROUT(transfer)) {
444 /* Isochronous write is not supported */
445 err = LIBUSB_ERROR_NOT_SUPPORTED;
448 err = _sync_gen_transfer(itransfer);
450 case LIBUSB_TRANSFER_TYPE_BULK:
451 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
452 if (IS_XFEROUT(transfer) &&
453 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
454 err = LIBUSB_ERROR_NOT_SUPPORTED;
457 err = _sync_gen_transfer(itransfer);
459 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
460 err = LIBUSB_ERROR_NOT_SUPPORTED;
467 usbi_signal_transfer_completion(itransfer);
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_transfer_completion(struct usbi_transfer *itransfer)
491 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
495 netbsd_clock_gettime(int clkid, struct timespec *tp)
497 usbi_dbg("clock %d", clkid);
499 if (clkid == USBI_CLOCK_REALTIME)
500 return clock_gettime(CLOCK_REALTIME, tp);
502 if (clkid == USBI_CLOCK_MONOTONIC)
503 return clock_gettime(CLOCK_MONOTONIC, tp);
505 return (LIBUSB_ERROR_INVALID_PARAM);
509 _errno_to_libusb(int err)
513 return (LIBUSB_ERROR_IO);
515 return (LIBUSB_ERROR_ACCESS);
517 return (LIBUSB_ERROR_NO_DEVICE);
519 return (LIBUSB_ERROR_NO_MEM);
522 usbi_dbg("error: %s", strerror(err));
524 return (LIBUSB_ERROR_OTHER);
528 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
530 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
531 struct usb_config_desc ucd;
532 struct usb_full_desc ufd;
536 usbi_dbg("fd %d", fd);
538 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
540 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
541 return _errno_to_libusb(errno);
543 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
545 len = UGETW(ucd.ucd_desc.wTotalLength);
548 return (LIBUSB_ERROR_NO_MEM);
550 ufd.ufd_config_index = ucd.ucd_config_index;
554 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
556 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
558 return _errno_to_libusb(errno);
569 _sync_control_transfer(struct usbi_transfer *itransfer)
571 struct libusb_transfer *transfer;
572 struct libusb_control_setup *setup;
573 struct device_priv *dpriv;
574 struct usb_ctl_request req;
576 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
577 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
578 setup = (struct libusb_control_setup *)transfer->buffer;
580 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
581 setup->bmRequestType, setup->bRequest,
582 libusb_le16_to_cpu(setup->wValue),
583 libusb_le16_to_cpu(setup->wIndex),
584 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
586 req.ucr_request.bmRequestType = setup->bmRequestType;
587 req.ucr_request.bRequest = setup->bRequest;
588 /* Don't use USETW, libusb already deals with the endianness */
589 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
590 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
591 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
592 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
594 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
595 req.ucr_flags = USBD_SHORT_XFER_OK;
597 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
598 return _errno_to_libusb(errno);
600 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
601 return _errno_to_libusb(errno);
603 itransfer->transferred = req.ucr_actlen;
605 usbi_dbg("transferred %d", itransfer->transferred);
611 _access_endpoint(struct libusb_transfer *transfer)
613 struct handle_priv *hpriv;
614 struct device_priv *dpriv;
615 char *s, devnode[16];
619 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
620 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
622 endpt = UE_GET_ADDR(transfer->endpoint);
623 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
625 usbi_dbg("endpoint %d mode %d", endpt, mode);
627 if (hpriv->endpoints[endpt] < 0) {
628 /* Pick the right node given the control one */
629 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
630 s = strchr(devnode, '.');
631 snprintf(s, 4, ".%02d", endpt);
633 /* We may need to read/write to the same endpoint later. */
634 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
635 if ((fd = open(devnode, mode)) < 0)
638 hpriv->endpoints[endpt] = fd;
641 return (hpriv->endpoints[endpt]);
645 _sync_gen_transfer(struct usbi_transfer *itransfer)
647 struct libusb_transfer *transfer;
650 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
653 * Bulk, Interrupt or Isochronous transfer depends on the
654 * endpoint and thus the node to open.
656 if ((fd = _access_endpoint(transfer)) < 0)
657 return _errno_to_libusb(errno);
659 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
660 return _errno_to_libusb(errno);
662 if (IS_XFERIN(transfer)) {
663 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
664 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
665 return _errno_to_libusb(errno);
667 nr = read(fd, transfer->buffer, transfer->length);
669 nr = write(fd, transfer->buffer, transfer->length);
673 return _errno_to_libusb(errno);
675 itransfer->transferred = nr;