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