libftdi: (tomj) find all devices by vendor/product id
[libftdi] / src / 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 ***************************************************************************/
d9f0cce7 16
98452d97 17#include <usb.h>
a8f46ddc 18#include <string.h>
0e302db6 19
98452d97 20#include "ftdi.h"
a3da1d95 21
21abaf2e 22#define ftdi_error_return(code, str) do { \
2f73e59f 23 ftdi->error_str = str; \
21abaf2e
TJ
24 return code; \
25 } while(0);
c3d95b87
TJ
26
27
4837f98a
TJ
28/* ftdi_init
29
60b7513e 30 Initializes a ftdi_context.
4837f98a
TJ
31
32 Return codes:
33 0: All fine
34 -1: Couldn't allocate read buffer
948f9ada 35*/
a8f46ddc
TJ
36int ftdi_init(struct ftdi_context *ftdi)
37{
98452d97 38 ftdi->usb_dev = NULL;
545820ce
TJ
39 ftdi->usb_read_timeout = 5000;
40 ftdi->usb_write_timeout = 5000;
a3da1d95 41
53ad271d 42 ftdi->type = TYPE_BM; /* chip type */
a3da1d95
GE
43 ftdi->baudrate = -1;
44 ftdi->bitbang_enabled = 0;
45
948f9ada
TJ
46 ftdi->readbuffer = NULL;
47 ftdi->readbuffer_offset = 0;
48 ftdi->readbuffer_remaining = 0;
49 ftdi->writebuffer_chunksize = 4096;
50
545820ce
TJ
51 ftdi->interface = 0;
52 ftdi->index = 0;
53 ftdi->in_ep = 0x02;
54 ftdi->out_ep = 0x81;
3119537f 55 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
53ad271d 56
a3da1d95
GE
57 ftdi->error_str = NULL;
58
1c733d33
TJ
59 /* All fine. Now allocate the readbuffer */
60 return ftdi_read_data_set_chunksize(ftdi, 4096);
948f9ada 61}
4837f98a 62
46860c4c 63/* ftdi_set_interface
4837f98a 64
c4446c36 65 Call after ftdi_init
4837f98a 66
c4446c36 67 Open selected channels on a chip, otherwise use first channel
0ce2f5fa
TJ
68 0: all fine
69 -1: unknown interface
c4446c36 70*/
0ce2f5fa 71int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
c4446c36
TJ
72{
73 switch (interface) {
74 case INTERFACE_ANY:
75 case INTERFACE_A:
0ce2f5fa 76 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
c4446c36
TJ
77 break;
78 case INTERFACE_B:
79 ftdi->interface = 1;
80 ftdi->index = INTERFACE_B;
81 ftdi->in_ep = 0x04;
82 ftdi->out_ep = 0x83;
83 break;
84 default:
85 ftdi_error_return(-1, "Unknown interface");
86 }
87 return 0;
88}
948f9ada 89
4837f98a
TJ
90/* ftdi_deinit
91
92 Deinitializes a ftdi_context.
93*/
a8f46ddc
TJ
94void ftdi_deinit(struct ftdi_context *ftdi)
95{
948f9ada 96 if (ftdi->readbuffer != NULL) {
d9f0cce7
TJ
97 free(ftdi->readbuffer);
98 ftdi->readbuffer = NULL;
948f9ada 99 }
a3da1d95
GE
100}
101
4837f98a
TJ
102/* ftdi_set_usbdev
103
104 Use an already open device.
105*/
a8f46ddc
TJ
106void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
107{
98452d97
TJ
108 ftdi->usb_dev = usb;
109}
110
111
edb82cbf
TJ
112/* ftdi_usb_find_all
113
114 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
115 needs to be deallocated by ftdi_list_free after use.
116
117 Return codes:
118 >0: number of devices found
119 -1: usb_find_busses() failed
120 -2: usb_find_devices() failed
121 -3: out of memory
122*/
123int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
124{
125 struct ftdi_device_list **curdev;
126 struct usb_bus *bus;
127 struct usb_device *dev;
128 int count = 0;
129
130 usb_init();
131 if (usb_find_busses() < 0)
132 ftdi_error_return(-1, "usb_find_busses() failed");
133 if (usb_find_devices() < 0)
134 ftdi_error_return(-2, "usb_find_devices() failed");
135
136 curdev = devlist;
137 for (bus = usb_busses; bus; bus = bus->next) {
138 for (dev = bus->devices; dev; dev = dev->next) {
139 if (dev->descriptor.idVendor == vendor
140 && dev->descriptor.idProduct == product)
141 {
142 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
143 if (!*curdev)
144 ftdi_error_return(-3, "out of memory");
145
146 (*curdev)->next = NULL;
147 (*curdev)->dev = dev;
148
149 curdev = &(*curdev)->next;
150 count++;
151 }
152 }
153 }
154
155 return count;
156}
157
158/* ftdi_list_free
159
160 Frees a created device list.
161*/
162void ftdi_list_free(struct ftdi_device_list **devlist)
163{
164 struct ftdi_device_list **curdev;
165 for (; *devlist == NULL; devlist = curdev) {
166 curdev = &(*devlist)->next;
167 free(*devlist);
168 }
169
170 devlist = NULL;
171}
172
173int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
174{
175 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
176}
177
178/* ftdi_usb_open_desc
179
180 Opens the first device with a given, vendor id, product id,
181 description and serial.
4837f98a
TJ
182
183 Return codes:
184 0: all fine
185 -1: usb_find_busses() failed
186 -2: usb_find_devices() failed
187 -3: usb device not found
188 -4: unable to open device
189 -5: unable to claim device
190 -6: reset failed
191 -7: set baudrate failed
192 -8: get product description failed
193 -9: get serial number failed
edb82cbf 194 -10: unable to close device
a3da1d95 195*/
04e1ea0a 196int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
a8f46ddc
TJ
197 const char* description, const char* serial)
198{
98452d97
TJ
199 struct usb_bus *bus;
200 struct usb_device *dev;
c3d95b87 201 char string[256];
98452d97
TJ
202
203 usb_init();
204
c3d95b87
TJ
205 if (usb_find_busses() < 0)
206 ftdi_error_return(-1, "usb_find_busses() failed");
c3d95b87 207 if (usb_find_devices() < 0)
edb82cbf 208 ftdi_error_return(-2, "usb_find_devices() failed");
a3da1d95 209
98452d97
TJ
210 for (bus = usb_busses; bus; bus = bus->next) {
211 for (dev = bus->devices; dev; dev = dev->next) {
a8f46ddc 212 if (dev->descriptor.idVendor == vendor
c3d95b87
TJ
213 && dev->descriptor.idProduct == product) {
214 if (!(ftdi->usb_dev = usb_open(dev)))
215 ftdi_error_return(-4, "usb_open() failed");
216
a8f46ddc
TJ
217 if (description != NULL) {
218 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
c3d95b87
TJ
219 usb_close (ftdi->usb_dev);
220 ftdi_error_return(-8, "unable to fetch product description");
98452d97 221 }
a8f46ddc 222 if (strncmp(string, description, sizeof(string)) != 0) {
edb82cbf
TJ
223 if (usb_close (ftdi->usb_dev) != 0)
224 ftdi_error_return(-10, "unable to close device");
a8f46ddc
TJ
225 continue;
226 }
227 }
228 if (serial != NULL) {
229 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
c3d95b87
TJ
230 usb_close (ftdi->usb_dev);
231 ftdi_error_return(-9, "unable to fetch serial number");
a8f46ddc
TJ
232 }
233 if (strncmp(string, serial, sizeof(string)) != 0) {
a8f46ddc 234 if (usb_close (ftdi->usb_dev) != 0)
edb82cbf 235 ftdi_error_return(-10, "unable to close device");
a8f46ddc
TJ
236 continue;
237 }
238 }
98452d97 239
edb82cbf
TJ
240 if (usb_close (ftdi->usb_dev) != 0)
241 ftdi_error_return(-10, "unable to close device");
242
243 return ftdi_usb_open_dev(ftdi, dev);
98452d97
TJ
244 }
245 }
98452d97 246 }
a3da1d95 247
98452d97 248 // device not found
c3d95b87 249 ftdi_error_return(-3, "device not found");
a3da1d95
GE
250}
251
4837f98a 252/* ftdi_usb_reset
a3da1d95 253
4837f98a
TJ
254 Resets the ftdi device.
255
256 Return codes:
257 0: all fine
258 -1: FTDI reset failed
259*/
edb82cbf 260int ftdi_usb_reset(struct ftdi_context *ftdi)
a8f46ddc 261{
c3d95b87
TJ
262 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
263 ftdi_error_return(-1,"FTDI reset failed");
264
545820ce 265 // Invalidate data in the readbuffer
bfcee05b
TJ
266 ftdi->readbuffer_offset = 0;
267 ftdi->readbuffer_remaining = 0;
268
a3da1d95
GE
269 return 0;
270}
271
4837f98a
TJ
272/* ftdi_usb_purge_buffers
273
274 Cleans the buffers of the ftdi device.
275
276 Return codes:
277 0: all fine
278 -1: write buffer purge failed
279 -2: read buffer purge failed
280*/
a8f46ddc
TJ
281int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
282{
c3d95b87
TJ
283 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
284 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
285
545820ce 286 // Invalidate data in the readbuffer
bfcee05b
TJ
287 ftdi->readbuffer_offset = 0;
288 ftdi->readbuffer_remaining = 0;
a60be878 289
c3d95b87
TJ
290 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
291 ftdi_error_return(-2, "FTDI purge of TX buffer failed");
545820ce 292
a60be878
TJ
293 return 0;
294}
a3da1d95 295
4837f98a
TJ
296/* ftdi_usb_close
297
298 Closes the ftdi device.
299
300 Return codes:
301 0: all fine
302 -1: usb_release failed
303 -2: usb_close failed
a3da1d95 304*/
a8f46ddc
TJ
305int ftdi_usb_close(struct ftdi_context *ftdi)
306{
a3da1d95
GE
307 int rtn = 0;
308
98452d97 309 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
a3da1d95 310 rtn = -1;
98452d97
TJ
311
312 if (usb_close (ftdi->usb_dev) != 0)
a3da1d95 313 rtn = -2;
98452d97 314
a3da1d95
GE
315 return rtn;
316}
317
318
319/*
53ad271d
TJ
320 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
321 Function is only used internally
322*/
0126d22e 323static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
a8f46ddc
TJ
324 unsigned short *value, unsigned short *index)
325{
53ad271d
TJ
326 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
327 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
328 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
329 int divisor, best_divisor, best_baud, best_baud_diff;
330 unsigned long encoded_divisor;
331 int i;
332
333 if (baudrate <= 0) {
334 // Return error
335 return -1;
336 }
337
338 divisor = 24000000 / baudrate;
339
0126d22e 340 if (ftdi->type == TYPE_AM) {
53ad271d
TJ
341 // Round down to supported fraction (AM only)
342 divisor -= am_adjust_dn[divisor & 7];
343 }
344
345 // Try this divisor and the one above it (because division rounds down)
346 best_divisor = 0;
347 best_baud = 0;
348 best_baud_diff = 0;
349 for (i = 0; i < 2; i++) {
350 int try_divisor = divisor + i;
351 int baud_estimate;
352 int baud_diff;
353
354 // Round up to supported divisor value
df612d35 355 if (try_divisor <= 8) {
53ad271d
TJ
356 // Round up to minimum supported divisor
357 try_divisor = 8;
0126d22e 358 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
53ad271d
TJ
359 // BM doesn't support divisors 9 through 11 inclusive
360 try_divisor = 12;
361 } else if (divisor < 16) {
362 // AM doesn't support divisors 9 through 15 inclusive
363 try_divisor = 16;
364 } else {
0126d22e 365 if (ftdi->type == TYPE_AM) {
53ad271d
TJ
366 // Round up to supported fraction (AM only)
367 try_divisor += am_adjust_up[try_divisor & 7];
368 if (try_divisor > 0x1FFF8) {
369 // Round down to maximum supported divisor value (for AM)
370 try_divisor = 0x1FFF8;
371 }
372 } else {
373 if (try_divisor > 0x1FFFF) {
374 // Round down to maximum supported divisor value (for BM)
375 try_divisor = 0x1FFFF;
376 }
377 }
378 }
379 // Get estimated baud rate (to nearest integer)
380 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
381 // Get absolute difference from requested baud rate
382 if (baud_estimate < baudrate) {
383 baud_diff = baudrate - baud_estimate;
384 } else {
385 baud_diff = baud_estimate - baudrate;
386 }
387 if (i == 0 || baud_diff < best_baud_diff) {
388 // Closest to requested baud rate so far
389 best_divisor = try_divisor;
390 best_baud = baud_estimate;
391 best_baud_diff = baud_diff;
392 if (baud_diff == 0) {
393 // Spot on! No point trying
394 break;
395 }
396 }
397 }
398 // Encode the best divisor value
399 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
400 // Deal with special cases for encoded value
401 if (encoded_divisor == 1) {
4837f98a 402 encoded_divisor = 0; // 3000000 baud
53ad271d 403 } else if (encoded_divisor == 0x4001) {
4837f98a 404 encoded_divisor = 1; // 2000000 baud (BM only)
53ad271d
TJ
405 }
406 // Split into "value" and "index" values
407 *value = (unsigned short)(encoded_divisor & 0xFFFF);
de22df10 408 if(ftdi->type == TYPE_2232C) {
0126d22e
TJ
409 *index = (unsigned short)(encoded_divisor >> 8);
410 *index &= 0xFF00;
411 *index |= ftdi->interface;
412 }
413 else
414 *index = (unsigned short)(encoded_divisor >> 16);
c3d95b87 415
53ad271d
TJ
416 // Return the nearest baud rate
417 return best_baud;
418}
419
420/*
4837f98a
TJ
421 ftdi_set_baudrate
422
423 Sets the chip baudrate
424
425 Return codes:
a3da1d95
GE
426 0: all fine
427 -1: invalid baudrate
428 -2: setting baudrate failed
429*/
a8f46ddc
TJ
430int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
431{
53ad271d
TJ
432 unsigned short value, index;
433 int actual_baudrate;
a3da1d95
GE
434
435 if (ftdi->bitbang_enabled) {
436 baudrate = baudrate*4;
437 }
438
25707904 439 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
c3d95b87
TJ
440 if (actual_baudrate <= 0)
441 ftdi_error_return (-1, "Silly baudrate <= 0.");
a3da1d95 442
53ad271d
TJ
443 // Check within tolerance (about 5%)
444 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
445 || ((actual_baudrate < baudrate)
446 ? (actual_baudrate * 21 < baudrate * 20)
c3d95b87
TJ
447 : (baudrate * 21 < actual_baudrate * 20)))
448 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
545820ce 449
c3d95b87
TJ
450 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
451 ftdi_error_return (-2, "Setting new baudrate failed");
a3da1d95
GE
452
453 ftdi->baudrate = baudrate;
454 return 0;
455}
456
2f73e59f 457/*
4837f98a
TJ
458 ftdi_set_line_property
459
460 set (RS232) line characteristics by Alain Abbas
461
462 Return codes:
463 0: all fine
464 -1: Setting line property failed
2f73e59f
TJ
465*/
466int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
467 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
468{
469 unsigned short value = bits;
470
471 switch(parity) {
472 case NONE:
473 value |= (0x00 << 8);
474 break;
475 case ODD:
476 value |= (0x01 << 8);
477 break;
478 case EVEN:
479 value |= (0x02 << 8);
480 break;
481 case MARK:
482 value |= (0x03 << 8);
483 break;
484 case SPACE:
485 value |= (0x04 << 8);
486 break;
487 }
488
489 switch(sbit) {
490 case STOP_BIT_1:
491 value |= (0x00 << 11);
492 break;
493 case STOP_BIT_15:
494 value |= (0x01 << 11);
495 break;
496 case STOP_BIT_2:
497 value |= (0x02 << 11);
498 break;
499 }
500
501 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
502 ftdi_error_return (-1, "Setting new line property failed");
503
504 return 0;
505}
a3da1d95 506
a8f46ddc
TJ
507int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
508{
a3da1d95
GE
509 int ret;
510 int offset = 0;
545820ce 511 int total_written = 0;
c3d95b87 512
a3da1d95 513 while (offset < size) {
948f9ada 514 int write_size = ftdi->writebuffer_chunksize;
a3da1d95
GE
515
516 if (offset+write_size > size)
517 write_size = size-offset;
518
98452d97 519 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
c3d95b87
TJ
520 if (ret < 0)
521 ftdi_error_return(ret, "usb bulk write failed");
a3da1d95 522
c3d95b87 523 total_written += ret;
a3da1d95
GE
524 offset += write_size;
525 }
526
545820ce 527 return total_written;
a3da1d95
GE
528}
529
530
a8f46ddc
TJ
531int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
532{
948f9ada
TJ
533 ftdi->writebuffer_chunksize = chunksize;
534 return 0;
535}
536
537
a8f46ddc
TJ
538int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
539{
948f9ada
TJ
540 *chunksize = ftdi->writebuffer_chunksize;
541 return 0;
542}
cbabb7d3 543
948f9ada 544
a8f46ddc
TJ
545int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
546{
1c733d33 547 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
d9f0cce7 548
948f9ada
TJ
549 // everything we want is still in the readbuffer?
550 if (size <= ftdi->readbuffer_remaining) {
d9f0cce7
TJ
551 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
552
553 // Fix offsets
554 ftdi->readbuffer_remaining -= size;
555 ftdi->readbuffer_offset += size;
556
545820ce 557 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
d9f0cce7
TJ
558
559 return size;
979a145c 560 }
948f9ada
TJ
561 // something still in the readbuffer, but not enough to satisfy 'size'?
562 if (ftdi->readbuffer_remaining != 0) {
d9f0cce7 563 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
979a145c 564
d9f0cce7
TJ
565 // Fix offset
566 offset += ftdi->readbuffer_remaining;
948f9ada 567 }
948f9ada 568 // do the actual USB read
cbabb7d3 569 while (offset < size && ret > 0) {
d9f0cce7
TJ
570 ftdi->readbuffer_remaining = 0;
571 ftdi->readbuffer_offset = 0;
98452d97
TJ
572 /* returns how much received */
573 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
c3d95b87
TJ
574 if (ret < 0)
575 ftdi_error_return(ret, "usb bulk read failed");
98452d97 576
d9f0cce7
TJ
577 if (ret > 2) {
578 // skip FTDI status bytes.
579 // Maybe stored in the future to enable modem use
1c733d33
TJ
580 num_of_chunks = ret / 64;
581 chunk_remains = ret % 64;
582 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
583
d9f0cce7
TJ
584 ftdi->readbuffer_offset += 2;
585 ret -= 2;
1c733d33 586
fde0a89e 587 if (ret > 62) {
1c733d33
TJ
588 for (i = 1; i < num_of_chunks; i++)
589 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
590 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
591 62);
592 if (chunk_remains > 2) {
593 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
594 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
595 chunk_remains-2);
596 ret -= 2*num_of_chunks;
597 } else
598 ret -= 2*(num_of_chunks-1)+chunk_remains;
599 }
d9f0cce7
TJ
600 } else if (ret <= 2) {
601 // no more data to read?
602 return offset;
603 }
d9f0cce7
TJ
604 if (ret > 0) {
605 // data still fits in buf?
606 if (offset+ret <= size) {
607 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
545820ce 608 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
d9f0cce7
TJ
609 offset += ret;
610
53ad271d 611 /* Did we read exactly the right amount of bytes? */
d9f0cce7 612 if (offset == size)
c4446c36
TJ
613 //printf("read_data exact rem %d offset %d\n",
614 //ftdi->readbuffer_remaining, offset);
d9f0cce7
TJ
615 return offset;
616 } else {
617 // only copy part of the data or size <= readbuffer_chunksize
618 int part_size = size-offset;
619 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
98452d97 620
d9f0cce7
TJ
621 ftdi->readbuffer_offset += part_size;
622 ftdi->readbuffer_remaining = ret-part_size;
623 offset += part_size;
624
53ad271d
TJ
625 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
626 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
d9f0cce7
TJ
627
628 return offset;
629 }
630 }
cbabb7d3 631 }
948f9ada 632 // never reached
29c4af7f 633 return -127;
a3da1d95
GE
634}
635
636
a8f46ddc
TJ
637int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
638{
29c4af7f
TJ
639 unsigned char *new_buf;
640
948f9ada
TJ
641 // Invalidate all remaining data
642 ftdi->readbuffer_offset = 0;
643 ftdi->readbuffer_remaining = 0;
644
c3d95b87
TJ
645 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
646 ftdi_error_return(-1, "out of memory for readbuffer");
d9f0cce7 647
948f9ada
TJ
648 ftdi->readbuffer = new_buf;
649 ftdi->readbuffer_chunksize = chunksize;
650
651 return 0;
652}
653
654
a8f46ddc
TJ
655int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
656{
948f9ada
TJ
657 *chunksize = ftdi->readbuffer_chunksize;
658 return 0;
659}
660
661
662
a8f46ddc
TJ
663int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
664{
a3da1d95
GE
665 unsigned short usb_val;
666
d9f0cce7 667 usb_val = bitmask; // low byte: bitmask
3119537f
TJ
668 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
669 usb_val |= (ftdi->bitbang_mode << 8);
670
c3d95b87
TJ
671 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
672 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
673
a3da1d95
GE
674 ftdi->bitbang_enabled = 1;
675 return 0;
676}
677
678
a8f46ddc
TJ
679int ftdi_disable_bitbang(struct ftdi_context *ftdi)
680{
c3d95b87
TJ
681 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
682 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
a3da1d95
GE
683
684 ftdi->bitbang_enabled = 0;
685 return 0;
686}
687
688
c4446c36
TJ
689int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
690{
691 unsigned short usb_val;
692
693 usb_val = bitmask; // low byte: bitmask
694 usb_val |= (mode << 8);
695 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
696 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
697
698 ftdi->bitbang_mode = mode;
699 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
700 return 0;
701}
702
a8f46ddc
TJ
703int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
704{
a3da1d95 705 unsigned short usb_val;
c3d95b87
TJ
706 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
707 ftdi_error_return(-1, "read pins failed");
a3da1d95
GE
708
709 *pins = (unsigned char)usb_val;
710 return 0;
711}
712
713
a8f46ddc
TJ
714int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
715{
a3da1d95
GE
716 unsigned short usb_val;
717
c3d95b87
TJ
718 if (latency < 1)
719 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
a3da1d95 720
d79d2e68 721 usb_val = latency;
c3d95b87
TJ
722 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
723 ftdi_error_return(-2, "unable to set latency timer");
724
a3da1d95
GE
725 return 0;
726}
727
728
a8f46ddc
TJ
729int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
730{
a3da1d95 731 unsigned short usb_val;
c3d95b87
TJ
732 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
733 ftdi_error_return(-1, "reading latency timer failed");
a3da1d95
GE
734
735 *latency = (unsigned char)usb_val;
736 return 0;
737}
738
739
a8f46ddc
TJ
740void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
741{
f396dbad
TJ
742 eeprom->vendor_id = 0x0403;
743 eeprom->product_id = 0x6001;
d9f0cce7 744
b8aa7b35
TJ
745 eeprom->self_powered = 1;
746 eeprom->remote_wakeup = 1;
747 eeprom->BM_type_chip = 1;
d9f0cce7 748
b8aa7b35
TJ
749 eeprom->in_is_isochronous = 0;
750 eeprom->out_is_isochronous = 0;
751 eeprom->suspend_pull_downs = 0;
d9f0cce7 752
b8aa7b35
TJ
753 eeprom->use_serial = 0;
754 eeprom->change_usb_version = 0;
f396dbad 755 eeprom->usb_version = 0x0200;
b8aa7b35 756 eeprom->max_power = 0;
d9f0cce7 757
b8aa7b35
TJ
758 eeprom->manufacturer = NULL;
759 eeprom->product = NULL;
760 eeprom->serial = NULL;
761}
762
763
764/*
4837f98a
TJ
765 ftdi_eeprom_build
766
767 Build binary output from ftdi_eeprom structure.
768 Output is suitable for ftdi_write_eeprom.
769
770 Return codes:
8ed61121 771 positive value: used eeprom size
b8aa7b35
TJ
772 -1: eeprom size (128 bytes) exceeded by custom strings
773*/
a8f46ddc
TJ
774int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
775{
b8aa7b35
TJ
776 unsigned char i, j;
777 unsigned short checksum, value;
778 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
779 int size_check;
780
781 if (eeprom->manufacturer != NULL)
d9f0cce7 782 manufacturer_size = strlen(eeprom->manufacturer);
b8aa7b35 783 if (eeprom->product != NULL)
d9f0cce7 784 product_size = strlen(eeprom->product);
b8aa7b35 785 if (eeprom->serial != NULL)
d9f0cce7 786 serial_size = strlen(eeprom->serial);
b8aa7b35 787
d9f0cce7
TJ
788 size_check = 128; // eeprom is 128 bytes
789 size_check -= 28; // 28 are always in use (fixed)
b8aa7b35
TJ
790 size_check -= manufacturer_size*2;
791 size_check -= product_size*2;
792 size_check -= serial_size*2;
793
794 // eeprom size exceeded?
795 if (size_check < 0)
d9f0cce7 796 return (-1);
b8aa7b35
TJ
797
798 // empty eeprom
799 memset (output, 0, 128);
800
801 // Addr 00: Stay 00 00
802 // Addr 02: Vendor ID
803 output[0x02] = eeprom->vendor_id;
804 output[0x03] = eeprom->vendor_id >> 8;
805
806 // Addr 04: Product ID
807 output[0x04] = eeprom->product_id;
808 output[0x05] = eeprom->product_id >> 8;
809
810 // Addr 06: Device release number (0400h for BM features)
811 output[0x06] = 0x00;
d9f0cce7 812
b8aa7b35 813 if (eeprom->BM_type_chip == 1)
d9f0cce7 814 output[0x07] = 0x04;
b8aa7b35 815 else
d9f0cce7 816 output[0x07] = 0x02;
b8aa7b35
TJ
817
818 // Addr 08: Config descriptor
819 // Bit 1: remote wakeup if 1
820 // Bit 0: self powered if 1
821 //
822 j = 0;
823 if (eeprom->self_powered == 1)
d9f0cce7 824 j = j | 1;
b8aa7b35 825 if (eeprom->remote_wakeup == 1)
d9f0cce7 826 j = j | 2;
b8aa7b35
TJ
827 output[0x08] = j;
828
829 // Addr 09: Max power consumption: max power = value * 2 mA
d9f0cce7
TJ
830 output[0x09] = eeprom->max_power;
831 ;
832
b8aa7b35
TJ
833 // Addr 0A: Chip configuration
834 // Bit 7: 0 - reserved
835 // Bit 6: 0 - reserved
836 // Bit 5: 0 - reserved
837 // Bit 4: 1 - Change USB version
838 // Bit 3: 1 - Use the serial number string
839 // Bit 2: 1 - Enable suspend pull downs for lower power
840 // Bit 1: 1 - Out EndPoint is Isochronous
841 // Bit 0: 1 - In EndPoint is Isochronous
842 //
843 j = 0;
844 if (eeprom->in_is_isochronous == 1)
d9f0cce7 845 j = j | 1;
b8aa7b35 846 if (eeprom->out_is_isochronous == 1)
d9f0cce7 847 j = j | 2;
b8aa7b35 848 if (eeprom->suspend_pull_downs == 1)
d9f0cce7 849 j = j | 4;
b8aa7b35 850 if (eeprom->use_serial == 1)
d9f0cce7 851 j = j | 8;
b8aa7b35 852 if (eeprom->change_usb_version == 1)
d9f0cce7 853 j = j | 16;
b8aa7b35 854 output[0x0A] = j;
d9f0cce7 855
b8aa7b35
TJ
856 // Addr 0B: reserved
857 output[0x0B] = 0x00;
d9f0cce7 858
b8aa7b35
TJ
859 // Addr 0C: USB version low byte when 0x0A bit 4 is set
860 // Addr 0D: USB version high byte when 0x0A bit 4 is set
861 if (eeprom->change_usb_version == 1) {
862 output[0x0C] = eeprom->usb_version;
d9f0cce7 863 output[0x0D] = eeprom->usb_version >> 8;
b8aa7b35
TJ
864 }
865
866
867 // Addr 0E: Offset of the manufacturer string + 0x80
868 output[0x0E] = 0x14 + 0x80;
869
870 // Addr 0F: Length of manufacturer string
871 output[0x0F] = manufacturer_size*2 + 2;
872
873 // Addr 10: Offset of the product string + 0x80, calculated later
874 // Addr 11: Length of product string
875 output[0x11] = product_size*2 + 2;
876
877 // Addr 12: Offset of the serial string + 0x80, calculated later
878 // Addr 13: Length of serial string
879 output[0x13] = serial_size*2 + 2;
880
881 // Dynamic content
a862ddcf 882 output[0x14] = manufacturer_size*2 + 2;
d9f0cce7
TJ
883 output[0x15] = 0x03; // type: string
884
b8aa7b35 885 i = 0x16, j = 0;
d9f0cce7 886
b8aa7b35
TJ
887 // Output manufacturer
888 for (j = 0; j < manufacturer_size; j++) {
d9f0cce7
TJ
889 output[i] = eeprom->manufacturer[j], i++;
890 output[i] = 0x00, i++;
b8aa7b35
TJ
891 }
892
893 // Output product name
d9f0cce7 894 output[0x10] = i + 0x80; // calculate offset
b8aa7b35
TJ
895 output[i] = product_size*2 + 2, i++;
896 output[i] = 0x03, i++;
897 for (j = 0; j < product_size; j++) {
d9f0cce7
TJ
898 output[i] = eeprom->product[j], i++;
899 output[i] = 0x00, i++;
b8aa7b35 900 }
d9f0cce7 901
b8aa7b35 902 // Output serial
d9f0cce7 903 output[0x12] = i + 0x80; // calculate offset
b8aa7b35
TJ
904 output[i] = serial_size*2 + 2, i++;
905 output[i] = 0x03, i++;
906 for (j = 0; j < serial_size; j++) {
d9f0cce7
TJ
907 output[i] = eeprom->serial[j], i++;
908 output[i] = 0x00, i++;
b8aa7b35
TJ
909 }
910
911 // calculate checksum
912 checksum = 0xAAAA;
d9f0cce7 913
b8aa7b35 914 for (i = 0; i < 63; i++) {
d9f0cce7
TJ
915 value = output[i*2];
916 value += output[(i*2)+1] << 8;
b8aa7b35 917
d9f0cce7
TJ
918 checksum = value^checksum;
919 checksum = (checksum << 1) | (checksum >> 15);
b8aa7b35
TJ
920 }
921
922 output[0x7E] = checksum;
d9f0cce7 923 output[0x7F] = checksum >> 8;
b8aa7b35 924
8ed61121 925 return size_check;
b8aa7b35
TJ
926}
927
928
a8f46ddc
TJ
929int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
930{
a3da1d95
GE
931 int i;
932
933 for (i = 0; i < 64; i++) {
c3d95b87
TJ
934 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
935 ftdi_error_return(-1, "reading eeprom failed");
a3da1d95
GE
936 }
937
938 return 0;
939}
940
941
a8f46ddc
TJ
942int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
943{
a3da1d95
GE
944 unsigned short usb_val;
945 int i;
946
947 for (i = 0; i < 64; i++) {
d9f0cce7
TJ
948 usb_val = eeprom[i*2];
949 usb_val += eeprom[(i*2)+1] << 8;
c3d95b87
TJ
950 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
951 ftdi_error_return(-1, "unable to write eeprom");
a3da1d95
GE
952 }
953
954 return 0;
955}
956
957
a8f46ddc
TJ
958int ftdi_erase_eeprom(struct ftdi_context *ftdi)
959{
c3d95b87
TJ
960 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
961 ftdi_error_return(-1, "unable to erase eeprom");
a3da1d95
GE
962
963 return 0;
964}
c3d95b87
TJ
965
966
967char *ftdi_get_error_string (struct ftdi_context *ftdi)
968{
969 return ftdi->error_str;
970}