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