Commit | Line | Data |
---|---|---|
a3da1d95 GE |
1 | /*************************************************************************** |
2 | ftdi.c - description | |
3 | ------------------- | |
4 | begin : Fri Apr 4 2003 | |
5 | copyright : (C) 2003 by Intra2net AG | |
5fdb1cb1 | 6 | email : opensource@intra2net.com |
a3da1d95 GE |
7 | ***************************************************************************/ |
8 | ||
9 | /*************************************************************************** | |
10 | * * | |
11 | * This program is free software; you can redistribute it and/or modify * | |
12 | * it under the terms of the GNU Lesser General Public License * | |
13 | * version 2.1 as published by the Free Software Foundation; * | |
14 | * * | |
15 | ***************************************************************************/ | |
16 | ||
17 | #include <usb.h> | |
d9f0cce7 | 18 | |
a3da1d95 GE |
19 | #include "ftdi.h" |
20 | ||
948f9ada TJ |
21 | /* ftdi_init return codes: |
22 | 0: all fine | |
6d9aa99f | 23 | -1: couldn't allocate read buffer |
948f9ada | 24 | */ |
a3da1d95 GE |
25 | int ftdi_init(struct ftdi_context *ftdi) { |
26 | ftdi->usb_dev = NULL; | |
545820ce TJ |
27 | ftdi->usb_read_timeout = 5000; |
28 | ftdi->usb_write_timeout = 5000; | |
a3da1d95 GE |
29 | |
30 | ftdi->baudrate = -1; | |
31 | ftdi->bitbang_enabled = 0; | |
32 | ||
948f9ada TJ |
33 | ftdi->readbuffer = NULL; |
34 | ftdi->readbuffer_offset = 0; | |
35 | ftdi->readbuffer_remaining = 0; | |
36 | ftdi->writebuffer_chunksize = 4096; | |
37 | ||
545820ce TJ |
38 | ftdi->interface = 0; |
39 | ftdi->index = 0; | |
40 | ftdi->in_ep = 0x02; | |
41 | ftdi->out_ep = 0x81; | |
a3da1d95 | 42 | ftdi->error_str = NULL; |
545820ce | 43 | ftdi->reading = 0; |
a3da1d95 | 44 | |
948f9ada TJ |
45 | // all fine. Now allocate the readbuffer |
46 | return ftdi_read_data_set_chunksize(ftdi, 4096); | |
47 | } | |
48 | ||
49 | ||
50 | void ftdi_deinit(struct ftdi_context *ftdi) { | |
51 | if (ftdi->readbuffer != NULL) { | |
d9f0cce7 TJ |
52 | free(ftdi->readbuffer); |
53 | ftdi->readbuffer = NULL; | |
948f9ada | 54 | } |
a3da1d95 GE |
55 | } |
56 | ||
57 | ||
58 | void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) { | |
59 | ftdi->usb_dev = usb; | |
60 | } | |
61 | ||
62 | ||
63 | /* ftdi_usb_open return codes: | |
64 | 0: all fine | |
65 | -1: usb_find_busses() failed | |
66 | -2: usb_find_devices() failed | |
67 | -3: usb device not found | |
68 | -4: unable to open device | |
69 | -5: unable to claim device | |
70 | -6: reset failed | |
71 | -7: set baudrate failed | |
72 | */ | |
73 | int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) { | |
74 | struct usb_bus *bus; | |
75 | struct usb_device *dev; | |
76 | ||
77 | usb_init(); | |
78 | ||
79 | if (usb_find_busses() < 0) { | |
80 | ftdi->error_str = "usb_find_busses() failed"; | |
81 | return -1; | |
82 | } | |
83 | ||
84 | if (usb_find_devices() < 0) { | |
85 | ftdi->error_str = "usb_find_devices() failed"; | |
86 | return -2; | |
87 | } | |
88 | ||
89 | for (bus = usb_busses; bus; bus = bus->next) { | |
90 | for (dev = bus->devices; dev; dev = dev->next) { | |
91 | if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) { | |
92 | ftdi->usb_dev = usb_open(dev); | |
93 | if (ftdi->usb_dev) { | |
545820ce | 94 | if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) { |
a3da1d95 GE |
95 | ftdi->error_str = "unable to claim usb device. You can still use it though..."; |
96 | return -5; | |
97 | } | |
98 | ||
99 | if (ftdi_usb_reset (ftdi) != 0) | |
d9f0cce7 | 100 | return -6; |
a3da1d95 GE |
101 | |
102 | if (ftdi_set_baudrate (ftdi, 9600) != 0) | |
d9f0cce7 | 103 | return -7; |
a3da1d95 GE |
104 | |
105 | return 0; | |
106 | } else { | |
107 | ftdi->error_str = "usb_open() failed"; | |
108 | return -4; | |
109 | } | |
110 | } | |
111 | } | |
112 | ||
113 | } | |
114 | ||
115 | // device not found | |
116 | return -3; | |
117 | } | |
118 | ||
119 | ||
120 | int ftdi_usb_reset(struct ftdi_context *ftdi) { | |
545820ce | 121 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a3da1d95 GE |
122 | ftdi->error_str = "FTDI reset failed"; |
123 | return -1; | |
124 | } | |
545820ce | 125 | // Invalidate data in the readbuffer |
bfcee05b TJ |
126 | ftdi->readbuffer_offset = 0; |
127 | ftdi->readbuffer_remaining = 0; | |
128 | ||
a3da1d95 GE |
129 | return 0; |
130 | } | |
131 | ||
a60be878 | 132 | int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) { |
545820ce | 133 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a60be878 TJ |
134 | ftdi->error_str = "FTDI purge of RX buffer failed"; |
135 | return -1; | |
136 | } | |
545820ce | 137 | // Invalidate data in the readbuffer |
bfcee05b TJ |
138 | ftdi->readbuffer_offset = 0; |
139 | ftdi->readbuffer_remaining = 0; | |
a60be878 | 140 | |
545820ce | 141 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a60be878 TJ |
142 | ftdi->error_str = "FTDI purge of TX buffer failed"; |
143 | return -1; | |
144 | } | |
145 | ||
545820ce | 146 | |
a60be878 TJ |
147 | return 0; |
148 | } | |
a3da1d95 GE |
149 | |
150 | /* ftdi_usb_close return codes | |
151 | 0: all fine | |
152 | -1: usb_release failed | |
153 | -2: usb_close failed | |
154 | */ | |
155 | int ftdi_usb_close(struct ftdi_context *ftdi) { | |
156 | int rtn = 0; | |
157 | ||
545820ce | 158 | if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0) |
a3da1d95 GE |
159 | rtn = -1; |
160 | ||
161 | if (usb_close (ftdi->usb_dev) != 0) | |
162 | rtn = -2; | |
163 | ||
164 | return rtn; | |
165 | } | |
166 | ||
167 | ||
168 | /* | |
169 | ftdi_set_baudrate return codes: | |
170 | 0: all fine | |
171 | -1: invalid baudrate | |
172 | -2: setting baudrate failed | |
173 | */ | |
174 | int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) { | |
175 | unsigned short ftdi_baudrate; | |
176 | ||
177 | if (ftdi->bitbang_enabled) { | |
178 | baudrate = baudrate*4; | |
179 | } | |
180 | ||
181 | switch (baudrate) { | |
182 | case 300: | |
183 | ftdi_baudrate = 0x2710; | |
184 | break; | |
185 | case 600: | |
186 | ftdi_baudrate = 0x1388; | |
187 | break; | |
188 | case 1200: | |
189 | ftdi_baudrate = 0x09C4; | |
190 | break; | |
191 | case 2400: | |
192 | ftdi_baudrate = 0x04E2; | |
193 | break; | |
194 | case 4800: | |
195 | ftdi_baudrate = 0x0271; | |
196 | break; | |
197 | case 9600: | |
198 | ftdi_baudrate = 0x4138; | |
199 | break; | |
200 | case 19200: | |
201 | ftdi_baudrate = 0x809C; | |
202 | break; | |
203 | case 38400: | |
204 | ftdi_baudrate = 0xC04E; | |
205 | break; | |
206 | case 57600: | |
207 | ftdi_baudrate = 0x0034; | |
208 | break; | |
209 | case 115200: | |
210 | ftdi_baudrate = 0x001A; | |
211 | break; | |
212 | case 230400: | |
213 | ftdi_baudrate = 0x000D; | |
214 | break; | |
215 | case 460800: | |
216 | ftdi_baudrate = 0x4006; | |
217 | break; | |
218 | case 921600: | |
219 | ftdi_baudrate = 0x8003; | |
220 | break; | |
221 | default: | |
222 | ftdi->error_str = "Unknown baudrate. Note: bitbang baudrates are automatically multiplied by 4"; | |
223 | return -1; | |
224 | } | |
225 | ||
545820ce TJ |
226 | |
227 | if (usb_control_msg(ftdi->usb_dev, 0x40, 3, ftdi_baudrate, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { | |
a3da1d95 GE |
228 | ftdi->error_str = "Setting new baudrate failed"; |
229 | return -2; | |
230 | } | |
231 | ||
232 | ftdi->baudrate = baudrate; | |
233 | return 0; | |
234 | } | |
235 | ||
236 | ||
be5d7eec | 237 | int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) { |
a3da1d95 GE |
238 | int ret; |
239 | int offset = 0; | |
545820ce | 240 | int total_written = 0; |
a3da1d95 | 241 | while (offset < size) { |
948f9ada | 242 | int write_size = ftdi->writebuffer_chunksize; |
a3da1d95 GE |
243 | |
244 | if (offset+write_size > size) | |
245 | write_size = size-offset; | |
246 | ||
545820ce | 247 | ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout); |
cbabb7d3 | 248 | if (ret == -1) { |
d9f0cce7 | 249 | ftdi->error_str = "bulk write failed"; |
a3da1d95 | 250 | return -1; |
d9f0cce7 | 251 | } |
545820ce | 252 | total_written += ret; |
a3da1d95 GE |
253 | |
254 | offset += write_size; | |
255 | } | |
256 | ||
545820ce | 257 | return total_written; |
a3da1d95 GE |
258 | } |
259 | ||
260 | ||
948f9ada TJ |
261 | int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) { |
262 | ftdi->writebuffer_chunksize = chunksize; | |
263 | return 0; | |
264 | } | |
265 | ||
266 | ||
267 | int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) { | |
268 | *chunksize = ftdi->writebuffer_chunksize; | |
269 | return 0; | |
270 | } | |
cbabb7d3 | 271 | |
948f9ada TJ |
272 | |
273 | int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) { | |
274 | int offset = 0, ret = 1; | |
d9f0cce7 | 275 | |
948f9ada TJ |
276 | // everything we want is still in the readbuffer? |
277 | if (size <= ftdi->readbuffer_remaining) { | |
d9f0cce7 TJ |
278 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size); |
279 | ||
280 | // Fix offsets | |
281 | ftdi->readbuffer_remaining -= size; | |
282 | ftdi->readbuffer_offset += size; | |
283 | ||
545820ce | 284 | /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */ |
d9f0cce7 TJ |
285 | |
286 | return size; | |
979a145c | 287 | } |
948f9ada TJ |
288 | // something still in the readbuffer, but not enough to satisfy 'size'? |
289 | if (ftdi->readbuffer_remaining != 0) { | |
d9f0cce7 | 290 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining); |
979a145c | 291 | |
d9f0cce7 TJ |
292 | // Fix offset |
293 | offset += ftdi->readbuffer_remaining; | |
948f9ada | 294 | } |
948f9ada | 295 | // do the actual USB read |
cbabb7d3 | 296 | while (offset < size && ret > 0) { |
d9f0cce7 TJ |
297 | ftdi->readbuffer_remaining = 0; |
298 | ftdi->readbuffer_offset = 0; | |
545820ce TJ |
299 | /* returns how much received */ |
300 | ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout); | |
cbabb7d3 | 301 | |
d9f0cce7 TJ |
302 | if (ret == -1) { |
303 | ftdi->error_str = "bulk read failed"; | |
cbabb7d3 | 304 | return -1; |
d9f0cce7 TJ |
305 | } |
306 | ||
307 | if (ret > 2) { | |
308 | // skip FTDI status bytes. | |
309 | // Maybe stored in the future to enable modem use | |
310 | ftdi->readbuffer_offset += 2; | |
311 | ret -= 2; | |
312 | } else if (ret <= 2) { | |
313 | // no more data to read? | |
314 | return offset; | |
315 | } | |
d9f0cce7 TJ |
316 | if (ret > 0) { |
317 | // data still fits in buf? | |
318 | if (offset+ret <= size) { | |
319 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret); | |
545820ce | 320 | //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]); |
d9f0cce7 TJ |
321 | offset += ret; |
322 | ||
545820ce | 323 | /* Did we read exactly the right amount of bytes? */ |
d9f0cce7 TJ |
324 | if (offset == size) |
325 | return offset; | |
326 | } else { | |
327 | // only copy part of the data or size <= readbuffer_chunksize | |
328 | int part_size = size-offset; | |
329 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size); | |
330 | ||
331 | ftdi->readbuffer_offset += part_size; | |
332 | ftdi->readbuffer_remaining = ret-part_size; | |
333 | offset += part_size; | |
334 | ||
545820ce TJ |
335 | /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n", |
336 | part_size, size, offset, ret, ftdi->readbuffer_remaining); */ | |
d9f0cce7 TJ |
337 | |
338 | return offset; | |
339 | } | |
340 | } | |
cbabb7d3 | 341 | } |
948f9ada TJ |
342 | // never reached |
343 | return -2; | |
a3da1d95 GE |
344 | } |
345 | ||
346 | ||
948f9ada TJ |
347 | int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) { |
348 | // Invalidate all remaining data | |
349 | ftdi->readbuffer_offset = 0; | |
350 | ftdi->readbuffer_remaining = 0; | |
351 | ||
352 | unsigned char *new_buf; | |
353 | if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) { | |
d9f0cce7 TJ |
354 | ftdi->error_str = "out of memory for readbuffer"; |
355 | return -1; | |
948f9ada | 356 | } |
d9f0cce7 | 357 | |
948f9ada TJ |
358 | ftdi->readbuffer = new_buf; |
359 | ftdi->readbuffer_chunksize = chunksize; | |
360 | ||
361 | return 0; | |
362 | } | |
363 | ||
364 | ||
365 | int ftdi_readt_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) { | |
366 | *chunksize = ftdi->readbuffer_chunksize; | |
367 | return 0; | |
368 | } | |
369 | ||
370 | ||
371 | ||
a3da1d95 GE |
372 | int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) { |
373 | unsigned short usb_val; | |
374 | ||
d9f0cce7 | 375 | usb_val = bitmask; // low byte: bitmask |
545820ce TJ |
376 | usb_val |= (1 << 8); // high byte: enable flag |
377 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { | |
a3da1d95 GE |
378 | ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?"; |
379 | return -1; | |
380 | } | |
a3da1d95 GE |
381 | ftdi->bitbang_enabled = 1; |
382 | return 0; | |
383 | } | |
384 | ||
385 | ||
386 | int ftdi_disable_bitbang(struct ftdi_context *ftdi) { | |
545820ce | 387 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a3da1d95 GE |
388 | ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?"; |
389 | return -1; | |
390 | } | |
391 | ||
392 | ftdi->bitbang_enabled = 0; | |
393 | return 0; | |
394 | } | |
395 | ||
396 | ||
397 | int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) { | |
398 | unsigned short usb_val; | |
545820ce | 399 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) { |
a3da1d95 GE |
400 | ftdi->error_str = "Read pins failed"; |
401 | return -1; | |
402 | } | |
403 | ||
404 | *pins = (unsigned char)usb_val; | |
405 | return 0; | |
406 | } | |
407 | ||
408 | ||
409 | int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) { | |
410 | unsigned short usb_val; | |
411 | ||
412 | if (latency < 1) { | |
d9f0cce7 TJ |
413 | ftdi->error_str = "Latency out of range. Only valid for 1-255"; |
414 | return -1; | |
a3da1d95 GE |
415 | } |
416 | ||
d79d2e68 | 417 | usb_val = latency; |
545820ce | 418 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
d9f0cce7 TJ |
419 | ftdi->error_str = "Unable to set latency timer"; |
420 | return -2; | |
a3da1d95 GE |
421 | } |
422 | return 0; | |
423 | } | |
424 | ||
425 | ||
426 | int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) { | |
427 | unsigned short usb_val; | |
545820ce | 428 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) { |
a3da1d95 GE |
429 | ftdi->error_str = "Reading latency timer failed"; |
430 | return -1; | |
431 | } | |
432 | ||
433 | *latency = (unsigned char)usb_val; | |
434 | return 0; | |
435 | } | |
436 | ||
437 | ||
b8aa7b35 TJ |
438 | void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) { |
439 | eeprom->vendor_id = 0403; | |
440 | eeprom->product_id = 6001; | |
d9f0cce7 | 441 | |
b8aa7b35 TJ |
442 | eeprom->self_powered = 1; |
443 | eeprom->remote_wakeup = 1; | |
444 | eeprom->BM_type_chip = 1; | |
d9f0cce7 | 445 | |
b8aa7b35 TJ |
446 | eeprom->in_is_isochronous = 0; |
447 | eeprom->out_is_isochronous = 0; | |
448 | eeprom->suspend_pull_downs = 0; | |
d9f0cce7 | 449 | |
b8aa7b35 TJ |
450 | eeprom->use_serial = 0; |
451 | eeprom->change_usb_version = 0; | |
452 | eeprom->usb_version = 200; | |
453 | eeprom->max_power = 0; | |
d9f0cce7 | 454 | |
b8aa7b35 TJ |
455 | eeprom->manufacturer = NULL; |
456 | eeprom->product = NULL; | |
457 | eeprom->serial = NULL; | |
458 | } | |
459 | ||
460 | ||
461 | /* | |
462 | ftdi_eeprom_build return codes: | |
8ed61121 | 463 | positive value: used eeprom size |
b8aa7b35 TJ |
464 | -1: eeprom size (128 bytes) exceeded by custom strings |
465 | */ | |
466 | int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) { | |
467 | unsigned char i, j; | |
468 | unsigned short checksum, value; | |
469 | unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0; | |
470 | int size_check; | |
471 | ||
472 | if (eeprom->manufacturer != NULL) | |
d9f0cce7 | 473 | manufacturer_size = strlen(eeprom->manufacturer); |
b8aa7b35 | 474 | if (eeprom->product != NULL) |
d9f0cce7 | 475 | product_size = strlen(eeprom->product); |
b8aa7b35 | 476 | if (eeprom->serial != NULL) |
d9f0cce7 | 477 | serial_size = strlen(eeprom->serial); |
b8aa7b35 | 478 | |
d9f0cce7 TJ |
479 | size_check = 128; // eeprom is 128 bytes |
480 | size_check -= 28; // 28 are always in use (fixed) | |
b8aa7b35 TJ |
481 | size_check -= manufacturer_size*2; |
482 | size_check -= product_size*2; | |
483 | size_check -= serial_size*2; | |
484 | ||
485 | // eeprom size exceeded? | |
486 | if (size_check < 0) | |
d9f0cce7 | 487 | return (-1); |
b8aa7b35 TJ |
488 | |
489 | // empty eeprom | |
490 | memset (output, 0, 128); | |
491 | ||
492 | // Addr 00: Stay 00 00 | |
493 | // Addr 02: Vendor ID | |
494 | output[0x02] = eeprom->vendor_id; | |
495 | output[0x03] = eeprom->vendor_id >> 8; | |
496 | ||
497 | // Addr 04: Product ID | |
498 | output[0x04] = eeprom->product_id; | |
499 | output[0x05] = eeprom->product_id >> 8; | |
500 | ||
501 | // Addr 06: Device release number (0400h for BM features) | |
502 | output[0x06] = 0x00; | |
d9f0cce7 | 503 | |
b8aa7b35 | 504 | if (eeprom->BM_type_chip == 1) |
d9f0cce7 | 505 | output[0x07] = 0x04; |
b8aa7b35 | 506 | else |
d9f0cce7 | 507 | output[0x07] = 0x02; |
b8aa7b35 TJ |
508 | |
509 | // Addr 08: Config descriptor | |
510 | // Bit 1: remote wakeup if 1 | |
511 | // Bit 0: self powered if 1 | |
512 | // | |
513 | j = 0; | |
514 | if (eeprom->self_powered == 1) | |
d9f0cce7 | 515 | j = j | 1; |
b8aa7b35 | 516 | if (eeprom->remote_wakeup == 1) |
d9f0cce7 | 517 | j = j | 2; |
b8aa7b35 TJ |
518 | output[0x08] = j; |
519 | ||
520 | // Addr 09: Max power consumption: max power = value * 2 mA | |
d9f0cce7 TJ |
521 | output[0x09] = eeprom->max_power; |
522 | ; | |
523 | ||
b8aa7b35 TJ |
524 | // Addr 0A: Chip configuration |
525 | // Bit 7: 0 - reserved | |
526 | // Bit 6: 0 - reserved | |
527 | // Bit 5: 0 - reserved | |
528 | // Bit 4: 1 - Change USB version | |
529 | // Bit 3: 1 - Use the serial number string | |
530 | // Bit 2: 1 - Enable suspend pull downs for lower power | |
531 | // Bit 1: 1 - Out EndPoint is Isochronous | |
532 | // Bit 0: 1 - In EndPoint is Isochronous | |
533 | // | |
534 | j = 0; | |
535 | if (eeprom->in_is_isochronous == 1) | |
d9f0cce7 | 536 | j = j | 1; |
b8aa7b35 | 537 | if (eeprom->out_is_isochronous == 1) |
d9f0cce7 | 538 | j = j | 2; |
b8aa7b35 | 539 | if (eeprom->suspend_pull_downs == 1) |
d9f0cce7 | 540 | j = j | 4; |
b8aa7b35 | 541 | if (eeprom->use_serial == 1) |
d9f0cce7 | 542 | j = j | 8; |
b8aa7b35 | 543 | if (eeprom->change_usb_version == 1) |
d9f0cce7 | 544 | j = j | 16; |
b8aa7b35 | 545 | output[0x0A] = j; |
d9f0cce7 | 546 | |
b8aa7b35 TJ |
547 | // Addr 0B: reserved |
548 | output[0x0B] = 0x00; | |
d9f0cce7 | 549 | |
b8aa7b35 TJ |
550 | // Addr 0C: USB version low byte when 0x0A bit 4 is set |
551 | // Addr 0D: USB version high byte when 0x0A bit 4 is set | |
552 | if (eeprom->change_usb_version == 1) { | |
553 | output[0x0C] = eeprom->usb_version; | |
d9f0cce7 | 554 | output[0x0D] = eeprom->usb_version >> 8; |
b8aa7b35 TJ |
555 | } |
556 | ||
557 | ||
558 | // Addr 0E: Offset of the manufacturer string + 0x80 | |
559 | output[0x0E] = 0x14 + 0x80; | |
560 | ||
561 | // Addr 0F: Length of manufacturer string | |
562 | output[0x0F] = manufacturer_size*2 + 2; | |
563 | ||
564 | // Addr 10: Offset of the product string + 0x80, calculated later | |
565 | // Addr 11: Length of product string | |
566 | output[0x11] = product_size*2 + 2; | |
567 | ||
568 | // Addr 12: Offset of the serial string + 0x80, calculated later | |
569 | // Addr 13: Length of serial string | |
570 | output[0x13] = serial_size*2 + 2; | |
571 | ||
572 | // Dynamic content | |
a862ddcf | 573 | output[0x14] = manufacturer_size*2 + 2; |
d9f0cce7 TJ |
574 | output[0x15] = 0x03; // type: string |
575 | ||
b8aa7b35 | 576 | i = 0x16, j = 0; |
d9f0cce7 | 577 | |
b8aa7b35 TJ |
578 | // Output manufacturer |
579 | for (j = 0; j < manufacturer_size; j++) { | |
d9f0cce7 TJ |
580 | output[i] = eeprom->manufacturer[j], i++; |
581 | output[i] = 0x00, i++; | |
b8aa7b35 TJ |
582 | } |
583 | ||
584 | // Output product name | |
d9f0cce7 | 585 | output[0x10] = i + 0x80; // calculate offset |
b8aa7b35 TJ |
586 | output[i] = product_size*2 + 2, i++; |
587 | output[i] = 0x03, i++; | |
588 | for (j = 0; j < product_size; j++) { | |
d9f0cce7 TJ |
589 | output[i] = eeprom->product[j], i++; |
590 | output[i] = 0x00, i++; | |
b8aa7b35 | 591 | } |
d9f0cce7 | 592 | |
b8aa7b35 | 593 | // Output serial |
d9f0cce7 | 594 | output[0x12] = i + 0x80; // calculate offset |
b8aa7b35 TJ |
595 | output[i] = serial_size*2 + 2, i++; |
596 | output[i] = 0x03, i++; | |
597 | for (j = 0; j < serial_size; j++) { | |
d9f0cce7 TJ |
598 | output[i] = eeprom->serial[j], i++; |
599 | output[i] = 0x00, i++; | |
b8aa7b35 TJ |
600 | } |
601 | ||
602 | // calculate checksum | |
603 | checksum = 0xAAAA; | |
d9f0cce7 | 604 | |
b8aa7b35 | 605 | for (i = 0; i < 63; i++) { |
d9f0cce7 TJ |
606 | value = output[i*2]; |
607 | value += output[(i*2)+1] << 8; | |
b8aa7b35 | 608 | |
d9f0cce7 TJ |
609 | checksum = value^checksum; |
610 | checksum = (checksum << 1) | (checksum >> 15); | |
b8aa7b35 TJ |
611 | } |
612 | ||
613 | output[0x7E] = checksum; | |
d9f0cce7 | 614 | output[0x7F] = checksum >> 8; |
b8aa7b35 | 615 | |
8ed61121 | 616 | return size_check; |
b8aa7b35 TJ |
617 | } |
618 | ||
619 | ||
be5d7eec | 620 | int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) { |
a3da1d95 GE |
621 | int i; |
622 | ||
623 | for (i = 0; i < 64; i++) { | |
545820ce | 624 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) { |
d9f0cce7 TJ |
625 | ftdi->error_str = "Reading eeprom failed"; |
626 | return -1; | |
a3da1d95 GE |
627 | } |
628 | } | |
629 | ||
630 | return 0; | |
631 | } | |
632 | ||
633 | ||
be5d7eec | 634 | int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) { |
a3da1d95 GE |
635 | unsigned short usb_val; |
636 | int i; | |
637 | ||
638 | for (i = 0; i < 64; i++) { | |
d9f0cce7 TJ |
639 | usb_val = eeprom[i*2]; |
640 | usb_val += eeprom[(i*2)+1] << 8; | |
545820ce | 641 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) { |
d9f0cce7 TJ |
642 | ftdi->error_str = "Unable to write eeprom"; |
643 | return -1; | |
644 | } | |
a3da1d95 GE |
645 | } |
646 | ||
647 | return 0; | |
648 | } | |
649 | ||
650 | ||
651 | int ftdi_erase_eeprom(struct ftdi_context *ftdi) { | |
545820ce | 652 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a3da1d95 GE |
653 | ftdi->error_str = "Unable to erase eeprom"; |
654 | return -1; | |
655 | } | |
656 | ||
657 | return 0; | |
658 | } |