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