comparison fteeprom/fteeprom-prog.c @ 45:a94bd4002076

fteeprom-prog: implement -d option for interface detach
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Sep 2023 02:48:22 +0000
parents 940cde8a99b6
children ca2250b4833d
comparison
equal deleted inserted replaced
44:5160f6717903 45:a94bd4002076
17 #include <usb.h> 17 #include <usb.h>
18 #include "../libuwrap/find_dev.h" 18 #include "../libuwrap/find_dev.h"
19 #include "../libuwrap/open_close.h" 19 #include "../libuwrap/open_close.h"
20 #include "../libftmini/eeprom_func.h" 20 #include "../libftmini/eeprom_func.h"
21 21
22 char *device_selector, *input_filename;
23 int detach_num, ft232r_mode;
22 unsigned eeprom_size; 24 unsigned eeprom_size;
23 u_short eeprom[256]; 25 u_short eeprom[256];
26
27 process_cmdline(argc, argv)
28 char **argv;
29 {
30 int c;
31 extern int optind;
32 extern char *optarg;
33
34 while ((c = getopt(argc, argv, "d:r")) != EOF) {
35 switch (c) {
36 case 'd':
37 detach_num = atoi(optarg);
38 switch (detach_num) {
39 case 0:
40 case 1:
41 case 2:
42 case 4:
43 break;
44 default:
45 fprintf(stderr, "error: invalid -d argument\n");
46 exit(1);
47 }
48 continue;
49 case 'r':
50 ft232r_mode = 1;
51 detach_num = 1;
52 continue;
53 default:
54 /* error msg already printed */
55 exit(1);
56 }
57 }
58 if (argc < optind + 1 || argc > optind + 2) {
59 fprintf(stderr,
60 "usage: %s [options] device-selector [eeprom-image]\n",
61 argv[0]);
62 exit(1);
63 }
64 device_selector = argv[optind];
65 input_filename = argv[optind + 1];
66 }
24 67
25 read_eeprom_common(inf, filename_for_errs) 68 read_eeprom_common(inf, filename_for_errs)
26 FILE *inf; 69 FILE *inf;
27 char *filename_for_errs; 70 char *filename_for_errs;
28 { 71 {
81 } 124 }
82 125
83 main(argc, argv) 126 main(argc, argv)
84 char **argv; 127 char **argv;
85 { 128 {
86 char **ap, *devsel;
87 struct usb_device *dev; 129 struct usb_device *dev;
88 usb_dev_handle *usbh; 130 usb_dev_handle *usbh;
89 int ft232r_mode = 0;
90 unsigned n; 131 unsigned n;
91 132
92 if (argc < 2) { 133 process_cmdline(argc, argv);
93 usage: fprintf(stderr, 134 if (input_filename)
94 "usage: %s [-r] device-selector [eeprom-image]\n", 135 read_eeprom_from_file(input_filename);
95 argv[0]); 136 else
96 exit(1);
97 }
98 ap = argv + 1;
99 if (!strcmp(*ap, "-r")) {
100 ft232r_mode = 1;
101 ap++;
102 }
103 if (!*ap)
104 goto usage;
105 devsel = *ap++;
106 if (*ap) {
107 if (ap[1])
108 goto usage;
109 read_eeprom_from_file(*ap);
110 } else
111 read_eeprom_from_stdin(); 137 read_eeprom_from_stdin();
112 dev = find_usbdev_by_desc_string(devsel); 138 dev = find_usbdev_by_desc_string(device_selector);
113 if (!dev) { 139 if (!dev) {
114 fprintf(stderr, "error: specified USB device not found\n"); 140 fprintf(stderr, "error: specified USB device not found\n");
115 exit(1); 141 exit(1);
116 } 142 }
117 usbh = usb_open(dev); 143 usbh = usb_open(dev);
118 if (!usbh) { 144 if (!usbh) {
119 fprintf(stderr, "error: usb_open() failed\n"); 145 fprintf(stderr, "error: usb_open() failed\n");
120 exit(1); 146 exit(1);
121 } 147 }
148 if (detach_num)
149 usbwrap_claim_all_ifs(usbh, detach_num);
122 if (ft232r_mode) 150 if (ft232r_mode)
123 ft232r_eeprom_magic(usbh); 151 ft232r_eeprom_magic(usbh);
124 for (n = 0; n < eeprom_size; n++) 152 for (n = 0; n < eeprom_size; n++)
125 ftmini_write_eeprom_loc(usbh, n, eeprom[n]); 153 ftmini_write_eeprom_loc(usbh, n, eeprom[n]);
126 usb_close(usbh); 154 usb_close(usbh);