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