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