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 ***************************************************************************/
21 /* ftdi_init return codes:
23 -1: couldn't allocate read buffer
25 int ftdi_init(struct ftdi_context *ftdi) {
27 ftdi->usb_timeout = 5000;
30 ftdi->bitbang_enabled = 0;
32 ftdi->readbuffer = NULL;
33 ftdi->readbuffer_offset = 0;
34 ftdi->readbuffer_remaining = 0;
35 ftdi->writebuffer_chunksize = 4096;
37 ftdi->error_str = NULL;
39 // all fine. Now allocate the readbuffer
40 return ftdi_read_data_set_chunksize(ftdi, 4096);
44 void ftdi_deinit(struct ftdi_context *ftdi) {
45 if (ftdi->readbuffer != NULL) {
46 free(ftdi->readbuffer);
47 ftdi->readbuffer = NULL;
52 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) {
57 /* ftdi_usb_open return codes:
59 -1: usb_find_busses() failed
60 -2: usb_find_devices() failed
61 -3: usb device not found
62 -4: unable to open device
63 -5: unable to claim device
65 -7: set baudrate failed
67 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) {
69 struct usb_device *dev;
73 if (usb_find_busses() < 0) {
74 ftdi->error_str = "usb_find_busses() failed";
78 if (usb_find_devices() < 0) {
79 ftdi->error_str = "usb_find_devices() failed";
83 for (bus = usb_busses; bus; bus = bus->next) {
84 for (dev = bus->devices; dev; dev = dev->next) {
85 if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) {
86 ftdi->usb_dev = usb_open(dev);
88 if (usb_claim_interface(ftdi->usb_dev, 0) != 0) {
89 ftdi->error_str = "unable to claim usb device. You can still use it though...";
93 if (ftdi_usb_reset (ftdi) != 0)
96 if (ftdi_set_baudrate (ftdi, 9600) != 0)
101 ftdi->error_str = "usb_open() failed";
114 int ftdi_usb_reset(struct ftdi_context *ftdi) {
115 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
116 ftdi->error_str = "FTDI reset failed";
123 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) {
124 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, 0, NULL, 0, ftdi->usb_timeout) != 0) {
125 ftdi->error_str = "FTDI purge of RX buffer failed";
129 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, 0, NULL, 0, ftdi->usb_timeout) != 0) {
130 ftdi->error_str = "FTDI purge of TX buffer failed";
137 /* ftdi_usb_close return codes
139 -1: usb_release failed
142 int ftdi_usb_close(struct ftdi_context *ftdi) {
145 if (usb_release_interface(ftdi->usb_dev, 0) != 0)
148 if (usb_close (ftdi->usb_dev) != 0)
156 ftdi_set_baudrate return codes:
159 -2: setting baudrate failed
161 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) {
162 unsigned short ftdi_baudrate;
164 if (ftdi->bitbang_enabled) {
165 baudrate = baudrate*4;
170 ftdi_baudrate = 0x2710;
173 ftdi_baudrate = 0x1388;
176 ftdi_baudrate = 0x09C4;
179 ftdi_baudrate = 0x04E2;
182 ftdi_baudrate = 0x0271;
185 ftdi_baudrate = 0x4138;
188 ftdi_baudrate = 0x809C;
191 ftdi_baudrate = 0xC04E;
194 ftdi_baudrate = 0x0034;
197 ftdi_baudrate = 0x001A;
200 ftdi_baudrate = 0x000D;
203 ftdi_baudrate = 0x4006;
206 ftdi_baudrate = 0x8003;
209 ftdi->error_str = "Unknown baudrate. Note: bitbang baudrates are automatically multiplied by 4";
213 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, ftdi_baudrate, 0, NULL, 0, ftdi->usb_timeout) != 0) {
214 ftdi->error_str = "Setting new baudrate failed";
218 ftdi->baudrate = baudrate;
223 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
226 while (offset < size) {
227 int write_size = ftdi->writebuffer_chunksize;
229 if (offset+write_size > size)
230 write_size = size-offset;
232 ret=usb_bulk_write(ftdi->usb_dev, 2, buf+offset, write_size, ftdi->usb_timeout);
234 ftdi->error_str = "bulk write failed";
238 offset += write_size;
245 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
246 ftdi->writebuffer_chunksize = chunksize;
251 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
252 *chunksize = ftdi->writebuffer_chunksize;
257 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
258 int offset = 0, ret = 1;
260 // everything we want is still in the readbuffer?
261 if (size <= ftdi->readbuffer_remaining) {
262 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
265 ftdi->readbuffer_remaining -= size;
266 ftdi->readbuffer_offset += size;
268 // printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining);
273 // something still in the readbuffer, but not enough to satisfy 'size'?
274 if (ftdi->readbuffer_remaining != 0) {
275 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
277 // printf("Got bytes from buffer: %d\n", ftdi->readbuffer_remaining);
280 offset += ftdi->readbuffer_remaining;
283 // do the actual USB read
284 while (offset < size && ret > 0) {
285 ftdi->readbuffer_remaining = 0;
286 ftdi->readbuffer_offset = 0;
287 ret = usb_bulk_read (ftdi->usb_dev, 0x81, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_timeout);
290 ftdi->error_str = "bulk read failed";
295 // skip FTDI status bytes.
296 // Maybe stored in the future to enable modem use
297 ftdi->readbuffer_offset += 2;
299 } else if (ret <= 2) {
300 // no more data to read?
305 // data still fits in buf?
306 if (offset+ret <= size) {
307 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
313 // only copy part of the data or size <= readbuffer_chunksize
314 int part_size = size-offset;
315 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
317 ftdi->readbuffer_offset += part_size;
318 ftdi->readbuffer_remaining = ret-part_size;
321 // printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n", part_size, size, offset, ret, ftdi->readbuffer_remaining);
333 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
334 // Invalidate all remaining data
335 ftdi->readbuffer_offset = 0;
336 ftdi->readbuffer_remaining = 0;
338 unsigned char *new_buf;
339 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) {
340 ftdi->error_str = "out of memory for readbuffer";
344 ftdi->readbuffer = new_buf;
345 ftdi->readbuffer_chunksize = chunksize;
351 int ftdi_readt_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
352 *chunksize = ftdi->readbuffer_chunksize;
358 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) {
359 unsigned short usb_val;
361 usb_val = bitmask; // low byte: bitmask
362 usb_val += 1 << 8; // high byte: enable flag
363 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
364 ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?";
368 ftdi->bitbang_enabled = 1;
373 int ftdi_disable_bitbang(struct ftdi_context *ftdi) {
374 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
375 ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?";
379 ftdi->bitbang_enabled = 0;
384 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) {
385 unsigned short usb_val;
386 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
387 ftdi->error_str = "Read pins failed";
391 *pins = (unsigned char)usb_val;
396 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
397 unsigned short usb_val;
400 ftdi->error_str = "Latency out of range. Only valid for 1-255";
405 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
406 ftdi->error_str = "Unable to set latency timer";
413 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) {
414 unsigned short usb_val;
415 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
416 ftdi->error_str = "Reading latency timer failed";
420 *latency = (unsigned char)usb_val;
425 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
426 eeprom->vendor_id = 0403;
427 eeprom->product_id = 6001;
429 eeprom->self_powered = 1;
430 eeprom->remote_wakeup = 1;
431 eeprom->BM_type_chip = 1;
433 eeprom->in_is_isochronous = 0;
434 eeprom->out_is_isochronous = 0;
435 eeprom->suspend_pull_downs = 0;
437 eeprom->use_serial = 0;
438 eeprom->change_usb_version = 0;
439 eeprom->usb_version = 200;
440 eeprom->max_power = 0;
442 eeprom->manufacturer = NULL;
443 eeprom->product = NULL;
444 eeprom->serial = NULL;
449 ftdi_eeprom_build return codes:
450 positive value: used eeprom size
451 -1: eeprom size (128 bytes) exceeded by custom strings
453 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
455 unsigned short checksum, value;
456 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
459 if (eeprom->manufacturer != NULL)
460 manufacturer_size = strlen(eeprom->manufacturer);
461 if (eeprom->product != NULL)
462 product_size = strlen(eeprom->product);
463 if (eeprom->serial != NULL)
464 serial_size = strlen(eeprom->serial);
466 size_check = 128; // eeprom is 128 bytes
467 size_check -= 28; // 28 are always in use (fixed)
468 size_check -= manufacturer_size*2;
469 size_check -= product_size*2;
470 size_check -= serial_size*2;
472 // eeprom size exceeded?
477 memset (output, 0, 128);
479 // Addr 00: Stay 00 00
480 // Addr 02: Vendor ID
481 output[0x02] = eeprom->vendor_id;
482 output[0x03] = eeprom->vendor_id >> 8;
484 // Addr 04: Product ID
485 output[0x04] = eeprom->product_id;
486 output[0x05] = eeprom->product_id >> 8;
488 // Addr 06: Device release number (0400h for BM features)
491 if (eeprom->BM_type_chip == 1)
496 // Addr 08: Config descriptor
497 // Bit 1: remote wakeup if 1
498 // Bit 0: self powered if 1
501 if (eeprom->self_powered == 1)
503 if (eeprom->remote_wakeup == 1)
507 // Addr 09: Max power consumption: max power = value * 2 mA
508 output[0x09] = eeprom->max_power;;
510 // Addr 0A: Chip configuration
511 // Bit 7: 0 - reserved
512 // Bit 6: 0 - reserved
513 // Bit 5: 0 - reserved
514 // Bit 4: 1 - Change USB version
515 // Bit 3: 1 - Use the serial number string
516 // Bit 2: 1 - Enable suspend pull downs for lower power
517 // Bit 1: 1 - Out EndPoint is Isochronous
518 // Bit 0: 1 - In EndPoint is Isochronous
521 if (eeprom->in_is_isochronous == 1)
523 if (eeprom->out_is_isochronous == 1)
525 if (eeprom->suspend_pull_downs == 1)
527 if (eeprom->use_serial == 1)
529 if (eeprom->change_usb_version == 1)
536 // Addr 0C: USB version low byte when 0x0A bit 4 is set
537 // Addr 0D: USB version high byte when 0x0A bit 4 is set
538 if (eeprom->change_usb_version == 1) {
539 output[0x0C] = eeprom->usb_version;
540 output[0x0D] = eeprom->usb_version >> 8;
544 // Addr 0E: Offset of the manufacturer string + 0x80
545 output[0x0E] = 0x14 + 0x80;
547 // Addr 0F: Length of manufacturer string
548 output[0x0F] = manufacturer_size*2 + 2;
550 // Addr 10: Offset of the product string + 0x80, calculated later
551 // Addr 11: Length of product string
552 output[0x11] = product_size*2 + 2;
554 // Addr 12: Offset of the serial string + 0x80, calculated later
555 // Addr 13: Length of serial string
556 output[0x13] = serial_size*2 + 2;
559 output[0x14] = manufacturer_size*2 + 2;
560 output[0x15] = 0x03; // type: string
564 // Output manufacturer
565 for (j = 0; j < manufacturer_size; j++) {
566 output[i] = eeprom->manufacturer[j], i++;
567 output[i] = 0x00, i++;
570 // Output product name
571 output[0x10] = i + 0x80; // calculate offset
572 output[i] = product_size*2 + 2, i++;
573 output[i] = 0x03, i++;
574 for (j = 0; j < product_size; j++) {
575 output[i] = eeprom->product[j], i++;
576 output[i] = 0x00, i++;
580 output[0x12] = i + 0x80; // calculate offset
581 output[i] = serial_size*2 + 2, i++;
582 output[i] = 0x03, i++;
583 for (j = 0; j < serial_size; j++) {
584 output[i] = eeprom->serial[j], i++;
585 output[i] = 0x00, i++;
588 // calculate checksum
591 for (i = 0; i < 63; i++) {
593 value += output[(i*2)+1] << 8;
595 checksum = value^checksum;
596 checksum = (checksum << 1) | (checksum >> 15);
599 output[0x7E] = checksum;
600 output[0x7F] = checksum >> 8;
606 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
609 for (i = 0; i < 64; i++) {
610 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_timeout) != 2) {
611 ftdi->error_str = "Reading eeprom failed";
620 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
621 unsigned short usb_val;
624 for (i = 0; i < 64; i++) {
625 usb_val = eeprom[i*2];
626 usb_val += eeprom[(i*2)+1] << 8;
627 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_timeout) != 0) {
628 ftdi->error_str = "Unable to write eeprom";
637 int ftdi_erase_eeprom(struct ftdi_context *ftdi) {
638 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
639 ftdi->error_str = "Unable to erase eeprom";