1 /***************************************************************************
5 copyright : (C) 2003 by Intra2net AG
6 email : info@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 int ftdi_init(struct ftdi_context *ftdi) {
23 ftdi->usb_timeout = 5000;
26 ftdi->bitbang_enabled = 0;
28 ftdi->error_str = NULL;
34 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) {
39 /* ftdi_usb_open return codes:
41 -1: usb_find_busses() failed
42 -2: usb_find_devices() failed
43 -3: usb device not found
44 -4: unable to open device
45 -5: unable to claim device
47 -7: set baudrate failed
49 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) {
51 struct usb_device *dev;
55 if (usb_find_busses() < 0) {
56 ftdi->error_str = "usb_find_busses() failed";
60 if (usb_find_devices() < 0) {
61 ftdi->error_str = "usb_find_devices() failed";
65 for (bus = usb_busses; bus; bus = bus->next) {
66 for (dev = bus->devices; dev; dev = dev->next) {
67 if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) {
68 ftdi->usb_dev = usb_open(dev);
70 if (usb_claim_interface(ftdi->usb_dev, 0) != 0) {
71 ftdi->error_str = "unable to claim usb device. You can still use it though...";
75 if (ftdi_usb_reset (ftdi) != 0)
78 if (ftdi_set_baudrate (ftdi, 9600) != 0)
83 ftdi->error_str = "usb_open() failed";
96 int ftdi_usb_reset(struct ftdi_context *ftdi) {
97 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
98 ftdi->error_str = "FTDI reset failed";
105 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) {
106 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, 0, NULL, 0, ftdi->usb_timeout) != 0) {
107 ftdi->error_str = "FTDI purge of RX buffer failed";
111 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, 0, NULL, 0, ftdi->usb_timeout) != 0) {
112 ftdi->error_str = "FTDI purge of TX buffer failed";
119 /* ftdi_usb_close return codes
121 -1: usb_release failed
124 int ftdi_usb_close(struct ftdi_context *ftdi) {
127 if (usb_release_interface(ftdi->usb_dev, 0) != 0)
130 if (usb_close (ftdi->usb_dev) != 0)
138 ftdi_set_baudrate return codes:
141 -2: setting baudrate failed
143 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) {
144 unsigned short ftdi_baudrate;
146 if (ftdi->bitbang_enabled) {
147 baudrate = baudrate*4;
152 ftdi_baudrate = 0x2710;
155 ftdi_baudrate = 0x1388;
158 ftdi_baudrate = 0x09C4;
161 ftdi_baudrate = 0x04E2;
164 ftdi_baudrate = 0x0271;
167 ftdi_baudrate = 0x4138;
170 ftdi_baudrate = 0x809C;
173 ftdi_baudrate = 0xC04E;
176 ftdi_baudrate = 0x0034;
179 ftdi_baudrate = 0x001A;
182 ftdi_baudrate = 0x000D;
185 ftdi_baudrate = 0x4006;
188 ftdi_baudrate = 0x8003;
191 ftdi->error_str = "Unknown baudrate. Note: bitbang baudrates are automatically multiplied by 4";
195 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, ftdi_baudrate, 0, NULL, 0, ftdi->usb_timeout) != 0) {
196 ftdi->error_str = "Setting new baudrate failed";
200 ftdi->baudrate = baudrate;
205 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
208 while (offset < size) {
211 if (offset+write_size > size)
212 write_size = size-offset;
214 ret=usb_bulk_write(ftdi->usb_dev, 2, buf+offset, write_size, ftdi->usb_timeout);
216 ftdi->error_str = "bulk write failed";
220 offset += write_size;
227 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
228 static unsigned char readbuf[64];
232 if (size != 0 && size % 64 != 0) {
233 ftdi->error_str = "Sorry, read buffer size must currently be a multiple (1x, 2x, 3x...) of 64";
237 while (offset < size && ret > 0) {
238 ret = usb_bulk_read (ftdi->usb_dev, 0x81, readbuf, 64, ftdi->usb_timeout);
239 // Skip FTDI status bytes
244 memcpy (buf+offset, readbuf+2, ret);
248 ftdi->error_str = "bulk read failed";
259 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) {
260 unsigned short usb_val;
262 usb_val = bitmask; // low byte: bitmask
263 usb_val += 1 << 8; // high byte: enable flag
264 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
265 ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?";
269 ftdi->bitbang_enabled = 1;
274 int ftdi_disable_bitbang(struct ftdi_context *ftdi) {
275 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
276 ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?";
280 ftdi->bitbang_enabled = 0;
285 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) {
286 unsigned short usb_val;
287 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
288 ftdi->error_str = "Read pins failed";
292 *pins = (unsigned char)usb_val;
297 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
298 unsigned short usb_val;
301 ftdi->error_str = "Latency out of range. Only valid for 1-255";
305 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
306 ftdi->error_str = "Unable to set latency timer";
313 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) {
314 unsigned short usb_val;
315 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x09, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
316 ftdi->error_str = "Reading latency timer failed";
320 *latency = (unsigned char)usb_val;
325 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
326 eeprom->vendor_id = 0403;
327 eeprom->product_id = 6001;
329 eeprom->self_powered = 1;
330 eeprom->remote_wakeup = 1;
331 eeprom->BM_type_chip = 1;
333 eeprom->in_is_isochronous = 0;
334 eeprom->out_is_isochronous = 0;
335 eeprom->suspend_pull_downs = 0;
337 eeprom->use_serial = 0;
338 eeprom->change_usb_version = 0;
339 eeprom->usb_version = 200;
340 eeprom->max_power = 0;
342 eeprom->manufacturer = NULL;
343 eeprom->product = NULL;
344 eeprom->serial = NULL;
349 ftdi_eeprom_build return codes:
350 positive value: used eeprom size
351 -1: eeprom size (128 bytes) exceeded by custom strings
353 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
355 unsigned short checksum, value;
356 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
359 if (eeprom->manufacturer != NULL)
360 manufacturer_size = strlen(eeprom->manufacturer);
361 if (eeprom->product != NULL)
362 product_size = strlen(eeprom->product);
363 if (eeprom->serial != NULL)
364 serial_size = strlen(eeprom->serial);
366 size_check = 128; // eeprom is 128 bytes
367 size_check -= 28; // 28 are always in use (fixed)
368 size_check -= manufacturer_size*2;
369 size_check -= product_size*2;
370 size_check -= serial_size*2;
372 // eeprom size exceeded?
377 memset (output, 0, 128);
379 // Addr 00: Stay 00 00
380 // Addr 02: Vendor ID
381 output[0x02] = eeprom->vendor_id;
382 output[0x03] = eeprom->vendor_id >> 8;
384 // Addr 04: Product ID
385 output[0x04] = eeprom->product_id;
386 output[0x05] = eeprom->product_id >> 8;
388 // Addr 06: Device release number (0400h for BM features)
391 if (eeprom->BM_type_chip == 1)
396 // Addr 08: Config descriptor
397 // Bit 1: remote wakeup if 1
398 // Bit 0: self powered if 1
401 if (eeprom->self_powered == 1)
403 if (eeprom->remote_wakeup == 1)
407 // Addr 09: Max power consumption: max power = value * 2 mA
408 output[0x09] = eeprom->max_power;;
410 // Addr 0A: Chip configuration
411 // Bit 7: 0 - reserved
412 // Bit 6: 0 - reserved
413 // Bit 5: 0 - reserved
414 // Bit 4: 1 - Change USB version
415 // Bit 3: 1 - Use the serial number string
416 // Bit 2: 1 - Enable suspend pull downs for lower power
417 // Bit 1: 1 - Out EndPoint is Isochronous
418 // Bit 0: 1 - In EndPoint is Isochronous
421 if (eeprom->in_is_isochronous == 1)
423 if (eeprom->out_is_isochronous == 1)
425 if (eeprom->suspend_pull_downs == 1)
427 if (eeprom->use_serial == 1)
429 if (eeprom->change_usb_version == 1)
436 // Addr 0C: USB version low byte when 0x0A bit 4 is set
437 // Addr 0D: USB version high byte when 0x0A bit 4 is set
438 if (eeprom->change_usb_version == 1) {
439 output[0x0C] = eeprom->usb_version;
440 output[0x0D] = eeprom->usb_version >> 8;
444 // Addr 0E: Offset of the manufacturer string + 0x80
445 output[0x0E] = 0x14 + 0x80;
447 // Addr 0F: Length of manufacturer string
448 output[0x0F] = manufacturer_size*2 + 2;
450 // Addr 10: Offset of the product string + 0x80, calculated later
451 // Addr 11: Length of product string
452 output[0x11] = product_size*2 + 2;
454 // Addr 12: Offset of the serial string + 0x80, calculated later
455 // Addr 13: Length of serial string
456 output[0x13] = serial_size*2 + 2;
459 output[0x14] = manufacturer_size*2 + 2;
460 output[0x15] = 0x03; // type: string
464 // Output manufacturer
465 for (j = 0; j < manufacturer_size; j++) {
466 output[i] = eeprom->manufacturer[j], i++;
467 output[i] = 0x00, i++;
470 // Output product name
471 output[0x10] = i + 0x80; // calculate offset
472 output[i] = product_size*2 + 2, i++;
473 output[i] = 0x03, i++;
474 for (j = 0; j < product_size; j++) {
475 output[i] = eeprom->product[j], i++;
476 output[i] = 0x00, i++;
480 output[0x12] = i + 0x80; // calculate offset
481 output[i] = serial_size*2 + 2, i++;
482 output[i] = 0x03, i++;
483 for (j = 0; j < serial_size; j++) {
484 output[i] = eeprom->serial[j], i++;
485 output[i] = 0x00, i++;
488 // calculate checksum
491 for (i = 0; i < 63; i++) {
493 value += output[(i*2)+1] << 8;
495 checksum = value^checksum;
496 checksum = (checksum << 1) | (checksum >> 15);
499 output[0x7E] = checksum;
500 output[0x7F] = checksum >> 8;
506 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
509 for (i = 0; i < 64; i++) {
510 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_timeout) != 2) {
511 ftdi->error_str = "Reading eeprom failed";
520 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
521 unsigned short usb_val;
524 for (i = 0; i < 64; i++) {
525 usb_val = eeprom[i*2];
526 usb_val += eeprom[(i*2)+1] << 8;
527 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_timeout) != 0) {
528 ftdi->error_str = "Unable to write eeprom";
537 int ftdi_erase_eeprom(struct ftdi_context *ftdi) {
538 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
539 ftdi->error_str = "Unable to erase eeprom";