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_read_timeout = 5000;
28 ftdi->usb_write_timeout = 5000;
31 ftdi->bitbang_enabled = 0;
33 ftdi->readbuffer = NULL;
34 ftdi->readbuffer_offset = 0;
35 ftdi->readbuffer_remaining = 0;
36 ftdi->writebuffer_chunksize = 4096;
42 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
44 ftdi->error_str = NULL;
46 // all fine. Now allocate the readbuffer
47 return ftdi_read_data_set_chunksize(ftdi, 4096);
51 void ftdi_deinit(struct ftdi_context *ftdi) {
52 if (ftdi->readbuffer != NULL) {
53 free(ftdi->readbuffer);
54 ftdi->readbuffer = NULL;
59 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) {
64 /* ftdi_usb_open return codes:
66 -1: usb_find_busses() failed
67 -2: usb_find_devices() failed
68 -3: usb device not found
69 -4: unable to open device
70 -5: unable to claim device
72 -7: set baudrate failed
74 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) {
76 struct usb_device *dev;
80 if (usb_find_busses() < 0) {
81 ftdi->error_str = "usb_find_busses() failed";
85 if (usb_find_devices() < 0) {
86 ftdi->error_str = "usb_find_devices() failed";
90 for (bus = usb_busses; bus; bus = bus->next) {
91 for (dev = bus->devices; dev; dev = dev->next) {
92 if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) {
93 ftdi->usb_dev = usb_open(dev);
95 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
96 ftdi->error_str = "unable to claim usb device. You can still use it though...";
100 if (ftdi_usb_reset (ftdi) != 0)
103 if (ftdi_set_baudrate (ftdi, 9600) != 0)
108 ftdi->error_str = "usb_open() failed";
121 int ftdi_usb_reset(struct ftdi_context *ftdi) {
122 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
123 ftdi->error_str = "FTDI reset failed";
126 // Invalidate data in the readbuffer
127 ftdi->readbuffer_offset = 0;
128 ftdi->readbuffer_remaining = 0;
133 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) {
134 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
135 ftdi->error_str = "FTDI purge of RX buffer failed";
138 // Invalidate data in the readbuffer
139 ftdi->readbuffer_offset = 0;
140 ftdi->readbuffer_remaining = 0;
142 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
143 ftdi->error_str = "FTDI purge of TX buffer failed";
151 /* ftdi_usb_close return codes
153 -1: usb_release failed
156 int ftdi_usb_close(struct ftdi_context *ftdi) {
159 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
162 if (usb_close (ftdi->usb_dev) != 0)
170 ftdi_set_baudrate return codes:
173 -2: setting baudrate failed
175 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) {
176 unsigned short ftdi_baudrate;
178 if (ftdi->bitbang_enabled) {
179 baudrate = baudrate*4;
184 ftdi_baudrate = 0x2710;
187 ftdi_baudrate = 0x1388;
190 ftdi_baudrate = 0x09C4;
193 ftdi_baudrate = 0x04E2;
196 ftdi_baudrate = 0x0271;
199 ftdi_baudrate = 0x4138;
202 ftdi_baudrate = 0x809C;
205 ftdi_baudrate = 0xC04E;
208 ftdi_baudrate = 0x0034;
211 ftdi_baudrate = 0x001A;
214 ftdi_baudrate = 0x000D;
217 ftdi_baudrate = 0x4006;
220 ftdi_baudrate = 0x8003;
223 ftdi->error_str = "Unknown baudrate. Note: bitbang baudrates are automatically multiplied by 4";
228 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, ftdi_baudrate, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
229 ftdi->error_str = "Setting new baudrate failed";
233 ftdi->baudrate = baudrate;
238 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
241 int total_written = 0;
242 while (offset < size) {
243 int write_size = ftdi->writebuffer_chunksize;
245 if (offset+write_size > size)
246 write_size = size-offset;
248 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
250 ftdi->error_str = "bulk write failed";
253 total_written += ret;
255 offset += write_size;
258 return total_written;
262 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
263 ftdi->writebuffer_chunksize = chunksize;
268 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
269 *chunksize = ftdi->writebuffer_chunksize;
274 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
275 int offset = 0, ret = 1;
277 // everything we want is still in the readbuffer?
278 if (size <= ftdi->readbuffer_remaining) {
279 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
282 ftdi->readbuffer_remaining -= size;
283 ftdi->readbuffer_offset += size;
285 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
289 // something still in the readbuffer, but not enough to satisfy 'size'?
290 if (ftdi->readbuffer_remaining != 0) {
291 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
294 offset += ftdi->readbuffer_remaining;
296 // do the actual USB read
297 while (offset < size && ret > 0) {
298 ftdi->readbuffer_remaining = 0;
299 ftdi->readbuffer_offset = 0;
300 /* returns how much received */
301 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
304 ftdi->error_str = "bulk read failed";
309 // skip FTDI status bytes.
310 // Maybe stored in the future to enable modem use
311 ftdi->readbuffer_offset += 2;
313 } else if (ret <= 2) {
314 // no more data to read?
318 // data still fits in buf?
319 if (offset+ret <= size) {
320 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
321 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
324 /* Did we read exactly the right amount of bytes? */
328 // only copy part of the data or size <= readbuffer_chunksize
329 int part_size = size-offset;
330 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
332 ftdi->readbuffer_offset += part_size;
333 ftdi->readbuffer_remaining = ret-part_size;
336 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
337 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
348 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
349 // Invalidate all remaining data
350 ftdi->readbuffer_offset = 0;
351 ftdi->readbuffer_remaining = 0;
353 unsigned char *new_buf;
354 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) {
355 ftdi->error_str = "out of memory for readbuffer";
359 ftdi->readbuffer = new_buf;
360 ftdi->readbuffer_chunksize = chunksize;
366 int ftdi_readt_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
367 *chunksize = ftdi->readbuffer_chunksize;
373 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) {
374 unsigned short usb_val;
376 usb_val = bitmask; // low byte: bitmask
377 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
378 usb_val |= (ftdi->bitbang_mode << 8);
380 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
381 ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?";
384 ftdi->bitbang_enabled = 1;
389 int ftdi_disable_bitbang(struct ftdi_context *ftdi) {
390 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
391 ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?";
395 ftdi->bitbang_enabled = 0;
400 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) {
401 unsigned short usb_val;
402 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) {
403 ftdi->error_str = "Read pins failed";
407 *pins = (unsigned char)usb_val;
412 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
413 unsigned short usb_val;
416 ftdi->error_str = "Latency out of range. Only valid for 1-255";
421 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
422 ftdi->error_str = "Unable to set latency timer";
429 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) {
430 unsigned short usb_val;
431 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) {
432 ftdi->error_str = "Reading latency timer failed";
436 *latency = (unsigned char)usb_val;
441 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
442 eeprom->vendor_id = 0403;
443 eeprom->product_id = 6001;
445 eeprom->self_powered = 1;
446 eeprom->remote_wakeup = 1;
447 eeprom->BM_type_chip = 1;
449 eeprom->in_is_isochronous = 0;
450 eeprom->out_is_isochronous = 0;
451 eeprom->suspend_pull_downs = 0;
453 eeprom->use_serial = 0;
454 eeprom->change_usb_version = 0;
455 eeprom->usb_version = 200;
456 eeprom->max_power = 0;
458 eeprom->manufacturer = NULL;
459 eeprom->product = NULL;
460 eeprom->serial = NULL;
465 ftdi_eeprom_build return codes:
466 positive value: used eeprom size
467 -1: eeprom size (128 bytes) exceeded by custom strings
469 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
471 unsigned short checksum, value;
472 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
475 if (eeprom->manufacturer != NULL)
476 manufacturer_size = strlen(eeprom->manufacturer);
477 if (eeprom->product != NULL)
478 product_size = strlen(eeprom->product);
479 if (eeprom->serial != NULL)
480 serial_size = strlen(eeprom->serial);
482 size_check = 128; // eeprom is 128 bytes
483 size_check -= 28; // 28 are always in use (fixed)
484 size_check -= manufacturer_size*2;
485 size_check -= product_size*2;
486 size_check -= serial_size*2;
488 // eeprom size exceeded?
493 memset (output, 0, 128);
495 // Addr 00: Stay 00 00
496 // Addr 02: Vendor ID
497 output[0x02] = eeprom->vendor_id;
498 output[0x03] = eeprom->vendor_id >> 8;
500 // Addr 04: Product ID
501 output[0x04] = eeprom->product_id;
502 output[0x05] = eeprom->product_id >> 8;
504 // Addr 06: Device release number (0400h for BM features)
507 if (eeprom->BM_type_chip == 1)
512 // Addr 08: Config descriptor
513 // Bit 1: remote wakeup if 1
514 // Bit 0: self powered if 1
517 if (eeprom->self_powered == 1)
519 if (eeprom->remote_wakeup == 1)
523 // Addr 09: Max power consumption: max power = value * 2 mA
524 output[0x09] = eeprom->max_power;
527 // Addr 0A: Chip configuration
528 // Bit 7: 0 - reserved
529 // Bit 6: 0 - reserved
530 // Bit 5: 0 - reserved
531 // Bit 4: 1 - Change USB version
532 // Bit 3: 1 - Use the serial number string
533 // Bit 2: 1 - Enable suspend pull downs for lower power
534 // Bit 1: 1 - Out EndPoint is Isochronous
535 // Bit 0: 1 - In EndPoint is Isochronous
538 if (eeprom->in_is_isochronous == 1)
540 if (eeprom->out_is_isochronous == 1)
542 if (eeprom->suspend_pull_downs == 1)
544 if (eeprom->use_serial == 1)
546 if (eeprom->change_usb_version == 1)
553 // Addr 0C: USB version low byte when 0x0A bit 4 is set
554 // Addr 0D: USB version high byte when 0x0A bit 4 is set
555 if (eeprom->change_usb_version == 1) {
556 output[0x0C] = eeprom->usb_version;
557 output[0x0D] = eeprom->usb_version >> 8;
561 // Addr 0E: Offset of the manufacturer string + 0x80
562 output[0x0E] = 0x14 + 0x80;
564 // Addr 0F: Length of manufacturer string
565 output[0x0F] = manufacturer_size*2 + 2;
567 // Addr 10: Offset of the product string + 0x80, calculated later
568 // Addr 11: Length of product string
569 output[0x11] = product_size*2 + 2;
571 // Addr 12: Offset of the serial string + 0x80, calculated later
572 // Addr 13: Length of serial string
573 output[0x13] = serial_size*2 + 2;
576 output[0x14] = manufacturer_size*2 + 2;
577 output[0x15] = 0x03; // type: string
581 // Output manufacturer
582 for (j = 0; j < manufacturer_size; j++) {
583 output[i] = eeprom->manufacturer[j], i++;
584 output[i] = 0x00, i++;
587 // Output product name
588 output[0x10] = i + 0x80; // calculate offset
589 output[i] = product_size*2 + 2, i++;
590 output[i] = 0x03, i++;
591 for (j = 0; j < product_size; j++) {
592 output[i] = eeprom->product[j], i++;
593 output[i] = 0x00, i++;
597 output[0x12] = i + 0x80; // calculate offset
598 output[i] = serial_size*2 + 2, i++;
599 output[i] = 0x03, i++;
600 for (j = 0; j < serial_size; j++) {
601 output[i] = eeprom->serial[j], i++;
602 output[i] = 0x00, i++;
605 // calculate checksum
608 for (i = 0; i < 63; i++) {
610 value += output[(i*2)+1] << 8;
612 checksum = value^checksum;
613 checksum = (checksum << 1) | (checksum >> 15);
616 output[0x7E] = checksum;
617 output[0x7F] = checksum >> 8;
623 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
626 for (i = 0; i < 64; i++) {
627 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) {
628 ftdi->error_str = "Reading eeprom failed";
637 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
638 unsigned short usb_val;
641 for (i = 0; i < 64; i++) {
642 usb_val = eeprom[i*2];
643 usb_val += eeprom[(i*2)+1] << 8;
644 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) {
645 ftdi->error_str = "Unable to write eeprom";
654 int ftdi_erase_eeprom(struct ftdi_context *ftdi) {
655 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) {
656 ftdi->error_str = "Unable to erase eeprom";