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; \
28 /* ftdi_init return codes:
30 -1: couldn't allocate read buffer
32 int ftdi_init(struct ftdi_context *ftdi)
35 ftdi->usb_read_timeout = 5000;
36 ftdi->usb_write_timeout = 5000;
38 ftdi->type = TYPE_BM; /* chip type */
40 ftdi->bitbang_enabled = 0;
42 ftdi->readbuffer = NULL;
43 ftdi->readbuffer_offset = 0;
44 ftdi->readbuffer_remaining = 0;
45 ftdi->writebuffer_chunksize = 4096;
51 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
53 ftdi->error_str = NULL;
55 /* All fine. Now allocate the readbuffer */
56 return ftdi_read_data_set_chunksize(ftdi, 4096);
60 Open selected channels on a chip, otherwise use first channel
64 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
69 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
73 ftdi->index = INTERFACE_B;
78 ftdi_error_return(-1, "Unknown interface");
83 void ftdi_deinit(struct ftdi_context *ftdi)
85 if (ftdi->readbuffer != NULL) {
86 free(ftdi->readbuffer);
87 ftdi->readbuffer = NULL;
92 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
98 /* ftdi_usb_open return codes:
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
106 -7: set baudrate failed
107 -8: get product description failed
108 -9: get serial number failed
109 -10: unable to close device
111 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
113 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
116 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
117 const char* description, const char* serial)
120 struct usb_device *dev;
125 if (usb_find_busses() < 0)
126 ftdi_error_return(-1, "usb_find_busses() failed");
128 if (usb_find_devices() < 0)
129 ftdi_error_return(-2,"usb_find_devices() failed");
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");
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");
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");
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");
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");
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!");
167 if (ftdi_usb_reset (ftdi) != 0) {
168 usb_close (ftdi->usb_dev);
169 ftdi_error_return(-6, "ftdi_usb_reset failed");
172 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
173 usb_close (ftdi->usb_dev);
174 ftdi_error_return(-7, "set baudrate failed");
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;
187 ftdi->index = INTERFACE_A;
189 ftdi_error_return(0, "all fine");
195 ftdi_error_return(-3, "device not found");
199 int ftdi_usb_reset(struct ftdi_context *ftdi)
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");
204 // Invalidate data in the readbuffer
205 ftdi->readbuffer_offset = 0;
206 ftdi->readbuffer_remaining = 0;
211 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
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");
216 // Invalidate data in the readbuffer
217 ftdi->readbuffer_offset = 0;
218 ftdi->readbuffer_remaining = 0;
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");
226 /* ftdi_usb_close return codes
228 -1: usb_release failed
231 int ftdi_usb_close(struct ftdi_context *ftdi)
235 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
238 if (usb_close (ftdi->usb_dev) != 0)
246 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
247 Function is only used internally
249 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
250 unsigned short *value, unsigned short *index)
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;
264 divisor = 24000000 / baudrate;
266 if (ftdi->type == TYPE_AM) {
267 // Round down to supported fraction (AM only)
268 divisor -= am_adjust_dn[divisor & 7];
271 // Try this divisor and the one above it (because division rounds down)
275 for (i = 0; i < 2; i++) {
276 int try_divisor = divisor + i;
280 // Round up to supported divisor value
281 if (try_divisor <= 8) {
282 // Round up to minimum supported divisor
284 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
285 // BM doesn't support divisors 9 through 11 inclusive
287 } else if (divisor < 16) {
288 // AM doesn't support divisors 9 through 15 inclusive
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;
299 if (try_divisor > 0x1FFFF) {
300 // Round down to maximum supported divisor value (for BM)
301 try_divisor = 0x1FFFF;
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;
311 baud_diff = baud_estimate - baudrate;
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
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)
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);
337 *index |= ftdi->interface;
340 *index = (unsigned short)(encoded_divisor >> 16);
342 // Return the nearest baud rate
347 ftdi_set_baudrate return codes:
350 -2: setting baudrate failed
352 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
354 unsigned short value, index;
357 if (ftdi->bitbang_enabled) {
358 baudrate = baudrate*4;
361 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
362 if (actual_baudrate <= 0)
363 ftdi_error_return (-1, "Silly baudrate <= 0.");
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");
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");
375 ftdi->baudrate = baudrate;
380 set (RS232) line characteristics
383 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
384 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
386 unsigned short value = bits;
390 value |= (0x00 << 8);
393 value |= (0x01 << 8);
396 value |= (0x02 << 8);
399 value |= (0x03 << 8);
402 value |= (0x04 << 8);
408 value |= (0x00 << 11);
411 value |= (0x01 << 11);
414 value |= (0x02 << 11);
418 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
419 ftdi_error_return (-1, "Setting new line property failed");
424 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
428 int total_written = 0;
430 while (offset < size) {
431 int write_size = ftdi->writebuffer_chunksize;
433 if (offset+write_size > size)
434 write_size = size-offset;
436 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
438 ftdi_error_return(ret, "usb bulk write failed");
440 total_written += ret;
441 offset += write_size;
444 return total_written;
448 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
450 ftdi->writebuffer_chunksize = chunksize;
455 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
457 *chunksize = ftdi->writebuffer_chunksize;
462 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
464 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
466 // everything we want is still in the readbuffer?
467 if (size <= ftdi->readbuffer_remaining) {
468 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
471 ftdi->readbuffer_remaining -= size;
472 ftdi->readbuffer_offset += size;
474 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
478 // something still in the readbuffer, but not enough to satisfy 'size'?
479 if (ftdi->readbuffer_remaining != 0) {
480 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
483 offset += ftdi->readbuffer_remaining;
485 // do the actual USB read
486 while (offset < size && ret > 0) {
487 ftdi->readbuffer_remaining = 0;
488 ftdi->readbuffer_offset = 0;
489 /* returns how much received */
490 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
492 ftdi_error_return(ret, "usb bulk read failed");
495 // skip FTDI status bytes.
496 // Maybe stored in the future to enable modem use
497 num_of_chunks = ret / 64;
498 chunk_remains = ret % 64;
499 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
501 ftdi->readbuffer_offset += 2;
505 for (i = 1; i < num_of_chunks; i++)
506 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
507 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
509 if (chunk_remains > 2) {
510 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
511 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
513 ret -= 2*num_of_chunks;
515 ret -= 2*(num_of_chunks-1)+chunk_remains;
517 } else if (ret <= 2) {
518 // no more data to read?
522 // data still fits in buf?
523 if (offset+ret <= size) {
524 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
525 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
528 /* Did we read exactly the right amount of bytes? */
530 //printf("read_data exact rem %d offset %d\n",
531 //ftdi->readbuffer_remaining, offset);
534 // only copy part of the data or size <= readbuffer_chunksize
535 int part_size = size-offset;
536 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
538 ftdi->readbuffer_offset += part_size;
539 ftdi->readbuffer_remaining = ret-part_size;
542 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
543 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
554 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
556 unsigned char *new_buf;
558 // Invalidate all remaining data
559 ftdi->readbuffer_offset = 0;
560 ftdi->readbuffer_remaining = 0;
562 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
563 ftdi_error_return(-1, "out of memory for readbuffer");
565 ftdi->readbuffer = new_buf;
566 ftdi->readbuffer_chunksize = chunksize;
572 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
574 *chunksize = ftdi->readbuffer_chunksize;
580 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
582 unsigned short usb_val;
584 usb_val = bitmask; // low byte: bitmask
585 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
586 usb_val |= (ftdi->bitbang_mode << 8);
588 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
589 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
591 ftdi->bitbang_enabled = 1;
596 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
598 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
599 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
601 ftdi->bitbang_enabled = 0;
606 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
608 unsigned short usb_val;
610 usb_val = bitmask; // low byte: bitmask
611 usb_val |= (mode << 8);
612 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
613 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
615 ftdi->bitbang_mode = mode;
616 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
620 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
622 unsigned short usb_val;
623 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
624 ftdi_error_return(-1, "read pins failed");
626 *pins = (unsigned char)usb_val;
631 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
633 unsigned short usb_val;
636 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
639 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
640 ftdi_error_return(-2, "unable to set latency timer");
646 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
648 unsigned short usb_val;
649 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
650 ftdi_error_return(-1, "reading latency timer failed");
652 *latency = (unsigned char)usb_val;
657 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
659 eeprom->vendor_id = 0x0403;
660 eeprom->product_id = 0x6001;
662 eeprom->self_powered = 1;
663 eeprom->remote_wakeup = 1;
664 eeprom->BM_type_chip = 1;
666 eeprom->in_is_isochronous = 0;
667 eeprom->out_is_isochronous = 0;
668 eeprom->suspend_pull_downs = 0;
670 eeprom->use_serial = 0;
671 eeprom->change_usb_version = 0;
672 eeprom->usb_version = 0x0200;
673 eeprom->max_power = 0;
675 eeprom->manufacturer = NULL;
676 eeprom->product = NULL;
677 eeprom->serial = NULL;
682 ftdi_eeprom_build return codes:
683 positive value: used eeprom size
684 -1: eeprom size (128 bytes) exceeded by custom strings
686 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
689 unsigned short checksum, value;
690 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
693 if (eeprom->manufacturer != NULL)
694 manufacturer_size = strlen(eeprom->manufacturer);
695 if (eeprom->product != NULL)
696 product_size = strlen(eeprom->product);
697 if (eeprom->serial != NULL)
698 serial_size = strlen(eeprom->serial);
700 size_check = 128; // eeprom is 128 bytes
701 size_check -= 28; // 28 are always in use (fixed)
702 size_check -= manufacturer_size*2;
703 size_check -= product_size*2;
704 size_check -= serial_size*2;
706 // eeprom size exceeded?
711 memset (output, 0, 128);
713 // Addr 00: Stay 00 00
714 // Addr 02: Vendor ID
715 output[0x02] = eeprom->vendor_id;
716 output[0x03] = eeprom->vendor_id >> 8;
718 // Addr 04: Product ID
719 output[0x04] = eeprom->product_id;
720 output[0x05] = eeprom->product_id >> 8;
722 // Addr 06: Device release number (0400h for BM features)
725 if (eeprom->BM_type_chip == 1)
730 // Addr 08: Config descriptor
731 // Bit 1: remote wakeup if 1
732 // Bit 0: self powered if 1
735 if (eeprom->self_powered == 1)
737 if (eeprom->remote_wakeup == 1)
741 // Addr 09: Max power consumption: max power = value * 2 mA
742 output[0x09] = eeprom->max_power;
745 // Addr 0A: Chip configuration
746 // Bit 7: 0 - reserved
747 // Bit 6: 0 - reserved
748 // Bit 5: 0 - reserved
749 // Bit 4: 1 - Change USB version
750 // Bit 3: 1 - Use the serial number string
751 // Bit 2: 1 - Enable suspend pull downs for lower power
752 // Bit 1: 1 - Out EndPoint is Isochronous
753 // Bit 0: 1 - In EndPoint is Isochronous
756 if (eeprom->in_is_isochronous == 1)
758 if (eeprom->out_is_isochronous == 1)
760 if (eeprom->suspend_pull_downs == 1)
762 if (eeprom->use_serial == 1)
764 if (eeprom->change_usb_version == 1)
771 // Addr 0C: USB version low byte when 0x0A bit 4 is set
772 // Addr 0D: USB version high byte when 0x0A bit 4 is set
773 if (eeprom->change_usb_version == 1) {
774 output[0x0C] = eeprom->usb_version;
775 output[0x0D] = eeprom->usb_version >> 8;
779 // Addr 0E: Offset of the manufacturer string + 0x80
780 output[0x0E] = 0x14 + 0x80;
782 // Addr 0F: Length of manufacturer string
783 output[0x0F] = manufacturer_size*2 + 2;
785 // Addr 10: Offset of the product string + 0x80, calculated later
786 // Addr 11: Length of product string
787 output[0x11] = product_size*2 + 2;
789 // Addr 12: Offset of the serial string + 0x80, calculated later
790 // Addr 13: Length of serial string
791 output[0x13] = serial_size*2 + 2;
794 output[0x14] = manufacturer_size*2 + 2;
795 output[0x15] = 0x03; // type: string
799 // Output manufacturer
800 for (j = 0; j < manufacturer_size; j++) {
801 output[i] = eeprom->manufacturer[j], i++;
802 output[i] = 0x00, i++;
805 // Output product name
806 output[0x10] = i + 0x80; // calculate offset
807 output[i] = product_size*2 + 2, i++;
808 output[i] = 0x03, i++;
809 for (j = 0; j < product_size; j++) {
810 output[i] = eeprom->product[j], i++;
811 output[i] = 0x00, i++;
815 output[0x12] = i + 0x80; // calculate offset
816 output[i] = serial_size*2 + 2, i++;
817 output[i] = 0x03, i++;
818 for (j = 0; j < serial_size; j++) {
819 output[i] = eeprom->serial[j], i++;
820 output[i] = 0x00, i++;
823 // calculate checksum
826 for (i = 0; i < 63; i++) {
828 value += output[(i*2)+1] << 8;
830 checksum = value^checksum;
831 checksum = (checksum << 1) | (checksum >> 15);
834 output[0x7E] = checksum;
835 output[0x7F] = checksum >> 8;
841 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
845 for (i = 0; i < 64; i++) {
846 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
847 ftdi_error_return(-1, "reading eeprom failed");
854 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
856 unsigned short usb_val;
859 for (i = 0; i < 64; i++) {
860 usb_val = eeprom[i*2];
861 usb_val += eeprom[(i*2)+1] << 8;
862 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
863 ftdi_error_return(-1, "unable to write eeprom");
870 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
872 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
873 ftdi_error_return(-1, "unable to erase eeprom");
879 char *ftdi_get_error_string (struct ftdi_context *ftdi)
881 return ftdi->error_str;