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