Make usbi_get_device_by_session_id return a ref to the found device
[libusbx.git] / libusb / os / netbsd_usb.c
blob1da9e1e178541530ac0be0231f12ffaa99e35bb6
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 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 *,
56 int *);
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,
69 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_events(struct libusb_context *ctx, struct pollfd *,
78 nfds_t, int);
79 static int netbsd_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 netbsd_backend = {
91 "Synchronous NetBSD backend",
93 NULL, /* init() */
94 NULL, /* exit() */
95 netbsd_get_device_list,
96 NULL, /* hotplug_poll */
97 netbsd_open,
98 netbsd_close,
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,
112 netbsd_clear_halt,
113 netbsd_reset_device,
115 NULL, /* kernel_driver_active() */
116 NULL, /* detach_kernel_driver() */
117 NULL, /* attach_kernel_driver() */
119 netbsd_destroy_device,
121 netbsd_submit_transfer,
122 netbsd_cancel_transfer,
123 netbsd_clear_transfer_priv,
125 netbsd_handle_events,
127 netbsd_clock_gettime,
128 sizeof(struct device_priv),
129 sizeof(struct handle_priv),
130 0, /* transfer_priv_size */
131 0, /* add_iso_packet_size */
135 netbsd_get_device_list(struct libusb_context * ctx,
136 struct discovered_devs **discdevs)
138 struct libusb_device *dev;
139 struct device_priv *dpriv;
140 struct usb_device_info di;
141 unsigned long session_id;
142 char devnode[16];
143 int fd, err, i;
145 usbi_dbg("");
147 /* Only ugen(4) is supported */
148 for (i = 0; i < USB_MAX_DEVICES; i++) {
149 /* Control endpoint is always .00 */
150 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
152 if ((fd = open(devnode, O_RDONLY)) < 0) {
153 if (errno != ENOENT && errno != ENXIO)
154 usbi_err(ctx, "could not open %s", devnode);
155 continue;
158 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
159 continue;
161 session_id = (di.udi_bus << 8 | di.udi_addr);
162 dev = usbi_get_device_by_session_id(ctx, session_id);
164 if (dev == NULL) {
165 dev = usbi_alloc_device(ctx, session_id);
166 if (dev == NULL)
167 return (LIBUSB_ERROR_NO_MEM);
169 dev->bus_number = di.udi_bus;
170 dev->device_address = di.udi_addr;
171 dev->speed = di.udi_speed;
173 dpriv = (struct device_priv *)dev->os_priv;
174 strlcpy(dpriv->devnode, devnode, sizeof(devnode));
175 dpriv->fd = -1;
177 if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
178 err = errno;
179 goto error;
182 dpriv->cdesc = NULL;
183 if (_cache_active_config_descriptor(dev, fd)) {
184 err = errno;
185 goto error;
188 if ((err = usbi_sanitize_device(dev)))
189 goto error;
191 close(fd);
193 if (discovered_devs_append(*discdevs, dev) == NULL)
194 return (LIBUSB_ERROR_NO_MEM);
196 libusb_unref_device(dev);
199 return (LIBUSB_SUCCESS);
201 error:
202 close(fd);
203 libusb_unref_device(dev);
204 return _errno_to_libusb(err);
208 netbsd_open(struct libusb_device_handle *handle)
210 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
211 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
213 dpriv->fd = open(dpriv->devnode, O_RDWR);
214 if (dpriv->fd < 0) {
215 dpriv->fd = open(dpriv->devnode, O_RDONLY);
216 if (dpriv->fd < 0)
217 return _errno_to_libusb(errno);
220 usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
222 if (pipe(hpriv->pipe) < 0)
223 return _errno_to_libusb(errno);
225 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
228 void
229 netbsd_close(struct libusb_device_handle *handle)
231 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
232 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
234 usbi_dbg("close: fd %d", dpriv->fd);
236 close(dpriv->fd);
237 dpriv->fd = -1;
239 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
241 close(hpriv->pipe[0]);
242 close(hpriv->pipe[1]);
246 netbsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
247 int *host_endian)
249 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
251 usbi_dbg("");
253 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
255 *host_endian = 0;
257 return (LIBUSB_SUCCESS);
261 netbsd_get_active_config_descriptor(struct libusb_device *dev,
262 unsigned char *buf, size_t len, int *host_endian)
264 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
265 usb_config_descriptor_t *ucd;
267 ucd = (usb_config_descriptor_t *) dpriv->cdesc;
268 len = MIN(len, UGETW(ucd->wTotalLength));
270 usbi_dbg("len %d", len);
272 memcpy(buf, dpriv->cdesc, len);
274 *host_endian = 0;
276 return len;
280 netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
281 unsigned char *buf, size_t len, int *host_endian)
283 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
284 struct usb_full_desc ufd;
285 int fd, err;
287 usbi_dbg("index %d, len %d", idx, len);
289 /* A config descriptor may be requested before opening the device */
290 if (dpriv->fd >= 0) {
291 fd = dpriv->fd;
292 } else {
293 fd = open(dpriv->devnode, O_RDONLY);
294 if (fd < 0)
295 return _errno_to_libusb(errno);
298 ufd.ufd_config_index = idx;
299 ufd.ufd_size = len;
300 ufd.ufd_data = buf;
302 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
303 err = errno;
304 if (dpriv->fd < 0)
305 close(fd);
306 return _errno_to_libusb(err);
309 if (dpriv->fd < 0)
310 close(fd);
312 *host_endian = 0;
314 return len;
318 netbsd_get_configuration(struct libusb_device_handle *handle, int *config)
320 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
322 usbi_dbg("");
324 if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
325 return _errno_to_libusb(errno);
327 usbi_dbg("configuration %d", *config);
329 return (LIBUSB_SUCCESS);
333 netbsd_set_configuration(struct libusb_device_handle *handle, int config)
335 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
337 usbi_dbg("configuration %d", config);
339 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
340 return _errno_to_libusb(errno);
342 return _cache_active_config_descriptor(handle->dev, dpriv->fd);
346 netbsd_claim_interface(struct libusb_device_handle *handle, int iface)
348 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
349 int i;
351 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
352 hpriv->endpoints[i] = -1;
354 return (LIBUSB_SUCCESS);
358 netbsd_release_interface(struct libusb_device_handle *handle, int iface)
360 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
361 int i;
363 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
364 if (hpriv->endpoints[i] >= 0)
365 close(hpriv->endpoints[i]);
367 return (LIBUSB_SUCCESS);
371 netbsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
372 int altsetting)
374 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
375 struct usb_alt_interface intf;
377 usbi_dbg("iface %d, setting %d", iface, altsetting);
379 memset(&intf, 0, sizeof(intf));
381 intf.uai_interface_index = iface;
382 intf.uai_alt_no = altsetting;
384 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
385 return _errno_to_libusb(errno);
387 return (LIBUSB_SUCCESS);
391 netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
393 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
394 struct usb_ctl_request req;
396 usbi_dbg("");
398 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
399 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
400 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
401 USETW(req.ucr_request.wIndex, endpoint);
402 USETW(req.ucr_request.wLength, 0);
404 if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
405 return _errno_to_libusb(errno);
407 return (LIBUSB_SUCCESS);
411 netbsd_reset_device(struct libusb_device_handle *handle)
413 usbi_dbg("");
415 return (LIBUSB_ERROR_NOT_SUPPORTED);
418 void
419 netbsd_destroy_device(struct libusb_device *dev)
421 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
423 usbi_dbg("");
425 free(dpriv->cdesc);
429 netbsd_submit_transfer(struct usbi_transfer *itransfer)
431 struct libusb_transfer *transfer;
432 struct handle_priv *hpriv;
433 int err = 0;
435 usbi_dbg("");
437 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
438 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
440 switch (transfer->type) {
441 case LIBUSB_TRANSFER_TYPE_CONTROL:
442 err = _sync_control_transfer(itransfer);
443 break;
444 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
445 if (IS_XFEROUT(transfer)) {
446 /* Isochronous write is not supported */
447 err = LIBUSB_ERROR_NOT_SUPPORTED;
448 break;
450 err = _sync_gen_transfer(itransfer);
451 break;
452 case LIBUSB_TRANSFER_TYPE_BULK:
453 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
454 if (IS_XFEROUT(transfer) &&
455 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
456 err = LIBUSB_ERROR_NOT_SUPPORTED;
457 break;
459 err = _sync_gen_transfer(itransfer);
460 break;
463 if (err)
464 return (err);
466 if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
467 return _errno_to_libusb(errno);
469 return (LIBUSB_SUCCESS);
473 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
475 usbi_dbg("");
477 return (LIBUSB_ERROR_NOT_SUPPORTED);
480 void
481 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
483 usbi_dbg("");
485 /* Nothing to do */
489 netbsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
490 int num_ready)
492 struct libusb_device_handle *handle;
493 struct handle_priv *hpriv = NULL;
494 struct usbi_transfer *itransfer;
495 struct pollfd *pollfd;
496 int i, err = 0;
498 usbi_dbg("");
500 pthread_mutex_lock(&ctx->open_devs_lock);
501 for (i = 0; i < nfds && num_ready > 0; i++) {
502 pollfd = &fds[i];
504 if (!pollfd->revents)
505 continue;
507 hpriv = NULL;
508 num_ready--;
509 list_for_each_entry(handle, &ctx->open_devs, list,
510 struct libusb_device_handle) {
511 hpriv = (struct handle_priv *)handle->os_priv;
513 if (hpriv->pipe[0] == pollfd->fd)
514 break;
516 hpriv = NULL;
519 if (NULL == hpriv) {
520 usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
521 err = ENOENT;
522 break;
525 if (pollfd->revents & POLLERR) {
526 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
527 usbi_handle_disconnect(handle);
528 continue;
531 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
532 err = errno;
533 break;
536 if ((err = usbi_handle_transfer_completion(itransfer,
537 LIBUSB_TRANSFER_COMPLETED)))
538 break;
540 pthread_mutex_unlock(&ctx->open_devs_lock);
542 if (err)
543 return _errno_to_libusb(err);
545 return (LIBUSB_SUCCESS);
549 netbsd_clock_gettime(int clkid, struct timespec *tp)
551 usbi_dbg("clock %d", clkid);
553 if (clkid == USBI_CLOCK_REALTIME)
554 return clock_gettime(CLOCK_REALTIME, tp);
556 if (clkid == USBI_CLOCK_MONOTONIC)
557 return clock_gettime(CLOCK_MONOTONIC, tp);
559 return (LIBUSB_ERROR_INVALID_PARAM);
563 _errno_to_libusb(int err)
565 switch (err) {
566 case EIO:
567 return (LIBUSB_ERROR_IO);
568 case EACCES:
569 return (LIBUSB_ERROR_ACCESS);
570 case ENOENT:
571 return (LIBUSB_ERROR_NO_DEVICE);
572 case ENOMEM:
573 return (LIBUSB_ERROR_NO_MEM);
576 usbi_dbg("error: %s", strerror(err));
578 return (LIBUSB_ERROR_OTHER);
582 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
584 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
585 struct usb_config_desc ucd;
586 struct usb_full_desc ufd;
587 unsigned char* buf;
588 int len;
590 usbi_dbg("fd %d", fd);
592 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
594 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
595 return _errno_to_libusb(errno);
597 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
599 len = UGETW(ucd.ucd_desc.wTotalLength);
600 buf = malloc(len);
601 if (buf == NULL)
602 return (LIBUSB_ERROR_NO_MEM);
604 ufd.ufd_config_index = ucd.ucd_config_index;
605 ufd.ufd_size = len;
606 ufd.ufd_data = buf;
608 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
610 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
611 free(buf);
612 return _errno_to_libusb(errno);
615 if (dpriv->cdesc)
616 free(dpriv->cdesc);
617 dpriv->cdesc = buf;
619 return (0);
623 _sync_control_transfer(struct usbi_transfer *itransfer)
625 struct libusb_transfer *transfer;
626 struct libusb_control_setup *setup;
627 struct device_priv *dpriv;
628 struct usb_ctl_request req;
630 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
631 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
632 setup = (struct libusb_control_setup *)transfer->buffer;
634 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
635 setup->bmRequestType, setup->bRequest,
636 libusb_le16_to_cpu(setup->wValue),
637 libusb_le16_to_cpu(setup->wIndex),
638 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
640 req.ucr_request.bmRequestType = setup->bmRequestType;
641 req.ucr_request.bRequest = setup->bRequest;
642 /* Don't use USETW, libusbx already deals with the endianness */
643 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
644 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
645 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
646 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
648 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
649 req.ucr_flags = USBD_SHORT_XFER_OK;
651 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
652 return _errno_to_libusb(errno);
654 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
655 return _errno_to_libusb(errno);
657 itransfer->transferred = req.ucr_actlen;
659 usbi_dbg("transferred %d", itransfer->transferred);
661 return (0);
665 _access_endpoint(struct libusb_transfer *transfer)
667 struct handle_priv *hpriv;
668 struct device_priv *dpriv;
669 char *s, devnode[16];
670 int fd, endpt;
671 mode_t mode;
673 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
674 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
676 endpt = UE_GET_ADDR(transfer->endpoint);
677 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
679 usbi_dbg("endpoint %d mode %d", endpt, mode);
681 if (hpriv->endpoints[endpt] < 0) {
682 /* Pick the right node given the control one */
683 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
684 s = strchr(devnode, '.');
685 snprintf(s, 4, ".%02d", endpt);
687 /* We may need to read/write to the same endpoint later. */
688 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
689 if ((fd = open(devnode, mode)) < 0)
690 return (-1);
692 hpriv->endpoints[endpt] = fd;
695 return (hpriv->endpoints[endpt]);
699 _sync_gen_transfer(struct usbi_transfer *itransfer)
701 struct libusb_transfer *transfer;
702 int fd, nr = 1;
704 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
707 * Bulk, Interrupt or Isochronous transfer depends on the
708 * endpoint and thus the node to open.
710 if ((fd = _access_endpoint(transfer)) < 0)
711 return _errno_to_libusb(errno);
713 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
714 return _errno_to_libusb(errno);
716 if (IS_XFERIN(transfer)) {
717 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
718 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
719 return _errno_to_libusb(errno);
721 nr = read(fd, transfer->buffer, transfer->length);
722 } else {
723 nr = write(fd, transfer->buffer, transfer->length);
726 if (nr < 0)
727 return _errno_to_libusb(errno);
729 itransfer->transferred = nr;
731 return (0);