openbsd: Fix memleak in obsd_get_device_list()
[libusbx.git] / libusb / os / openbsd_usb.c
blobf4fd4543a63661a33dc0814178ff4dde76e7113e
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 NULL, /* hotplug_poll */
97 obsd_open,
98 obsd_close,
100 obsd_get_device_descriptor,
101 obsd_get_active_config_descriptor,
102 obsd_get_config_descriptor,
103 NULL, /* get_config_descriptor_by_value() */
105 obsd_get_configuration,
106 obsd_set_configuration,
108 obsd_claim_interface,
109 obsd_release_interface,
111 obsd_set_interface_altsetting,
112 obsd_clear_halt,
113 obsd_reset_device,
115 NULL, /* kernel_driver_active() */
116 NULL, /* detach_kernel_driver() */
117 NULL, /* attach_kernel_driver() */
119 obsd_destroy_device,
121 obsd_submit_transfer,
122 obsd_cancel_transfer,
123 obsd_clear_transfer_priv,
125 obsd_handle_events,
127 obsd_clock_gettime,
128 sizeof(struct device_priv),
129 sizeof(struct handle_priv),
130 0, /* transfer_priv_size */
131 0, /* add_iso_packet_size */
135 obsd_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) {
165 dev = libusb_ref_device(dev);
166 } else {
167 dev = usbi_alloc_device(ctx, session_id);
168 if (dev == NULL)
169 return (LIBUSB_ERROR_NO_MEM);
171 dev->bus_number = di.udi_bus;
172 dev->device_address = di.udi_addr;
173 dev->speed = di.udi_speed;
175 dpriv = (struct device_priv *)dev->os_priv;
176 strlcpy(dpriv->devnode, devnode, sizeof(devnode));
177 dpriv->fd = -1;
179 if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
180 err = errno;
181 goto error;
184 dpriv->cdesc = NULL;
185 if (_cache_active_config_descriptor(dev, fd)) {
186 err = errno;
187 goto error;
190 if ((err = usbi_sanitize_device(dev)))
191 goto error;
193 close(fd);
195 if (discovered_devs_append(*discdevs, dev) == NULL)
196 return (LIBUSB_ERROR_NO_MEM);
198 libusb_unref_device(dev);
201 return (LIBUSB_SUCCESS);
203 error:
204 close(fd);
205 libusb_unref_device(dev);
206 return _errno_to_libusb(err);
210 obsd_open(struct libusb_device_handle *handle)
212 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
213 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
215 dpriv->fd = open(dpriv->devnode, O_RDWR);
216 if (dpriv->fd < 0) {
217 dpriv->fd = open(dpriv->devnode, O_RDONLY);
218 if (dpriv->fd < 0)
219 return _errno_to_libusb(errno);
222 usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
224 if (pipe(hpriv->pipe) < 0)
225 return _errno_to_libusb(errno);
227 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
230 void
231 obsd_close(struct libusb_device_handle *handle)
233 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
234 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
236 usbi_dbg("close: fd %d", dpriv->fd);
238 close(dpriv->fd);
239 dpriv->fd = -1;
241 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
243 close(hpriv->pipe[0]);
244 close(hpriv->pipe[1]);
248 obsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
249 int *host_endian)
251 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
253 usbi_dbg("");
255 memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
257 *host_endian = 0;
259 return (LIBUSB_SUCCESS);
263 obsd_get_active_config_descriptor(struct libusb_device *dev,
264 unsigned char *buf, size_t len, int *host_endian)
266 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
267 usb_config_descriptor_t *ucd;
269 ucd = (usb_config_descriptor_t *) dpriv->cdesc;
270 len = MIN(len, UGETW(ucd->wTotalLength));
272 usbi_dbg("len %d", len);
274 memcpy(buf, dpriv->cdesc, len);
276 *host_endian = 0;
278 return len;
282 obsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
283 unsigned char *buf, size_t len, int *host_endian)
285 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
286 struct usb_full_desc ufd;
287 int fd, err;
289 usbi_dbg("index %d, len %d", idx, len);
291 /* A config descriptor may be requested before opening the device */
292 if (dpriv->fd >= 0) {
293 fd = dpriv->fd;
294 } else {
295 fd = open(dpriv->devnode, O_RDONLY);
296 if (fd < 0)
297 return _errno_to_libusb(errno);
300 ufd.ufd_config_index = idx;
301 ufd.ufd_size = len;
302 ufd.ufd_data = buf;
304 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
305 err = errno;
306 if (dpriv->fd < 0)
307 close(fd);
308 return _errno_to_libusb(err);
311 if (dpriv->fd < 0)
312 close(fd);
314 *host_endian = 0;
316 return len;
320 obsd_get_configuration(struct libusb_device_handle *handle, int *config)
322 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
324 usbi_dbg("");
326 if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
327 return _errno_to_libusb(errno);
329 usbi_dbg("configuration %d", *config);
331 return (LIBUSB_SUCCESS);
335 obsd_set_configuration(struct libusb_device_handle *handle, int config)
337 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
339 usbi_dbg("configuration %d", config);
341 if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
342 return _errno_to_libusb(errno);
344 return _cache_active_config_descriptor(handle->dev, dpriv->fd);
348 obsd_claim_interface(struct libusb_device_handle *handle, int iface)
350 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
351 int i;
353 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
354 hpriv->endpoints[i] = -1;
356 return (LIBUSB_SUCCESS);
360 obsd_release_interface(struct libusb_device_handle *handle, int iface)
362 struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
363 int i;
365 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
366 if (hpriv->endpoints[i] >= 0)
367 close(hpriv->endpoints[i]);
369 return (LIBUSB_SUCCESS);
373 obsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
374 int altsetting)
376 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
377 struct usb_alt_interface intf;
379 usbi_dbg("iface %d, setting %d", iface, altsetting);
381 memset(&intf, 0, sizeof(intf));
383 intf.uai_interface_index = iface;
384 intf.uai_alt_no = altsetting;
386 if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
387 return _errno_to_libusb(errno);
389 return (LIBUSB_SUCCESS);
393 obsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
395 struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
396 struct usb_ctl_request req;
398 usbi_dbg("");
400 req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
401 req.ucr_request.bRequest = UR_CLEAR_FEATURE;
402 USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
403 USETW(req.ucr_request.wIndex, endpoint);
404 USETW(req.ucr_request.wLength, 0);
406 if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
407 return _errno_to_libusb(errno);
409 return (LIBUSB_SUCCESS);
413 obsd_reset_device(struct libusb_device_handle *handle)
415 usbi_dbg("");
417 return (LIBUSB_ERROR_NOT_SUPPORTED);
420 void
421 obsd_destroy_device(struct libusb_device *dev)
423 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
425 usbi_dbg("");
427 free(dpriv->cdesc);
431 obsd_submit_transfer(struct usbi_transfer *itransfer)
433 struct libusb_transfer *transfer;
434 struct handle_priv *hpriv;
435 int err = 0;
437 usbi_dbg("");
439 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
440 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
442 switch (transfer->type) {
443 case LIBUSB_TRANSFER_TYPE_CONTROL:
444 err = _sync_control_transfer(itransfer);
445 break;
446 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
447 if (IS_XFEROUT(transfer)) {
448 /* Isochronous write is not supported */
449 err = LIBUSB_ERROR_NOT_SUPPORTED;
450 break;
452 err = _sync_gen_transfer(itransfer);
453 break;
454 case LIBUSB_TRANSFER_TYPE_BULK:
455 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
456 if (IS_XFEROUT(transfer) &&
457 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
458 err = LIBUSB_ERROR_NOT_SUPPORTED;
459 break;
461 err = _sync_gen_transfer(itransfer);
462 break;
465 if (err)
466 return (err);
468 if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
469 return _errno_to_libusb(errno);
471 return (LIBUSB_SUCCESS);
475 obsd_cancel_transfer(struct usbi_transfer *itransfer)
477 usbi_dbg("");
479 return (LIBUSB_ERROR_NOT_SUPPORTED);
482 void
483 obsd_clear_transfer_priv(struct usbi_transfer *itransfer)
485 usbi_dbg("");
487 /* Nothing to do */
491 obsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
492 int num_ready)
494 struct libusb_device_handle *handle;
495 struct handle_priv *hpriv = NULL;
496 struct usbi_transfer *itransfer;
497 struct pollfd *pollfd;
498 int i, err = 0;
500 usbi_dbg("");
502 pthread_mutex_lock(&ctx->open_devs_lock);
503 for (i = 0; i < nfds && num_ready > 0; i++) {
504 pollfd = &fds[i];
506 if (!pollfd->revents)
507 continue;
509 hpriv = NULL;
510 num_ready--;
511 list_for_each_entry(handle, &ctx->open_devs, list,
512 struct libusb_device_handle) {
513 hpriv = (struct handle_priv *)handle->os_priv;
515 if (hpriv->pipe[0] == pollfd->fd)
516 break;
518 hpriv = NULL;
521 if (NULL == hpriv) {
522 usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
523 err = ENOENT;
524 break;
527 if (pollfd->revents & POLLERR) {
528 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
529 usbi_handle_disconnect(handle);
530 continue;
533 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
534 err = errno;
535 break;
538 if ((err = usbi_handle_transfer_completion(itransfer,
539 LIBUSB_TRANSFER_COMPLETED)))
540 break;
542 pthread_mutex_unlock(&ctx->open_devs_lock);
544 if (err)
545 return _errno_to_libusb(err);
547 return (LIBUSB_SUCCESS);
551 obsd_clock_gettime(int clkid, struct timespec *tp)
553 usbi_dbg("clock %d", clkid);
555 if (clkid == USBI_CLOCK_REALTIME)
556 return clock_gettime(CLOCK_REALTIME, tp);
558 if (clkid == USBI_CLOCK_MONOTONIC)
559 return clock_gettime(CLOCK_MONOTONIC, tp);
561 return (LIBUSB_ERROR_INVALID_PARAM);
565 _errno_to_libusb(int err)
567 switch (err) {
568 case EIO:
569 return (LIBUSB_ERROR_IO);
570 case EACCES:
571 return (LIBUSB_ERROR_ACCESS);
572 case ENOENT:
573 return (LIBUSB_ERROR_NO_DEVICE);
574 case ENOMEM:
575 return (LIBUSB_ERROR_NO_MEM);
578 usbi_dbg("error: %s", strerror(err));
580 return (LIBUSB_ERROR_OTHER);
584 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
586 struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
587 struct usb_config_desc ucd;
588 struct usb_full_desc ufd;
589 unsigned char* buf;
590 int len;
592 usbi_dbg("fd %d", fd);
594 ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
596 if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
597 return _errno_to_libusb(errno);
599 usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
601 len = UGETW(ucd.ucd_desc.wTotalLength);
602 buf = malloc(len);
603 if (buf == NULL)
604 return (LIBUSB_ERROR_NO_MEM);
606 ufd.ufd_config_index = ucd.ucd_config_index;
607 ufd.ufd_size = len;
608 ufd.ufd_data = buf;
610 usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
612 if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
613 free(buf);
614 return _errno_to_libusb(errno);
617 if (dpriv->cdesc)
618 free(dpriv->cdesc);
619 dpriv->cdesc = buf;
621 return (0);
625 _sync_control_transfer(struct usbi_transfer *itransfer)
627 struct libusb_transfer *transfer;
628 struct libusb_control_setup *setup;
629 struct device_priv *dpriv;
630 struct usb_ctl_request req;
632 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
633 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
634 setup = (struct libusb_control_setup *)transfer->buffer;
636 usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
637 setup->bmRequestType, setup->bRequest,
638 libusb_le16_to_cpu(setup->wValue),
639 libusb_le16_to_cpu(setup->wIndex),
640 libusb_le16_to_cpu(setup->wLength), transfer->timeout);
642 req.ucr_request.bmRequestType = setup->bmRequestType;
643 req.ucr_request.bRequest = setup->bRequest;
644 /* Don't use USETW, libusbx already deals with the endianness */
645 (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
646 (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
647 (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
648 req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
650 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
651 req.ucr_flags = USBD_SHORT_XFER_OK;
653 if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
654 return _errno_to_libusb(errno);
656 if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
657 return _errno_to_libusb(errno);
659 itransfer->transferred = req.ucr_actlen;
661 usbi_dbg("transferred %d", itransfer->transferred);
663 return (0);
667 _access_endpoint(struct libusb_transfer *transfer)
669 struct handle_priv *hpriv;
670 struct device_priv *dpriv;
671 char *s, devnode[16];
672 int fd, endpt;
673 mode_t mode;
675 hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
676 dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
678 endpt = UE_GET_ADDR(transfer->endpoint);
679 mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
681 usbi_dbg("endpoint %d mode %d", endpt, mode);
683 if (hpriv->endpoints[endpt] < 0) {
684 /* Pick the right node given the control one */
685 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
686 s = strchr(devnode, '.');
687 snprintf(s, 4, ".%02d", endpt);
689 /* We may need to read/write to the same endpoint later. */
690 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
691 if ((fd = open(devnode, mode)) < 0)
692 return (-1);
694 hpriv->endpoints[endpt] = fd;
697 return (hpriv->endpoints[endpt]);
701 _sync_gen_transfer(struct usbi_transfer *itransfer)
703 struct libusb_transfer *transfer;
704 int fd, nr = 1;
706 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
709 * Bulk, Interrupt or Isochronous transfer depends on the
710 * endpoint and thus the node to open.
712 if ((fd = _access_endpoint(transfer)) < 0)
713 return _errno_to_libusb(errno);
715 if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
716 return _errno_to_libusb(errno);
718 if (IS_XFERIN(transfer)) {
719 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
720 if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
721 return _errno_to_libusb(errno);
723 nr = read(fd, transfer->buffer, transfer->length);
724 } else {
725 nr = write(fd, transfer->buffer, transfer->length);
728 if (nr < 0)
729 return _errno_to_libusb(errno);
731 itransfer->transferred = nr;
733 return (0);