From 58912f68c2489bcee787599837447e0d64dfd61a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Wed, 24 May 2017 21:03:56 +0200 Subject: New upstream version 1.0.27 --- sanei/sanei_usb.c | 262 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 147 insertions(+), 115 deletions(-) (limited to 'sanei/sanei_usb.c') diff --git a/sanei/sanei_usb.c b/sanei/sanei_usb.c index f210d4f..e4b23dc 100644 --- a/sanei/sanei_usb.c +++ b/sanei/sanei_usb.c @@ -62,31 +62,22 @@ #include #include -/* for debug messages */ -#if __STDC_VERSION__ < 199901L -# if __GNUC__ >= 2 -# define __func__ __FUNCTION__ -# else -# define __func__ "" -# endif -#endif - #ifdef HAVE_RESMGR #include #endif -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY #ifdef HAVE_LUSB0_USB_H #include #else #include #endif -#endif /* HAVE_LIBUSB */ +#endif /* HAVE_LIBUSB_LEGACY */ -#ifdef HAVE_LIBUSB_1_0 +#ifdef HAVE_LIBUSB #include -#endif /* HAVE_LIBUSB_1_0 */ +#endif /* HAVE_LIBUSB */ #ifdef HAVE_USBCALLS #include @@ -156,14 +147,14 @@ typedef struct SANE_Int interface_nr; SANE_Int alt_setting; SANE_Int missing; -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY usb_dev_handle *libusb_handle; struct usb_device *libusb_device; -#endif /* HAVE_LIBUSB */ -#ifdef HAVE_LIBUSB_1_0 +#endif /* HAVE_LIBUSB_LEGACY */ +#ifdef HAVE_LIBUSB libusb_device *lu_device; libusb_device_handle *lu_handle; -#endif /* HAVE_LIBUSB_1_0 */ +#endif /* HAVE_LIBUSB */ } device_list_type; @@ -183,13 +174,13 @@ static int device_number=0; * count number of time sanei_usb has been initialized */ static int initialized=0; -#if defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) +#if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB) static int libusb_timeout = 30 * 1000; /* 30 seconds */ -#endif /* HAVE_LIBUSB */ +#endif /* HAVE_LIBUSB_LEGACY */ -#ifdef HAVE_LIBUSB_1_0 +#ifdef HAVE_LIBUSB static libusb_context *sanei_usb_ctx; -#endif /* HAVE_LIBUSB_1_0 */ +#endif /* HAVE_LIBUSB */ #if defined (__linux__) /* From /usr/src/linux/driver/usb/scanner.h */ @@ -266,7 +257,7 @@ print_buffer (const SANE_Byte * buffer, SANE_Int size) } } -#if !defined(HAVE_LIBUSB) && !defined(HAVE_LIBUSB_1_0) +#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) static void kernel_get_vendor_product (int fd, const char *name, int *vendorID, int *productID) { @@ -340,7 +331,7 @@ kernel_get_vendor_product (int fd, const char *name, int *vendorID, int *product #endif /* defined (__linux__), defined(__BEOS__), ... */ /* put more os-dependant stuff ... */ } -#endif /* !defined(HAVE_LIBUSB) && !defined(HAVE_LIBUSB_1_0) */ +#endif /* !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) */ /** * store the given device in device list if it isn't already @@ -366,10 +357,10 @@ store_device (device_list_type device) * Need to update the LibUSB device pointer, since it might * have changed after the latest USB scan. */ -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY devices[i].libusb_device = device.libusb_device; #endif -#ifdef HAVE_LIBUSB_1_0 +#ifdef HAVE_LIBUSB devices[i].lu_device = device.lu_device; #endif @@ -408,7 +399,7 @@ store_device (device_list_type device) devices[pos].open = SANE_FALSE; } -#ifdef HAVE_LIBUSB_1_0 +#ifdef HAVE_LIBUSB static char * sanei_libusb_strerror (int errcode) { @@ -462,14 +453,14 @@ sanei_libusb_strerror (int errcode) return "Unknown libusb-1.0 error code"; } } -#endif /* HAVE_LIBUSB_1_0 */ +#endif /* HAVE_LIBUSB */ void sanei_usb_init (void) { -#ifdef HAVE_LIBUSB_1_0 +#ifdef HAVE_LIBUSB int ret; -#endif /* HAVE_LIBUSB_1_0 */ +#endif /* HAVE_LIBUSB */ DBG_INIT (); #ifdef DBG_LEVEL @@ -483,18 +474,18 @@ sanei_usb_init (void) memset (devices, 0, sizeof (devices)); /* initialize USB with old libusb library */ -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY DBG (4, "%s: Looking for libusb devices\n", __func__); usb_init (); #ifdef DBG_LEVEL if (DBG_LEVEL > 4) usb_set_debug (255); #endif /* DBG_LEVEL */ -#endif /* HAVE_LIBUSB */ +#endif /* HAVE_LIBUSB_LEGACY */ /* initialize USB using libusb-1.0 */ -#ifdef HAVE_LIBUSB_1_0 +#ifdef HAVE_LIBUSB if (!sanei_usb_ctx) { DBG (4, "%s: initializing libusb-1.0\n", __func__); @@ -511,9 +502,9 @@ sanei_usb_init (void) libusb_set_debug (sanei_usb_ctx, 3); #endif /* DBG_LEVEL */ } -#endif /* HAVE_LIBUSB_1_0 */ +#endif /* HAVE_LIBUSB */ -#if !defined(HAVE_LIBUSB) && !defined(HAVE_LIBUSB_1_0) +#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) DBG (4, "%s: SANE is built without support for libusb\n", __func__); #endif @@ -553,7 +544,7 @@ int i; devices[i].devname=NULL; } } -#ifdef HAVE_LIBUSB_1_0 +#ifdef HAVE_LIBUSB if (sanei_usb_ctx) { libusb_exit (sanei_usb_ctx); @@ -643,7 +634,7 @@ static void usbcall_scan_devices(void) } #endif /* HAVE_USBCALLS */ -#if !defined(HAVE_LIBUSB) && !defined(HAVE_LIBUSB_1_0) +#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) /** scan for devices using kernel device. * Check for devices using kernel device */ @@ -742,9 +733,9 @@ static void kernel_scan_devices(void) closedir (dir); } } -#endif /* !defined(HAVE_LIBUSB) && !defined(HAVE_LIBUSB_1_0) */ +#endif /* !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) */ -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY /** scan for devices using old libusb * Check for devices using 0.1.x libusb */ @@ -855,9 +846,9 @@ static void libusb_scan_devices(void) } } } -#endif /* HAVE_LIBUSB */ +#endif /* HAVE_LIBUSB_LEGACY */ -#ifdef HAVE_LIBUSB_1_0 +#ifdef HAVE_LIBUSB /** scan for devices using libusb * Check for devices using libusb-1.0 */ @@ -1031,7 +1022,7 @@ static void libusb_scan_devices(void) libusb_free_device_list (devlist, 1); } -#endif /* HAVE_LIBUSB_1_0 */ +#endif /* HAVE_LIBUSB */ void @@ -1057,11 +1048,11 @@ sanei_usb_scan_devices (void) } /* Check for devices using the kernel scanner driver */ -#if !defined(HAVE_LIBUSB) && !defined(HAVE_LIBUSB_1_0) +#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) kernel_scan_devices(); #endif -#if defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) +#if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB) /* Check for devices using libusb (old or new)*/ libusb_scan_devices(); #endif @@ -1308,7 +1299,6 @@ sanei_usb_open (SANE_String_Const devname, SANE_Int * dn) { int devcount; SANE_Bool found = SANE_FALSE; - int c, i, a; DBG (5, "sanei_usb_open: trying to open device `%s'\n", devname); if (!dn) @@ -1341,10 +1331,11 @@ sanei_usb_open (SANE_String_Const devname, SANE_Int * dn) if (devices[devcount].method == sanei_usb_method_libusb) { -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY struct usb_device *dev; struct usb_interface_descriptor *interface; int result, num; + int c, i, a; devices[devcount].libusb_handle = usb_open (devices[devcount].libusb_device); @@ -1600,13 +1591,14 @@ sanei_usb_open (SANE_String_Const devname, SANE_Int * dn) } } -#elif defined(HAVE_LIBUSB_1_0) /* libusb-1.0 */ +#elif defined(HAVE_LIBUSB) /* libusb-1.0 */ int config; libusb_device *dev; struct libusb_device_descriptor desc; struct libusb_config_descriptor *config0; int result, num; + int c, i, a; dev = devices[devcount].lu_device; @@ -1902,11 +1894,11 @@ sanei_usb_open (SANE_String_Const devname, SANE_Int * dn) libusb_free_config_descriptor (config); } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ DBG (1, "sanei_usb_open: can't open device `%s': " "libusb support missing\n", devname); return SANE_STATUS_UNSUPPORTED; -#endif /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ } else if (devices[devcount].method == sanei_usb_method_scanner_driver) { @@ -2108,6 +2100,17 @@ sanei_usb_open (SANE_String_Const devname, SANE_Int * dn) void sanei_usb_close (SANE_Int dn) { + char *env; + int workaround = 0; + + DBG (5, "sanei_usb_close: evaluating environment variable SANE_USB_WORKAROUND\n"); + env = getenv ("SANE_USB_WORKAROUND"); + if (env) + { + workaround = atoi(env); + DBG (5, "sanei_usb_close: workaround: %d\n", workaround); + } + DBG (5, "sanei_usb_close: closing device %d\n", dn); if (dn >= device_number || dn < 0) { @@ -2133,31 +2136,37 @@ sanei_usb_close (SANE_Int dn) #endif } else -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY { /* This call seems to be required by Linux xhci driver * even though it should be a no-op. Without it, the * host or driver does not reset it's data toggle bit. * We intentionally ignore the return val */ - sanei_usb_set_altinterface (dn, devices[dn].alt_setting); + if (workaround) + { + sanei_usb_set_altinterface (dn, devices[dn].alt_setting); + } usb_release_interface (devices[dn].libusb_handle, devices[dn].interface_nr); usb_close (devices[dn].libusb_handle); } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) { /* This call seems to be required by Linux xhci driver * even though it should be a no-op. Without it, the * host or driver does not reset it's data toggle bit. * We intentionally ignore the return val */ - sanei_usb_set_altinterface (dn, devices[dn].alt_setting); + if (workaround) + { + sanei_usb_set_altinterface (dn, devices[dn].alt_setting); + } libusb_release_interface (devices[dn].lu_handle, devices[dn].interface_nr); libusb_close (devices[dn].lu_handle); } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ DBG (1, "sanei_usb_close: libusb support missing\n"); #endif devices[dn].open = SANE_FALSE; @@ -2165,19 +2174,28 @@ sanei_usb_close (SANE_Int dn) } void -sanei_usb_set_timeout (SANE_Int timeout) +sanei_usb_set_timeout (SANE_Int __sane_unused__ timeout) { -#if defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) +#if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB) libusb_timeout = timeout; #else DBG (1, "sanei_usb_set_timeout: libusb support missing\n"); -#endif /* HAVE_LIBUSB || HAVE_LIBUSB_1_0 */ +#endif /* HAVE_LIBUSB_LEGACY || HAVE_LIBUSB */ } SANE_Status sanei_usb_clear_halt (SANE_Int dn) { - int ret; + char *env; + int workaround = 0; + + DBG (5, "sanei_usb_clear_halt: evaluating environment variable SANE_USB_WORKAROUND\n"); + env = getenv ("SANE_USB_WORKAROUND"); + if (env) + { + workaround = atoi(env); + DBG (5, "sanei_usb_clear_halt: workaround: %d\n", workaround); + } if (dn >= device_number || dn < 0) { @@ -2185,13 +2203,17 @@ sanei_usb_clear_halt (SANE_Int dn) return SANE_STATUS_INVAL; } -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY + int ret; /* This call seems to be required by Linux xhci driver * even though it should be a no-op. Without it, the * host or driver does not send the clear to the device. * We intentionally ignore the return val */ - sanei_usb_set_altinterface (dn, devices[dn].alt_setting); + if (workaround) + { + sanei_usb_set_altinterface (dn, devices[dn].alt_setting); + } ret = usb_clear_halt (devices[dn].libusb_handle, devices[dn].bulk_in_ep); if (ret){ @@ -2205,13 +2227,17 @@ sanei_usb_clear_halt (SANE_Int dn) return SANE_STATUS_INVAL; } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) + int ret; /* This call seems to be required by Linux xhci driver * even though it should be a no-op. Without it, the * host or driver does not send the clear to the device. * We intentionally ignore the return val */ - sanei_usb_set_altinterface (dn, devices[dn].alt_setting); + if (workaround) + { + sanei_usb_set_altinterface (dn, devices[dn].alt_setting); + } ret = libusb_clear_halt (devices[dn].lu_handle, devices[dn].bulk_in_ep); if (ret){ @@ -2224,17 +2250,17 @@ sanei_usb_clear_halt (SANE_Int dn) DBG (1, "sanei_usb_clear_halt: BULK_OUT ret=%d\n", ret); return SANE_STATUS_INVAL; } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ DBG (1, "sanei_usb_clear_halt: libusb support missing\n"); -#endif /* HAVE_LIBUSB || HAVE_LIBUSB_1_0 */ +#endif /* HAVE_LIBUSB_LEGACY || HAVE_LIBUSB */ return SANE_STATUS_GOOD; } SANE_Status -sanei_usb_reset (SANE_Int dn) +sanei_usb_reset (SANE_Int __sane_unused__ dn) { -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY int ret; ret = usb_reset (devices[dn].libusb_handle); @@ -2243,7 +2269,7 @@ sanei_usb_reset (SANE_Int dn) return SANE_STATUS_INVAL; } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) int ret; ret = libusb_reset_device (devices[dn].lu_handle); @@ -2252,9 +2278,9 @@ sanei_usb_reset (SANE_Int dn) return SANE_STATUS_INVAL; } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ DBG (1, "sanei_usb_reset: libusb support missing\n"); -#endif /* HAVE_LIBUSB || HAVE_LIBUSB_1_0 */ +#endif /* HAVE_LIBUSB_LEGACY || HAVE_LIBUSB */ return SANE_STATUS_GOOD; } @@ -2287,7 +2313,7 @@ sanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size) strerror (errno)); } else if (devices[dn].method == sanei_usb_method_libusb) -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY { if (devices[dn].bulk_in_ep) { @@ -2306,14 +2332,14 @@ sanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) { if (devices[dn].bulk_in_ep) { - int ret; + int ret, rsize; ret = libusb_bulk_transfer (devices[dn].lu_handle, devices[dn].bulk_in_ep, buffer, - (int) *size, (int *) &read_size, + (int) *size, &rsize, libusb_timeout); if (ret < 0) @@ -2323,6 +2349,10 @@ sanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size) read_size = -1; } + else + { + read_size = rsize; + } } else { @@ -2331,12 +2361,12 @@ sanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_read_bulk: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB */ +#endif /* not HAVE_LIBUSB_LEGACY */ else if (devices[dn].method == sanei_usb_method_usbcalls) { #ifdef HAVE_USBCALLS @@ -2382,10 +2412,10 @@ sanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size) if (read_size < 0) { -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY if (devices[dn].method == sanei_usb_method_libusb) usb_clear_halt (devices[dn].libusb_handle, devices[dn].bulk_in_ep); -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) if (devices[dn].method == sanei_usb_method_libusb) libusb_clear_halt (devices[dn].lu_handle, devices[dn].bulk_in_ep); #endif @@ -2437,7 +2467,7 @@ sanei_usb_write_bulk (SANE_Int dn, const SANE_Byte * buffer, size_t * size) strerror (errno)); } else if (devices[dn].method == sanei_usb_method_libusb) -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY { if (devices[dn].bulk_out_ep) { @@ -2456,7 +2486,7 @@ sanei_usb_write_bulk (SANE_Int dn, const SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) { if (devices[dn].bulk_out_ep) { @@ -2464,7 +2494,7 @@ sanei_usb_write_bulk (SANE_Int dn, const SANE_Byte * buffer, size_t * size) int trans_bytes; ret = libusb_bulk_transfer (devices[dn].lu_handle, devices[dn].bulk_out_ep, - buffer, + (unsigned char *) buffer, (int) *size, &trans_bytes, libusb_timeout); if (ret < 0) @@ -2484,12 +2514,12 @@ sanei_usb_write_bulk (SANE_Int dn, const SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_write_bulk: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else if (devices[dn].method == sanei_usb_method_usbcalls) { #ifdef HAVE_USBCALLS @@ -2537,10 +2567,10 @@ sanei_usb_write_bulk (SANE_Int dn, const SANE_Byte * buffer, size_t * size) if (write_size < 0) { *size = 0; -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY if (devices[dn].method == sanei_usb_method_libusb) usb_clear_halt (devices[dn].libusb_handle, devices[dn].bulk_out_ep); -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) if (devices[dn].method == sanei_usb_method_libusb) libusb_clear_halt (devices[dn].lu_handle, devices[dn].bulk_out_ep); #endif @@ -2616,7 +2646,7 @@ sanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req, #endif /* not __linux__ */ } else if (devices[dn].method == sanei_usb_method_libusb) -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY { int result; @@ -2633,7 +2663,7 @@ sanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req, print_buffer (data, len); return SANE_STATUS_GOOD; } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) { int result; @@ -2650,12 +2680,12 @@ sanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req, print_buffer (data, len); return SANE_STATUS_GOOD; } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0*/ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB*/ { DBG (1, "sanei_usb_control_msg: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else if (devices[dn].method == sanei_usb_method_usbcalls) { #ifdef HAVE_USBCALLS @@ -2692,7 +2722,7 @@ SANE_Status sanei_usb_read_int (SANE_Int dn, SANE_Byte * buffer, size_t * size) { ssize_t read_size = 0; -#if defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) +#if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB) SANE_Bool stalled = SANE_FALSE; #endif @@ -2717,7 +2747,7 @@ sanei_usb_read_int (SANE_Int dn, SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } else if (devices[dn].method == sanei_usb_method_libusb) -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY { if (devices[dn].int_in_ep) { @@ -2739,7 +2769,7 @@ sanei_usb_read_int (SANE_Int dn, SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) { if (devices[dn].int_in_ep) { @@ -2764,12 +2794,12 @@ sanei_usb_read_int (SANE_Int dn, SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_read_int: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else if (devices[dn].method == sanei_usb_method_usbcalls) { #ifdef HAVE_USBCALLS @@ -2808,11 +2838,11 @@ sanei_usb_read_int (SANE_Int dn, SANE_Byte * buffer, size_t * size) if (read_size < 0) { -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY if (devices[dn].method == sanei_usb_method_libusb) if (stalled) usb_clear_halt (devices[dn].libusb_handle, devices[dn].int_in_ep); -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) if (devices[dn].method == sanei_usb_method_libusb) if (stalled) libusb_clear_halt (devices[dn].lu_handle, devices[dn].int_in_ep); @@ -2858,7 +2888,7 @@ sanei_usb_set_configuration (SANE_Int dn, SANE_Int configuration) #endif /* not __linux__ */ } else if (devices[dn].method == sanei_usb_method_libusb) -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY { int result; @@ -2872,7 +2902,7 @@ sanei_usb_set_configuration (SANE_Int dn, SANE_Int configuration) } return SANE_STATUS_GOOD; } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) { int result; @@ -2885,12 +2915,12 @@ sanei_usb_set_configuration (SANE_Int dn, SANE_Int configuration) } return SANE_STATUS_GOOD; } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_set_configuration: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else { DBG (1, @@ -2928,7 +2958,7 @@ sanei_usb_claim_interface (SANE_Int dn, SANE_Int interface_number) #endif /* not __linux__ */ } else if (devices[dn].method == sanei_usb_method_libusb) -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY { int result; @@ -2941,7 +2971,7 @@ sanei_usb_claim_interface (SANE_Int dn, SANE_Int interface_number) } return SANE_STATUS_GOOD; } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) { int result; @@ -2954,12 +2984,12 @@ sanei_usb_claim_interface (SANE_Int dn, SANE_Int interface_number) } return SANE_STATUS_GOOD; } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_claim_interface: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else { DBG (1, "sanei_usb_claim_interface: access method %d not implemented\n", @@ -2995,7 +3025,7 @@ sanei_usb_release_interface (SANE_Int dn, SANE_Int interface_number) #endif /* not __linux__ */ } else if (devices[dn].method == sanei_usb_method_libusb) -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY { int result; @@ -3008,7 +3038,7 @@ sanei_usb_release_interface (SANE_Int dn, SANE_Int interface_number) } return SANE_STATUS_GOOD; } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) { int result; @@ -3021,12 +3051,12 @@ sanei_usb_release_interface (SANE_Int dn, SANE_Int interface_number) } return SANE_STATUS_GOOD; } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_release_interface: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else { DBG (1, @@ -3061,7 +3091,7 @@ sanei_usb_set_altinterface (SANE_Int dn, SANE_Int alternate) #endif /* not __linux__ */ } else if (devices[dn].method == sanei_usb_method_libusb) -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY { int result; @@ -3074,7 +3104,7 @@ sanei_usb_set_altinterface (SANE_Int dn, SANE_Int alternate) } return SANE_STATUS_GOOD; } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) { int result; @@ -3088,12 +3118,12 @@ sanei_usb_set_altinterface (SANE_Int dn, SANE_Int alternate) } return SANE_STATUS_GOOD; } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_set_altinterface: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else { DBG (1, @@ -3104,7 +3134,9 @@ sanei_usb_set_altinterface (SANE_Int dn, SANE_Int alternate) } extern SANE_Status -sanei_usb_get_descriptor( SANE_Int dn, struct sanei_usb_dev_descriptor *desc ) +sanei_usb_get_descriptor( SANE_Int dn, + struct sanei_usb_dev_descriptor __sane_unused__ + *desc ) { if (dn >= device_number || dn < 0) { @@ -3115,7 +3147,7 @@ sanei_usb_get_descriptor( SANE_Int dn, struct sanei_usb_dev_descriptor *desc ) } DBG (5, "sanei_usb_get_descriptor\n"); -#ifdef HAVE_LIBUSB +#ifdef HAVE_LIBUSB_LEGACY { struct usb_device_descriptor *usb_descr; @@ -3130,7 +3162,7 @@ sanei_usb_get_descriptor( SANE_Int dn, struct sanei_usb_dev_descriptor *desc ) desc->max_packet_size = usb_descr->bMaxPacketSize0; return SANE_STATUS_GOOD; } -#elif defined(HAVE_LIBUSB_1_0) +#elif defined(HAVE_LIBUSB) { struct libusb_device_descriptor lu_desc; int ret; @@ -3155,10 +3187,10 @@ sanei_usb_get_descriptor( SANE_Int dn, struct sanei_usb_dev_descriptor *desc ) desc->max_packet_size = lu_desc.bMaxPacketSize0; return SANE_STATUS_GOOD; } -#else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_get_descriptor: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ } -- cgit v1.2.3 From 1687222e1b9e74c89cafbb5910e72d8ec7bfd40f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Wed, 31 Jul 2019 16:59:49 +0200 Subject: New upstream version 1.0.28 --- sanei/sanei_usb.c | 427 ++++++++++++++++-------------------------------------- 1 file changed, 124 insertions(+), 303 deletions(-) (limited to 'sanei/sanei_usb.c') diff --git a/sanei/sanei_usb.c b/sanei/sanei_usb.c index e4b23dc..6fd6040 100644 --- a/sanei/sanei_usb.c +++ b/sanei/sanei_usb.c @@ -1262,6 +1262,77 @@ sanei_usb_set_endpoint (SANE_Int dn, SANE_Int ep_type, SANE_Int ep) } } +#if HAVE_LIBUSB_LEGACY || HAVE_LIBUSB || HAVE_USBCALLS +static const char* sanei_usb_transfer_type_desc(SANE_Int transfer_type) +{ + switch (transfer_type) + { + case USB_ENDPOINT_TYPE_INTERRUPT: return "interrupt"; + case USB_ENDPOINT_TYPE_BULK: return "bulk"; + case USB_ENDPOINT_TYPE_ISOCHRONOUS: return "isochronous"; + case USB_ENDPOINT_TYPE_CONTROL: return "control"; + } + return NULL; +} + +// Similar sanei_usb_set_endpoint, but ignors duplicate endpoints +static void sanei_usb_add_endpoint(device_list_type* device, + SANE_Int transfer_type, + SANE_Int ep_address, + SANE_Int ep_direction) +{ + DBG(5, "%s: direction: %d, address: %d, transfer_type: %d\n", + __func__, ep_direction, ep_address, transfer_type); + + SANE_Int* ep_in = NULL; + SANE_Int* ep_out = NULL; + const char* transfer_type_msg = sanei_usb_transfer_type_desc(transfer_type); + + switch (transfer_type) + { + case USB_ENDPOINT_TYPE_INTERRUPT: + ep_in = &device->int_in_ep; + ep_out = &device->int_out_ep; + break; + case USB_ENDPOINT_TYPE_BULK: + ep_in = &device->bulk_in_ep; + ep_out = &device->bulk_out_ep; + break; + case USB_ENDPOINT_TYPE_ISOCHRONOUS: + ep_in = &device->iso_in_ep; + ep_out = &device->iso_out_ep; + break; + case USB_ENDPOINT_TYPE_CONTROL: + ep_in = &device->control_in_ep; + ep_out = &device->control_out_ep; + break; + } + + DBG(5, "%s: found %s-%s endpoint (address 0x%02x)\n", + __func__, transfer_type_msg, ep_direction ? "in" : "out", + ep_address); + + if (ep_direction) // in + { + if (*ep_in) + DBG(3, "%s: we already have a %s-in endpoint " + "(address: 0x%02x), ignoring the new one\n", + __func__, transfer_type_msg, *ep_in); + else + *ep_in = ep_address; + } + else + { + if (*ep_out) + DBG(3, "%s: we already have a %s-out endpoint " + "(address: 0x%02x), ignoring the new one\n", + __func__, transfer_type_msg, *ep_out); + else + *ep_out = ep_address; + } +} +#endif // HAVE_LIBUSB_LEGACY || HAVE_LIBUSB || HAVE_USBCALLS + SANE_Int sanei_usb_get_endpoint (SANE_Int dn, SANE_Int ep_type) { @@ -1460,132 +1531,12 @@ sanei_usb_open (SANE_String_Const devname, SANE_Int * dn) DBG (5, "sanei_usb_open: endpoint nr: %d\n", num); transfer_type = endpoint->bmAttributes & USB_ENDPOINT_TYPE_MASK; - address = - endpoint-> - bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK; direction = endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK; - DBG (5, "sanei_usb_open: direction: %d\n", direction); - - DBG (5, - "sanei_usb_open: address: %d transfertype: %d\n", - address, transfer_type); - - - /* save the endpoints we need later */ - if (transfer_type == USB_ENDPOINT_TYPE_INTERRUPT) - { - DBG (5, - "sanei_usb_open: found interrupt-%s endpoint (address 0x%02x)\n", - direction ? "in" : "out", address); - if (direction) /* in */ - { - if (devices[devcount].int_in_ep) - DBG (3, - "sanei_usb_open: we already have a int-in endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].int_in_ep); - else - devices[devcount].int_in_ep = - endpoint->bEndpointAddress; - } - else - { - if (devices[devcount].int_out_ep) - DBG (3, - "sanei_usb_open: we already have a int-out endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].int_out_ep); - else - devices[devcount].int_out_ep = - endpoint->bEndpointAddress; - } - } - else if (transfer_type == USB_ENDPOINT_TYPE_BULK) - { - DBG (5, - "sanei_usb_open: found bulk-%s endpoint (address 0x%02x)\n", - direction ? "in" : "out", address); - if (direction) /* in */ - { - if (devices[devcount].bulk_in_ep) - DBG (3, - "sanei_usb_open: we already have a bulk-in endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].bulk_in_ep); - else - devices[devcount].bulk_in_ep = - endpoint->bEndpointAddress; - } - else - { - if (devices[devcount].bulk_out_ep) - DBG (3, - "sanei_usb_open: we already have a bulk-out endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].bulk_out_ep); - else - devices[devcount].bulk_out_ep = - endpoint->bEndpointAddress; - } - } - else if (transfer_type == USB_ENDPOINT_TYPE_ISOCHRONOUS) - { - DBG (5, - "sanei_usb_open: found isochronous-%s endpoint (address 0x%02x)\n", - direction ? "in" : "out", address); - if (direction) /* in */ - { - if (devices[devcount].iso_in_ep) - DBG (3, - "sanei_usb_open: we already have a isochronous-in endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].iso_in_ep); - else - devices[devcount].iso_in_ep = - endpoint->bEndpointAddress; - } - else - { - if (devices[devcount].iso_out_ep) - DBG (3, - "sanei_usb_open: we already have a isochronous-out endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].iso_out_ep); - else - devices[devcount].iso_out_ep = - endpoint->bEndpointAddress; - } - } - else if (transfer_type == USB_ENDPOINT_TYPE_CONTROL) - { - DBG (5, - "sanei_usb_open: found control-%s endpoint (address 0x%02x)\n", - direction ? "in" : "out", address); - if (direction) /* in */ - { - if (devices[devcount].control_in_ep) - DBG (3, - "sanei_usb_open: we already have a control-in endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].control_in_ep); - else - devices[devcount].control_in_ep = - endpoint->bEndpointAddress; - } - else - { - if (devices[devcount].control_out_ep) - DBG (3, - "sanei_usb_open: we already have a control-out endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].control_out_ep); - else - devices[devcount].control_out_ep = - endpoint->bEndpointAddress; - } - } + sanei_usb_add_endpoint(&devices[devcount], transfer_type, + endpoint->bEndpointAddress, + direction); } } } @@ -1769,124 +1720,39 @@ sanei_usb_open (SANE_String_Const devname, SANE_Int * dn) for (num = 0; num < interface->bNumEndpoints; num++) { const struct libusb_endpoint_descriptor *endpoint; - int address, direction, transfer_type; + int direction, transfer_type, transfer_type_libusb; endpoint = &interface->endpoint[num]; DBG (5, "sanei_usb_open: endpoint nr: %d\n", num); - transfer_type = endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK; - address = endpoint->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK; + transfer_type_libusb = + endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK; direction = endpoint->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK; - DBG (5, "sanei_usb_open: direction: %d\n", direction); - DBG (5, "sanei_usb_open: address: %d transfertype: %d\n", - address, transfer_type); - - /* save the endpoints we need later */ - if (transfer_type == LIBUSB_TRANSFER_TYPE_INTERRUPT) - { - DBG (5, - "sanei_usb_open: found interrupt-%s endpoint (address 0x%02x)\n", - direction ? "in" : "out", address); - if (direction) /* in */ - { - if (devices[devcount].int_in_ep) - DBG (3, - "sanei_usb_open: we already have a int-in endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].int_in_ep); - else - devices[devcount].int_in_ep = endpoint->bEndpointAddress; - } - else - { - if (devices[devcount].int_out_ep) - DBG (3, - "sanei_usb_open: we already have a int-out endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].int_out_ep); - else - devices[devcount].int_out_ep = endpoint->bEndpointAddress; - } - } - else if (transfer_type == LIBUSB_TRANSFER_TYPE_BULK) - { - DBG (5, - "sanei_usb_open: found bulk-%s endpoint (address 0x%02x)\n", - direction ? "in" : "out", address); - if (direction) /* in */ - { - if (devices[devcount].bulk_in_ep) - DBG (3, - "sanei_usb_open: we already have a bulk-in endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].bulk_in_ep); - else - devices[devcount].bulk_in_ep = endpoint->bEndpointAddress; - } - else - { - if (devices[devcount].bulk_out_ep) - DBG (3, - "sanei_usb_open: we already have a bulk-out endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].bulk_out_ep); - else - devices[devcount].bulk_out_ep = endpoint->bEndpointAddress; - } - } - else if (transfer_type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) - { - DBG (5, - "sanei_usb_open: found isochronous-%s endpoint (address 0x%02x)\n", - direction ? "in" : "out", address); - if (direction) /* in */ - { - if (devices[devcount].iso_in_ep) - DBG (3, - "sanei_usb_open: we already have a isochronous-in endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].iso_in_ep); - else - devices[devcount].iso_in_ep = endpoint->bEndpointAddress; - } - else - { - if (devices[devcount].iso_out_ep) - DBG (3, - "sanei_usb_open: we already have a isochronous-out endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].iso_out_ep); - else - devices[devcount].iso_out_ep = endpoint->bEndpointAddress; - } - } - else if (transfer_type == LIBUSB_TRANSFER_TYPE_CONTROL) - { - DBG (5, - "sanei_usb_open: found control-%s endpoint (address 0x%02x)\n", - direction ? "in" : "out", address); - if (direction) /* in */ - { - if (devices[devcount].control_in_ep) - DBG (3, - "sanei_usb_open: we already have a control-in endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].control_in_ep); - else - devices[devcount].control_in_ep = endpoint->bEndpointAddress; - } - else - { - if (devices[devcount].control_out_ep) - DBG (3, - "sanei_usb_open: we already have a control-out endpoint " - "(address: 0x%02x), ignoring the new one\n", - devices[devcount].control_out_ep); - else - devices[devcount].control_out_ep = endpoint->bEndpointAddress; - } - } + // don't rely on LIBUSB_TRANSFER_TYPE_* mapping to + // USB_ENDPOINT_TYPE_* even though they'll most likely be + // the same + switch (transfer_type_libusb) + { + case LIBUSB_TRANSFER_TYPE_INTERRUPT: + transfer_type = USB_ENDPOINT_TYPE_INTERRUPT; + break; + case LIBUSB_TRANSFER_TYPE_BULK: + transfer_type = USB_ENDPOINT_TYPE_BULK; + break; + case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: + transfer_type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; + break; + case LIBUSB_TRANSFER_TYPE_CONTROL: + transfer_type = USB_ENDPOINT_TYPE_CONTROL; + break; + + } + + sanei_usb_add_endpoint(&devices[devcount], + transfer_type, + endpoint->bEndpointAddress, + direction); } } } @@ -2016,62 +1882,21 @@ sanei_usb_open (SANE_String_Const devname, SANE_Int * dn) break; case USB_DT_ENDPOINT: endpoint = (struct usb_endpoint_descriptor*)pDescHead; - address = endpoint->bEndpointAddress; direction = endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK; transfer_type = endpoint->bmAttributes & USB_ENDPOINT_TYPE_MASK; - /* save the endpoints we need later */ - if (transfer_type == USB_ENDPOINT_TYPE_INTERRUPT) - { - DBG (5, "sanei_usb_open: found interupt-%s endpoint (address %2x)\n", - direction ? "in" : "out", address); - if (direction) /* in */ - { - if (devices[devcount].int_in_ep) - DBG (3, "sanei_usb_open: we already have a int-in endpoint " - "(address: %d), ignoring the new one\n", - devices[devcount].int_in_ep); - else - devices[devcount].int_in_ep = endpoint->bEndpointAddress; - } - else - if (devices[devcount].int_out_ep) - DBG (3, "sanei_usb_open: we already have a int-out endpoint " - "(address: %d), ignoring the new one\n", - devices[devcount].int_out_ep); - else - devices[devcount].int_out_ep = endpoint->bEndpointAddress; - } - else if (transfer_type == USB_ENDPOINT_TYPE_BULK) - { - DBG (5, "sanei_usb_open: found bulk-%s endpoint (address %2x)\n", - direction ? "in" : "out", address); - if (direction) /* in */ - { - if (devices[devcount].bulk_in_ep) - DBG (3, "sanei_usb_open: we already have a bulk-in endpoint " - "(address: %d), ignoring the new one\n", - devices[devcount].bulk_in_ep); - else - devices[devcount].bulk_in_ep = endpoint->bEndpointAddress; - } - else - { - if (devices[devcount].bulk_out_ep) - DBG (3, "sanei_usb_open: we already have a bulk-out endpoint " - "(address: %d), ignoring the new one\n", - devices[devcount].bulk_out_ep); - else - devices[devcount].bulk_out_ep = endpoint->bEndpointAddress; - } - } + + if (transfer_type == USB_ENDPOINT_TYPE_INTERRUPT || + transfer_type == USB_ENDPOINT_TYPE_BULK) + { + sanei_usb_add_endpoint(&devices[devcount], transfer_type, + endpoint->bEndpointAddress, direction); + } /* ignore currently unsupported endpoints */ else { DBG (5, "sanei_usb_open: ignoring %s-%s endpoint " "(address: %d)\n", - transfer_type == USB_ENDPOINT_TYPE_CONTROL ? "control" : - transfer_type == USB_ENDPOINT_TYPE_ISOCHRONOUS - ? "isochronous" : "interrupt", - direction ? "in" : "out", address); + sanei_usb_transfer_type_desc(transfer_type), + direction ? "in" : "out", address); continue; } break; @@ -2344,8 +2169,8 @@ sanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size) if (ret < 0) { - DBG (1, "sanei_usb_read_bulk: read failed: %s\n", - sanei_libusb_strerror (ret)); + DBG (1, "sanei_usb_read_bulk: read failed (still got %d bytes): %s\n", + rsize, sanei_libusb_strerror (ret)); read_size = -1; } @@ -2372,9 +2197,10 @@ sanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size) #ifdef HAVE_USBCALLS int rc; char* buffer_ptr = (char*) buffer; - while (*size) + size_t requested_size = *size; + while (requested_size) { - ULONG ulToRead = (*size>MAX_RW)?MAX_RW:*size; + ULONG ulToRead = (requested_size>MAX_RW)?MAX_RW:requested_size; ULONG ulNum = ulToRead; DBG (5, "Entered usbcalls UsbBulkRead with dn = %d\n",dn); DBG (5, "Entered usbcalls UsbBulkRead with dh = %p\n",dh); @@ -2392,7 +2218,7 @@ sanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } if (rc || (ulNum!=ulToRead)) return SANE_STATUS_INVAL; - *size -=ulToRead; + requested_size -=ulToRead; buffer_ptr += ulToRead; read_size += ulToRead; } @@ -2529,11 +2355,12 @@ sanei_usb_write_bulk (SANE_Int dn, const SANE_Byte * buffer, size_t * size) DBG (5, "Entered usbcalls UsbBulkWrite with bulk_out_ep = 0x%02x\n",devices[dn].bulk_out_ep); DBG (5, "Entered usbcalls UsbBulkWrite with interface_nr = %d\n",devices[dn].interface_nr); DBG (5, "Entered usbcalls UsbBulkWrite with usbcalls_timeout = %d\n",usbcalls_timeout); - while (*size) + size_t requested_size = *size; + while (requested_size) { - ULONG ulToWrite = (*size>MAX_RW)?MAX_RW:*size; + ULONG ulToWrite = (requested_size>MAX_RW)?MAX_RW:requested_size; - DBG (5, "size requested to write = %lu, ulToWrite = %lu\n",(unsigned long) *size,ulToWrite); + DBG (5, "size requested to write = %lu, ulToWrite = %lu\n",(unsigned long) requested_size,ulToWrite); if (devices[dn].bulk_out_ep){ rc = UsbBulkWrite (dh, devices[dn].bulk_out_ep, devices[dn].interface_nr, ulToWrite, (char*) buffer, usbcalls_timeout); @@ -2545,10 +2372,10 @@ sanei_usb_write_bulk (SANE_Int dn, const SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } if (rc) return SANE_STATUS_INVAL; - *size -=ulToWrite; + requested_size -=ulToWrite; buffer += ulToWrite; write_size += ulToWrite; - DBG (5, "size = %d, write_size = %d\n",*size, write_size); + DBG (5, "size = %d, write_size = %d\n", requested_size, write_size); } #else /* not HAVE_USBCALLS */ { @@ -2619,7 +2446,6 @@ sanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req, } if ((rtype & 0x80) && debug_level > 10) print_buffer (data, len); - return SANE_STATUS_GOOD; #elif defined(__BEOS__) struct usb_scanner_ioctl_ctrlmsg c; @@ -2638,8 +2464,6 @@ sanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req, } if ((rtype & 0x80) && debug_level > 10) print_buffer (data, len); - - return SANE_STATUS_GOOD; #else /* not __linux__ */ DBG (5, "sanei_usb_control_msg: not supported on this OS\n"); return SANE_STATUS_UNSUPPORTED; @@ -2661,7 +2485,6 @@ sanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req, } if ((rtype & 0x80) && debug_level > 10) print_buffer (data, len); - return SANE_STATUS_GOOD; } #elif defined(HAVE_LIBUSB) { @@ -2678,7 +2501,6 @@ sanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req, } if ((rtype & 0x80) && debug_level > 10) print_buffer (data, len); - return SANE_STATUS_GOOD; } #else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB*/ { @@ -2702,7 +2524,6 @@ sanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req, } if ((rtype & 0x80) && debug_level > 10) print_buffer (data, len); - return SANE_STATUS_GOOD; #else /* not HAVE_USBCALLS */ { DBG (1, "sanei_usb_control_msg: usbcalls support missing\n"); @@ -2716,6 +2537,7 @@ sanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req, devices[dn].method); return SANE_STATUS_UNSUPPORTED; } + return SANE_STATUS_GOOD; } SANE_Status @@ -3160,7 +2982,6 @@ sanei_usb_get_descriptor( SANE_Int dn, desc->dev_sub_class = usb_descr->bDeviceSubClass; desc->dev_protocol = usb_descr->bDeviceProtocol; desc->max_packet_size = usb_descr->bMaxPacketSize0; - return SANE_STATUS_GOOD; } #elif defined(HAVE_LIBUSB) { @@ -3185,7 +3006,6 @@ sanei_usb_get_descriptor( SANE_Int dn, desc->dev_sub_class = lu_desc.bDeviceSubClass; desc->dev_protocol = lu_desc.bDeviceProtocol; desc->max_packet_size = lu_desc.bMaxPacketSize0; - return SANE_STATUS_GOOD; } #else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { @@ -3193,4 +3013,5 @@ sanei_usb_get_descriptor( SANE_Int dn, return SANE_STATUS_UNSUPPORTED; } #endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ + return SANE_STATUS_GOOD; } -- cgit v1.2.3