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 handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
218 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
220 dpriv->fd = open(dpriv->devnode, O_RDWR);
222 dpriv->fd = open(dpriv->devnode, O_RDONLY);
224 return _errno_to_libusb(errno);
227 usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
229 return (LIBUSB_SUCCESS);
233 netbsd_close(struct libusb_device_handle *handle)
235 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
236 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
238 usbi_dbg("close: fd %d", dpriv->fd);
245 netbsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
248 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
252 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
256 return (LIBUSB_SUCCESS);
260 netbsd_get_active_config_descriptor(struct libusb_device *dev,
261 unsigned char *buf, size_t len, int *host_endian)
263 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
264 usb_config_descriptor_t *ucd;
266 ucd = (usb_config_descriptor_t *) dpriv->cdesc;
267 len = MIN(len, UGETW(ucd->wTotalLength));
269 usbi_dbg("len %d", len);
271 memcpy(buf, dpriv->cdesc, len);
279 netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
280 unsigned char *buf, size_t len, int *host_endian)
282 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
283 struct usb_full_desc ufd;
286 usbi_dbg("index %d, len %d", idx, len);
288 /* A config descriptor may be requested before opening the device */
289 if (dpriv->fd >= 0) {
292 fd = open(dpriv->devnode, O_RDONLY);
294 return _errno_to_libusb(errno);
297 ufd.ufd_config_index = idx;
301 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
305 return _errno_to_libusb(err);
317 netbsd_get_configuration(struct libusb_device_handle *handle, int *config)
319 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
323 if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
324 return _errno_to_libusb(errno);
326 usbi_dbg("configuration %d", *config);
328 return (LIBUSB_SUCCESS);
332 netbsd_set_configuration(struct libusb_device_handle *handle, int config)
334 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
336 usbi_dbg("configuration %d", config);
338 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
339 return _errno_to_libusb(errno);
341 return _cache_active_config_descriptor(handle->dev, dpriv->fd);
345 netbsd_claim_interface(struct libusb_device_handle *handle, int iface)
347 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
350 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
351 hpriv->endpoints[i] = -1;
353 return (LIBUSB_SUCCESS);
357 netbsd_release_interface(struct libusb_device_handle *handle, int iface)
359 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
362 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
363 if (hpriv->endpoints[i] >= 0)
364 close(hpriv->endpoints[i]);
366 return (LIBUSB_SUCCESS);
370 netbsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
373 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
374 struct usb_alt_interface intf;
376 usbi_dbg("iface %d, setting %d", iface, altsetting);
378 memset(&intf, 0, sizeof(intf));
380 intf.uai_interface_index = iface;
381 intf.uai_alt_no = altsetting;
383 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
384 return _errno_to_libusb(errno);
386 return (LIBUSB_SUCCESS);
390 netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
392 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
393 struct usb_ctl_request req;
397 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
398 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
399 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
400 USETW(req.ucr_request.wIndex, endpoint);
401 USETW(req.ucr_request.wLength, 0);
403 if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
404 return _errno_to_libusb(errno);
406 return (LIBUSB_SUCCESS);
410 netbsd_reset_device(struct libusb_device_handle *handle)
414 return (LIBUSB_ERROR_NOT_SUPPORTED);
418 netbsd_destroy_device(struct libusb_device *dev)
420 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
428 netbsd_submit_transfer(struct usbi_transfer *itransfer)
430 struct libusb_transfer *transfer;
431 struct handle_priv *hpriv;
436 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
437 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
439 switch (transfer->type) {
440 case LIBUSB_TRANSFER_TYPE_CONTROL:
441 err = _sync_control_transfer(itransfer);
443 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
444 if (IS_XFEROUT(transfer)) {
445 /* Isochronous write is not supported */
446 err = LIBUSB_ERROR_NOT_SUPPORTED;
449 err = _sync_gen_transfer(itransfer);
451 case LIBUSB_TRANSFER_TYPE_BULK:
452 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
453 if (IS_XFEROUT(transfer) &&
454 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
455 err = LIBUSB_ERROR_NOT_SUPPORTED;
458 err = _sync_gen_transfer(itransfer);
460 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
461 err = LIBUSB_ERROR_NOT_SUPPORTED;
468 usbi_signal_transfer_completion(itransfer);
470 return (LIBUSB_SUCCESS);
474 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
478 return (LIBUSB_ERROR_NOT_SUPPORTED);
482 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
490 netbsd_handle_transfer_completion(struct usbi_transfer *itransfer)
492 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
496 netbsd_clock_gettime(int clkid, struct timespec *tp)
498 usbi_dbg("clock %d", clkid);
500 if (clkid == USBI_CLOCK_REALTIME)
501 return clock_gettime(CLOCK_REALTIME, tp);
503 if (clkid == USBI_CLOCK_MONOTONIC)
504 return clock_gettime(CLOCK_MONOTONIC, tp);
506 return (LIBUSB_ERROR_INVALID_PARAM);
510 _errno_to_libusb(int err)
514 return (LIBUSB_ERROR_IO);
516 return (LIBUSB_ERROR_ACCESS);
518 return (LIBUSB_ERROR_NO_DEVICE);
520 return (LIBUSB_ERROR_NO_MEM);
523 usbi_dbg("error: %s", strerror(err));
525 return (LIBUSB_ERROR_OTHER);
529 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
531 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
532 struct usb_config_desc ucd;
533 struct usb_full_desc ufd;
537 usbi_dbg("fd %d", fd);
539 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
541 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
542 return _errno_to_libusb(errno);
544 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
546 len = UGETW(ucd.ucd_desc.wTotalLength);
549 return (LIBUSB_ERROR_NO_MEM);
551 ufd.ufd_config_index = ucd.ucd_config_index;
555 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
557 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
559 return _errno_to_libusb(errno);
570 _sync_control_transfer(struct usbi_transfer *itransfer)
572 struct libusb_transfer *transfer;
573 struct libusb_control_setup *setup;
574 struct device_priv *dpriv;
575 struct usb_ctl_request req;
577 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
578 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
579 setup = (struct libusb_control_setup *)transfer->buffer;
581 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
582 setup->bmRequestType, setup->bRequest,
583 libusb_le16_to_cpu(setup->wValue),
584 libusb_le16_to_cpu(setup->wIndex),
585 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
587 req.ucr_request.bmRequestType = setup->bmRequestType;
588 req.ucr_request.bRequest = setup->bRequest;
589 /* Don't use USETW, libusb already deals with the endianness */
590 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
591 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
592 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
593 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
595 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
596 req.ucr_flags = USBD_SHORT_XFER_OK;
598 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
599 return _errno_to_libusb(errno);
601 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
602 return _errno_to_libusb(errno);
604 itransfer->transferred = req.ucr_actlen;
606 usbi_dbg("transferred %d", itransfer->transferred);
612 _access_endpoint(struct libusb_transfer *transfer)
614 struct handle_priv *hpriv;
615 struct device_priv *dpriv;
616 char *s, devnode[16];
620 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
621 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
623 endpt = UE_GET_ADDR(transfer->endpoint);
624 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
626 usbi_dbg("endpoint %d mode %d", endpt, mode);
628 if (hpriv->endpoints[endpt] < 0) {
629 /* Pick the right node given the control one */
630 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
631 s = strchr(devnode, '.');
632 snprintf(s, 4, ".%02d", endpt);
634 /* We may need to read/write to the same endpoint later. */
635 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
636 if ((fd = open(devnode, mode)) < 0)
639 hpriv->endpoints[endpt] = fd;
642 return (hpriv->endpoints[endpt]);
646 _sync_gen_transfer(struct usbi_transfer *itransfer)
648 struct libusb_transfer *transfer;
651 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
654 * Bulk, Interrupt or Isochronous transfer depends on the
655 * endpoint and thus the node to open.
657 if ((fd = _access_endpoint(transfer)) < 0)
658 return _errno_to_libusb(errno);
660 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
661 return _errno_to_libusb(errno);
663 if (IS_XFERIN(transfer)) {
664 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
665 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
666 return _errno_to_libusb(errno);
668 nr = read(fd, transfer->buffer, transfer->length);
670 nr = write(fd, transfer->buffer, transfer->length);
674 return _errno_to_libusb(errno);
676 itransfer->transferred = nr;