79
83
return (GP_PORT_USB);
82
static time_t gp_devslastchecked = 0;
83
static int gp_nrofdevs = 0;
84
static struct libusb_device_descriptor *gp_descs;
85
static libusb_device **gp_devs;
88
load_devicelist (libusb_context *ctx) {
88
load_devicelist (GPPortPrivateLibrary *pl) {
92
if (xtime != gp_devslastchecked) {
94
libusb_free_device_list (gp_devs, 1);
92
if (xtime != pl->devslastchecked) {
94
libusb_free_device_list (pl->devs, 1);
103
gp_nrofdevs = libusb_get_device_list (ctx, &gp_devs);
104
gp_descs = malloc (sizeof(gp_descs[0])*gp_nrofdevs);
105
for (i=0;i<gp_nrofdevs;i++) {
103
pl->nrofdevs = libusb_get_device_list (pl->ctx, &pl->devs);
104
pl->descs = malloc (sizeof(pl->descs[0])*pl->nrofdevs);
105
for (i=0;i<pl->nrofdevs;i++) {
107
ret = libusb_get_device_descriptor(gp_devs[i], &gp_descs[i]);
107
ret = libusb_get_device_descriptor(pl->devs[i], &pl->descs[i]);
109
109
gp_log (GP_LOG_ERROR, "libusb1", "libusb_get_device_descriptor(%d) returned %d", i, ret);
112
time (&gp_devslastchecked);
112
time (&pl->devslastchecked);
120
120
int nrofdevices = 0;
121
121
int d, i, i1, i2, unknownint;
123
libusb_device **devs = NULL;
125
struct libusb_device_descriptor *descs;
123
127
/* generic matcher. This will catch passed XXX,YYY entries for instance. */
124
info.type = GP_PORT_USB;
125
strcpy (info.name, "");
126
strcpy (info.path, "^usb:");
128
gp_port_info_new (&info);
129
gp_port_info_set_type (info, GP_PORT_USB);
130
gp_port_info_set_name (info, "");
131
gp_port_info_set_path (info, "^usb:");
127
132
CHECK (gp_port_info_list_append (list, info));
130
gp_nrofdevs = load_devicelist (NULL);
134
if (libusb_init (&ctx) != 0) {
135
gp_log (GP_LOG_ERROR, "libusb1", "libusb_init failed.");
138
nrofdevs = libusb_get_device_list (ctx, &devs);
139
descs = malloc (sizeof(descs[0])*nrofdevs);
140
for (i=0;i<nrofdevs;i++) {
142
ret = libusb_get_device_descriptor(devs[i], &descs[i]);
144
gp_log (GP_LOG_ERROR, "libusb1", "libusb_get_device_descriptor(%d) returned %d", i, ret);
132
for (d = 0; d < gp_nrofdevs; d++) {
147
for (d = 0; d < nrofdevs; d++) {
133
148
/* Devices which are definitely not cameras. */
134
if ( (gp_descs[d].bDeviceClass == LIBUSB_CLASS_HUB) ||
135
(gp_descs[d].bDeviceClass == LIBUSB_CLASS_HID) ||
136
(gp_descs[d].bDeviceClass == LIBUSB_CLASS_PRINTER) ||
137
(gp_descs[d].bDeviceClass == LIBUSB_CLASS_COMM) ||
138
(gp_descs[d].bDeviceClass == 0xe0) /* wireless / bluetooth */
149
if ( (descs[d].bDeviceClass == LIBUSB_CLASS_HUB) ||
150
(descs[d].bDeviceClass == LIBUSB_CLASS_HID) ||
151
(descs[d].bDeviceClass == LIBUSB_CLASS_PRINTER) ||
152
(descs[d].bDeviceClass == LIBUSB_CLASS_COMM) ||
153
(descs[d].bDeviceClass == 0xe0) /* wireless / bluetooth */
141
156
/* excepts HUBs, usually the interfaces have the classes, not
144
for (i = 0; i < gp_descs[d].bNumConfigurations; i++) {
159
for (i = 0; i < descs[d].bNumConfigurations; i++) {
145
160
struct libusb_config_descriptor *config;
148
ret = libusb_get_config_descriptor (gp_devs[d], i, &config);
163
ret = libusb_get_config_descriptor (devs[d], i, &config);
185
200
/* Redo the same bus/device walk, but now add the ports with usb:x,y notation,
186
201
* so we can address all USB devices.
188
for (d = 0; d < gp_nrofdevs; d++) {
203
for (d = 0; d < nrofdevs; d++) {
189
206
/* Devices which are definitely not cameras. */
190
if ( (gp_descs[d].bDeviceClass == LIBUSB_CLASS_HUB) ||
191
(gp_descs[d].bDeviceClass == LIBUSB_CLASS_HID) ||
192
(gp_descs[d].bDeviceClass == LIBUSB_CLASS_PRINTER) ||
193
(gp_descs[d].bDeviceClass == LIBUSB_CLASS_COMM)
207
if ( (descs[d].bDeviceClass == LIBUSB_CLASS_HUB) ||
208
(descs[d].bDeviceClass == LIBUSB_CLASS_HID) ||
209
(descs[d].bDeviceClass == LIBUSB_CLASS_PRINTER) ||
210
(descs[d].bDeviceClass == LIBUSB_CLASS_COMM)
196
213
/* excepts HUBs, usually the interfaces have the classes, not
199
for (i = 0; i < gp_descs[d].bNumConfigurations; i++) {
216
for (i = 0; i < descs[d].bNumConfigurations; i++) {
200
217
struct libusb_config_descriptor *config;
203
ret = libusb_get_config_descriptor (gp_devs[d], i, &config);
220
ret = libusb_get_config_descriptor (devs[d], i, &config);
205
222
gp_log (GP_LOG_ERROR, "libusb1", "libusb_get_config_descriptor(%d) returned %d", d, ret);
223
240
/* Note: We do not skip USB storage. Some devices can support both,
224
241
* and the Ricoh erronously reports it.
226
info.type = GP_PORT_USB;
227
strcpy (info.name, "Universal Serial Bus");
228
snprintf (info.path,sizeof(info.path), "usb:%03d,%03d",
229
libusb_get_bus_number (gp_devs[d]),
230
libusb_get_device_address (gp_devs[d])
243
gp_port_info_new (&info);
244
gp_port_info_set_type (info, GP_PORT_USB);
245
gp_port_info_set_name (info, "Universal Serial Bus");
246
snprintf (path,sizeof(path), "usb:%03d,%03d",
247
libusb_get_bus_number (devs[d]),
248
libusb_get_device_address (devs[d])
250
gp_port_info_set_path (info, path);
232
251
CHECK (gp_port_info_list_append (list, info));
234
253
/* This will only be added if no other device was ever added.
235
254
* Users doing "usb:" usage will enter the regular expression matcher case. */
236
255
if (nrofdevices == 0) {
237
info.type = GP_PORT_USB;
238
strcpy (info.name, "Universal Serial Bus");
239
strcpy (info.path, "usb:");
256
gp_port_info_new (&info);
257
gp_port_info_set_type (info, GP_PORT_USB);
258
gp_port_info_set_name (info, "Universal Serial Bus");
259
gp_port_info_set_path (info, "usb:");
240
260
CHECK (gp_port_info_list_append (list, info));
262
libusb_exit (ctx); /* should free all stuff above */
253
273
port->pl->config = port->pl->interface = port->pl->altsetting = -1;
255
libusb_init (&port->pl->ctx);
275
if (libusb_init (&port->pl->ctx) != 0) {
276
gp_log (GP_LOG_ERROR, "libusb1", "libusb_init failed.");
256
282
libusb_set_debug (port->pl->ctx, 255);
268
288
gp_port_usb_exit (GPPort *port)
291
free (port->pl->descs);
271
292
libusb_exit (port->pl->ctx);
275
if (gp_devs) libusb_free_device_list (gp_devs, 1);
299
static int gp_port_usb_find_path_lib(GPPort *port);
281
301
gp_port_usb_open (GPPort *port)
285
305
gp_log (GP_LOG_DEBUG,"libusb1","gp_port_usb_open()");
286
if (!port || !port->pl->d)
287
307
return GP_ERROR_BAD_PARAMETERS;
310
gp_port_usb_find_path_lib(port);
312
return GP_ERROR_BAD_PARAMETERS;
289
315
ret = libusb_open (port->pl->d, &port->pl->dh);
291
317
gp_log (GP_LOG_ERROR, "libusb1", "libusb_open returned %d", ret);
429
455
int ret, curread;
431
if (!port || !port->pl->dh)
457
if (!port || !port->pl->dh) {
458
gp_log (GP_LOG_ERROR, "libusb1", "gp_port_usb_read: bad parameters");
432
459
return GP_ERROR_BAD_PARAMETERS;
462
gp_log (GP_LOG_DEBUG, "libusb1", "reading with timeout %d", port->timeout);
434
463
ret = libusb_bulk_transfer (port->pl->dh, port->settings.usb.inep,
435
464
(unsigned char*)bytes, size, &curread, port->timeout);
465
gp_log (GP_LOG_DEBUG, "libusb1", "ret = %d", ret);
437
467
return GP_ERROR_IO_READ;
473
gp_port_usb_reset(GPPort *port)
477
if (!port || !port->pl->dh) {
478
gp_log (GP_LOG_ERROR, "libusb1", "gp_port_usb_reset: bad parameters");
479
return GP_ERROR_BAD_PARAMETERS;
482
gp_log (GP_LOG_DEBUG, "libusb1", "reseting");
483
ret = libusb_reset_device (port->pl->dh);
484
gp_log (GP_LOG_DEBUG, "libusb1", "ret = %d", ret);
486
return GP_ERROR_IO_READ;
443
491
gp_port_usb_check_int (GPPort *port, char *bytes, int size, int timeout)
445
493
int ret, curread;
559
607
return GP_ERROR_BAD_PARAMETERS;
561
gp_log (GP_LOG_DEBUG, "libusb1", "gp_port_usb_update(old int=%d, conf=%d, alt=%d), (new int=%d, conf=%d, alt=%d)",
609
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",
562
610
port->settings.usb.interface,
563
611
port->settings.usb.config,
564
612
port->settings.usb.altsetting,
613
port->settings.usb.port,
565
614
port->settings_pending.usb.interface,
566
615
port->settings_pending.usb.config,
567
port->settings_pending.usb.altsetting
616
port->settings_pending.usb.altsetting,
617
port->settings_pending.usb.port
570
/* do not set it here, otherwise stv680 doesnt work
620
/* do not overwrite it ... we need to set it.
571
621
if (port->pl->interface == -1) port->pl->interface = port->settings.usb.interface;
572
622
if (port->pl->config == -1) port->pl->config = port->settings.usb.config;
573
623
if (port->pl->altsetting == -1) port->pl->altsetting = port->settings.usb.altsetting;
793
gp_port_usb_find_path_lib(GPPort *port)
796
int d, busnr = 0, devnr = 0;
797
GPPortPrivateLibrary *pl = port->pl;
800
return (GP_ERROR_BAD_PARAMETERS);
802
s = strchr (port->settings.usb.port,':');
803
if (s && (s[1] != '\0')) { /* usb:%d,%d */
804
if (sscanf (s+1, "%d,%d", &busnr, &devnr) != 2)
805
return GP_ERROR_BAD_PARAMETERS;
807
return GP_ERROR_BAD_PARAMETERS;
810
pl->nrofdevs = load_devicelist (port->pl);
812
for (d = 0; d < pl->nrofdevs; d++) {
813
struct libusb_config_descriptor *confdesc;
815
int config = -1, interface = -1, altsetting = -1;
817
if (busnr != libusb_get_bus_number (pl->devs[d]))
819
if (devnr != libusb_get_device_address (pl->devs[d]))
822
port->pl->d = pl->devs[d];
824
gp_log (GP_LOG_VERBOSE, "libusb1", "Found path %s", port->settings.usb.port);
826
/* Use the first config, interface and altsetting we find */
827
gp_port_usb_find_first_altsetting(pl->devs[d], &config, &interface, &altsetting);
829
ret = libusb_get_config_descriptor (pl->devs[d], config, &confdesc);
833
/* Set the defaults */
834
port->settings.usb.config = confdesc->bConfigurationValue;
835
port->settings.usb.interface = confdesc->interface[interface].altsetting[altsetting].bInterfaceNumber;
836
port->settings.usb.altsetting = confdesc->interface[interface].altsetting[altsetting].bAlternateSetting;
838
port->settings.usb.inep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_BULK);
839
port->settings.usb.outep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK);
840
port->settings.usb.intep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_INTERRUPT);
842
port->settings.usb.maxpacketsize = libusb_get_max_packet_size (pl->devs[d], port->settings.usb.inep);
843
gp_log (GP_LOG_VERBOSE, "libusb1",
844
"Detected defaults: config %d, "
845
"interface %d, altsetting %d, "
846
"inep %02x, outep %02x, intep %02x, "
847
"class %02x, subclass %02x",
848
port->settings.usb.config,
849
port->settings.usb.interface,
850
port->settings.usb.altsetting,
851
port->settings.usb.inep,
852
port->settings.usb.outep,
853
port->settings.usb.intep,
854
confdesc->interface[interface].altsetting[altsetting].bInterfaceClass,
855
confdesc->interface[interface].altsetting[altsetting].bInterfaceSubClass
857
libusb_free_config_descriptor (confdesc);
861
gp_port_set_error (port, _("Could not find USB device "
862
"(vendor 0x%x, product 0x%x). Make sure this device "
863
"is connected to the computer."), idvendor, idproduct);
865
return GP_ERROR_IO_USB_FIND;
743
868
gp_port_usb_find_device_lib(GPPort *port, int idvendor, int idproduct)
746
871
int d, busnr = 0, devnr = 0;
872
GPPortPrivateLibrary *pl = port->pl;
749
875
return (GP_ERROR_BAD_PARAMETERS);
767
893
return GP_ERROR_BAD_PARAMETERS;
770
gp_nrofdevs = load_devicelist (port->pl->ctx);
896
pl->nrofdevs = load_devicelist (port->pl);
772
for (d = 0; d < gp_nrofdevs; d++) {
898
for (d = 0; d < pl->nrofdevs; d++) {
773
899
struct libusb_config_descriptor *confdesc;
775
901
int config = -1, interface = -1, altsetting = -1;
777
if ((gp_descs[d].idVendor != idvendor) ||
778
(gp_descs[d].idProduct != idproduct))
781
if (busnr && (busnr != libusb_get_bus_number (gp_devs[d])))
783
if (devnr && (devnr != libusb_get_device_address (gp_devs[d])))
786
port->pl->d = gp_devs[d];
903
if ((pl->descs[d].idVendor != idvendor) ||
904
(pl->descs[d].idProduct != idproduct))
907
if (busnr && (busnr != libusb_get_bus_number (pl->devs[d])))
909
if (devnr && (devnr != libusb_get_device_address (pl->devs[d])))
912
port->pl->d = pl->devs[d];
788
914
gp_log (GP_LOG_VERBOSE, "libusb1",
789
915
"Looking for USB device "
810
936
port->settings.usb.interface = confdesc->interface[interface].altsetting[altsetting].bInterfaceNumber;
811
937
port->settings.usb.altsetting = confdesc->interface[interface].altsetting[altsetting].bAlternateSetting;
813
port->settings.usb.inep = gp_port_usb_find_ep(gp_devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_BULK);
814
port->settings.usb.outep = gp_port_usb_find_ep(gp_devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK);
815
port->settings.usb.intep = gp_port_usb_find_ep(gp_devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_INTERRUPT);
939
port->settings.usb.inep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_BULK);
940
port->settings.usb.outep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK);
941
port->settings.usb.intep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_INTERRUPT);
817
port->settings.usb.maxpacketsize = libusb_get_max_packet_size (gp_devs[d], port->settings.usb.inep);
943
port->settings.usb.maxpacketsize = libusb_get_max_packet_size (pl->devs[d], port->settings.usb.inep);
818
944
gp_log (GP_LOG_VERBOSE, "libusb1",
819
945
"Detected defaults: config %d, "
820
946
"interface %d, altsetting %d, "
1066
1194
return GP_ERROR_BAD_PARAMETERS;
1068
gp_nrofdevs = load_devicelist (port->pl->ctx);
1069
for (d = 0; d < gp_nrofdevs; d++) {
1196
pl->nrofdevs = load_devicelist (port->pl);
1197
for (d = 0; d < pl->nrofdevs; d++) {
1070
1198
struct libusb_config_descriptor *confdesc;
1071
1199
int i, ret, config = -1, interface = -1, altsetting = -1;
1073
if (busnr && (busnr != libusb_get_bus_number (gp_devs[d])))
1201
if (busnr && (busnr != libusb_get_bus_number (pl->devs[d])))
1075
if (devnr && (devnr != libusb_get_device_address (gp_devs[d])))
1203
if (devnr && (devnr != libusb_get_device_address (pl->devs[d])))
1078
1206
gp_log (GP_LOG_VERBOSE, "gphoto2-port-usb",
1080
1208
"(class 0x%x, subclass, 0x%x, protocol 0x%x)...",
1081
1209
class, subclass, protocol);
1083
ret = gp_port_usb_match_device_by_class(gp_devs[d], class, subclass, protocol, &config, &interface, &altsetting);
1211
ret = gp_port_usb_match_device_by_class(pl->devs[d], class, subclass, protocol, &config, &interface, &altsetting);
1087
port->pl->d = gp_devs[d];
1215
port->pl->d = pl->devs[d];
1088
1216
gp_log (GP_LOG_VERBOSE, "libusb1",
1089
1217
"Found USB class device "
1090
1218
"(class 0x%x, subclass, 0x%x, protocol 0x%x)",
1091
1219
class, subclass, protocol);
1093
ret = libusb_get_config_descriptor (gp_devs[d], config, &confdesc);
1221
ret = libusb_get_config_descriptor (pl->devs[d], config, &confdesc);
1094
1222
if (ret) continue;
1096
1224
/* Set the defaults */
1098
1226
port->settings.usb.interface = confdesc->interface[interface].altsetting[altsetting].bInterfaceNumber;
1099
1227
port->settings.usb.altsetting = confdesc->interface[interface].altsetting[altsetting].bAlternateSetting;
1101
port->settings.usb.inep = gp_port_usb_find_ep(gp_devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_BULK);
1102
port->settings.usb.outep = gp_port_usb_find_ep(gp_devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK);
1103
port->settings.usb.intep = gp_port_usb_find_ep(gp_devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_INTERRUPT);
1229
port->settings.usb.inep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_BULK);
1230
port->settings.usb.outep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK);
1231
port->settings.usb.intep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_INTERRUPT);
1104
1232
port->settings.usb.maxpacketsize = 0;
1105
1233
gp_log (GP_LOG_DEBUG, "libusb1", "inep to look for is %02x", port->settings.usb.inep);
1106
1234
for (i=0;i<confdesc->interface[interface].altsetting[altsetting].bNumEndpoints;i++) {