libftdi: (tomj) added changelog and increased version to 0.2
[libftdi] / ftdi / ftdi.c
CommitLineData
a3da1d95
GE
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
21int 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
34void 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*/
49int 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
96int 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*/
111int 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*/
130int 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
be5d7eec 192int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
a3da1d95
GE
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);
cbabb7d3
TJ
202 if (ret == -1) {
203 ftdi->error_str = "bulk write failed";
a3da1d95 204 return -1;
cbabb7d3 205 }
a3da1d95
GE
206
207 offset += write_size;
208 }
209
210 return 0;
211}
212
213
be5d7eec 214int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
cbabb7d3
TJ
215 static unsigned char readbuf[64];
216 int ret = 1;
217 int offset = 0;
218
979a145c
TJ
219 if (size != 0 && size % 64 != 0) {
220 ftdi->error_str = "Sorry, read buffer size must currently be a multiple (1x, 2x, 3x...) of 64";
221 return -2;
222 }
223
cbabb7d3
TJ
224 while (offset < size && ret > 0) {
225 ret = usb_bulk_read (ftdi->usb_dev, 0x81, readbuf, 64, ftdi->usb_timeout);
226 // Skip FTDI status bytes
227 if (ret >= 2)
228 ret-=2;
229
230 if (ret > 0) {
231 memcpy (buf+offset, readbuf+2, ret);
232 }
233
234 if (ret == -1) {
235 ftdi->error_str = "bulk read failed";
236 return -1;
237 }
238
239 offset += ret;
240 }
241
242 return offset;
a3da1d95
GE
243}
244
245
246int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) {
247 unsigned short usb_val;
248
249 usb_val = bitmask; // low byte: bitmask
250 usb_val += 1 << 8; // high byte: enable flag
251 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
252 ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?";
253 return -1;
254 }
255
256 ftdi->bitbang_enabled = 1;
257 return 0;
258}
259
260
261int ftdi_disable_bitbang(struct ftdi_context *ftdi) {
262 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
263 ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?";
264 return -1;
265 }
266
267 ftdi->bitbang_enabled = 0;
268 return 0;
269}
270
271
272int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) {
273 unsigned short usb_val;
274 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
275 ftdi->error_str = "Read pins failed";
276 return -1;
277 }
278
279 *pins = (unsigned char)usb_val;
280 return 0;
281}
282
283
284int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
285 unsigned short usb_val;
286
287 if (latency < 1) {
288 ftdi->error_str = "Latency out of range. Only valid for 1-255";
289 return -1;
290 }
291
292 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, 0, NULL, 0, ftdi->usb_timeout) != 0) {
293 ftdi->error_str = "Unable to set latency timer";
294 return -2;
295 }
296 return 0;
297}
298
299
300int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) {
301 unsigned short usb_val;
302 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x09, 0, 0, (char *)&usb_val, 1, ftdi->usb_timeout) != 1) {
303 ftdi->error_str = "Reading latency timer failed";
304 return -1;
305 }
306
307 *latency = (unsigned char)usb_val;
308 return 0;
309}
310
311
b8aa7b35
TJ
312void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
313 eeprom->vendor_id = 0403;
314 eeprom->product_id = 6001;
315
316 eeprom->self_powered = 1;
317 eeprom->remote_wakeup = 1;
318 eeprom->BM_type_chip = 1;
319
320 eeprom->in_is_isochronous = 0;
321 eeprom->out_is_isochronous = 0;
322 eeprom->suspend_pull_downs = 0;
323
324 eeprom->use_serial = 0;
325 eeprom->change_usb_version = 0;
326 eeprom->usb_version = 200;
327 eeprom->max_power = 0;
328
329 eeprom->manufacturer = NULL;
330 eeprom->product = NULL;
331 eeprom->serial = NULL;
332}
333
334
335/*
336 ftdi_eeprom_build return codes:
8ed61121 337 positive value: used eeprom size
b8aa7b35
TJ
338 -1: eeprom size (128 bytes) exceeded by custom strings
339*/
340int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
341 unsigned char i, j;
342 unsigned short checksum, value;
343 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
344 int size_check;
345
346 if (eeprom->manufacturer != NULL)
347 manufacturer_size = strlen(eeprom->manufacturer);
348 if (eeprom->product != NULL)
349 product_size = strlen(eeprom->product);
350 if (eeprom->serial != NULL)
351 serial_size = strlen(eeprom->serial);
352
353 size_check = 128; // eeprom is 128 bytes
354 size_check -= 28; // 28 are always in use (fixed)
355 size_check -= manufacturer_size*2;
356 size_check -= product_size*2;
357 size_check -= serial_size*2;
358
359 // eeprom size exceeded?
360 if (size_check < 0)
361 return (-1);
362
363 // empty eeprom
364 memset (output, 0, 128);
365
366 // Addr 00: Stay 00 00
367 // Addr 02: Vendor ID
368 output[0x02] = eeprom->vendor_id;
369 output[0x03] = eeprom->vendor_id >> 8;
370
371 // Addr 04: Product ID
372 output[0x04] = eeprom->product_id;
373 output[0x05] = eeprom->product_id >> 8;
374
375 // Addr 06: Device release number (0400h for BM features)
376 output[0x06] = 0x00;
377
378 if (eeprom->BM_type_chip == 1)
379 output[0x07] = 0x04;
380 else
381 output[0x07] = 0x02;
382
383 // Addr 08: Config descriptor
384 // Bit 1: remote wakeup if 1
385 // Bit 0: self powered if 1
386 //
387 j = 0;
388 if (eeprom->self_powered == 1)
389 j = j | 1;
390 if (eeprom->remote_wakeup == 1)
391 j = j | 2;
392 output[0x08] = j;
393
394 // Addr 09: Max power consumption: max power = value * 2 mA
395 output[0x09] = eeprom->max_power;;
396
397 // Addr 0A: Chip configuration
398 // Bit 7: 0 - reserved
399 // Bit 6: 0 - reserved
400 // Bit 5: 0 - reserved
401 // Bit 4: 1 - Change USB version
402 // Bit 3: 1 - Use the serial number string
403 // Bit 2: 1 - Enable suspend pull downs for lower power
404 // Bit 1: 1 - Out EndPoint is Isochronous
405 // Bit 0: 1 - In EndPoint is Isochronous
406 //
407 j = 0;
408 if (eeprom->in_is_isochronous == 1)
409 j = j | 1;
410 if (eeprom->out_is_isochronous == 1)
411 j = j | 2;
412 if (eeprom->suspend_pull_downs == 1)
413 j = j | 4;
414 if (eeprom->use_serial == 1)
415 j = j | 8;
416 if (eeprom->change_usb_version == 1)
417 j = j | 16;
418 output[0x0A] = j;
419
420 // Addr 0B: reserved
421 output[0x0B] = 0x00;
422
423 // Addr 0C: USB version low byte when 0x0A bit 4 is set
424 // Addr 0D: USB version high byte when 0x0A bit 4 is set
425 if (eeprom->change_usb_version == 1) {
426 output[0x0C] = eeprom->usb_version;
427 output[0x0D] = eeprom->usb_version >> 8;
428 }
429
430
431 // Addr 0E: Offset of the manufacturer string + 0x80
432 output[0x0E] = 0x14 + 0x80;
433
434 // Addr 0F: Length of manufacturer string
435 output[0x0F] = manufacturer_size*2 + 2;
436
437 // Addr 10: Offset of the product string + 0x80, calculated later
438 // Addr 11: Length of product string
439 output[0x11] = product_size*2 + 2;
440
441 // Addr 12: Offset of the serial string + 0x80, calculated later
442 // Addr 13: Length of serial string
443 output[0x13] = serial_size*2 + 2;
444
445 // Dynamic content
a862ddcf 446 output[0x14] = manufacturer_size*2 + 2;
b8aa7b35
TJ
447 output[0x15] = 0x03; // type: string
448
449 i = 0x16, j = 0;
450
451 // Output manufacturer
452 for (j = 0; j < manufacturer_size; j++) {
453 output[i] = eeprom->manufacturer[j], i++;
454 output[i] = 0x00, i++;
455 }
456
457 // Output product name
458 output[0x10] = i + 0x80; // calculate offset
459 output[i] = product_size*2 + 2, i++;
460 output[i] = 0x03, i++;
461 for (j = 0; j < product_size; j++) {
462 output[i] = eeprom->product[j], i++;
463 output[i] = 0x00, i++;
464 }
465
466 // Output serial
467 output[0x12] = i + 0x80; // calculate offset
468 output[i] = serial_size*2 + 2, i++;
469 output[i] = 0x03, i++;
470 for (j = 0; j < serial_size; j++) {
471 output[i] = eeprom->serial[j], i++;
472 output[i] = 0x00, i++;
473 }
474
475 // calculate checksum
476 checksum = 0xAAAA;
477
478 for (i = 0; i < 63; i++) {
479 value = output[i*2];
480 value += output[(i*2)+1] << 8;
481
482 checksum = value^checksum;
483 checksum = (checksum << 1) | (checksum >> 15);
484 }
485
486 output[0x7E] = checksum;
487 output[0x7F] = checksum >> 8;
488
8ed61121 489 return size_check;
b8aa7b35
TJ
490}
491
492
be5d7eec 493int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
a3da1d95
GE
494 int i;
495
496 for (i = 0; i < 64; i++) {
497 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_timeout) != 2) {
498 ftdi->error_str = "Reading eeprom failed";
499 return -1;
500 }
501 }
502
503 return 0;
504}
505
506
be5d7eec 507int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
a3da1d95
GE
508 unsigned short usb_val;
509 int i;
510
511 for (i = 0; i < 64; i++) {
512 usb_val = eeprom[i*2];
513 usb_val += eeprom[(i*2)+1] << 8;
514 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_timeout) != 0) {
515 ftdi->error_str = "Unable to write eeprom";
516 return -1;
517 }
518 }
519
520 return 0;
521}
522
523
524int ftdi_erase_eeprom(struct ftdi_context *ftdi) {
525 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_timeout) != 0) {
526 ftdi->error_str = "Unable to erase eeprom";
527 return -1;
528 }
529
530 return 0;
531}