125
132
struct libusb_device_descriptor *descs;
127
134
/* generic matcher. This will catch passed XXX,YYY entries for instance. */
128
info.type = GP_PORT_USB;
129
strcpy (info.name, "");
130
strcpy (info.path, "^usb:");
135
gp_port_info_new (&info);
136
gp_port_info_set_type (info, GP_PORT_USB);
137
gp_port_info_set_name (info, "");
138
gp_port_info_set_path (info, "^usb:");
131
139
CHECK (gp_port_info_list_append (list, info));
133
141
if (libusb_init (&ctx) != 0) {
134
142
gp_log (GP_LOG_ERROR, "libusb1", "libusb_init failed.");
135
143
return GP_ERROR_IO;
138
145
nrofdevs = libusb_get_device_list (ctx, &devs);
139
146
descs = malloc (sizeof(descs[0])*nrofdevs);
140
147
for (i=0;i<nrofdevs;i++) {
238
247
/* Note: We do not skip USB storage. Some devices can support both,
239
248
* and the Ricoh erronously reports it.
241
info.type = GP_PORT_USB;
242
strcpy (info.name, "Universal Serial Bus");
243
snprintf (info.path,sizeof(info.path), "usb:%03d,%03d",
250
gp_port_info_new (&info);
251
gp_port_info_set_type (info, GP_PORT_USB);
252
gp_port_info_set_name (info, "Universal Serial Bus");
253
snprintf (path,sizeof(path), "usb:%03d,%03d",
244
254
libusb_get_bus_number (devs[d]),
245
255
libusb_get_device_address (devs[d])
257
gp_port_info_set_path (info, path);
247
258
CHECK (gp_port_info_list_append (list, info));
249
260
/* This will only be added if no other device was ever added.
250
261
* Users doing "usb:" usage will enter the regular expression matcher case. */
251
262
if (nrofdevices == 0) {
252
info.type = GP_PORT_USB;
253
strcpy (info.name, "Universal Serial Bus");
254
strcpy (info.path, "usb:");
263
gp_port_info_new (&info);
264
gp_port_info_set_type (info, GP_PORT_USB);
265
gp_port_info_set_name (info, "Universal Serial Bus");
266
gp_port_info_set_path (info, "usb:");
255
267
CHECK (gp_port_info_list_append (list, info));
257
269
libusb_exit (ctx); /* should free all stuff above */
306
static int gp_port_usb_find_path_lib(GPPort *port);
296
308
gp_port_usb_open (GPPort *port)
300
312
gp_log (GP_LOG_DEBUG,"libusb1","gp_port_usb_open()");
301
if (!port || !port->pl->d)
302
314
return GP_ERROR_BAD_PARAMETERS;
317
gp_port_usb_find_path_lib(port);
319
return GP_ERROR_BAD_PARAMETERS;
304
322
ret = libusb_open (port->pl->d, &port->pl->dh);
306
324
gp_log (GP_LOG_ERROR, "libusb1", "libusb_open returned %d", ret);
444
462
int ret, curread;
446
if (!port || !port->pl->dh)
464
if (!port || !port->pl->dh) {
465
gp_log (GP_LOG_ERROR, "libusb1", "gp_port_usb_read: bad parameters");
447
466
return GP_ERROR_BAD_PARAMETERS;
469
gp_log (GP_LOG_DEBUG, "libusb1", "reading with timeout %d", port->timeout);
449
470
ret = libusb_bulk_transfer (port->pl->dh, port->settings.usb.inep,
450
471
(unsigned char*)bytes, size, &curread, port->timeout);
472
gp_log (GP_LOG_DEBUG, "libusb1", "ret = %d", ret);
452
474
return GP_ERROR_IO_READ;
480
gp_port_usb_reset(GPPort *port)
484
if (!port || !port->pl->dh) {
485
gp_log (GP_LOG_ERROR, "libusb1", "gp_port_usb_reset: bad parameters");
486
return GP_ERROR_BAD_PARAMETERS;
489
gp_log (GP_LOG_DEBUG, "libusb1", "reseting");
490
ret = libusb_reset_device (port->pl->dh);
491
gp_log (GP_LOG_DEBUG, "libusb1", "ret = %d", ret);
493
return GP_ERROR_IO_READ;
458
498
gp_port_usb_check_int (GPPort *port, char *bytes, int size, int timeout)
460
500
int ret, curread;
574
614
return GP_ERROR_BAD_PARAMETERS;
576
gp_log (GP_LOG_DEBUG, "libusb1", "gp_port_usb_update(old int=%d, conf=%d, alt=%d), (new int=%d, conf=%d, alt=%d)",
616
gp_log (GP_LOG_DEBUG, "libusb1", "gp_port_usb_update(old int=%d, conf=%d, alt=%d) port %s, (new int=%d, conf=%d, alt=%d) port %s",
577
617
port->settings.usb.interface,
578
618
port->settings.usb.config,
579
619
port->settings.usb.altsetting,
620
port->settings.usb.port,
580
621
port->settings_pending.usb.interface,
581
622
port->settings_pending.usb.config,
582
port->settings_pending.usb.altsetting
623
port->settings_pending.usb.altsetting,
624
port->settings_pending.usb.port
585
/* do not set it here, otherwise stv680 doesnt work
627
/* do not overwrite it ... we need to set it.
586
628
if (port->pl->interface == -1) port->pl->interface = port->settings.usb.interface;
587
629
if (port->pl->config == -1) port->pl->config = port->settings.usb.config;
588
630
if (port->pl->altsetting == -1) port->pl->altsetting = port->settings.usb.altsetting;
800
gp_port_usb_find_path_lib(GPPort *port)
803
int d, busnr = 0, devnr = 0;
804
GPPortPrivateLibrary *pl = port->pl;
807
return (GP_ERROR_BAD_PARAMETERS);
809
s = strchr (port->settings.usb.port,':');
810
if (s && (s[1] != '\0')) { /* usb:%d,%d */
811
if (sscanf (s+1, "%d,%d", &busnr, &devnr) != 2)
812
return GP_ERROR_BAD_PARAMETERS;
814
return GP_ERROR_BAD_PARAMETERS;
817
pl->nrofdevs = load_devicelist (port->pl);
819
for (d = 0; d < pl->nrofdevs; d++) {
820
struct libusb_config_descriptor *confdesc;
822
int config = -1, interface = -1, altsetting = -1;
824
if (busnr != libusb_get_bus_number (pl->devs[d]))
826
if (devnr != libusb_get_device_address (pl->devs[d]))
829
port->pl->d = pl->devs[d];
831
gp_log (GP_LOG_VERBOSE, "libusb1", "Found path %s", port->settings.usb.port);
833
/* Use the first config, interface and altsetting we find */
834
gp_port_usb_find_first_altsetting(pl->devs[d], &config, &interface, &altsetting);
836
ret = libusb_get_config_descriptor (pl->devs[d], config, &confdesc);
840
/* Set the defaults */
841
port->settings.usb.config = confdesc->bConfigurationValue;
842
port->settings.usb.interface = confdesc->interface[interface].altsetting[altsetting].bInterfaceNumber;
843
port->settings.usb.altsetting = confdesc->interface[interface].altsetting[altsetting].bAlternateSetting;
845
port->settings.usb.inep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_BULK);
846
port->settings.usb.outep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK);
847
port->settings.usb.intep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_INTERRUPT);
849
port->settings.usb.maxpacketsize = libusb_get_max_packet_size (pl->devs[d], port->settings.usb.inep);
850
gp_log (GP_LOG_VERBOSE, "libusb1",
851
"Detected defaults: config %d, "
852
"interface %d, altsetting %d, "
853
"inep %02x, outep %02x, intep %02x, "
854
"class %02x, subclass %02x",
855
port->settings.usb.config,
856
port->settings.usb.interface,
857
port->settings.usb.altsetting,
858
port->settings.usb.inep,
859
port->settings.usb.outep,
860
port->settings.usb.intep,
861
confdesc->interface[interface].altsetting[altsetting].bInterfaceClass,
862
confdesc->interface[interface].altsetting[altsetting].bInterfaceSubClass
864
libusb_free_config_descriptor (confdesc);
868
gp_port_set_error (port, _("Could not find USB device "
869
"(vendor 0x%x, product 0x%x). Make sure this device "
870
"is connected to the computer."), idvendor, idproduct);
872
return GP_ERROR_IO_USB_FIND;
758
875
gp_port_usb_find_device_lib(GPPort *port, int idvendor, int idproduct)