From bf7093d6077131031e89e447082c4f21352aa46d Mon Sep 17 00:00:00 2001 From: Daniel Drake Date: Wed, 30 Jan 2008 14:16:35 +0000 Subject: [PATCH] Port to libusb-0.9.0 (synchronous I/O) Straightforward port to libusb-0.9.0 sync I/O functions, to give libusb-1.0 a bit of a test drive and to create a reference point for later. --- configure.ac | 2 +- libfprint/aeslib.c | 15 +++- libfprint/core.c | 33 ++++---- libfprint/data.c | 1 + libfprint/drivers/aes1610.c | 20 +++-- libfprint/drivers/aes2501.c | 20 +++-- libfprint/drivers/aes4000.c | 20 +++-- libfprint/drivers/fdu2000.c | 63 +++++++++------- libfprint/drivers/upektc.c | 29 ++++--- libfprint/drivers/upekts.c | 55 ++++++++++---- libfprint/drivers/uru4000.c | 146 +++++++++++++++++++++++++----------- libfprint/fp_internal.h | 8 +- 12 files changed, 274 insertions(+), 138 deletions(-) diff --git a/configure.ac b/configure.ac index fb128cd..c6fa828 100644 --- a/configure.ac +++ b/configure.ac @@ -17,7 +17,7 @@ AC_SUBST(lt_major) AC_SUBST(lt_revision) AC_SUBST(lt_age) -PKG_CHECK_MODULES(LIBUSB, "libusb") +PKG_CHECK_MODULES(LIBUSB, [libusb-1.0 = 0.9.0]) AC_SUBST(LIBUSB_CFLAGS) AC_SUBST(LIBUSB_LIBS) diff --git a/libfprint/aeslib.c b/libfprint/aeslib.c index f6d8dfb..9b22043 100644 --- a/libfprint/aeslib.c +++ b/libfprint/aeslib.c @@ -21,6 +21,7 @@ #include +#include #include #include "fp_internal.h" @@ -29,8 +30,8 @@ #define MAX_REGWRITES_PER_REQUEST 16 #define BULK_TIMEOUT 4000 -#define EP_IN (1 | USB_ENDPOINT_IN) -#define EP_OUT (2 | USB_ENDPOINT_OUT) +#define EP_IN (1 | LIBUSB_ENDPOINT_IN) +#define EP_OUT (2 | LIBUSB_ENDPOINT_OUT) static int do_write_regv(struct fp_img_dev *dev, const struct aes_regwrite *regs, unsigned int num) @@ -40,18 +41,24 @@ static int do_write_regv(struct fp_img_dev *dev, unsigned int i; size_t offset = 0; int r; + int transferred; + struct libusb_bulk_transfer msg = { + .endpoint = EP_OUT, + .data = data, + .length = alloc_size, + }; for (i = 0; i < num; i++) { data[offset++] = regs[i].reg; data[offset++] = regs[i].value; } - r = usb_bulk_write(dev->udev, EP_OUT, data, alloc_size, BULK_TIMEOUT); + r = libusb_bulk_transfer(dev->udev, &msg, &transferred, BULK_TIMEOUT); g_free(data); if (r < 0) { fp_err("bulk write error %d", r); return r; - } else if ((unsigned int) r < alloc_size) { + } else if ((unsigned int) transferred < alloc_size) { fp_err("unexpected short write %d/%d", r, alloc_size); return -EIO; } diff --git a/libfprint/core.c b/libfprint/core.c index 490bdc4..aeef6df 100644 --- a/libfprint/core.c +++ b/libfprint/core.c @@ -22,7 +22,7 @@ #include #include -#include +#include #include "fp_internal.h" @@ -351,18 +351,18 @@ static void register_drivers(void) } } -static struct fp_driver *find_supporting_driver(struct usb_device *udev, +static struct fp_driver *find_supporting_driver(libusb_dev *udev, const struct usb_id **usb_id) { GSList *elem = registered_drivers; + struct libusb_dev_descriptor *dsc = libusb_dev_get_descriptor(udev); do { struct fp_driver *drv = elem->data; const struct usb_id *id; for (id = drv->id_table; id->vendor; id++) - if (udev->descriptor.idVendor == id->vendor && - udev->descriptor.idProduct == id->product) { + if (dsc->idVendor == id->vendor && dsc->idProduct == id->product) { fp_dbg("driver %s supports USB device %04x:%04x", drv->name, id->vendor, id->product); *usb_id = id; @@ -372,7 +372,7 @@ static struct fp_driver *find_supporting_driver(struct usb_device *udev, return NULL; } -static struct fp_dscv_dev *discover_dev(struct usb_device *udev) +static struct fp_dscv_dev *discover_dev(libusb_dev *udev) { const struct usb_id *usb_id; struct fp_driver *drv = find_supporting_driver(udev, &usb_id); @@ -408,23 +408,20 @@ API_EXPORTED struct fp_dscv_dev **fp_discover_devs(void) { GSList *tmplist = NULL; struct fp_dscv_dev **list; - struct usb_device *udev; - struct usb_bus *bus; + struct libusb_dev *udev; int dscv_count = 0; if (registered_drivers == NULL) return NULL; - usb_find_busses(); - usb_find_devices(); + libusb_find_devices(); /* Check each device against each driver, temporarily storing successfully * discovered devices in a GSList. * * Quite inefficient but excusable as we'll only be dealing with small * sets of drivers against small sets of USB devices */ - for (bus = usb_get_busses(); bus; bus = bus->next) - for (udev = bus->devices; udev; udev = udev->next) { + for (udev = libusb_get_devices(); udev; udev = libusb_dev_next(udev)) { struct fp_dscv_dev *ddev = discover_dev(udev); if (!ddev) continue; @@ -581,7 +578,7 @@ API_EXPORTED struct fp_dev *fp_dev_open(struct fp_dscv_dev *ddev) struct fp_driver *drv = ddev->drv; int r; - usb_dev_handle *udevh = usb_open(ddev->udev); + libusb_dev_handle *udevh = libusb_open(ddev->udev); if (!udevh) { fp_err("usb_open failed"); return NULL; @@ -596,7 +593,7 @@ API_EXPORTED struct fp_dev *fp_dev_open(struct fp_dscv_dev *ddev) r = drv->init(dev, ddev->driver_data); if (r) { fp_err("device initialisation failed, driver=%s", drv->name); - usb_close(udevh); + libusb_close(udevh); g_free(dev); return NULL; } @@ -612,7 +609,7 @@ static void do_close(struct fp_dev *dev) { if (dev->drv->exit) dev->drv->exit(dev); - usb_close(dev->udev); + libusb_close(dev->udev); g_free(dev); } @@ -1119,8 +1116,13 @@ API_EXPORTED int fp_identify_finger_img(struct fp_dev *dev, */ API_EXPORTED int fp_init(void) { + int r; fp_dbg(""); - usb_init(); + + r = libusb_init(); + if (r < 0) + return r; + register_drivers(); return 0; } @@ -1147,5 +1149,6 @@ API_EXPORTED void fp_exit(void) fpi_data_exit(); g_slist_free(registered_drivers); registered_drivers = NULL; + libusb_exit(); } diff --git a/libfprint/data.c b/libfprint/data.c index 2027e9c..61dc805 100644 --- a/libfprint/data.c +++ b/libfprint/data.c @@ -19,6 +19,7 @@ #include #include +#include #include #include #include diff --git a/libfprint/drivers/aes1610.c b/libfprint/drivers/aes1610.c index eb71a4b..7ef5fe2 100644 --- a/libfprint/drivers/aes1610.c +++ b/libfprint/drivers/aes1610.c @@ -27,14 +27,14 @@ #include #include -#include +#include #include #include /* FIXME these need checking */ -#define EP_IN (1 | USB_ENDPOINT_IN) -#define EP_OUT (2 | USB_ENDPOINT_OUT) +#define EP_IN (1 | LIBUSB_ENDPOINT_IN) +#define EP_OUT (2 | LIBUSB_ENDPOINT_OUT) #define BULK_TIMEOUT 4000 @@ -63,13 +63,19 @@ static int read_data(struct fp_img_dev *dev, unsigned char *data, size_t len) { int r; + int transferred; + struct libusb_bulk_transfer msg = { + .endpoint = EP_IN, + .data = data, + .length = len, + }; fp_dbg("len=%zd", len); - r = usb_bulk_read(dev->udev, EP_IN, data, len, BULK_TIMEOUT); + r = libusb_bulk_transfer(dev->udev, &msg, &transferred, BULK_TIMEOUT); if (r < 0) { fp_err("bulk read error %d", r); return r; - } else if (r < (int) len) { + } else if (transferred < len) { fp_err("unexpected short read %d/%zd", r, len); return -EIO; } @@ -88,7 +94,7 @@ static int dev_init(struct fp_img_dev *dev, unsigned long driver_data) { int r; - r = usb_claim_interface(dev->udev, 0); + r = libusb_claim_interface(dev->udev, 0); if (r < 0) { fp_err("could not claim interface 0"); return r; @@ -107,7 +113,7 @@ static int do_exit(struct fp_img_dev *dev) static void dev_exit(struct fp_img_dev *dev) { do_exit(dev); - usb_release_interface(dev->udev, 0); + libusb_release_interface(dev->udev, 0); } static const struct aes_regwrite finger_det_reqs[] = { diff --git a/libfprint/drivers/aes2501.c b/libfprint/drivers/aes2501.c index ff7cd10..26a6e54 100644 --- a/libfprint/drivers/aes2501.c +++ b/libfprint/drivers/aes2501.c @@ -26,15 +26,15 @@ #include #include -#include +#include #include #include #include "aes2501.h" /* FIXME these need checking */ -#define EP_IN (1 | USB_ENDPOINT_IN) -#define EP_OUT (2 | USB_ENDPOINT_OUT) +#define EP_IN (1 | LIBUSB_ENDPOINT_IN) +#define EP_OUT (2 | LIBUSB_ENDPOINT_OUT) #define BULK_TIMEOUT 4000 @@ -60,13 +60,19 @@ static int read_data(struct fp_img_dev *dev, unsigned char *data, size_t len) { int r; + int transferred; + struct libusb_bulk_transfer msg = { + .endpoint = EP_IN, + .data = data, + .length = len, + }; fp_dbg("len=%zd", len); - r = usb_bulk_read(dev->udev, EP_IN, data, len, BULK_TIMEOUT); + r = libusb_bulk_transfer(dev->udev, &msg, &transferred, BULK_TIMEOUT); if (r < 0) { fp_err("bulk read error %d", r); return r; - } else if (r < len) { + } else if (transferred < len) { fp_err("unexpected short read %d/%zd", r, len); return -EIO; } @@ -232,7 +238,7 @@ static int dev_init(struct fp_img_dev *dev, unsigned long driver_data) { int r; - r = usb_claim_interface(dev->udev, 0); + r = libusb_claim_interface(dev->udev, 0); if (r < 0) { fp_err("could not claim interface 0"); return r; @@ -245,7 +251,7 @@ static int dev_init(struct fp_img_dev *dev, unsigned long driver_data) static void dev_exit(struct fp_img_dev *dev) { - usb_release_interface(dev->udev, 0); + libusb_release_interface(dev->udev, 0); } static const struct aes_regwrite finger_det_reqs[] = { diff --git a/libfprint/drivers/aes4000.c b/libfprint/drivers/aes4000.c index f2c737f..7e1ad88 100644 --- a/libfprint/drivers/aes4000.c +++ b/libfprint/drivers/aes4000.c @@ -22,14 +22,14 @@ #include #include -#include +#include #include #include #define CTRL_TIMEOUT 1000 -#define EP_IN (1 | USB_ENDPOINT_IN) -#define EP_OUT (2 | USB_ENDPOINT_OUT) +#define EP_IN (1 | LIBUSB_ENDPOINT_IN) +#define EP_OUT (2 | LIBUSB_ENDPOINT_OUT) #define DATA_BUFLEN 0x1259 #define NR_SUBARRAYS 6 #define SUBARRAY_LEN 768 @@ -114,9 +114,14 @@ static int capture(struct fp_img_dev *dev, gboolean unconditional, { int i; int r; + int transferred; struct fp_img *img; unsigned char *data; unsigned char *ptr; + struct libusb_bulk_transfer msg = { + .endpoint = EP_IN, + .length = DATA_BUFLEN, + }; r = aes_write_regv(dev, init_reqs, G_N_ELEMENTS(init_reqs)); if (r < 0) @@ -125,18 +130,19 @@ static int capture(struct fp_img_dev *dev, gboolean unconditional, img = fpi_img_new_for_imgdev(dev); data = g_malloc(DATA_BUFLEN); ptr = data; + msg.data = data; /* See the timeout explanation in the uru4000 driver for the reasoning * behind this silly loop. */ retry: - r = usb_bulk_read(dev->udev, EP_IN, data, DATA_BUFLEN, 1000); + r = libusb_bulk_transfer(dev->udev, &msg, &transferred, 1000); if (r == -ETIMEDOUT) goto retry; if (r < 0) { fp_err("data read failed, error %d", r); goto err; - } else if (r < DATA_BUFLEN) { + } else if (transferred < DATA_BUFLEN) { fp_err("short data read (%d)", r); r = -EIO; goto err; @@ -163,7 +169,7 @@ static int dev_init(struct fp_img_dev *dev, unsigned long driver_data) { int r; - r = usb_claim_interface(dev->udev, 0); + r = libusb_claim_interface(dev->udev, 0); if (r < 0) { fp_err("could not claim interface 0"); return r; @@ -174,7 +180,7 @@ static int dev_init(struct fp_img_dev *dev, unsigned long driver_data) static void dev_exit(struct fp_img_dev *dev) { - usb_release_interface(dev->udev, 0); + libusb_release_interface(dev->udev, 0); } static const struct usb_id id_table[] = { diff --git a/libfprint/drivers/fdu2000.c b/libfprint/drivers/fdu2000.c index f9ef85b..0e6c53b 100644 --- a/libfprint/drivers/fdu2000.c +++ b/libfprint/drivers/fdu2000.c @@ -21,7 +21,7 @@ #include #include -#include +#include #define FP_COMPONENT "fdu2000" #include @@ -53,9 +53,9 @@ memmem(const gpointer haystack, size_t haystack_len, const gpointer needle, size } #endif /* HAVE_MEMMEM */ -#define EP_IMAGE ( 0x02 | USB_ENDPOINT_IN ) -#define EP_REPLY ( 0x01 | USB_ENDPOINT_IN ) -#define EP_CMD ( 0x01 | USB_ENDPOINT_OUT ) +#define EP_IMAGE ( 0x02 | LIBUSB_ENDPOINT_IN ) +#define EP_REPLY ( 0x01 | LIBUSB_ENDPOINT_IN ) +#define EP_CMD ( 0x01 | LIBUSB_ENDPOINT_OUT ) #define BULK_TIMEOUT 200 /* fdu_req[] index */ @@ -112,15 +112,26 @@ static const struct fdu2000_req { * Write a command and verify reponse */ static gint -bulk_write_safe(usb_dev_handle *dev, req_index rIndex) { +bulk_write_safe(libusb_dev_handle *dev, req_index rIndex) { gchar reponse[ACK_LEN]; gint r; gchar *cmd = (gchar *)fdu_req[rIndex].cmd; gchar *ack = (gchar *)fdu_req[rIndex].ack; gint ack_len = fdu_req[rIndex].ack_len; + struct libusb_bulk_transfer wrmsg = { + .endpoint = EP_CMD, + .data = cmd, + .length = sizeof(cmd), + }; + struct libusb_bulk_transfer readmsg = { + .endpoint = EP_REPLY, + .data = reponse, + .length = sizeof(reponse), + }; + int trf; - r = usb_bulk_write(dev, EP_CMD, cmd, sizeof(cmd), BULK_TIMEOUT); + r = libusb_bulk_transfer(dev, &wrmsg, &trf, BULK_TIMEOUT); if (r < 0) return r; @@ -128,8 +139,7 @@ bulk_write_safe(usb_dev_handle *dev, req_index rIndex) { return 0; /* Check reply from FP */ - r = usb_bulk_read (dev, EP_REPLY, - reponse, sizeof(reponse), BULK_TIMEOUT); + r = libusb_bulk_transfer(dev, &readmsg, &trf, BULK_TIMEOUT); if (r < 0) return r; @@ -149,15 +159,19 @@ capture(struct fp_img_dev *dev, gboolean unconditional, #define RAW_IMAGE_SIZE (RAW_IMAGE_WIDTH * RAW_IMAGE_HEIGTH) struct fp_img *img = NULL; - guint bytes, r; + int bytes, r; const gchar SOF[] = { 0x0f, 0x0f, 0x0f, 0x0f, 0x00, 0x00, 0x0c, 0x07 }; // Start of frame const gchar SOL[] = { 0x0f, 0x0f, 0x0f, 0x0f, 0x00, 0x00, 0x0b, 0x06 }; // Start of line + { L L } (L: Line num) (8 nibbles) - gchar *buffer; + gchar *buffer = g_malloc0(RAW_IMAGE_SIZE * 6); gchar *image; gchar *p; guint offset; + struct libusb_bulk_transfer msg = { + .endpoint = EP_IMAGE, + .data = buffer, + .length = RAW_IMAGE_SIZE * 6, + }; - buffer = g_malloc0(RAW_IMAGE_SIZE * 6); image = g_malloc0(RAW_IMAGE_SIZE); if ((r = bulk_write_safe(dev->udev, LED_ON))) { @@ -178,12 +192,8 @@ read: /* Now we are ready to read from dev */ - bytes = usb_bulk_read(dev->udev, - EP_IMAGE, - buffer, RAW_IMAGE_SIZE * 6, - BULK_TIMEOUT * 10); - - if (bytes < 1) + r = libusb_bulk_transfer(dev->udev, &msg, &bytes, BULK_TIMEOUT * 10); + if (r < 0 || bytes < 1) goto read; /* @@ -249,17 +259,17 @@ static gint dev_init(struct fp_img_dev *dev, unsigned long driver_data) { gint r; - if ( (r = usb_set_configuration(dev->udev, 1)) < 0 ) + //if ( (r = usb_set_configuration(dev->udev, 1)) < 0 ) + // goto out; + + if ( (r = libusb_claim_interface(dev->udev, 0)) < 0 ) goto out; - if ( (r = usb_claim_interface(dev->udev, 0)) < 0 ) - goto out; + //if ( (r = usb_set_altinterface(dev->udev, 1)) < 0 ) + // goto out; - if ( (r = usb_set_altinterface(dev->udev, 1)) < 0 ) - goto out; - - if ( (r = usb_clear_halt(dev->udev, EP_CMD)) < 0 ) - goto out; + //if ( (r = usb_clear_halt(dev->udev, EP_CMD)) < 0 ) + // goto out; /* Make sure sensor mode is not capture_{ready|read} */ if ((r = bulk_write_safe(dev->udev, CAPTURE_END))) { @@ -276,7 +286,6 @@ gint dev_init(struct fp_img_dev *dev, unsigned long driver_data) out: fp_err("could not init dev"); - fp_err(usb_strerror()); return r; } @@ -286,7 +295,7 @@ void dev_exit(struct fp_img_dev *dev) if (bulk_write_safe(dev->udev, CAPTURE_END)) fp_err("Command: CAPTURE_END"); - usb_release_interface(dev->udev, 0); + libusb_release_interface(dev->udev, 0); } static const struct usb_id id_table[] = { diff --git a/libfprint/drivers/upektc.c b/libfprint/drivers/upektc.c index 16df00a..1723287 100644 --- a/libfprint/drivers/upektc.c +++ b/libfprint/drivers/upektc.c @@ -23,7 +23,7 @@ #include #include -#include +#include #include @@ -70,9 +70,20 @@ static const unsigned char anInitCommand[ 0x40 ] = { static sint32 askScanner( struct fp_img_dev *dev, const unsigned char *pnRawString, sint32 nLen, sint8 *pnBuffer ) { sint8 anBuf[ 65535 ]; sint32 nRet; + int transferred; + struct libusb_bulk_transfer msg1 = { + .endpoint = 3, + .data = pnRawString, + .length = 0x40, + }; + struct libusb_bulk_transfer msg2 = { + .endpoint = 0x82, + .data = anBuf, + .length = nLen, + }; - nRet = usb_bulk_write( dev -> udev, 0x00000003, pnRawString, 0x40, 1003 ); - if ( nRet != 0x40 ) { + nRet = libusb_bulk_transfer(dev->udev, &msg1, &transferred, 1003); + if (transferred != 0x40) { return -1; } @@ -80,10 +91,10 @@ static sint32 askScanner( struct fp_img_dev *dev, const unsigned char *pnRawStri return 0; } - nRet = usb_bulk_read( dev -> udev, 0x00000082, anBuf, nLen, 1003 ); - if ( ( nRet == nLen ) && ( pnBuffer != NULL ) ) { + nRet = libusb_bulk_transfer(dev->udev, &msg2, &transferred, 1003); + if ( ( transferred == nLen ) && ( pnBuffer != NULL ) ) { memcpy( pnBuffer, anBuf, nLen ); - return nRet; + return transferred; } return nRet; @@ -114,7 +125,7 @@ static sint32 ValidScan( sint8 *pnImage ) { * \return error code */ static sint32 SetupSensor( struct fp_img_dev *dev ) { - usb_claim_interface( dev -> udev, 0 ); + libusb_claim_interface(dev->udev, 0); /* setup sensor */ if ( askScanner( dev, "\x03\x00\x00\x00\x02\xfe\x00\x01\xc0\xbd\xf0\xff\xff\xff\xff\xff\x00\xf0\xfd\x7f\x00\x60\xfd\x7f\x14\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\xcc\xf8\x2f\x01\x09\x48\xe7\x77\xf0\xfa\x2f\x01\x09\x48\xe7\x77\xe0\x3a\xe6\x77", 0x00, NULL ) < 0 ) { @@ -365,7 +376,7 @@ static int capture( struct fp_img_dev *dev, gboolean unconditional, struct fp_im static int dev_init( struct fp_img_dev *dev, unsigned long driver_data ) { int nResult; - nResult = usb_claim_interface( dev -> udev, 0 ); + nResult = libusb_claim_interface(dev->udev, 0); if ( nResult < 0 ) { fp_err( "could not claim interface 0" ); return nResult; @@ -377,7 +388,7 @@ static int dev_init( struct fp_img_dev *dev, unsigned long driver_data ) { } static void dev_exit( struct fp_img_dev *dev ) { - usb_release_interface( dev -> udev, 0 ); + libusb_release_interface(dev->udev, 0); } static const struct usb_id id_table[] = { diff --git a/libfprint/drivers/upekts.c b/libfprint/drivers/upekts.c index 8b8ce08..caf5f6f 100644 --- a/libfprint/drivers/upekts.c +++ b/libfprint/drivers/upekts.c @@ -31,12 +31,12 @@ #include #include -#include +#include #include -#define EP_IN (1 | USB_ENDPOINT_IN) -#define EP_OUT (2 | USB_ENDPOINT_OUT) +#define EP_IN (1 | LIBUSB_ENDPOINT_IN) +#define EP_OUT (2 | LIBUSB_ENDPOINT_OUT) #define TIMEOUT 5000 struct upekts_dev { @@ -131,6 +131,7 @@ static int send_cmd(struct fp_dev *dev, unsigned char seq_a, unsigned char seq_b, unsigned char *data, uint16_t len) { int r; + int transferred; uint16_t crc; /* 9 bytes extra for: 4 byte 'Ciao', 1 byte A, 1 byte B | lenHI, @@ -138,6 +139,11 @@ static int send_cmd(struct fp_dev *dev, unsigned char seq_a, size_t urblen = len + 9; unsigned char *buf; + struct libusb_bulk_transfer msg = { + .endpoint = EP_OUT, + .length = urblen, + }; + if (!data && len > 0) { fp_err("len>0 but no data?"); return -EINVAL; @@ -161,12 +167,13 @@ static int send_cmd(struct fp_dev *dev, unsigned char seq_a, buf[urblen - 2] = crc >> 8; buf[urblen - 1] = crc & 0xff; - r = usb_bulk_write(dev->udev, EP_OUT, buf, urblen, TIMEOUT); + msg.data = buf; + r = libusb_bulk_transfer(dev->udev, &msg, &transferred, TIMEOUT); g_free(buf); if (r < 0) { fp_err("cmd write failed, code %d", r); return r; - } else if ((unsigned int) r < urblen) { + } else if ((unsigned int) transferred < urblen) { fp_err("cmd write too short (%d/%d)", r, urblen); return -EIO; } @@ -217,12 +224,19 @@ static unsigned char *__read_msg(struct fp_dev *dev, size_t *data_len) uint16_t computed_crc, msg_crc; uint16_t len; int r; + int transferred; - r = usb_bulk_read(dev->udev, EP_IN, buf, buf_size, TIMEOUT); + struct libusb_bulk_transfer msg = { + .endpoint = EP_IN, + .data = buf, + .length = buf_size, + }; + + r = libusb_bulk_transfer(dev->udev, &msg, &transferred, TIMEOUT); if (r < 0) { fp_err("msg read failed, code %d", r); goto err; - } else if (r < 9) { + } else if (transferred < 9) { fp_err("msg read too short (%d/%d)", r, buf_size); goto err; } @@ -247,14 +261,19 @@ static unsigned char *__read_msg(struct fp_dev *dev, size_t *data_len) * to read the remainder. This is handled below. */ if (len > MAX_DATA_IN_READ_BUF) { int needed = len - MAX_DATA_IN_READ_BUF; + struct libusb_bulk_transfer extend_msg = { + .endpoint = EP_IN, + .length = needed, + }; + fp_dbg("didn't fit in buffer, need to extend by %d bytes", needed); buf = g_realloc((gpointer) buf, MSG_READ_BUF_SIZE + needed); - r = usb_bulk_read(dev->udev, EP_IN, buf + MSG_READ_BUF_SIZE, needed, - TIMEOUT); + extend_msg.data = buf + MSG_READ_BUF_SIZE; + r = libusb_bulk_transfer(dev->udev, &extend_msg, &transferred, TIMEOUT); if (r < 0) { fp_err("extended msg read failed, code %d", r); goto err; - } else if (r < needed) { + } else if (transferred < needed) { fp_err("extended msg short read (%d/%d)", r, needed); goto err; } @@ -427,8 +446,16 @@ static int do_init(struct fp_dev *dev) uint8_t seq; int r; - r = usb_control_msg(dev->udev, USB_TYPE_VENDOR | USB_RECIP_DEVICE, - 0x0c, 0x100, 0x400, &dummy, sizeof(dummy), TIMEOUT); + struct libusb_control_transfer msg = { + .requesttype = LIBUSB_TYPE_VENDOR | LIBUSB_RECIP_DEVICE, + .request = 0x0c, + .value = 0x0100, + .index = 0x0400, + .length = sizeof(dummy), + .data = &dummy, + }; + + r = libusb_control_transfer(dev->udev, &msg, TIMEOUT); if (r < 0) { fp_dbg("control write failed\n"); return r; @@ -528,7 +555,7 @@ static int dev_init(struct fp_dev *dev, unsigned long driver_data) struct upekts_dev *upekdev = NULL; int r; - r = usb_claim_interface(dev->udev, 0); + r = libusb_claim_interface(dev->udev, 0); if (r < 0) return r; @@ -542,7 +569,7 @@ static int dev_init(struct fp_dev *dev, unsigned long driver_data) static void dev_exit(struct fp_dev *dev) { - usb_release_interface(dev->udev, 0); + libusb_release_interface(dev->udev, 0); g_free(dev->priv); } diff --git a/libfprint/drivers/uru4000.c b/libfprint/drivers/uru4000.c index 2469867..955aca0 100644 --- a/libfprint/drivers/uru4000.c +++ b/libfprint/drivers/uru4000.c @@ -24,15 +24,15 @@ #include #include -#include +#include #include -#define EP_INTR (1 | USB_ENDPOINT_IN) -#define EP_DATA (2 | USB_ENDPOINT_IN) +#define EP_INTR (1 | LIBUSB_ENDPOINT_IN) +#define EP_DATA (2 | LIBUSB_ENDPOINT_IN) #define USB_RQ 0x04 -#define CTRL_IN (USB_TYPE_VENDOR | USB_ENDPOINT_IN) -#define CTRL_OUT (USB_TYPE_VENDOR | USB_ENDPOINT_OUT) +#define CTRL_IN (LIBUSB_TYPE_VENDOR | LIBUSB_ENDPOINT_IN) +#define CTRL_OUT (LIBUSB_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT) #define CTRL_TIMEOUT 5000 #define BULK_TIMEOUT 5000 #define DATABLK1_RQLEN 0x10000 @@ -156,8 +156,16 @@ static int get_hwstat(struct fp_img_dev *dev, unsigned char *data) /* The windows driver uses a request of 0x0c here. We use 0x04 to be * consistent with every other command we know about. */ - r = usb_control_msg(dev->udev, CTRL_IN, USB_RQ, REG_HWSTAT, 0, - data, 1, CTRL_TIMEOUT); + struct libusb_control_transfer msg = { + .requesttype = CTRL_IN, + .request = USB_RQ, + .value = REG_HWSTAT, + .index = 0, + .length = 1, + .data = data, + }; + + r = libusb_control_transfer(dev->udev, &msg, CTRL_TIMEOUT); if (r < 0) { fp_err("error %d", r); return r; @@ -173,10 +181,17 @@ static int get_hwstat(struct fp_img_dev *dev, unsigned char *data) static int set_hwstat(struct fp_img_dev *dev, unsigned char data) { int r; - fp_dbg("val=%02x", data); + struct libusb_control_transfer msg = { + .requesttype = CTRL_OUT, + .request = USB_RQ, + .value = REG_HWSTAT, + .index = 0, + .length = 1, + .data = &data, + }; - r = usb_control_msg(dev->udev, CTRL_OUT, USB_RQ, REG_HWSTAT, 0, - &data, 1, CTRL_TIMEOUT); + fp_dbg("val=%02x", data); + r = libusb_control_transfer(dev->udev, &msg, CTRL_TIMEOUT); if (r < 0) { fp_err("error %d", r); return r; @@ -191,10 +206,17 @@ static int set_hwstat(struct fp_img_dev *dev, unsigned char data) static int set_mode(struct fp_img_dev *dev, unsigned char mode) { int r; + struct libusb_control_transfer msg = { + .requesttype = CTRL_OUT, + .request = USB_RQ, + .value = REG_MODE, + .index = 0, + .length = 1, + .data = &mode, + }; fp_dbg("%02x", mode); - r = usb_control_msg(dev->udev, CTRL_OUT, USB_RQ, REG_MODE, 0, &mode, 1, - CTRL_TIMEOUT); + r = libusb_control_transfer(dev->udev, &msg, CTRL_TIMEOUT); if (r < 0) { fp_err("error %d", r); return r; @@ -209,11 +231,16 @@ static int set_mode(struct fp_img_dev *dev, unsigned char mode) static int read_challenge(struct fp_img_dev *dev, unsigned char *data) { int r; + struct libusb_control_transfer msg = { + .requesttype = CTRL_IN, + .request = USB_RQ, + .value = REG_CHALLENGE, + .index = 0, + .length = CR_LENGTH, + .data = data, + }; - /* The windows driver uses a request of 0x0c here. We use 0x04 to be - * consistent with every other command we know about. */ - r = usb_control_msg(dev->udev, CTRL_IN, USB_RQ, REG_CHALLENGE, 0, - data, CR_LENGTH, CTRL_TIMEOUT); + r = libusb_control_transfer(dev->udev, &msg, CTRL_TIMEOUT); if (r < 0) { fp_err("error %d", r); return r; @@ -228,9 +255,16 @@ static int read_challenge(struct fp_img_dev *dev, unsigned char *data) static int write_response(struct fp_img_dev *dev, unsigned char *data) { int r; + struct libusb_control_transfer msg = { + .requesttype = CTRL_OUT, + .request = USB_RQ, + .value = REG_RESPONSE, + .index = 0, + .length = CR_LENGTH, + .data = data, + }; - r = usb_control_msg(dev->udev, CTRL_OUT, USB_RQ, REG_RESPONSE, 0, data, - CR_LENGTH, CTRL_TIMEOUT); + r = libusb_control_transfer(dev->udev, &msg, CTRL_TIMEOUT); if (r < 0) { fp_err("error %d", r); return r; @@ -276,6 +310,12 @@ static int get_irq(struct fp_img_dev *dev, unsigned char *buf, int timeout) uint16_t type; int r; int infinite_timeout = 0; + int transferred; + struct libusb_bulk_transfer msg = { + .endpoint = EP_INTR, + .data = buf, + .length = IRQ_LENGTH, + }; if (timeout == 0) { infinite_timeout = 1; @@ -289,7 +329,7 @@ static int get_irq(struct fp_img_dev *dev, unsigned char *buf, int timeout) * See http://thread.gmane.org/gmane.comp.lib.libusb.devel.general/1315 */ retry: - r = usb_interrupt_read(dev->udev, EP_INTR, buf, IRQ_LENGTH, timeout); + r = libusb_interrupt_transfer(dev->udev, &msg, &transferred, timeout); if (r == -ETIMEDOUT && infinite_timeout) goto retry; @@ -297,7 +337,7 @@ retry: if (r != -ETIMEDOUT) fp_err("interrupt read failed, error %d", r); return r; - } else if (r < IRQ_LENGTH) { + } else if (transferred < IRQ_LENGTH) { fp_err("received %d byte IRQ!?", r); return -EIO; } @@ -394,6 +434,16 @@ static int capture(struct fp_img_dev *dev, gboolean unconditional, struct fp_img *img; size_t image_size = DATABLK1_RQLEN + DATABLK2_EXPECT - CAPTURE_HDRLEN; int hdr_skip = CAPTURE_HDRLEN; + int transferred; + struct libusb_bulk_transfer msg1 = { + .endpoint = EP_DATA, + .length = DATABLK1_RQLEN, + }; + struct libusb_bulk_transfer msg2 = { + .endpoint = EP_DATA, + .length = DATABLK2_RQLEN, + }; + r = set_mode(dev, MODE_CAPTURE); if (r < 0) @@ -409,24 +459,24 @@ static int capture(struct fp_img_dev *dev, gboolean unconditional, * asked for. */ img = fpi_img_new(DATABLK1_RQLEN + DATABLK2_RQLEN); + msg1.data = img->data; + msg2.data = img->data + DATABLK1_RQLEN; - r = usb_bulk_read(dev->udev, EP_DATA, img->data, DATABLK1_RQLEN, - BULK_TIMEOUT); + r = libusb_bulk_transfer(dev->udev, &msg1, &transferred, BULK_TIMEOUT); if (r < 0) { fp_err("part 1 capture failed, error %d", r); goto err; - } else if (r < DATABLK1_RQLEN) { + } else if (transferred < DATABLK1_RQLEN) { fp_err("part 1 capture too short (%d)", r); r = -EIO; goto err; } - r = usb_bulk_read(dev->udev, EP_DATA, img->data + DATABLK1_RQLEN, - DATABLK2_RQLEN, BULK_TIMEOUT); + r = libusb_bulk_transfer(dev->udev, &msg2, &transferred, BULK_TIMEOUT); if (r < 0) { fp_err("part 2 capture failed, error %d", r); goto err; - } else if (r != DATABLK2_EXPECT) { + } else if (transferred != DATABLK2_EXPECT) { if (r == DATABLK2_EXPECT - CAPTURE_HDRLEN) { /* this is rather odd, but it happens sometimes with my MS * keyboard */ @@ -457,9 +507,16 @@ static int fix_firmware(struct fp_img_dev *dev) uint32_t enc_addr = FIRMWARE_START + urudev->profile->fw_enc_offset; unsigned char val, new; int r; + struct libusb_control_transfer msg = { + .requesttype = 0xc0, + .request = 0x0c, + .value = enc_addr, + .index = 0, + .data = &val, + .length = 1, + }; - r = usb_control_msg(dev->udev, 0xc0, 0x0c, enc_addr, 0, &val, 1, - CTRL_TIMEOUT); + r = libusb_control_transfer(dev->udev, &msg, CTRL_TIMEOUT); if (r < 0) return r; @@ -472,8 +529,11 @@ static int fix_firmware(struct fp_img_dev *dev) if (new == val) return 0; - r = usb_control_msg(dev->udev, 0x40, 0x04, enc_addr, 0, &new, 1, - CTRL_TIMEOUT); + msg.requesttype = 0x40; + msg.request = 0x04; + msg.data = &new; + + r = libusb_control_transfer(dev->udev, &msg, CTRL_TIMEOUT); if (r < 0) return r; @@ -585,18 +645,18 @@ retry: static int dev_init(struct fp_img_dev *dev, unsigned long driver_data) { - struct usb_config_descriptor *config; - struct usb_interface *iface = NULL; - struct usb_interface_descriptor *iface_desc; - struct usb_endpoint_descriptor *ep; + struct libusb_config_descriptor *config; + struct libusb_interface *iface = NULL; + struct libusb_interface_descriptor *iface_desc; + struct libusb_endpoint_descriptor *ep; struct uru4k_dev *urudev; int i; int r; /* Find fingerprint interface */ - config = usb_device(dev->udev)->config; + config = libusb_dev_get_config(libusb_devh_get_dev(dev->udev)); for (i = 0; i < config->bNumInterfaces; i++) { - struct usb_interface *cur_iface = &config->interface[i]; + struct libusb_interface *cur_iface = &config->interface[i]; if (cur_iface->num_altsetting < 1) continue; @@ -624,23 +684,23 @@ static int dev_init(struct fp_img_dev *dev, unsigned long driver_data) ep = &iface_desc->endpoint[0]; if (ep->bEndpointAddress != EP_INTR - || (ep->bmAttributes & USB_ENDPOINT_TYPE_MASK) != - USB_ENDPOINT_TYPE_INTERRUPT) { + || (ep->bmAttributes & LIBUSB_ENDPOINT_TYPE_MASK) != + LIBUSB_ENDPOINT_TYPE_INTERRUPT) { fp_err("unrecognised interrupt endpoint"); return -ENODEV; } ep = &iface_desc->endpoint[1]; if (ep->bEndpointAddress != EP_DATA - || (ep->bmAttributes & USB_ENDPOINT_TYPE_MASK) != - USB_ENDPOINT_TYPE_BULK) { + || (ep->bmAttributes & LIBUSB_ENDPOINT_TYPE_MASK) != + LIBUSB_ENDPOINT_TYPE_BULK) { fp_err("unrecognised bulk endpoint"); return -ENODEV; } /* Device looks like a supported reader */ - r = usb_claim_interface(dev->udev, iface_desc->bInterfaceNumber); + r = libusb_claim_interface(dev->udev, iface_desc->bInterfaceNumber); if (r < 0) { fp_err("interface claim failed"); return r; @@ -658,7 +718,7 @@ static int dev_init(struct fp_img_dev *dev, unsigned long driver_data) return 0; err: - usb_release_interface(dev->udev, iface_desc->bInterfaceNumber); + libusb_release_interface(dev->udev, iface_desc->bInterfaceNumber); g_free(urudev); return r; } @@ -669,7 +729,7 @@ static void dev_exit(struct fp_img_dev *dev) set_mode(dev, MODE_INIT); set_hwstat(dev, 0x80); - usb_release_interface(dev->udev, urudev->interface); + libusb_release_interface(dev->udev, urudev->interface); g_free(urudev); } diff --git a/libfprint/fp_internal.h b/libfprint/fp_internal.h index da8e763..b30c15b 100644 --- a/libfprint/fp_internal.h +++ b/libfprint/fp_internal.h @@ -24,7 +24,7 @@ #include #include -#include +#include #include @@ -64,7 +64,7 @@ void fpi_log(enum fpi_log_level, const char *component, const char *function, struct fp_dev { struct fp_driver *drv; - usb_dev_handle *udev; + libusb_dev_handle *udev; uint32_t devtype; void *priv; @@ -76,7 +76,7 @@ struct fp_dev { struct fp_img_dev { struct fp_dev *dev; - usb_dev_handle *udev; + libusb_dev_handle *udev; void *priv; }; @@ -153,7 +153,7 @@ void fpi_img_driver_setup(struct fp_img_driver *idriver); container_of((drv), struct fp_img_driver, driver) struct fp_dscv_dev { - struct usb_device *udev; + libusb_dev *udev; struct fp_driver *drv; unsigned long driver_data; uint32_t devtype;