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";
306 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
307 ftdi->error_str = "Unable to set latency timer";
314 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) {
315 unsigned short usb_val;
316 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
317 ftdi->error_str = "Reading latency timer failed";
321 *latency = (unsigned char)usb_val;
326 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
327 eeprom->vendor_id = 0403;
328 eeprom->product_id = 6001;
330 eeprom->self_powered = 1;
331 eeprom->remote_wakeup = 1;
332 eeprom->BM_type_chip = 1;
334 eeprom->in_is_isochronous = 0;
335 eeprom->out_is_isochronous = 0;
336 eeprom->suspend_pull_downs = 0;
338 eeprom->use_serial = 0;
339 eeprom->change_usb_version = 0;
340 eeprom->usb_version = 200;
341 eeprom->max_power = 0;
343 eeprom->manufacturer = NULL;
344 eeprom->product = NULL;
345 eeprom->serial = NULL;
350 ftdi_eeprom_build return codes:
351 positive value: used eeprom size
352 -1: eeprom size (128 bytes) exceeded by custom strings
354 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
356 unsigned short checksum, value;
357 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
360 if (eeprom->manufacturer != NULL)
361 manufacturer_size = strlen(eeprom->manufacturer);
362 if (eeprom->product != NULL)
363 product_size = strlen(eeprom->product);
364 if (eeprom->serial != NULL)
365 serial_size = strlen(eeprom->serial);
367 size_check = 128; // eeprom is 128 bytes
368 size_check -= 28; // 28 are always in use (fixed)
369 size_check -= manufacturer_size*2;
370 size_check -= product_size*2;
371 size_check -= serial_size*2;
373 // eeprom size exceeded?
378 memset (output, 0, 128);
380 // Addr 00: Stay 00 00
381 // Addr 02: Vendor ID
382 output[0x02] = eeprom->vendor_id;
383 output[0x03] = eeprom->vendor_id >> 8;
385 // Addr 04: Product ID
386 output[0x04] = eeprom->product_id;
387 output[0x05] = eeprom->product_id >> 8;
389 // Addr 06: Device release number (0400h for BM features)
392 if (eeprom->BM_type_chip == 1)
397 // Addr 08: Config descriptor
398 // Bit 1: remote wakeup if 1
399 // Bit 0: self powered if 1
402 if (eeprom->self_powered == 1)
404 if (eeprom->remote_wakeup == 1)
408 // Addr 09: Max power consumption: max power = value * 2 mA
409 output[0x09] = eeprom->max_power;;
411 // Addr 0A: Chip configuration
412 // Bit 7: 0 - reserved
413 // Bit 6: 0 - reserved
414 // Bit 5: 0 - reserved
415 // Bit 4: 1 - Change USB version
416 // Bit 3: 1 - Use the serial number string
417 // Bit 2: 1 - Enable suspend pull downs for lower power
418 // Bit 1: 1 - Out EndPoint is Isochronous
419 // Bit 0: 1 - In EndPoint is Isochronous
422 if (eeprom->in_is_isochronous == 1)
424 if (eeprom->out_is_isochronous == 1)
426 if (eeprom->suspend_pull_downs == 1)
428 if (eeprom->use_serial == 1)
430 if (eeprom->change_usb_version == 1)
437 // Addr 0C: USB version low byte when 0x0A bit 4 is set
438 // Addr 0D: USB version high byte when 0x0A bit 4 is set
439 if (eeprom->change_usb_version == 1) {
440 output[0x0C] = eeprom->usb_version;
441 output[0x0D] = eeprom->usb_version >> 8;
445 // Addr 0E: Offset of the manufacturer string + 0x80
446 output[0x0E] = 0x14 + 0x80;
448 // Addr 0F: Length of manufacturer string
449 output[0x0F] = manufacturer_size*2 + 2;
451 // Addr 10: Offset of the product string + 0x80, calculated later
452 // Addr 11: Length of product string
453 output[0x11] = product_size*2 + 2;
455 // Addr 12: Offset of the serial string + 0x80, calculated later
456 // Addr 13: Length of serial string
457 output[0x13] = serial_size*2 + 2;
460 output[0x14] = manufacturer_size*2 + 2;
461 output[0x15] = 0x03; // type: string
465 // Output manufacturer
466 for (j = 0; j < manufacturer_size; j++) {
467 output[i] = eeprom->manufacturer[j], i++;
468 output[i] = 0x00, i++;
471 // Output product name
472 output[0x10] = i + 0x80; // calculate offset
473 output[i] = product_size*2 + 2, i++;
474 output[i] = 0x03, i++;
475 for (j = 0; j < product_size; j++) {
476 output[i] = eeprom->product[j], i++;
477 output[i] = 0x00, i++;
481 output[0x12] = i + 0x80; // calculate offset
482 output[i] = serial_size*2 + 2, i++;
483 output[i] = 0x03, i++;
484 for (j = 0; j < serial_size; j++) {
485 output[i] = eeprom->serial[j], i++;
486 output[i] = 0x00, i++;
489 // calculate checksum
492 for (i = 0; i < 63; i++) {
494 value += output[(i*2)+1] << 8;
496 checksum = value^checksum;
497 checksum = (checksum << 1) | (checksum >> 15);
500 output[0x7E] = checksum;
501 output[0x7F] = checksum >> 8;
507 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
510 for (i = 0; i < 64; i++) {
511 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_timeout) != 2) {
512 ftdi->error_str = "Reading eeprom failed";
521 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
522 unsigned short usb_val;
525 for (i = 0; i < 64; i++) {
526 usb_val = eeprom[i*2];
527 usb_val += eeprom[(i*2)+1] << 8;
528 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_timeout) != 0) {
529 ftdi->error_str = "Unable to write eeprom";
538 int ftdi_erase_eeprom(struct ftdi_context *ftdi) {
539 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
540 ftdi->error_str = "Unable to erase eeprom";