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