libftdi: (tomj) applied patch to extend chip reset
[libftdi] / src / ftdi.c
... / ...
CommitLineData
1/***************************************************************************
2 ftdi.c - description
3 -------------------
4 begin : Fri Apr 4 2003
5 copyright : (C) 2003 by Intra2net AG
6 email : opensource@intra2net.com
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>
18#include <string.h>
19
20#include "ftdi.h"
21
22/* ftdi_init return codes:
23 0: all fine
24 -1: couldn't allocate read buffer
25*/
26int ftdi_init(struct ftdi_context *ftdi)
27{
28 ftdi->usb_dev = NULL;
29 ftdi->usb_read_timeout = 5000;
30 ftdi->usb_write_timeout = 5000;
31
32 ftdi->type = TYPE_BM; /* chip type */
33 ftdi->baudrate = -1;
34 ftdi->bitbang_enabled = 0;
35
36 ftdi->readbuffer = NULL;
37 ftdi->readbuffer_offset = 0;
38 ftdi->readbuffer_remaining = 0;
39 ftdi->writebuffer_chunksize = 4096;
40
41 ftdi->interface = 0;
42 ftdi->index = 0;
43 ftdi->in_ep = 0x02;
44 ftdi->out_ep = 0x81;
45 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
46
47 ftdi->error_str = NULL;
48
49 /* All fine. Now allocate the readbuffer */
50 return ftdi_read_data_set_chunksize(ftdi, 4096);
51}
52
53
54void ftdi_deinit(struct ftdi_context *ftdi)
55{
56 if (ftdi->readbuffer != NULL) {
57 free(ftdi->readbuffer);
58 ftdi->readbuffer = NULL;
59 }
60}
61
62
63void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
64{
65 ftdi->usb_dev = usb;
66}
67
68
69/* ftdi_usb_open return codes:
70 0: all fine
71 -1: usb_find_busses() failed
72 -2: usb_find_devices() failed
73 -3: usb device not found
74 -4: unable to open device
75 -5: unable to claim device
76 -6: reset failed
77 -7: set baudrate failed
78 -8: get product description failed
79 -9: get serial number failed
80 -10: unable to close device
81*/
82int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
83{
84 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
85}
86
87int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
88 const char* description, const char* serial)
89{
90 struct usb_bus *bus;
91 struct usb_device *dev;
92
93 usb_init();
94
95 if (usb_find_busses() < 0) {
96 ftdi->error_str = "usb_find_busses() failed";
97 return -1;
98 }
99
100 if (usb_find_devices() < 0) {
101 ftdi->error_str = "usb_find_devices() failed";
102 return -2;
103 }
104
105 for (bus = usb_busses; bus; bus = bus->next) {
106 for (dev = bus->devices; dev; dev = dev->next) {
107 if (dev->descriptor.idVendor == vendor
108 && dev->descriptor.idProduct == product) {
109 if (!(ftdi->usb_dev = usb_open(dev))) {
110 ftdi->error_str = "usb_open() failed";
111 return -4;
112 }
113
114 char string[256];
115 if (description != NULL) {
116 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
117 ftdi->error_str = "unable to fetch product description\n";
118 if (usb_close (ftdi->usb_dev) != 0)
119 return -10;
120 return -8;
121 }
122 if (strncmp(string, description, sizeof(string)) != 0) {
123 ftdi->error_str = "product description not matching\n";
124 if (usb_close (ftdi->usb_dev) != 0)
125 return -10;
126 continue;
127 }
128 }
129 if (serial != NULL) {
130 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
131 ftdi->error_str = "unable to fetch serial number\n";
132 if (usb_close (ftdi->usb_dev) != 0)
133 return -10;
134 return -9;
135 }
136 if (strncmp(string, serial, sizeof(string)) != 0) {
137 ftdi->error_str = "serial number not matching\n";
138 if (usb_close (ftdi->usb_dev) != 0)
139 return -10;
140 continue;
141 }
142 }
143
144 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
145 ftdi->error_str = "unable to claim usb device. Make sure ftdi_sio is unloaded!";
146 if (usb_close (ftdi->usb_dev) != 0)
147 return -10;
148 return -5;
149 }
150
151 if (ftdi_usb_reset (ftdi) != 0) {
152 if (usb_close (ftdi->usb_dev) != 0)
153 return -10;
154 return -6;
155 }
156
157 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
158 if (usb_close (ftdi->usb_dev) != 0)
159 return -10;
160 return -7;
161 }
162
163 // Try to guess chip type
164 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
165 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
166 && dev->descriptor.iSerialNumber == 0))
167 ftdi->type = TYPE_BM;
168 else if (dev->descriptor.bcdDevice == 0x200)
169 ftdi->type = TYPE_AM;
170 else if (dev->descriptor.bcdDevice == 0x500)
171 ftdi->type = TYPE_2232C;
172
173 return 0;
174 }
175 }
176 }
177
178 // device not found
179 return -3;
180}
181
182
183int ftdi_usb_reset(struct ftdi_context *ftdi)
184{
185 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
186 ftdi->error_str = "FTDI reset failed";
187 return -1;
188 }
189
190 if (usb_reset(ftdi->usb_dev) != 0) {
191 ftdi->error_str = "USB reset failed";
192 return -2;
193 }
194 // Invalidate data in the readbuffer
195 ftdi->readbuffer_offset = 0;
196 ftdi->readbuffer_remaining = 0;
197
198 return 0;
199}
200
201int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
202{
203 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
204 ftdi->error_str = "FTDI purge of RX buffer failed";
205 return -1;
206 }
207 // Invalidate data in the readbuffer
208 ftdi->readbuffer_offset = 0;
209 ftdi->readbuffer_remaining = 0;
210
211 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
212 ftdi->error_str = "FTDI purge of TX buffer failed";
213 return -1;
214 }
215
216
217 return 0;
218}
219
220/* ftdi_usb_close return codes
221 0: all fine
222 -1: usb_release failed
223 -2: usb_close failed
224*/
225int ftdi_usb_close(struct ftdi_context *ftdi)
226{
227 int rtn = 0;
228
229 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
230 rtn = -1;
231
232 if (usb_close (ftdi->usb_dev) != 0)
233 rtn = -2;
234
235 return rtn;
236}
237
238
239/*
240 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
241 Function is only used internally
242*/
243static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
244 unsigned short *value, unsigned short *index)
245{
246 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
247 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
248 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
249 int divisor, best_divisor, best_baud, best_baud_diff;
250 unsigned long encoded_divisor;
251 int i;
252
253 if (baudrate <= 0) {
254 // Return error
255 return -1;
256 }
257
258 divisor = 24000000 / baudrate;
259
260 if (ftdi->type == TYPE_AM) {
261 // Round down to supported fraction (AM only)
262 divisor -= am_adjust_dn[divisor & 7];
263 }
264
265 // Try this divisor and the one above it (because division rounds down)
266 best_divisor = 0;
267 best_baud = 0;
268 best_baud_diff = 0;
269 for (i = 0; i < 2; i++) {
270 int try_divisor = divisor + i;
271 int baud_estimate;
272 int baud_diff;
273
274 // Round up to supported divisor value
275 if (try_divisor < 8) {
276 // Round up to minimum supported divisor
277 try_divisor = 8;
278 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
279 // BM doesn't support divisors 9 through 11 inclusive
280 try_divisor = 12;
281 } else if (divisor < 16) {
282 // AM doesn't support divisors 9 through 15 inclusive
283 try_divisor = 16;
284 } else {
285 if (ftdi->type == TYPE_AM) {
286 // Round up to supported fraction (AM only)
287 try_divisor += am_adjust_up[try_divisor & 7];
288 if (try_divisor > 0x1FFF8) {
289 // Round down to maximum supported divisor value (for AM)
290 try_divisor = 0x1FFF8;
291 }
292 } else {
293 if (try_divisor > 0x1FFFF) {
294 // Round down to maximum supported divisor value (for BM)
295 try_divisor = 0x1FFFF;
296 }
297 }
298 }
299 // Get estimated baud rate (to nearest integer)
300 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
301 // Get absolute difference from requested baud rate
302 if (baud_estimate < baudrate) {
303 baud_diff = baudrate - baud_estimate;
304 } else {
305 baud_diff = baud_estimate - baudrate;
306 }
307 if (i == 0 || baud_diff < best_baud_diff) {
308 // Closest to requested baud rate so far
309 best_divisor = try_divisor;
310 best_baud = baud_estimate;
311 best_baud_diff = baud_diff;
312 if (baud_diff == 0) {
313 // Spot on! No point trying
314 break;
315 }
316 }
317 }
318 // Encode the best divisor value
319 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
320 // Deal with special cases for encoded value
321 if (encoded_divisor == 1) {
322 encoded_divisor = 0; // 3000000 baud
323 } else if (encoded_divisor == 0x4001) {
324 encoded_divisor = 1; // 2000000 baud (BM only)
325 }
326 // Split into "value" and "index" values
327 *value = (unsigned short)(encoded_divisor & 0xFFFF);
328 if(ftdi->type == TYPE_2232C) {
329 *index = (unsigned short)(encoded_divisor >> 8);
330 *index &= 0xFF00;
331 *index |= ftdi->interface;
332 }
333 else
334 *index = (unsigned short)(encoded_divisor >> 16);
335
336 // Return the nearest baud rate
337 return best_baud;
338}
339
340/*
341 ftdi_set_baudrate return codes:
342 0: all fine
343 -1: invalid baudrate
344 -2: setting baudrate failed
345*/
346int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
347{
348 unsigned short value, index;
349 int actual_baudrate;
350
351 if (ftdi->bitbang_enabled) {
352 baudrate = baudrate*4;
353 }
354
355 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
356 if (actual_baudrate <= 0) {
357 ftdi->error_str = "Silly baudrate <= 0.";
358 return -1;
359 }
360
361 // Check within tolerance (about 5%)
362 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
363 || ((actual_baudrate < baudrate)
364 ? (actual_baudrate * 21 < baudrate * 20)
365 : (baudrate * 21 < actual_baudrate * 20))) {
366 ftdi->error_str = "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4";
367 return -1;
368 }
369
370 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0) {
371 ftdi->error_str = "Setting new baudrate failed";
372 return -2;
373 }
374
375 ftdi->baudrate = baudrate;
376 return 0;
377}
378
379
380int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
381{
382 int ret;
383 int offset = 0;
384 int total_written = 0;
385 while (offset < size) {
386 int write_size = ftdi->writebuffer_chunksize;
387
388 if (offset+write_size > size)
389 write_size = size-offset;
390
391 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
392 if (ret < 0) {
393 if (ret == -1)
394 ftdi->error_str = "bulk write failed";
395 else
396 ftdi->error_str = "usb failed";
397 return ret;
398 }
399 total_written += ret;
400
401 offset += write_size;
402 }
403
404 return total_written;
405}
406
407
408int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
409{
410 ftdi->writebuffer_chunksize = chunksize;
411 return 0;
412}
413
414
415int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
416{
417 *chunksize = ftdi->writebuffer_chunksize;
418 return 0;
419}
420
421
422int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
423{
424 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
425
426 // everything we want is still in the readbuffer?
427 if (size <= ftdi->readbuffer_remaining) {
428 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
429
430 // Fix offsets
431 ftdi->readbuffer_remaining -= size;
432 ftdi->readbuffer_offset += size;
433
434 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
435
436 return size;
437 }
438 // something still in the readbuffer, but not enough to satisfy 'size'?
439 if (ftdi->readbuffer_remaining != 0) {
440 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
441
442 // Fix offset
443 offset += ftdi->readbuffer_remaining;
444 }
445 // do the actual USB read
446 while (offset < size && ret > 0) {
447 ftdi->readbuffer_remaining = 0;
448 ftdi->readbuffer_offset = 0;
449 /* returns how much received */
450 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
451
452 if (ret < 0) {
453 if (ret == -1)
454 ftdi->error_str = "bulk read failed";
455 else
456 ftdi->error_str = "usb failed";
457 return ret;
458 }
459
460 if (ret > 2) {
461 // skip FTDI status bytes.
462 // Maybe stored in the future to enable modem use
463 num_of_chunks = ret / 64;
464 chunk_remains = ret % 64;
465 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
466
467 ftdi->readbuffer_offset += 2;
468 ret -= 2;
469
470 if (ret > 64) {
471 for (i = 1; i < num_of_chunks; i++)
472 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
473 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
474 62);
475 if (chunk_remains > 2) {
476 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
477 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
478 chunk_remains-2);
479 ret -= 2*num_of_chunks;
480 } else
481 ret -= 2*(num_of_chunks-1)+chunk_remains;
482 }
483 } else if (ret <= 2) {
484 // no more data to read?
485 return offset;
486 }
487 if (ret > 0) {
488 // data still fits in buf?
489 if (offset+ret <= size) {
490 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
491 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
492 offset += ret;
493
494 /* Did we read exactly the right amount of bytes? */
495 if (offset == size)
496 return offset;
497 } else {
498 // only copy part of the data or size <= readbuffer_chunksize
499 int part_size = size-offset;
500 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
501
502 ftdi->readbuffer_offset += part_size;
503 ftdi->readbuffer_remaining = ret-part_size;
504 offset += part_size;
505
506 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
507 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
508
509 return offset;
510 }
511 }
512 }
513 // never reached
514 return -127;
515}
516
517
518int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
519{
520 unsigned char *new_buf;
521
522 // Invalidate all remaining data
523 ftdi->readbuffer_offset = 0;
524 ftdi->readbuffer_remaining = 0;
525
526 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) {
527 ftdi->error_str = "out of memory for readbuffer";
528 return -1;
529 }
530
531 ftdi->readbuffer = new_buf;
532 ftdi->readbuffer_chunksize = chunksize;
533
534 return 0;
535}
536
537
538int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
539{
540 *chunksize = ftdi->readbuffer_chunksize;
541 return 0;
542}
543
544
545
546int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
547{
548 unsigned short usb_val;
549
550 usb_val = bitmask; // low byte: bitmask
551 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
552 usb_val |= (ftdi->bitbang_mode << 8);
553
554 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
555 ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?";
556 return -1;
557 }
558 ftdi->bitbang_enabled = 1;
559 return 0;
560}
561
562
563int ftdi_disable_bitbang(struct ftdi_context *ftdi)
564{
565 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
566 ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?";
567 return -1;
568 }
569
570 ftdi->bitbang_enabled = 0;
571 return 0;
572}
573
574
575int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
576{
577 unsigned short usb_val;
578 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) {
579 ftdi->error_str = "Read pins failed";
580 return -1;
581 }
582
583 *pins = (unsigned char)usb_val;
584 return 0;
585}
586
587
588int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
589{
590 unsigned short usb_val;
591
592 if (latency < 1) {
593 ftdi->error_str = "Latency out of range. Only valid for 1-255";
594 return -1;
595 }
596
597 usb_val = latency;
598 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
599 ftdi->error_str = "Unable to set latency timer";
600 return -2;
601 }
602 return 0;
603}
604
605
606int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
607{
608 unsigned short usb_val;
609 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) {
610 ftdi->error_str = "Reading latency timer failed";
611 return -1;
612 }
613
614 *latency = (unsigned char)usb_val;
615 return 0;
616}
617
618
619void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
620{
621 eeprom->vendor_id = 0x0403;
622 eeprom->product_id = 0x6001;
623
624 eeprom->self_powered = 1;
625 eeprom->remote_wakeup = 1;
626 eeprom->BM_type_chip = 1;
627
628 eeprom->in_is_isochronous = 0;
629 eeprom->out_is_isochronous = 0;
630 eeprom->suspend_pull_downs = 0;
631
632 eeprom->use_serial = 0;
633 eeprom->change_usb_version = 0;
634 eeprom->usb_version = 0x0200;
635 eeprom->max_power = 0;
636
637 eeprom->manufacturer = NULL;
638 eeprom->product = NULL;
639 eeprom->serial = NULL;
640}
641
642
643/*
644 ftdi_eeprom_build return codes:
645 positive value: used eeprom size
646 -1: eeprom size (128 bytes) exceeded by custom strings
647*/
648int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
649{
650 unsigned char i, j;
651 unsigned short checksum, value;
652 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
653 int size_check;
654
655 if (eeprom->manufacturer != NULL)
656 manufacturer_size = strlen(eeprom->manufacturer);
657 if (eeprom->product != NULL)
658 product_size = strlen(eeprom->product);
659 if (eeprom->serial != NULL)
660 serial_size = strlen(eeprom->serial);
661
662 size_check = 128; // eeprom is 128 bytes
663 size_check -= 28; // 28 are always in use (fixed)
664 size_check -= manufacturer_size*2;
665 size_check -= product_size*2;
666 size_check -= serial_size*2;
667
668 // eeprom size exceeded?
669 if (size_check < 0)
670 return (-1);
671
672 // empty eeprom
673 memset (output, 0, 128);
674
675 // Addr 00: Stay 00 00
676 // Addr 02: Vendor ID
677 output[0x02] = eeprom->vendor_id;
678 output[0x03] = eeprom->vendor_id >> 8;
679
680 // Addr 04: Product ID
681 output[0x04] = eeprom->product_id;
682 output[0x05] = eeprom->product_id >> 8;
683
684 // Addr 06: Device release number (0400h for BM features)
685 output[0x06] = 0x00;
686
687 if (eeprom->BM_type_chip == 1)
688 output[0x07] = 0x04;
689 else
690 output[0x07] = 0x02;
691
692 // Addr 08: Config descriptor
693 // Bit 1: remote wakeup if 1
694 // Bit 0: self powered if 1
695 //
696 j = 0;
697 if (eeprom->self_powered == 1)
698 j = j | 1;
699 if (eeprom->remote_wakeup == 1)
700 j = j | 2;
701 output[0x08] = j;
702
703 // Addr 09: Max power consumption: max power = value * 2 mA
704 output[0x09] = eeprom->max_power;
705 ;
706
707 // Addr 0A: Chip configuration
708 // Bit 7: 0 - reserved
709 // Bit 6: 0 - reserved
710 // Bit 5: 0 - reserved
711 // Bit 4: 1 - Change USB version
712 // Bit 3: 1 - Use the serial number string
713 // Bit 2: 1 - Enable suspend pull downs for lower power
714 // Bit 1: 1 - Out EndPoint is Isochronous
715 // Bit 0: 1 - In EndPoint is Isochronous
716 //
717 j = 0;
718 if (eeprom->in_is_isochronous == 1)
719 j = j | 1;
720 if (eeprom->out_is_isochronous == 1)
721 j = j | 2;
722 if (eeprom->suspend_pull_downs == 1)
723 j = j | 4;
724 if (eeprom->use_serial == 1)
725 j = j | 8;
726 if (eeprom->change_usb_version == 1)
727 j = j | 16;
728 output[0x0A] = j;
729
730 // Addr 0B: reserved
731 output[0x0B] = 0x00;
732
733 // Addr 0C: USB version low byte when 0x0A bit 4 is set
734 // Addr 0D: USB version high byte when 0x0A bit 4 is set
735 if (eeprom->change_usb_version == 1) {
736 output[0x0C] = eeprom->usb_version;
737 output[0x0D] = eeprom->usb_version >> 8;
738 }
739
740
741 // Addr 0E: Offset of the manufacturer string + 0x80
742 output[0x0E] = 0x14 + 0x80;
743
744 // Addr 0F: Length of manufacturer string
745 output[0x0F] = manufacturer_size*2 + 2;
746
747 // Addr 10: Offset of the product string + 0x80, calculated later
748 // Addr 11: Length of product string
749 output[0x11] = product_size*2 + 2;
750
751 // Addr 12: Offset of the serial string + 0x80, calculated later
752 // Addr 13: Length of serial string
753 output[0x13] = serial_size*2 + 2;
754
755 // Dynamic content
756 output[0x14] = manufacturer_size*2 + 2;
757 output[0x15] = 0x03; // type: string
758
759 i = 0x16, j = 0;
760
761 // Output manufacturer
762 for (j = 0; j < manufacturer_size; j++) {
763 output[i] = eeprom->manufacturer[j], i++;
764 output[i] = 0x00, i++;
765 }
766
767 // Output product name
768 output[0x10] = i + 0x80; // calculate offset
769 output[i] = product_size*2 + 2, i++;
770 output[i] = 0x03, i++;
771 for (j = 0; j < product_size; j++) {
772 output[i] = eeprom->product[j], i++;
773 output[i] = 0x00, i++;
774 }
775
776 // Output serial
777 output[0x12] = i + 0x80; // calculate offset
778 output[i] = serial_size*2 + 2, i++;
779 output[i] = 0x03, i++;
780 for (j = 0; j < serial_size; j++) {
781 output[i] = eeprom->serial[j], i++;
782 output[i] = 0x00, i++;
783 }
784
785 // calculate checksum
786 checksum = 0xAAAA;
787
788 for (i = 0; i < 63; i++) {
789 value = output[i*2];
790 value += output[(i*2)+1] << 8;
791
792 checksum = value^checksum;
793 checksum = (checksum << 1) | (checksum >> 15);
794 }
795
796 output[0x7E] = checksum;
797 output[0x7F] = checksum >> 8;
798
799 return size_check;
800}
801
802
803int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
804{
805 int i;
806
807 for (i = 0; i < 64; i++) {
808 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) {
809 ftdi->error_str = "Reading eeprom failed";
810 return -1;
811 }
812 }
813
814 return 0;
815}
816
817
818int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
819{
820 unsigned short usb_val;
821 int i;
822
823 for (i = 0; i < 64; i++) {
824 usb_val = eeprom[i*2];
825 usb_val += eeprom[(i*2)+1] << 8;
826 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) {
827 ftdi->error_str = "Unable to write eeprom";
828 return -1;
829 }
830 }
831
832 return 0;
833}
834
835
836int ftdi_erase_eeprom(struct ftdi_context *ftdi)
837{
838 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) {
839 ftdi->error_str = "Unable to erase eeprom";
840 return -1;
841 }
842
843 return 0;
844}