libftdi: (tomj) added ftdi_usb_purge_buffers by request
[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                : info@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 int ftdi_init(struct ftdi_context *ftdi) {
22     ftdi->usb_dev = NULL;
23     ftdi->usb_timeout = 5000;
24
25     ftdi->baudrate = -1;
26     ftdi->bitbang_enabled = 0;
27
28     ftdi->error_str = NULL;
29
30     return 0;
31 }
32
33
34 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) {
35     ftdi->usb_dev = usb;
36 }
37
38
39 /* ftdi_usb_open return codes:
40    0: all fine
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
46   -6: reset failed
47   -7: set baudrate failed
48 */
49 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) {
50     struct usb_bus *bus;
51     struct usb_device *dev;
52
53     usb_init();
54
55     if (usb_find_busses() < 0) {
56         ftdi->error_str = "usb_find_busses() failed";
57         return -1;
58     }
59
60     if (usb_find_devices() < 0) {
61         ftdi->error_str = "usb_find_devices() failed";
62         return -2;
63     }
64
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);
69                 if (ftdi->usb_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...";
72                         return -5;
73                     }
74
75                     if (ftdi_usb_reset (ftdi) != 0)
76                        return -6;
77
78                     if (ftdi_set_baudrate (ftdi, 9600) != 0)
79                        return -7;
80
81                     return 0;
82                 } else {
83                     ftdi->error_str = "usb_open() failed";
84                     return -4;
85                 }
86             }
87         }
88
89     }
90
91     // device not found
92     return -3;
93 }
94
95
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";
99         return -1;
100     }
101
102     return 0;
103 }
104
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";
108         return -1;
109     }
110
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";
113         return -1;
114     }
115
116     return 0;
117 }
118
119 /* ftdi_usb_close return codes
120     0: all fine
121    -1: usb_release failed
122    -2: usb_close failed
123 */
124 int ftdi_usb_close(struct ftdi_context *ftdi) {
125     int rtn = 0;
126
127     if (usb_release_interface(ftdi->usb_dev, 0) != 0)
128         rtn = -1;
129
130     if (usb_close (ftdi->usb_dev) != 0)
131         rtn = -2;
132
133     return rtn;
134 }
135
136
137 /*
138     ftdi_set_baudrate return codes:
139      0: all fine
140     -1: invalid baudrate
141     -2: setting baudrate failed
142 */
143 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) {
144     unsigned short ftdi_baudrate;
145
146     if (ftdi->bitbang_enabled) {
147         baudrate = baudrate*4;
148     }
149
150     switch (baudrate) {
151     case 300:
152         ftdi_baudrate = 0x2710;
153         break;
154     case 600:
155         ftdi_baudrate = 0x1388;
156         break;
157     case 1200:
158         ftdi_baudrate = 0x09C4;
159         break;
160     case 2400:
161         ftdi_baudrate = 0x04E2;
162         break;
163     case 4800:
164         ftdi_baudrate = 0x0271;
165         break;
166     case 9600:
167         ftdi_baudrate = 0x4138;
168         break;
169     case 19200:
170         ftdi_baudrate = 0x809C;
171         break;
172     case 38400:
173         ftdi_baudrate = 0xC04E;
174         break;
175     case 57600:
176         ftdi_baudrate = 0x0034;
177         break;
178     case 115200:
179         ftdi_baudrate = 0x001A;
180         break;
181     case 230400:
182         ftdi_baudrate = 0x000D;
183         break;
184     case 460800:
185         ftdi_baudrate = 0x4006;
186         break;
187     case 921600:
188         ftdi_baudrate = 0x8003;
189         break;
190     default:
191         ftdi->error_str = "Unknown baudrate. Note: bitbang baudrates are automatically multiplied by 4";
192         return -1;
193     }
194
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";
197         return -2;
198     }
199
200     ftdi->baudrate = baudrate;
201     return 0;
202 }
203
204
205 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
206     int ret;
207     int offset = 0;
208     while (offset < size) {
209         int write_size = 64;
210
211         if (offset+write_size > size)
212             write_size = size-offset;
213
214         ret=usb_bulk_write(ftdi->usb_dev, 2, buf+offset, write_size, ftdi->usb_timeout);
215         if (ret == -1) {
216             ftdi->error_str = "bulk write failed";
217             return -1;
218         }
219
220         offset += write_size;
221     }
222
223     return 0;
224 }
225
226
227 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
228     static unsigned char readbuf[64];
229     int ret = 1;
230     int offset = 0;
231
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";
234             return -2;
235     }
236
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
240         if (ret >= 2)
241             ret-=2;
242         
243         if (ret > 0) {
244             memcpy (buf+offset, readbuf+2, ret);
245         }
246
247         if (ret == -1) {
248             ftdi->error_str = "bulk read failed";
249             return -1;
250         }
251
252         offset += ret;
253     }
254
255     return offset;
256 }
257
258
259 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) {
260     unsigned short usb_val;
261
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?";
266         return -1;
267     }
268
269     ftdi->bitbang_enabled = 1;
270     return 0;
271 }
272
273
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?";
277         return -1;
278     }
279
280     ftdi->bitbang_enabled = 0;
281     return 0;
282 }
283
284
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";
289         return -1;
290     }
291
292     *pins = (unsigned char)usb_val;
293     return 0;
294 }
295
296
297 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
298     unsigned short usb_val;
299
300     if (latency < 1) {
301        ftdi->error_str = "Latency out of range. Only valid for 1-255";
302        return -1;
303     }
304
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";
307        return -2;
308     }
309     return 0;
310 }
311
312
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";
317         return -1;
318     }
319
320     *latency = (unsigned char)usb_val;
321     return 0;
322 }
323
324
325 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
326     eeprom->vendor_id = 0403;
327     eeprom->product_id = 6001;
328     
329     eeprom->self_powered = 1;
330     eeprom->remote_wakeup = 1;
331     eeprom->BM_type_chip = 1;
332     
333     eeprom->in_is_isochronous = 0;
334     eeprom->out_is_isochronous = 0;
335     eeprom->suspend_pull_downs = 0;
336     
337     eeprom->use_serial = 0;
338     eeprom->change_usb_version = 0;
339     eeprom->usb_version = 200;
340     eeprom->max_power = 0;
341     
342     eeprom->manufacturer = NULL;
343     eeprom->product = NULL;
344     eeprom->serial = NULL;
345 }
346
347
348 /*
349     ftdi_eeprom_build return codes:
350     positive value: used eeprom size
351     -1: eeprom size (128 bytes) exceeded by custom strings
352 */
353 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
354     unsigned char i, j;
355     unsigned short checksum, value;
356     unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
357     int size_check;
358
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);
365
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;
371
372     // eeprom size exceeded?
373     if (size_check < 0)
374         return (-1);
375
376     // empty eeprom
377     memset (output, 0, 128);
378
379     // Addr 00: Stay 00 00
380     // Addr 02: Vendor ID
381     output[0x02] = eeprom->vendor_id;
382     output[0x03] = eeprom->vendor_id >> 8;
383
384     // Addr 04: Product ID
385     output[0x04] = eeprom->product_id;
386     output[0x05] = eeprom->product_id >> 8;
387
388     // Addr 06: Device release number (0400h for BM features)
389     output[0x06] = 0x00;
390     
391     if (eeprom->BM_type_chip == 1)
392         output[0x07] = 0x04;
393     else
394         output[0x07] = 0x02;
395
396     // Addr 08: Config descriptor
397     // Bit 1: remote wakeup if 1
398     // Bit 0: self powered if 1
399     //
400     j = 0;
401     if (eeprom->self_powered == 1)
402         j = j | 1;
403     if (eeprom->remote_wakeup == 1)
404         j = j | 2;
405     output[0x08] = j;
406
407     // Addr 09: Max power consumption: max power = value * 2 mA
408     output[0x09] = eeprom->max_power;;
409     
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
419     //
420     j = 0;
421     if (eeprom->in_is_isochronous == 1)
422         j = j | 1;
423     if (eeprom->out_is_isochronous == 1)
424         j = j | 2;
425     if (eeprom->suspend_pull_downs == 1)
426         j = j | 4;
427     if (eeprom->use_serial == 1)
428         j = j | 8;
429     if (eeprom->change_usb_version == 1)
430         j = j | 16;
431     output[0x0A] = j;
432     
433     // Addr 0B: reserved
434     output[0x0B] = 0x00;
435     
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;
441     }
442
443
444     // Addr 0E: Offset of the manufacturer string + 0x80
445     output[0x0E] = 0x14 + 0x80;
446
447     // Addr 0F: Length of manufacturer string
448     output[0x0F] = manufacturer_size*2 + 2;
449
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;
453
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;
457
458     // Dynamic content
459     output[0x14] = manufacturer_size*2 + 2;
460     output[0x15] = 0x03;        // type: string
461     
462     i = 0x16, j = 0;
463     
464     // Output manufacturer
465     for (j = 0; j < manufacturer_size; j++) {
466         output[i] = eeprom->manufacturer[j], i++;
467         output[i] = 0x00, i++;
468     }
469
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++;
477     }
478     
479     // Output serial
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++;
486     }
487
488     // calculate checksum
489     checksum = 0xAAAA;
490     
491     for (i = 0; i < 63; i++) {
492         value = output[i*2];
493         value += output[(i*2)+1] << 8;
494
495         checksum = value^checksum;
496         checksum = (checksum << 1) | (checksum >> 15);  
497     }
498
499     output[0x7E] = checksum;
500     output[0x7F] = checksum >> 8;    
501
502     return size_check;
503 }
504
505
506 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
507     int i;
508
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";
512            return -1;
513         }
514     }
515
516     return 0;
517 }
518
519
520 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
521     unsigned short usb_val;
522     int i;
523
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";
529           return -1;
530        }
531     }
532
533     return 0;
534 }
535
536
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";
540         return -1;
541     }
542
543     return 0;
544 }