FreeCalypso > hg > fc-usbser-tools
comparison fteeprom/ftee-gen2232h.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_char group0, group1, group2, group3; | |
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 = 0x0D; | |
56 } else { | |
57 eeprom_size = 128; | |
58 eeprom_string_ptr = 0x4D; | |
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, "group0")) | |
118 group0 = strtoul(cp, 0, 16); | |
119 else if (!strcmp(np, "group1")) | |
120 group1 = strtoul(cp, 0, 16); | |
121 else if (!strcmp(np, "group2")) | |
122 group2 = strtoul(cp, 0, 16); | |
123 else if (!strcmp(np, "group3")) | |
124 group3 = strtoul(cp, 0, 16); | |
125 else { | |
126 fprintf(stderr, "%s line %d: unknown \"%s\" setting\n", | |
127 configfile, lineno, np); | |
128 exit(1); | |
129 } | |
130 } | |
131 fclose(inf); | |
132 if (!manuf) { | |
133 fprintf(stderr, "error: manuf not set in %s\n", configfile); | |
134 exit(1); | |
135 } | |
136 if (!product) { | |
137 fprintf(stderr, "error: product not set in %s\n", configfile); | |
138 exit(1); | |
139 } | |
140 } | |
141 | |
142 write_string(str) | |
143 char *str; | |
144 { | |
145 unsigned longlen, startptr; | |
146 | |
147 if (eeprom_size - 1 - eeprom_string_ptr < strlen(str) + 1) { | |
148 fprintf(stderr, "error: strings are too long\n"); | |
149 exit(1); | |
150 } | |
151 longlen = strlen(str) * 2 + 2; | |
152 startptr = eeprom_string_ptr; | |
153 eeprom[eeprom_string_ptr++] = 0x0300 | longlen; | |
154 while (*str) | |
155 eeprom[eeprom_string_ptr++] = *str++; | |
156 return (longlen << 8) | 0x80 | (startptr << 1); | |
157 } | |
158 | |
159 fill_eeprom() | |
160 { | |
161 u_char byte09; | |
162 | |
163 if (serial) | |
164 byte0A |= 0x08; | |
165 else | |
166 byte0A &= 0xF7; | |
167 byte09 = maxpower / 2; | |
168 eeprom[0] = (byte01 << 8) | byte00; | |
169 eeprom[1] = vid; | |
170 eeprom[2] = pid; | |
171 eeprom[3] = 0x0700; | |
172 eeprom[4] = (byte09 << 8) | byte08; | |
173 eeprom[5] = byte0A; | |
174 eeprom[6] = (group3 << 12) | (group2 << 8) | (group1 << 4) | group0; | |
175 eeprom[7] = write_string(manuf); | |
176 eeprom[8] = write_string(product); | |
177 if (serial) | |
178 eeprom[9] = write_string(serial); | |
179 else | |
180 eeprom[9] = 0; | |
181 eeprom[12] = eeprom_chip; | |
182 } | |
183 | |
184 do_checksum() | |
185 { | |
186 u_short chksum = 0xAAAA; | |
187 unsigned n; | |
188 | |
189 for (n = 0; n < eeprom_size - 1; n++) { | |
190 chksum ^= eeprom[n]; | |
191 chksum = (chksum << 1) | (chksum >> 15); | |
192 } | |
193 eeprom[n] = chksum; | |
194 } | |
195 | |
196 emit_output() | |
197 { | |
198 unsigned n, col; | |
199 | |
200 for (n = 0; n < eeprom_size; n++) { | |
201 col = n & 7; | |
202 if (col == 0) | |
203 printf("%02X:", n * 2); | |
204 printf(" %04X", eeprom[n]); | |
205 if (col == 7) | |
206 putchar('\n'); | |
207 } | |
208 } | |
209 | |
210 main(argc, argv) | |
211 char **argv; | |
212 { | |
213 process_cmdline(argc, argv); | |
214 read_config_file(); | |
215 init_eeprom_size(); | |
216 fill_eeprom(); | |
217 do_checksum(); | |
218 emit_output(); | |
219 exit(0); | |
220 } |