From e9364d72151ae1de9cce4175f330fe1529f02511 Mon Sep 17 00:00:00 2001 From: Daniel Drake Date: Fri, 4 Jan 2008 00:40:49 +0000 Subject: Rename to libusb-1.0 I've taken over the libusb project, and what was previously known as fpusb will eventually be released as libusb-1.0. --- Makefile.am | 6 +- README | 13 +- configure.ac | 6 +- examples/Makefile.am | 4 +- examples/dpfp.c | 94 +++---- examples/lsusb.c | 22 +- fpusb.pc.in | 11 - libfpusb/Makefile.am | 7 - libfpusb/core.c | 348 ------------------------- libfpusb/descriptor.c | 355 ------------------------- libfpusb/fpusb.h | 264 ------------------- libfpusb/fpusbi.h | 206 --------------- libfpusb/io.c | 704 -------------------------------------------------- libfpusb/signalfd.h | 76 ------ libfpusb/usbfs.h | 135 ---------- libusb-1.0.pc.in | 11 + libusb/Makefile.am | 7 + libusb/core.c | 346 +++++++++++++++++++++++++ libusb/descriptor.c | 353 +++++++++++++++++++++++++ libusb/io.c | 702 +++++++++++++++++++++++++++++++++++++++++++++++++ libusb/libusb.h | 262 +++++++++++++++++++ libusb/libusbi.h | 204 +++++++++++++++ libusb/signalfd.h | 76 ++++++ libusb/usbfs.h | 133 ++++++++++ 24 files changed, 2165 insertions(+), 2180 deletions(-) delete mode 100644 fpusb.pc.in delete mode 100644 libfpusb/Makefile.am delete mode 100644 libfpusb/core.c delete mode 100644 libfpusb/descriptor.c delete mode 100644 libfpusb/fpusb.h delete mode 100644 libfpusb/fpusbi.h delete mode 100644 libfpusb/io.c delete mode 100644 libfpusb/signalfd.h delete mode 100644 libfpusb/usbfs.h create mode 100644 libusb-1.0.pc.in create mode 100644 libusb/Makefile.am create mode 100644 libusb/core.c create mode 100644 libusb/descriptor.c create mode 100644 libusb/io.c create mode 100644 libusb/libusb.h create mode 100644 libusb/libusbi.h create mode 100644 libusb/signalfd.h create mode 100644 libusb/usbfs.h diff --git a/Makefile.am b/Makefile.am index 870224a..aecbe83 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,14 +1,14 @@ AUTOMAKE_OPTIONS = dist-bzip2 -DISTCLEANFILES = ChangeLog fpusb.pc +DISTCLEANFILES = ChangeLog libusb-1.0.pc EXTRA_DIST = TODO -SUBDIRS = libfpusb +SUBDIRS = libusb if BUILD_EXAMPLES SUBDIRS += examples endif pkgconfigdir=$(libdir)/pkgconfig -pkgconfig_DATA=fpusb.pc +pkgconfig_DATA=libusb-1.0.pc .PHONY: ChangeLog dist-up ChangeLog: diff --git a/README b/README index e7df070..a84fab4 100644 --- a/README +++ b/README @@ -1,17 +1,14 @@ -fpusb -===== +libusb +====== -fpusb is another library for USB device access from Linux userspace. +libusb is another library for USB device access from Linux userspace. It is written in C and licensed under the LGPL-2.1 (see COPYING). See the homepage for more information: -http://www.reactivated.net/fprint/wiki/Fpusb +http://libusb.sourceforge.net Use the mailing list for questions, comments, etc: -http://www.reactivated.net/fprint/wiki/Mailing_list - -Use the bug tracker for bugs and feature requests: -http://www.reactivated.net/fprint/bugs/index.php?project=4&switch=1&do=index +https://sourceforge.net/mailarchive/forum.php?forum_name=libusb-devel - Daniel Drake diff --git a/configure.ac b/configure.ac index 2f1efd0..191f8e9 100644 --- a/configure.ac +++ b/configure.ac @@ -1,6 +1,6 @@ -AC_INIT([fpusb], [0.0]) +AC_INIT([libusb], [0.9.0]) AM_INIT_AUTOMAKE -AC_CONFIG_SRCDIR([libfpusb/core.c]) +AC_CONFIG_SRCDIR([libusb/core.c]) AM_CONFIG_HEADER([config.h]) AC_PREREQ([2.50]) @@ -44,6 +44,6 @@ AC_DEFINE([API_EXPORTED], [__attribute__((visibility("default")))], [Default vis AM_CFLAGS="-std=gnu99 -fgnu89-inline -Wall -Wundef -Wunused -Wstrict-prototypes -Werror-implicit-function-declaration -Wno-pointer-sign -Wshadow" AC_SUBST(AM_CFLAGS) -AC_CONFIG_FILES([fpusb.pc] [Makefile] [libfpusb/Makefile] [examples/Makefile]) +AC_CONFIG_FILES([libusb-1.0.pc] [Makefile] [libusb/Makefile] [examples/Makefile]) AC_OUTPUT diff --git a/examples/Makefile.am b/examples/Makefile.am index 8aa3cc2..dbc45d6 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -2,8 +2,8 @@ INCLUDES = -I$(top_srcdir) noinst_PROGRAMS = lsusb dpfp lsusb_SOURCES = lsusb.c -lsusb_LDADD = ../libfpusb/libfpusb.la -lfpusb +lsusb_LDADD = ../libusb/libusb-1.0.la -lusb-1.0 dpfp_SOURCES = dpfp.c -dpfp_LDADD = ../libfpusb/libfpusb.la -lfpusb +dpfp_LDADD = ../libusb/libusb-1.0.la -lusb-1.0 diff --git a/examples/dpfp.c b/examples/dpfp.c index fe1445c..ff4ae76 100644 --- a/examples/dpfp.c +++ b/examples/dpfp.c @@ -1,5 +1,5 @@ /* - * fpusb example program to manipulate U.are.U 4000B fingerprint scanner. + * libusb example program to manipulate U.are.U 4000B fingerprint scanner. * Copyright (C) 2007 Daniel Drake * * Basic image capture program only, does not consider the powerup quirks or @@ -26,7 +26,7 @@ #include #include -#include +#include #define EP_INTR (1 | USB_ENDPOINT_IN) #define EP_DATA (2 | USB_ENDPOINT_IN) @@ -58,34 +58,34 @@ enum { }; static int state = 0; -static struct fpusb_dev_handle *devh = NULL; +static struct libusb_dev_handle *devh = NULL; static unsigned char imgbuf[0x1b340]; static unsigned char irqbuf[INTR_LENGTH]; -static fpusb_urb_handle *img_urbh = NULL; -static fpusb_urb_handle *irq_urbh = NULL; +static libusb_urb_handle *img_urbh = NULL; +static libusb_urb_handle *irq_urbh = NULL; static int img_idx = 0; static int do_exit = 0; -static struct fpusb_bulk_msg imgmsg = { +static struct libusb_bulk_msg imgmsg = { .endpoint = EP_DATA, .data = imgbuf, .length = sizeof(imgbuf), }; -static struct fpusb_bulk_msg irqmsg = { +static struct libusb_bulk_msg irqmsg = { .endpoint = EP_INTR, .data = irqbuf, .length = sizeof(irqbuf), }; -static struct fpusb_dev *find_dpfp_device(void) +static struct libusb_dev *find_dpfp_device(void) { - struct fpusb_dev *dev; + struct libusb_dev *dev; - fpusb_find_devices(); + libusb_find_devices(); - for (dev = fpusb_get_devices(); dev; dev = fpusb_dev_next(dev)) { - struct usb_dev_descriptor *desc = fpusb_dev_get_descriptor(dev); + for (dev = libusb_get_devices(); dev; dev = libusb_dev_next(dev)) { + struct usb_dev_descriptor *desc = libusb_dev_get_descriptor(dev); if (desc->idVendor == 0x05ba && desc->idProduct == 0x000a) return dev; } @@ -96,7 +96,7 @@ static struct fpusb_dev *find_dpfp_device(void) static int print_f0_data(void) { unsigned char data[0x10]; - struct fpusb_ctrl_msg msg = { + struct libusb_ctrl_msg msg = { .requesttype = CTRL_IN, .request = USB_RQ, .value = 0xf0, @@ -107,7 +107,7 @@ static int print_f0_data(void) int r; unsigned int i; - r = fpusb_ctrl_msg(devh, &msg, 0); + r = libusb_ctrl_msg(devh, &msg, 0); if (r < 0) { fprintf(stderr, "F0 error %d\n", r); return r; @@ -126,7 +126,7 @@ static int print_f0_data(void) static int get_hwstat(unsigned char *status) { - struct fpusb_ctrl_msg msg = { + struct libusb_ctrl_msg msg = { .requesttype = CTRL_IN, .request = USB_RQ, .value = 0x07, @@ -136,7 +136,7 @@ static int get_hwstat(unsigned char *status) }; int r; - r = fpusb_ctrl_msg(devh, &msg, 0); + r = libusb_ctrl_msg(devh, &msg, 0); if (r < 0) { fprintf(stderr, "read hwstat error %d\n", r); return r; @@ -153,7 +153,7 @@ static int get_hwstat(unsigned char *status) static int set_hwstat(unsigned char data) { int r; - struct fpusb_ctrl_msg msg = { + struct libusb_ctrl_msg msg = { .requesttype = CTRL_OUT, .request = USB_RQ, .value = 0x07, @@ -164,7 +164,7 @@ static int set_hwstat(unsigned char data) printf("set hwstat to %02x\n", data); - r = fpusb_ctrl_msg(devh, &msg, 0); + r = libusb_ctrl_msg(devh, &msg, 0); if (r < 0) { fprintf(stderr, "set hwstat error %d\n", r); return r; @@ -180,7 +180,7 @@ static int set_hwstat(unsigned char data) static int set_mode(unsigned char data) { int r; - struct fpusb_ctrl_msg msg = { + struct libusb_ctrl_msg msg = { .requesttype = CTRL_OUT, .request = USB_RQ, .value = 0x4e, @@ -191,7 +191,7 @@ static int set_mode(unsigned char data) printf("set mode %02x\n", data); - r = fpusb_ctrl_msg(devh, &msg, 0); + r = libusb_ctrl_msg(devh, &msg, 0); if (r < 0) { fprintf(stderr, "set mode error %d\n", r); return r; @@ -204,8 +204,8 @@ static int set_mode(unsigned char data) return 0; } -static void cb_mode_changed(struct fpusb_dev_handle *_devh, - struct fpusb_urb_handle *urbh, enum fp_urb_cb_status status, +static void cb_mode_changed(struct libusb_dev_handle *_devh, + struct libusb_urb_handle *urbh, enum fp_urb_cb_status status, struct usb_ctrl_setup *setup, unsigned char *data, int actual_length, void *user_data) { @@ -221,8 +221,8 @@ static void cb_mode_changed(struct fpusb_dev_handle *_devh, static int set_mode_async(unsigned char data) { - fpusb_urb_handle *urbh; - struct fpusb_ctrl_msg msg = { + libusb_urb_handle *urbh; + struct libusb_ctrl_msg msg = { .requesttype = CTRL_OUT, .request = USB_RQ, .value = 0x4e, @@ -233,7 +233,7 @@ static int set_mode_async(unsigned char data) printf("async set mode %02x\n", data); - urbh = fpusb_submit_ctrl_msg(devh, &msg, cb_mode_changed, NULL, 1000); + urbh = libusb_submit_ctrl_msg(devh, &msg, cb_mode_changed, NULL, 1000); if (!urbh) { fprintf(stderr, "set mode submit error\n"); return -1; @@ -244,7 +244,7 @@ static int set_mode_async(unsigned char data) static int do_sync_intr(unsigned char *data) { - struct fpusb_bulk_msg msg = { + struct libusb_bulk_msg msg = { .endpoint = EP_INTR, .data = data, .length = INTR_LENGTH, @@ -252,7 +252,7 @@ static int do_sync_intr(unsigned char *data) int r; int transferred; - r = fpusb_intr_msg(devh, &msg, &transferred, 1000); + r = libusb_intr_msg(devh, &msg, &transferred, 1000); if (r < 0) { fprintf(stderr, "intr error %d\n", r); return r; @@ -335,7 +335,7 @@ static int next_state(void) return 0; } -static void cb_irq(fpusb_dev_handle *_devh, fpusb_urb_handle *urbh, +static void cb_irq(libusb_dev_handle *_devh, libusb_urb_handle *urbh, enum fp_urb_cb_status status, unsigned char endpoint, int rqlength, unsigned char *data, int actual_length, void *user_data) { @@ -374,7 +374,7 @@ static void cb_irq(fpusb_dev_handle *_devh, fpusb_urb_handle *urbh, do_exit = 2; } -static void cb_img(fpusb_dev_handle *_devh, fpusb_urb_handle *urbh, +static void cb_img(libusb_dev_handle *_devh, libusb_urb_handle *urbh, enum fp_urb_cb_status status, unsigned char endpoint, int rqlength, unsigned char *data, int actual_length, void *user_data) { @@ -396,15 +396,15 @@ static void cb_img(fpusb_dev_handle *_devh, fpusb_urb_handle *urbh, static int submit_irq_urb(void) { - fpusb_urb_handle_free(irq_urbh); - irq_urbh = fpusb_submit_intr_msg(devh, &irqmsg, cb_irq, NULL, 0); + libusb_urb_handle_free(irq_urbh); + irq_urbh = libusb_submit_intr_msg(devh, &irqmsg, cb_irq, NULL, 0); return irq_urbh != NULL; } static int submit_img_urb(void) { - fpusb_urb_handle_free(img_urbh); - img_urbh = fpusb_submit_bulk_msg(devh, &imgmsg, cb_img, NULL, 0); + libusb_urb_handle_free(img_urbh); + img_urbh = libusb_submit_bulk_msg(devh, &imgmsg, cb_img, NULL, 0); return img_urbh != NULL; } @@ -418,7 +418,7 @@ static int init_capture(void) r = submit_img_urb(); if (r < 0) { - fpusb_urb_handle_cancel_sync(devh, img_urbh); + libusb_urb_handle_cancel_sync(devh, img_urbh); return r; } @@ -468,13 +468,13 @@ static void sighandler(int signum) int main(void) { - struct fpusb_dev *dev; + struct libusb_dev *dev; struct sigaction sigact; int r = 1; - r = fpusb_init(0); + r = libusb_init(0); if (r < 0) { - fprintf(stderr, "failed to initialise fpusb\n"); + fprintf(stderr, "failed to initialise libusb\n"); exit(1); } @@ -485,14 +485,14 @@ int main(void) } printf("found device\n"); - devh = fpusb_devh_open(dev); + devh = libusb_devh_open(dev); if (!devh) { fprintf(stderr, "Could not open device\n"); goto out; } printf("opened device\n"); - r = fpusb_devh_claim_intf(devh, 0); + r = libusb_devh_claim_intf(devh, 0); if (r < 0) { fprintf(stderr, "usb_claim_interface error %d %s\n", r, strerror(-r)); goto out; @@ -521,18 +521,18 @@ int main(void) sigaction(SIGQUIT, &sigact, NULL); while (!do_exit) { - r = fpusb_poll(); + r = libusb_poll(); if (r < 0) goto out_deinit; } printf("shutting down...\n"); - r = fpusb_urb_handle_cancel_sync(devh, irq_urbh); + r = libusb_urb_handle_cancel_sync(devh, irq_urbh); if (r < 0) goto out_deinit; - r = fpusb_urb_handle_cancel_sync(devh, img_urbh); + r = libusb_urb_handle_cancel_sync(devh, img_urbh); if (r < 0) goto out_deinit; @@ -542,15 +542,15 @@ int main(void) r = 1; out_deinit: - fpusb_urb_handle_free(img_urbh); - fpusb_urb_handle_free(irq_urbh); + libusb_urb_handle_free(img_urbh); + libusb_urb_handle_free(irq_urbh); set_mode(0); set_hwstat(0x80); out_release: - fpusb_devh_release_intf(devh, 0); + libusb_devh_release_intf(devh, 0); out: - fpusb_devh_close(devh); - fpusb_exit(); + libusb_devh_close(devh); + libusb_exit(); return r >= 0 ? r : -r; } diff --git a/examples/lsusb.c b/examples/lsusb.c index 00fb178..d093709 100644 --- a/examples/lsusb.c +++ b/examples/lsusb.c @@ -1,5 +1,5 @@ /* - * fpusb example program to list devices on the bus + * libusb example program to list devices on the bus * Copyright (C) 2007 Daniel Drake * * This library is free software; you can redistribute it and/or @@ -19,28 +19,28 @@ #include -#include +#include -void print_devs(fpusb_dev *devs) +void print_devs(libusb_dev *devs) { - fpusb_dev *dev; + libusb_dev *dev; - for (dev = devs; dev; dev = fpusb_dev_next(dev)) { - struct usb_dev_descriptor *desc = fpusb_dev_get_descriptor(dev); + for (dev = devs; dev; dev = libusb_dev_next(dev)) { + struct usb_dev_descriptor *desc = libusb_dev_get_descriptor(dev); printf("%04x:%04x\n", desc->idVendor, desc->idProduct); } } int main(void) { - fpusb_dev *devs; - fpusb_init(0); - fpusb_find_devices(); - devs = fpusb_get_devices(); + libusb_dev *devs; + libusb_init(0); + libusb_find_devices(); + devs = libusb_get_devices(); print_devs(devs); - fpusb_exit(); + libusb_exit(); return 0; } diff --git a/fpusb.pc.in b/fpusb.pc.in deleted file mode 100644 index 8fd7221..0000000 --- a/fpusb.pc.in +++ /dev/null @@ -1,11 +0,0 @@ -prefix=@prefix@ -exec_prefix=@exec_prefix@ -libdir=@libdir@ -includedir=@includedir@ - -Name: fpusb -Description: C API for USB device access from Linux userspace -Version: @VERSION@ -Libs: -L${libdir} -lfpusb -Cflags: -I${includedir}/fpusb - diff --git a/libfpusb/Makefile.am b/libfpusb/Makefile.am deleted file mode 100644 index 9e9bee2..0000000 --- a/libfpusb/Makefile.am +++ /dev/null @@ -1,7 +0,0 @@ -lib_LTLIBRARIES = libfpusb.la - -libfpusb_la_CFLAGS = -fvisibility=hidden $(AM_CFLAGS) -libfpusb_la_SOURCES = signalfd.h fpusbi.h usbfs.h core.c descriptor.c io.c -libfpusb_la_LIBADD = -lrt - -pkginclude_HEADERS = fpusb.h diff --git a/libfpusb/core.c b/libfpusb/core.c deleted file mode 100644 index a2fa961..0000000 --- a/libfpusb/core.c +++ /dev/null @@ -1,348 +0,0 @@ -/* - * Core functions for libfpusb - * Copyright (C) 2007 Daniel Drake - * - * Portions based on libusb-0.1 - * Copyright (c) 2001 Johannes Erdfelt - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "fpusb.h" -#include "fpusbi.h" - -static struct list_head usb_devs; -struct list_head open_devs; - -static int scan_device(char *busdir, const char *devnum) -{ - char path[PATH_MAX + 1]; - unsigned char raw_desc[DEVICE_DESC_LENGTH]; - struct fpusb_dev *dev = malloc(sizeof(*dev)); - int fd = 0; - int i; - int r; - int tmp; - - if (!dev) - return -1; - - snprintf(path, PATH_MAX, "%s/%s", busdir, devnum); - fp_dbg("%s", path); - fd = open(path, O_RDWR); - if (!fd) { - fp_dbg("open '%s' failed, ret=%d errno=%d", path, fd, errno); - r = -1; - goto err; - } - - r = read(fd, raw_desc, DEVICE_DESC_LENGTH); - if (r < 0) { - fp_err("read failed ret=%d errno=%d", r, errno); - goto err; - } - /* FIXME: short read handling? */ - - fpi_parse_descriptor(raw_desc, "bbWbbbbWWWbbbb", &dev->desc); - - /* Now try to fetch the rest of the descriptors */ - if (dev->desc.bNumConfigurations > USB_MAXCONFIG) { - fp_err("too many configurations"); - r = -1; - goto err; - } - - if (dev->desc.bNumConfigurations < 1) { - fp_dbg("no configurations?"); - r = -1; - goto err; - } - - tmp = dev->desc.bNumConfigurations * sizeof(struct usb_config_descriptor); - dev->config = malloc(tmp); - if (!dev->config) { - r = -1; - goto err; - } - - memset(dev->config, 0, tmp); - - for (i = 0; i < dev->desc.bNumConfigurations; i++) { - unsigned char buffer[8], *bigbuffer; - struct usb_config_descriptor config; - - /* Get the first 8 bytes to figure out what the total length is */ - r = read(fd, buffer, sizeof(buffer)); - if (r < sizeof(buffer)) { - fp_err("short descriptor read (%d/%d)", r, sizeof(buffer)); - goto err; - } - - fpi_parse_descriptor(buffer, "bbw", &config); - - bigbuffer = malloc(config.wTotalLength); - if (!bigbuffer) - goto err; - - /* Read the rest of the config descriptor */ - memcpy(bigbuffer, buffer, sizeof(buffer)); - - tmp = config.wTotalLength - 8; - r = read(fd, bigbuffer + 8, tmp); - if (r < tmp) { - fp_err("short descriptor read (%d/%d)", r, tmp); - free(bigbuffer); - goto err; - } - - r = fpi_parse_configuration(&dev->config[i], bigbuffer); - if (r > 0) - fp_warn("descriptor data still left\n"); - free(bigbuffer); - } - - dev->nodepath = strdup(path); - if (!dev->nodepath) - goto err; - - fp_dbg("found device %04x:%04x", dev->desc.idVendor, dev->desc.idProduct); - list_add(&dev->list, &usb_devs); - r = 0; - -err: - if (fd) - close(fd); - if (r < 0 && dev) - free(dev); - return r; -} - -static int scan_busdir(const char *busnum) -{ - DIR *dir; - char dirpath[PATH_MAX + 1]; - struct dirent *entry; - - snprintf(dirpath, PATH_MAX, "%s/%s", USBFS_PATH, busnum); - fp_dbg("%s", dirpath); - dir = opendir(dirpath); - if (!dir) { - fp_err("opendir '%s' failed, errno=%d", dirpath, errno); - return -1; - } - - while ((entry = readdir(dir))) { - if (entry->d_name[0] == '.') - continue; - /* deliberately ignoring errors due to valid unplug race conditions */ - scan_device(dirpath, entry->d_name); - } - - return 0; -} - -API_EXPORTED int fpusb_find_devices(void) -{ - DIR *busses; - struct dirent *entry; - fp_dbg(""); - - busses = opendir(USBFS_PATH); - if (!busses) { - fp_err("opendir busses failed errno=%d", errno); - return -1; - } - - while ((entry = readdir(busses))) { - if (entry->d_name[0] == '.') - continue; - /* deliberately ignoring errors, valid race conditions exist - * e.g. unplugging of hubs in the middle of this loop*/ - scan_busdir(entry->d_name); - } - - return 0; -} - -API_EXPORTED struct fpusb_dev *fpusb_get_devices(void) -{ - if (list_empty(&usb_devs)) - return NULL; - return list_entry(usb_devs.next, struct fpusb_dev, list); -} - -API_EXPORTED struct fpusb_dev *fpusb_dev_next(struct fpusb_dev *dev) -{ - struct list_head *head = &dev->list; - if (!head || head->next == &usb_devs) - return NULL; - return list_entry(head->next, struct fpusb_dev, list); -} - -API_EXPORTED struct usb_dev_descriptor *fpusb_dev_get_descriptor( - struct fpusb_dev *dev) -{ - return &dev->desc; -} - -API_EXPORTED struct usb_config_descriptor *fpusb_dev_get_config( - struct fpusb_dev *dev) -{ - return dev->config; -} - -API_EXPORTED struct fpusb_dev_handle *fpusb_devh_open(struct fpusb_dev *dev) -{ - struct fpusb_dev_handle *devh; - int fd; - fp_dbg("open %04x:%04x", dev->desc.idVendor, dev->desc.idProduct); - - fd = open(dev->nodepath, O_RDWR); - if (!fd) { - fp_err("open failed, code %d errno %d", fd, errno); - return NULL; - } - - devh = malloc(sizeof(*devh)); - if (!devh) { - close(fd); - return NULL; - } - - devh->fd = fd; - devh->dev = dev; - list_add(&devh->list, &open_devs); - return devh; -} - -static void do_close(struct fpusb_dev_handle *devh) -{ - close(devh->fd); -} - -API_EXPORTED void fpusb_devh_close(struct fpusb_dev_handle *devh) -{ - if (!devh) - return; - fp_dbg(""); - - list_del(&devh->list); - do_close(devh); - free(devh); -} - -API_EXPORTED struct fpusb_dev *fpusb_devh_get_dev(struct fpusb_dev_handle *devh) -{ - return devh->dev; -} - -API_EXPORTED int fpusb_devh_claim_intf(struct fpusb_dev_handle *dev, - int iface) -{ - int r; - fp_dbg("interface %d", iface); - - r = ioctl(dev->fd, IOCTL_USB_CLAIMINTF, &iface); - if (r < 0) - fp_err("claim interface failed, error %d", r); - return r; -} - -API_EXPORTED int fpusb_devh_release_intf(struct fpusb_dev_handle *dev, - int iface) -{ - int r; - fp_dbg("interface %d", iface); - - r = ioctl(dev->fd, IOCTL_USB_RELEASEINTF, &iface); - if (r < 0) - fp_err("release interface failed, error %d", r); - return r; -} - -API_EXPORTED int fpusb_init(int signum) -{ - /* FIXME: find correct usb node path */ - fp_dbg(""); - list_init(&usb_devs); - list_init(&open_devs); - return fpi_io_init(signum); -} - -API_EXPORTED void fpusb_exit(void) -{ - struct fpusb_dev_handle *devh; - fp_dbg(""); - if (!list_empty(&open_devs)) { - fp_dbg("naughty app left some devices open!\n"); - list_for_each_entry(devh, &open_devs, list) - do_close(devh); - } - fpi_io_exit(); -} - -void fpi_log(enum fpi_log_level level, const char *function, - const char *format, ...) -{ - va_list args; - FILE *stream = stdout; - const char *prefix; - - switch (level) { - case LOG_LEVEL_INFO: - prefix = "info"; - break; - case LOG_LEVEL_WARNING: - stream = stderr; - prefix = "warning"; - break; - case LOG_LEVEL_ERROR: - stream = stderr; - prefix = "error"; - break; - case LOG_LEVEL_DEBUG: - stream = stderr; - prefix = "debug"; - break; - default: - stream = stderr; - prefix = "unknown"; - break; - } - - fprintf(stream, "fpusb:%s [%s] ", prefix, function); - - va_start (args, format); - vfprintf(stream, format, args); - va_end (args); - - fprintf(stream, "\n"); -} - diff --git a/libfpusb/descriptor.c b/libfpusb/descriptor.c deleted file mode 100644 index 1f2333d..0000000 --- a/libfpusb/descriptor.c +++ /dev/null @@ -1,355 +0,0 @@ -/* - * USB descriptor handling functions for libfpusb - * Copyright (C) 2007 Daniel Drake - * - * Portions based on libusb-0.1 - * Copyright (c) 2001 Johannes Erdfelt - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include -#include - -#include "fpusbi.h" - -#define DESC_HEADER_LENGTH 2 -#define DEVICE_DESC_LENGTH 18 -#define CONFIG_DESC_LENGTH 9 -#define INTERFACE_DESC_LENGTH 9 -#define ENDPOINT_DESC_LENGTH 7 -#define ENDPOINT_AUDIO_DESC_LENGTH 9 - -int fpi_parse_descriptor(unsigned char *source, char *descriptor, void *dest) -{ - unsigned char *sp = source, *dp = dest; - uint16_t w; - uint32_t d; - char *cp; - - for (cp = descriptor; *cp; cp++) { - switch (*cp) { - case 'b': /* 8-bit byte */ - *dp++ = *sp++; - break; - case 'w': /* 16-bit word, convert from little endian to CPU */ - w = (sp[1] << 8) | sp[0]; sp += 2; - dp += ((unsigned long)dp & 1); /* Align to word boundary */ - *((uint16_t *)dp) = w; dp += 2; - break; - case 'd': /* 32-bit dword, convert from little endian to CPU */ - d = (sp[3] << 24) | (sp[2] << 16) | (sp[1] << 8) | sp[0]; sp += 4; - dp += ((unsigned long)dp & 2); /* Align to dword boundary */ - *((uint32_t *)dp) = d; dp += 4; - break; - case 'W': /* 16-bit word, keep CPU endianess */ - dp += ((unsigned long)dp & 1); /* Align to word boundary */ - memcpy(dp, sp, 2); sp += 2; dp += 2; - break; - case 'D': /* 32-bit dword, keep CPU endianess */ - dp += ((unsigned long)dp & 2); /* Align to dword boundary */ - memcpy(dp, sp, 4); sp += 4; dp += 4; - break; - } - } - - return sp - source; -} - -static int parse_endpoint(struct usb_endpoint_descriptor *endpoint, - unsigned char *buffer, int size) -{ - struct usb_descriptor_header header; - unsigned char *begin; - int parsed = 0; - int len; - - fpi_parse_descriptor(buffer, "bb", &header); - - /* Everything should be fine being passed into here, but we sanity */ - /* check JIC */ - if (header.bLength > size) { - fp_err("ran out of descriptors parsing"); - return -1; - } - - if (header.bDescriptorType != USB_DT_ENDPOINT) { - fp_err("unexpected descriptor %x (expected %x)", - header.bDescriptorType, USB_DT_ENDPOINT); - return parsed; - } - - if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH) - fpi_parse_descriptor(buffer, "bbbbwbbb", endpoint); - else if (header.bLength >= ENDPOINT_DESC_LENGTH) - fpi_parse_descriptor(buffer, "bbbbwb", endpoint); - - buffer += header.bLength; - size -= header.bLength; - parsed += header.bLength; - - /* Skip over the rest of the Class Specific or Vendor Specific */ - /* descriptors */ - begin = buffer; - while (size >= DESC_HEADER_LENGTH) { - fpi_parse_descriptor(buffer, "bb", &header); - - if (header.bLength < 2) { - fp_err("invalid descriptor length %d", header.bLength); - return -1; - } - - /* If we find another "proper" descriptor then we're done */ - if ((header.bDescriptorType == USB_DT_ENDPOINT) || - (header.bDescriptorType == USB_DT_INTERFACE) || - (header.bDescriptorType == USB_DT_CONFIG) || - (header.bDescriptorType == USB_DT_DEVICE)) - break; - - fp_dbg("skipping descriptor %x", header.bDescriptorType); - buffer += header.bLength; - size -= header.bLength; - parsed += header.bLength; - } - - /* Copy any unknown descriptors into a storage area for drivers */ - /* to later parse */ - len = (int)(buffer - begin); - if (!len) { - endpoint->extra = NULL; - endpoint->extralen = 0; - return parsed; - } - - endpoint->extra = malloc(len); - if (!endpoint->extra) { - endpoint->extralen = 0; - return parsed; - } - - memcpy(endpoint->extra, begin, len); - endpoint->extralen = len; - - return parsed; -} - -static int parse_interface(struct usb_interface *interface, - unsigned char *buffer, int size) -{ - int i; - int len; - int r; - int parsed = 0; - int tmp; - struct usb_descriptor_header header; - struct usb_interface_descriptor *ifp; - unsigned char *begin; - - interface->num_altsetting = 0; - - while (size >= INTERFACE_DESC_LENGTH) { - interface->altsetting = realloc(interface->altsetting, - sizeof(struct usb_interface_descriptor) * - (interface->num_altsetting + 1)); - if (!interface->altsetting) - return -1; - - ifp = interface->altsetting + interface->num_altsetting; - interface->num_altsetting++; - fpi_parse_descriptor(buffer, "bbbbbbbbb", ifp); - - /* Skip over the interface */ - buffer += ifp->bLength; - parsed += ifp->bLength; - size -= ifp->bLength; - - begin = buffer; - - /* Skip over any interface, class or vendor descriptors */ - while (size >= DESC_HEADER_LENGTH) { - fpi_parse_descriptor(buffer, "bb", &header); - if (header.bLength < 2) { - fp_err("invalid descriptor of length %d", header.bLength); - return -1; - } - - /* If we find another "proper" descriptor then we're done */ - if ((header.bDescriptorType == USB_DT_INTERFACE) || - (header.bDescriptorType == USB_DT_ENDPOINT) || - (header.bDescriptorType == USB_DT_CONFIG) || - (header.bDescriptorType == USB_DT_DEVICE)) - break; - - buffer += header.bLength; - parsed += header.bLength; - size -= header.bLength; - } - - /* Copy any unknown descriptors into a storage area for */ - /* drivers to later parse */ - len = (int)(buffer - begin); - if (!len) { - ifp->extra = NULL; - ifp->extralen = 0; - } else { - ifp->extra = malloc(len); - if (!ifp->extra) { - ifp->extralen = 0; - /* FIXME will leak memory */ - return -1; - } - memcpy(ifp->extra, begin, len); - ifp->extralen = len; - } - - /* Did we hit an unexpected descriptor? */ - fpi_parse_descriptor(buffer, "bb", &header); - if ((size >= DESC_HEADER_LENGTH) && - ((header.bDescriptorType == USB_DT_CONFIG) || - (header.bDescriptorType == USB_DT_DEVICE))) - return parsed; - - if (ifp->bNumEndpoints > USB_MAXENDPOINTS) { - fp_err("too many endpoints (%d)", ifp->bNumEndpoints); - /* FIXME will leak memory */ - return -1; - } - - if (ifp->bNumEndpoints > 0) { - tmp = ifp->bNumEndpoints * sizeof(struct usb_endpoint_descriptor); - ifp->endpoint = malloc(tmp); - if (!ifp->endpoint) - /* FIXME will leak memory? */ - return -1; - - memset(ifp->endpoint, 0, tmp); - for (i = 0; i < ifp->bNumEndpoints; i++) { - fpi_parse_descriptor(buffer, "bb", &header); - - if (header.bLength > size) { - fp_err("ran out of descriptors parsing"); - /* FIXME will leak memory */ - return -1; - } - - r = parse_endpoint(ifp->endpoint + i, buffer, size); - if (r < 0) - /* FIXME will leak memory */ - return r; - - buffer += r; - parsed += r; - size -= r; - } - } else - ifp->endpoint = NULL; - - /* We check to see if it's an alternate to this one */ - ifp = (struct usb_interface_descriptor *) buffer; - if (size < USB_DT_INTERFACE_SIZE || - ifp->bDescriptorType != USB_DT_INTERFACE || - !ifp->bAlternateSetting) - return parsed; - } - - return parsed; -} - -int fpi_parse_configuration(struct usb_config_descriptor *config, - unsigned char *buffer) -{ - int i; - int r; - int size; - int tmp; - struct usb_descriptor_header header; - - fpi_parse_descriptor(buffer, "bbwbbbbb", config); - size = config->wTotalLength; - - if (config->bNumInterfaces > USB_MAXINTERFACES) { - fp_err("too many interfaces (%d)", config->bNumInterfaces); - return -1; - } - - tmp = config->bNumInterfaces * sizeof(struct usb_interface); - config->interface = malloc(tmp); - if (!config->interface) - return -1; - - memset(config->interface, 0, tmp); - buffer += config->bLength; - size -= config->bLength; - - config->extra = NULL; - config->extralen = 0; - - for (i = 0; i < config->bNumInterfaces; i++) { - int len; - unsigned char *begin; - - /* Skip over the rest of the Class Specific or Vendor */ - /* Specific descriptors */ - begin = buffer; - while (size >= DESC_HEADER_LENGTH) { - fpi_parse_descriptor(buffer, "bb", &header); - - if ((header.bLength > size) || - (header.bLength < DESC_HEADER_LENGTH)) { - fp_err("invalid descriptor length of %d", header.bLength); - return -1; - } - - /* If we find another "proper" descriptor then we're done */ - if ((header.bDescriptorType == USB_DT_ENDPOINT) || - (header.bDescriptorType == USB_DT_INTERFACE) || - (header.bDescriptorType == USB_DT_CONFIG) || - (header.bDescriptorType == USB_DT_DEVICE)) - break; - - fp_dbg("skipping descriptor 0x%x\n", header.bDescriptorType); - buffer += header.bLength; - size -= header.bLength; - } - - /* Copy any unknown descriptors into a storage area for */ - /* drivers to later parse */ - len = (int)(buffer - begin); - if (len) { - /* FIXME: We should realloc and append here */ - if (!config->extralen) { - config->extra = malloc(len); - if (!config->extra) { - config->extralen = 0; - /* FIXME will leak memory */ - return -1; - } - - memcpy(config->extra, begin, len); - config->extralen = len; - } - } - - r = parse_interface(config->interface + i, buffer, size); - if (r < 0) - return r; - - buffer += r; - size -= r; - } - - return size; -} - diff --git a/libfpusb/fpusb.h b/libfpusb/fpusb.h deleted file mode 100644 index abcb6c9..0000000 --- a/libfpusb/fpusb.h +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Public libfpusb header file - * Copyright (C) 2007 Daniel Drake - * - * Portions based on libusb-0.1 - * Copyright (c) 2001 Johannes Erdfelt - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef __FPUSB_H__ -#define __FPUSB_H__ - -#include -#include - -/* standard USB stuff */ - -/* Device and/or Interface Class codes */ -#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ -#define USB_CLASS_AUDIO 1 -#define USB_CLASS_COMM 2 -#define USB_CLASS_HID 3 -#define USB_CLASS_PRINTER 7 -#define USB_CLASS_PTP 6 -#define USB_CLASS_MASS_STORAGE 8 -#define USB_CLASS_HUB 9 -#define USB_CLASS_DATA 10 -#define USB_CLASS_VENDOR_SPEC 0xff - -/* Descriptor types */ -#define USB_DT_DEVICE 0x01 -#define USB_DT_CONFIG 0x02 -#define USB_DT_STRING 0x03 -#define USB_DT_INTERFACE 0x04 -#define USB_DT_ENDPOINT 0x05 -#define USB_DT_HID 0x21 -#define USB_DT_REPORT 0x22 -#define USB_DT_PHYSICAL 0x23 -#define USB_DT_HUB 0x29 - -/* Descriptor sizes per descriptor type */ -#define USB_DT_DEVICE_SIZE 18 -#define USB_DT_CONFIG_SIZE 9 -#define USB_DT_INTERFACE_SIZE 9 -#define USB_DT_ENDPOINT_SIZE 7 -#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ -#define USB_DT_HUB_NONVAR_SIZE 7 - -#define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ -#define USB_ENDPOINT_DIR_MASK 0x80 - -#define USB_ENDPOINT_IN 0x80 -#define USB_ENDPOINT_OUT 0x00 - -#define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */ -#define USB_ENDPOINT_TYPE_CONTROL 0 -#define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 -#define USB_ENDPOINT_TYPE_BULK 2 -#define USB_ENDPOINT_TYPE_INTERRUPT 3 - -/* Standard requests */ -#define USB_REQ_GET_STATUS 0x00 -#define USB_REQ_CLEAR_FEATURE 0x01 -/* 0x02 is reserved */ -#define USB_REQ_SET_FEATURE 0x03 -/* 0x04 is reserved */ -#define USB_REQ_SET_ADDRESS 0x05 -#define USB_REQ_GET_DESCRIPTOR 0x06 -#define USB_REQ_SET_DESCRIPTOR 0x07 -#define USB_REQ_GET_CONFIGURATION 0x08 -#define USB_REQ_SET_CONFIGURATION 0x09 -#define USB_REQ_GET_INTERFACE 0x0A -#define USB_REQ_SET_INTERFACE 0x0B -#define USB_REQ_SYNCH_FRAME 0x0C - -#define USB_TYPE_STANDARD (0x00 << 5) -#define USB_TYPE_CLASS (0x01 << 5) -#define USB_TYPE_VENDOR (0x02 << 5) -#define USB_TYPE_RESERVED (0x03 << 5) - -#define USB_RECIP_DEVICE 0x00 -#define USB_RECIP_INTERFACE 0x01 -#define USB_RECIP_ENDPOINT 0x02 -#define USB_RECIP_OTHER 0x03 - -struct usb_dev_descriptor { - uint8_t bLength; - uint8_t bDescriptorType; - uint16_t bcdUSB; - uint8_t bDeviceClass; - uint8_t bDeviceSubClass; - uint8_t bDeviceProtocol; - uint8_t bMaxPacketSize0; - uint16_t idVendor; - uint16_t idProduct; - uint16_t bcdDevice; - uint8_t iManufacturer; - uint8_t iProduct; - uint8_t iSerialNumber; - uint8_t bNumConfigurations; -}; - -struct usb_endpoint_descriptor { - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bEndpointAddress; - uint8_t bmAttributes; - uint16_t wMaxPacketSize; - uint8_t bInterval; - uint8_t bRefresh; - uint8_t bSynchAddress; - - unsigned char *extra; /* Extra descriptors */ - int extralen; -}; - -struct usb_interface_descriptor { - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bInterfaceNumber; - uint8_t bAlternateSetting; - uint8_t bNumEndpoints; - uint8_t bInterfaceClass; - uint8_t bInterfaceSubClass; - uint8_t bInterfaceProtocol; - uint8_t iInterface; - - struct usb_endpoint_descriptor *endpoint; - - unsigned char *extra; /* Extra descriptors */ - int extralen; -}; - -struct usb_interface { - struct usb_interface_descriptor *altsetting; - int num_altsetting; -}; - -struct usb_config_descriptor { - uint8_t bLength; - uint8_t bDescriptorType; - uint16_t wTotalLength; - uint8_t bNumInterfaces; - uint8_t bConfigurationValue; - uint8_t iConfiguration; - uint8_t bmAttributes; - uint8_t MaxPower; - - struct usb_interface *interface; - - unsigned char *extra; /* Extra descriptors */ - int extralen; -}; - -/* off-the-wire structures */ - -struct usb_ctrl_setup { - uint8_t bRequestType; - uint8_t bRequest; - uint16_t wValue; - uint16_t wIndex; - uint16_t wLength; -} __attribute__((packed)); - -/* fpusb */ - -struct fpusb_dev; -typedef struct fpusb_dev fpusb_dev; - -struct fpusb_dev_handle; -typedef struct fpusb_dev_handle fpusb_dev_handle; - -struct fpusb_urb_handle; -typedef struct fpusb_urb_handle fpusb_urb_handle; - -enum fp_urb_cb_status { - FP_URB_SILENT_COMPLETION = 0, - FP_URB_COMPLETED, - FP_URB_TIMEOUT, - FP_URB_CANCELLED, -}; - -struct fpusb_ctrl_msg { - uint8_t requesttype; - uint8_t request; - uint16_t value; - uint16_t index; - uint16_t length; - unsigned char *data; -}; - -typedef void (*fpusb_ctrl_cb_fn)(fpusb_dev_handle *devh, fpusb_urb_handle *urbh, - enum fp_urb_cb_status status, struct usb_ctrl_setup *setup, - unsigned char *data, int actual_length, void *user_data); - -struct fpusb_bulk_msg { - unsigned char endpoint; - unsigned char *data; - int length; -}; - -typedef void (*fpusb_bulk_cb_fn)(fpusb_dev_handle *devh, fpusb_urb_handle *urbh, - enum fp_urb_cb_status status, unsigned char endpoint, - int rqlength, unsigned char *data, int actual_length, void *user_data); - -int fpusb_init(int signum); -void fpusb_exit(void); - -int fpusb_find_devices(void); -fpusb_dev *fpusb_get_devices(void); -struct usb_dev_descriptor *fpusb_dev_get_descriptor(fpusb_dev *dev); -struct usb_config_descriptor *fpusb_dev_get_config(fpusb_dev *dev); -fpusb_dev *fpusb_dev_next(fpusb_dev *dev); - -fpusb_dev_handle *fpusb_devh_open(fpusb_dev *dev); -void fpusb_devh_close(fpusb_dev_handle *devh); -struct fpusb_dev *fpusb_devh_get_dev(fpusb_dev_handle *devh); -int fpusb_devh_claim_intf(fpusb_dev_handle *dev, int iface); -int fpusb_devh_release_intf(fpusb_dev_handle *dev, int iface); - -/* async I/O */ - -fpusb_urb_handle *fpusb_submit_ctrl_msg(fpusb_dev_handle *devh, - struct fpusb_ctrl_msg *msg, fpusb_ctrl_cb_fn callback, void *user_data, - unsigned int timeout); -fpusb_urb_handle *fpusb_submit_bulk_msg(fpusb_dev_handle *devh, - struct fpusb_bulk_msg *msg, fpusb_bulk_cb_fn callback, void *user_data, - unsigned int timeout); -fpusb_urb_handle *fpusb_submit_intr_msg(fpusb_dev_handle *devh, - struct fpusb_bulk_msg *msg, fpusb_bulk_cb_fn callback, void *user_data, - unsigned int timeout); - -int fpusb_urb_handle_cancel(fpusb_dev_handle *devh, fpusb_urb_handle *urbh); -int fpusb_urb_handle_cancel_sync(fpusb_dev_handle *devh, - fpusb_urb_handle *urbh); -void fpusb_urb_handle_free(fpusb_urb_handle *urbh); - -int fpusb_poll_timeout(struct timeval *tv); -int fpusb_poll(void); -int fpusb_get_pollfd(void); - -/* sync I/O */ - -int fpusb_ctrl_msg(fpusb_dev_handle *devh, struct fpusb_ctrl_msg *msg, - unsigned int timeout); -int fpusb_bulk_msg(fpusb_dev_handle *devh, struct fpusb_bulk_msg *msg, - int *transferred, unsigned int timeout); -int fpusb_intr_msg(fpusb_dev_handle *devh, struct fpusb_bulk_msg *msg, - int *transferred, unsigned int timeout); - -#endif diff --git a/libfpusb/fpusbi.h b/libfpusb/fpusbi.h deleted file mode 100644 index 4c9f9d2..0000000 --- a/libfpusb/fpusbi.h +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Internal header for libfpusb - * Copyright (C) 2007 Daniel Drake - * - * Portions based on libusb-0.1 - * Copyright (c) 2001 Johannes Erdfelt - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef __FPUSBI_H__ -#define __FPUSBI_H__ - -#include - -#include -#include -#include -#include - -#include -#include - -#define USBFS_PATH "/dev/bus/usb" -#define DEVICE_DESC_LENGTH 18 - -#define USB_MAXENDPOINTS 32 -#define USB_MAXINTERFACES 32 -#define USB_MAXCONFIG 8 - -struct list_head { - struct list_head *prev, *next; -}; - -/* Get an entry from the list - * ptr - the address of this list_head element in "type" - * type - the data type that contains "member" - * member - the list_head element in "type" - */ -#define list_entry(ptr, type, member) \ - ((type *)((char *)(ptr) - (unsigned long)(&((type *)0L)->member))) - -/* Get each entry from a list - * pos - A structure pointer has a "member" element - * head - list head - * member - the list_head element in "pos" - */ -#define list_for_each_entry(pos, head, member) \ - for (pos = list_entry((head)->next, typeof(*pos), member); \ - &pos->member != (head); \ - pos = list_entry(pos->member.next, typeof(*pos), member)) - -#define list_for_each_entry_safe(pos, n, head, member) \ - for (pos = list_entry((head)->next, typeof(*pos), member), \ - n = list_entry(pos->member.next, typeof(*pos), member); \ - &pos->member != (head); \ - pos = n, n = list_entry(n->member.next, typeof(*n), member)) - -#define list_empty(entry) ((entry)->next == (entry)) - -static inline void list_init(struct list_head *entry) -{ - entry->prev = entry->next = entry; -} - -static inline void list_add(struct list_head *entry, struct list_head *head) -{ - entry->next = head->next; - entry->prev = head; - - head->next->prev = entry; - head->next = entry; -} - -static inline void list_add_tail(struct list_head *entry, - struct list_head *head) -{ - entry->next = head; - entry->prev = head->prev; - - head->prev->next = entry; - head->prev = entry; -} - -static inline void list_del(struct list_head *entry) -{ - entry->next->prev = entry->prev; - entry->prev->next = entry->next; -} - -#define container_of(ptr, type, member) ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );}) - -#define bswap16(x) (((x & 0xff) << 8) | (x >> 8)) -#if __BYTE_ORDER == __LITTLE_ENDIAN -#define cpu_to_le16(x) (x) -#define le16_to_cpu(x) (x) -#elif __BYTE_ORDER == __BIG_ENDIAN -#define le16_to_cpu(x) bswap16(x) -#define cpu_to_le16(x) bswap16(x) -#else -#error "Unrecognized endianness" -#endif - -#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) - -#define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0) - -enum fpi_log_level { - LOG_LEVEL_DEBUG, - LOG_LEVEL_INFO, - LOG_LEVEL_WARNING, - LOG_LEVEL_ERROR, -}; - -void fpi_log(enum fpi_log_level, const char *function, const char *format, ...); - -#ifdef ENABLE_LOGGING -#define _fpi_log(level, fmt...) fpi_log(level, __FUNCTION__, fmt) -#else -#define _fpi_log(level, fmt...) -#endif - -#ifdef ENABLE_DEBUG_LOGGING -#define fp_dbg(fmt...) _fpi_log(LOG_LEVEL_DEBUG, fmt) -#else -#define fp_dbg(fmt...) -#endif - -#define fp_info(fmt...) _fpi_log(LOG_LEVEL_INFO, fmt) -#define fp_warn(fmt...) _fpi_log(LOG_LEVEL_WARNING, fmt) -#define fp_err(fmt...) _fpi_log(LOG_LEVEL_ERROR, fmt) - -struct fpusb_dev { - struct list_head list; - char *nodepath; - struct usb_dev_descriptor desc; - struct usb_config_descriptor *config; -}; - -struct fpusb_dev_handle { - struct list_head list; - struct fpusb_dev *dev; - int fd; -}; - -enum fpusb_urb_type { - FPUSB_URB_CONTROL, - FPUSB_URB_BULK, -}; - -#define FPUSB_URBH_DATA_BELONGS_TO_USER (1<<0) -#define FPUSB_URBH_SYNC_CANCELLED (1<<1) -#define FPUSB_URBH_TIMED_OUT (1<<2) - -struct fpusb_urb_handle { - struct fpusb_dev_handle *devh; - struct usb_urb urb; - struct list_head list; - struct timespec timeout; - timer_t timer; - unsigned char urb_type; - unsigned char endpoint; - int transfer_len; - int transferred; - unsigned char *buffer; - void *callback; - void *user_data; - uint8_t flags; -}; - -/* bus structures */ - -/* All standard descriptors have these 2 fields in common */ -struct usb_descriptor_header { - uint8_t bLength; - uint8_t bDescriptorType; -}; - -/* shared data and functions */ - -extern struct list_head open_devs; - -int fpi_io_init(int _signum); -void fpi_io_exit(void); - -int fpi_parse_descriptor(unsigned char *source, char *descriptor, void *dest); -int fpi_parse_configuration(struct usb_config_descriptor *config, - unsigned char *buffer); - -#endif - diff --git a/libfpusb/io.c b/libfpusb/io.c deleted file mode 100644 index f5949ea..0000000 --- a/libfpusb/io.c +++ /dev/null @@ -1,704 +0,0 @@ -/* - * I/O functions for libfpusb - * Copyright (C) 2007 Daniel Drake - * - * Portions based on libusb-0.1 - * Copyright (c) 2001 Johannes Erdfelt - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* signalfd() support is present in glibc-2.7 onwards, but glibc-2.7 contains - * a bug where the header is neither installed or compilable. This will be - * fixed for glibc-2.8. */ -#if __GLIBC_PREREQ(2, 8) -#include -#else -#include "signalfd.h" -#endif - -#include "fpusbi.h" - -static int sigfd; -static int signum; - -/* this is a list of in-flight fp_urb_handles, sorted by timeout expiration. - * URBs to timeout the soonest are placed at the beginning of the list, URBs - * that will time out later are placed after, and urbs with infinite timeout - * are always placed at the very end. */ -static struct list_head flying_urbs; - -static int setup_signalfd(int _signum) -{ - sigset_t sigset; - if (_signum == 0) - _signum = SIGRTMIN; - fp_dbg("signal %d", _signum); - - sigemptyset(&sigset); - sigaddset(&sigset, _signum); - sigfd = signalfd(-1, &sigset, 0); - if (sigfd < 0) { - fp_err("signalfd failed, code=%d errno=%d", sigfd, errno); - return sigfd; - } - fp_dbg("got signalfd %d", sigfd); - signum = _signum; - - sigemptyset(&sigset); - sigaddset(&sigset, _signum); - return sigprocmask(SIG_BLOCK, &sigset, NULL); -} - -int fpi_io_init(int _signum) -{ - list_init(&flying_urbs); - return setup_signalfd(signum); -} - -void fpi_io_exit(void) -{ - close(sigfd); -} - -static int calculate_timeout(struct fpusb_urb_handle *urbh, - unsigned int timeout) -{ - int r; - struct timespec current_time; - struct sigevent sigevt = { - .sigev_notify = SIGEV_SIGNAL, - .sigev_signo = signum, - }; - struct itimerspec itspec; - struct timespec *it_value = &itspec.it_value; - - if (!timeout) - return 0; - - r = clock_gettime(CLOCK_MONOTONIC, ¤t_time); - if (r < 0) { - fp_err("failed to read monotonic clock, errno=%d", errno); - return r; - } - - r = timer_create(CLOCK_MONOTONIC, &sigevt, &urbh->timer); - if (r < 0) { - fp_err("failed to create monotonic timer"); - return r; - } - - memset(&itspec, 0, sizeof(itspec)); - it_value->tv_sec = current_time.tv_sec + (timeout / 1000); - it_value->tv_nsec = current_time.tv_nsec + - ((timeout % 1000) * 1000000); - - if (it_value->tv_nsec > 1000000000) { - it_value->tv_nsec -= 1000000000; - it_value->tv_sec++; - } - - r = timer_settime(&urbh->timer, TIMER_ABSTIME, &itspec, NULL); - if (r < 0) { - fp_err("failed to arm monotonic timer"); - return r; - } - - urbh->timeout = itspec.it_value; - - return 0; -} - -static void add_to_flying_list(struct fpusb_urb_handle *urbh) -{ - struct fpusb_urb_handle *cur; - struct timespec *timeout = &urbh->timeout; - - /* if we have no other flying urbs, start the list with this one */ - if (list_empty(&flying_urbs)) { - list_add(&urbh->list, &flying_urbs); - return; - } - - /* if we have infinite timeout, append to end of list */ - if (!TIMESPEC_IS_SET(timeout)) { - list_add_tail(&urbh->list, &flying_urbs); - return; - } - - /* otherwise, find appropriate place in list */ - list_for_each_entry(cur, &flying_urbs, list) { - /* find first timeout that occurs after the urbh in question */ - struct timespec *cur_ts = &cur->timeout; - - if (!TIMESPEC_IS_SET(cur_ts) || (cur_ts->tv_sec > timeout->tv_sec) || - (cur_ts->tv_sec == timeout->tv_sec && - cur_ts->tv_nsec > timeout->tv_nsec)) { - list_add_tail(&urbh->list, &cur->list); - return; - } - } - - /* otherwise we need to be inserted at the end */ - list_add_tail(&urbh->list, &flying_urbs); -} - -static int submit_urb(struct fpusb_dev_handle *devh, - struct fpusb_urb_handle *urbh) -{ - int r; - struct usb_urb *urb = &urbh->urb; - int to_be_transferred = urbh->transfer_len - urbh->transferred; - - urb->type = urbh->urb_type; - urb->endpoint = urbh->endpoint; - urb->buffer = urbh->buffer + urbh->transferred; - urb->buffer_length = MIN(to_be_transferred, MAX_URB_BUFFER_LENGTH); - urb->signr = signum; - - /* FIXME: for requests that we have to split into multiple URBs, we should - * submit all the URBs instantly: submit, submit, submit, reap, reap, reap - * rather than: submit, reap, submit, reap, submit, reap - * this will improve performance and fix bugs concerning behaviour when - * the user submits two similar multiple-urb requests */ - fp_dbg("transferring %d from %d bytes", urb->buffer_length, - to_be_transferred); - - r = ioctl(devh->fd, IOCTL_USB_SUBMITURB, &urbh->urb); - if (r < 0) { - fp_err("submiturb failed error %d errno=%d", r, errno); - return r; - } - - add_to_flying_list(urbh); - return 0; -} - -API_EXPORTED struct fpusb_urb_handle *fpusb_submit_ctrl_msg( - struct fpusb_dev_handle *devh, struct fpusb_ctrl_msg *msg, - fpusb_ctrl_cb_fn callback, void *user_data, unsigned int timeout) -{ - struct fpusb_urb_handle *urbh = malloc(sizeof(*urbh)); - struct usb_ctrl_setup *setup; - unsigned char *urbdata; - int urbdata_length = sizeof(struct usb_ctrl_setup) + msg->length; - int r; - - if (!urbh) - return NULL; - memset(urbh, 0, sizeof(*urbh)); - urbh->devh = devh; - urbh->callback = callback; - urbh->user_data = user_data; - r = calculate_timeout(urbh, timeout); - if (r < 0) { - free(urbh); - return NULL; - } - - urbdata = malloc(urbdata_length); - if (!urbdata) { - free(urbh); - return NULL; - } - - fp_dbg("RQT=%02x RQ=%02x VAL=%04x IDX=%04x length=%d", - msg->requesttype, msg->request, msg->value, msg->index, msg->length); - - setup = (struct usb_ctrl_setup *) urbdata; - setup->bRequestType = msg->requesttype; - setup->bRequest = msg->request; - setup->wValue = cpu_to_le16(msg->value); - setup->wIndex = cpu_to_le16(msg->index); - setup->wLength = cpu_to_le16(msg->length); - - if ((msg->requesttype & 0x80) == USB_ENDPOINT_OUT) - memcpy(urbdata + sizeof(struct usb_ctrl_setup), msg->data, msg->length); - - urbh->urb_type = USB_URB_TYPE_CONTROL; - urbh->buffer = urbdata; - urbh->transfer_len = urbdata_length; - - r = submit_urb(devh, urbh); - if (r < 0) { - free(urbh); - free(urbdata); - return NULL; - } - - return urbh; -} - -static struct fpusb_urb_handle *submit_bulk_msg(struct fpusb_dev_handle *devh, - struct fpusb_bulk_msg *msg, fpusb_bulk_cb_fn callback, void *user_data, - unsigned int timeout, unsigned char urbtype) -{ - struct fpusb_urb_handle *urbh = malloc(sizeof(*urbh)); - int r; - - fp_dbg("length %d timeout %d", msg->length, timeout); - - if (!urbh) - return NULL; - memset(urbh, 0, sizeof(*urbh)); - r = calculate_timeout(urbh, timeout); - if (r < 0) { - free(urbh); - return NULL; - } - urbh->devh = devh; - urbh->callback = callback; - urbh->user_data = user_data; - urbh->flags |= FPUSB_URBH_DATA_BELONGS_TO_USER; - urbh->endpoint = msg->endpoint; - urbh->urb_type = urbtype; - urbh->buffer = msg->data; - urbh->transfer_len = msg->length; - - r = submit_urb(devh, urbh); - if (r < 0) { - free(urbh); - return NULL; - } - - return urbh; -} - -API_EXPORTED struct fpusb_urb_handle *fpusb_submit_bulk_msg( - struct fpusb_dev_handle *devh, struct fpusb_bulk_msg *msg, - fpusb_bulk_cb_fn callback, void *user_data, unsigned int timeout) -{ - return submit_bulk_msg(devh, msg, callback, user_data, timeout, - USB_URB_TYPE_BULK); -} - -API_EXPORTED struct fpusb_urb_handle *fpusb_submit_intr_msg( - struct fpusb_dev_handle *devh, struct fpusb_bulk_msg *msg, - fpusb_bulk_cb_fn callback, void *user_data, unsigned int timeout) -{ - return submit_bulk_msg(devh, msg, callback, user_data, timeout, - USB_URB_TYPE_INTERRUPT); -} - -API_EXPORTED int fpusb_urb_handle_cancel(struct fpusb_dev_handle *devh, - struct fpusb_urb_handle *urbh) -{ - int r; - fp_dbg(""); - r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb); - if (r < 0) - fp_err("cancel urb failed error %d", r); - return r; -} - -API_EXPORTED int fpusb_urb_handle_cancel_sync(struct fpusb_dev_handle *devh, - struct fpusb_urb_handle *urbh) -{ - int r; - fp_dbg(""); - r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb); - if (r < 0) { - fp_err("cancel urb failed error %d", r); - return r; - } - - urbh->flags |= FPUSB_URBH_SYNC_CANCELLED; - while (urbh->flags & FPUSB_URBH_SYNC_CANCELLED) { - r = fpusb_poll(); - if (r < 0) - return r; - } - - return 0; -} - -int handle_transfer_completion(struct fpusb_dev_handle *devh, - struct fpusb_urb_handle *urbh, enum fp_urb_cb_status status) -{ - struct usb_urb *urb = &urbh->urb; - - if (TIMESPEC_IS_SET(&urbh->timeout)) - timer_delete(urbh->timer); - - if (status == FP_URB_SILENT_COMPLETION) - return 0; - - if (urb->type == USB_URB_TYPE_CONTROL) { - fpusb_ctrl_cb_fn callback = urbh->callback; - if (callback) - callback(devh, urbh, status, urb->buffer, - urb->buffer + sizeof(struct usb_ctrl_setup), urbh->transferred, - urbh->user_data); - } else if (urb->type == USB_URB_TYPE_BULK || - urb->type == USB_URB_TYPE_INTERRUPT) { - fpusb_bulk_cb_fn callback = urbh->callback; - if (callback) - callback(devh, urbh, status, urbh->endpoint, urbh->transfer_len, - urbh->buffer, urbh->transferred, urbh->user_data); - } - return 0; -} - -static int handle_transfer_cancellation(struct fpusb_dev_handle *devh, - struct fpusb_urb_handle *urbh) -{ - /* if the URB is being cancelled synchronously, raise cancellation - * completion event by unsetting flag, and ensure that user callback does - * not get called. - */ - if (urbh->flags & FPUSB_URBH_SYNC_CANCELLED) { - urbh->flags &= ~FPUSB_URBH_SYNC_CANCELLED; - fp_dbg("detected sync. cancel"); - return handle_transfer_completion(devh, urbh, FP_URB_SILENT_COMPLETION); - } - - /* if the URB was cancelled due to timeout, report timeout to the user */ - if (urbh->flags & FPUSB_URBH_TIMED_OUT) { - fp_dbg("detected timeout cancellation"); - return handle_transfer_completion(devh, urbh, FP_URB_TIMEOUT); - } - - /* otherwise its a normal async cancel */ - return handle_transfer_completion(devh, urbh, FP_URB_CANCELLED); -} - -static int reap_for_devh(struct fpusb_dev_handle *devh) -{ - int r; - struct usb_urb *urb; - struct fpusb_urb_handle *urbh; - int trf_requested; - - r = ioctl(devh->fd, IOCTL_USB_REAPURBNDELAY, &urb); - if (r == -1 && errno == EAGAIN) - return r; - if (r < 0) { - fp_err("reap failed error %d errno=%d", r, errno); - return r; - } - - urbh = container_of(urb, struct fpusb_urb_handle, urb); - - fp_dbg("urb type=%d status=%d transferred=%d", urb->type, urb->status, - urb->actual_length); - list_del(&urbh->list); - - if (urb->status == -2) - return handle_transfer_cancellation(devh, urbh); - /* FIXME: research what other status codes may exist */ - if (urb->status != 0) - fp_warn("unrecognised urb status %d", urb->status); - - /* determine how much data was asked for */ - trf_requested = MIN(urbh->transfer_len - urbh->transferred, - MAX_URB_BUFFER_LENGTH); - - urbh->transferred += urb->actual_length; - - /* if we were provided less data than requested, then our transfer is - * done */ - if (urb->actual_length < trf_requested) { - fp_dbg("less data than requested (%d/%d) --> all done", - urb->actual_length, trf_requested); - return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED); - } - - /* if we've transferred all data, we're done */ - if (urbh->transferred == urbh->transfer_len) { - fp_dbg("transfer complete --> all done"); - return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED); - } - - /* otherwise, we have more data to transfer */ - fp_dbg("more data to transfer..."); - memset(urb, 0, sizeof(*urb)); - return submit_urb(devh, urbh); -} - -static void handle_timeout(struct fpusb_urb_handle *urbh) -{ - /* handling timeouts is tricky, as we may race with the kernel: we may - * detect a timeout racing with the condition that the urb has actually - * completed. we asynchronously cancel the URB and report timeout - * to the user when the URB cancellation completes (or not at all if the - * URB actually gets delivered as per this race) */ - int r; - - - urbh->flags |= FPUSB_URBH_TIMED_OUT; - r = fpusb_urb_handle_cancel(urbh->devh, urbh); - if (r < 0) - fp_warn("async cancel failed %d errno=%d", r, errno); -} - -static int handle_timeouts(void) -{ - struct timespec systime; - struct fpusb_urb_handle *urbh; - int r; - - if (list_empty(&flying_urbs)) - return 0; - - /* get current time */ - r = clock_gettime(CLOCK_MONOTONIC, &systime); - if (r < 0) - return r; - - /* iterate through flying urbs list, finding all urbs that have expired - * timeouts */ - list_for_each_entry(urbh, &flying_urbs, list) { - struct timespec *cur_ts = &urbh->timeout; - - /* if we've reached urbs of infinite timeout, we're all done */ - if (!TIMESPEC_IS_SET(cur_ts)) - return 0; - - /* if urb has non-expired timeout, nothing more to do */ - if ((cur_ts->tv_sec > systime.tv_sec) || - (cur_ts->tv_sec == systime.tv_sec && - cur_ts->tv_nsec > systime.tv_nsec)) - return 0; - - /* otherwise, we've got an expired timeout to handle */ - handle_timeout(urbh); - } - - return 0; -} - -static int reap(void) -{ - struct fpusb_dev_handle *devh; - int r; - - list_for_each_entry(devh, &open_devs, list) { - r = reap_for_devh(devh); - if (r == -1 && errno == EAGAIN) - continue; - if (r < 0) - return r; - } - - r = handle_timeouts(); - - return 0; -} - -static int flush_sigfd(void) -{ - int r; - struct signalfd_siginfo siginfo; - r = read(sigfd, &siginfo, sizeof(siginfo)); - if (r < 0) { - fp_err("sigfd read failed %d %d", r, errno); - return r; - } - if ((unsigned int) r < sizeof(siginfo)) { - fp_err("sigfd short read (%d/%d)", r, sizeof(siginfo)); - return -1; - } - return 0; -} - -static int poll_io(struct timeval *tv) -{ - int r; - fd_set fds; - - FD_ZERO(&fds); - FD_SET(sigfd, &fds); - r = select(sigfd + 1, &fds, NULL, NULL, tv); - if (r == -1 && errno == EINTR) - return 0; - if (r < 0) { - fp_err("select failed %d err=%d\n", r, errno); - return r; - } - - if (r > 0) { - flush_sigfd(); - return reap(); - } - - return 0; -} - -API_EXPORTED int fpusb_poll_timeout(struct timeval *tv) -{ - return poll_io(tv); -} - -API_EXPORTED int fpusb_poll(void) -{ - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 500000; - return poll_io(&tv); -} - -struct sync_ctrl_handle { - enum fp_urb_cb_status status; - unsigned char *data; - int actual_length; -}; - -static void ctrl_msg_cb(struct fpusb_dev_handle *devh, - struct fpusb_urb_handle *urbh, enum fp_urb_cb_status status, - struct usb_ctrl_setup *setup, unsigned char *data, int actual_length, - void *user_data) -{ - struct sync_ctrl_handle *ctrlh = (struct sync_ctrl_handle *) user_data; - fp_dbg("actual_length=%d", actual_length); - - if (status == FP_URB_COMPLETED) { - /* copy results into user-defined buffer */ - if (setup->bRequestType & USB_ENDPOINT_IN) - memcpy(ctrlh->data, data, actual_length); - } - - ctrlh->status = status; - ctrlh->actual_length = actual_length; - /* caller frees urbh */ -} - -API_EXPORTED int fpusb_ctrl_msg(struct fpusb_dev_handle *devh, - struct fpusb_ctrl_msg *msg, unsigned int timeout) -{ - struct fpusb_urb_handle *urbh; - struct sync_ctrl_handle ctrlh; - - memset(&ctrlh, 0, sizeof(ctrlh)); - ctrlh.data = msg->data; - - urbh = fpusb_submit_ctrl_msg(devh, msg, ctrl_msg_cb, &ctrlh, timeout); - if (!urbh) - return -1; - - while (!ctrlh.status) { - int r = fpusb_poll(); - if (r < 0) { - fpusb_urb_handle_cancel_sync(devh, urbh); - fpusb_urb_handle_free(urbh); - return r; - } - } - - fpusb_urb_handle_free(urbh); - switch (ctrlh.status) { - case FP_URB_COMPLETED: - return ctrlh.actual_length; - case FP_URB_TIMEOUT: - return -ETIMEDOUT; - default: - fp_warn("unrecognised status code %d", ctrlh.status); - return -1; - } -} - -struct sync_bulk_handle { - enum fp_urb_cb_status status; - int actual_length; -}; - -static void bulk_msg_cb(struct fpusb_dev_handle *devh, - struct fpusb_urb_handle *urbh, enum fp_urb_cb_status status, - unsigned char endpoint, int rqlength, unsigned char *data, - int actual_length, void *user_data) -{ - struct sync_bulk_handle *bulkh = (struct sync_bulk_handle *) user_data; - fp_dbg(""); - bulkh->status = status; - bulkh->actual_length = actual_length; - /* caller frees urbh */ -} - -static int do_sync_bulk_msg(struct fpusb_dev_handle *devh, - struct fpusb_bulk_msg *msg, int *transferred, unsigned int timeout, - unsigned char urbtype) -{ - struct fpusb_urb_handle *urbh; - struct sync_bulk_handle bulkh; - - memset(&bulkh, 0, sizeof(bulkh)); - - urbh = submit_bulk_msg(devh, msg, bulk_msg_cb, &bulkh, timeout, urbtype); - if (!urbh) - return -1; - - while (!bulkh.status) { - int r = fpusb_poll(); - if (r < 0) { - fpusb_urb_handle_cancel_sync(devh, urbh); - fpusb_urb_handle_free(urbh); - return r; - } - } - - *transferred = bulkh.actual_length; - fpusb_urb_handle_free(urbh); - - switch (bulkh.status) { - case FP_URB_COMPLETED: - return 0; - case FP_URB_TIMEOUT: - return -ETIMEDOUT; - default: - fp_warn("unrecognised status code %d", bulkh.status); - return -1; - } -} - -API_EXPORTED int fpusb_intr_msg(struct fpusb_dev_handle *devh, - struct fpusb_bulk_msg *msg, int *transferred, unsigned int timeout) -{ - return do_sync_bulk_msg(devh, msg, transferred, timeout, - USB_URB_TYPE_INTERRUPT); -} - -API_EXPORTED int fpusb_bulk_msg(struct fpusb_dev_handle *devh, - struct fpusb_bulk_msg *msg, int *transferred, unsigned int timeout) -{ - return do_sync_bulk_msg(devh, msg, transferred, timeout, - USB_URB_TYPE_BULK); -} - -API_EXPORTED void fpusb_urb_handle_free(struct fpusb_urb_handle *urbh) -{ - if (!urbh) - return; - - if (!(urbh->flags & FPUSB_URBH_DATA_BELONGS_TO_USER)) - free(urbh->urb.buffer); - free(urbh); -} - -API_EXPORTED int fpusb_get_pollfd(void) -{ - return sigfd; -} - diff --git a/libfpusb/signalfd.h b/libfpusb/signalfd.h deleted file mode 100644 index 03d4be4..0000000 --- a/libfpusb/signalfd.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * signalfd header - * Copyright (C) 2007 Daniel Drake - * - * Based on glibc header - * Copyright (C) 2007 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef __FPUSB_SIGNALFD_H__ -#define __FPUSB_SIGNALFD_H__ - -/* FIXME: in future, remove this and unconditionally use glibc directly when - * glibc-2.8 is widespread */ - -#include -#include - -#ifdef __i386__ -#define __NR_signalfd 321 -#elif defined(__x86_64__) -#define __NR_signalfd 282 -#else -#error "signalfd unsupported on this architecture" -#endif - -/* signalfd() implementation was added as of glibc-2.7 */ -#if __GLIBC_PREREQ(2, 7) -int signalfd(int fd, const sigset_t *mask, int flags); -#else -#include - -#define SIZEOF_SIG (_NSIG / 8) -#define SIZEOF_SIGSET (SIZEOF_SIG > sizeof(sigset_t) ? sizeof(sigset_t): SIZEOF_SIG) - -static inline int signalfd(int fd, const sigset_t *mask, int flags) -{ - return syscall(__NR_signalfd, fd, mask, SIZEOF_SIGSET); -} -#endif - -struct signalfd_siginfo { - uint32_t ssi_signo; - int32_t ssi_errno; - int32_t ssi_code; - uint32_t ssi_pid; - uint32_t ssi_uid; - int32_t ssi_fd; - uint32_t ssi_tid; - uint32_t ssi_band; - uint32_t ssi_overrun; - uint32_t ssi_trapno; - int32_t ssi_status; - int32_t ssi_int; - uint64_t ssi_ptr; - uint64_t ssi_utime; - uint64_t ssi_stime; - uint64_t ssi_addr; - uint8_t __pad[48]; -}; - -#endif - diff --git a/libfpusb/usbfs.h b/libfpusb/usbfs.h deleted file mode 100644 index 0a2204a..0000000 --- a/libfpusb/usbfs.h +++ /dev/null @@ -1,135 +0,0 @@ -/* - * usbfs header structures - * Copyright (C) 2007 Daniel Drake - * - * Portions based on libusb-0.1 - * Copyright (c) 2001 Johannes Erdfelt - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef __FPUSB_USBFS_H__ -#define __FPUSB_USBFS_H__ - -struct usb_ctrltransfer { - /* keep in sync with usbdevice_fs.h:usbdevfs_ctrltransfer */ - uint8_t bRequestType; - uint8_t bRequest; - uint16_t wValue; - uint16_t wIndex; - uint16_t wLength; - - uint32_t timeout; /* in milliseconds */ - - /* pointer to data */ - void *data; -}; - -struct usb_bulktransfer { - /* keep in sync with usbdevice_fs.h:usbdevfs_bulktransfer */ - unsigned int ep; - unsigned int len; - unsigned int timeout; /* in milliseconds */ - - /* pointer to data */ - void *data; -}; - -struct usb_setinterface { - /* keep in sync with usbdevice_fs.h:usbdevfs_setinterface */ - unsigned int interface; - unsigned int altsetting; -}; - -#define USB_MAXDRIVERNAME 255 - -struct usb_getdriver { - unsigned int interface; - char driver[USB_MAXDRIVERNAME + 1]; -}; - -#define USB_URB_DISABLE_SPD 1 -#define USB_URB_ISO_ASAP 2 -#define USB_URB_QUEUE_BULK 0x10 - -enum usb_urb_type { - USB_URB_TYPE_ISO = 0, - USB_URB_TYPE_INTERRUPT = 1, - USB_URB_TYPE_CONTROL = 2, - USB_URB_TYPE_BULK = 3, -}; - -struct usb_iso_packet_desc { - unsigned int length; - unsigned int actual_length; - unsigned int status; -}; - -#define MAX_URB_BUFFER_LENGTH 16384 - -struct usb_urb { - unsigned char type; - unsigned char endpoint; - int status; - unsigned int flags; - void *buffer; - int buffer_length; - int actual_length; - int start_frame; - int number_of_packets; - int error_count; - unsigned int signr; - void *usercontext; - struct usb_iso_packet_desc iso_frame_desc[0]; -}; - -struct usb_connectinfo { - unsigned int devnum; - unsigned char slow; -}; - -struct usb_ioctl { - int ifno; /* interface 0..N ; negative numbers reserved */ - int ioctl_code; /* MUST encode size + direction of data so the - * macros in give correct values */ - void *data; /* param buffer (in, or out) */ -}; - -struct usb_hub_portinfo { - unsigned char numports; - unsigned char port[127]; /* port to device num mapping */ -}; - -#define IOCTL_USB_CONTROL _IOWR('U', 0, struct usb_ctrltransfer) -#define IOCTL_USB_BULK _IOWR('U', 2, struct usb_bulktransfer) -#define IOCTL_USB_RESETEP _IOR('U', 3, unsigned int) -#define IOCTL_USB_SETINTF _IOR('U', 4, struct usb_setinterface) -#define IOCTL_USB_SETCONFIG _IOR('U', 5, unsigned int) -#define IOCTL_USB_GETDRIVER _IOW('U', 8, struct usb_getdriver) -#define IOCTL_USB_SUBMITURB _IOR('U', 10, struct usb_urb) -#define IOCTL_USB_DISCARDURB _IO('U', 11) -#define IOCTL_USB_REAPURB _IOW('U', 12, void *) -#define IOCTL_USB_REAPURBNDELAY _IOW('U', 13, void *) -#define IOCTL_USB_CLAIMINTF _IOR('U', 15, unsigned int) -#define IOCTL_USB_RELEASEINTF _IOR('U', 16, unsigned int) -#define IOCTL_USB_CONNECTINFO _IOW('U', 17, struct usb_connectinfo) -#define IOCTL_USB_IOCTL _IOWR('U', 18, struct usb_ioctl) -#define IOCTL_USB_HUB_PORTINFO _IOR('U', 19, struct usb_hub_portinfo) -#define IOCTL_USB_RESET _IO('U', 20) -#define IOCTL_USB_CLEAR_HALT _IOR('U', 21, unsigned int) -#define IOCTL_USB_DISCONNECT _IO('U', 22) -#define IOCTL_USB_CONNECT _IO('U', 23) - -#endif diff --git a/libusb-1.0.pc.in b/libusb-1.0.pc.in new file mode 100644 index 0000000..dd6bde0 --- /dev/null +++ b/libusb-1.0.pc.in @@ -0,0 +1,11 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: libusb-1.0 +Description: C API for USB device access from Linux userspace +Version: @VERSION@ +Libs: -L${libdir} -lusb-1.0 +Cflags: -I${includedir}/libusb-1.0 + diff --git a/libusb/Makefile.am b/libusb/Makefile.am new file mode 100644 index 0000000..d921468 --- /dev/null +++ b/libusb/Makefile.am @@ -0,0 +1,7 @@ +lib_LTLIBRARIES = libusb-1.0.la + +libusb_1_0_la_CFLAGS = -fvisibility=hidden $(AM_CFLAGS) +libusb_1_0_la_SOURCES = signalfd.h libusbi.h usbfs.h core.c descriptor.c io.c +libusb_1_0_la_LIBADD = -lrt + +pkginclude_HEADERS = libusb.h diff --git a/libusb/core.c b/libusb/core.c new file mode 100644 index 0000000..dc54720 --- /dev/null +++ b/libusb/core.c @@ -0,0 +1,346 @@ +/* + * Core functions for libusb + * Copyright (C) 2007 Daniel Drake + * Copyright (c) 2001 Johannes Erdfelt + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libusb.h" +#include "libusbi.h" + +static struct list_head usb_devs; +struct list_head open_devs; + +static int scan_device(char *busdir, const char *devnum) +{ + char path[PATH_MAX + 1]; + unsigned char raw_desc[DEVICE_DESC_LENGTH]; + struct libusb_dev *dev = malloc(sizeof(*dev)); + int fd = 0; + int i; + int r; + int tmp; + + if (!dev) + return -1; + + snprintf(path, PATH_MAX, "%s/%s", busdir, devnum); + fp_dbg("%s", path); + fd = open(path, O_RDWR); + if (!fd) { + fp_dbg("open '%s' failed, ret=%d errno=%d", path, fd, errno); + r = -1; + goto err; + } + + r = read(fd, raw_desc, DEVICE_DESC_LENGTH); + if (r < 0) { + fp_err("read failed ret=%d errno=%d", r, errno); + goto err; + } + /* FIXME: short read handling? */ + + fpi_parse_descriptor(raw_desc, "bbWbbbbWWWbbbb", &dev->desc); + + /* Now try to fetch the rest of the descriptors */ + if (dev->desc.bNumConfigurations > USB_MAXCONFIG) { + fp_err("too many configurations"); + r = -1; + goto err; + } + + if (dev->desc.bNumConfigurations < 1) { + fp_dbg("no configurations?"); + r = -1; + goto err; + } + + tmp = dev->desc.bNumConfigurations * sizeof(struct usb_config_descriptor); + dev->config = malloc(tmp); + if (!dev->config) { + r = -1; + goto err; + } + + memset(dev->config, 0, tmp); + + for (i = 0; i < dev->desc.bNumConfigurations; i++) { + unsigned char buffer[8], *bigbuffer; + struct usb_config_descriptor config; + + /* Get the first 8 bytes to figure out what the total length is */ + r = read(fd, buffer, sizeof(buffer)); + if (r < sizeof(buffer)) { + fp_err("short descriptor read (%d/%d)", r, sizeof(buffer)); + goto err; + } + + fpi_parse_descriptor(buffer, "bbw", &config); + + bigbuffer = malloc(config.wTotalLength); + if (!bigbuffer) + goto err; + + /* Read the rest of the config descriptor */ + memcpy(bigbuffer, buffer, sizeof(buffer)); + + tmp = config.wTotalLength - 8; + r = read(fd, bigbuffer + 8, tmp); + if (r < tmp) { + fp_err("short descriptor read (%d/%d)", r, tmp); + free(bigbuffer); + goto err; + } + + r = fpi_parse_configuration(&dev->config[i], bigbuffer); + if (r > 0) + fp_warn("descriptor data still left\n"); + free(bigbuffer); + } + + dev->nodepath = strdup(path); + if (!dev->nodepath) + goto err; + + fp_dbg("found device %04x:%04x", dev->desc.idVendor, dev->desc.idProduct); + list_add(&dev->list, &usb_devs); + r = 0; + +err: + if (fd) + close(fd); + if (r < 0 && dev) + free(dev); + return r; +} + +static int scan_busdir(const char *busnum) +{ + DIR *dir; + char dirpath[PATH_MAX + 1]; + struct dirent *entry; + + snprintf(dirpath, PATH_MAX, "%s/%s", USBFS_PATH, busnum); + fp_dbg("%s", dirpath); + dir = opendir(dirpath); + if (!dir) { + fp_err("opendir '%s' failed, errno=%d", dirpath, errno); + return -1; + } + + while ((entry = readdir(dir))) { + if (entry->d_name[0] == '.') + continue; + /* deliberately ignoring errors due to valid unplug race conditions */ + scan_device(dirpath, entry->d_name); + } + + return 0; +} + +API_EXPORTED int libusb_find_devices(void) +{ + DIR *busses; + struct dirent *entry; + fp_dbg(""); + + busses = opendir(USBFS_PATH); + if (!busses) { + fp_err("opendir busses failed errno=%d", errno); + return -1; + } + + while ((entry = readdir(busses))) { + if (entry->d_name[0] == '.') + continue; + /* deliberately ignoring errors, valid race conditions exist + * e.g. unplugging of hubs in the middle of this loop*/ + scan_busdir(entry->d_name); + } + + return 0; +} + +API_EXPORTED struct libusb_dev *libusb_get_devices(void) +{ + if (list_empty(&usb_devs)) + return NULL; + return list_entry(usb_devs.next, struct libusb_dev, list); +} + +API_EXPORTED struct libusb_dev *libusb_dev_next(struct libusb_dev *dev) +{ + struct list_head *head = &dev->list; + if (!head || head->next == &usb_devs) + return NULL; + return list_entry(head->next, struct libusb_dev, list); +} + +API_EXPORTED struct usb_dev_descriptor *libusb_dev_get_descriptor( + struct libusb_dev *dev) +{ + return &dev->desc; +} + +API_EXPORTED struct usb_config_descriptor *libusb_dev_get_config( + struct libusb_dev *dev) +{ + return dev->config; +} + +API_EXPORTED struct libusb_dev_handle *libusb_devh_open(struct libusb_dev *dev) +{ + struct libusb_dev_handle *devh; + int fd; + fp_dbg("open %04x:%04x", dev->desc.idVendor, dev->desc.idProduct); + + fd = open(dev->nodepath, O_RDWR); + if (!fd) { + fp_err("open failed, code %d errno %d", fd, errno); + return NULL; + } + + devh = malloc(sizeof(*devh)); + if (!devh) { + close(fd); + return NULL; + } + + devh->fd = fd; + devh->dev = dev; + list_add(&devh->list, &open_devs); + return devh; +} + +static void do_close(struct libusb_dev_handle *devh) +{ + close(devh->fd); +} + +API_EXPORTED void libusb_devh_close(struct libusb_dev_handle *devh) +{ + if (!devh) + return; + fp_dbg(""); + + list_del(&devh->list); + do_close(devh); + free(devh); +} + +API_EXPORTED struct libusb_dev *libusb_devh_get_dev(struct libusb_dev_handle *devh) +{ + return devh->dev; +} + +API_EXPORTED int libusb_devh_claim_intf(struct libusb_dev_handle *dev, + int iface) +{ + int r; + fp_dbg("interface %d", iface); + + r = ioctl(dev->fd, IOCTL_USB_CLAIMINTF, &iface); + if (r < 0) + fp_err("claim interface failed, error %d", r); + return r; +} + +API_EXPORTED int libusb_devh_release_intf(struct libusb_dev_handle *dev, + int iface) +{ + int r; + fp_dbg("interface %d", iface); + + r = ioctl(dev->fd, IOCTL_USB_RELEASEINTF, &iface); + if (r < 0) + fp_err("release interface failed, error %d", r); + return r; +} + +API_EXPORTED int libusb_init(int signum) +{ + /* FIXME: find correct usb node path */ + fp_dbg(""); + list_init(&usb_devs); + list_init(&open_devs); + return fpi_io_init(signum); +} + +API_EXPORTED void libusb_exit(void) +{ + struct libusb_dev_handle *devh; + fp_dbg(""); + if (!list_empty(&open_devs)) { + fp_dbg("naughty app left some devices open!\n"); + list_for_each_entry(devh, &open_devs, list) + do_close(devh); + } + fpi_io_exit(); +} + +void fpi_log(enum fpi_log_level level, const char *function, + const char *format, ...) +{ + va_list args; + FILE *stream = stdout; + const char *prefix; + + switch (level) { + case LOG_LEVEL_INFO: + prefix = "info"; + break; + case LOG_LEVEL_WARNING: + stream = stderr; + prefix = "warning"; + break; + case LOG_LEVEL_ERROR: + stream = stderr; + prefix = "error"; + break; + case LOG_LEVEL_DEBUG: + stream = stderr; + prefix = "debug"; + break; + default: + stream = stderr; + prefix = "unknown"; + break; + } + + fprintf(stream, "libusb:%s [%s] ", prefix, function); + + va_start (args, format); + vfprintf(stream, format, args); + va_end (args); + + fprintf(stream, "\n"); +} + diff --git a/libusb/descriptor.c b/libusb/descriptor.c new file mode 100644 index 0000000..d0c8bf9 --- /dev/null +++ b/libusb/descriptor.c @@ -0,0 +1,353 @@ +/* + * USB descriptor handling functions for libusb + * Copyright (C) 2007 Daniel Drake + * Copyright (c) 2001 Johannes Erdfelt + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "libusbi.h" + +#define DESC_HEADER_LENGTH 2 +#define DEVICE_DESC_LENGTH 18 +#define CONFIG_DESC_LENGTH 9 +#define INTERFACE_DESC_LENGTH 9 +#define ENDPOINT_DESC_LENGTH 7 +#define ENDPOINT_AUDIO_DESC_LENGTH 9 + +int fpi_parse_descriptor(unsigned char *source, char *descriptor, void *dest) +{ + unsigned char *sp = source, *dp = dest; + uint16_t w; + uint32_t d; + char *cp; + + for (cp = descriptor; *cp; cp++) { + switch (*cp) { + case 'b': /* 8-bit byte */ + *dp++ = *sp++; + break; + case 'w': /* 16-bit word, convert from little endian to CPU */ + w = (sp[1] << 8) | sp[0]; sp += 2; + dp += ((unsigned long)dp & 1); /* Align to word boundary */ + *((uint16_t *)dp) = w; dp += 2; + break; + case 'd': /* 32-bit dword, convert from little endian to CPU */ + d = (sp[3] << 24) | (sp[2] << 16) | (sp[1] << 8) | sp[0]; sp += 4; + dp += ((unsigned long)dp & 2); /* Align to dword boundary */ + *((uint32_t *)dp) = d; dp += 4; + break; + case 'W': /* 16-bit word, keep CPU endianess */ + dp += ((unsigned long)dp & 1); /* Align to word boundary */ + memcpy(dp, sp, 2); sp += 2; dp += 2; + break; + case 'D': /* 32-bit dword, keep CPU endianess */ + dp += ((unsigned long)dp & 2); /* Align to dword boundary */ + memcpy(dp, sp, 4); sp += 4; dp += 4; + break; + } + } + + return sp - source; +} + +static int parse_endpoint(struct usb_endpoint_descriptor *endpoint, + unsigned char *buffer, int size) +{ + struct usb_descriptor_header header; + unsigned char *begin; + int parsed = 0; + int len; + + fpi_parse_descriptor(buffer, "bb", &header); + + /* Everything should be fine being passed into here, but we sanity */ + /* check JIC */ + if (header.bLength > size) { + fp_err("ran out of descriptors parsing"); + return -1; + } + + if (header.bDescriptorType != USB_DT_ENDPOINT) { + fp_err("unexpected descriptor %x (expected %x)", + header.bDescriptorType, USB_DT_ENDPOINT); + return parsed; + } + + if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH) + fpi_parse_descriptor(buffer, "bbbbwbbb", endpoint); + else if (header.bLength >= ENDPOINT_DESC_LENGTH) + fpi_parse_descriptor(buffer, "bbbbwb", endpoint); + + buffer += header.bLength; + size -= header.bLength; + parsed += header.bLength; + + /* Skip over the rest of the Class Specific or Vendor Specific */ + /* descriptors */ + begin = buffer; + while (size >= DESC_HEADER_LENGTH) { + fpi_parse_descriptor(buffer, "bb", &header); + + if (header.bLength < 2) { + fp_err("invalid descriptor length %d", header.bLength); + return -1; + } + + /* If we find another "proper" descriptor then we're done */ + if ((header.bDescriptorType == USB_DT_ENDPOINT) || + (header.bDescriptorType == USB_DT_INTERFACE) || + (header.bDescriptorType == USB_DT_CONFIG) || + (header.bDescriptorType == USB_DT_DEVICE)) + break; + + fp_dbg("skipping descriptor %x", header.bDescriptorType); + buffer += header.bLength; + size -= header.bLength; + parsed += header.bLength; + } + + /* Copy any unknown descriptors into a storage area for drivers */ + /* to later parse */ + len = (int)(buffer - begin); + if (!len) { + endpoint->extra = NULL; + endpoint->extralen = 0; + return parsed; + } + + endpoint->extra = malloc(len); + if (!endpoint->extra) { + endpoint->extralen = 0; + return parsed; + } + + memcpy(endpoint->extra, begin, len); + endpoint->extralen = len; + + return parsed; +} + +static int parse_interface(struct usb_interface *interface, + unsigned char *buffer, int size) +{ + int i; + int len; + int r; + int parsed = 0; + int tmp; + struct usb_descriptor_header header; + struct usb_interface_descriptor *ifp; + unsigned char *begin; + + interface->num_altsetting = 0; + + while (size >= INTERFACE_DESC_LENGTH) { + interface->altsetting = realloc(interface->altsetting, + sizeof(struct usb_interface_descriptor) * + (interface->num_altsetting + 1)); + if (!interface->altsetting) + return -1; + + ifp = interface->altsetting + interface->num_altsetting; + interface->num_altsetting++; + fpi_parse_descriptor(buffer, "bbbbbbbbb", ifp); + + /* Skip over the interface */ + buffer += ifp->bLength; + parsed += ifp->bLength; + size -= ifp->bLength; + + begin = buffer; + + /* Skip over any interface, class or vendor descriptors */ + while (size >= DESC_HEADER_LENGTH) { + fpi_parse_descriptor(buffer, "bb", &header); + if (header.bLength < 2) { + fp_err("invalid descriptor of length %d", header.bLength); + return -1; + } + + /* If we find another "proper" descriptor then we're done */ + if ((header.bDescriptorType == USB_DT_INTERFACE) || + (header.bDescriptorType == USB_DT_ENDPOINT) || + (header.bDescriptorType == USB_DT_CONFIG) || + (header.bDescriptorType == USB_DT_DEVICE)) + break; + + buffer += header.bLength; + parsed += header.bLength; + size -= header.bLength; + } + + /* Copy any unknown descriptors into a storage area for */ + /* drivers to later parse */ + len = (int)(buffer - begin); + if (!len) { + ifp->extra = NULL; + ifp->extralen = 0; + } else { + ifp->extra = malloc(len); + if (!ifp->extra) { + ifp->extralen = 0; + /* FIXME will leak memory */ + return -1; + } + memcpy(ifp->extra, begin, len); + ifp->extralen = len; + } + + /* Did we hit an unexpected descriptor? */ + fpi_parse_descriptor(buffer, "bb", &header); + if ((size >= DESC_HEADER_LENGTH) && + ((header.bDescriptorType == USB_DT_CONFIG) || + (header.bDescriptorType == USB_DT_DEVICE))) + return parsed; + + if (ifp->bNumEndpoints > USB_MAXENDPOINTS) { + fp_err("too many endpoints (%d)", ifp->bNumEndpoints); + /* FIXME will leak memory */ + return -1; + } + + if (ifp->bNumEndpoints > 0) { + tmp = ifp->bNumEndpoints * sizeof(struct usb_endpoint_descriptor); + ifp->endpoint = malloc(tmp); + if (!ifp->endpoint) + /* FIXME will leak memory? */ + return -1; + + memset(ifp->endpoint, 0, tmp); + for (i = 0; i < ifp->bNumEndpoints; i++) { + fpi_parse_descriptor(buffer, "bb", &header); + + if (header.bLength > size) { + fp_err("ran out of descriptors parsing"); + /* FIXME will leak memory */ + return -1; + } + + r = parse_endpoint(ifp->endpoint + i, buffer, size); + if (r < 0) + /* FIXME will leak memory */ + return r; + + buffer += r; + parsed += r; + size -= r; + } + } else + ifp->endpoint = NULL; + + /* We check to see if it's an alternate to this one */ + ifp = (struct usb_interface_descriptor *) buffer; + if (size < USB_DT_INTERFACE_SIZE || + ifp->bDescriptorType != USB_DT_INTERFACE || + !ifp->bAlternateSetting) + return parsed; + } + + return parsed; +} + +int fpi_parse_configuration(struct usb_config_descriptor *config, + unsigned char *buffer) +{ + int i; + int r; + int size; + int tmp; + struct usb_descriptor_header header; + + fpi_parse_descriptor(buffer, "bbwbbbbb", config); + size = config->wTotalLength; + + if (config->bNumInterfaces > USB_MAXINTERFACES) { + fp_err("too many interfaces (%d)", config->bNumInterfaces); + return -1; + } + + tmp = config->bNumInterfaces * sizeof(struct usb_interface); + config->interface = malloc(tmp); + if (!config->interface) + return -1; + + memset(config->interface, 0, tmp); + buffer += config->bLength; + size -= config->bLength; + + config->extra = NULL; + config->extralen = 0; + + for (i = 0; i < config->bNumInterfaces; i++) { + int len; + unsigned char *begin; + + /* Skip over the rest of the Class Specific or Vendor */ + /* Specific descriptors */ + begin = buffer; + while (size >= DESC_HEADER_LENGTH) { + fpi_parse_descriptor(buffer, "bb", &header); + + if ((header.bLength > size) || + (header.bLength < DESC_HEADER_LENGTH)) { + fp_err("invalid descriptor length of %d", header.bLength); + return -1; + } + + /* If we find another "proper" descriptor then we're done */ + if ((header.bDescriptorType == USB_DT_ENDPOINT) || + (header.bDescriptorType == USB_DT_INTERFACE) || + (header.bDescriptorType == USB_DT_CONFIG) || + (header.bDescriptorType == USB_DT_DEVICE)) + break; + + fp_dbg("skipping descriptor 0x%x\n", header.bDescriptorType); + buffer += header.bLength; + size -= header.bLength; + } + + /* Copy any unknown descriptors into a storage area for */ + /* drivers to later parse */ + len = (int)(buffer - begin); + if (len) { + /* FIXME: We should realloc and append here */ + if (!config->extralen) { + config->extra = malloc(len); + if (!config->extra) { + config->extralen = 0; + /* FIXME will leak memory */ + return -1; + } + + memcpy(config->extra, begin, len); + config->extralen = len; + } + } + + r = parse_interface(config->interface + i, buffer, size); + if (r < 0) + return r; + + buffer += r; + size -= r; + } + + return size; +} + diff --git a/libusb/io.c b/libusb/io.c new file mode 100644 index 0000000..47e7bba --- /dev/null +++ b/libusb/io.c @@ -0,0 +1,702 @@ +/* + * I/O functions for libusb + * Copyright (C) 2007 Daniel Drake + * Copyright (c) 2001 Johannes Erdfelt + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* signalfd() support is present in glibc-2.7 onwards, but glibc-2.7 contains + * a bug where the header is neither installed or compilable. This will be + * fixed for glibc-2.8. */ +#if __GLIBC_PREREQ(2, 8) +#include +#else +#include "signalfd.h" +#endif + +#include "libusbi.h" + +static int sigfd; +static int signum; + +/* this is a list of in-flight fp_urb_handles, sorted by timeout expiration. + * URBs to timeout the soonest are placed at the beginning of the list, URBs + * that will time out later are placed after, and urbs with infinite timeout + * are always placed at the very end. */ +static struct list_head flying_urbs; + +static int setup_signalfd(int _signum) +{ + sigset_t sigset; + if (_signum == 0) + _signum = SIGRTMIN; + fp_dbg("signal %d", _signum); + + sigemptyset(&sigset); + sigaddset(&sigset, _signum); + sigfd = signalfd(-1, &sigset, 0); + if (sigfd < 0) { + fp_err("signalfd failed, code=%d errno=%d", sigfd, errno); + return sigfd; + } + fp_dbg("got signalfd %d", sigfd); + signum = _signum; + + sigemptyset(&sigset); + sigaddset(&sigset, _signum); + return sigprocmask(SIG_BLOCK, &sigset, NULL); +} + +int fpi_io_init(int _signum) +{ + list_init(&flying_urbs); + return setup_signalfd(signum); +} + +void fpi_io_exit(void) +{ + close(sigfd); +} + +static int calculate_timeout(struct libusb_urb_handle *urbh, + unsigned int timeout) +{ + int r; + struct timespec current_time; + struct sigevent sigevt = { + .sigev_notify = SIGEV_SIGNAL, + .sigev_signo = signum, + }; + struct itimerspec itspec; + struct timespec *it_value = &itspec.it_value; + + if (!timeout) + return 0; + + r = clock_gettime(CLOCK_MONOTONIC, ¤t_time); + if (r < 0) { + fp_err("failed to read monotonic clock, errno=%d", errno); + return r; + } + + r = timer_create(CLOCK_MONOTONIC, &sigevt, &urbh->timer); + if (r < 0) { + fp_err("failed to create monotonic timer"); + return r; + } + + memset(&itspec, 0, sizeof(itspec)); + it_value->tv_sec = current_time.tv_sec + (timeout / 1000); + it_value->tv_nsec = current_time.tv_nsec + + ((timeout % 1000) * 1000000); + + if (it_value->tv_nsec > 1000000000) { + it_value->tv_nsec -= 1000000000; + it_value->tv_sec++; + } + + r = timer_settime(&urbh->timer, TIMER_ABSTIME, &itspec, NULL); + if (r < 0) { + fp_err("failed to arm monotonic timer"); + return r; + } + + urbh->timeout = itspec.it_value; + + return 0; +} + +static void add_to_flying_list(struct libusb_urb_handle *urbh) +{ + struct libusb_urb_handle *cur; + struct timespec *timeout = &urbh->timeout; + + /* if we have no other flying urbs, start the list with this one */ + if (list_empty(&flying_urbs)) { + list_add(&urbh->list, &flying_urbs); + return; + } + + /* if we have infinite timeout, append to end of list */ + if (!TIMESPEC_IS_SET(timeout)) { + list_add_tail(&urbh->list, &flying_urbs); + return; + } + + /* otherwise, find appropriate place in list */ + list_for_each_entry(cur, &flying_urbs, list) { + /* find first timeout that occurs after the urbh in question */ + struct timespec *cur_ts = &cur->timeout; + + if (!TIMESPEC_IS_SET(cur_ts) || (cur_ts->tv_sec > timeout->tv_sec) || + (cur_ts->tv_sec == timeout->tv_sec && + cur_ts->tv_nsec > timeout->tv_nsec)) { + list_add_tail(&urbh->list, &cur->list); + return; + } + } + + /* otherwise we need to be inserted at the end */ + list_add_tail(&urbh->list, &flying_urbs); +} + +static int submit_urb(struct libusb_dev_handle *devh, + struct libusb_urb_handle *urbh) +{ + int r; + struct usb_urb *urb = &urbh->urb; + int to_be_transferred = urbh->transfer_len - urbh->transferred; + + urb->type = urbh->urb_type; + urb->endpoint = urbh->endpoint; + urb->buffer = urbh->buffer + urbh->transferred; + urb->buffer_length = MIN(to_be_transferred, MAX_URB_BUFFER_LENGTH); + urb->signr = signum; + + /* FIXME: for requests that we have to split into multiple URBs, we should + * submit all the URBs instantly: submit, submit, submit, reap, reap, reap + * rather than: submit, reap, submit, reap, submit, reap + * this will improve performance and fix bugs concerning behaviour when + * the user submits two similar multiple-urb requests */ + fp_dbg("transferring %d from %d bytes", urb->buffer_length, + to_be_transferred); + + r = ioctl(devh->fd, IOCTL_USB_SUBMITURB, &urbh->urb); + if (r < 0) { + fp_err("submiturb failed error %d errno=%d", r, errno); + return r; + } + + add_to_flying_list(urbh); + return 0; +} + +API_EXPORTED struct libusb_urb_handle *libusb_submit_ctrl_msg( + struct libusb_dev_handle *devh, struct libusb_ctrl_msg *msg, + libusb_ctrl_cb_fn callback, void *user_data, unsigned int timeout) +{ + struct libusb_urb_handle *urbh = malloc(sizeof(*urbh)); + struct usb_ctrl_setup *setup; + unsigned char *urbdata; + int urbdata_length = sizeof(struct usb_ctrl_setup) + msg->length; + int r; + + if (!urbh) + return NULL; + memset(urbh, 0, sizeof(*urbh)); + urbh->devh = devh; + urbh->callback = callback; + urbh->user_data = user_data; + r = calculate_timeout(urbh, timeout); + if (r < 0) { + free(urbh); + return NULL; + } + + urbdata = malloc(urbdata_length); + if (!urbdata) { + free(urbh); + return NULL; + } + + fp_dbg("RQT=%02x RQ=%02x VAL=%04x IDX=%04x length=%d", + msg->requesttype, msg->request, msg->value, msg->index, msg->length); + + setup = (struct usb_ctrl_setup *) urbdata; + setup->bRequestType = msg->requesttype; + setup->bRequest = msg->request; + setup->wValue = cpu_to_le16(msg->value); + setup->wIndex = cpu_to_le16(msg->index); + setup->wLength = cpu_to_le16(msg->length); + + if ((msg->requesttype & 0x80) == USB_ENDPOINT_OUT) + memcpy(urbdata + sizeof(struct usb_ctrl_setup), msg->data, msg->length); + + urbh->urb_type = USB_URB_TYPE_CONTROL; + urbh->buffer = urbdata; + urbh->transfer_len = urbdata_length; + + r = submit_urb(devh, urbh); + if (r < 0) { + free(urbh); + free(urbdata); + return NULL; + } + + return urbh; +} + +static struct libusb_urb_handle *submit_bulk_msg(struct libusb_dev_handle *devh, + struct libusb_bulk_msg *msg, libusb_bulk_cb_fn callback, void *user_data, + unsigned int timeout, unsigned char urbtype) +{ + struct libusb_urb_handle *urbh = malloc(sizeof(*urbh)); + int r; + + fp_dbg("length %d timeout %d", msg->length, timeout); + + if (!urbh) + return NULL; + memset(urbh, 0, sizeof(*urbh)); + r = calculate_timeout(urbh, timeout); + if (r < 0) { + free(urbh); + return NULL; + } + urbh->devh = devh; + urbh->callback = callback; + urbh->user_data = user_data; + urbh->flags |= LIBUSB_URBH_DATA_BELONGS_TO_USER; + urbh->endpoint = msg->endpoint; + urbh->urb_type = urbtype; + urbh->buffer = msg->data; + urbh->transfer_len = msg->length; + + r = submit_urb(devh, urbh); + if (r < 0) { + free(urbh); + return NULL; + } + + return urbh; +} + +API_EXPORTED struct libusb_urb_handle *libusb_submit_bulk_msg( + struct libusb_dev_handle *devh, struct libusb_bulk_msg *msg, + libusb_bulk_cb_fn callback, void *user_data, unsigned int timeout) +{ + return submit_bulk_msg(devh, msg, callback, user_data, timeout, + USB_URB_TYPE_BULK); +} + +API_EXPORTED struct libusb_urb_handle *libusb_submit_intr_msg( + struct libusb_dev_handle *devh, struct libusb_bulk_msg *msg, + libusb_bulk_cb_fn callback, void *user_data, unsigned int timeout) +{ + return submit_bulk_msg(devh, msg, callback, user_data, timeout, + USB_URB_TYPE_INTERRUPT); +} + +API_EXPORTED int libusb_urb_handle_cancel(struct libusb_dev_handle *devh, + struct libusb_urb_handle *urbh) +{ + int r; + fp_dbg(""); + r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb); + if (r < 0) + fp_err("cancel urb failed error %d", r); + return r; +} + +API_EXPORTED int libusb_urb_handle_cancel_sync(struct libusb_dev_handle *devh, + struct libusb_urb_handle *urbh) +{ + int r; + fp_dbg(""); + r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb); + if (r < 0) { + fp_err("cancel urb failed error %d", r); + return r; + } + + urbh->flags |= LIBUSB_URBH_SYNC_CANCELLED; + while (urbh->flags & LIBUSB_URBH_SYNC_CANCELLED) { + r = libusb_poll(); + if (r < 0) + return r; + } + + return 0; +} + +int handle_transfer_completion(struct libusb_dev_handle *devh, + struct libusb_urb_handle *urbh, enum fp_urb_cb_status status) +{ + struct usb_urb *urb = &urbh->urb; + + if (TIMESPEC_IS_SET(&urbh->timeout)) + timer_delete(urbh->timer); + + if (status == FP_URB_SILENT_COMPLETION) + return 0; + + if (urb->type == USB_URB_TYPE_CONTROL) { + libusb_ctrl_cb_fn callback = urbh->callback; + if (callback) + callback(devh, urbh, status, urb->buffer, + urb->buffer + sizeof(struct usb_ctrl_setup), urbh->transferred, + urbh->user_data); + } else if (urb->type == USB_URB_TYPE_BULK || + urb->type == USB_URB_TYPE_INTERRUPT) { + libusb_bulk_cb_fn callback = urbh->callback; + if (callback) + callback(devh, urbh, status, urbh->endpoint, urbh->transfer_len, + urbh->buffer, urbh->transferred, urbh->user_data); + } + return 0; +} + +static int handle_transfer_cancellation(struct libusb_dev_handle *devh, + struct libusb_urb_handle *urbh) +{ + /* if the URB is being cancelled synchronously, raise cancellation + * completion event by unsetting flag, and ensure that user callback does + * not get called. + */ + if (urbh->flags & LIBUSB_URBH_SYNC_CANCELLED) { + urbh->flags &= ~LIBUSB_URBH_SYNC_CANCELLED; + fp_dbg("detected sync. cancel"); + return handle_transfer_completion(devh, urbh, FP_URB_SILENT_COMPLETION); + } + + /* if the URB was cancelled due to timeout, report timeout to the user */ + if (urbh->flags & LIBUSB_URBH_TIMED_OUT) { + fp_dbg("detected timeout cancellation"); + return handle_transfer_completion(devh, urbh, FP_URB_TIMEOUT); + } + + /* otherwise its a normal async cancel */ + return handle_transfer_completion(devh, urbh, FP_URB_CANCELLED); +} + +static int reap_for_devh(struct libusb_dev_handle *devh) +{ + int r; + struct usb_urb *urb; + struct libusb_urb_handle *urbh; + int trf_requested; + + r = ioctl(devh->fd, IOCTL_USB_REAPURBNDELAY, &urb); + if (r == -1 && errno == EAGAIN) + return r; + if (r < 0) { + fp_err("reap failed error %d errno=%d", r, errno); + return r; + } + + urbh = container_of(urb, struct libusb_urb_handle, urb); + + fp_dbg("urb type=%d status=%d transferred=%d", urb->type, urb->status, + urb->actual_length); + list_del(&urbh->list); + + if (urb->status == -2) + return handle_transfer_cancellation(devh, urbh); + /* FIXME: research what other status codes may exist */ + if (urb->status != 0) + fp_warn("unrecognised urb status %d", urb->status); + + /* determine how much data was asked for */ + trf_requested = MIN(urbh->transfer_len - urbh->transferred, + MAX_URB_BUFFER_LENGTH); + + urbh->transferred += urb->actual_length; + + /* if we were provided less data than requested, then our transfer is + * done */ + if (urb->actual_length < trf_requested) { + fp_dbg("less data than requested (%d/%d) --> all done", + urb->actual_length, trf_requested); + return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED); + } + + /* if we've transferred all data, we're done */ + if (urbh->transferred == urbh->transfer_len) { + fp_dbg("transfer complete --> all done"); + return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED); + } + + /* otherwise, we have more data to transfer */ + fp_dbg("more data to transfer..."); + memset(urb, 0, sizeof(*urb)); + return submit_urb(devh, urbh); +} + +static void handle_timeout(struct libusb_urb_handle *urbh) +{ + /* handling timeouts is tricky, as we may race with the kernel: we may + * detect a timeout racing with the condition that the urb has actually + * completed. we asynchronously cancel the URB and report timeout + * to the user when the URB cancellation completes (or not at all if the + * URB actually gets delivered as per this race) */ + int r; + + + urbh->flags |= LIBUSB_URBH_TIMED_OUT; + r = libusb_urb_handle_cancel(urbh->devh, urbh); + if (r < 0) + fp_warn("async cancel failed %d errno=%d", r, errno); +} + +static int handle_timeouts(void) +{ + struct timespec systime; + struct libusb_urb_handle *urbh; + int r; + + if (list_empty(&flying_urbs)) + return 0; + + /* get current time */ + r = clock_gettime(CLOCK_MONOTONIC, &systime); + if (r < 0) + return r; + + /* iterate through flying urbs list, finding all urbs that have expired + * timeouts */ + list_for_each_entry(urbh, &flying_urbs, list) { + struct timespec *cur_ts = &urbh->timeout; + + /* if we've reached urbs of infinite timeout, we're all done */ + if (!TIMESPEC_IS_SET(cur_ts)) + return 0; + + /* if urb has non-expired timeout, nothing more to do */ + if ((cur_ts->tv_sec > systime.tv_sec) || + (cur_ts->tv_sec == systime.tv_sec && + cur_ts->tv_nsec > systime.tv_nsec)) + return 0; + + /* otherwise, we've got an expired timeout to handle */ + handle_timeout(urbh); + } + + return 0; +} + +static int reap(void) +{ + struct libusb_dev_handle *devh; + int r; + + list_for_each_entry(devh, &open_devs, list) { + r = reap_for_devh(devh); + if (r == -1 && errno == EAGAIN) + continue; + if (r < 0) + return r; + } + + r = handle_timeouts(); + + return 0; +} + +static int flush_sigfd(void) +{ + int r; + struct signalfd_siginfo siginfo; + r = read(sigfd, &siginfo, sizeof(siginfo)); + if (r < 0) { + fp_err("sigfd read failed %d %d", r, errno); + return r; + } + if ((unsigned int) r < sizeof(siginfo)) { + fp_err("sigfd short read (%d/%d)", r, sizeof(siginfo)); + return -1; + } + return 0; +} + +static int poll_io(struct timeval *tv) +{ + int r; + fd_set fds; + + FD_ZERO(&fds); + FD_SET(sigfd, &fds); + r = select(sigfd + 1, &fds, NULL, NULL, tv); + if (r == -1 && errno == EINTR) + return 0; + if (r < 0) { + fp_err("select failed %d err=%d\n", r, errno); + return r; + } + + if (r > 0) { + flush_sigfd(); + return reap(); + } + + return 0; +} + +API_EXPORTED int libusb_poll_timeout(struct timeval *tv) +{ + return poll_io(tv); +} + +API_EXPORTED int libusb_poll(void) +{ + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 500000; + return poll_io(&tv); +} + +struct sync_ctrl_handle { + enum fp_urb_cb_status status; + unsigned char *data; + int actual_length; +}; + +static void ctrl_msg_cb(struct libusb_dev_handle *devh, + struct libusb_urb_handle *urbh, enum fp_urb_cb_status status, + struct usb_ctrl_setup *setup, unsigned char *data, int actual_length, + void *user_data) +{ + struct sync_ctrl_handle *ctrlh = (struct sync_ctrl_handle *) user_data; + fp_dbg("actual_length=%d", actual_length); + + if (status == FP_URB_COMPLETED) { + /* copy results into user-defined buffer */ + if (setup->bRequestType & USB_ENDPOINT_IN) + memcpy(ctrlh->data, data, actual_length); + } + + ctrlh->status = status; + ctrlh->actual_length = actual_length; + /* caller frees urbh */ +} + +API_EXPORTED int libusb_ctrl_msg(struct libusb_dev_handle *devh, + struct libusb_ctrl_msg *msg, unsigned int timeout) +{ + struct libusb_urb_handle *urbh; + struct sync_ctrl_handle ctrlh; + + memset(&ctrlh, 0, sizeof(ctrlh)); + ctrlh.data = msg->data; + + urbh = libusb_submit_ctrl_msg(devh, msg, ctrl_msg_cb, &ctrlh, timeout); + if (!urbh) + return -1; + + while (!ctrlh.status) { + int r = libusb_poll(); + if (r < 0) { + libusb_urb_handle_cancel_sync(devh, urbh); + libusb_urb_handle_free(urbh); + return r; + } + } + + libusb_urb_handle_free(urbh); + switch (ctrlh.status) { + case FP_URB_COMPLETED: + return ctrlh.actual_length; + case FP_URB_TIMEOUT: + return -ETIMEDOUT; + default: + fp_warn("unrecognised status code %d", ctrlh.status); + return -1; + } +} + +struct sync_bulk_handle { + enum fp_urb_cb_status status; + int actual_length; +}; + +static void bulk_msg_cb(struct libusb_dev_handle *devh, + struct libusb_urb_handle *urbh, enum fp_urb_cb_status status, + unsigned char endpoint, int rqlength, unsigned char *data, + int actual_length, void *user_data) +{ + struct sync_bulk_handle *bulkh = (struct sync_bulk_handle *) user_data; + fp_dbg(""); + bulkh->status = status; + bulkh->actual_length = actual_length; + /* caller frees urbh */ +} + +static int do_sync_bulk_msg(struct libusb_dev_handle *devh, + struct libusb_bulk_msg *msg, int *transferred, unsigned int timeout, + unsigned char urbtype) +{ + struct libusb_urb_handle *urbh; + struct sync_bulk_handle bulkh; + + memset(&bulkh, 0, sizeof(bulkh)); + + urbh = submit_bulk_msg(devh, msg, bulk_msg_cb, &bulkh, timeout, urbtype); + if (!urbh) + return -1; + + while (!bulkh.status) { + int r = libusb_poll(); + if (r < 0) { + libusb_urb_handle_cancel_sync(devh, urbh); + libusb_urb_handle_free(urbh); + return r; + } + } + + *transferred = bulkh.actual_length; + libusb_urb_handle_free(urbh); + + switch (bulkh.status) { + case FP_URB_COMPLETED: + return 0; + case FP_URB_TIMEOUT: + return -ETIMEDOUT; + default: + fp_warn("unrecognised status code %d", bulkh.status); + return -1; + } +} + +API_EXPORTED int libusb_intr_msg(struct libusb_dev_handle *devh, + struct libusb_bulk_msg *msg, int *transferred, unsigned int timeout) +{ + return do_sync_bulk_msg(devh, msg, transferred, timeout, + USB_URB_TYPE_INTERRUPT); +} + +API_EXPORTED int libusb_bulk_msg(struct libusb_dev_handle *devh, + struct libusb_bulk_msg *msg, int *transferred, unsigned int timeout) +{ + return do_sync_bulk_msg(devh, msg, transferred, timeout, + USB_URB_TYPE_BULK); +} + +API_EXPORTED void libusb_urb_handle_free(struct libusb_urb_handle *urbh) +{ + if (!urbh) + return; + + if (!(urbh->flags & LIBUSB_URBH_DATA_BELONGS_TO_USER)) + free(urbh->urb.buffer); + free(urbh); +} + +API_EXPORTED int libusb_get_pollfd(void) +{ + return sigfd; +} + diff --git a/libusb/libusb.h b/libusb/libusb.h new file mode 100644 index 0000000..2dfe896 --- /dev/null +++ b/libusb/libusb.h @@ -0,0 +1,262 @@ +/* + * Public libusb header file + * Copyright (C) 2007 Daniel Drake + * Copyright (c) 2001 Johannes Erdfelt + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __LIBUSB_H__ +#define __LIBUSB_H__ + +#include +#include + +/* standard USB stuff */ + +/* Device and/or Interface Class codes */ +#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ +#define USB_CLASS_AUDIO 1 +#define USB_CLASS_COMM 2 +#define USB_CLASS_HID 3 +#define USB_CLASS_PRINTER 7 +#define USB_CLASS_PTP 6 +#define USB_CLASS_MASS_STORAGE 8 +#define USB_CLASS_HUB 9 +#define USB_CLASS_DATA 10 +#define USB_CLASS_VENDOR_SPEC 0xff + +/* Descriptor types */ +#define USB_DT_DEVICE 0x01 +#define USB_DT_CONFIG 0x02 +#define USB_DT_STRING 0x03 +#define USB_DT_INTERFACE 0x04 +#define USB_DT_ENDPOINT 0x05 +#define USB_DT_HID 0x21 +#define USB_DT_REPORT 0x22 +#define USB_DT_PHYSICAL 0x23 +#define USB_DT_HUB 0x29 + +/* Descriptor sizes per descriptor type */ +#define USB_DT_DEVICE_SIZE 18 +#define USB_DT_CONFIG_SIZE 9 +#define USB_DT_INTERFACE_SIZE 9 +#define USB_DT_ENDPOINT_SIZE 7 +#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ +#define USB_DT_HUB_NONVAR_SIZE 7 + +#define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ +#define USB_ENDPOINT_DIR_MASK 0x80 + +#define USB_ENDPOINT_IN 0x80 +#define USB_ENDPOINT_OUT 0x00 + +#define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */ +#define USB_ENDPOINT_TYPE_CONTROL 0 +#define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 +#define USB_ENDPOINT_TYPE_BULK 2 +#define USB_ENDPOINT_TYPE_INTERRUPT 3 + +/* Standard requests */ +#define USB_REQ_GET_STATUS 0x00 +#define USB_REQ_CLEAR_FEATURE 0x01 +/* 0x02 is reserved */ +#define USB_REQ_SET_FEATURE 0x03 +/* 0x04 is reserved */ +#define USB_REQ_SET_ADDRESS 0x05 +#define USB_REQ_GET_DESCRIPTOR 0x06 +#define USB_REQ_SET_DESCRIPTOR 0x07 +#define USB_REQ_GET_CONFIGURATION 0x08 +#define USB_REQ_SET_CONFIGURATION 0x09 +#define USB_REQ_GET_INTERFACE 0x0A +#define USB_REQ_SET_INTERFACE 0x0B +#define USB_REQ_SYNCH_FRAME 0x0C + +#define USB_TYPE_STANDARD (0x00 << 5) +#define USB_TYPE_CLASS (0x01 << 5) +#define USB_TYPE_VENDOR (0x02 << 5) +#define USB_TYPE_RESERVED (0x03 << 5) + +#define USB_RECIP_DEVICE 0x00 +#define USB_RECIP_INTERFACE 0x01 +#define USB_RECIP_ENDPOINT 0x02 +#define USB_RECIP_OTHER 0x03 + +struct usb_dev_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdUSB; + uint8_t bDeviceClass; + uint8_t bDeviceSubClass; + uint8_t bDeviceProtocol; + uint8_t bMaxPacketSize0; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t bNumConfigurations; +}; + +struct usb_endpoint_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bEndpointAddress; + uint8_t bmAttributes; + uint16_t wMaxPacketSize; + uint8_t bInterval; + uint8_t bRefresh; + uint8_t bSynchAddress; + + unsigned char *extra; /* Extra descriptors */ + int extralen; +}; + +struct usb_interface_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bInterfaceNumber; + uint8_t bAlternateSetting; + uint8_t bNumEndpoints; + uint8_t bInterfaceClass; + uint8_t bInterfaceSubClass; + uint8_t bInterfaceProtocol; + uint8_t iInterface; + + struct usb_endpoint_descriptor *endpoint; + + unsigned char *extra; /* Extra descriptors */ + int extralen; +}; + +struct usb_interface { + struct usb_interface_descriptor *altsetting; + int num_altsetting; +}; + +struct usb_config_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t wTotalLength; + uint8_t bNumInterfaces; + uint8_t bConfigurationValue; + uint8_t iConfiguration; + uint8_t bmAttributes; + uint8_t MaxPower; + + struct usb_interface *interface; + + unsigned char *extra; /* Extra descriptors */ + int extralen; +}; + +/* off-the-wire structures */ + +struct usb_ctrl_setup { + uint8_t bRequestType; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; +} __attribute__((packed)); + +/* libusb */ + +struct libusb_dev; +typedef struct libusb_dev libusb_dev; + +struct libusb_dev_handle; +typedef struct libusb_dev_handle libusb_dev_handle; + +struct libusb_urb_handle; +typedef struct libusb_urb_handle libusb_urb_handle; + +enum fp_urb_cb_status { + FP_URB_SILENT_COMPLETION = 0, + FP_URB_COMPLETED, + FP_URB_TIMEOUT, + FP_URB_CANCELLED, +}; + +struct libusb_ctrl_msg { + uint8_t requesttype; + uint8_t request; + uint16_t value; + uint16_t index; + uint16_t length; + unsigned char *data; +}; + +typedef void (*libusb_ctrl_cb_fn)(libusb_dev_handle *devh, libusb_urb_handle *urbh, + enum fp_urb_cb_status status, struct usb_ctrl_setup *setup, + unsigned char *data, int actual_length, void *user_data); + +struct libusb_bulk_msg { + unsigned char endpoint; + unsigned char *data; + int length; +}; + +typedef void (*libusb_bulk_cb_fn)(libusb_dev_handle *devh, libusb_urb_handle *urbh, + enum fp_urb_cb_status status, unsigned char endpoint, + int rqlength, unsigned char *data, int actual_length, void *user_data); + +int libusb_init(int signum); +void libusb_exit(void); + +int libusb_find_devices(void); +libusb_dev *libusb_get_devices(void); +struct usb_dev_descriptor *libusb_dev_get_descriptor(libusb_dev *dev); +struct usb_config_descriptor *libusb_dev_get_config(libusb_dev *dev); +libusb_dev *libusb_dev_next(libusb_dev *dev); + +libusb_dev_handle *libusb_devh_open(libusb_dev *dev); +void libusb_devh_close(libusb_dev_handle *devh); +struct libusb_dev *libusb_devh_get_dev(libusb_dev_handle *devh); +int libusb_devh_claim_intf(libusb_dev_handle *dev, int iface); +int libusb_devh_release_intf(libusb_dev_handle *dev, int iface); + +/* async I/O */ + +libusb_urb_handle *libusb_submit_ctrl_msg(libusb_dev_handle *devh, + struct libusb_ctrl_msg *msg, libusb_ctrl_cb_fn callback, void *user_data, + unsigned int timeout); +libusb_urb_handle *libusb_submit_bulk_msg(libusb_dev_handle *devh, + struct libusb_bulk_msg *msg, libusb_bulk_cb_fn callback, void *user_data, + unsigned int timeout); +libusb_urb_handle *libusb_submit_intr_msg(libusb_dev_handle *devh, + struct libusb_bulk_msg *msg, libusb_bulk_cb_fn callback, void *user_data, + unsigned int timeout); + +int libusb_urb_handle_cancel(libusb_dev_handle *devh, libusb_urb_handle *urbh); +int libusb_urb_handle_cancel_sync(libusb_dev_handle *devh, + libusb_urb_handle *urbh); +void libusb_urb_handle_free(libusb_urb_handle *urbh); + +int libusb_poll_timeout(struct timeval *tv); +int libusb_poll(void); +int libusb_get_pollfd(void); + +/* sync I/O */ + +int libusb_ctrl_msg(libusb_dev_handle *devh, struct libusb_ctrl_msg *msg, + unsigned int timeout); +int libusb_bulk_msg(libusb_dev_handle *devh, struct libusb_bulk_msg *msg, + int *transferred, unsigned int timeout); +int libusb_intr_msg(libusb_dev_handle *devh, struct libusb_bulk_msg *msg, + int *transferred, unsigned int timeout); + +#endif diff --git a/libusb/libusbi.h b/libusb/libusbi.h new file mode 100644 index 0000000..bea6d59 --- /dev/null +++ b/libusb/libusbi.h @@ -0,0 +1,204 @@ +/* + * Internal header for libusb + * Copyright (C) 2007 Daniel Drake + * Copyright (c) 2001 Johannes Erdfelt + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __LIBUSBI_H__ +#define __LIBUSBI_H__ + +#include + +#include +#include +#include +#include + +#include +#include + +#define USBFS_PATH "/dev/bus/usb" +#define DEVICE_DESC_LENGTH 18 + +#define USB_MAXENDPOINTS 32 +#define USB_MAXINTERFACES 32 +#define USB_MAXCONFIG 8 + +struct list_head { + struct list_head *prev, *next; +}; + +/* Get an entry from the list + * ptr - the address of this list_head element in "type" + * type - the data type that contains "member" + * member - the list_head element in "type" + */ +#define list_entry(ptr, type, member) \ + ((type *)((char *)(ptr) - (unsigned long)(&((type *)0L)->member))) + +/* Get each entry from a list + * pos - A structure pointer has a "member" element + * head - list head + * member - the list_head element in "pos" + */ +#define list_for_each_entry(pos, head, member) \ + for (pos = list_entry((head)->next, typeof(*pos), member); \ + &pos->member != (head); \ + pos = list_entry(pos->member.next, typeof(*pos), member)) + +#define list_for_each_entry_safe(pos, n, head, member) \ + for (pos = list_entry((head)->next, typeof(*pos), member), \ + n = list_entry(pos->member.next, typeof(*pos), member); \ + &pos->member != (head); \ + pos = n, n = list_entry(n->member.next, typeof(*n), member)) + +#define list_empty(entry) ((entry)->next == (entry)) + +static inline void list_init(struct list_head *entry) +{ + entry->prev = entry->next = entry; +} + +static inline void list_add(struct list_head *entry, struct list_head *head) +{ + entry->next = head->next; + entry->prev = head; + + head->next->prev = entry; + head->next = entry; +} + +static inline void list_add_tail(struct list_head *entry, + struct list_head *head) +{ + entry->next = head; + entry->prev = head->prev; + + head->prev->next = entry; + head->prev = entry; +} + +static inline void list_del(struct list_head *entry) +{ + entry->next->prev = entry->prev; + entry->prev->next = entry->next; +} + +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + +#define bswap16(x) (((x & 0xff) << 8) | (x >> 8)) +#if __BYTE_ORDER == __LITTLE_ENDIAN +#define cpu_to_le16(x) (x) +#define le16_to_cpu(x) (x) +#elif __BYTE_ORDER == __BIG_ENDIAN +#define le16_to_cpu(x) bswap16(x) +#define cpu_to_le16(x) bswap16(x) +#else +#error "Unrecognized endianness" +#endif + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0) + +enum fpi_log_level { + LOG_LEVEL_DEBUG, + LOG_LEVEL_INFO, + LOG_LEVEL_WARNING, + LOG_LEVEL_ERROR, +}; + +void fpi_log(enum fpi_log_level, const char *function, const char *format, ...); + +#ifdef ENABLE_LOGGING +#define _fpi_log(level, fmt...) fpi_log(level, __FUNCTION__, fmt) +#else +#define _fpi_log(level, fmt...) +#endif + +#ifdef ENABLE_DEBUG_LOGGING +#define fp_dbg(fmt...) _fpi_log(LOG_LEVEL_DEBUG, fmt) +#else +#define fp_dbg(fmt...) +#endif + +#define fp_info(fmt...) _fpi_log(LOG_LEVEL_INFO, fmt) +#define fp_warn(fmt...) _fpi_log(LOG_LEVEL_WARNING, fmt) +#define fp_err(fmt...) _fpi_log(LOG_LEVEL_ERROR, fmt) + +struct libusb_dev { + struct list_head list; + char *nodepath; + struct usb_dev_descriptor desc; + struct usb_config_descriptor *config; +}; + +struct libusb_dev_handle { + struct list_head list; + struct libusb_dev *dev; + int fd; +}; + +enum libusb_urb_type { + LIBUSB_URB_CONTROL, + LIBUSB_URB_BULK, +}; + +#define LIBUSB_URBH_DATA_BELONGS_TO_USER (1<<0) +#define LIBUSB_URBH_SYNC_CANCELLED (1<<1) +#define LIBUSB_URBH_TIMED_OUT (1<<2) + +struct libusb_urb_handle { + struct libusb_dev_handle *devh; + struct usb_urb urb; + struct list_head list; + struct timespec timeout; + timer_t timer; + unsigned char urb_type; + unsigned char endpoint; + int transfer_len; + int transferred; + unsigned char *buffer; + void *callback; + void *user_data; + uint8_t flags; +}; + +/* bus structures */ + +/* All standard descriptors have these 2 fields in common */ +struct usb_descriptor_header { + uint8_t bLength; + uint8_t bDescriptorType; +}; + +/* shared data and functions */ + +extern struct list_head open_devs; + +int fpi_io_init(int _signum); +void fpi_io_exit(void); + +int fpi_parse_descriptor(unsigned char *source, char *descriptor, void *dest); +int fpi_parse_configuration(struct usb_config_descriptor *config, + unsigned char *buffer); + +#endif + diff --git a/libusb/signalfd.h b/libusb/signalfd.h new file mode 100644 index 0000000..e96d693 --- /dev/null +++ b/libusb/signalfd.h @@ -0,0 +1,76 @@ +/* + * signalfd header + * Copyright (C) 2007 Daniel Drake + * + * Based on glibc header + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __LIBUSB_SIGNALFD_H__ +#define __LIBUSB_SIGNALFD_H__ + +/* FIXME: in future, remove this and unconditionally use glibc directly when + * glibc-2.8 is widespread */ + +#include +#include + +#ifdef __i386__ +#define __NR_signalfd 321 +#elif defined(__x86_64__) +#define __NR_signalfd 282 +#else +#error "signalfd unsupported on this architecture" +#endif + +/* signalfd() implementation was added as of glibc-2.7 */ +#if __GLIBC_PREREQ(2, 7) +int signalfd(int fd, const sigset_t *mask, int flags); +#else +#include + +#define SIZEOF_SIG (_NSIG / 8) +#define SIZEOF_SIGSET (SIZEOF_SIG > sizeof(sigset_t) ? sizeof(sigset_t): SIZEOF_SIG) + +static inline int signalfd(int fd, const sigset_t *mask, int flags) +{ + return syscall(__NR_signalfd, fd, mask, SIZEOF_SIGSET); +} +#endif + +struct signalfd_siginfo { + uint32_t ssi_signo; + int32_t ssi_errno; + int32_t ssi_code; + uint32_t ssi_pid; + uint32_t ssi_uid; + int32_t ssi_fd; + uint32_t ssi_tid; + uint32_t ssi_band; + uint32_t ssi_overrun; + uint32_t ssi_trapno; + int32_t ssi_status; + int32_t ssi_int; + uint64_t ssi_ptr; + uint64_t ssi_utime; + uint64_t ssi_stime; + uint64_t ssi_addr; + uint8_t __pad[48]; +}; + +#endif + diff --git a/libusb/usbfs.h b/libusb/usbfs.h new file mode 100644 index 0000000..9daf0cc --- /dev/null +++ b/libusb/usbfs.h @@ -0,0 +1,133 @@ +/* + * usbfs header structures + * Copyright (C) 2007 Daniel Drake + * Copyright (c) 2001 Johannes Erdfelt + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __LIBUSB_USBFS_H__ +#define __LIBUSB_USBFS_H__ + +struct usb_ctrltransfer { + /* keep in sync with usbdevice_fs.h:usbdevfs_ctrltransfer */ + uint8_t bRequestType; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; + + uint32_t timeout; /* in milliseconds */ + + /* pointer to data */ + void *data; +}; + +struct usb_bulktransfer { + /* keep in sync with usbdevice_fs.h:usbdevfs_bulktransfer */ + unsigned int ep; + unsigned int len; + unsigned int timeout; /* in milliseconds */ + + /* pointer to data */ + void *data; +}; + +struct usb_setinterface { + /* keep in sync with usbdevice_fs.h:usbdevfs_setinterface */ + unsigned int interface; + unsigned int altsetting; +}; + +#define USB_MAXDRIVERNAME 255 + +struct usb_getdriver { + unsigned int interface; + char driver[USB_MAXDRIVERNAME + 1]; +}; + +#define USB_URB_DISABLE_SPD 1 +#define USB_URB_ISO_ASAP 2 +#define USB_URB_QUEUE_BULK 0x10 + +enum usb_urb_type { + USB_URB_TYPE_ISO = 0, + USB_URB_TYPE_INTERRUPT = 1, + USB_URB_TYPE_CONTROL = 2, + USB_URB_TYPE_BULK = 3, +}; + +struct usb_iso_packet_desc { + unsigned int length; + unsigned int actual_length; + unsigned int status; +}; + +#define MAX_URB_BUFFER_LENGTH 16384 + +struct usb_urb { + unsigned char type; + unsigned char endpoint; + int status; + unsigned int flags; + void *buffer; + int buffer_length; + int actual_length; + int start_frame; + int number_of_packets; + int error_count; + unsigned int signr; + void *usercontext; + struct usb_iso_packet_desc iso_frame_desc[0]; +}; + +struct usb_connectinfo { + unsigned int devnum; + unsigned char slow; +}; + +struct usb_ioctl { + int ifno; /* interface 0..N ; negative numbers reserved */ + int ioctl_code; /* MUST encode size + direction of data so the + * macros in give correct values */ + void *data; /* param buffer (in, or out) */ +}; + +struct usb_hub_portinfo { + unsigned char numports; + unsigned char port[127]; /* port to device num mapping */ +}; + +#define IOCTL_USB_CONTROL _IOWR('U', 0, struct usb_ctrltransfer) +#define IOCTL_USB_BULK _IOWR('U', 2, struct usb_bulktransfer) +#define IOCTL_USB_RESETEP _IOR('U', 3, unsigned int) +#define IOCTL_USB_SETINTF _IOR('U', 4, struct usb_setinterface) +#define IOCTL_USB_SETCONFIG _IOR('U', 5, unsigned int) +#define IOCTL_USB_GETDRIVER _IOW('U', 8, struct usb_getdriver) +#define IOCTL_USB_SUBMITURB _IOR('U', 10, struct usb_urb) +#define IOCTL_USB_DISCARDURB _IO('U', 11) +#define IOCTL_USB_REAPURB _IOW('U', 12, void *) +#define IOCTL_USB_REAPURBNDELAY _IOW('U', 13, void *) +#define IOCTL_USB_CLAIMINTF _IOR('U', 15, unsigned int) +#define IOCTL_USB_RELEASEINTF _IOR('U', 16, unsigned int) +#define IOCTL_USB_CONNECTINFO _IOW('U', 17, struct usb_connectinfo) +#define IOCTL_USB_IOCTL _IOWR('U', 18, struct usb_ioctl) +#define IOCTL_USB_HUB_PORTINFO _IOR('U', 19, struct usb_hub_portinfo) +#define IOCTL_USB_RESET _IO('U', 20) +#define IOCTL_USB_CLEAR_HALT _IOR('U', 21, unsigned int) +#define IOCTL_USB_DISCONNECT _IO('U', 22) +#define IOCTL_USB_CONNECT _IO('U', 23) + +#endif -- cgit v1.2.1