124
179
{"sierra-mode", no_argument, 0, 'S'},
125
180
{"sony-mode", no_argument, 0, 'O'},
126
181
{"qisda-mode", no_argument, 0, 'B'},
182
{"quanta-mode", no_argument, 0, 'E'},
127
183
{"kobil-mode", no_argument, 0, 'T'},
128
184
{"gct-mode", no_argument, 0, 'G'},
129
185
{"sequans-mode", no_argument, 0, 'N'},
130
186
{"mobileaction-mode", no_argument, 0, 'A'},
131
187
{"cisco-mode", no_argument, 0, 'L'},
188
{"blackberry-mode", no_argument, 0, 'Z'},
189
{"pantech-mode", no_argument, 0, 'F'},
190
{"std-eject", no_argument, 0, 'K'},
132
191
{"need-response", no_argument, 0, 'n'},
133
192
{"reset-usb", no_argument, 0, 'R'},
134
193
{"config-file", required_argument, 0, 'c'},
135
194
{"verbose", no_argument, 0, 'W'},
136
195
{"quiet", no_argument, 0, 'Q'},
137
196
{"sysmode", no_argument, 0, 'D'},
138
{"no-inquire", no_argument, 0, 'I'},
197
{"inquire", no_argument, 0, 'I'},
139
198
{"stdinput", no_argument, 0, 't'},
199
{"find-mbim", no_argument, 0, 'j'},
140
200
{"long-config", required_argument, 0, 'f'},
141
201
{"check-success", required_argument, 0, 's'},
142
202
{"interface", required_argument, 0, 'i'},
154
214
ParseParamHex(configFilename, TargetClass);
155
215
ParseParamHex(configFilename, DefaultVendor);
156
216
ParseParamHex(configFilename, DefaultProduct);
157
ParseParamBool(configFilename, DetachStorageOnly);
158
ParseParamBool(configFilename, HuaweiMode);
159
ParseParamBool(configFilename, SierraMode);
160
ParseParamBool(configFilename, SonyMode);
161
ParseParamBool(configFilename, QisdaMode);
162
ParseParamBool(configFilename, GCTMode);
163
ParseParamBool(configFilename, KobilMode);
164
ParseParamBool(configFilename, SequansMode);
165
ParseParamBool(configFilename, MobileActionMode);
166
ParseParamBool(configFilename, CiscoMode);
217
ParseParamBoolMap(configFilename, DetachStorageOnly, ModeMap, DETACHONLY_MODE);
218
ParseParamBoolMap(configFilename, HuaweiMode, ModeMap, HUAWEI_MODE);
219
ParseParamBoolMap(configFilename, SierraMode, ModeMap, SIERRA_MODE);
220
ParseParamBoolMap(configFilename, SonyMode, ModeMap, SONY_MODE);
221
ParseParamBoolMap(configFilename, GCTMode, ModeMap, GCT_MODE);
222
ParseParamBoolMap(configFilename, KobilMode, ModeMap, KOBIL_MODE);
223
ParseParamBoolMap(configFilename, SequansMode, ModeMap, SEQUANS_MODE);
224
ParseParamBoolMap(configFilename, MobileActionMode, ModeMap, MOBILEACTION_MODE);
225
ParseParamBoolMap(configFilename, CiscoMode, ModeMap, CISCO_MODE);
226
ParseParamBoolMap(configFilename, QisdaMode, ModeMap, QISDA_MODE);
227
ParseParamBoolMap(configFilename, QuantaMode, ModeMap, QUANTA_MODE);
228
ParseParamBoolMap(configFilename, BlackberryMode, ModeMap, BLACKBERRY_MODE);
229
ParseParamBoolMap(configFilename, PantechMode, ModeMap, PANTECH_MODE);
230
ParseParamBool(configFilename, StandardEject);
167
231
ParseParamBool(configFilename, NoDriverLoading);
168
232
ParseParamHex(configFilename, MessageEndpoint);
169
233
ParseParamString(configFilename, MessageContent);
192
256
void printConfig()
194
258
if ( DefaultVendor )
195
printf ("DefaultVendor= 0x%04x\n", DefaultVendor);
197
fprintf (output,"DefaultVendor= not set\n");
259
fprintf (output,"DefaultVendor= 0x%04x\n", DefaultVendor);
198
260
if ( DefaultProduct )
199
261
fprintf (output,"DefaultProduct= 0x%04x\n", DefaultProduct);
201
fprintf (output,"DefaultProduct= not set\n");
202
262
if ( TargetVendor )
203
263
fprintf (output,"TargetVendor= 0x%04x\n", TargetVendor);
205
fprintf (output,"TargetVendor= not set\n");
206
264
if ( TargetProduct > -1 )
207
265
fprintf (output,"TargetProduct= 0x%04x\n", TargetProduct);
209
fprintf (output,"TargetProduct= not set\n");
210
266
if ( TargetClass )
211
267
fprintf (output,"TargetClass= 0x%02x\n", TargetClass);
213
fprintf (output,"TargetClass= not set\n");
214
fprintf (output,"TargetProductList=\"%s\"\n", TargetProductList);
215
fprintf (output,"\nDetachStorageOnly=%i\n", (int)DetachStorageOnly);
216
fprintf (output,"HuaweiMode=%i\n", (int)HuaweiMode);
217
fprintf (output,"SierraMode=%i\n", (int)SierraMode);
218
fprintf (output,"SonyMode=%i\n", (int)SonyMode);
219
fprintf (output,"QisdaMode=%i\n", (int)QisdaMode);
220
fprintf (output,"GCTMode=%i\n", (int)GCTMode);
221
fprintf (output,"KobilMode=%i\n", (int)KobilMode);
222
fprintf (output,"SequansMode=%i\n", (int)SequansMode);
223
fprintf (output,"MobileActionMode=%i\n", (int)MobileActionMode);
224
fprintf (output,"CiscoMode=%i\n", (int)CiscoMode);
268
if ( strlen(TargetProductList) )
269
fprintf (output,"TargetProductList=\"%s\"\n", TargetProductList);
271
fprintf (output,"\nStandardEject=1\n");
272
if (ModeMap & DETACHONLY_MODE)
273
fprintf (output,"\nDetachStorageOnly=1\n");
274
if (ModeMap & HUAWEI_MODE)
275
fprintf (output,"HuaweiMode=1\n");
276
if (ModeMap & SIERRA_MODE)
277
fprintf (output,"SierraMode=1\n");
278
if (ModeMap & SONY_MODE)
279
fprintf (output,"SonyMode=1\n");
280
if (ModeMap & QISDA_MODE)
281
fprintf (output,"QisdaMode=1\n");
282
if (ModeMap & QUANTA_MODE)
283
fprintf (output,"QuantaMode=1\n");
284
if (ModeMap & GCT_MODE)
285
fprintf (output,"GCTMode=1\n");
286
if (ModeMap & KOBIL_MODE)
287
fprintf (output,"KobilMode=1\n");
288
if (ModeMap & SEQUANS_MODE)
289
fprintf (output,"SequansMode=1\n");
290
if (ModeMap & MOBILEACTION_MODE)
291
fprintf (output,"MobileActionMode=1\n");
292
if (ModeMap & CISCO_MODE)
293
fprintf (output,"CiscoMode=1\n");
294
if (ModeMap & BLACKBERRY_MODE)
295
fprintf (output,"BlackberryMode=1\n");
296
if (ModeMap & PANTECH_MODE)
297
fprintf (output,"PantechMode=1\n");
225
298
if ( MessageEndpoint )
226
299
fprintf (output,"MessageEndpoint=0x%02x\n", MessageEndpoint);
228
fprintf (output,"MessageEndpoint= not set\n");
229
fprintf (output,"MessageContent=\"%s\"\n", MessageContent);
300
if ( strlen(MessageContent) )
301
fprintf (output,"MessageContent=\"%s\"\n", MessageContent);
230
302
if ( strlen(MessageContent2) )
231
303
fprintf (output,"MessageContent2=\"%s\"\n", MessageContent2);
232
304
if ( strlen(MessageContent3) )
293
356
case 'w': ReleaseDelay = strtol(optarg, NULL, 10); break;
294
357
case 'n': NeedResponse = 1; break;
295
358
case 'r': ResponseEndpoint = strtol(optarg, NULL, 16); break;
296
case 'd': DetachStorageOnly = 1; break;
297
case 'H': HuaweiMode = 1; break;
298
case 'S': SierraMode = 1; break;
299
case 'O': SonyMode = 1; break;
300
case 'B': QisdaMode = 1; break;
301
case 'G': GCTMode = 1; break;
302
case 'T': KobilMode = 1; break;
303
case 'N': SequansMode = 1; break;
304
case 'A': MobileActionMode = 1; break;
305
case 'L': CiscoMode = 1; break;
359
case 'K': StandardEject = 1; break;
360
case 'd': ModeMap = ModeMap + DETACHONLY_MODE; break;
361
case 'H': ModeMap = ModeMap + HUAWEI_MODE; break;
362
case 'S': ModeMap = ModeMap + SIERRA_MODE; break;
363
case 'O': ModeMap = ModeMap + SONY_MODE; break;; break;
364
case 'B': ModeMap = ModeMap + QISDA_MODE; break;
365
case 'E': ModeMap = ModeMap + QUANTA_MODE; break;
366
case 'G': ModeMap = ModeMap + GCT_MODE; break;
367
case 'T': ModeMap = ModeMap + KOBIL_MODE; break;
368
case 'N': ModeMap = ModeMap + SEQUANS_MODE; break;
369
case 'A': ModeMap = ModeMap + MOBILEACTION_MODE; break;
370
case 'L': ModeMap = ModeMap + CISCO_MODE; break;
371
case 'Z': ModeMap = ModeMap + BLACKBERRY_MODE; break;
372
case 'F': ModeMap = ModeMap + PANTECH_MODE; break;
306
373
case 'c': readConfigFile(optarg); break;
307
374
case 't': readConfigFile("stdin"); break;
308
375
case 'W': verbose = 1; show_progress = 1; count--; break;
309
376
case 'Q': show_progress = 0; verbose = 0; count--; break;
310
377
case 'D': sysmode = 1; count--; break;
311
378
case 's': CheckSuccess = strtol(optarg, NULL, 10); count--; break;
312
case 'I': InquireDevice = 0; break;
379
case 'I': InquireDevice = 1; break;
313
380
case 'b': busnum = strtol(optarg, NULL, 10); break;
314
381
case 'g': devnum = strtol(optarg, NULL, 10); break;
316
383
case 'i': Interface = strtol(optarg, NULL, 16); break;
317
384
case 'u': Configuration = strtol(optarg, NULL, 16); break;
318
385
case 'a': AltSetting = strtol(optarg, NULL, 16); break;
386
case 'j': mbim = 1; break;
321
389
longConfig = malloc(strlen(optarg)+5);
434
510
/* Count default devices, get the last one found */
435
SHOW_PROGRESS(output,"Looking for default devices ...\n");
436
dev = search_devices(&numDefaults, DefaultVendor, DefaultProduct, "\0", TargetClass, Configuration, searchMode);
511
SHOW_PROGRESS(output,"Look for default devices ...\n");
513
sprintf(DefaultProductList,"%04x",DefaultProduct);
514
dev = search_devices(&numDefaults, DefaultVendor, DefaultProductList, TargetClass, Configuration, searchMode);
437
515
if (numDefaults) {
438
SHOW_PROGRESS(output," Found device in default mode, class or configuration (%d)\n", numDefaults);
516
SHOW_PROGRESS(output," Found devices in default mode (%d)\n", numDefaults);
440
SHOW_PROGRESS(output," No devices in default mode found. Nothing to do. Bye.\n\n");
518
SHOW_PROGRESS(output," No devices in default mode found. Nothing to do. Bye!\n\n");
443
521
if (dev == NULL) {
444
SHOW_PROGRESS(output," No bus/device match. Is device connected? Bye.\n\n");
522
SHOW_PROGRESS(output," No bus/device match. Is device connected? Abort\n\n");
447
525
if (devnum == -1) {
448
devnum = dev->devnum;
449
busnum = (int)strtol(dev->bus->dirname,NULL,10);
450
SHOW_PROGRESS(output,"Accessing device %03d on bus %03d ...\n", devnum, busnum);
526
devnum = libusb_get_device_address(dev);
527
busnum = libusb_get_bus_number(dev);
528
SHOW_PROGRESS(output,"Access device %03d on bus %03d\n", devnum, busnum);
452
devh = usb_open(dev);
530
libusb_open(dev, &devh);
453
531
if (devh == NULL) {
454
SHOW_PROGRESS(output,"Error opening the device. Aborting.\n\n");
532
SHOW_PROGRESS(output,"Error opening the device. Abort\n\n");
536
libusb_get_active_config_descriptor(dev, &active_config);
459
/* Get current configuration of default device
460
* A configuration value of -1 denotes a quirky device which has
461
* trouble determining the current configuration. Just use the first
462
* branch (which may be incorrect)
464
if (Configuration > -1)
465
currentConfig = get_current_configuration(devh);
467
SHOW_PROGRESS(output,"Skipping the check for the current configuration\n");
538
/* Get current configuration of default device if parameter is set */
539
if (Configuration > -1) {
540
currentConfig = active_config->bConfigurationValue;
541
SHOW_PROGRESS(output,"Current configuration number is %d\n", currentConfig);
468
543
currentConfig = 0;
545
libusb_get_device_descriptor(dev, &descriptor);
546
defaultClass = descriptor.bDeviceClass;
548
Interface = active_config->interface[0].altsetting[0].bInterfaceNumber;
549
SHOW_PROGRESS(output,"Use interface number %d\n", Interface);
471
551
/* Get class of default device/interface */
472
defaultClass = dev->descriptor.bDeviceClass;
473
interfaceClass = get_interface0_class(dev, currentConfig);
552
interfaceClass = get_interface_class();
554
/* Check or get endpoints */
555
if (strlen(MessageContent) || StandardEject || InquireDevice || ModeMap & CISCO_MODE) {
556
if (!MessageEndpoint)
557
MessageEndpoint = find_first_bulk_endpoint(LIBUSB_ENDPOINT_OUT);
558
if (!ResponseEndpoint)
559
ResponseEndpoint = find_first_bulk_endpoint(LIBUSB_ENDPOINT_IN);
560
libusb_free_config_descriptor(active_config);
561
if (!MessageEndpoint) {
562
fprintf(stderr,"Error: message endpoint not given or found. Abort\n\n");
565
if (!ResponseEndpoint) {
566
fprintf(stderr,"Error: response endpoint not given or found. Abort\n\n");
569
SHOW_PROGRESS(output,"Use endpoints 0x%02x (out) and 0x%02x (in)\n", MessageEndpoint, ResponseEndpoint);
571
libusb_free_config_descriptor(active_config);
474
573
if (interfaceClass == -1) {
475
fprintf(stderr, "Error: getting the interface class failed. Aborting.\n\n");
574
fprintf(stderr, "Error: Could not get class of interface %d. Does it exist? Abort\n\n",Interface);
485
584
defaultClass = 8;
489
Interface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber;
490
SHOW_PROGRESS(output,"Using first interface: 0x%02x\n", Interface);
492
/* Check or get endpoints */
493
if (strlen(MessageContent) || InquireDevice || CiscoMode) {
494
if (!MessageEndpoint)
495
MessageEndpoint = find_first_bulk_output_endpoint(dev);
496
if (!MessageEndpoint) {
497
fprintf(stderr,"Error: message endpoint not given or found. Aborting.\n\n");
500
if (!ResponseEndpoint)
501
ResponseEndpoint = find_first_bulk_input_endpoint(dev);
502
if (!ResponseEndpoint) {
503
fprintf(stderr,"Error: response endpoint not given or found. Aborting.\n\n");
506
SHOW_PROGRESS(output,"Using endpoints 0x%02x (out) and 0x%02x (in)\n", MessageEndpoint, ResponseEndpoint);
509
if (!MessageEndpoint || !ResponseEndpoint)
510
if (InquireDevice && defaultClass == 0x08) {
511
SHOW_PROGRESS(output,"Endpoints not found, skipping SCSI inquiry\n");
587
if (strlen(MessageContent) && strncmp("55534243",MessageContent,8) == 0)
588
if (defaultClass != 8) {
589
fprintf(stderr, "Error: can't use storage command in MessageContent with interface %d;\n"
590
" interface class is %d, expected 8. Abort\n\n", Interface, defaultClass);
515
594
if (InquireDevice && show_progress) {
516
595
if (defaultClass == 0x08) {
517
SHOW_PROGRESS(output,"Inquiring device details; driver will be detached ...\n");
596
SHOW_PROGRESS(output,"Inquire device details; driver will be detached ...\n");
519
598
if (deviceInquire() >= 0)
520
599
InquireDevice = 2;
522
SHOW_PROGRESS(output,"Not a storage device, skipping SCSI inquiry\n");
601
SHOW_PROGRESS(output,"Not a storage device, skip SCSI inquiry\n");
525
604
deviceDescription();
526
605
if (show_progress) {
527
printf("\nUSB description data (for identification)\n");
528
printf("-------------------------\n");
529
printf("Manufacturer: %s\n", imanufact);
530
printf(" Product: %s\n", iproduct);
531
printf(" Serial No.: %s\n", iserial);
532
printf("-------------------------\n");
606
fprintf(output,"\nUSB description data (for identification)\n");
607
fprintf(output,"-------------------------\n");
608
fprintf(output,"Manufacturer: %s\n", imanufact);
609
fprintf(output," Product: %s\n", iproduct);
610
fprintf(output," Serial No.: %s\n", iserial);
611
fprintf(output,"-------------------------\n");
535
/* Some scenarios are exclusive, so check for unwanted combinations */
536
specialMode = DetachStorageOnly + HuaweiMode + SierraMode + SonyMode + QisdaMode + KobilMode
537
+ SequansMode + MobileActionMode + CiscoMode;
538
if ( specialMode > 1 ) {
539
SHOW_PROGRESS(output,"Invalid mode combination. Check your configuration. Aborting.\n\n");
614
/* Special modes are exclusive, so check for illegal combinations.
615
* More than one bit set?
617
if ( ModeMap & (ModeMap-1) ) {
618
fprintf(output,"Multiple special modes selected; check configuration. Abort\n\n");
543
if ( !specialMode && !strlen(MessageContent) && AltSetting == -1 && Configuration == 0 )
544
SHOW_PROGRESS(output,"Warning: no switching method given.\n");
622
if ((strlen(MessageContent) || StandardEject) && ModeMap ) {
623
MessageContent[0] = '\0';
625
fprintf(output,"Warning: MessageContent/StandardEject ignored; can't combine with special mode\n");
628
if (StandardEject && (strlen(MessageContent2) || strlen(MessageContent3))) {
629
fprintf(output,"Warning: MessageContent2/3 ignored; only one allowed with StandardEject\n");
632
if ( !ModeMap && !strlen(MessageContent) && AltSetting == -1 && !Configuration && !StandardEject )
633
SHOW_PROGRESS(output,"Warning: no switching method given. See documentation\n");
547
636
* The switching actions
744
858
memcpy(command, inquire_msg, sizeof (inquire_msg));
746
ret = usb_claim_interface(devh, Interface);
860
ret = libusb_claim_interface(devh, Interface);
748
SHOW_PROGRESS(output," Could not claim interface (error %d). Skipping device inquiry\n", ret);
751
usb_clear_halt(devh, MessageEndpoint);
753
ret = usb_bulk_write(devh, MessageEndpoint, (char *)command, 31, 0);
755
SHOW_PROGRESS(output," Could not send INQUIRY message (error %d)\n", ret);
759
ret = usb_bulk_read(devh, ResponseEndpoint, data, 36, 0);
761
SHOW_PROGRESS(output," Could not get INQUIRY response (error %d)\n", ret);
765
i = usb_bulk_read(devh, ResponseEndpoint, command, 13, 0);
767
printf("\nSCSI inquiry data (for identification)\n");
768
printf("-------------------------\n");
770
printf(" Vendor String: ");
862
SHOW_PROGRESS(output," Could not claim interface (error %d). Skip device inquiry\n", ret);
865
libusb_clear_halt(devh, MessageEndpoint);
867
ret = usb_bulk_io(devh, MessageEndpoint, (char *)command, 31, 0);
869
SHOW_PROGRESS(output," INQUIRY message failed (error %d)\n", ret);
873
ret = usb_bulk_io(devh, ResponseEndpoint, data, 36, 0);
875
SHOW_PROGRESS(output," INQUIRY response failed (error %d)\n", ret);
879
i = usb_bulk_io(devh, ResponseEndpoint, command, 13, 0);
881
fprintf(output,"\nSCSI inquiry data (for identification)\n");
882
fprintf(output,"-------------------------\n");
884
fprintf(output," Vendor String: ");
771
885
for (i = 8; i < 16; i++) printf("%c",data[i]);
886
fprintf(output,"\n");
774
printf(" Model String: ");
888
fprintf(output," Model String: ");
775
889
for (i = 16; i < 32; i++) printf("%c",data[i]);
890
fprintf(output,"\n");
778
printf("Revision String: ");
892
fprintf(output,"Revision String: ");
779
893
for (i = 32; i < 36; i++) printf("%c",data[i]);
781
printf("\n-------------------------\n");
895
fprintf(output,"\n-------------------------\n");
784
if (strlen(MessageContent) == 0)
785
usb_clear_halt(devh, MessageEndpoint);
786
usb_release_interface(devh, Interface);
898
if (strlen(MessageContent) == 0) {
899
libusb_clear_halt(devh, MessageEndpoint);
900
libusb_release_interface(devh, Interface);
907
/* Auxiliary function used by the wrapper */
908
int findMBIMConfig(int vendor, int product, int mode)
910
struct libusb_device **devs;
914
if (libusb_get_device_list(ctx, &devs) < 0) {
915
perror("Libusb could not access USB. Abort");
919
SHOW_PROGRESS(output,"Search USB devices ...\n");
920
while ((dev = devs[i++]) != NULL) {
921
struct libusb_device_descriptor descriptor;
922
libusb_get_device_descriptor(dev, &descriptor);
924
if (mode == SEARCH_BUSDEV) {
925
if ((libusb_get_bus_number(dev) != busnum) ||
926
(libusb_get_device_address(dev) != devnum)) {
929
if (descriptor.idVendor != vendor)
931
if (product != descriptor.idProduct)
935
SHOW_PROGRESS(output,"Found device, search for MBIM configuration...\n");
937
// No check if there is only one configuration
938
if (descriptor.bNumConfigurations < 2)
941
// Checking all interfaces of all configurations
942
for (j=0; j<descriptor.bNumConfigurations; j++) {
943
struct libusb_config_descriptor *config;
945
libusb_get_config_descriptor(dev, j, &config);
946
resultConfig = config->bConfigurationValue;
947
for (i=0; i<config->bNumInterfaces; i++) {
948
if ( config->interface[i].altsetting[0].bInterfaceClass == 2 )
949
if ( config->interface[i].altsetting[0].bInterfaceSubClass == 0x0e ) {
950
// found MBIM interface in this configuration
951
libusb_free_config_descriptor(config);
955
libusb_free_config_descriptor(config);
968
fprintf(output,"Device handle empty, skip USB reset\n");
797
971
if (show_progress) {
798
printf("Resetting usb device ");
972
fprintf(output,"Reset USB device ");
804
success = usb_reset(devh);
977
success = libusb_reset_device(devh);
805
978
if ( ((bpoint % 10) == 0) && show_progress ) {
810
983
if (bpoint > 100)
869
SHOW_PROGRESS(output,"Resetting response endpoint 0x%02x\n", ResponseEndpoint);
870
ret = usb_clear_halt(devh, ResponseEndpoint);
872
SHOW_PROGRESS(output," Could not reset endpoint (probably harmless): %d\n", ret);
873
SHOW_PROGRESS(output,"Resetting message endpoint 0x%02x\n", MessageEndpoint);
874
ret = usb_clear_halt(devh, MessageEndpoint);
876
SHOW_PROGRESS(output," Could not reset endpoint (probably harmless): %d\n", ret);
1042
SHOW_PROGRESS(output,"Reset response endpoint 0x%02x\n", ResponseEndpoint);
1043
ret = libusb_clear_halt(devh, ResponseEndpoint);
1045
SHOW_PROGRESS(output," Could not reset endpoint (probably harmless): %d\n", ret);
1046
SHOW_PROGRESS(output,"Reset message endpoint 0x%02x\n", MessageEndpoint);
1047
ret = libusb_clear_halt(devh, MessageEndpoint);
1049
SHOW_PROGRESS(output," Could not reset endpoint (probably harmless): %d\n", ret);
878
1052
if (ReleaseDelay) {
879
SHOW_PROGRESS(output,"Blocking the interface for %d ms before releasing ...\n", ReleaseDelay);
1053
SHOW_PROGRESS(output,"Wait for %d ms before releasing interface ...\n", ReleaseDelay);
880
1054
usleep(ReleaseDelay*1000);
882
ret = usb_release_interface(devh, Interface);
1056
ret = libusb_release_interface(devh, Interface);
888
SHOW_PROGRESS(output," Device is gone, skipping any further commands\n");
1062
SHOW_PROGRESS(output," Device is gone, skip any further commands\n");
894
1069
int switchConfiguration ()
896
1071
int count = SWITCH_CONFIG_MAXTRIES;
899
SHOW_PROGRESS(output,"Changing configuration to %i ...\n", Configuration);
900
while (((ret = usb_set_configuration(devh, Configuration)) < 0) && --count) {
901
SHOW_PROGRESS(output," Device is busy, trying to detach kernel driver\n");
1073
SHOW_PROGRESS(output,"Change configuration to %i ...\n", Configuration);
1074
while (((ret = libusb_set_configuration(devh, Configuration)) < 0) && --count) {
1075
SHOW_PROGRESS(output," Device is busy, try to detach kernel driver\n");
1079
SHOW_PROGRESS(output," Changing the configuration failed (error %d). Try to continue\n", ret);
905
1082
SHOW_PROGRESS(output," OK, configuration set\n");
908
SHOW_PROGRESS(output," Setting the configuration returned error %d. Trying to continue\n", ret);
913
1087
int switchAltSetting ()
917
SHOW_PROGRESS(output,"Changing to alt setting %i ...\n", AltSetting);
918
ret = usb_claim_interface(devh, Interface);
919
ret = usb_set_altinterface(devh, AltSetting);
920
usb_release_interface(devh, Interface);
922
SHOW_PROGRESS(output," Changing to alt setting returned error %d. Trying to continue\n", ret);
1090
SHOW_PROGRESS(output,"Change to alt setting %i ...\n", AltSetting);
1091
ret = libusb_claim_interface(devh, Interface);
1092
ret = libusb_set_interface_alt_setting(devh, Interface, AltSetting);
1093
libusb_release_interface(devh, Interface);
1095
SHOW_PROGRESS(output," Change to alt setting returned error %d. Try to continue\n", ret);
925
SHOW_PROGRESS(output," OK, changed to alt setting\n");
931
1102
void switchHuaweiMode ()
935
SHOW_PROGRESS(output,"Sending Huawei control message ...\n");
936
ret = usb_control_msg(devh, USB_TYPE_STANDARD + USB_RECIP_DEVICE, USB_REQ_SET_FEATURE, 00000001, 0, buffer, 0, 1000);
1105
SHOW_PROGRESS(output,"Send old Huawei control message ...\n");
1106
ret = libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE, \
1107
LIBUSB_REQUEST_SET_FEATURE, 00000001, 0, (unsigned char *)buffer, 0, 1000);
938
fprintf(stderr, "Error: sending Huawei control message failed (error %d). Aborting.\n\n", ret);
1109
fprintf(stderr, "Error: Huawei control message failed (error %d). Abort\n\n", ret);
941
SHOW_PROGRESS(output," OK, Huawei control message sent\n");
945
1115
void switchSierraMode ()
949
SHOW_PROGRESS(output,"Trying to send Sierra control message\n");
950
ret = usb_control_msg(devh, 0x40, 0x0b, 00000001, 0, buffer, 0, 1000);
952
fprintf(stderr, "Error: sending Sierra control message failed (error %d). Aborting.\n\n", ret);
1117
SHOW_PROGRESS(output,"Send Sierra control message\n");
1118
ret = libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_VENDOR, 0x0b, 00000001, 0, (unsigned char *)buffer, 0, 1000);
1119
if (ret == LIBUSB_ERROR_PIPE) {
1120
SHOW_PROGRESS(output," communication with device stopped. May have switched modes anyway\n");
1124
fprintf(stderr, "Error: Sierra control message failed (error %d). Abort\n\n", ret);
955
SHOW_PROGRESS(output," OK, Sierra control message sent\n");
959
1130
void switchGCTMode ()
963
ret = usb_claim_interface(devh, Interface);
1132
ret = libusb_claim_interface(devh, Interface);
965
SHOW_PROGRESS(output," Could not claim interface (error %d). Skipping GCT sequence \n", ret);
1134
SHOW_PROGRESS(output," Could not claim interface (error %d). Skip GCT sequence\n", ret);
969
SHOW_PROGRESS(output,"Sending GCT control message 1 ...\n");
970
ret = usb_control_msg(devh, 0xa1, 0xa0, 0, Interface, buffer, 1, 1000);
971
SHOW_PROGRESS(output,"Sending GCT control message 2 ...\n");
972
ret = usb_control_msg(devh, 0xa1, 0xfe, 0, Interface, buffer, 1, 1000);
973
SHOW_PROGRESS(output," OK, GCT control messages sent\n");
974
usb_release_interface(devh, Interface);
978
int switchKobilMode() {
981
SHOW_PROGRESS(output,"Sending Kobil control message ...\n");
982
ret = usb_control_msg(devh, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 0x88, 0, 0, buffer, 8, 1000);
984
fprintf(stderr, "Error: sending Kobil control message failed (error %d). Aborting.\n\n", ret);
987
SHOW_PROGRESS(output," OK, Kobil control message sent\n");
992
int switchQisdaMode () {
1137
SHOW_PROGRESS(output,"Send GCT control message 1 ...\n");
1138
ret = libusb_control_transfer(devh, 0xa1, 0xa0, 0, Interface, (unsigned char *)buffer, 1, 1000);
1140
SHOW_PROGRESS(output," GCT control message 1 failed (error %d), continue anyway ...\n", ret);
1142
SHOW_PROGRESS(output,"Send GCT control message 2 ...\n");
1143
ret = libusb_control_transfer(devh, 0xa1, 0xfe, 0, Interface, (unsigned char *)buffer, 1, 1000);
1145
SHOW_PROGRESS(output," GCT control message 2 failed (error %d). Abort\n\n", ret);
1147
libusb_release_interface(devh, Interface);
1153
void switchKobilMode() {
1154
SHOW_PROGRESS(output,"Send Kobil control message ...\n");
1155
ret = libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
1156
0x88, 0, 0, (unsigned char *)buffer, 8, 1000);
1158
fprintf(stderr, "Error: Kobil control message failed (error %d). Abort\n\n", ret);
1164
void switchQisdaMode () {
995
1165
SHOW_PROGRESS(output,"Sending Qisda control message ...\n");
996
1166
memcpy(buffer, "\x05\x8c\x04\x08\xa0\xee\x20\x00\x5c\x01\x04\x08\x98\xcd\xea\xbf", 16);
997
ret = usb_control_msg(devh, 0x40, 0x04, 00000000, 0, buffer, 16, 1000);
999
fprintf(stderr, "Error: sending Qisda control message failed (error %d). Aborting.\n\n", ret);
1002
SHOW_PROGRESS(output," OK, Qisda control message sent\n");
1007
int switchSonyMode ()
1013
printf("Note: CheckSuccess pointless with Sony mode, disabling\n");
1017
SHOW_PROGRESS(output,"Trying to send Sony control message\n");
1018
ret = usb_control_msg(devh, 0xc0, 0x11, 2, 0, buffer, 3, 100);
1020
fprintf(stderr, "Error: sending Sony control message failed (error %d). Aborting.\n\n", ret);
1023
SHOW_PROGRESS(output," OK, control message sent, waiting for device to return ...\n");
1028
/* Now waiting for the device to reappear */
1033
while ( dev == 0 && i < 30 ) {
1037
dev = search_devices(&found, DefaultVendor, DefaultProduct, "\0", TargetClass, 0, SEARCH_TARGET);
1042
if (show_progress) {
1048
SHOW_PROGRESS(output,"\n After %d seconds:",i);
1050
SHOW_PROGRESS(output," device came back, proceeding\n");
1051
devh = usb_open( dev );
1053
fprintf(stderr, "Error: could not get handle on device\n");
1057
SHOW_PROGRESS(output," device still gone, cancelling\n");
1062
SHOW_PROGRESS(output,"Sending Sony control message again ...\n");
1063
ret = usb_control_msg(devh, 0xc0, 0x11, 2, 0, buffer, 3, 100);
1065
fprintf(stderr, "Error: sending Sony control message (2) failed (error %d)\n", ret);
1068
SHOW_PROGRESS(output," OK, control message sent\n");
1167
ret = libusb_control_transfer(devh, 0x40, 0x04, 0, 0, (unsigned char *)buffer, 16, 1000);
1169
fprintf(stderr, "Error: Qisda control message failed (error %d). Abort\n\n", ret);
1175
void switchQuantaMode() {
1176
SHOW_PROGRESS(output,"Send Quanta control message ...\n");
1177
ret = libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
1178
0xff, 0, 0, (unsigned char *)buffer, 0, 1000);
1180
SHOW_PROGRESS(output,"Error: Quanta control message failed (error %d). Abort\n\n", ret);
1186
void switchBlackberryMode ()
1188
SHOW_PROGRESS(output,"Send Blackberry control message 1 ...\n");
1189
ret = libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
1190
0xb1, 0x0000, 0, (unsigned char *)buffer, 8, 1000);
1192
fprintf(stderr, "Error: Blackberry control message 1 failed (result %d)\n", ret);
1194
SHOW_PROGRESS(output,"Send Blackberry control message 2 ...\n");
1195
ret = libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
1196
0xa9, 0x000e, 0, (unsigned char *)buffer, 2, 1000);
1198
fprintf(stderr, "Error: Blackberry control message 2 failed (result %d). Abort\n\n", ret);
1204
void switchPantechMode()
1206
SHOW_PROGRESS(output,"Send Pantech control message ...\n");
1207
ret = libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, 0x70, 2, 0, (unsigned char *)buffer, 0, 1000);
1209
SHOW_PROGRESS(output," Error: Pantech control message failed (error %d). Abort\n\n", ret);
1077
1219
#define MOBILE_ACTION_READLOOP1 63
1078
1220
#define MOBILE_ACTION_READLOOP2 73
1080
int switchActionMode ()
1222
/* If anyone can test the MobileAction cable - I bet this
1223
* function (which is confirmed working) can be greatly
1227
void switchActionMode ()
1083
SHOW_PROGRESS(output,"Sending MobileAction control sequence ...\n");
1084
memcpy(buffer, "\xb0\x04\x00\x00\x02\x90\x26\x86", SIZE);
1085
usb_control_msg(devh, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 0x09, 0x0300, 0, buffer, SIZE, 1000);
1086
memcpy(buffer, "\xb0\x04\x00\x00\x02\x90\x26\x86", SIZE);
1087
usb_control_msg(devh, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 0x09, 0x0300, 0, buffer, SIZE, 1000);
1088
usb_interrupt_read(devh, EP_IN, buffer, SIZE, 1000);
1089
usb_interrupt_read(devh, EP_IN, buffer, SIZE, 1000);
1230
SHOW_PROGRESS(output,"Send MobileAction control sequence ...\n");
1231
memcpy(buffer, "\xb0\x04\x00\x00\x02\x90\x26\x86", SIZE);
1232
libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_CLASS + LIBUSB_RECIPIENT_INTERFACE, 0x09, 0x0300, 0, (unsigned char *)buffer, SIZE, 1000);
1233
memcpy(buffer, "\xb0\x04\x00\x00\x02\x90\x26\x86", SIZE);
1234
libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_CLASS + LIBUSB_RECIPIENT_INTERFACE, 0x09, 0x0300, 0, (unsigned char *)buffer, SIZE, 1000);
1235
usb_interrupt_io(devh, EP_IN, buffer, SIZE, 1000);
1236
usb_interrupt_io(devh, EP_IN, buffer, SIZE, 1000);
1090
1237
memcpy(buffer, "\x37\x01\xfe\xdb\xc1\x33\x1f\x83", SIZE);
1091
usb_interrupt_write(devh, EP_OUT, buffer, SIZE, 1000);
1092
usb_interrupt_read(devh, EP_IN, buffer, SIZE, 1000);
1238
usb_interrupt_io(devh, EP_OUT, buffer, SIZE, 1000);
1239
usb_interrupt_io(devh, EP_IN, buffer, SIZE, 1000);
1093
1240
memcpy(buffer, "\x37\x0e\xb5\x9d\x3b\x8a\x91\x51", SIZE);
1094
usb_interrupt_write(devh, EP_OUT, buffer, SIZE, 1000);
1095
usb_interrupt_read(devh, EP_IN, buffer, SIZE, 1000);
1241
usb_interrupt_io(devh, EP_OUT, buffer, SIZE, 1000);
1242
usb_interrupt_io(devh, EP_IN, buffer, SIZE, 1000);
1096
1243
memcpy(buffer, "\x34\x87\xba\x0d\xfc\x8a\x91\x51", SIZE);
1097
usb_interrupt_write(devh, EP_OUT, buffer, SIZE, 1000);
1244
usb_interrupt_io(devh, EP_OUT, buffer, SIZE, 1000);
1098
1245
for (i=0; i < MOBILE_ACTION_READLOOP1; i++) {
1099
usb_interrupt_read(devh, EP_IN, buffer, SIZE, 1000);
1246
usb_interrupt_io(devh, EP_IN, buffer, SIZE, 1000);
1101
1248
memcpy(buffer, "\x37\x01\xfe\xdb\xc1\x33\x1f\x83", SIZE);
1102
usb_interrupt_write(devh, EP_OUT, buffer, SIZE, 1000);
1103
usb_interrupt_read(devh, EP_IN, buffer, SIZE, 1000);
1249
usb_interrupt_io(devh, EP_OUT, buffer, SIZE, 1000);
1250
usb_interrupt_io(devh, EP_IN, buffer, SIZE, 1000);
1104
1251
memcpy(buffer, "\x37\x0e\xb5\x9d\x3b\x8a\x91\x51", SIZE);
1105
usb_interrupt_write(devh, EP_OUT, buffer, SIZE, 1000);
1106
usb_interrupt_read(devh, EP_IN, buffer, SIZE, 1000);
1252
usb_interrupt_io(devh, EP_OUT, buffer, SIZE, 1000);
1253
usb_interrupt_io(devh, EP_IN, buffer, SIZE, 1000);
1107
1254
memcpy(buffer, "\x34\x87\xba\x0d\xfc\x8a\x91\x51", SIZE);
1108
usb_interrupt_write(devh, EP_OUT, buffer, SIZE, 1000);
1255
usb_interrupt_io(devh, EP_OUT, buffer, SIZE, 1000);
1109
1256
for (i=0; i < MOBILE_ACTION_READLOOP2; i++) {
1110
usb_interrupt_read(devh, EP_IN, buffer, SIZE, 1000);
1257
usb_interrupt_io(devh, EP_IN, buffer, SIZE, 1000);
1112
1259
memcpy(buffer, "\x33\x04\xfe\x00\xf4\x6c\x1f\xf0", SIZE);
1113
usb_interrupt_write(devh, EP_OUT, buffer, SIZE, 1000);
1114
usb_interrupt_read(devh, EP_IN, buffer, SIZE, 1000);
1260
usb_interrupt_io(devh, EP_OUT, buffer, SIZE, 1000);
1261
usb_interrupt_io(devh, EP_IN, buffer, SIZE, 1000);
1115
1262
memcpy(buffer, "\x32\x07\xfe\xf0\x29\xb9\x3a\xf0", SIZE);
1116
ret = usb_interrupt_write(devh, EP_OUT, buffer, SIZE, 1000);
1117
usb_interrupt_read(devh, EP_IN, buffer, SIZE, 1000);
1263
ret = usb_interrupt_io(devh, EP_OUT, buffer, SIZE, 1000);
1264
usb_interrupt_io(devh, EP_IN, buffer, SIZE, 1000);
1119
1266
SHOW_PROGRESS(output," MobileAction control sequence did not complete\n Last error was %d\n",ret);
1122
1268
SHOW_PROGRESS(output," MobileAction control sequence complete\n");
1178
1317
if ( sendMessage(msg[i], i+1) )
1181
SHOW_PROGRESS(output,"Reading the response (CSW) to bulk message %d ...\n",i+1);
1320
SHOW_PROGRESS(output," Read the response (CSW) to bulk message %d ...\n",i+1);
1182
1321
ret = read_bulk(ResponseEndpoint, ByteString, 13);
1187
1326
if (ReleaseDelay) {
1188
SHOW_PROGRESS(output,"Blocking the interface for %d ms before releasing ...\n", ReleaseDelay);
1327
SHOW_PROGRESS(output,"Wait for %d ms before releasing interface ...\n", ReleaseDelay);
1189
1328
usleep(ReleaseDelay*1000);
1191
ret = usb_release_interface(devh, Interface);
1330
ret = libusb_release_interface(devh, Interface);
1336
SHOW_PROGRESS(output,"Device returned error %d, skip further commands\n", ret);
1342
int switchSonyMode ()
1351
SHOW_PROGRESS(output,"Send Sony control message\n");
1352
ret = libusb_control_transfer(devh, 0xc0, 0x11, 2, 0, (unsigned char *)buffer, 3, 100);
1354
fprintf(stderr, "Error: Sony control message failed (error %d). Abort\n\n", ret);
1357
SHOW_PROGRESS(output," OK, control message sent, wait for device to return ...\n");
1362
/* Now waiting for the device to reappear */
1367
while ( dev == 0 && i < 30 ) {
1369
dev = search_devices(&found, DefaultVendor, DefaultProductList, TargetClass, 0, SEARCH_TARGET);
1374
if (show_progress) {
1375
fprintf(output,"#");
1380
SHOW_PROGRESS(output,"\n After %d seconds:",i);
1382
SHOW_PROGRESS(output," device came back, proceed\n");
1383
libusb_open(dev, &devh);
1385
fprintf(stderr, "Error: could not get handle on device\n");
1389
SHOW_PROGRESS(output," device still gone, abort\n");
1394
SHOW_PROGRESS(output,"Send Sony control message again ...\n");
1395
ret = libusb_control_transfer(devh, 0xc0, 0x11, 2, 0, (unsigned char *)buffer, 3, 100);
1397
fprintf(stderr, "Error: Sony control message (2) failed (error %d)\n", ret);
1400
SHOW_PROGRESS(output," OK, control message sent\n");
1197
SHOW_PROGRESS(output,"Device returned error, skipping any further commands\n");
1203
1405
/* Detach driver
1205
1407
int detachDriver()
1209
#ifndef LIBUSB_HAS_GET_DRIVER_NP
1210
printf(" Cant't do driver detection and detaching on this platform.\n");
1410
// Driver already detached during SCSI inquiry ?
1411
if (InquireDevice == 2)
1214
1413
SHOW_PROGRESS(output,"Looking for active driver ...\n");
1215
ret = usb_get_driver_np(devh, Interface, buffer, BUF_SIZE);
1217
SHOW_PROGRESS(output," No driver found. Either detached before or never attached\n");
1414
ret = libusb_kernel_driver_active(devh, 0);
1415
if (ret == LIBUSB_ERROR_NOT_SUPPORTED) {
1416
fprintf(output," Can't do driver detection on this platform.\n");
1420
fprintf(output," Driver check failed with error %d. Try to continue\n", ret);
1424
SHOW_PROGRESS(output," No active driver found. Detached before or never attached\n");
1220
if (strncmp("dummy",buffer,5) == 0) {
1221
SHOW_PROGRESS(output," OK, driver found; name unknown, limitation of libusb1\n");
1222
strcpy(buffer,"unkown");
1224
SHOW_PROGRESS(output," OK, driver found (\"%s\")\n", buffer);
1428
ret = libusb_detach_kernel_driver(devh, Interface);
1429
if (ret == LIBUSB_ERROR_NOT_SUPPORTED) {
1430
fprintf(output," Can't do driver detaching on this platform.\n");
1227
#ifndef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1228
SHOW_PROGRESS(output," Can't do driver detaching on this platform\n");
1233
ret = usb_detach_kernel_driver_np(devh, Interface);
1234
1433
if (ret == 0) {
1235
SHOW_PROGRESS(output," OK, driver \"%s\" detached\n", buffer);
1434
SHOW_PROGRESS(output," OK, driver detached\n");
1237
SHOW_PROGRESS(output," Driver \"%s\" detach failed with error %d. Trying to continue\n", buffer, ret);
1436
SHOW_PROGRESS(output," Driver detach failed (error %d). Try to continue\n", ret);
1242
1441
int sendMessage(char* message, int count)
1244
int message_length, ret;
1246
1445
if (strlen(message) % 2 != 0) {
1247
1446
fprintf(stderr, "Error: MessageContent %d hex string has uneven length. Skipping ...\n", count);
1318
1517
* description is read for syslog message
1320
1519
for (i=i; i < CheckSuccess; i++) {
1321
SHOW_PROGRESS(output," Searching for target devices ...\n");
1322
ret = usb_find_busses();
1324
ret = usb_find_devices();
1326
SHOW_PROGRESS(output,"Error: libusb1 bug, no more searching, try to work around\n");
1330
dev = search_devices(&newTargetCount, TargetVendor, TargetProduct, TargetProductList, TargetClass, 0, SEARCH_TARGET);
1520
SHOW_PROGRESS(output," Search for target devices ...\n");
1521
dev = search_devices(&newTargetCount, TargetVendor, TargetProductList, TargetClass, 0, SEARCH_TARGET);
1331
1522
if (dev && (newTargetCount > targetDeviceCount)) {
1332
printf("\nFound target device, now opening\n");
1333
devh = usb_open(dev);
1523
fprintf(output,"\nFound target device, open it\n");
1524
libusb_open(dev, &devh);
1334
1525
deviceDescription();
1338
printf("\nFound target device %03d on bus %03d\n", \
1339
dev->devnum, (int)strtol(dev->bus->dirname,NULL,10));
1340
printf("\nTarget device description data\n");
1341
printf("-------------------------\n");
1342
printf("Manufacturer: %s\n", imanufact);
1343
printf(" Product: %s\n", iproduct);
1344
printf(" Serial No.: %s\n", iserial);
1345
printf("-------------------------\n");
1529
fprintf(output,"\nFound target device %03d on bus %03d\n", \
1530
libusb_get_device_address(dev), libusb_get_bus_number(dev));
1531
fprintf(output,"\nTarget device description data\n");
1532
fprintf(output,"-------------------------\n");
1533
fprintf(output,"Manufacturer: %s\n", imanufact);
1534
fprintf(output," Product: %s\n", iproduct);
1535
fprintf(output," Serial No.: %s\n", iserial);
1536
fprintf(output,"-------------------------\n");
1347
SHOW_PROGRESS(output," Found correct target device\n\nMode switch succeeded. Bye.\n\n");
1538
SHOW_PROGRESS(output," Found correct target device\n\nMode switch succeeded. Bye!\n\n");
1351
1542
if (i == CheckSuccess-1) {
1352
SHOW_PROGRESS(output," No new devices in target mode or class found\n\nMode switch has failed. Bye.\n\n");
1543
SHOW_PROGRESS(output," No new devices in target mode or class found\n\nMode switch has failed. Bye!\n\n");
1434
1622
/* Iterates over busses and devices, counts the ones which match the given
1435
1623
* parameters and returns the last one of them
1437
struct usb_device* search_devices( int *numFound, int vendor, int product, char* productList, int targetClass, int configuration, int mode)
1625
struct libusb_device* search_devices( int *numFound, int vendor, char* productList, int targetClass, int configuration, int mode)
1439
struct usb_bus *bus;
1440
1627
char *listcopy=NULL, *token, buffer[2];
1442
struct usb_device* right_dev = NULL;
1443
struct usb_dev_handle *testdevh;
1628
int devClass, product;
1629
struct libusb_device* right_dev = NULL;
1630
// struct libusb_device_handle *testdevh;
1631
struct libusb_device **devs;
1445
1634
/* only target class given, target vendor and product assumed unchanged */
1446
if ( targetClass && !(vendor || product) ) {
1635
if ( targetClass && !(vendor || strlen(productList)) ) {
1447
1636
vendor = DefaultVendor;
1448
product = DefaultProduct;
1637
productList = DefaultProductList;
1452
1641
/* Sanity check */
1453
if (!vendor || (!product && productList == '\0') )
1642
if (!vendor || productList == '\0')
1456
if (productList != '\0')
1457
listcopy = malloc(strlen(productList)+1);
1459
for (bus = usb_get_busses(); bus; bus = bus->next) {
1460
if (mode == SEARCH_BUSDEV)
1461
if (busnum != (int)strtol(bus->dirname,NULL,10))
1463
struct usb_device *dev;
1464
for (dev = bus->devices; dev; dev = dev->next) {
1465
if (mode == SEARCH_BUSDEV) {
1466
if (dev->devnum != devnum)
1469
SHOW_PROGRESS(output," bus/device number matched\n");
1472
fprintf (output," searching devices, found USB ID %04x:%04x\n", dev->descriptor.idVendor, dev->descriptor.idProduct);
1473
if (dev->descriptor.idVendor != vendor)
1476
fprintf (output," found matching vendor ID\n");
1477
// product list given
1478
if ( strlen(productList) ) {
1479
strcpy(listcopy, productList);
1480
token = strtok(listcopy, ",");
1481
while (token != NULL) {
1482
if (strlen(token) != 4) {
1483
SHOW_PROGRESS(output,"Error: entry in product ID list has wrong length: %s. Ignoring\n", token);
1486
if ( hexstr2bin(token, buffer, strlen(token)/2) == -1) {
1487
SHOW_PROGRESS(output,"Error: entry in product ID list is not a hex string: %s. Ignoring\n", token);
1491
product += (unsigned char)buffer[0];
1493
product += (unsigned char)buffer[1];
1494
if (product == dev->descriptor.idProduct) {
1645
listcopy = malloc(strlen(productList)+1);
1647
if (libusb_get_device_list(ctx, &devs) < 0) {
1648
perror("Libusb failed to get USB access!");
1652
while ((dev = devs[i++]) != NULL) {
1653
struct libusb_device_descriptor descriptor;
1654
libusb_get_device_descriptor(dev, &descriptor);
1656
if (mode == SEARCH_BUSDEV) {
1657
if ((libusb_get_bus_number(dev) != busnum) ||
1658
(libusb_get_device_address(dev) != devnum))
1661
SHOW_PROGRESS(output," bus/device number matched\n");
1665
fprintf (output," found USB ID %04x:%04x\n",
1666
descriptor.idVendor, descriptor.idProduct);
1667
if (descriptor.idVendor != vendor)
1670
fprintf (output," vendor ID matched\n");
1672
strcpy(listcopy, productList);
1673
token = strtok(listcopy, ",");
1674
while (token != NULL) {
1675
if (strlen(token) != 4) {
1676
SHOW_PROGRESS(output,"Error: entry in product ID list has wrong length: %s. Ignored\n", token);
1679
if ( hexstr2bin(token, buffer, strlen(token)/2) == -1) {
1680
SHOW_PROGRESS(output,"Error: entry in product ID list is not a hex string: %s. Ignored\n", token);
1684
product += (unsigned char)buffer[0];
1686
product += (unsigned char)buffer[1];
1687
if (product == descriptor.idProduct) {
1688
SHOW_PROGRESS(output," product ID matched\n");
1690
if (targetClass != 0) {
1691
// TargetClass is set, check class of first interface
1692
struct libusb_device_descriptor descriptor;
1693
libusb_get_device_descriptor(dev, &descriptor);
1694
devClass = descriptor.bDeviceClass;
1695
struct libusb_config_descriptor *config;
1696
libusb_get_config_descriptor(dev, 0, &config);
1697
int ifaceClass = config->interface[0].altsetting[0].bInterfaceClass;
1698
libusb_free_config_descriptor(config);
1700
devClass = ifaceClass;
1702
/* Check for some quirky devices */
1703
if (devClass != ifaceClass)
1704
devClass = ifaceClass;
1705
if (devClass == targetClass) {
1496
fprintf (output," found matching product ID from list\n");
1707
fprintf (output," target class %02x matches\n", targetClass);
1708
if (mode == SEARCH_TARGET) {
1499
1710
right_dev = dev;
1501
if (dev->devnum >= devnum && (int)strtol(dev->bus->dirname,NULL,10) == busnum) {
1503
TargetProduct = dev->descriptor.idProduct;
1508
token = strtok(NULL, ",");
1510
/* Product ID is given */
1512
if (product == dev->descriptor.idProduct) {
1513
SHOW_PROGRESS(output," found matching product ID\n");
1514
if (targetClass == 0 && configuration < 1) {
1516
SHOW_PROGRESS(output," adding device\n");
1712
fprintf (output," count device\n");
1715
fprintf (output," device not counted, target class reached\n");
1519
if (targetClass != 0) {
1520
devClass = dev->descriptor.bDeviceClass;
1522
devClass = dev->config[0].interface[0].altsetting[0].bInterfaceClass;
1524
/* Check for some quirky devices */
1525
if (devClass != dev->config[0].interface[0].altsetting[0].bInterfaceClass)
1526
devClass = dev->config[0].interface[0].altsetting[0].bInterfaceClass;
1527
if (devClass == targetClass) {
1529
fprintf (output," target class %02x matching\n", targetClass);
1530
if (mode == SEARCH_TARGET) {
1534
fprintf (output," adding device\n");
1537
fprintf (output," not adding device\n");
1540
fprintf (output," target class %02x not matching\n", targetClass);
1541
if (mode == SEARCH_DEFAULT || mode == SEARCH_BUSDEV) {
1545
fprintf (output," adding device\n");
1549
// check configuration (only if no target class given)
1550
testdevh = usb_open(dev);
1551
int testconfig = get_current_configuration(testdevh);
1552
if (testconfig != configuration) {
1554
fprintf (output," device configuration %d not matching parameter\n", testconfig);
1558
fprintf (output," adding device\n");
1561
fprintf (output," not adding device, target configuration already set\n");
1718
fprintf (output," device class %02x not matching target\n", devClass);
1719
if (mode == SEARCH_DEFAULT || mode == SEARCH_BUSDEV) {
1723
fprintf (output," count device\n");
1726
} else if (configuration > 0) {
1727
// Configuration parameter is set, check device configuration
1728
int testconfig = get_current_configuration(dev);
1729
if (testconfig != configuration) {
1731
fprintf (output," device configuration %d not matching target\n", testconfig);
1735
fprintf (output," count device\n");
1738
fprintf (output," device not counted, target configuration reached\n");
1740
// Neither TargetClass nor Configuration are set
1743
if (mode == SEARCH_BUSDEV)
1749
token = strtok(NULL, ",");
1567
1752
if (listcopy != NULL)
1576
1761
/* Autodetect bulk endpoints (ab) */
1578
int find_first_bulk_output_endpoint(struct usb_device *dev)
1581
struct usb_interface_descriptor *alt = &(dev->config[0].interface[0].altsetting[0]);
1582
struct usb_endpoint_descriptor *ep;
1584
for(i=0;i < alt->bNumEndpoints;i++) {
1585
ep=&(alt->endpoint[i]);
1586
if( ( (ep->bmAttributes & USB_ENDPOINT_TYPE_MASK) == USB_ENDPOINT_TYPE_BULK) &&
1587
( (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT ) ) {
1588
return ep->bEndpointAddress;
1596
int find_first_bulk_input_endpoint(struct usb_device *dev)
1599
struct usb_interface_descriptor *alt = &(dev->config[0].interface[0].altsetting[0]);
1600
struct usb_endpoint_descriptor *ep;
1602
for(i=0;i < alt->bNumEndpoints;i++) {
1603
ep=&(alt->endpoint[i]);
1604
if( ( (ep->bmAttributes & USB_ENDPOINT_TYPE_MASK) == USB_ENDPOINT_TYPE_BULK) &&
1605
( (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN ) ) {
1606
return ep->bEndpointAddress;
1613
int get_current_configuration(struct usb_dev_handle* devh)
1617
SHOW_PROGRESS(output,"Getting the current device configuration ...\n");
1618
ret = usb_control_msg(devh, USB_DIR_IN + USB_TYPE_STANDARD + USB_RECIP_DEVICE, USB_REQ_GET_CONFIGURATION, 0, 0, buffer, 1, 1000);
1620
// There are quirky devices which fail to respond properly to this command
1621
fprintf(stderr, "Error getting the current configuration (error %d). Assuming configuration 1.\n", ret);
1622
if (Configuration) {
1623
fprintf(stderr, " No configuration setting possible for this device.\n");
1628
SHOW_PROGRESS(output," OK, got current device configuration (%d)\n", buffer[0]);
1634
int get_interface0_class(struct usb_device *dev, int devconfig)
1636
/* Hack for quirky devices */
1638
return dev->config[0].interface[0].altsetting[0].bInterfaceClass;
1641
for (i=0; i<dev->descriptor.bNumConfigurations; i++)
1642
if (dev->config[i].bConfigurationValue == devconfig)
1643
return dev->config[i].interface[0].altsetting[0].bInterfaceClass;
1763
int find_first_bulk_endpoint(int direction)
1766
const struct libusb_interface_descriptor *alt;
1767
const struct libusb_endpoint_descriptor *ep;
1769
for (j=0; j < active_config->bNumInterfaces; j++) {
1770
alt = &(active_config->interface[j].altsetting[0]);
1771
if (alt->bInterfaceNumber == Interface) {
1772
for(i=0; i < alt->bNumEndpoints; i++) {
1773
ep=&(alt->endpoint[i]);
1774
if( ( (ep->bmAttributes & LIBUSB_ENDPOINT_ADDRESS_MASK) == LIBUSB_TRANSFER_TYPE_BULK) &&
1775
( (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == direction ) ) {
1776
return ep->bEndpointAddress;
1784
int get_current_configuration()
1787
SHOW_PROGRESS(output,"Get the current device configuration ...\n");
1788
if (active_config == NULL)
1789
libusb_get_active_config_descriptor(dev, &active_config);
1791
cfg = active_config->bConfigurationValue;
1792
libusb_free_config_descriptor(active_config);
1799
int get_interface_class()
1802
for (i=0; i < active_config->bNumInterfaces; i++) {
1803
if (active_config->interface[i].altsetting[0].bInterfaceNumber == Interface)
1804
return active_config->interface[i].altsetting[0].bInterfaceClass;
1648
1809
/* Parameter parsing */
1650
1811
char* ReadParseParam(const char* FileName, char *VariableName)
1652
1813
static int numLines = 0;
1653
1814
static char* ConfigBuffer[MAXLINES];
1655
1815
char *VarName, *Comment=NULL, *Equal=NULL;
1656
1816
char *FirstQuote, *LastQuote, *P1, *P2;
1657
int Line=0, Len=0, Pos=0;
1818
unsigned Len=0, Pos=0;
1658
1819
char Str[LINE_DIM], *token, *configPos;
1659
1820
FILE *file = NULL;
1661
1822
// Reading and storing input during the first call
1662
1823
if (numLines==0) {
1663
1824
if (strncmp(FileName,"##",2) == 0) {
1664
if (verbose) fprintf(output,"\nReading long config from command line\n");
1825
if (verbose) fprintf(output,"\nRead long config from command line\n");
1665
1826
// "Embedded" configuration data
1666
1827
configPos = (char*)FileName;
1667
1828
token = strtok(configPos, "\n");
1668
1829
strncpy(Str,token,LINE_DIM-1);
1670
1831
if (strcmp(FileName, "stdin")==0) {
1671
if (verbose) fprintf(output,"\nReading long config from stdin\n");
1832
if (verbose) fprintf(output,"\nRead long config from stdin\n");
1674
if (verbose) fprintf(output,"\nReading config file: %s\n", FileName);
1835
if (verbose) fprintf(output,"\nRead config file: %s\n", FileName);
1675
1836
file=fopen(FileName, "r");
1677
1838
if (file==NULL) {
1678
fprintf(stderr, "Error: Could not find file %s\n\n", FileName);
1839
fprintf(stderr, "Error: Could not find file %s. Abort\n\n", FileName);
1681
1842
token = fgets(Str, LINE_DIM-1, file);