libftdi: (tomj) new variable: bitbang_mode to support FT2232C's SPI bitbang mode
[libftdi] / ftdi / ftdi.c
1 /***************************************************************************
2                           ftdi.c  -  description
3                              -------------------
4     begin                : Fri Apr 4 2003
5     copyright            : (C) 2003 by Intra2net AG
6     email                : opensource@intra2net.com
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
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;             *
14  *                                                                         *
15  ***************************************************************************/
16
17 #include <usb.h>
18
19 #include "ftdi.h"
20
21 /* ftdi_init return codes:
22    0: all fine
23   -1: couldn't allocate read buffer
24 */
25 int ftdi_init(struct ftdi_context *ftdi) {
26     ftdi->usb_dev = NULL;
27     ftdi->usb_read_timeout = 5000;
28     ftdi->usb_write_timeout = 5000;
29
30     ftdi->baudrate = -1;
31     ftdi->bitbang_enabled = 0;
32
33     ftdi->readbuffer = NULL;
34     ftdi->readbuffer_offset = 0;
35     ftdi->readbuffer_remaining = 0;
36     ftdi->writebuffer_chunksize = 4096;
37
38     ftdi->interface = 0;
39     ftdi->index = 0;
40     ftdi->in_ep = 0x02;
41     ftdi->out_ep = 0x81;
42     ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
43     
44     ftdi->error_str = NULL;
45
46     // all fine. Now allocate the readbuffer
47     return ftdi_read_data_set_chunksize(ftdi, 4096);
48 }
49
50
51 void ftdi_deinit(struct ftdi_context *ftdi) {
52     if (ftdi->readbuffer != NULL) {
53         free(ftdi->readbuffer);
54         ftdi->readbuffer = NULL;
55     }
56 }
57
58
59 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) {
60     ftdi->usb_dev = usb;
61 }
62
63
64 /* ftdi_usb_open return codes:
65    0: all fine
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
71   -6: reset failed
72   -7: set baudrate failed
73 */
74 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) {
75     struct usb_bus *bus;
76     struct usb_device *dev;
77
78     usb_init();
79
80     if (usb_find_busses() < 0) {
81         ftdi->error_str = "usb_find_busses() failed";
82         return -1;
83     }
84
85     if (usb_find_devices() < 0) {
86         ftdi->error_str = "usb_find_devices() failed";
87         return -2;
88     }
89
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);
94                 if (ftdi->usb_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...";
97                         return -5;
98                     }
99
100                     if (ftdi_usb_reset (ftdi) != 0)
101                         return -6;
102
103                     if (ftdi_set_baudrate (ftdi, 9600) != 0)
104                         return -7;
105
106                     return 0;
107                 } else {
108                     ftdi->error_str = "usb_open() failed";
109                     return -4;
110                 }
111             }
112         }
113
114     }
115
116     // device not found
117     return -3;
118 }
119
120
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";
124         return -1;
125     }
126     // Invalidate data in the readbuffer
127     ftdi->readbuffer_offset = 0;
128     ftdi->readbuffer_remaining = 0;
129
130     return 0;
131 }
132
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";
136         return -1;
137     }
138     // Invalidate data in the readbuffer
139     ftdi->readbuffer_offset = 0;
140     ftdi->readbuffer_remaining = 0;
141
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";
144         return -1;
145     }
146
147
148     return 0;
149 }
150
151 /* ftdi_usb_close return codes
152     0: all fine
153    -1: usb_release failed
154    -2: usb_close failed
155 */
156 int ftdi_usb_close(struct ftdi_context *ftdi) {
157     int rtn = 0;
158
159     if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
160         rtn = -1;
161
162     if (usb_close (ftdi->usb_dev) != 0)
163         rtn = -2;
164
165     return rtn;
166 }
167
168
169 /*
170     ftdi_set_baudrate return codes:
171      0: all fine
172     -1: invalid baudrate
173     -2: setting baudrate failed
174 */
175 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) {
176     unsigned short ftdi_baudrate;
177
178     if (ftdi->bitbang_enabled) {
179         baudrate = baudrate*4;
180     }
181
182     switch (baudrate) {
183     case 300:
184         ftdi_baudrate = 0x2710;
185         break;
186     case 600:
187         ftdi_baudrate = 0x1388;
188         break;
189     case 1200:
190         ftdi_baudrate = 0x09C4;
191         break;
192     case 2400:
193         ftdi_baudrate = 0x04E2;
194         break;
195     case 4800:
196         ftdi_baudrate = 0x0271;
197         break;
198     case 9600:
199         ftdi_baudrate = 0x4138;
200         break;
201     case 19200:
202         ftdi_baudrate = 0x809C;
203         break;
204     case 38400:
205         ftdi_baudrate = 0xC04E;
206         break;
207     case 57600:
208         ftdi_baudrate = 0x0034;
209         break;
210     case 115200:
211         ftdi_baudrate = 0x001A;
212         break;
213     case 230400:
214         ftdi_baudrate = 0x000D;
215         break;
216     case 460800:
217         ftdi_baudrate = 0x4006;
218         break;
219     case 921600:
220         ftdi_baudrate = 0x8003;
221         break;
222     default:
223         ftdi->error_str = "Unknown baudrate. Note: bitbang baudrates are automatically multiplied by 4";
224         return -1;
225     }
226
227
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";
230         return -2;
231     }
232
233     ftdi->baudrate = baudrate;
234     return 0;
235 }
236
237
238 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
239     int ret;
240     int offset = 0;
241     int total_written = 0;
242     while (offset < size) {
243         int write_size = ftdi->writebuffer_chunksize;
244
245         if (offset+write_size > size)
246             write_size = size-offset;
247
248         ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
249         if (ret == -1) {
250             ftdi->error_str = "bulk write failed";
251             return -1;
252         }
253         total_written += ret;
254
255         offset += write_size;
256     }
257
258     return total_written;
259 }
260
261
262 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
263     ftdi->writebuffer_chunksize = chunksize;
264     return 0;
265 }
266
267
268 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
269     *chunksize = ftdi->writebuffer_chunksize;
270     return 0;
271 }
272
273
274 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
275     int offset = 0, ret = 1;
276
277     // everything we want is still in the readbuffer?
278     if (size <= ftdi->readbuffer_remaining) {
279         memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
280
281         // Fix offsets
282         ftdi->readbuffer_remaining -= size;
283         ftdi->readbuffer_offset += size;
284
285         /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
286
287         return size;
288     }
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);
292
293         // Fix offset
294         offset += ftdi->readbuffer_remaining;
295     }
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);
302
303         if (ret == -1) {
304             ftdi->error_str = "bulk read failed";
305             return -1;
306         }
307
308         if (ret > 2) {
309             // skip FTDI status bytes.
310             // Maybe stored in the future to enable modem use
311             ftdi->readbuffer_offset += 2;
312             ret -= 2;
313         } else if (ret <= 2) {
314             // no more data to read?
315             return offset;
316         }
317         if (ret > 0) {
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]);
322                 offset += ret;
323
324                 /* Did we read exactly the right amount of bytes? */
325                 if (offset == size)
326                     return offset;
327             } else {
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);
331
332                 ftdi->readbuffer_offset += part_size;
333                 ftdi->readbuffer_remaining = ret-part_size;
334                 offset += part_size;
335
336                 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n", 
337                            part_size, size, offset, ret, ftdi->readbuffer_remaining); */
338
339                 return offset;
340             }
341         }
342     }
343     // never reached
344     return -2;
345 }
346
347
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;
352
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";
356         return -1;
357     }
358
359     ftdi->readbuffer = new_buf;
360     ftdi->readbuffer_chunksize = chunksize;
361
362     return 0;
363 }
364
365
366 int ftdi_readt_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
367     *chunksize = ftdi->readbuffer_chunksize;
368     return 0;
369 }
370
371
372
373 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) {
374     unsigned short usb_val;
375
376     usb_val = bitmask; // low byte: bitmask
377     /* FT2232C: Set bitbang_mode to 2 to enable SPI */
378     usb_val |= (ftdi->bitbang_mode << 8);
379
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?";
382         return -1;
383     }
384     ftdi->bitbang_enabled = 1;
385     return 0;
386 }
387
388
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?";
392         return -1;
393     }
394
395     ftdi->bitbang_enabled = 0;
396     return 0;
397 }
398
399
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";
404         return -1;
405     }
406
407     *pins = (unsigned char)usb_val;
408     return 0;
409 }
410
411
412 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
413     unsigned short usb_val;
414
415     if (latency < 1) {
416         ftdi->error_str = "Latency out of range. Only valid for 1-255";
417         return -1;
418     }
419
420     usb_val = latency;
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";
423         return -2;
424     }
425     return 0;
426 }
427
428
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";
433         return -1;
434     }
435
436     *latency = (unsigned char)usb_val;
437     return 0;
438 }
439
440
441 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
442     eeprom->vendor_id = 0403;
443     eeprom->product_id = 6001;
444
445     eeprom->self_powered = 1;
446     eeprom->remote_wakeup = 1;
447     eeprom->BM_type_chip = 1;
448
449     eeprom->in_is_isochronous = 0;
450     eeprom->out_is_isochronous = 0;
451     eeprom->suspend_pull_downs = 0;
452
453     eeprom->use_serial = 0;
454     eeprom->change_usb_version = 0;
455     eeprom->usb_version = 200;
456     eeprom->max_power = 0;
457
458     eeprom->manufacturer = NULL;
459     eeprom->product = NULL;
460     eeprom->serial = NULL;
461 }
462
463
464 /*
465     ftdi_eeprom_build return codes:
466     positive value: used eeprom size
467     -1: eeprom size (128 bytes) exceeded by custom strings
468 */
469 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
470     unsigned char i, j;
471     unsigned short checksum, value;
472     unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
473     int size_check;
474
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);
481
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;
487
488     // eeprom size exceeded?
489     if (size_check < 0)
490         return (-1);
491
492     // empty eeprom
493     memset (output, 0, 128);
494
495     // Addr 00: Stay 00 00
496     // Addr 02: Vendor ID
497     output[0x02] = eeprom->vendor_id;
498     output[0x03] = eeprom->vendor_id >> 8;
499
500     // Addr 04: Product ID
501     output[0x04] = eeprom->product_id;
502     output[0x05] = eeprom->product_id >> 8;
503
504     // Addr 06: Device release number (0400h for BM features)
505     output[0x06] = 0x00;
506
507     if (eeprom->BM_type_chip == 1)
508         output[0x07] = 0x04;
509     else
510         output[0x07] = 0x02;
511
512     // Addr 08: Config descriptor
513     // Bit 1: remote wakeup if 1
514     // Bit 0: self powered if 1
515     //
516     j = 0;
517     if (eeprom->self_powered == 1)
518         j = j | 1;
519     if (eeprom->remote_wakeup == 1)
520         j = j | 2;
521     output[0x08] = j;
522
523     // Addr 09: Max power consumption: max power = value * 2 mA
524     output[0x09] = eeprom->max_power;
525     ;
526
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
536     //
537     j = 0;
538     if (eeprom->in_is_isochronous == 1)
539         j = j | 1;
540     if (eeprom->out_is_isochronous == 1)
541         j = j | 2;
542     if (eeprom->suspend_pull_downs == 1)
543         j = j | 4;
544     if (eeprom->use_serial == 1)
545         j = j | 8;
546     if (eeprom->change_usb_version == 1)
547         j = j | 16;
548     output[0x0A] = j;
549
550     // Addr 0B: reserved
551     output[0x0B] = 0x00;
552
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;
558     }
559
560
561     // Addr 0E: Offset of the manufacturer string + 0x80
562     output[0x0E] = 0x14 + 0x80;
563
564     // Addr 0F: Length of manufacturer string
565     output[0x0F] = manufacturer_size*2 + 2;
566
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;
570
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;
574
575     // Dynamic content
576     output[0x14] = manufacturer_size*2 + 2;
577     output[0x15] = 0x03; // type: string
578
579     i = 0x16, j = 0;
580
581     // Output manufacturer
582     for (j = 0; j < manufacturer_size; j++) {
583         output[i] = eeprom->manufacturer[j], i++;
584         output[i] = 0x00, i++;
585     }
586
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++;
594     }
595
596     // Output serial
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++;
603     }
604
605     // calculate checksum
606     checksum = 0xAAAA;
607
608     for (i = 0; i < 63; i++) {
609         value = output[i*2];
610         value += output[(i*2)+1] << 8;
611
612         checksum = value^checksum;
613         checksum = (checksum << 1) | (checksum >> 15);
614     }
615
616     output[0x7E] = checksum;
617     output[0x7F] = checksum >> 8;
618
619     return size_check;
620 }
621
622
623 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
624     int i;
625
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";
629             return -1;
630         }
631     }
632
633     return 0;
634 }
635
636
637 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
638     unsigned short usb_val;
639     int i;
640
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";
646             return -1;
647         }
648     }
649
650     return 0;
651 }
652
653
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";
657         return -1;
658     }
659
660     return 0;
661 }