From a424dea2ccc857d153ff8d3bfec60ac2023c0299 Mon Sep 17 00:00:00 2001 From: Lutz Mueller Date: Sat, 22 Sep 2001 08:09:29 +0000 Subject: [PATCH] =?UTF-8?q?2001-09-22=20=20Lutz=20M=EF=BF=BDller=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patch from Fredrik * include/gphoto2-port.h: * libgphoto2_port/gp_port.c: * serial/unix.c: * usb/libusb.c: Clean up settings code. git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@2296 67ed7778-7388-44ab-90cf-0a291f65f57c --- libgphoto2_port/ChangeLog | 9 +++ libgphoto2_port/include/gphoto2-port.h | 3 - libgphoto2_port/libgphoto2_port/gp_port.c | 31 +++------ libgphoto2_port/serial/unix.c | 1 - libgphoto2_port/usb/libusb.c | 82 +++++++++++++++++------ 5 files changed, 77 insertions(+), 49 deletions(-) diff --git a/libgphoto2_port/ChangeLog b/libgphoto2_port/ChangeLog index 9d9524f27..2f27a1ac6 100644 --- a/libgphoto2_port/ChangeLog +++ b/libgphoto2_port/ChangeLog @@ -1,3 +1,12 @@ +2001-09-22 Lutz Müller + + Patch from Fredrik + + * include/gphoto2-port.h: + * libgphoto2_port/gp_port.c: + * serial/unix.c: + * usb/libusb.c: Clean up settings code. + 2001-09-22 Lutz Müller Patch from Carsten Pfeiffer diff --git a/libgphoto2_port/include/gphoto2-port.h b/libgphoto2_port/include/gphoto2-port.h index 44cbd9a4b..5e222b693 100644 --- a/libgphoto2_port/include/gphoto2-port.h +++ b/libgphoto2_port/include/gphoto2-port.h @@ -159,8 +159,6 @@ struct gp_port { gp_port_settings settings; gp_port_settings settings_pending; - gp_port_settings settings_saved; - int device_fd; #ifdef WIN32 HANDLE device_handle; @@ -173,7 +171,6 @@ struct gp_port { void *library_handle; int debug_level; - int previously_opened; }; /* Core functions diff --git a/libgphoto2_port/libgphoto2_port/gp_port.c b/libgphoto2_port/libgphoto2_port/gp_port.c index 38ef9de8c..ed53936a4 100644 --- a/libgphoto2_port/libgphoto2_port/gp_port.c +++ b/libgphoto2_port/libgphoto2_port/gp_port.c @@ -184,7 +184,6 @@ gp_port_new (gp_port **dev, gp_port_type type) } (*dev)->debug_level = glob_debug_level; - (*dev)->previously_opened = 0; (*dev)->type = type; (*dev)->device_fd = 0; @@ -212,14 +211,12 @@ gp_port_new (gp_port **dev, gp_port_type type) gp_port_timeout_set (*dev, 50000); break; case GP_PORT_USB: - - /* Set some default settings */ - settings.usb.inep = 0x81; - settings.usb.outep = 0x02; - settings.usb.config = 1; - settings.usb.interface = 0; - settings.usb.altsetting = 0; - gp_port_settings_set (*dev, settings); + /* Initialize settings.usb */ + (*dev)->settings.usb.inep = -1; + (*dev)->settings.usb.outep = -1; + (*dev)->settings.usb.config = -1; + (*dev)->settings.usb.interface = 0; + (*dev)->settings.usb.altsetting = -1; gp_port_timeout_set (*dev, 5000); break; @@ -260,18 +257,6 @@ gp_port_open (gp_port *dev) if (retval < 0) return (retval); - /* Now update the settings */ - retval = dev->ops->update (dev); - if (retval != GP_OK) { - dev->device_fd = 0; - gp_port_debug_printf (GP_DEBUG_LOW, dev->debug_level, - "gp_port_open: update error"); - return retval; - } - - /* Set the default timeout */ - gp_port_timeout_set (dev, dev->timeout); - return GP_OK; } @@ -396,10 +381,10 @@ gp_port_settings_set (gp_port *dev, gp_port_settings settings) { int retval; - /* need to memcpy() settings to dev->settings */ + /* need to memcpy() settings to dev->settings_pending */ memcpy(&dev->settings_pending, &settings, sizeof(dev->settings_pending)); - retval = dev->ops->update(dev); + retval = dev->ops->update(dev); gp_port_debug_printf(GP_DEBUG_LOW, dev->debug_level, "gp_port_set_settings: update %s", retval < 0? "error":"ok"); return (retval); diff --git a/libgphoto2_port/serial/unix.c b/libgphoto2_port/serial/unix.c index 05ef255f9..8efc142d8 100644 --- a/libgphoto2_port/serial/unix.c +++ b/libgphoto2_port/serial/unix.c @@ -332,7 +332,6 @@ gp_port_serial_init (gp_port *dev) { DeviceHandle *handle; - /* save previous setttings in to dev->settings_saved */ #if HAVE_TERMIOS_H if (tcgetattr(dev->device_fd, &term_old) < 0) { perror("tcgetattr2"); diff --git a/libgphoto2_port/usb/libusb.c b/libgphoto2_port/usb/libusb.c index 3fbcd8233..0823d8c93 100644 --- a/libgphoto2_port/usb/libusb.c +++ b/libgphoto2_port/usb/libusb.c @@ -130,11 +130,11 @@ int gp_port_usb_open(gp_port *dev) ret = usb_claim_interface (dev->device_handle, dev->settings.usb.interface); if (ret < 0) { - fprintf (stderr, "gp_port_usb_open: could not claim intf %d: " - "%s\n", dev->settings.usb.interface, strerror (errno)); + fprintf(stderr, "gp_port_usb_open: could not claim intf %d: " + "%s\n", dev->settings.usb.interface, strerror (errno)); return GP_ERROR_IO_OPEN; } - + return GP_OK; } @@ -250,33 +250,71 @@ int gp_port_usb_msg_read_lib(gp_port * dev, int request, int value, int index, } /* - * This function applys changes to the device + * This function applys changes to the device. + * + * New settings are in dev->settings_pending and the old ones + * are in dev->settings. Compare them first and only call + * usb_set_configuration() and usb_set_altinterface() if needed + * since some USB devices does not like it if this is done + * more than necessary (Canon Digital IXUS 300 for one). + * */ int gp_port_usb_update(gp_port * dev) { int ret; - - memcpy(&dev->settings, &dev->settings_pending, sizeof(dev->settings)); - if ((dev->device_handle != NULL) && (! dev->previously_opened)) { - ret = usb_set_configuration (dev->device_handle, - dev->settings.usb.config); - if (ret < 0) { - fprintf(stderr, "gp_port_usb_update: could not set config %d: " - "%s\n", dev->settings.usb.config, strerror (errno)); - return GP_ERROR_IO_OPEN; + if (!dev) { + fprintf(stderr, "gp_port_usb_update: called on NULL device\n"); + return GP_ERROR_IO_UPDATE; + } + + if (dev->device_handle == NULL) { + fprintf(stderr, "gp_port_usb_update: called on non-open device\n"); + return GP_ERROR_IO_UPDATE; + } + + if (dev->debug_level) + printf("gp_port_usb_update() called\n"); + + if (memcmp(&dev->settings.usb, &dev->settings_pending.usb, sizeof(dev->settings.usb))) { + + /* settings.usb structure is different than the old one */ + + if (dev->settings.usb.config != dev->settings_pending.usb.config) { + ret = usb_set_configuration(dev->device_handle, + dev->settings_pending.usb.config); + if (ret < 0) { + fprintf(stderr, "gp_port_usb_update: could not set config %d/%d: " + "%s\n", dev->settings_pending.usb.interface, + dev->settings_pending.usb.config, strerror(errno)); + + return GP_ERROR_IO_UPDATE; + } + + if (dev->debug_level) + printf("gp_port_usb_update: changed usb.config from %d to %d\n", + dev->settings.usb.config, dev->settings_pending.usb.config); + + /* copy at once if something else fails so that this does not get re-applied */ + dev->settings.usb.config = dev->settings_pending.usb.config; } - ret = usb_set_altinterface (dev->device_handle, - dev->settings.usb.altsetting); - if (ret < 0) { - fprintf (stderr, "gp_port_usb_update: could not set intf %d/%d: " - "%s\n", dev->settings.usb.interface, - dev->settings.usb.altsetting, strerror (errno)); - return GP_ERROR_IO_OPEN; - } + if (dev->settings.usb.altsetting != dev->settings_pending.usb.altsetting) { + ret = usb_set_altinterface(dev->device_handle, dev->settings_pending.usb.altsetting); + if (ret < 0) { + fprintf(stderr, "gp_port_usb_update: could not set intf %d/%d: " + "%s\n", dev->settings_pending.usb.interface, + dev->settings_pending.usb.altsetting, strerror(errno)); - dev->previously_opened = 1; + return GP_ERROR_IO_UPDATE; + } + + if (dev->debug_level) + printf("gp_port_usb_update: changed usb.altsetting from %d to %d\n", + dev->settings.usb.altsetting, dev->settings_pending.usb.altsetting); + } + + memcpy(&dev->settings.usb, &dev->settings_pending.usb, sizeof(dev->settings.usb)); } return GP_OK;