configure: Fix --enable-udev help text
[libusbx.git] / libusb / os / openbsd_usb.c
blob353385b889f3886be0e3e18c74c1645cd37e8b3b
1 /*
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
19 #include <sys/time.h>
20 #include <sys/types.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include <dev/usb/usb.h>
31 #include "libusb.h"
32 #include "libusbi.h"
34 struct device_priv {
35 char devnode[16];
36 int fd;
38 unsigned char *cdesc; /* active config descriptor */
39 usb_device_descriptor_t ddesc; /* usb device descriptor */
42 struct handle_priv {
43 int pipe[2]; /* for event notification */
44 int endpoints[USB_MAX_ENDPOINTS];
48 * Backend functions
50 static int obsd_get_device_list(struct libusb_context *,
51 struct discovered_devs **);
52 static int obsd_open(struct libusb_device_handle *);
53 static void obsd_close(struct libusb_device_handle *);
55 static int obsd_get_device_descriptor(struct libusb_device *, unsigned char *,
56 int *);
57 static int obsd_get_active_config_descriptor(struct libusb_device *,
58 unsigned char *, size_t, int *);
59 static int obsd_get_config_descriptor(struct libusb_device *, uint8_t,
60 unsigned char *, size_t, int *);
62 static int obsd_get_configuration(struct libusb_device_handle *, int *);
63 static int obsd_set_configuration(struct libusb_device_handle *, int);
65 static int obsd_claim_interface(struct libusb_device_handle *, int);
66 static int obsd_release_interface(struct libusb_device_handle *, int);
68 static int obsd_set_interface_altsetting(struct libusb_device_handle *, int,
69 int);
70 static int obsd_clear_halt(struct libusb_device_handle *, unsigned char);
71 static int obsd_reset_device(struct libusb_device_handle *);
72 static void obsd_destroy_device(struct libusb_device *);
74 static int obsd_submit_transfer(struct usbi_transfer *);
75 static int obsd_cancel_transfer(struct usbi_transfer *);
76 static void obsd_clear_transfer_priv(struct usbi_transfer *);
77 static int obsd_handle_events(struct libusb_context *ctx, struct pollfd *,
78 nfds_t, int);
79 static int obsd_clock_gettime(int, struct timespec *);
82 * Private functions
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 openbsd_backend = {
91 "Synchronous OpenBSD backend",
93 NULL, /* init() */
94 NULL, /* exit() */
95 obsd_get_device_list,
96 obsd_open,
97 obsd_close,
99 obsd_get_device_descriptor,
100 obsd_get_active_config_descriptor,
101 obsd_get_config_descriptor,
103 obsd_get_configuration,
104 obsd_set_configuration,
106 obsd_claim_interface,
107 obsd_release_interface,
109 obsd_set_interface_altsetting,
110 obsd_clear_halt,
111 obsd_reset_device,
113 NULL, /* kernel_driver_active() */
114 NULL, /* detach_kernel_driver() */
115 NULL, /* attach_kernel_driver() */
117 obsd_destroy_device,
119 obsd_submit_transfer,
120 obsd_cancel_transfer,
121 obsd_clear_transfer_priv,
123 obsd_handle_events,
125 obsd_clock_gettime,
126 sizeof(struct device_priv),
127 sizeof(struct handle_priv),
128 0, /* transfer_priv_size */
129 0, /* add_iso_packet_size */
133 obsd_get_device_list(struct libusb_context * ctx,
134 struct discovered_devs **discdevs)
136 struct libusb_device *dev;
137 struct device_priv *dpriv;
138 struct usb_device_info di;
139 unsigned long session_id;
140 char devnode[16];
141 int fd, err, i;
143 usbi_dbg("");
145 /* Only ugen(4) is supported */
146 for (i = 0; i < USB_MAX_DEVICES; i++) {
147 /* Control endpoint is always .00 */
148 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
150 if ((fd = open(devnode, O_RDONLY)) < 0) {
151 if (errno != ENOENT && errno != ENXIO)
152 usbi_err(ctx, "could not open %s", devnode);
153 continue;
156 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
157 continue;
159 session_id = (di.udi_bus << 8 | di.udi_addr);
160 dev = usbi_get_device_by_session_id(ctx, session_id);
162 if (dev == NULL) {
163 dev = usbi_alloc_device(ctx, session_id);
164 if (dev == NULL)
165 return (LIBUSB_ERROR_NO_MEM);
167 dev->bus_number = di.udi_bus;
168 dev->device_address = di.udi_addr;
169 dev->speed = di.udi_speed;
171 dpriv = (struct device_priv *)dev->os_priv;
172 strlcpy(dpriv->devnode, devnode, sizeof(devnode));
173 dpriv->fd = -1;
175 if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
176 err = errno;
177 goto error;
180 dpriv->cdesc = NULL;
181 if (_cache_active_config_descriptor(dev, fd)) {
182 err = errno;
183 goto error;
186 if ((err = usbi_sanitize_device(dev)))
187 goto error;
189 close(fd);
191 if (discovered_devs_append(*discdevs, dev) == NULL)
192 return (LIBUSB_ERROR_NO_MEM);
195 return (LIBUSB_SUCCESS);
197 error:
198 close(fd);
199 libusb_unref_device(dev);
200 return _errno_to_libusb(err);
204 obsd_open(struct libusb_device_handle *handle)
206 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
207 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
209 dpriv->fd = open(dpriv->devnode, O_RDWR);
210 if (dpriv->fd < 0) {
211 dpriv->fd = open(dpriv->devnode, O_RDONLY);
212 if (dpriv->fd < 0)
213 return _errno_to_libusb(errno);
216 usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
218 if (pipe(hpriv->pipe) < 0)
219 return _errno_to_libusb(errno);
221 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
224 void
225 obsd_close(struct libusb_device_handle *handle)
227 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
228 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
230 usbi_dbg("close: fd %d", dpriv->fd);
232 close(dpriv->fd);
233 dpriv->fd = -1;
235 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
237 close(hpriv->pipe[0]);
238 close(hpriv->pipe[1]);
242 obsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
243 int *host_endian)
245 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
247 usbi_dbg("");
249 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
251 *host_endian = 0;
253 return (LIBUSB_SUCCESS);
257 obsd_get_active_config_descriptor(struct libusb_device *dev,
258 unsigned char *buf, size_t len, int *host_endian)
260 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
261 usb_config_descriptor_t *ucd;
263 ucd = (usb_config_descriptor_t *) dpriv->cdesc;
264 len = MIN(len, UGETW(ucd->wTotalLength));
266 usbi_dbg("len %d", len);
268 memcpy(buf, dpriv->cdesc, len);
270 *host_endian = 0;
272 return (LIBUSB_SUCCESS);
276 obsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
277 unsigned char *buf, size_t len, int *host_endian)
279 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
280 struct usb_full_desc ufd;
281 int fd, err;
283 usbi_dbg("index %d, len %d", idx, len);
285 /* A config descriptor may be requested before opening the device */
286 if (dpriv->fd >= 0) {
287 fd = dpriv->fd;
288 } else {
289 fd = open(dpriv->devnode, O_RDONLY);
290 if (fd < 0)
291 return _errno_to_libusb(errno);
294 ufd.ufd_config_index = idx;
295 ufd.ufd_size = len;
296 ufd.ufd_data = buf;
298 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
299 err = errno;
300 if (dpriv->fd < 0)
301 close(fd);
302 return _errno_to_libusb(err);
305 if (dpriv->fd < 0)
306 close(fd);
308 *host_endian = 0;
310 return (LIBUSB_SUCCESS);
314 obsd_get_configuration(struct libusb_device_handle *handle, int *config)
316 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
318 usbi_dbg("");
320 if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
321 return _errno_to_libusb(errno);
323 usbi_dbg("configuration %d", *config);
325 return (LIBUSB_SUCCESS);
329 obsd_set_configuration(struct libusb_device_handle *handle, int config)
331 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
333 usbi_dbg("configuration %d", config);
335 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
336 return _errno_to_libusb(errno);
338 return _cache_active_config_descriptor(handle->dev, dpriv->fd);
342 obsd_claim_interface(struct libusb_device_handle *handle, int iface)
344 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
345 int i;
347 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
348 hpriv->endpoints[i] = -1;
350 return (LIBUSB_SUCCESS);
354 obsd_release_interface(struct libusb_device_handle *handle, int iface)
356 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
357 int i;
359 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
360 if (hpriv->endpoints[i] >= 0)
361 close(hpriv->endpoints[i]);
363 return (LIBUSB_SUCCESS);
367 obsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
368 int altsetting)
370 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
371 struct usb_alt_interface intf;
373 usbi_dbg("iface %d, setting %d", iface, altsetting);
375 memset(&intf, 0, sizeof(intf));
377 intf.uai_interface_index = iface;
378 intf.uai_alt_no = altsetting;
380 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
381 return _errno_to_libusb(errno);
383 return (LIBUSB_SUCCESS);
387 obsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
389 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
390 struct usb_ctl_request req;
392 usbi_dbg("");
394 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
395 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
396 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
397 USETW(req.ucr_request.wIndex, endpoint);
398 USETW(req.ucr_request.wLength, 0);
400 if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
401 return _errno_to_libusb(errno);
403 return (LIBUSB_SUCCESS);
407 obsd_reset_device(struct libusb_device_handle *handle)
409 usbi_dbg("");
411 return (LIBUSB_ERROR_NOT_SUPPORTED);
414 void
415 obsd_destroy_device(struct libusb_device *dev)
417 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
419 usbi_dbg("");
421 free(dpriv->cdesc);
425 obsd_submit_transfer(struct usbi_transfer *itransfer)
427 struct libusb_transfer *transfer;
428 struct handle_priv *hpriv;
429 int err = 0;
431 usbi_dbg("");
433 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
434 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
436 switch (transfer->type) {
437 case LIBUSB_TRANSFER_TYPE_CONTROL:
438 err = _sync_control_transfer(itransfer);
439 break;
440 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
441 if (IS_XFEROUT(transfer)) {
442 /* Isochronous write is not supported */
443 err = LIBUSB_ERROR_NOT_SUPPORTED;
444 break;
446 err = _sync_gen_transfer(itransfer);
447 break;
448 case LIBUSB_TRANSFER_TYPE_BULK:
449 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
450 if (IS_XFEROUT(transfer) &&
451 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
452 err = LIBUSB_ERROR_NOT_SUPPORTED;
453 break;
455 err = _sync_gen_transfer(itransfer);
456 break;
459 if (err)
460 return (err);
462 if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
463 return _errno_to_libusb(errno);
465 return (LIBUSB_SUCCESS);
469 obsd_cancel_transfer(struct usbi_transfer *itransfer)
471 usbi_dbg("");
473 return (LIBUSB_ERROR_NOT_SUPPORTED);
476 void
477 obsd_clear_transfer_priv(struct usbi_transfer *itransfer)
479 usbi_dbg("");
481 /* Nothing to do */
485 obsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
486 int num_ready)
488 struct libusb_device_handle *handle;
489 struct handle_priv *hpriv = NULL;
490 struct usbi_transfer *itransfer;
491 struct pollfd *pollfd;
492 int i, err = 0;
494 usbi_dbg("");
496 pthread_mutex_lock(&ctx->open_devs_lock);
497 for (i = 0; i < nfds && num_ready > 0; i++) {
498 pollfd = &fds[i];
500 if (!pollfd->revents)
501 continue;
503 hpriv = NULL;
504 num_ready--;
505 list_for_each_entry(handle, &ctx->open_devs, list,
506 struct libusb_device_handle) {
507 hpriv = (struct handle_priv *)handle->os_priv;
509 if (hpriv->pipe[0] == pollfd->fd)
510 break;
512 hpriv = NULL;
515 if (NULL == hpriv) {
516 usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
517 err = ENOENT;
518 break;
521 if (pollfd->revents & POLLERR) {
522 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
523 usbi_handle_disconnect(handle);
524 continue;
527 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
528 err = errno;
529 break;
532 if ((err = usbi_handle_transfer_completion(itransfer,
533 LIBUSB_TRANSFER_COMPLETED)))
534 break;
536 pthread_mutex_unlock(&ctx->open_devs_lock);
538 if (err)
539 return _errno_to_libusb(err);
541 return (LIBUSB_SUCCESS);
545 obsd_clock_gettime(int clkid, struct timespec *tp)
547 usbi_dbg("clock %d", clkid);
549 if (clkid == USBI_CLOCK_REALTIME)
550 return clock_gettime(CLOCK_REALTIME, tp);
552 if (clkid == USBI_CLOCK_MONOTONIC)
553 return clock_gettime(CLOCK_MONOTONIC, tp);
555 return (LIBUSB_ERROR_INVALID_PARAM);
559 _errno_to_libusb(int err)
561 switch (err) {
562 case EIO:
563 return (LIBUSB_ERROR_IO);
564 case EACCES:
565 return (LIBUSB_ERROR_ACCESS);
566 case ENOENT:
567 return (LIBUSB_ERROR_NO_DEVICE);
568 case ENOMEM:
569 return (LIBUSB_ERROR_NO_MEM);
572 usbi_dbg("error: %s", strerror(err));
574 return (LIBUSB_ERROR_OTHER);
578 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
580 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
581 struct usb_config_desc ucd;
582 struct usb_full_desc ufd;
583 unsigned char* buf;
584 int len;
586 usbi_dbg("fd %d", fd);
588 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
590 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
591 return _errno_to_libusb(errno);
593 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
595 len = UGETW(ucd.ucd_desc.wTotalLength);
596 buf = malloc(len);
597 if (buf == NULL)
598 return (LIBUSB_ERROR_NO_MEM);
600 ufd.ufd_config_index = ucd.ucd_config_index;
601 ufd.ufd_size = len;
602 ufd.ufd_data = buf;
604 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
606 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
607 free(buf);
608 return _errno_to_libusb(errno);
611 if (dpriv->cdesc)
612 free(dpriv->cdesc);
613 dpriv->cdesc = buf;
615 return (0);
619 _sync_control_transfer(struct usbi_transfer *itransfer)
621 struct libusb_transfer *transfer;
622 struct libusb_control_setup *setup;
623 struct device_priv *dpriv;
624 struct usb_ctl_request req;
626 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
627 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
628 setup = (struct libusb_control_setup *)transfer->buffer;
630 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
631 setup->bmRequestType, setup->bRequest,
632 libusb_le16_to_cpu(setup->wValue),
633 libusb_le16_to_cpu(setup->wIndex),
634 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
636 req.ucr_request.bmRequestType = setup->bmRequestType;
637 req.ucr_request.bRequest = setup->bRequest;
638 /* Don't use USETW, libusbx already deals with the endianness */
639 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
640 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
641 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
642 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
644 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
645 req.ucr_flags = USBD_SHORT_XFER_OK;
647 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
648 return _errno_to_libusb(errno);
650 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
651 return _errno_to_libusb(errno);
653 itransfer->transferred = req.ucr_actlen;
655 usbi_dbg("transferred %d", itransfer->transferred);
657 return (0);
661 _access_endpoint(struct libusb_transfer *transfer)
663 struct handle_priv *hpriv;
664 struct device_priv *dpriv;
665 char *s, devnode[16];
666 int fd, endpt;
667 mode_t mode;
669 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
670 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
672 endpt = UE_GET_ADDR(transfer->endpoint);
673 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
675 usbi_dbg("endpoint %d mode %d", endpt, mode);
677 if (hpriv->endpoints[endpt] < 0) {
678 /* Pick the right node given the control one */
679 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
680 s = strchr(devnode, '.');
681 snprintf(s, 4, ".%02d", endpt);
683 /* We may need to read/write to the same endpoint later. */
684 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
685 if ((fd = open(devnode, mode)) < 0)
686 return (-1);
688 hpriv->endpoints[endpt] = fd;
691 return (hpriv->endpoints[endpt]);
695 _sync_gen_transfer(struct usbi_transfer *itransfer)
697 struct libusb_transfer *transfer;
698 int fd, nr = 1;
700 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
703 * Bulk, Interrupt or Isochronous transfer depends on the
704 * endpoint and thus the node to open.
706 if ((fd = _access_endpoint(transfer)) < 0)
707 return _errno_to_libusb(errno);
709 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
710 return _errno_to_libusb(errno);
712 if (IS_XFERIN(transfer)) {
713 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
714 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
715 return _errno_to_libusb(errno);
717 nr = read(fd, transfer->buffer, transfer->length);
718 } else {
719 nr = write(fd, transfer->buffer, transfer->length);
722 if (nr < 0)
723 return _errno_to_libusb(errno);
725 itransfer->transferred = nr;
727 return (0);