static int gp_port_serial_unlock (GPPort *dev, const char *path) { #if defined(HAVE_TTYLOCK) if (ttyunlock ((char*) path)) { if (dev) gp_port_set_error (dev, _("Device '%s' could not be " "unlocked."), path); return (GP_ERROR_IO_LOCK); } #elif defined(HAVE_LOCKDEV) int pid; pid = dev_unlock (path, 0); if (pid) { if (dev) { if (pid > 0) gp_port_set_error (dev, _("Device '%s' could " "not be unlocked as it is locked by " "pid %d."), path, pid); else gp_port_set_error (dev, _("Device '%s' could " "not be unlocked (dev_unlock " "returned %d)"), path, pid); } return (GP_ERROR_IO_LOCK); } #endif /* !HAVE_LOCKDEV */ return (GP_OK); }
static int gp_port_usbdiskdirect_lock (GPPort *port, const char *path) { #ifdef HAVE_LOCKDEV int pid; GP_LOG_D ("Trying to lock '%s'...", path); pid = dev_lock (path); if (pid) { if (port) { if (pid > 0) gp_port_set_error (port, _("Device '%s' is " "locked by pid %d"), path, pid); else gp_port_set_error (port, _("Device '%s' could " "not be locked (dev_lock returned " "%d)"), path, pid); } return GP_ERROR_IO_LOCK; } #else # ifdef __GCC__ # warning No locking library found. # warning You will run into problems if you use # warning gphoto2 with a usbdiskdirect picframe in # warning combination with Konqueror (KDE) or Nautilus (GNOME). # warning This will *not* concern USB cameras. # endif #endif return GP_OK; }
static int gp_port_usbscsi_lock (GPPort *port) { #if HAVE_FLOCK GP_LOG_D ("Trying to lock '%s'...", port->settings.usbscsi.path); if (flock(port->pl->fd, LOCK_EX | LOCK_NB) != 0) { switch (errno) { case EWOULDBLOCK: gp_port_set_error (port, _("Device '%s' is locked by another app."), port->settings.usbscsi.path); return GP_ERROR_IO_LOCK; default: gp_port_set_error (port, _("Failed to lock '%s' (%m)."), port->settings.usbscsi.path); return GP_ERROR_IO; } } #else GP_LOG_D ("Locking '%s' not possible, flock not availbale.", port->settings.usbscsi.path); #endif return GP_OK; }
static int gp_port_usb_open (GPPort *port) { int ret; char name[64]; gp_log (GP_LOG_DEBUG,"libusb","gp_port_usb_open()"); if (!port || !port->pl->d) return GP_ERROR_BAD_PARAMETERS; /* * Open the device using the previous usb_handle returned by * find_device */ port->pl->dh = usb_open (port->pl->d); if (!port->pl->dh) { gp_port_set_error (port, _("Could not open USB device (%m).")); return GP_ERROR_IO; } #if defined(LIBUSB_HAS_GET_DRIVER_NP) && defined(LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP) memset(name,0,sizeof(name)); ret = usb_get_driver_np (port->pl->dh, port->settings.usb.interface, name, sizeof(name) ); if (strstr(name,"usbfs") || strstr(name,"storage")) { /* other gphoto instance most likely * Also let mass storage be served by regular means. */ gp_port_set_error (port, _("Camera is already in use.")); return GP_ERROR_IO_LOCK; } if (ret >= 0) { gp_log (GP_LOG_DEBUG,"libusb",_("Device has driver '%s' attached, detaching it now."), name); ret = usb_detach_kernel_driver_np (port->pl->dh, port->settings.usb.interface); if (ret < 0) gp_port_set_error (port, _("Could not detach kernel driver '%s' of camera device."),name); else port->pl->detached = 1; } else { if (errno != ENODATA) /* ENODATA - just no driver there */ gp_port_set_error (port, _("Could not query kernel driver of device.")); } #endif gp_log (GP_LOG_DEBUG,"libusb","claiming interface %d", port->settings.usb.interface); ret = usb_claim_interface (port->pl->dh, port->settings.usb.interface); if (ret < 0) { gp_port_set_error (port, _("Could not claim " "interface %d (%m). Make sure no other program " "or kernel module (such as %s) is using the device " "and you have read/write access to the device."), port->settings.usb.interface, "sdc2xx, stv680, spca50x"); return GP_ERROR_IO_USB_CLAIM; } return GP_OK; }
static int gp_port_usb_close (GPPort *port) { if (!port || !port->pl->dh) return GP_ERROR_BAD_PARAMETERS; if (usb_release_interface (port->pl->dh, port->settings.usb.interface) < 0) { gp_port_set_error (port, _("Could not " "release interface %d (%m)."), port->settings.usb.interface); return (GP_ERROR_IO); } #if 0 /* This does break Canon EOS and some PowerShots ... -Marcus */ /* This is only for our very special Canon cameras which need a good * whack after close, otherwise they get timeouts on reconnect. */ if (port->pl->d->descriptor.idVendor == 0x04a9) { if (usb_reset (port->pl->dh) < 0) { gp_port_set_error (port, _("Could not reset USB port (%m).")); return (GP_ERROR_IO); } } #endif #if defined(LIBUSB_HAS_GET_DRIVER_NP) && defined(LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP) && defined(USBDEVFS_CONNECT) if (port->pl->detached) { char filename[PATH_MAX + 1]; int fd; /* FIXME shouldn't be a fixed path to usb root */ snprintf(filename, sizeof(filename) - 1, "%s/%s/%s", "/dev/bus/usb", port->pl->d->bus->dirname, port->pl->d->filename); fd = open(filename, O_RDWR); if (fd >= 0) { struct usbdevfs_ioctl command; command.ifno = 0; command.ioctl_code = USBDEVFS_CONNECT; command.data = NULL; if (ioctl(fd, USBDEVFS_IOCTL, &command)) gp_log (GP_LOG_DEBUG,"libusb","reattach kernel driver failed"); close(fd); } } #endif if (usb_close (port->pl->dh) < 0) { gp_port_set_error (port, _("Could not close USB port (%m).")); return (GP_ERROR_IO); } port->pl->dh = NULL; return GP_OK; }
static int gp_libusb1_close (GPPort *port) { C_PARAMS (port); if (port->pl->dh == NULL) return GP_OK; _close_async_interrupts(port); if (libusb_release_interface (port->pl->dh, port->settings.usb.interface) < 0) { int saved_errno = errno; gp_port_set_error (port, _("Could not release interface %d (%s)."), port->settings.usb.interface, strerror(saved_errno)); return GP_ERROR_IO; } #if 0 /* This confuses the EOS 5d camera and possible other EOSs. *sigh* */ /* This is only for our very special Canon cameras which need a good * whack after close, otherwise they get timeouts on reconnect. */ if (port->pl->d->descriptor.idVendor == 0x04a9) { if (usb_reset (port->pl->dh) < 0) { int saved_errno = errno; gp_port_set_error (port, _("Could not reset " "USB port (%s)."), strerror(saved_errno)); return (GP_ERROR_IO); } } #endif if (port->pl->detached) { if (LOG_ON_LIBUSB_E (libusb_attach_kernel_driver (port->pl->dh, port->settings.usb.interface))) gp_port_set_error (port, _("Could not reattach kernel driver of camera device.")); } libusb_close (port->pl->dh); free (port->pl->irqs); port->pl->irqs = NULL; free (port->pl->irqlens); port->pl->irqlens = NULL; port->pl->nrofirqs = 0; port->pl->dh = NULL; return GP_OK; }
static int gp_port_usb_clear_halt_lib(GPPort *port, int ep) { int ret=0; if (!port || !port->pl->dh) return GP_ERROR_BAD_PARAMETERS; switch (ep) { case GP_PORT_USB_ENDPOINT_IN : ret=libusb_clear_halt(port->pl->dh, port->settings.usb.inep); break; case GP_PORT_USB_ENDPOINT_OUT : ret=libusb_clear_halt(port->pl->dh, port->settings.usb.outep); break; case GP_PORT_USB_ENDPOINT_INT : ret=libusb_clear_halt(port->pl->dh, port->settings.usb.intep); break; default: gp_port_set_error (port, "gp_port_usb_clear_halt: " "bad EndPoint argument"); return GP_ERROR_BAD_PARAMETERS; } return (ret ? GP_ERROR_IO_USB_CLEAR_HALT : GP_OK); }
static int gp_port_usbscsi_open (GPPort *port) { int result, i; const int max_tries = 5; const char *path = port->settings.usbscsi.path; result = gp_port_usbscsi_lock (port, path); if (result != GP_OK) { for (i = 0; i < max_tries; i++) { result = gp_port_usbscsi_lock (port, path); if (result == GP_OK) break; gp_log (GP_LOG_DEBUG, "gphoto2-port-usbscsi", "Failed to get a lock, trying again..."); sleep (1); } CHECK (result) } port->pl->fd = open (path, O_RDWR); if (port->pl->fd == -1) { gp_port_usbscsi_unlock (port, path); gp_port_set_error (port, _("Failed to open '%s' (%m)."), path); return GP_ERROR_IO; } return GP_OK; }
static int gp_port_usbscsi_open (GPPort *port) { int result, i; const int max_tries = 5; const char *path = port->settings.usbscsi.path; port->pl->fd = open (path, O_RDWR); if (port->pl->fd == -1) { gp_port_set_error (port, _("Failed to open '%s' (%m)."), path); return GP_ERROR_IO; } result = gp_port_usbscsi_lock (port); for (i = 0; i < max_tries && result == GP_ERROR_IO_LOCK; i++) { GP_LOG_D ("Failed to get a lock, trying again..."); sleep (1); result = gp_port_usbscsi_lock (port); } if (result != GP_OK) { close (port->pl->fd); port->pl->fd = -1; } return result; }
static int gp_port_serial_flush (GPPort *dev, int direction) { /* The device needs to be opened for that operation */ if (!dev->pl->fd) CHECK (gp_port_serial_open (dev)); /* Make sure we are operating at the specified speed */ CHECK (gp_port_serial_check_speed (dev)); #ifdef HAVE_TERMIOS_H if (tcflush (dev->pl->fd, direction ? TCOFLUSH : TCIFLUSH) < 0) { int saved_errno = errno; gp_port_set_error (dev, _("Could not flush '%s' (%s)."), dev->settings.serial.port, strerror(saved_errno)); return (GP_ERROR_IO); } #else # ifdef __GCC__ # warning SERIAL FLUSH NOT IMPLEMENTED FOR NON TERMIOS SYSTEMS! # endif #endif return (GP_OK); }
static int gp_port_vusb_clear_halt_lib(GPPort *port, int ep) { unsigned char internal_ep; gp_log(GP_LOG_DEBUG,__FUNCTION__,"()"); C_PARAMS (port && port->pl); switch (ep) { case GP_PORT_USB_ENDPOINT_IN : internal_ep = port->settings.usb.inep; break; case GP_PORT_USB_ENDPOINT_OUT : internal_ep = port->settings.usb.outep; break; case GP_PORT_USB_ENDPOINT_INT : internal_ep = port->settings.usb.intep; break; default: gp_port_set_error (port, "Bad EndPoint argument 0x%x", ep); return GP_ERROR_BAD_PARAMETERS; } gp_log(GP_LOG_DEBUG,"gp_port_vusb_clear_halt_lib","clearing halt on ep 0x%x", internal_ep); /* now clear halt */ return GP_OK; }
static int gp_libusb1_clear_halt_lib(GPPort *port, int ep) { unsigned char internal_ep; C_PARAMS (port && port->pl->dh); switch (ep) { case GP_PORT_USB_ENDPOINT_IN : internal_ep = port->settings.usb.inep; break; case GP_PORT_USB_ENDPOINT_OUT : internal_ep = port->settings.usb.outep; break; case GP_PORT_USB_ENDPOINT_INT : internal_ep = port->settings.usb.intep; break; default: gp_port_set_error (port, "bad EndPoint argument 0x%x", ep); return GP_ERROR_BAD_PARAMETERS; } C_LIBUSB (libusb_clear_halt(port->pl->dh, internal_ep), GP_ERROR_IO_USB_CLEAR_HALT); return GP_OK; }
static int gp_port_serial_set_pin (GPPort *dev, GPPin pin, GPLevel level) { #ifdef HAVE_TERMIOS_H int bit, request; #endif if (!dev) return (GP_ERROR_BAD_PARAMETERS); #ifdef HAVE_TERMIOS_H CHECK (get_termios_bit (dev, pin, &bit)); switch (level) { case GP_LEVEL_LOW: request = TIOCMBIS; break; default: request = TIOCMBIC; break; } if (ioctl (dev->pl->fd, request, &bit) < 0) { int saved_errno = errno; gp_port_set_error (dev, _("Could not set level of pin %i to " "%i (%s)."), pin, level, strerror(saved_errno)); return GP_ERROR_IO; } #else # ifdef __GCC__ # warning ACCESSING PINS IS NOT IMPLEMENTED FOR NON-TERMIOS SYSTEMS! # endif #endif return GP_OK; }
static int gp_port_serial_get_pin (GPPort *dev, GPPin pin, GPLevel *level) { #ifdef HAVE_TERMIOS_H int j, bit; #endif if (!dev || !level) return (GP_ERROR_BAD_PARAMETERS); *level = 0; #ifdef HAVE_TERMIOS_H CHECK (get_termios_bit (dev, pin, &bit)); if (ioctl (dev->pl->fd, TIOCMGET, &j) < 0) { int saved_errno = errno; gp_port_set_error (dev, _("Could not get level of pin %i " "(%s)."), pin, strerror(saved_errno)); return GP_ERROR_IO; } *level = j & bit; #else # ifdef __GCC__ # warning ACCESSING PINS IS NOT IMPLEMENTED FOR NON-TERMIOS SYSTEMS! # endif #endif return (GP_OK); }
static int get_termios_bit (GPPort *dev, GPPin pin, int *bit) { switch (pin) { case GP_PIN_RTS: *bit = TIOCM_RTS; break; case GP_PIN_DTR: *bit = TIOCM_DTR; break; case GP_PIN_CTS: *bit = TIOCM_CTS; break; case GP_PIN_DSR: *bit = TIOCM_DSR; break; case GP_PIN_CD: *bit = TIOCM_CD; break; case GP_PIN_RING: *bit = TIOCM_RNG; break; default: gp_port_set_error (dev, _("Unknown pin %i."), pin); return GP_ERROR_IO; } return (GP_OK); }
static int gp_port_usbdiskdirect_open (GPPort *port) { int result, i; const int max_tries = 5; const char *path = port->settings.usbdiskdirect.path; result = gp_port_usbdiskdirect_lock (port, path); if (result != GP_OK) { for (i = 0; i < max_tries; i++) { result = gp_port_usbdiskdirect_lock (port, path); if (result == GP_OK) break; GP_LOG_D ("Failed to get a lock, trying again..."); sleep (1); } CHECK (result) } port->pl->fd = open (path, O_RDWR|O_DIRECT|O_SYNC); if (port->pl->fd == -1) { gp_port_usbdiskdirect_unlock (port, path); gp_port_set_error (port, _("Failed to open '%s' (%m)."), path); return GP_ERROR_IO; } return GP_OK; }
static int gp_port_serial_lock (GPPort *dev, const char *path) { #if defined(HAVE_LOCKDEV) int pid; #endif gp_log (GP_LOG_DEBUG, "gphoto2-port-serial", "Trying to lock '%s'...", path); #if defined(HAVE_TTYLOCK) if (ttylock ((char*) path)) { if (dev) gp_port_set_error (dev, _("Could not lock device " "'%s'"), path); return (GP_ERROR_IO_LOCK); } #define __HAVE_LOCKING #elif defined(HAVE_LOCKDEV) pid = dev_lock (path); if (pid) { if (dev) { if (pid > 0) gp_port_set_error (dev, _("Device '%s' is " "locked by pid %d"), path, pid); else gp_port_set_error (dev, _("Device '%s' could " "not be locked (dev_lock returned " "%d)"), path, pid); } return (GP_ERROR_IO_LOCK); } #define __HAVE_LOCKING #endif #ifndef __HAVE_LOCKING # ifdef __GCC__ # warning No locking library found. # warning You will run into problems if you use # warning gphoto2 with a serial (RS232) camera in # warning combination with Konqueror (KDE) or Nautilus (GNOME). # warning This will *not* concern USB cameras. # endif #endif return (GP_OK); }
static int gp_port_serial_open (GPPort *dev) { int result, max_tries = 5, i; #ifdef OS2 int fd; #endif char *port; GPPortInfo info; result = gp_port_get_info (dev, &info); if (result < GP_OK) return result; result = gp_port_info_get_path (info, &port); if (result < GP_OK) return result; gp_log (GP_LOG_DEBUG, "gp_port_serial_open", "opening %s", port); /* Ports are named "serial:/dev/whatever/port" */ port = strchr (port, ':'); if (!port) return GP_ERROR_UNKNOWN_PORT; port++; result = gp_port_serial_lock (dev, port); if (result != GP_OK) { for (i = 0; i < max_tries; i++) { result = gp_port_serial_lock (dev, port); if (result == GP_OK) break; gp_log (GP_LOG_DEBUG, "gphoto2-port-serial", "Failed to get a lock, trying again..."); sleep (1); } CHECK (result); } dev->pl->fd = -1; #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__) dev->pl->fd = open (port, O_RDWR | O_NOCTTY | O_NONBLOCK); #elif OS2 fd = open (port, O_RDWR | O_BINARY); dev->pl->fd = open (port, O_RDWR | O_BINARY); close(fd); #else if (dev->pl->fd == -1) dev->pl->fd = open (port, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK); #endif if (dev->pl->fd == -1) { int saved_errno = errno; gp_port_set_error (dev, _("Failed to open '%s' (%s)."), port, strerror(saved_errno)); dev->pl->fd = 0; return GP_ERROR_IO; } return GP_OK; }
static int gp_port_usb_close (GPPort *port) { if (!port || !port->pl->dh) return GP_ERROR_BAD_PARAMETERS; if (libusb_release_interface (port->pl->dh, port->settings.usb.interface) < 0) { int saved_errno = errno; gp_port_set_error (port, _("Could not release " "interface %d (%s)."), port->settings.usb.interface, strerror(saved_errno)); return (GP_ERROR_IO); } #if 0 /* This confuses the EOS 5d camera and possible other EOSs. *sigh* */ /* This is only for our very special Canon cameras which need a good * whack after close, otherwise they get timeouts on reconnect. */ if (port->pl->d->descriptor.idVendor == 0x04a9) { if (usb_reset (port->pl->dh) < 0) { int saved_errno = errno; gp_port_set_error (port, _("Could not reset " "USB port (%s)."), strerror(saved_errno)); return (GP_ERROR_IO); } } #endif if (port->pl->detached) { int ret; ret = libusb_attach_kernel_driver (port->pl->dh, port->settings.usb.interface); if (ret < 0) gp_port_set_error (port, _("Could not reattach kernel driver of camera device.")); } libusb_close (port->pl->dh); port->pl->dh = NULL; return GP_OK; }
static int gp_port_usbscsi_unlock (GPPort *port) { #ifdef HAVE_FLOCK if (flock(port->pl->fd, LOCK_UN) != 0) { gp_port_set_error (port, _("Failed to unlock '%s' (%m)."), port->settings.usbscsi.path); return GP_ERROR_IO; } #endif return GP_OK; }
static int gp_port_serial_write (GPPort *dev, const char *bytes, int size) { int len, ret; if (!dev) return (GP_ERROR_BAD_PARAMETERS); /* The device needs to be opened for that operation */ if (!dev->pl->fd) CHECK (gp_port_serial_open (dev)); /* Make sure we are operating at the specified speed */ CHECK (gp_port_serial_check_speed (dev)); len = 0; while (len < size) { /* * Make sure we write all data while handling * the harmless errors */ ret = write (dev->pl->fd, bytes, size - len); if (ret == -1) { int saved_errno = errno; switch (saved_errno) { case EAGAIN: case EINTR: ret = 0; break; default: gp_port_set_error (dev, _("Could not write " "to port (%s)"), strerror(saved_errno)); return GP_ERROR_IO_WRITE; } } len += ret; } /* wait till all bytes are really sent */ #ifndef OS2 #ifdef HAVE_TERMIOS_H tcdrain (dev->pl->fd); #else ioctl (dev->pl->fd, TCDRAIN, 0); #endif #endif return GP_OK; }
static int gp_port_usbdiskdirect_unlock (GPPort *port, const char *path) { #ifdef HAVE_LOCKDEV int pid; pid = dev_unlock (path, 0); if (pid) { if (port) { if (pid > 0) gp_port_set_error (port, _("Device '%s' could " "not be unlocked as it is locked by " "pid %d."), path, pid); else gp_port_set_error (port, _("Device '%s' could " "not be unlocked (dev_unlock " "returned %d)"), path, pid); } return GP_ERROR_IO_LOCK; } #endif /* !HAVE_LOCKDEV */ return GP_OK; }
static int gp_port_usbscsi_send_scsi_cmd (GPPort *port, int to_dev, char *cmd, int cmd_size, char *sense, int sense_size, char *data, int data_size) { #ifdef HAVE_SCSI_SG_H sg_io_hdr_t io_hdr; if (!port) return GP_ERROR_BAD_PARAMETERS; /* The device needs to be opened for that operation */ if (port->pl->fd == -1) CHECK (gp_port_usbscsi_open (port)) memset(sense, 0, sense_size); memset(&io_hdr, 0, sizeof(sg_io_hdr_t)); if (to_dev) { io_hdr.dxfer_direction = SG_DXFER_TO_DEV; } else { memset (data, 0, data_size); io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; } io_hdr.interface_id = 'S'; io_hdr.cmdp = (unsigned char *)cmd; io_hdr.cmd_len = cmd_size; io_hdr.sbp = (unsigned char *)sense; io_hdr.mx_sb_len = sense_size; io_hdr.dxferp = (unsigned char *)data; io_hdr.dxfer_len = data_size; /*io_hdr.timeout = 1500;*/ io_hdr.timeout = port->timeout; gp_log (GP_LOG_DEBUG, "port/usbscsi", "setting scsi command timeout to %d", port->timeout); if (io_hdr.timeout < 1500) io_hdr.timeout = 1500; if (ioctl (port->pl->fd, SG_IO, &io_hdr) < 0) { gp_port_set_error (port, _("Could not send scsi command to: " "'%s' (%m)."), port->settings.usbscsi.path); return GP_ERROR_IO; } return GP_OK; #else return GP_ERROR_NOT_SUPPORTED; #endif }
static int gp_port_usbdiskdirect_close (GPPort *port) { if (!port || port->pl->fd == -1) return GP_OK; if (close (port->pl->fd) == -1) { gp_port_set_error (port, _("Could not close " "'%s' (%m)."), port->settings.usbdiskdirect.path); return GP_ERROR_IO; } port->pl->fd = -1; CHECK (gp_port_usbdiskdirect_unlock (port, port->settings.usbdiskdirect.path)) return GP_OK; }
static int gp_port_usbscsi_close (GPPort *port) { int result; if (!port || port->pl->fd == -1) return GP_OK; result = gp_port_usbscsi_unlock (port); if (close (port->pl->fd) == -1) { gp_port_set_error (port, _("Could not close " "'%s' (%m)."), port->settings.usbscsi.path); return GP_ERROR_IO; } port->pl->fd = -1; return result; }
static int gp_port_usbdiskdirect_read (GPPort *port, char *bytes, int size) { int ret; C_PARAMS (port); /* The device needs to be opened for that operation */ if (port->pl->fd == -1) CHECK (gp_port_usbdiskdirect_open (port)) ret = read (port->pl->fd, bytes, size); if (ret < 0) { gp_port_set_error (port, _("Could not read from '%s' (%m)."), port->settings.usbdiskdirect.path); return GP_ERROR_IO; } return ret; }
static int gp_port_usbdiskdirect_seek (GPPort *port, int offset, int whence) { off_t ret; C_PARAMS (!port); /* The device needs to be opened for that operation */ if (port->pl->fd == -1) CHECK (gp_port_usbdiskdirect_open (port)) ret = lseek (port->pl->fd, offset, whence); if (ret == -1) { gp_port_set_error (port, _("Could not seek to offset: %x on " "'%s' (%m)."), offset, port->settings.usbdiskdirect.path); return GP_ERROR_IO; } return ret; }
static int gp_port_usbdiskdirect_write (GPPort *port, const char *bytes, int size) { int ret; if (!port) return GP_ERROR_BAD_PARAMETERS; /* The device needs to be opened for that operation */ if (port->pl->fd == -1) CHECK (gp_port_usbdiskdirect_open (port)) ret = write (port->pl->fd, bytes, size); if (ret < 0) { gp_port_set_error (port, _("Could not write to '%s' (%m)."), port->settings.usbdiskdirect.path); return GP_ERROR_IO; } return ret; }
static int gp_port_serial_close (GPPort *dev) { const char *path; if (!dev) return (GP_OK); if (dev->pl->fd) { if (close (dev->pl->fd) == -1) { int saved_errno = errno; gp_port_set_error (dev, _("Could not close " "'%s' (%s)."), dev->settings.serial.port, strerror(saved_errno)); return GP_ERROR_IO; } dev->pl->fd = 0; } /* Unlock the port */ path = strchr (dev->settings.serial.port, ':'); if (!path) return GP_ERROR_BAD_PARAMETERS; path++; CHECK (gp_port_serial_unlock (dev, path)); #if defined(__sgi) /* * On IRIX, we need to set the baudrate each time after opening * the port. */ dev->pl->baudrate = 0; #endif return GP_OK; }
static int gp_libusb1_find_path_lib(GPPort *port) { char *s; int d, busnr = 0, devnr = 0; GPPortPrivateLibrary *pl; C_PARAMS (port); pl = port->pl; s = strchr (port->settings.usb.port,':'); C_PARAMS (s && (s[1] != '\0')); C_PARAMS (sscanf (s+1, "%d,%d", &busnr, &devnr) == 2); /* usb:%d,%d */ pl->nrofdevs = load_devicelist (port->pl); for (d = 0; d < pl->nrofdevs; d++) { struct libusb_config_descriptor *confdesc; int config = -1, interface = -1, altsetting = -1; if (busnr != libusb_get_bus_number (pl->devs[d])) continue; if (devnr != libusb_get_device_address (pl->devs[d])) continue; port->pl->d = pl->devs[d]; GP_LOG_D ("Found path %s", port->settings.usb.port); /* Use the first config, interface and altsetting we find */ gp_libusb1_find_first_altsetting(pl->devs[d], &config, &interface, &altsetting); if (LOG_ON_LIBUSB_E (libusb_get_config_descriptor (pl->devs[d], config, &confdesc))) continue; /* Set the defaults */ port->settings.usb.config = confdesc->bConfigurationValue; port->settings.usb.interface = confdesc->interface[interface].altsetting[altsetting].bInterfaceNumber; port->settings.usb.altsetting = confdesc->interface[interface].altsetting[altsetting].bAlternateSetting; port->settings.usb.inep = gp_libusb1_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_BULK); port->settings.usb.outep = gp_libusb1_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK); port->settings.usb.intep = gp_libusb1_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_INTERRUPT); port->settings.usb.maxpacketsize = libusb_get_max_packet_size (pl->devs[d], port->settings.usb.inep); GP_LOG_D ("Detected defaults: config %d, interface %d, altsetting %d, " "inep %02x, outep %02x, intep %02x, class %02x, subclass %02x", port->settings.usb.config, port->settings.usb.interface, port->settings.usb.altsetting, port->settings.usb.inep, port->settings.usb.outep, port->settings.usb.intep, confdesc->interface[interface].altsetting[altsetting].bInterfaceClass, confdesc->interface[interface].altsetting[altsetting].bInterfaceSubClass ); libusb_free_config_descriptor (confdesc); return GP_OK; } #if 0 gp_port_set_error (port, _("Could not find USB device " "(vendor 0x%x, product 0x%x). Make sure this device " "is connected to the computer."), idvendor, idproduct); #endif return GP_ERROR_IO_USB_FIND; }