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";
120 ftdi->readbuffer_offset = 0;
121 ftdi->readbuffer_remaining = 0;
126 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) {
127 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, 0, NULL, 0, ftdi->usb_timeout) != 0) {
128 ftdi->error_str = "FTDI purge of RX buffer failed";
132 ftdi->readbuffer_offset = 0;
133 ftdi->readbuffer_remaining = 0;
135 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, 0, NULL, 0, ftdi->usb_timeout) != 0) {
136 ftdi->error_str = "FTDI purge of TX buffer failed";
143 /* ftdi_usb_close return codes
145 -1: usb_release failed
148 int ftdi_usb_close(struct ftdi_context *ftdi) {
151 if (usb_release_interface(ftdi->usb_dev, 0) != 0)
154 if (usb_close (ftdi->usb_dev) != 0)
162 ftdi_set_baudrate return codes:
165 -2: setting baudrate failed
167 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) {
168 unsigned short ftdi_baudrate;
170 if (ftdi->bitbang_enabled) {
171 baudrate = baudrate*4;
176 ftdi_baudrate = 0x2710;
179 ftdi_baudrate = 0x1388;
182 ftdi_baudrate = 0x09C4;
185 ftdi_baudrate = 0x04E2;
188 ftdi_baudrate = 0x0271;
191 ftdi_baudrate = 0x4138;
194 ftdi_baudrate = 0x809C;
197 ftdi_baudrate = 0xC04E;
200 ftdi_baudrate = 0x0034;
203 ftdi_baudrate = 0x001A;
206 ftdi_baudrate = 0x000D;
209 ftdi_baudrate = 0x4006;
212 ftdi_baudrate = 0x8003;
215 ftdi->error_str = "Unknown baudrate. Note: bitbang baudrates are automatically multiplied by 4";
219 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, ftdi_baudrate, 0, NULL, 0, ftdi->usb_timeout) != 0) {
220 ftdi->error_str = "Setting new baudrate failed";
224 ftdi->baudrate = baudrate;
229 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
232 while (offset < size) {
233 int write_size = ftdi->writebuffer_chunksize;
235 if (offset+write_size > size)
236 write_size = size-offset;
238 ret=usb_bulk_write(ftdi->usb_dev, 2, buf+offset, write_size, ftdi->usb_timeout);
240 ftdi->error_str = "bulk write failed";
244 offset += write_size;
251 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
252 ftdi->writebuffer_chunksize = chunksize;
257 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
258 *chunksize = ftdi->writebuffer_chunksize;
263 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
264 int offset = 0, ret = 1;
266 // everything we want is still in the readbuffer?
267 if (size <= ftdi->readbuffer_remaining) {
268 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
271 ftdi->readbuffer_remaining -= size;
272 ftdi->readbuffer_offset += size;
274 // printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining);
279 // something still in the readbuffer, but not enough to satisfy 'size'?
280 if (ftdi->readbuffer_remaining != 0) {
281 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
283 // printf("Got bytes from buffer: %d\n", ftdi->readbuffer_remaining);
286 offset += ftdi->readbuffer_remaining;
289 // do the actual USB read
290 while (offset < size && ret > 0) {
291 ftdi->readbuffer_remaining = 0;
292 ftdi->readbuffer_offset = 0;
293 ret = usb_bulk_read (ftdi->usb_dev, 0x81, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_timeout);
296 ftdi->error_str = "bulk read failed";
301 // skip FTDI status bytes.
302 // Maybe stored in the future to enable modem use
303 ftdi->readbuffer_offset += 2;
305 } else if (ret <= 2) {
306 // no more data to read?
311 // data still fits in buf?
312 if (offset+ret <= size) {
313 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
319 // only copy part of the data or size <= readbuffer_chunksize
320 int part_size = size-offset;
321 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
323 ftdi->readbuffer_offset += part_size;
324 ftdi->readbuffer_remaining = ret-part_size;
327 // printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n", part_size, size, offset, ret, ftdi->readbuffer_remaining);
339 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
340 // Invalidate all remaining data
341 ftdi->readbuffer_offset = 0;
342 ftdi->readbuffer_remaining = 0;
344 unsigned char *new_buf;
345 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) {
346 ftdi->error_str = "out of memory for readbuffer";
350 ftdi->readbuffer = new_buf;
351 ftdi->readbuffer_chunksize = chunksize;
357 int ftdi_readt_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
358 *chunksize = ftdi->readbuffer_chunksize;
364 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) {
365 unsigned short usb_val;
367 usb_val = bitmask; // low byte: bitmask
368 usb_val += 1 << 8; // high byte: enable flag
369 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
370 ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?";
374 ftdi->bitbang_enabled = 1;
379 int ftdi_disable_bitbang(struct ftdi_context *ftdi) {
380 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
381 ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?";
385 ftdi->bitbang_enabled = 0;
390 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) {
391 unsigned short usb_val;
392 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
393 ftdi->error_str = "Read pins failed";
397 *pins = (unsigned char)usb_val;
402 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
403 unsigned short usb_val;
406 ftdi->error_str = "Latency out of range. Only valid for 1-255";
411 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
412 ftdi->error_str = "Unable to set latency timer";
419 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) {
420 unsigned short usb_val;
421 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
422 ftdi->error_str = "Reading latency timer failed";
426 *latency = (unsigned char)usb_val;
431 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
432 eeprom->vendor_id = 0403;
433 eeprom->product_id = 6001;
435 eeprom->self_powered = 1;
436 eeprom->remote_wakeup = 1;
437 eeprom->BM_type_chip = 1;
439 eeprom->in_is_isochronous = 0;
440 eeprom->out_is_isochronous = 0;
441 eeprom->suspend_pull_downs = 0;
443 eeprom->use_serial = 0;
444 eeprom->change_usb_version = 0;
445 eeprom->usb_version = 200;
446 eeprom->max_power = 0;
448 eeprom->manufacturer = NULL;
449 eeprom->product = NULL;
450 eeprom->serial = NULL;
455 ftdi_eeprom_build return codes:
456 positive value: used eeprom size
457 -1: eeprom size (128 bytes) exceeded by custom strings
459 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
461 unsigned short checksum, value;
462 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
465 if (eeprom->manufacturer != NULL)
466 manufacturer_size = strlen(eeprom->manufacturer);
467 if (eeprom->product != NULL)
468 product_size = strlen(eeprom->product);
469 if (eeprom->serial != NULL)
470 serial_size = strlen(eeprom->serial);
472 size_check = 128; // eeprom is 128 bytes
473 size_check -= 28; // 28 are always in use (fixed)
474 size_check -= manufacturer_size*2;
475 size_check -= product_size*2;
476 size_check -= serial_size*2;
478 // eeprom size exceeded?
483 memset (output, 0, 128);
485 // Addr 00: Stay 00 00
486 // Addr 02: Vendor ID
487 output[0x02] = eeprom->vendor_id;
488 output[0x03] = eeprom->vendor_id >> 8;
490 // Addr 04: Product ID
491 output[0x04] = eeprom->product_id;
492 output[0x05] = eeprom->product_id >> 8;
494 // Addr 06: Device release number (0400h for BM features)
497 if (eeprom->BM_type_chip == 1)
502 // Addr 08: Config descriptor
503 // Bit 1: remote wakeup if 1
504 // Bit 0: self powered if 1
507 if (eeprom->self_powered == 1)
509 if (eeprom->remote_wakeup == 1)
513 // Addr 09: Max power consumption: max power = value * 2 mA
514 output[0x09] = eeprom->max_power;;
516 // Addr 0A: Chip configuration
517 // Bit 7: 0 - reserved
518 // Bit 6: 0 - reserved
519 // Bit 5: 0 - reserved
520 // Bit 4: 1 - Change USB version
521 // Bit 3: 1 - Use the serial number string
522 // Bit 2: 1 - Enable suspend pull downs for lower power
523 // Bit 1: 1 - Out EndPoint is Isochronous
524 // Bit 0: 1 - In EndPoint is Isochronous
527 if (eeprom->in_is_isochronous == 1)
529 if (eeprom->out_is_isochronous == 1)
531 if (eeprom->suspend_pull_downs == 1)
533 if (eeprom->use_serial == 1)
535 if (eeprom->change_usb_version == 1)
542 // Addr 0C: USB version low byte when 0x0A bit 4 is set
543 // Addr 0D: USB version high byte when 0x0A bit 4 is set
544 if (eeprom->change_usb_version == 1) {
545 output[0x0C] = eeprom->usb_version;
546 output[0x0D] = eeprom->usb_version >> 8;
550 // Addr 0E: Offset of the manufacturer string + 0x80
551 output[0x0E] = 0x14 + 0x80;
553 // Addr 0F: Length of manufacturer string
554 output[0x0F] = manufacturer_size*2 + 2;
556 // Addr 10: Offset of the product string + 0x80, calculated later
557 // Addr 11: Length of product string
558 output[0x11] = product_size*2 + 2;
560 // Addr 12: Offset of the serial string + 0x80, calculated later
561 // Addr 13: Length of serial string
562 output[0x13] = serial_size*2 + 2;
565 output[0x14] = manufacturer_size*2 + 2;
566 output[0x15] = 0x03; // type: string
570 // Output manufacturer
571 for (j = 0; j < manufacturer_size; j++) {
572 output[i] = eeprom->manufacturer[j], i++;
573 output[i] = 0x00, i++;
576 // Output product name
577 output[0x10] = i + 0x80; // calculate offset
578 output[i] = product_size*2 + 2, i++;
579 output[i] = 0x03, i++;
580 for (j = 0; j < product_size; j++) {
581 output[i] = eeprom->product[j], i++;
582 output[i] = 0x00, i++;
586 output[0x12] = i + 0x80; // calculate offset
587 output[i] = serial_size*2 + 2, i++;
588 output[i] = 0x03, i++;
589 for (j = 0; j < serial_size; j++) {
590 output[i] = eeprom->serial[j], i++;
591 output[i] = 0x00, i++;
594 // calculate checksum
597 for (i = 0; i < 63; i++) {
599 value += output[(i*2)+1] << 8;
601 checksum = value^checksum;
602 checksum = (checksum << 1) | (checksum >> 15);
605 output[0x7E] = checksum;
606 output[0x7F] = checksum >> 8;
612 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
615 for (i = 0; i < 64; i++) {
616 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_timeout) != 2) {
617 ftdi->error_str = "Reading eeprom failed";
626 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
627 unsigned short usb_val;
630 for (i = 0; i < 64; i++) {
631 usb_val = eeprom[i*2];
632 usb_val += eeprom[(i*2)+1] << 8;
633 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_timeout) != 0) {
634 ftdi->error_str = "Unable to write eeprom";
643 int ftdi_erase_eeprom(struct ftdi_context *ftdi) {
644 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
645 ftdi->error_str = "Unable to erase eeprom";