1 /***************************************************************************
5 copyright : (C) 2003 by Intra2net AG
6 email : opensource@intra2net.com
7 ***************************************************************************/
9 /***************************************************************************
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; *
15 ***************************************************************************/
22 #define ftdi_error_return(code, str) do { \
23 ftdi->error_str = str; \
30 Initializes a ftdi_context.
34 -1: Couldn't allocate read buffer
36 int ftdi_init(struct ftdi_context *ftdi)
39 ftdi->usb_read_timeout = 5000;
40 ftdi->usb_write_timeout = 5000;
42 ftdi->type = TYPE_BM; /* chip type */
44 ftdi->bitbang_enabled = 0;
46 ftdi->readbuffer = NULL;
47 ftdi->readbuffer_offset = 0;
48 ftdi->readbuffer_remaining = 0;
49 ftdi->writebuffer_chunksize = 4096;
55 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
57 ftdi->error_str = NULL;
59 /* All fine. Now allocate the readbuffer */
60 return ftdi_read_data_set_chunksize(ftdi, 4096);
67 Open selected channels on a chip, otherwise use first channel
71 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
76 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
80 ftdi->index = INTERFACE_B;
85 ftdi_error_return(-1, "Unknown interface");
92 Deinitializes a ftdi_context.
94 void ftdi_deinit(struct ftdi_context *ftdi)
96 if (ftdi->readbuffer != NULL) {
97 free(ftdi->readbuffer);
98 ftdi->readbuffer = NULL;
104 Use an already open device.
106 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
113 Opens the first device with a given vendor and product ids.
117 -1: usb_find_busses() failed
118 -2: usb_find_devices() failed
119 -3: usb device not found
120 -4: unable to open device
121 -5: unable to claim device
123 -7: set baudrate failed
124 -8: get product description failed
125 -9: get serial number failed
126 -10: unable to close device
128 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
130 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
133 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
134 const char* description, const char* serial)
137 struct usb_device *dev;
142 if (usb_find_busses() < 0)
143 ftdi_error_return(-1, "usb_find_busses() failed");
145 if (usb_find_devices() < 0)
146 ftdi_error_return(-2,"usb_find_devices() failed");
148 for (bus = usb_busses; bus; bus = bus->next) {
149 for (dev = bus->devices; dev; dev = dev->next) {
150 if (dev->descriptor.idVendor == vendor
151 && dev->descriptor.idProduct == product) {
152 if (!(ftdi->usb_dev = usb_open(dev)))
153 ftdi_error_return(-4, "usb_open() failed");
155 if (description != NULL) {
156 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
157 usb_close (ftdi->usb_dev);
158 ftdi_error_return(-8, "unable to fetch product description");
160 if (strncmp(string, description, sizeof(string)) != 0) {
161 if (usb_close (ftdi->usb_dev) < 0)
162 ftdi_error_return(-10, "product description not matching");
166 if (serial != NULL) {
167 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
168 usb_close (ftdi->usb_dev);
169 ftdi_error_return(-9, "unable to fetch serial number");
171 if (strncmp(string, serial, sizeof(string)) != 0) {
172 ftdi->error_str = "serial number not matching\n";
173 if (usb_close (ftdi->usb_dev) != 0)
174 ftdi_error_return(-10, "unable to fetch serial number");
179 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
180 usb_close (ftdi->usb_dev);
181 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
184 if (ftdi_usb_reset (ftdi) != 0) {
185 usb_close (ftdi->usb_dev);
186 ftdi_error_return(-6, "ftdi_usb_reset failed");
189 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
190 usb_close (ftdi->usb_dev);
191 ftdi_error_return(-7, "set baudrate failed");
194 // Try to guess chip type
195 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
196 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
197 && dev->descriptor.iSerialNumber == 0))
198 ftdi->type = TYPE_BM;
199 else if (dev->descriptor.bcdDevice == 0x200)
200 ftdi->type = TYPE_AM;
201 else if (dev->descriptor.bcdDevice == 0x500) {
202 ftdi->type = TYPE_2232C;
204 ftdi->index = INTERFACE_A;
206 ftdi_error_return(0, "all fine");
212 ftdi_error_return(-3, "device not found");
217 Resets the ftdi device.
221 -1: FTDI reset failed
223 nt ftdi_usb_reset(struct ftdi_context *ftdi)
225 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
226 ftdi_error_return(-1,"FTDI reset failed");
228 // Invalidate data in the readbuffer
229 ftdi->readbuffer_offset = 0;
230 ftdi->readbuffer_remaining = 0;
235 /* ftdi_usb_purge_buffers
237 Cleans the buffers of the ftdi device.
241 -1: write buffer purge failed
242 -2: read buffer purge failed
244 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
246 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
247 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
249 // Invalidate data in the readbuffer
250 ftdi->readbuffer_offset = 0;
251 ftdi->readbuffer_remaining = 0;
253 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
254 ftdi_error_return(-2, "FTDI purge of TX buffer failed");
261 Closes the ftdi device.
265 -1: usb_release failed
268 int ftdi_usb_close(struct ftdi_context *ftdi)
272 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
275 if (usb_close (ftdi->usb_dev) != 0)
283 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
284 Function is only used internally
286 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
287 unsigned short *value, unsigned short *index)
289 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
290 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
291 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
292 int divisor, best_divisor, best_baud, best_baud_diff;
293 unsigned long encoded_divisor;
301 divisor = 24000000 / baudrate;
303 if (ftdi->type == TYPE_AM) {
304 // Round down to supported fraction (AM only)
305 divisor -= am_adjust_dn[divisor & 7];
308 // Try this divisor and the one above it (because division rounds down)
312 for (i = 0; i < 2; i++) {
313 int try_divisor = divisor + i;
317 // Round up to supported divisor value
318 if (try_divisor <= 8) {
319 // Round up to minimum supported divisor
321 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
322 // BM doesn't support divisors 9 through 11 inclusive
324 } else if (divisor < 16) {
325 // AM doesn't support divisors 9 through 15 inclusive
328 if (ftdi->type == TYPE_AM) {
329 // Round up to supported fraction (AM only)
330 try_divisor += am_adjust_up[try_divisor & 7];
331 if (try_divisor > 0x1FFF8) {
332 // Round down to maximum supported divisor value (for AM)
333 try_divisor = 0x1FFF8;
336 if (try_divisor > 0x1FFFF) {
337 // Round down to maximum supported divisor value (for BM)
338 try_divisor = 0x1FFFF;
342 // Get estimated baud rate (to nearest integer)
343 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
344 // Get absolute difference from requested baud rate
345 if (baud_estimate < baudrate) {
346 baud_diff = baudrate - baud_estimate;
348 baud_diff = baud_estimate - baudrate;
350 if (i == 0 || baud_diff < best_baud_diff) {
351 // Closest to requested baud rate so far
352 best_divisor = try_divisor;
353 best_baud = baud_estimate;
354 best_baud_diff = baud_diff;
355 if (baud_diff == 0) {
356 // Spot on! No point trying
361 // Encode the best divisor value
362 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
363 // Deal with special cases for encoded value
364 if (encoded_divisor == 1) {
365 encoded_divisor = 0; // 3000000 baud
366 } else if (encoded_divisor == 0x4001) {
367 encoded_divisor = 1; // 2000000 baud (BM only)
369 // Split into "value" and "index" values
370 *value = (unsigned short)(encoded_divisor & 0xFFFF);
371 if(ftdi->type == TYPE_2232C) {
372 *index = (unsigned short)(encoded_divisor >> 8);
374 *index |= ftdi->interface;
377 *index = (unsigned short)(encoded_divisor >> 16);
379 // Return the nearest baud rate
386 Sets the chip baudrate
391 -2: setting baudrate failed
393 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
395 unsigned short value, index;
398 if (ftdi->bitbang_enabled) {
399 baudrate = baudrate*4;
402 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
403 if (actual_baudrate <= 0)
404 ftdi_error_return (-1, "Silly baudrate <= 0.");
406 // Check within tolerance (about 5%)
407 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
408 || ((actual_baudrate < baudrate)
409 ? (actual_baudrate * 21 < baudrate * 20)
410 : (baudrate * 21 < actual_baudrate * 20)))
411 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
413 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
414 ftdi_error_return (-2, "Setting new baudrate failed");
416 ftdi->baudrate = baudrate;
421 ftdi_set_line_property
423 set (RS232) line characteristics by Alain Abbas
427 -1: Setting line property failed
429 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
430 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
432 unsigned short value = bits;
436 value |= (0x00 << 8);
439 value |= (0x01 << 8);
442 value |= (0x02 << 8);
445 value |= (0x03 << 8);
448 value |= (0x04 << 8);
454 value |= (0x00 << 11);
457 value |= (0x01 << 11);
460 value |= (0x02 << 11);
464 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
465 ftdi_error_return (-1, "Setting new line property failed");
470 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
474 int total_written = 0;
476 while (offset < size) {
477 int write_size = ftdi->writebuffer_chunksize;
479 if (offset+write_size > size)
480 write_size = size-offset;
482 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
484 ftdi_error_return(ret, "usb bulk write failed");
486 total_written += ret;
487 offset += write_size;
490 return total_written;
494 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
496 ftdi->writebuffer_chunksize = chunksize;
501 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
503 *chunksize = ftdi->writebuffer_chunksize;
508 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
510 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
512 // everything we want is still in the readbuffer?
513 if (size <= ftdi->readbuffer_remaining) {
514 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
517 ftdi->readbuffer_remaining -= size;
518 ftdi->readbuffer_offset += size;
520 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
524 // something still in the readbuffer, but not enough to satisfy 'size'?
525 if (ftdi->readbuffer_remaining != 0) {
526 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
529 offset += ftdi->readbuffer_remaining;
531 // do the actual USB read
532 while (offset < size && ret > 0) {
533 ftdi->readbuffer_remaining = 0;
534 ftdi->readbuffer_offset = 0;
535 /* returns how much received */
536 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
538 ftdi_error_return(ret, "usb bulk read failed");
541 // skip FTDI status bytes.
542 // Maybe stored in the future to enable modem use
543 num_of_chunks = ret / 64;
544 chunk_remains = ret % 64;
545 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
547 ftdi->readbuffer_offset += 2;
551 for (i = 1; i < num_of_chunks; i++)
552 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
553 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
555 if (chunk_remains > 2) {
556 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
557 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
559 ret -= 2*num_of_chunks;
561 ret -= 2*(num_of_chunks-1)+chunk_remains;
563 } else if (ret <= 2) {
564 // no more data to read?
568 // data still fits in buf?
569 if (offset+ret <= size) {
570 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
571 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
574 /* Did we read exactly the right amount of bytes? */
576 //printf("read_data exact rem %d offset %d\n",
577 //ftdi->readbuffer_remaining, offset);
580 // only copy part of the data or size <= readbuffer_chunksize
581 int part_size = size-offset;
582 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
584 ftdi->readbuffer_offset += part_size;
585 ftdi->readbuffer_remaining = ret-part_size;
588 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
589 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
600 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
602 unsigned char *new_buf;
604 // Invalidate all remaining data
605 ftdi->readbuffer_offset = 0;
606 ftdi->readbuffer_remaining = 0;
608 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
609 ftdi_error_return(-1, "out of memory for readbuffer");
611 ftdi->readbuffer = new_buf;
612 ftdi->readbuffer_chunksize = chunksize;
618 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
620 *chunksize = ftdi->readbuffer_chunksize;
626 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
628 unsigned short usb_val;
630 usb_val = bitmask; // low byte: bitmask
631 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
632 usb_val |= (ftdi->bitbang_mode << 8);
634 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
635 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
637 ftdi->bitbang_enabled = 1;
642 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
644 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
645 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
647 ftdi->bitbang_enabled = 0;
652 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
654 unsigned short usb_val;
656 usb_val = bitmask; // low byte: bitmask
657 usb_val |= (mode << 8);
658 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
659 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
661 ftdi->bitbang_mode = mode;
662 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
666 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
668 unsigned short usb_val;
669 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
670 ftdi_error_return(-1, "read pins failed");
672 *pins = (unsigned char)usb_val;
677 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
679 unsigned short usb_val;
682 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
685 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
686 ftdi_error_return(-2, "unable to set latency timer");
692 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
694 unsigned short usb_val;
695 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
696 ftdi_error_return(-1, "reading latency timer failed");
698 *latency = (unsigned char)usb_val;
703 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
705 eeprom->vendor_id = 0x0403;
706 eeprom->product_id = 0x6001;
708 eeprom->self_powered = 1;
709 eeprom->remote_wakeup = 1;
710 eeprom->BM_type_chip = 1;
712 eeprom->in_is_isochronous = 0;
713 eeprom->out_is_isochronous = 0;
714 eeprom->suspend_pull_downs = 0;
716 eeprom->use_serial = 0;
717 eeprom->change_usb_version = 0;
718 eeprom->usb_version = 0x0200;
719 eeprom->max_power = 0;
721 eeprom->manufacturer = NULL;
722 eeprom->product = NULL;
723 eeprom->serial = NULL;
730 Build binary output from ftdi_eeprom structure.
731 Output is suitable for ftdi_write_eeprom.
734 positive value: used eeprom size
735 -1: eeprom size (128 bytes) exceeded by custom strings
737 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
740 unsigned short checksum, value;
741 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
744 if (eeprom->manufacturer != NULL)
745 manufacturer_size = strlen(eeprom->manufacturer);
746 if (eeprom->product != NULL)
747 product_size = strlen(eeprom->product);
748 if (eeprom->serial != NULL)
749 serial_size = strlen(eeprom->serial);
751 size_check = 128; // eeprom is 128 bytes
752 size_check -= 28; // 28 are always in use (fixed)
753 size_check -= manufacturer_size*2;
754 size_check -= product_size*2;
755 size_check -= serial_size*2;
757 // eeprom size exceeded?
762 memset (output, 0, 128);
764 // Addr 00: Stay 00 00
765 // Addr 02: Vendor ID
766 output[0x02] = eeprom->vendor_id;
767 output[0x03] = eeprom->vendor_id >> 8;
769 // Addr 04: Product ID
770 output[0x04] = eeprom->product_id;
771 output[0x05] = eeprom->product_id >> 8;
773 // Addr 06: Device release number (0400h for BM features)
776 if (eeprom->BM_type_chip == 1)
781 // Addr 08: Config descriptor
782 // Bit 1: remote wakeup if 1
783 // Bit 0: self powered if 1
786 if (eeprom->self_powered == 1)
788 if (eeprom->remote_wakeup == 1)
792 // Addr 09: Max power consumption: max power = value * 2 mA
793 output[0x09] = eeprom->max_power;
796 // Addr 0A: Chip configuration
797 // Bit 7: 0 - reserved
798 // Bit 6: 0 - reserved
799 // Bit 5: 0 - reserved
800 // Bit 4: 1 - Change USB version
801 // Bit 3: 1 - Use the serial number string
802 // Bit 2: 1 - Enable suspend pull downs for lower power
803 // Bit 1: 1 - Out EndPoint is Isochronous
804 // Bit 0: 1 - In EndPoint is Isochronous
807 if (eeprom->in_is_isochronous == 1)
809 if (eeprom->out_is_isochronous == 1)
811 if (eeprom->suspend_pull_downs == 1)
813 if (eeprom->use_serial == 1)
815 if (eeprom->change_usb_version == 1)
822 // Addr 0C: USB version low byte when 0x0A bit 4 is set
823 // Addr 0D: USB version high byte when 0x0A bit 4 is set
824 if (eeprom->change_usb_version == 1) {
825 output[0x0C] = eeprom->usb_version;
826 output[0x0D] = eeprom->usb_version >> 8;
830 // Addr 0E: Offset of the manufacturer string + 0x80
831 output[0x0E] = 0x14 + 0x80;
833 // Addr 0F: Length of manufacturer string
834 output[0x0F] = manufacturer_size*2 + 2;
836 // Addr 10: Offset of the product string + 0x80, calculated later
837 // Addr 11: Length of product string
838 output[0x11] = product_size*2 + 2;
840 // Addr 12: Offset of the serial string + 0x80, calculated later
841 // Addr 13: Length of serial string
842 output[0x13] = serial_size*2 + 2;
845 output[0x14] = manufacturer_size*2 + 2;
846 output[0x15] = 0x03; // type: string
850 // Output manufacturer
851 for (j = 0; j < manufacturer_size; j++) {
852 output[i] = eeprom->manufacturer[j], i++;
853 output[i] = 0x00, i++;
856 // Output product name
857 output[0x10] = i + 0x80; // calculate offset
858 output[i] = product_size*2 + 2, i++;
859 output[i] = 0x03, i++;
860 for (j = 0; j < product_size; j++) {
861 output[i] = eeprom->product[j], i++;
862 output[i] = 0x00, i++;
866 output[0x12] = i + 0x80; // calculate offset
867 output[i] = serial_size*2 + 2, i++;
868 output[i] = 0x03, i++;
869 for (j = 0; j < serial_size; j++) {
870 output[i] = eeprom->serial[j], i++;
871 output[i] = 0x00, i++;
874 // calculate checksum
877 for (i = 0; i < 63; i++) {
879 value += output[(i*2)+1] << 8;
881 checksum = value^checksum;
882 checksum = (checksum << 1) | (checksum >> 15);
885 output[0x7E] = checksum;
886 output[0x7F] = checksum >> 8;
892 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
896 for (i = 0; i < 64; i++) {
897 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
898 ftdi_error_return(-1, "reading eeprom failed");
905 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
907 unsigned short usb_val;
910 for (i = 0; i < 64; i++) {
911 usb_val = eeprom[i*2];
912 usb_val += eeprom[(i*2)+1] << 8;
913 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
914 ftdi_error_return(-1, "unable to write eeprom");
921 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
923 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
924 ftdi_error_return(-1, "unable to erase eeprom");
930 char *ftdi_get_error_string (struct ftdi_context *ftdi)
932 return ftdi->error_str;