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