FreeCalypso > hg > fc-usbser-tools
comparison fteeprom/ftee-gen2232c.c @ 0:11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 03 Sep 2023 18:08:22 +0000 |
parents | |
children | b2c891299e83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:11b8a30333b3 |
---|---|
1 #include <sys/types.h> | |
2 #include <ctype.h> | |
3 #include <string.h> | |
4 #include <strings.h> | |
5 #include <stdio.h> | |
6 #include <stdlib.h> | |
7 #include <unistd.h> | |
8 | |
9 char *configfile, *serial; | |
10 | |
11 u_short vid = 0x0403, pid = 0x6010; | |
12 char *manuf, *product; | |
13 u_char byte00 = 0x08, byte01 = 0x08; | |
14 u_char byte08 = 0x80, byte0A = 0x00; | |
15 unsigned maxpower = 100; | |
16 u_short usb_version = 0x0200; | |
17 | |
18 u_short eeprom[128]; | |
19 u_char eeprom_chip = 0x46; | |
20 unsigned eeprom_size, eeprom_string_ptr; | |
21 | |
22 process_cmdline(argc, argv) | |
23 char **argv; | |
24 { | |
25 int c; | |
26 extern int optind; | |
27 | |
28 while ((c = getopt(argc, argv, "bB")) != EOF) { | |
29 switch (c) { | |
30 case 'b': | |
31 eeprom_chip = 0x56; | |
32 continue; | |
33 case 'B': | |
34 eeprom_chip = 0x66; | |
35 continue; | |
36 default: | |
37 /* error msg already printed */ | |
38 exit(1); | |
39 } | |
40 } | |
41 if (argc < optind + 1 || argc > optind + 2) { | |
42 fprintf(stderr, | |
43 "usage: %s [options] config-file [serial-num]\n", | |
44 argv[0]); | |
45 exit(1); | |
46 } | |
47 configfile = argv[optind]; | |
48 serial = argv[optind+1]; | |
49 } | |
50 | |
51 init_eeprom_size() | |
52 { | |
53 if (eeprom_chip == 0x46) { | |
54 eeprom_size = 64; | |
55 eeprom_string_ptr = 0x0B; | |
56 } else { | |
57 eeprom_size = 128; | |
58 eeprom_string_ptr = 0x4B; | |
59 } | |
60 } | |
61 | |
62 read_config_file() | |
63 { | |
64 FILE *inf; | |
65 char linebuf[1024]; | |
66 int lineno; | |
67 char *cp, *np; | |
68 | |
69 inf = fopen(configfile, "r"); | |
70 if (!inf) { | |
71 perror(configfile); | |
72 exit(1); | |
73 } | |
74 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) { | |
75 cp = index(linebuf, '\n'); | |
76 if (!cp) { | |
77 fprintf(stderr, | |
78 "%s line %d: too long or unterminated\n", | |
79 configfile, lineno); | |
80 exit(1); | |
81 } | |
82 *cp = '\0'; | |
83 for (cp = linebuf; isspace(*cp); cp++) | |
84 ; | |
85 if (*cp == '\0' || *cp == '#') | |
86 continue; | |
87 for (np = cp; *cp && !isspace(*cp); cp++) | |
88 ; | |
89 if (*cp) | |
90 *cp++ = '\0'; | |
91 while (isspace(*cp)) | |
92 cp++; | |
93 if (*cp == '\0' || *cp == '#') { | |
94 fprintf(stderr, | |
95 "%s line %d: \"%s\" setting without argument\n", | |
96 configfile, lineno, np); | |
97 exit(1); | |
98 } | |
99 if (!strcmp(np, "vid")) | |
100 vid = strtoul(cp, 0, 16); | |
101 else if (!strcmp(np, "pid")) | |
102 pid = strtoul(cp, 0, 16); | |
103 else if (!strcmp(np, "manuf")) | |
104 manuf = strdup(cp); | |
105 else if (!strcmp(np, "product")) | |
106 product = strdup(cp); | |
107 else if (!strcmp(np, "byte00")) | |
108 byte00 = strtoul(cp, 0, 16); | |
109 else if (!strcmp(np, "byte01")) | |
110 byte01 = strtoul(cp, 0, 16); | |
111 else if (!strcmp(np, "byte08")) | |
112 byte08 = strtoul(cp, 0, 16); | |
113 else if (!strcmp(np, "byte0A")) | |
114 byte0A = strtoul(cp, 0, 16); | |
115 else if (!strcmp(np, "maxpower")) | |
116 maxpower = strtoul(cp, 0, 10); | |
117 else if (!strcmp(np, "usbver")) | |
118 usb_version = strtoul(cp, 0, 16); | |
119 else { | |
120 fprintf(stderr, "%s line %d: unknown \"%s\" setting\n", | |
121 configfile, lineno, np); | |
122 exit(1); | |
123 } | |
124 } | |
125 fclose(inf); | |
126 if (!manuf) { | |
127 fprintf(stderr, "error: manuf not set in %s\n", configfile); | |
128 exit(1); | |
129 } | |
130 if (!product) { | |
131 fprintf(stderr, "error: product not set in %s\n", configfile); | |
132 exit(1); | |
133 } | |
134 } | |
135 | |
136 write_string(str) | |
137 char *str; | |
138 { | |
139 unsigned longlen, startptr; | |
140 | |
141 if (eeprom_size - 1 - eeprom_string_ptr < strlen(str) + 1) { | |
142 fprintf(stderr, "error: strings are too long\n"); | |
143 exit(1); | |
144 } | |
145 longlen = strlen(str) * 2 + 2; | |
146 startptr = eeprom_string_ptr; | |
147 eeprom[eeprom_string_ptr++] = 0x0300 | longlen; | |
148 while (*str) | |
149 eeprom[eeprom_string_ptr++] = *str++; | |
150 return (longlen << 8) | 0x80 | (startptr << 1); | |
151 } | |
152 | |
153 fill_eeprom() | |
154 { | |
155 u_char byte09; | |
156 | |
157 if (serial) | |
158 byte0A |= 0x08; | |
159 else | |
160 byte0A &= 0xF7; | |
161 byte09 = maxpower / 2; | |
162 eeprom[0] = (byte01 << 8) | byte00; | |
163 eeprom[1] = vid; | |
164 eeprom[2] = pid; | |
165 eeprom[3] = 0x0500; | |
166 eeprom[4] = (byte09 << 8) | byte08; | |
167 eeprom[5] = byte0A; | |
168 eeprom[6] = usb_version; | |
169 eeprom[7] = write_string(manuf); | |
170 eeprom[8] = write_string(product); | |
171 if (serial) | |
172 eeprom[9] = write_string(serial); | |
173 else | |
174 eeprom[9] = 0; | |
175 eeprom[10] = eeprom_chip; | |
176 } | |
177 | |
178 do_checksum() | |
179 { | |
180 u_short chksum = 0xAAAA; | |
181 unsigned n; | |
182 | |
183 for (n = 0; n < eeprom_size - 1; n++) { | |
184 chksum ^= eeprom[n]; | |
185 chksum = (chksum << 1) | (chksum >> 15); | |
186 } | |
187 eeprom[n] = chksum; | |
188 } | |
189 | |
190 emit_output() | |
191 { | |
192 unsigned n, col; | |
193 | |
194 for (n = 0; n < eeprom_size; n++) { | |
195 col = n & 7; | |
196 if (col == 0) | |
197 printf("%02X:", n * 2); | |
198 printf(" %04X", eeprom[n]); | |
199 if (col == 7) | |
200 putchar('\n'); | |
201 } | |
202 } | |
203 | |
204 main(argc, argv) | |
205 char **argv; | |
206 { | |
207 process_cmdline(argc, argv); | |
208 read_config_file(); | |
209 init_eeprom_size(); | |
210 fill_eeprom(); | |
211 do_checksum(); | |
212 emit_output(); | |
213 exit(0); | |
214 } |