updated changelog
[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
b5ec1820
TJ
17/**
18 \mainpage libftdi API documentation
19
ad397a4b 20 Library to talk to FTDI chips. You find the latest versions of libftdi at
b5ec1820
TJ
21 http://www.intra2net.com/de/produkte/opensource/ftdi/
22
ad397a4b
TJ
23 The library is easy to use. Have a look at this short example:
24 \include simple.c
25
26 More examples can be found in the "examples" directory.
b5ec1820
TJ
27*/
28/** \addtogroup libftdi */
29/* @{ */
30
98452d97 31#include <usb.h>
a8f46ddc 32#include <string.h>
d2f10023 33#include <errno.h>
0e302db6 34
98452d97 35#include "ftdi.h"
a3da1d95 36
21abaf2e 37#define ftdi_error_return(code, str) do { \
2f73e59f 38 ftdi->error_str = str; \
21abaf2e 39 return code; \
d2f10023 40 } while(0);
c3d95b87
TJ
41
42
1941414d
TJ
43/**
44 Initializes a ftdi_context.
4837f98a 45
1941414d 46 \param ftdi pointer to ftdi_context
4837f98a 47
1941414d
TJ
48 \retval 0: all fine
49 \retval -1: couldn't allocate read buffer
50
51 \remark This should be called before all functions
948f9ada 52*/
a8f46ddc
TJ
53int ftdi_init(struct ftdi_context *ftdi)
54{
98452d97 55 ftdi->usb_dev = NULL;
545820ce
TJ
56 ftdi->usb_read_timeout = 5000;
57 ftdi->usb_write_timeout = 5000;
a3da1d95 58
53ad271d 59 ftdi->type = TYPE_BM; /* chip type */
a3da1d95
GE
60 ftdi->baudrate = -1;
61 ftdi->bitbang_enabled = 0;
62
948f9ada
TJ
63 ftdi->readbuffer = NULL;
64 ftdi->readbuffer_offset = 0;
65 ftdi->readbuffer_remaining = 0;
66 ftdi->writebuffer_chunksize = 4096;
67
545820ce
TJ
68 ftdi->interface = 0;
69 ftdi->index = 0;
70 ftdi->in_ep = 0x02;
71 ftdi->out_ep = 0x81;
3119537f 72 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
53ad271d 73
a3da1d95
GE
74 ftdi->error_str = NULL;
75
1c733d33
TJ
76 /* All fine. Now allocate the readbuffer */
77 return ftdi_read_data_set_chunksize(ftdi, 4096);
948f9ada 78}
4837f98a 79
1941414d
TJ
80/**
81 Open selected channels on a chip, otherwise use first channel.
82
83 \param ftdi pointer to ftdi_context
84 \param interface Interface to use for FT2232C chips.
85
86 \retval 0: all fine
87 \retval -1: unknown interface
c4446c36 88*/
0ce2f5fa 89int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
c4446c36
TJ
90{
91 switch (interface) {
92 case INTERFACE_ANY:
93 case INTERFACE_A:
0ce2f5fa 94 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
c4446c36
TJ
95 break;
96 case INTERFACE_B:
97 ftdi->interface = 1;
98 ftdi->index = INTERFACE_B;
99 ftdi->in_ep = 0x04;
100 ftdi->out_ep = 0x83;
101 break;
102 default:
103 ftdi_error_return(-1, "Unknown interface");
104 }
105 return 0;
106}
948f9ada 107
1941414d
TJ
108/**
109 Deinitializes a ftdi_context.
4837f98a 110
1941414d 111 \param ftdi pointer to ftdi_context
4837f98a 112*/
a8f46ddc
TJ
113void ftdi_deinit(struct ftdi_context *ftdi)
114{
948f9ada 115 if (ftdi->readbuffer != NULL) {
d9f0cce7
TJ
116 free(ftdi->readbuffer);
117 ftdi->readbuffer = NULL;
948f9ada 118 }
a3da1d95
GE
119}
120
1941414d
TJ
121/**
122 Use an already open libusb device.
123
124 \param ftdi pointer to ftdi_context
125 \param usb libusb usb_dev_handle to use
4837f98a 126*/
a8f46ddc
TJ
127void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
128{
98452d97
TJ
129 ftdi->usb_dev = usb;
130}
131
132
1941414d
TJ
133/**
134 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
135 needs to be deallocated by ftdi_list_free() after use.
136
137 \param ftdi pointer to ftdi_context
138 \param devlist Pointer where to store list of found devices
139 \param vendor Vendor ID to search for
140 \param product Product ID to search for
edb82cbf 141
1941414d
TJ
142 \retval >0: number of devices found
143 \retval -1: usb_find_busses() failed
144 \retval -2: usb_find_devices() failed
145 \retval -3: out of memory
edb82cbf 146*/
d2f10023 147int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
edb82cbf
TJ
148{
149 struct ftdi_device_list **curdev;
150 struct usb_bus *bus;
151 struct usb_device *dev;
152 int count = 0;
d2f10023 153
edb82cbf
TJ
154 usb_init();
155 if (usb_find_busses() < 0)
156 ftdi_error_return(-1, "usb_find_busses() failed");
157 if (usb_find_devices() < 0)
158 ftdi_error_return(-2, "usb_find_devices() failed");
159
160 curdev = devlist;
161 for (bus = usb_busses; bus; bus = bus->next) {
162 for (dev = bus->devices; dev; dev = dev->next) {
163 if (dev->descriptor.idVendor == vendor
164 && dev->descriptor.idProduct == product)
165 {
166 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
167 if (!*curdev)
168 ftdi_error_return(-3, "out of memory");
d2f10023 169
edb82cbf
TJ
170 (*curdev)->next = NULL;
171 (*curdev)->dev = dev;
172
173 curdev = &(*curdev)->next;
174 count++;
175 }
176 }
177 }
d2f10023 178
edb82cbf
TJ
179 return count;
180}
181
1941414d
TJ
182/**
183 Frees a usb device list.
edb82cbf 184
1941414d 185 \param devlist USB device list created by ftdi_usb_find_all()
edb82cbf 186*/
d2f10023 187void ftdi_list_free(struct ftdi_device_list **devlist)
edb82cbf
TJ
188{
189 struct ftdi_device_list **curdev;
190 for (; *devlist == NULL; devlist = curdev) {
191 curdev = &(*devlist)->next;
192 free(*devlist);
193 }
194
195 devlist = NULL;
196}
197
1941414d
TJ
198/**
199 Opens a ftdi device given by a usb_device.
7b18bef6 200
1941414d
TJ
201 \param ftdi pointer to ftdi_context
202 \param dev libusb usb_dev to use
203
204 \retval 0: all fine
205 \retval -4: unable to open device
206 \retval -5: unable to claim device
207 \retval -6: reset failed
208 \retval -7: set baudrate failed
7b18bef6
TJ
209*/
210int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
211{
d2f10023 212 int detach_errno = 0;
7b18bef6
TJ
213 if (!(ftdi->usb_dev = usb_open(dev)))
214 ftdi_error_return(-4, "usb_open() failed");
d2f10023
TJ
215
216#ifdef LIBUSB_HAS_GET_DRIVER_NP
217 // Try to detach ftdi_sio kernel module
218 // Returns ENODATA if driver is not loaded
219 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
220 detach_errno = errno;
221#endif
222
7b18bef6
TJ
223 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
224 usb_close (ftdi->usb_dev);
d2f10023
TJ
225 if (detach_errno == EPERM) {
226 ftdi_error_return(-8, "inappropriate permissions on device!");
227 } else {
228 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
229 }
7b18bef6
TJ
230 }
231
232 if (ftdi_usb_reset (ftdi) != 0) {
233 usb_close (ftdi->usb_dev);
234 ftdi_error_return(-6, "ftdi_usb_reset failed");
235 }
236
237 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
238 usb_close (ftdi->usb_dev);
239 ftdi_error_return(-7, "set baudrate failed");
240 }
241
242 // Try to guess chip type
243 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
244 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
245 && dev->descriptor.iSerialNumber == 0))
246 ftdi->type = TYPE_BM;
247 else if (dev->descriptor.bcdDevice == 0x200)
248 ftdi->type = TYPE_AM;
249 else if (dev->descriptor.bcdDevice == 0x500) {
250 ftdi->type = TYPE_2232C;
251 if (!ftdi->index)
252 ftdi->index = INTERFACE_A;
253 }
254
255 ftdi_error_return(0, "all fine");
256}
257
1941414d
TJ
258/**
259 Opens the first device with a given vendor and product ids.
260
261 \param ftdi pointer to ftdi_context
262 \param vendor Vendor ID
263 \param product Product ID
264
9bec2387 265 \retval same as ftdi_usb_open_desc()
1941414d 266*/
edb82cbf
TJ
267int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
268{
269 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
270}
271
1941414d
TJ
272/**
273 Opens the first device with a given, vendor id, product id,
274 description and serial.
275
276 \param ftdi pointer to ftdi_context
277 \param vendor Vendor ID
278 \param product Product ID
279 \param description Description to search for. Use NULL if not needed.
280 \param serial Serial to search for. Use NULL if not needed.
281
282 \retval 0: all fine
283 \retval -1: usb_find_busses() failed
284 \retval -2: usb_find_devices() failed
285 \retval -3: usb device not found
286 \retval -4: unable to open device
287 \retval -5: unable to claim device
288 \retval -6: reset failed
289 \retval -7: set baudrate failed
290 \retval -8: get product description failed
291 \retval -9: get serial number failed
292 \retval -10: unable to close device
a3da1d95 293*/
04e1ea0a 294int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
a8f46ddc
TJ
295 const char* description, const char* serial)
296{
98452d97
TJ
297 struct usb_bus *bus;
298 struct usb_device *dev;
c3d95b87 299 char string[256];
98452d97
TJ
300
301 usb_init();
302
c3d95b87
TJ
303 if (usb_find_busses() < 0)
304 ftdi_error_return(-1, "usb_find_busses() failed");
c3d95b87 305 if (usb_find_devices() < 0)
edb82cbf 306 ftdi_error_return(-2, "usb_find_devices() failed");
a3da1d95 307
98452d97
TJ
308 for (bus = usb_busses; bus; bus = bus->next) {
309 for (dev = bus->devices; dev; dev = dev->next) {
a8f46ddc 310 if (dev->descriptor.idVendor == vendor
c3d95b87
TJ
311 && dev->descriptor.idProduct == product) {
312 if (!(ftdi->usb_dev = usb_open(dev)))
313 ftdi_error_return(-4, "usb_open() failed");
314
a8f46ddc
TJ
315 if (description != NULL) {
316 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
c3d95b87
TJ
317 usb_close (ftdi->usb_dev);
318 ftdi_error_return(-8, "unable to fetch product description");
98452d97 319 }
a8f46ddc 320 if (strncmp(string, description, sizeof(string)) != 0) {
edb82cbf
TJ
321 if (usb_close (ftdi->usb_dev) != 0)
322 ftdi_error_return(-10, "unable to close device");
a8f46ddc
TJ
323 continue;
324 }
325 }
326 if (serial != NULL) {
327 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
c3d95b87
TJ
328 usb_close (ftdi->usb_dev);
329 ftdi_error_return(-9, "unable to fetch serial number");
a8f46ddc
TJ
330 }
331 if (strncmp(string, serial, sizeof(string)) != 0) {
a8f46ddc 332 if (usb_close (ftdi->usb_dev) != 0)
edb82cbf 333 ftdi_error_return(-10, "unable to close device");
a8f46ddc
TJ
334 continue;
335 }
336 }
98452d97 337
edb82cbf
TJ
338 if (usb_close (ftdi->usb_dev) != 0)
339 ftdi_error_return(-10, "unable to close device");
d2f10023 340
edb82cbf 341 return ftdi_usb_open_dev(ftdi, dev);
98452d97
TJ
342 }
343 }
98452d97 344 }
a3da1d95 345
98452d97 346 // device not found
c3d95b87 347 ftdi_error_return(-3, "device not found");
a3da1d95
GE
348}
349
1941414d
TJ
350/**
351 Resets the ftdi device.
a3da1d95 352
1941414d
TJ
353 \param ftdi pointer to ftdi_context
354
355 \retval 0: all fine
356 \retval -1: FTDI reset failed
4837f98a 357*/
edb82cbf 358int ftdi_usb_reset(struct ftdi_context *ftdi)
a8f46ddc 359{
c3d95b87
TJ
360 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
361 ftdi_error_return(-1,"FTDI reset failed");
362
545820ce 363 // Invalidate data in the readbuffer
bfcee05b
TJ
364 ftdi->readbuffer_offset = 0;
365 ftdi->readbuffer_remaining = 0;
366
a3da1d95
GE
367 return 0;
368}
369
1941414d
TJ
370/**
371 Clears the buffers on the chip.
372
373 \param ftdi pointer to ftdi_context
4837f98a 374
1941414d
TJ
375 \retval 0: all fine
376 \retval -1: write buffer purge failed
377 \retval -2: read buffer purge failed
4837f98a 378*/
a8f46ddc
TJ
379int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
380{
c3d95b87
TJ
381 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
382 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
383
545820ce 384 // Invalidate data in the readbuffer
bfcee05b
TJ
385 ftdi->readbuffer_offset = 0;
386 ftdi->readbuffer_remaining = 0;
a60be878 387
c3d95b87
TJ
388 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
389 ftdi_error_return(-2, "FTDI purge of TX buffer failed");
545820ce 390
a60be878
TJ
391 return 0;
392}
a3da1d95 393
1941414d
TJ
394/**
395 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
396
397 \param ftdi pointer to ftdi_context
398
399 \retval 0: all fine
400 \retval -1: usb_release failed
401 \retval -2: usb_close failed
a3da1d95 402*/
a8f46ddc
TJ
403int ftdi_usb_close(struct ftdi_context *ftdi)
404{
a3da1d95
GE
405 int rtn = 0;
406
98452d97 407 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
a3da1d95 408 rtn = -1;
98452d97
TJ
409
410 if (usb_close (ftdi->usb_dev) != 0)
a3da1d95 411 rtn = -2;
98452d97 412
a3da1d95
GE
413 return rtn;
414}
415
a3da1d95 416/*
53ad271d
TJ
417 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
418 Function is only used internally
b5ec1820 419 \internal
53ad271d 420*/
0126d22e 421static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
a8f46ddc
TJ
422 unsigned short *value, unsigned short *index)
423{
53ad271d
TJ
424 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
425 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
426 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
427 int divisor, best_divisor, best_baud, best_baud_diff;
428 unsigned long encoded_divisor;
429 int i;
430
431 if (baudrate <= 0) {
432 // Return error
433 return -1;
434 }
435
436 divisor = 24000000 / baudrate;
437
0126d22e 438 if (ftdi->type == TYPE_AM) {
53ad271d
TJ
439 // Round down to supported fraction (AM only)
440 divisor -= am_adjust_dn[divisor & 7];
441 }
442
443 // Try this divisor and the one above it (because division rounds down)
444 best_divisor = 0;
445 best_baud = 0;
446 best_baud_diff = 0;
447 for (i = 0; i < 2; i++) {
448 int try_divisor = divisor + i;
449 int baud_estimate;
450 int baud_diff;
451
452 // Round up to supported divisor value
df612d35 453 if (try_divisor <= 8) {
53ad271d
TJ
454 // Round up to minimum supported divisor
455 try_divisor = 8;
0126d22e 456 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
53ad271d
TJ
457 // BM doesn't support divisors 9 through 11 inclusive
458 try_divisor = 12;
459 } else if (divisor < 16) {
460 // AM doesn't support divisors 9 through 15 inclusive
461 try_divisor = 16;
462 } else {
0126d22e 463 if (ftdi->type == TYPE_AM) {
53ad271d
TJ
464 // Round up to supported fraction (AM only)
465 try_divisor += am_adjust_up[try_divisor & 7];
466 if (try_divisor > 0x1FFF8) {
467 // Round down to maximum supported divisor value (for AM)
468 try_divisor = 0x1FFF8;
469 }
470 } else {
471 if (try_divisor > 0x1FFFF) {
472 // Round down to maximum supported divisor value (for BM)
473 try_divisor = 0x1FFFF;
474 }
475 }
476 }
477 // Get estimated baud rate (to nearest integer)
478 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
479 // Get absolute difference from requested baud rate
480 if (baud_estimate < baudrate) {
481 baud_diff = baudrate - baud_estimate;
482 } else {
483 baud_diff = baud_estimate - baudrate;
484 }
485 if (i == 0 || baud_diff < best_baud_diff) {
486 // Closest to requested baud rate so far
487 best_divisor = try_divisor;
488 best_baud = baud_estimate;
489 best_baud_diff = baud_diff;
490 if (baud_diff == 0) {
491 // Spot on! No point trying
492 break;
493 }
494 }
495 }
496 // Encode the best divisor value
497 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
498 // Deal with special cases for encoded value
499 if (encoded_divisor == 1) {
4837f98a 500 encoded_divisor = 0; // 3000000 baud
53ad271d 501 } else if (encoded_divisor == 0x4001) {
4837f98a 502 encoded_divisor = 1; // 2000000 baud (BM only)
53ad271d
TJ
503 }
504 // Split into "value" and "index" values
505 *value = (unsigned short)(encoded_divisor & 0xFFFF);
de22df10 506 if(ftdi->type == TYPE_2232C) {
0126d22e
TJ
507 *index = (unsigned short)(encoded_divisor >> 8);
508 *index &= 0xFF00;
a9c57c05 509 *index |= ftdi->index;
0126d22e
TJ
510 }
511 else
512 *index = (unsigned short)(encoded_divisor >> 16);
c3d95b87 513
53ad271d
TJ
514 // Return the nearest baud rate
515 return best_baud;
516}
517
1941414d 518/**
9bec2387 519 Sets the chip baud rate
1941414d
TJ
520
521 \param ftdi pointer to ftdi_context
9bec2387 522 \param baudrate baud rate to set
1941414d
TJ
523
524 \retval 0: all fine
525 \retval -1: invalid baudrate
526 \retval -2: setting baudrate failed
a3da1d95 527*/
a8f46ddc
TJ
528int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
529{
53ad271d
TJ
530 unsigned short value, index;
531 int actual_baudrate;
a3da1d95
GE
532
533 if (ftdi->bitbang_enabled) {
534 baudrate = baudrate*4;
535 }
536
25707904 537 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
c3d95b87
TJ
538 if (actual_baudrate <= 0)
539 ftdi_error_return (-1, "Silly baudrate <= 0.");
a3da1d95 540
53ad271d
TJ
541 // Check within tolerance (about 5%)
542 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
543 || ((actual_baudrate < baudrate)
544 ? (actual_baudrate * 21 < baudrate * 20)
c3d95b87
TJ
545 : (baudrate * 21 < actual_baudrate * 20)))
546 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
545820ce 547
c3d95b87
TJ
548 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
549 ftdi_error_return (-2, "Setting new baudrate failed");
a3da1d95
GE
550
551 ftdi->baudrate = baudrate;
552 return 0;
553}
554
1941414d
TJ
555/**
556 Set (RS232) line characteristics by Alain Abbas
4837f98a 557
1941414d
TJ
558 \param ftdi pointer to ftdi_context
559 \param bits Number of bits
560 \param sbit Number of stop bits
561 \param parity Parity mode
562
563 \retval 0: all fine
564 \retval -1: Setting line property failed
2f73e59f
TJ
565*/
566int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
d2f10023 567 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
2f73e59f
TJ
568{
569 unsigned short value = bits;
570
571 switch(parity) {
572 case NONE:
573 value |= (0x00 << 8);
574 break;
575 case ODD:
576 value |= (0x01 << 8);
577 break;
578 case EVEN:
579 value |= (0x02 << 8);
580 break;
581 case MARK:
582 value |= (0x03 << 8);
583 break;
584 case SPACE:
585 value |= (0x04 << 8);
586 break;
587 }
d2f10023 588
2f73e59f
TJ
589 switch(sbit) {
590 case STOP_BIT_1:
591 value |= (0x00 << 11);
592 break;
593 case STOP_BIT_15:
594 value |= (0x01 << 11);
595 break;
596 case STOP_BIT_2:
597 value |= (0x02 << 11);
598 break;
599 }
d2f10023 600
2f73e59f
TJ
601 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
602 ftdi_error_return (-1, "Setting new line property failed");
d2f10023 603
2f73e59f
TJ
604 return 0;
605}
a3da1d95 606
1941414d
TJ
607/**
608 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
609
610 \param ftdi pointer to ftdi_context
611 \param buf Buffer with the data
612 \param size Size of the buffer
613
614 \retval <0: error code from usb_bulk_write()
615 \retval >0: number of bytes written
616*/
a8f46ddc
TJ
617int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
618{
a3da1d95
GE
619 int ret;
620 int offset = 0;
545820ce 621 int total_written = 0;
c3d95b87 622
a3da1d95 623 while (offset < size) {
948f9ada 624 int write_size = ftdi->writebuffer_chunksize;
a3da1d95
GE
625
626 if (offset+write_size > size)
627 write_size = size-offset;
628
98452d97 629 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
c3d95b87
TJ
630 if (ret < 0)
631 ftdi_error_return(ret, "usb bulk write failed");
a3da1d95 632
c3d95b87 633 total_written += ret;
a3da1d95
GE
634 offset += write_size;
635 }
636
545820ce 637 return total_written;
a3da1d95
GE
638}
639
1941414d
TJ
640/**
641 Configure write buffer chunk size.
642 Default is 4096.
643
644 \param ftdi pointer to ftdi_context
645 \param chunksize Chunk size
a3da1d95 646
1941414d
TJ
647 \retval 0: all fine
648*/
a8f46ddc
TJ
649int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
650{
948f9ada
TJ
651 ftdi->writebuffer_chunksize = chunksize;
652 return 0;
653}
654
1941414d
TJ
655/**
656 Get write buffer chunk size.
657
658 \param ftdi pointer to ftdi_context
659 \param chunksize Pointer to store chunk size in
948f9ada 660
1941414d
TJ
661 \retval 0: all fine
662*/
a8f46ddc
TJ
663int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
664{
948f9ada
TJ
665 *chunksize = ftdi->writebuffer_chunksize;
666 return 0;
667}
cbabb7d3 668
1941414d
TJ
669/**
670 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
671
672 Automatically strips the two modem status bytes transfered during every read.
948f9ada 673
1941414d
TJ
674 \param ftdi pointer to ftdi_context
675 \param buf Buffer to store data in
676 \param size Size of the buffer
677
678 \retval <0: error code from usb_bulk_read()
679 \retval >0: number of bytes read
680
681 \remark This function is not useful in bitbang mode.
682 Use ftdi_read_pins() to get the current state of the pins.
683*/
a8f46ddc
TJ
684int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
685{
1c733d33 686 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
d9f0cce7 687
948f9ada
TJ
688 // everything we want is still in the readbuffer?
689 if (size <= ftdi->readbuffer_remaining) {
d9f0cce7
TJ
690 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
691
692 // Fix offsets
693 ftdi->readbuffer_remaining -= size;
694 ftdi->readbuffer_offset += size;
695
545820ce 696 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
d9f0cce7
TJ
697
698 return size;
979a145c 699 }
948f9ada
TJ
700 // something still in the readbuffer, but not enough to satisfy 'size'?
701 if (ftdi->readbuffer_remaining != 0) {
d9f0cce7 702 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
979a145c 703
d9f0cce7
TJ
704 // Fix offset
705 offset += ftdi->readbuffer_remaining;
948f9ada 706 }
948f9ada 707 // do the actual USB read
cbabb7d3 708 while (offset < size && ret > 0) {
d9f0cce7
TJ
709 ftdi->readbuffer_remaining = 0;
710 ftdi->readbuffer_offset = 0;
98452d97
TJ
711 /* returns how much received */
712 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
c3d95b87
TJ
713 if (ret < 0)
714 ftdi_error_return(ret, "usb bulk read failed");
98452d97 715
d9f0cce7
TJ
716 if (ret > 2) {
717 // skip FTDI status bytes.
718 // Maybe stored in the future to enable modem use
1c733d33
TJ
719 num_of_chunks = ret / 64;
720 chunk_remains = ret % 64;
721 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
722
d9f0cce7
TJ
723 ftdi->readbuffer_offset += 2;
724 ret -= 2;
1c733d33 725
fde0a89e 726 if (ret > 62) {
1c733d33
TJ
727 for (i = 1; i < num_of_chunks; i++)
728 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
729 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
730 62);
731 if (chunk_remains > 2) {
732 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
733 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
734 chunk_remains-2);
735 ret -= 2*num_of_chunks;
736 } else
737 ret -= 2*(num_of_chunks-1)+chunk_remains;
738 }
d9f0cce7
TJ
739 } else if (ret <= 2) {
740 // no more data to read?
741 return offset;
742 }
d9f0cce7
TJ
743 if (ret > 0) {
744 // data still fits in buf?
745 if (offset+ret <= size) {
746 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
545820ce 747 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
d9f0cce7
TJ
748 offset += ret;
749
53ad271d 750 /* Did we read exactly the right amount of bytes? */
d9f0cce7 751 if (offset == size)
c4446c36
TJ
752 //printf("read_data exact rem %d offset %d\n",
753 //ftdi->readbuffer_remaining, offset);
d9f0cce7
TJ
754 return offset;
755 } else {
756 // only copy part of the data or size <= readbuffer_chunksize
757 int part_size = size-offset;
758 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
98452d97 759
d9f0cce7
TJ
760 ftdi->readbuffer_offset += part_size;
761 ftdi->readbuffer_remaining = ret-part_size;
762 offset += part_size;
763
53ad271d
TJ
764 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
765 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
d9f0cce7
TJ
766
767 return offset;
768 }
769 }
cbabb7d3 770 }
948f9ada 771 // never reached
29c4af7f 772 return -127;
a3da1d95
GE
773}
774
1941414d
TJ
775/**
776 Configure read buffer chunk size.
777 Default is 4096.
778
779 Automatically reallocates the buffer.
a3da1d95 780
1941414d
TJ
781 \param ftdi pointer to ftdi_context
782 \param chunksize Chunk size
783
784 \retval 0: all fine
785*/
a8f46ddc
TJ
786int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
787{
29c4af7f
TJ
788 unsigned char *new_buf;
789
948f9ada
TJ
790 // Invalidate all remaining data
791 ftdi->readbuffer_offset = 0;
792 ftdi->readbuffer_remaining = 0;
793
c3d95b87
TJ
794 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
795 ftdi_error_return(-1, "out of memory for readbuffer");
d9f0cce7 796
948f9ada
TJ
797 ftdi->readbuffer = new_buf;
798 ftdi->readbuffer_chunksize = chunksize;
799
800 return 0;
801}
802
1941414d
TJ
803/**
804 Get read buffer chunk size.
948f9ada 805
1941414d
TJ
806 \param ftdi pointer to ftdi_context
807 \param chunksize Pointer to store chunk size in
808
809 \retval 0: all fine
810*/
a8f46ddc
TJ
811int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
812{
948f9ada
TJ
813 *chunksize = ftdi->readbuffer_chunksize;
814 return 0;
815}
816
817
1941414d
TJ
818/**
819 Enable bitbang mode.
948f9ada 820
1941414d
TJ
821 For advanced bitbang modes of the FT2232C chip use ftdi_set_bitmode().
822
823 \param ftdi pointer to ftdi_context
824 \param bitmask Bitmask to configure lines.
825 HIGH/ON value configures a line as output.
826
827 \retval 0: all fine
828 \retval -1: can't enable bitbang mode
829*/
a8f46ddc
TJ
830int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
831{
a3da1d95
GE
832 unsigned short usb_val;
833
d9f0cce7 834 usb_val = bitmask; // low byte: bitmask
3119537f
TJ
835 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
836 usb_val |= (ftdi->bitbang_mode << 8);
837
c3d95b87
TJ
838 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
839 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
840
a3da1d95
GE
841 ftdi->bitbang_enabled = 1;
842 return 0;
843}
844
1941414d
TJ
845/**
846 Disable bitbang mode.
a3da1d95 847
1941414d
TJ
848 \param ftdi pointer to ftdi_context
849
850 \retval 0: all fine
851 \retval -1: can't disable bitbang mode
852*/
a8f46ddc
TJ
853int ftdi_disable_bitbang(struct ftdi_context *ftdi)
854{
c3d95b87
TJ
855 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
856 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
a3da1d95
GE
857
858 ftdi->bitbang_enabled = 0;
859 return 0;
860}
861
1941414d
TJ
862/**
863 Enable advanced bitbang mode for FT2232C chips.
a3da1d95 864
1941414d
TJ
865 \param ftdi pointer to ftdi_context
866 \param bitmask Bitmask to configure lines.
867 HIGH/ON value configures a line as output.
868 \param mode Bitbang mode: 1 for normal mode, 2 for SPI mode
869
870 \retval 0: all fine
871 \retval -1: can't enable bitbang mode
872*/
c4446c36
TJ
873int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
874{
875 unsigned short usb_val;
876
877 usb_val = bitmask; // low byte: bitmask
878 usb_val |= (mode << 8);
879 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
880 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
881
882 ftdi->bitbang_mode = mode;
883 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
884 return 0;
885}
886
1941414d
TJ
887/**
888 Directly read pin state. Useful for bitbang mode.
889
890 \param ftdi pointer to ftdi_context
891 \param pins Pointer to store pins into
892
893 \retval 0: all fine
894 \retval -1: read pins failed
895*/
a8f46ddc
TJ
896int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
897{
85f3c596 898 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1)
c3d95b87 899 ftdi_error_return(-1, "read pins failed");
a3da1d95 900
a3da1d95
GE
901 return 0;
902}
903
1941414d
TJ
904/**
905 Set latency timer
906
907 The FTDI chip keeps data in the internal buffer for a specific
908 amount of time if the buffer is not full yet to decrease
909 load on the usb bus.
a3da1d95 910
1941414d
TJ
911 \param ftdi pointer to ftdi_context
912 \param latency Value between 1 and 255
913
914 \retval 0: all fine
915 \retval -1: latency out of range
916 \retval -2: unable to set latency timer
917*/
a8f46ddc
TJ
918int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
919{
a3da1d95
GE
920 unsigned short usb_val;
921
c3d95b87
TJ
922 if (latency < 1)
923 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
a3da1d95 924
d79d2e68 925 usb_val = latency;
c3d95b87
TJ
926 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
927 ftdi_error_return(-2, "unable to set latency timer");
928
a3da1d95
GE
929 return 0;
930}
931
1941414d
TJ
932/**
933 Get latency timer
a3da1d95 934
1941414d
TJ
935 \param ftdi pointer to ftdi_context
936 \param latency Pointer to store latency value in
937
938 \retval 0: all fine
939 \retval -1: unable to get latency timer
940*/
a8f46ddc
TJ
941int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
942{
a3da1d95 943 unsigned short usb_val;
c3d95b87
TJ
944 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
945 ftdi_error_return(-1, "reading latency timer failed");
a3da1d95
GE
946
947 *latency = (unsigned char)usb_val;
948 return 0;
949}
950
1941414d
TJ
951/**
952 Init eeprom with default values.
a3da1d95 953
1941414d
TJ
954 \param eeprom Pointer to ftdi_eeprom
955*/
a8f46ddc
TJ
956void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
957{
f396dbad
TJ
958 eeprom->vendor_id = 0x0403;
959 eeprom->product_id = 0x6001;
d9f0cce7 960
b8aa7b35
TJ
961 eeprom->self_powered = 1;
962 eeprom->remote_wakeup = 1;
963 eeprom->BM_type_chip = 1;
d9f0cce7 964
b8aa7b35
TJ
965 eeprom->in_is_isochronous = 0;
966 eeprom->out_is_isochronous = 0;
967 eeprom->suspend_pull_downs = 0;
d9f0cce7 968
b8aa7b35
TJ
969 eeprom->use_serial = 0;
970 eeprom->change_usb_version = 0;
f396dbad 971 eeprom->usb_version = 0x0200;
b8aa7b35 972 eeprom->max_power = 0;
d9f0cce7 973
b8aa7b35
TJ
974 eeprom->manufacturer = NULL;
975 eeprom->product = NULL;
976 eeprom->serial = NULL;
977}
978
1941414d
TJ
979/**
980 Build binary output from ftdi_eeprom structure.
981 Output is suitable for ftdi_write_eeprom().
b8aa7b35 982
1941414d
TJ
983 \param eeprom Pointer to ftdi_eeprom
984 \param output Buffer of 128 bytes to store eeprom image to
985
986 \retval >0: used eeprom size
987 \retval -1: eeprom size (128 bytes) exceeded by custom strings
b8aa7b35 988*/
a8f46ddc
TJ
989int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
990{
b8aa7b35
TJ
991 unsigned char i, j;
992 unsigned short checksum, value;
993 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
994 int size_check;
995
996 if (eeprom->manufacturer != NULL)
d9f0cce7 997 manufacturer_size = strlen(eeprom->manufacturer);
b8aa7b35 998 if (eeprom->product != NULL)
d9f0cce7 999 product_size = strlen(eeprom->product);
b8aa7b35 1000 if (eeprom->serial != NULL)
d9f0cce7 1001 serial_size = strlen(eeprom->serial);
b8aa7b35 1002
d9f0cce7
TJ
1003 size_check = 128; // eeprom is 128 bytes
1004 size_check -= 28; // 28 are always in use (fixed)
b8aa7b35
TJ
1005 size_check -= manufacturer_size*2;
1006 size_check -= product_size*2;
1007 size_check -= serial_size*2;
1008
1009 // eeprom size exceeded?
1010 if (size_check < 0)
d9f0cce7 1011 return (-1);
b8aa7b35
TJ
1012
1013 // empty eeprom
1014 memset (output, 0, 128);
1015
1016 // Addr 00: Stay 00 00
1017 // Addr 02: Vendor ID
1018 output[0x02] = eeprom->vendor_id;
1019 output[0x03] = eeprom->vendor_id >> 8;
1020
1021 // Addr 04: Product ID
1022 output[0x04] = eeprom->product_id;
1023 output[0x05] = eeprom->product_id >> 8;
1024
1025 // Addr 06: Device release number (0400h for BM features)
1026 output[0x06] = 0x00;
d9f0cce7 1027
b8aa7b35 1028 if (eeprom->BM_type_chip == 1)
d9f0cce7 1029 output[0x07] = 0x04;
b8aa7b35 1030 else
d9f0cce7 1031 output[0x07] = 0x02;
b8aa7b35
TJ
1032
1033 // Addr 08: Config descriptor
1034 // Bit 1: remote wakeup if 1
1035 // Bit 0: self powered if 1
1036 //
1037 j = 0;
1038 if (eeprom->self_powered == 1)
d9f0cce7 1039 j = j | 1;
b8aa7b35 1040 if (eeprom->remote_wakeup == 1)
d9f0cce7 1041 j = j | 2;
b8aa7b35
TJ
1042 output[0x08] = j;
1043
1044 // Addr 09: Max power consumption: max power = value * 2 mA
d9f0cce7
TJ
1045 output[0x09] = eeprom->max_power;
1046 ;
1047
b8aa7b35
TJ
1048 // Addr 0A: Chip configuration
1049 // Bit 7: 0 - reserved
1050 // Bit 6: 0 - reserved
1051 // Bit 5: 0 - reserved
1052 // Bit 4: 1 - Change USB version
1053 // Bit 3: 1 - Use the serial number string
1054 // Bit 2: 1 - Enable suspend pull downs for lower power
1055 // Bit 1: 1 - Out EndPoint is Isochronous
1056 // Bit 0: 1 - In EndPoint is Isochronous
1057 //
1058 j = 0;
1059 if (eeprom->in_is_isochronous == 1)
d9f0cce7 1060 j = j | 1;
b8aa7b35 1061 if (eeprom->out_is_isochronous == 1)
d9f0cce7 1062 j = j | 2;
b8aa7b35 1063 if (eeprom->suspend_pull_downs == 1)
d9f0cce7 1064 j = j | 4;
b8aa7b35 1065 if (eeprom->use_serial == 1)
d9f0cce7 1066 j = j | 8;
b8aa7b35 1067 if (eeprom->change_usb_version == 1)
d9f0cce7 1068 j = j | 16;
b8aa7b35 1069 output[0x0A] = j;
d9f0cce7 1070
b8aa7b35
TJ
1071 // Addr 0B: reserved
1072 output[0x0B] = 0x00;
d9f0cce7 1073
b8aa7b35
TJ
1074 // Addr 0C: USB version low byte when 0x0A bit 4 is set
1075 // Addr 0D: USB version high byte when 0x0A bit 4 is set
1076 if (eeprom->change_usb_version == 1) {
1077 output[0x0C] = eeprom->usb_version;
d9f0cce7 1078 output[0x0D] = eeprom->usb_version >> 8;
b8aa7b35
TJ
1079 }
1080
1081
1082 // Addr 0E: Offset of the manufacturer string + 0x80
1083 output[0x0E] = 0x14 + 0x80;
1084
1085 // Addr 0F: Length of manufacturer string
1086 output[0x0F] = manufacturer_size*2 + 2;
1087
1088 // Addr 10: Offset of the product string + 0x80, calculated later
1089 // Addr 11: Length of product string
1090 output[0x11] = product_size*2 + 2;
1091
1092 // Addr 12: Offset of the serial string + 0x80, calculated later
1093 // Addr 13: Length of serial string
1094 output[0x13] = serial_size*2 + 2;
1095
1096 // Dynamic content
a862ddcf 1097 output[0x14] = manufacturer_size*2 + 2;
d9f0cce7
TJ
1098 output[0x15] = 0x03; // type: string
1099
b8aa7b35 1100 i = 0x16, j = 0;
d9f0cce7 1101
b8aa7b35
TJ
1102 // Output manufacturer
1103 for (j = 0; j < manufacturer_size; j++) {
d9f0cce7
TJ
1104 output[i] = eeprom->manufacturer[j], i++;
1105 output[i] = 0x00, i++;
b8aa7b35
TJ
1106 }
1107
1108 // Output product name
d9f0cce7 1109 output[0x10] = i + 0x80; // calculate offset
b8aa7b35
TJ
1110 output[i] = product_size*2 + 2, i++;
1111 output[i] = 0x03, i++;
1112 for (j = 0; j < product_size; j++) {
d9f0cce7
TJ
1113 output[i] = eeprom->product[j], i++;
1114 output[i] = 0x00, i++;
b8aa7b35 1115 }
d9f0cce7 1116
b8aa7b35 1117 // Output serial
d9f0cce7 1118 output[0x12] = i + 0x80; // calculate offset
b8aa7b35
TJ
1119 output[i] = serial_size*2 + 2, i++;
1120 output[i] = 0x03, i++;
1121 for (j = 0; j < serial_size; j++) {
d9f0cce7
TJ
1122 output[i] = eeprom->serial[j], i++;
1123 output[i] = 0x00, i++;
b8aa7b35
TJ
1124 }
1125
1126 // calculate checksum
1127 checksum = 0xAAAA;
d9f0cce7 1128
b8aa7b35 1129 for (i = 0; i < 63; i++) {
d9f0cce7
TJ
1130 value = output[i*2];
1131 value += output[(i*2)+1] << 8;
b8aa7b35 1132
d9f0cce7
TJ
1133 checksum = value^checksum;
1134 checksum = (checksum << 1) | (checksum >> 15);
b8aa7b35
TJ
1135 }
1136
1137 output[0x7E] = checksum;
d9f0cce7 1138 output[0x7F] = checksum >> 8;
b8aa7b35 1139
8ed61121 1140 return size_check;
b8aa7b35
TJ
1141}
1142
1941414d
TJ
1143/**
1144 Read eeprom
1145
1146 \param ftdi pointer to ftdi_context
1147 \param eeprom Pointer to store eeprom into
b8aa7b35 1148
1941414d
TJ
1149 \retval 0: all fine
1150 \retval -1: read failed
1151*/
a8f46ddc
TJ
1152int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1153{
a3da1d95
GE
1154 int i;
1155
1156 for (i = 0; i < 64; i++) {
c3d95b87
TJ
1157 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
1158 ftdi_error_return(-1, "reading eeprom failed");
a3da1d95
GE
1159 }
1160
1161 return 0;
1162}
1163
1941414d
TJ
1164/**
1165 Write eeprom
a3da1d95 1166
1941414d
TJ
1167 \param ftdi pointer to ftdi_context
1168 \param eeprom Pointer to read eeprom from
1169
1170 \retval 0: all fine
1171 \retval -1: read failed
1172*/
a8f46ddc
TJ
1173int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1174{
a3da1d95
GE
1175 unsigned short usb_val;
1176 int i;
1177
1178 for (i = 0; i < 64; i++) {
d9f0cce7
TJ
1179 usb_val = eeprom[i*2];
1180 usb_val += eeprom[(i*2)+1] << 8;
c3d95b87
TJ
1181 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
1182 ftdi_error_return(-1, "unable to write eeprom");
a3da1d95
GE
1183 }
1184
1185 return 0;
1186}
1187
1941414d
TJ
1188/**
1189 Erase eeprom
a3da1d95 1190
1941414d
TJ
1191 \param ftdi pointer to ftdi_context
1192
1193 \retval 0: all fine
1194 \retval -1: erase failed
1195*/
a8f46ddc
TJ
1196int ftdi_erase_eeprom(struct ftdi_context *ftdi)
1197{
c3d95b87
TJ
1198 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
1199 ftdi_error_return(-1, "unable to erase eeprom");
a3da1d95
GE
1200
1201 return 0;
1202}
c3d95b87 1203
1941414d
TJ
1204/**
1205 Get string representation for last error code
c3d95b87 1206
1941414d
TJ
1207 \param ftdi pointer to ftdi_context
1208
1209 \retval Pointer to error string
1210*/
c3d95b87
TJ
1211char *ftdi_get_error_string (struct ftdi_context *ftdi)
1212{
1213 return ftdi->error_str;
1214}
a01d31e2 1215
9bec2387
TJ
1216/*
1217 Flow control code by Lorenz Moesenlechner (lorenz@hcilab.org)
1218 and Matthias Kranz (matthias@hcilab.org)
1219*/
1941414d
TJ
1220/**
1221 Set flowcontrol for ftdi chip
a01d31e2 1222
1941414d
TJ
1223 \param ftdi pointer to ftdi_context
1224 \param flowctrl flow control to use. should be
1225 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1226
1227 \retval 0: all fine
1228 \retval -1: set flow control failed
1229*/
a01d31e2
TJ
1230int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1231{
1232 if (usb_control_msg(ftdi->usb_dev, SIO_SET_FLOW_CTRL_REQUEST_TYPE,
d2f10023
TJ
1233 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->interface),
1234 NULL, 0, ftdi->usb_write_timeout) != 0)
1235 ftdi_error_return(-1, "set flow control failed");
a01d31e2
TJ
1236
1237 return 0;
1238}
1239
1941414d
TJ
1240/**
1241 Set dtr line
1242
1243 \param ftdi pointer to ftdi_context
1244 \param state state to set line to (1 or 0)
1245
1246 \retval 0: all fine
1247 \retval -1: set dtr failed
1248*/
a01d31e2
TJ
1249int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1250{
1251 unsigned short usb_val;
1252
d2f10023 1253 if (state)
a01d31e2
TJ
1254 usb_val = SIO_SET_DTR_HIGH;
1255 else
1256 usb_val = SIO_SET_DTR_LOW;
1257
1258 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
d2f10023
TJ
1259 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1260 NULL, 0, ftdi->usb_write_timeout) != 0)
1261 ftdi_error_return(-1, "set dtr failed");
a01d31e2
TJ
1262
1263 return 0;
1264}
1265
1941414d
TJ
1266/**
1267 Set rts line
1268
1269 \param ftdi pointer to ftdi_context
1270 \param state state to set line to (1 or 0)
1271
1272 \retval 0: all fine
1273 \retval -1 set rts failed
1274*/
a01d31e2
TJ
1275int ftdi_setrts(struct ftdi_context *ftdi, int state)
1276{
1277 unsigned short usb_val;
1278
d2f10023 1279 if (state)
a01d31e2
TJ
1280 usb_val = SIO_SET_RTS_HIGH;
1281 else
1282 usb_val = SIO_SET_RTS_LOW;
1283
d2f10023
TJ
1284 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1285 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1286 NULL, 0, ftdi->usb_write_timeout) != 0)
1287 ftdi_error_return(-1, "set of rts failed");
a01d31e2
TJ
1288
1289 return 0;
1290}
b5ec1820
TJ
1291
1292/* @} end of doxygen libftdi group */