libftdi: (tomj) completed eeprom building code
[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
106 /* ftdi_usb_close return codes
107     0: all fine
108    -1: usb_release failed
109    -2: usb_close failed
110 */
111 int ftdi_usb_close(struct ftdi_context *ftdi) {
112     int rtn = 0;
113
114     if (usb_release_interface(ftdi->usb_dev, 0) != 0)
115         rtn = -1;
116
117     if (usb_close (ftdi->usb_dev) != 0)
118         rtn = -2;
119
120     return rtn;
121 }
122
123
124 /*
125     ftdi_set_baudrate return codes:
126      0: all fine
127     -1: invalid baudrate
128     -2: setting baudrate failed
129 */
130 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) {
131     unsigned short ftdi_baudrate;
132
133     if (ftdi->bitbang_enabled) {
134         baudrate = baudrate*4;
135     }
136
137     switch (baudrate) {
138     case 300:
139         ftdi_baudrate = 0x2710;
140         break;
141     case 600:
142         ftdi_baudrate = 0x1388;
143         break;
144     case 1200:
145         ftdi_baudrate = 0x09C4;
146         break;
147     case 2400:
148         ftdi_baudrate = 0x04E2;
149         break;
150     case 4800:
151         ftdi_baudrate = 0x0271;
152         break;
153     case 9600:
154         ftdi_baudrate = 0x4138;
155         break;
156     case 19200:
157         ftdi_baudrate = 0x809C;
158         break;
159     case 38400:
160         ftdi_baudrate = 0xC04E;
161         break;
162     case 57600:
163         ftdi_baudrate = 0x0034;
164         break;
165     case 115200:
166         ftdi_baudrate = 0x001A;
167         break;
168     case 230400:
169         ftdi_baudrate = 0x000D;
170         break;
171     case 460800:
172         ftdi_baudrate = 0x4006;
173         break;
174     case 921600:
175         ftdi_baudrate = 0x8003;
176         break;
177     default:
178         ftdi->error_str = "Unknown baudrate. Note: bitbang baudrates are automatically multiplied by 4";
179         return -1;
180     }
181
182     if (usb_control_msg(ftdi->usb_dev, 0x40, 3, ftdi_baudrate, 0, NULL, 0, ftdi->usb_timeout) != 0) {
183         ftdi->error_str = "Setting new baudrate failed";
184         return -2;
185     }
186
187     ftdi->baudrate = baudrate;
188     return 0;
189 }
190
191
192 int ftdi_write_data(struct ftdi_context *ftdi, char *buf, int size) {
193     int ret;
194     int offset = 0;
195     while (offset < size) {
196         int write_size = 64;
197
198         if (offset+write_size > size)
199             write_size = size-offset;
200
201         ret=usb_bulk_write(ftdi->usb_dev, 2, buf+offset, write_size, ftdi->usb_timeout);
202         if (ret == -1)
203             return -1;
204
205         offset += write_size;
206     }
207
208     return 0;
209 }
210
211
212 int ftdi_read_data(struct ftdi_context *ftdi, char *buf, int size) {
213     /*
214       unsigned char buf[64];
215       int read_bytes;
216
217       read_bytes = usb_bulk_read (udev, 0x81, (char *)&buf, 64, USB_TIMEOUT);
218     */
219     ftdi->error_str = "Not implemented yet";
220     return -1;
221 }
222
223
224 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) {
225     unsigned short usb_val;
226
227     usb_val = bitmask;  // low byte: bitmask
228     usb_val += 1 << 8;  // high byte: enable flag
229     if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
230         ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?";
231         return -1;
232     }
233
234     ftdi->bitbang_enabled = 1;
235     return 0;
236 }
237
238
239 int ftdi_disable_bitbang(struct ftdi_context *ftdi) {
240     if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
241         ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?";
242         return -1;
243     }
244
245     ftdi->bitbang_enabled = 0;
246     return 0;
247 }
248
249
250 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) {
251     unsigned short usb_val;
252     if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
253         ftdi->error_str = "Read pins failed";
254         return -1;
255     }
256
257     *pins = (unsigned char)usb_val;
258     return 0;
259 }
260
261
262 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
263     unsigned short usb_val;
264
265     if (latency < 1) {
266        ftdi->error_str = "Latency out of range. Only valid for 1-255";
267        return -1;
268     }
269
270     if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
271        ftdi->error_str = "Unable to set latency timer";
272        return -2;
273     }
274     return 0;
275 }
276
277
278 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) {
279     unsigned short usb_val;
280     if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x09, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
281         ftdi->error_str = "Reading latency timer failed";
282         return -1;
283     }
284
285     *latency = (unsigned char)usb_val;
286     return 0;
287 }
288
289
290 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
291     eeprom->vendor_id = 0403;
292     eeprom->product_id = 6001;
293     
294     eeprom->self_powered = 1;
295     eeprom->remote_wakeup = 1;
296     eeprom->BM_type_chip = 1;
297     
298     eeprom->in_is_isochronous = 0;
299     eeprom->out_is_isochronous = 0;
300     eeprom->suspend_pull_downs = 0;
301     
302     eeprom->use_serial = 0;
303     eeprom->change_usb_version = 0;
304     eeprom->usb_version = 200;
305     eeprom->max_power = 0;
306     
307     eeprom->manufacturer = NULL;
308     eeprom->product = NULL;
309     eeprom->serial = NULL;
310 }
311
312
313 /*
314     ftdi_eeprom_build return codes:
315      0: all fine
316     -1: eeprom size (128 bytes) exceeded by custom strings
317 */
318 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
319     unsigned char i, j;
320     unsigned short checksum, value;
321     unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
322     int size_check;
323
324     if (eeprom->manufacturer != NULL)
325         manufacturer_size = strlen(eeprom->manufacturer);
326     if (eeprom->product != NULL)
327         product_size = strlen(eeprom->product);
328     if (eeprom->serial != NULL)
329         serial_size = strlen(eeprom->serial);
330
331     size_check = 128;   // eeprom is 128 bytes
332     size_check -= 28;   // 28 are always in use (fixed)
333     size_check -= manufacturer_size*2;
334     size_check -= product_size*2;
335     size_check -= serial_size*2;
336
337     // eeprom size exceeded?
338     if (size_check < 0)
339         return (-1);
340
341     // empty eeprom
342     memset (output, 0, 128);
343
344     // Addr 00: Stay 00 00
345     // Addr 02: Vendor ID
346     output[0x02] = eeprom->vendor_id;
347     output[0x03] = eeprom->vendor_id >> 8;
348
349     // Addr 04: Product ID
350     output[0x04] = eeprom->product_id;
351     output[0x05] = eeprom->product_id >> 8;
352
353     // Addr 06: Device release number (0400h for BM features)
354     output[0x06] = 0x00;
355     
356     if (eeprom->BM_type_chip == 1)
357         output[0x07] = 0x04;
358     else
359         output[0x07] = 0x02;
360
361     // Addr 08: Config descriptor
362     // Bit 1: remote wakeup if 1
363     // Bit 0: self powered if 1
364     //
365     j = 0;
366     if (eeprom->self_powered == 1)
367         j = j | 1;
368     if (eeprom->remote_wakeup == 1)
369         j = j | 2;
370     output[0x08] = j;
371
372     // Addr 09: Max power consumption: max power = value * 2 mA
373     output[0x09] = eeprom->max_power;;
374     
375     // Addr 0A: Chip configuration
376     // Bit 7: 0 - reserved
377     // Bit 6: 0 - reserved
378     // Bit 5: 0 - reserved
379     // Bit 4: 1 - Change USB version
380     // Bit 3: 1 - Use the serial number string
381     // Bit 2: 1 - Enable suspend pull downs for lower power
382     // Bit 1: 1 - Out EndPoint is Isochronous
383     // Bit 0: 1 - In EndPoint is Isochronous
384     //
385     j = 0;
386     if (eeprom->in_is_isochronous == 1)
387         j = j | 1;
388     if (eeprom->out_is_isochronous == 1)
389         j = j | 2;
390     if (eeprom->suspend_pull_downs == 1)
391         j = j | 4;
392     if (eeprom->use_serial == 1)
393         j = j | 8;
394     if (eeprom->change_usb_version == 1)
395         j = j | 16;
396     output[0x0A] = j;
397     
398     // Addr 0B: reserved
399     output[0x0B] = 0x00;
400     
401     // Addr 0C: USB version low byte when 0x0A bit 4 is set
402     // Addr 0D: USB version high byte when 0x0A bit 4 is set
403     if (eeprom->change_usb_version == 1) {
404         output[0x0C] = eeprom->usb_version;
405         output[0x0D] = eeprom->usb_version >> 8;
406     }
407
408
409     // Addr 0E: Offset of the manufacturer string + 0x80
410     output[0x0E] = 0x14 + 0x80;
411
412     // Addr 0F: Length of manufacturer string
413     output[0x0F] = manufacturer_size*2 + 2;
414
415     // Addr 10: Offset of the product string + 0x80, calculated later
416     // Addr 11: Length of product string
417     output[0x11] = product_size*2 + 2;
418
419     // Addr 12: Offset of the serial string + 0x80, calculated later
420     // Addr 13: Length of serial string
421     output[0x13] = serial_size*2 + 2;
422
423     // Dynamic content
424     output[0x14] = manufacturer_size;
425     output[0x15] = 0x03;        // type: string
426     
427     i = 0x16, j = 0;
428     
429     // Output manufacturer
430     for (j = 0; j < manufacturer_size; j++) {
431         output[i] = eeprom->manufacturer[j], i++;
432         output[i] = 0x00, i++;
433     }
434
435     // Output product name
436     output[0x10] = i + 0x80;    // calculate offset
437     output[i] = product_size*2 + 2, i++;
438     output[i] = 0x03, i++;
439     for (j = 0; j < product_size; j++) {
440         output[i] = eeprom->product[j], i++;
441         output[i] = 0x00, i++;
442     }
443     
444     // Output serial
445     output[0x12] = i + 0x80;    // calculate offset
446     output[i] = serial_size*2 + 2, i++;
447     output[i] = 0x03, i++;
448     for (j = 0; j < serial_size; j++) {
449         output[i] = eeprom->serial[j], i++;
450         output[i] = 0x00, i++;
451     }
452
453     // calculate checksum
454     checksum = 0xAAAA;
455     
456     for (i = 0; i < 63; i++) {
457         value = output[i*2];
458         value += output[(i*2)+1] << 8;
459
460         checksum = value^checksum;
461         checksum = (checksum << 1) | (checksum >> 15);  
462     }
463
464     output[0x7E] = checksum;
465     output[0x7F] = checksum >> 8;    
466
467     return 0;
468 }
469
470
471 int ftdi_read_eeprom(struct ftdi_context *ftdi, char *eeprom) {
472     int i;
473
474     for (i = 0; i < 64; i++) {
475         if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_timeout) != 2) {
476            ftdi->error_str = "Reading eeprom failed";
477            return -1;
478         }
479     }
480
481     return 0;
482 }
483
484
485 int ftdi_write_eeprom(struct ftdi_context *ftdi, char *eeprom) {
486     unsigned short usb_val;
487     int i;
488
489     for (i = 0; i < 64; i++) {
490        usb_val = eeprom[i*2];
491        usb_val += eeprom[(i*2)+1] << 8;
492        if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_timeout) != 0) {
493           ftdi->error_str = "Unable to write eeprom";
494           return -1;
495        }
496     }
497
498     return 0;
499 }
500
501
502 int ftdi_erase_eeprom(struct ftdi_context *ftdi) {
503     if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
504         ftdi->error_str = "Unable to erase eeprom";
505         return -1;
506     }
507
508     return 0;
509 }