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