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