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