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