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