indent before checkin
[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_timeout = 5000;
28
29     ftdi->baudrate = -1;
30     ftdi->bitbang_enabled = 0;
31
32     ftdi->readbuffer = NULL;
33     ftdi->readbuffer_offset = 0;
34     ftdi->readbuffer_remaining = 0;
35     ftdi->writebuffer_chunksize = 4096;
36
37     ftdi->error_str = NULL;
38
39     // all fine. Now allocate the readbuffer
40     return ftdi_read_data_set_chunksize(ftdi, 4096);
41 }
42
43
44 void ftdi_deinit(struct ftdi_context *ftdi) {
45     if (ftdi->readbuffer != NULL) {
46         free(ftdi->readbuffer);
47         ftdi->readbuffer = NULL;
48     }
49 }
50
51
52 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) {
53     ftdi->usb_dev = usb;
54 }
55
56
57 /* ftdi_usb_open return codes:
58    0: all fine
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
64   -6: reset failed
65   -7: set baudrate failed
66 */
67 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) {
68     struct usb_bus *bus;
69     struct usb_device *dev;
70
71     usb_init();
72
73     if (usb_find_busses() < 0) {
74         ftdi->error_str = "usb_find_busses() failed";
75         return -1;
76     }
77
78     if (usb_find_devices() < 0) {
79         ftdi->error_str = "usb_find_devices() failed";
80         return -2;
81     }
82
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);
87                 if (ftdi->usb_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...";
90                         return -5;
91                     }
92
93                     if (ftdi_usb_reset (ftdi) != 0)
94                         return -6;
95
96                     if (ftdi_set_baudrate (ftdi, 9600) != 0)
97                         return -7;
98
99                     return 0;
100                 } else {
101                     ftdi->error_str = "usb_open() failed";
102                     return -4;
103                 }
104             }
105         }
106
107     }
108
109     // device not found
110     return -3;
111 }
112
113
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";
117         return -1;
118     }
119
120     ftdi->readbuffer_offset = 0;
121     ftdi->readbuffer_remaining = 0;
122
123     return 0;
124 }
125
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";
129         return -1;
130     }
131
132     ftdi->readbuffer_offset = 0;
133     ftdi->readbuffer_remaining = 0;
134
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";
137         return -1;
138     }
139
140     return 0;
141 }
142
143 /* ftdi_usb_close return codes
144     0: all fine
145    -1: usb_release failed
146    -2: usb_close failed
147 */
148 int ftdi_usb_close(struct ftdi_context *ftdi) {
149     int rtn = 0;
150
151     if (usb_release_interface(ftdi->usb_dev, 0) != 0)
152         rtn = -1;
153
154     if (usb_close (ftdi->usb_dev) != 0)
155         rtn = -2;
156
157     return rtn;
158 }
159
160
161 /*
162     ftdi_set_baudrate return codes:
163      0: all fine
164     -1: invalid baudrate
165     -2: setting baudrate failed
166 */
167 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) {
168     unsigned short ftdi_baudrate;
169
170     if (ftdi->bitbang_enabled) {
171         baudrate = baudrate*4;
172     }
173
174     switch (baudrate) {
175     case 300:
176         ftdi_baudrate = 0x2710;
177         break;
178     case 600:
179         ftdi_baudrate = 0x1388;
180         break;
181     case 1200:
182         ftdi_baudrate = 0x09C4;
183         break;
184     case 2400:
185         ftdi_baudrate = 0x04E2;
186         break;
187     case 4800:
188         ftdi_baudrate = 0x0271;
189         break;
190     case 9600:
191         ftdi_baudrate = 0x4138;
192         break;
193     case 19200:
194         ftdi_baudrate = 0x809C;
195         break;
196     case 38400:
197         ftdi_baudrate = 0xC04E;
198         break;
199     case 57600:
200         ftdi_baudrate = 0x0034;
201         break;
202     case 115200:
203         ftdi_baudrate = 0x001A;
204         break;
205     case 230400:
206         ftdi_baudrate = 0x000D;
207         break;
208     case 460800:
209         ftdi_baudrate = 0x4006;
210         break;
211     case 921600:
212         ftdi_baudrate = 0x8003;
213         break;
214     default:
215         ftdi->error_str = "Unknown baudrate. Note: bitbang baudrates are automatically multiplied by 4";
216         return -1;
217     }
218
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";
221         return -2;
222     }
223
224     ftdi->baudrate = baudrate;
225     return 0;
226 }
227
228
229 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
230     int ret;
231     int offset = 0;
232     while (offset < size) {
233         int write_size = ftdi->writebuffer_chunksize;
234
235         if (offset+write_size > size)
236             write_size = size-offset;
237
238         ret=usb_bulk_write(ftdi->usb_dev, 2, buf+offset, write_size, ftdi->usb_timeout);
239         if (ret == -1) {
240             ftdi->error_str = "bulk write failed";
241             return -1;
242         }
243
244         offset += write_size;
245     }
246
247     return 0;
248 }
249
250
251 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
252     ftdi->writebuffer_chunksize = chunksize;
253     return 0;
254 }
255
256
257 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
258     *chunksize = ftdi->writebuffer_chunksize;
259     return 0;
260 }
261
262
263 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
264     int offset = 0, ret = 1;
265
266     // everything we want is still in the readbuffer?
267     if (size <= ftdi->readbuffer_remaining) {
268         memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
269
270         // Fix offsets
271         ftdi->readbuffer_remaining -= size;
272         ftdi->readbuffer_offset += size;
273
274         // printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining);
275
276         return size;
277     }
278
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);
282
283         // printf("Got bytes from buffer: %d\n", ftdi->readbuffer_remaining);
284
285         // Fix offset
286         offset += ftdi->readbuffer_remaining;
287     }
288
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);
294
295         if (ret == -1) {
296             ftdi->error_str = "bulk read failed";
297             return -1;
298         }
299
300         if (ret > 2) {
301             // skip FTDI status bytes.
302             // Maybe stored in the future to enable modem use
303             ftdi->readbuffer_offset += 2;
304             ret -= 2;
305         } else if (ret <= 2) {
306             // no more data to read?
307             return offset;
308         }
309
310         if (ret > 0) {
311             // data still fits in buf?
312             if (offset+ret <= size) {
313                 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
314                 offset += ret;
315
316                 if (offset == size)
317                     return offset;
318             } else {
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);
322
323                 ftdi->readbuffer_offset += part_size;
324                 ftdi->readbuffer_remaining = ret-part_size;
325                 offset += part_size;
326
327                 // printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n", part_size, size, offset, ret, ftdi->readbuffer_remaining);
328
329                 return offset;
330             }
331         }
332     }
333
334     // never reached
335     return -2;
336 }
337
338
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;
343
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";
347         return -1;
348     }
349
350     ftdi->readbuffer = new_buf;
351     ftdi->readbuffer_chunksize = chunksize;
352
353     return 0;
354 }
355
356
357 int ftdi_readt_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
358     *chunksize = ftdi->readbuffer_chunksize;
359     return 0;
360 }
361
362
363
364 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) {
365     unsigned short usb_val;
366
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?";
371         return -1;
372     }
373
374     ftdi->bitbang_enabled = 1;
375     return 0;
376 }
377
378
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?";
382         return -1;
383     }
384
385     ftdi->bitbang_enabled = 0;
386     return 0;
387 }
388
389
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";
394         return -1;
395     }
396
397     *pins = (unsigned char)usb_val;
398     return 0;
399 }
400
401
402 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
403     unsigned short usb_val;
404
405     if (latency < 1) {
406         ftdi->error_str = "Latency out of range. Only valid for 1-255";
407         return -1;
408     }
409
410     usb_val = latency;
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";
413         return -2;
414     }
415     return 0;
416 }
417
418
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";
423         return -1;
424     }
425
426     *latency = (unsigned char)usb_val;
427     return 0;
428 }
429
430
431 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
432     eeprom->vendor_id = 0403;
433     eeprom->product_id = 6001;
434
435     eeprom->self_powered = 1;
436     eeprom->remote_wakeup = 1;
437     eeprom->BM_type_chip = 1;
438
439     eeprom->in_is_isochronous = 0;
440     eeprom->out_is_isochronous = 0;
441     eeprom->suspend_pull_downs = 0;
442
443     eeprom->use_serial = 0;
444     eeprom->change_usb_version = 0;
445     eeprom->usb_version = 200;
446     eeprom->max_power = 0;
447
448     eeprom->manufacturer = NULL;
449     eeprom->product = NULL;
450     eeprom->serial = NULL;
451 }
452
453
454 /*
455     ftdi_eeprom_build return codes:
456     positive value: used eeprom size
457     -1: eeprom size (128 bytes) exceeded by custom strings
458 */
459 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
460     unsigned char i, j;
461     unsigned short checksum, value;
462     unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
463     int size_check;
464
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);
471
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;
477
478     // eeprom size exceeded?
479     if (size_check < 0)
480         return (-1);
481
482     // empty eeprom
483     memset (output, 0, 128);
484
485     // Addr 00: Stay 00 00
486     // Addr 02: Vendor ID
487     output[0x02] = eeprom->vendor_id;
488     output[0x03] = eeprom->vendor_id >> 8;
489
490     // Addr 04: Product ID
491     output[0x04] = eeprom->product_id;
492     output[0x05] = eeprom->product_id >> 8;
493
494     // Addr 06: Device release number (0400h for BM features)
495     output[0x06] = 0x00;
496
497     if (eeprom->BM_type_chip == 1)
498         output[0x07] = 0x04;
499     else
500         output[0x07] = 0x02;
501
502     // Addr 08: Config descriptor
503     // Bit 1: remote wakeup if 1
504     // Bit 0: self powered if 1
505     //
506     j = 0;
507     if (eeprom->self_powered == 1)
508         j = j | 1;
509     if (eeprom->remote_wakeup == 1)
510         j = j | 2;
511     output[0x08] = j;
512
513     // Addr 09: Max power consumption: max power = value * 2 mA
514     output[0x09] = eeprom->max_power;
515     ;
516
517     // Addr 0A: Chip configuration
518     // Bit 7: 0 - reserved
519     // Bit 6: 0 - reserved
520     // Bit 5: 0 - reserved
521     // Bit 4: 1 - Change USB version
522     // Bit 3: 1 - Use the serial number string
523     // Bit 2: 1 - Enable suspend pull downs for lower power
524     // Bit 1: 1 - Out EndPoint is Isochronous
525     // Bit 0: 1 - In EndPoint is Isochronous
526     //
527     j = 0;
528     if (eeprom->in_is_isochronous == 1)
529         j = j | 1;
530     if (eeprom->out_is_isochronous == 1)
531         j = j | 2;
532     if (eeprom->suspend_pull_downs == 1)
533         j = j | 4;
534     if (eeprom->use_serial == 1)
535         j = j | 8;
536     if (eeprom->change_usb_version == 1)
537         j = j | 16;
538     output[0x0A] = j;
539
540     // Addr 0B: reserved
541     output[0x0B] = 0x00;
542
543     // Addr 0C: USB version low byte when 0x0A bit 4 is set
544     // Addr 0D: USB version high byte when 0x0A bit 4 is set
545     if (eeprom->change_usb_version == 1) {
546         output[0x0C] = eeprom->usb_version;
547         output[0x0D] = eeprom->usb_version >> 8;
548     }
549
550
551     // Addr 0E: Offset of the manufacturer string + 0x80
552     output[0x0E] = 0x14 + 0x80;
553
554     // Addr 0F: Length of manufacturer string
555     output[0x0F] = manufacturer_size*2 + 2;
556
557     // Addr 10: Offset of the product string + 0x80, calculated later
558     // Addr 11: Length of product string
559     output[0x11] = product_size*2 + 2;
560
561     // Addr 12: Offset of the serial string + 0x80, calculated later
562     // Addr 13: Length of serial string
563     output[0x13] = serial_size*2 + 2;
564
565     // Dynamic content
566     output[0x14] = manufacturer_size*2 + 2;
567     output[0x15] = 0x03; // type: string
568
569     i = 0x16, j = 0;
570
571     // Output manufacturer
572     for (j = 0; j < manufacturer_size; j++) {
573         output[i] = eeprom->manufacturer[j], i++;
574         output[i] = 0x00, i++;
575     }
576
577     // Output product name
578     output[0x10] = i + 0x80;  // calculate offset
579     output[i] = product_size*2 + 2, i++;
580     output[i] = 0x03, i++;
581     for (j = 0; j < product_size; j++) {
582         output[i] = eeprom->product[j], i++;
583         output[i] = 0x00, i++;
584     }
585
586     // Output serial
587     output[0x12] = i + 0x80; // calculate offset
588     output[i] = serial_size*2 + 2, i++;
589     output[i] = 0x03, i++;
590     for (j = 0; j < serial_size; j++) {
591         output[i] = eeprom->serial[j], i++;
592         output[i] = 0x00, i++;
593     }
594
595     // calculate checksum
596     checksum = 0xAAAA;
597
598     for (i = 0; i < 63; i++) {
599         value = output[i*2];
600         value += output[(i*2)+1] << 8;
601
602         checksum = value^checksum;
603         checksum = (checksum << 1) | (checksum >> 15);
604     }
605
606     output[0x7E] = checksum;
607     output[0x7F] = checksum >> 8;
608
609     return size_check;
610 }
611
612
613 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
614     int i;
615
616     for (i = 0; i < 64; i++) {
617         if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_timeout) != 2) {
618             ftdi->error_str = "Reading eeprom failed";
619             return -1;
620         }
621     }
622
623     return 0;
624 }
625
626
627 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
628     unsigned short usb_val;
629     int i;
630
631     for (i = 0; i < 64; i++) {
632         usb_val = eeprom[i*2];
633         usb_val += eeprom[(i*2)+1] << 8;
634         if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_timeout) != 0) {
635             ftdi->error_str = "Unable to write eeprom";
636             return -1;
637         }
638     }
639
640     return 0;
641 }
642
643
644 int ftdi_erase_eeprom(struct ftdi_context *ftdi) {
645     if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
646         ftdi->error_str = "Unable to erase eeprom";
647         return -1;
648     }
649
650     return 0;
651 }