Commit | Line | Data |
---|---|---|
a3da1d95 GE |
1 | /*************************************************************************** |
2 | ftdi.c - description | |
3 | ------------------- | |
4 | begin : Fri Apr 4 2003 | |
8970ed7e | 5 | copyright : (C) 2003-2010 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 |
1bfc403c | 21 | http://www.intra2net.com/en/developer/libftdi/ |
b5ec1820 | 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> |
b56d5a64 | 34 | #include <stdio.h> |
0e302db6 | 35 | |
98452d97 | 36 | #include "ftdi.h" |
a3da1d95 | 37 | |
7cc9950e | 38 | /* stuff needed for async write */ |
f01d7ca6 | 39 | #ifdef LIBFTDI_LINUX_ASYNC_MODE |
22d12cda | 40 | #include <sys/ioctl.h> |
22d12cda TJ |
41 | #include <sys/select.h> |
42 | #include <sys/types.h> | |
43 | #include <unistd.h> | |
44 | #include <linux/usbdevice_fs.h> | |
f01d7ca6 | 45 | #endif |
7cc9950e | 46 | |
21abaf2e | 47 | #define ftdi_error_return(code, str) do { \ |
2f73e59f | 48 | ftdi->error_str = str; \ |
21abaf2e | 49 | return code; \ |
d2f10023 | 50 | } while(0); |
c3d95b87 | 51 | |
418aaa72 | 52 | |
3b92d479 UB |
53 | #if defined( __WIN32__) && !defined(__MINGW32__) |
54 | #include <windows.h> | |
55 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 | |
56 | struct timeval { | |
57 | long tv_sec; | |
58 | long tv_usec; | |
59 | }; | |
60 | int gettimeofday( struct timeval *tv, void null) | |
61 | { | |
62 | FILETIME ft; | |
63 | unsigned __int64 tmpres = 0; | |
64 | if(tv) | |
65 | { | |
66 | GetSystemTimeAsFileTime(&ft); | |
67 | tmpres |= ft.dwHighDateTime; | |
68 | tmpres <<= 32; | |
69 | tmpres |= ft.dwLowDateTime; | |
70 | ||
71 | /*converting file time to unix epoch*/ | |
72 | tmpres /= 10; /*convert into microseconds*/ | |
73 | tmpres -= DELTA_EPOCH_IN_MICROSECS; | |
74 | tv->tv_sec = (LONG)(tmpres / 1000000UL); | |
75 | tv->tv_usec = (LONG)(tmpres % 1000000UL); | |
76 | } | |
77 | /* Warning: Timezone not handled (and not needed here) */ | |
78 | return 0; | |
79 | } | |
1884225d TJ |
80 | #else |
81 | // Include sys/time.h on non-Windows platforms | |
82 | // as gettimeofday() needs it. | |
83 | #include <sys/time.h> | |
3b92d479 UB |
84 | #endif |
85 | ||
f3f81007 TJ |
86 | /** |
87 | Internal function to close usb device pointer. | |
88 | Sets ftdi->usb_dev to NULL. | |
89 | \internal | |
90 | ||
91 | \param ftdi pointer to ftdi_context | |
92 | ||
93 | \retval zero if all is fine, otherwise error code from usb_close() | |
94 | */ | |
95 | static int ftdi_usb_close_internal (struct ftdi_context *ftdi) | |
dff4fdb0 NF |
96 | { |
97 | int ret = 0; | |
98 | ||
8970ed7e | 99 | if (ftdi && ftdi->usb_dev) |
dff4fdb0 NF |
100 | { |
101 | ret = usb_close (ftdi->usb_dev); | |
102 | ftdi->usb_dev = NULL; | |
103 | } | |
f3f81007 | 104 | |
dff4fdb0 NF |
105 | return ret; |
106 | } | |
c3d95b87 | 107 | |
1941414d TJ |
108 | /** |
109 | Initializes a ftdi_context. | |
4837f98a | 110 | |
1941414d | 111 | \param ftdi pointer to ftdi_context |
4837f98a | 112 | |
1941414d TJ |
113 | \retval 0: all fine |
114 | \retval -1: couldn't allocate read buffer | |
115 | ||
116 | \remark This should be called before all functions | |
948f9ada | 117 | */ |
a8f46ddc TJ |
118 | int ftdi_init(struct ftdi_context *ftdi) |
119 | { | |
bf35baa0 | 120 | unsigned int i; |
7cc9950e | 121 | |
98452d97 | 122 | ftdi->usb_dev = NULL; |
545820ce TJ |
123 | ftdi->usb_read_timeout = 5000; |
124 | ftdi->usb_write_timeout = 5000; | |
a3da1d95 | 125 | |
53ad271d | 126 | ftdi->type = TYPE_BM; /* chip type */ |
a3da1d95 | 127 | ftdi->baudrate = -1; |
418aaa72 | 128 | ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */ |
a3da1d95 | 129 | |
948f9ada TJ |
130 | ftdi->readbuffer = NULL; |
131 | ftdi->readbuffer_offset = 0; | |
132 | ftdi->readbuffer_remaining = 0; | |
133 | ftdi->writebuffer_chunksize = 4096; | |
e2f12a4f | 134 | ftdi->max_packet_size = 0; |
948f9ada | 135 | |
545820ce TJ |
136 | ftdi->interface = 0; |
137 | ftdi->index = 0; | |
138 | ftdi->in_ep = 0x02; | |
139 | ftdi->out_ep = 0x81; | |
418aaa72 | 140 | ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */ |
53ad271d | 141 | |
a3da1d95 GE |
142 | ftdi->error_str = NULL; |
143 | ||
f01d7ca6 | 144 | #ifdef LIBFTDI_LINUX_ASYNC_MODE |
7cc9950e GE |
145 | ftdi->async_usb_buffer_size=10; |
146 | if ((ftdi->async_usb_buffer=malloc(sizeof(struct usbdevfs_urb)*ftdi->async_usb_buffer_size)) == NULL) | |
147 | ftdi_error_return(-1, "out of memory for async usb buffer"); | |
148 | ||
149 | /* initialize async usb buffer with unused-marker */ | |
150 | for (i=0; i < ftdi->async_usb_buffer_size; i++) | |
151 | ((struct usbdevfs_urb*)ftdi->async_usb_buffer)[i].usercontext = FTDI_URB_USERCONTEXT_COOKIE; | |
f01d7ca6 TJ |
152 | #else |
153 | ftdi->async_usb_buffer_size=0; | |
154 | ftdi->async_usb_buffer = NULL; | |
155 | #endif | |
7cc9950e | 156 | |
c201f80f TJ |
157 | ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE; |
158 | ||
1c733d33 TJ |
159 | /* All fine. Now allocate the readbuffer */ |
160 | return ftdi_read_data_set_chunksize(ftdi, 4096); | |
948f9ada | 161 | } |
4837f98a | 162 | |
1941414d | 163 | /** |
cef378aa TJ |
164 | Allocate and initialize a new ftdi_context |
165 | ||
166 | \return a pointer to a new ftdi_context, or NULL on failure | |
167 | */ | |
672ac008 | 168 | struct ftdi_context *ftdi_new(void) |
cef378aa TJ |
169 | { |
170 | struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context)); | |
171 | ||
22d12cda TJ |
172 | if (ftdi == NULL) |
173 | { | |
cef378aa TJ |
174 | return NULL; |
175 | } | |
176 | ||
22d12cda TJ |
177 | if (ftdi_init(ftdi) != 0) |
178 | { | |
cef378aa | 179 | free(ftdi); |
cdf448f6 | 180 | return NULL; |
cef378aa TJ |
181 | } |
182 | ||
183 | return ftdi; | |
184 | } | |
185 | ||
186 | /** | |
1941414d TJ |
187 | Open selected channels on a chip, otherwise use first channel. |
188 | ||
189 | \param ftdi pointer to ftdi_context | |
f9d69895 | 190 | \param interface Interface to use for FT2232C/2232H/4232H chips. |
1941414d TJ |
191 | |
192 | \retval 0: all fine | |
193 | \retval -1: unknown interface | |
8970ed7e | 194 | \retval -2: USB device unavailable |
c4446c36 | 195 | */ |
0ce2f5fa | 196 | int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface) |
c4446c36 | 197 | { |
6b11b482 | 198 | if (ftdi == NULL) |
8970ed7e TJ |
199 | ftdi_error_return(-2, "USB device unavailable"); |
200 | ||
22d12cda TJ |
201 | switch (interface) |
202 | { | |
203 | case INTERFACE_ANY: | |
204 | case INTERFACE_A: | |
205 | /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */ | |
206 | break; | |
207 | case INTERFACE_B: | |
208 | ftdi->interface = 1; | |
209 | ftdi->index = INTERFACE_B; | |
210 | ftdi->in_ep = 0x04; | |
211 | ftdi->out_ep = 0x83; | |
212 | break; | |
f9d69895 AH |
213 | case INTERFACE_C: |
214 | ftdi->interface = 2; | |
215 | ftdi->index = INTERFACE_C; | |
216 | ftdi->in_ep = 0x06; | |
217 | ftdi->out_ep = 0x85; | |
218 | break; | |
219 | case INTERFACE_D: | |
220 | ftdi->interface = 3; | |
221 | ftdi->index = INTERFACE_D; | |
222 | ftdi->in_ep = 0x08; | |
223 | ftdi->out_ep = 0x87; | |
224 | break; | |
22d12cda TJ |
225 | default: |
226 | ftdi_error_return(-1, "Unknown interface"); | |
c4446c36 TJ |
227 | } |
228 | return 0; | |
229 | } | |
948f9ada | 230 | |
1941414d TJ |
231 | /** |
232 | Deinitializes a ftdi_context. | |
4837f98a | 233 | |
1941414d | 234 | \param ftdi pointer to ftdi_context |
4837f98a | 235 | */ |
a8f46ddc TJ |
236 | void ftdi_deinit(struct ftdi_context *ftdi) |
237 | { | |
8970ed7e TJ |
238 | if (ftdi == NULL) |
239 | return; | |
240 | ||
f3f81007 | 241 | ftdi_usb_close_internal (ftdi); |
dff4fdb0 | 242 | |
22d12cda TJ |
243 | if (ftdi->async_usb_buffer != NULL) |
244 | { | |
7cc9950e GE |
245 | free(ftdi->async_usb_buffer); |
246 | ftdi->async_usb_buffer = NULL; | |
247 | } | |
248 | ||
22d12cda TJ |
249 | if (ftdi->readbuffer != NULL) |
250 | { | |
d9f0cce7 TJ |
251 | free(ftdi->readbuffer); |
252 | ftdi->readbuffer = NULL; | |
948f9ada | 253 | } |
a3da1d95 GE |
254 | } |
255 | ||
1941414d | 256 | /** |
cef378aa TJ |
257 | Deinitialize and free an ftdi_context. |
258 | ||
259 | \param ftdi pointer to ftdi_context | |
260 | */ | |
261 | void ftdi_free(struct ftdi_context *ftdi) | |
262 | { | |
263 | ftdi_deinit(ftdi); | |
264 | free(ftdi); | |
265 | } | |
266 | ||
267 | /** | |
1941414d TJ |
268 | Use an already open libusb device. |
269 | ||
270 | \param ftdi pointer to ftdi_context | |
271 | \param usb libusb usb_dev_handle to use | |
4837f98a | 272 | */ |
a8f46ddc TJ |
273 | void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) |
274 | { | |
8970ed7e TJ |
275 | if (ftdi == NULL) |
276 | return; | |
277 | ||
98452d97 TJ |
278 | ftdi->usb_dev = usb; |
279 | } | |
280 | ||
281 | ||
1941414d TJ |
282 | /** |
283 | Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which | |
284 | needs to be deallocated by ftdi_list_free() after use. | |
285 | ||
286 | \param ftdi pointer to ftdi_context | |
287 | \param devlist Pointer where to store list of found devices | |
288 | \param vendor Vendor ID to search for | |
289 | \param product Product ID to search for | |
edb82cbf | 290 | |
1941414d TJ |
291 | \retval >0: number of devices found |
292 | \retval -1: usb_find_busses() failed | |
293 | \retval -2: usb_find_devices() failed | |
294 | \retval -3: out of memory | |
edb82cbf | 295 | */ |
d2f10023 | 296 | int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product) |
edb82cbf TJ |
297 | { |
298 | struct ftdi_device_list **curdev; | |
299 | struct usb_bus *bus; | |
300 | struct usb_device *dev; | |
301 | int count = 0; | |
d2f10023 | 302 | |
edb82cbf TJ |
303 | usb_init(); |
304 | if (usb_find_busses() < 0) | |
305 | ftdi_error_return(-1, "usb_find_busses() failed"); | |
306 | if (usb_find_devices() < 0) | |
307 | ftdi_error_return(-2, "usb_find_devices() failed"); | |
308 | ||
309 | curdev = devlist; | |
6db32169 | 310 | *curdev = NULL; |
22d12cda TJ |
311 | for (bus = usb_get_busses(); bus; bus = bus->next) |
312 | { | |
313 | for (dev = bus->devices; dev; dev = dev->next) | |
314 | { | |
edb82cbf TJ |
315 | if (dev->descriptor.idVendor == vendor |
316 | && dev->descriptor.idProduct == product) | |
317 | { | |
318 | *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list)); | |
319 | if (!*curdev) | |
320 | ftdi_error_return(-3, "out of memory"); | |
d2f10023 | 321 | |
edb82cbf TJ |
322 | (*curdev)->next = NULL; |
323 | (*curdev)->dev = dev; | |
324 | ||
325 | curdev = &(*curdev)->next; | |
326 | count++; | |
327 | } | |
328 | } | |
329 | } | |
d2f10023 | 330 | |
edb82cbf TJ |
331 | return count; |
332 | } | |
333 | ||
1941414d TJ |
334 | /** |
335 | Frees a usb device list. | |
edb82cbf | 336 | |
1941414d | 337 | \param devlist USB device list created by ftdi_usb_find_all() |
edb82cbf | 338 | */ |
d2f10023 | 339 | void ftdi_list_free(struct ftdi_device_list **devlist) |
edb82cbf | 340 | { |
6db32169 TJ |
341 | struct ftdi_device_list *curdev, *next; |
342 | ||
22d12cda TJ |
343 | for (curdev = *devlist; curdev != NULL;) |
344 | { | |
6db32169 TJ |
345 | next = curdev->next; |
346 | free(curdev); | |
347 | curdev = next; | |
edb82cbf TJ |
348 | } |
349 | ||
6db32169 | 350 | *devlist = NULL; |
edb82cbf TJ |
351 | } |
352 | ||
1941414d | 353 | /** |
cef378aa TJ |
354 | Frees a usb device list. |
355 | ||
356 | \param devlist USB device list created by ftdi_usb_find_all() | |
357 | */ | |
358 | void ftdi_list_free2(struct ftdi_device_list *devlist) | |
359 | { | |
360 | ftdi_list_free(&devlist); | |
361 | } | |
362 | ||
363 | /** | |
474786c0 TJ |
364 | Return device ID strings from the usb device. |
365 | ||
366 | The parameters manufacturer, description and serial may be NULL | |
367 | or pointer to buffers to store the fetched strings. | |
368 | ||
898c34dd TJ |
369 | \note Use this function only in combination with ftdi_usb_find_all() |
370 | as it closes the internal "usb_dev" after use. | |
371 | ||
474786c0 TJ |
372 | \param ftdi pointer to ftdi_context |
373 | \param dev libusb usb_dev to use | |
374 | \param manufacturer Store manufacturer string here if not NULL | |
375 | \param mnf_len Buffer size of manufacturer string | |
376 | \param description Store product description string here if not NULL | |
377 | \param desc_len Buffer size of product description string | |
378 | \param serial Store serial string here if not NULL | |
379 | \param serial_len Buffer size of serial string | |
380 | ||
381 | \retval 0: all fine | |
382 | \retval -1: wrong arguments | |
383 | \retval -4: unable to open device | |
384 | \retval -7: get product manufacturer failed | |
385 | \retval -8: get product description failed | |
386 | \retval -9: get serial number failed | |
387 | \retval -10: unable to close device | |
388 | */ | |
389 | int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev, | |
22d12cda | 390 | char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len) |
474786c0 TJ |
391 | { |
392 | if ((ftdi==NULL) || (dev==NULL)) | |
393 | return -1; | |
394 | ||
395 | if (!(ftdi->usb_dev = usb_open(dev))) | |
396 | ftdi_error_return(-4, usb_strerror()); | |
397 | ||
22d12cda TJ |
398 | if (manufacturer != NULL) |
399 | { | |
400 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0) | |
401 | { | |
f3f81007 | 402 | ftdi_usb_close_internal (ftdi); |
474786c0 TJ |
403 | ftdi_error_return(-7, usb_strerror()); |
404 | } | |
405 | } | |
406 | ||
22d12cda TJ |
407 | if (description != NULL) |
408 | { | |
409 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0) | |
410 | { | |
f3f81007 | 411 | ftdi_usb_close_internal (ftdi); |
474786c0 TJ |
412 | ftdi_error_return(-8, usb_strerror()); |
413 | } | |
414 | } | |
415 | ||
22d12cda TJ |
416 | if (serial != NULL) |
417 | { | |
418 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0) | |
419 | { | |
f3f81007 | 420 | ftdi_usb_close_internal (ftdi); |
474786c0 TJ |
421 | ftdi_error_return(-9, usb_strerror()); |
422 | } | |
423 | } | |
424 | ||
f3f81007 | 425 | if (ftdi_usb_close_internal (ftdi) != 0) |
474786c0 TJ |
426 | ftdi_error_return(-10, usb_strerror()); |
427 | ||
428 | return 0; | |
429 | } | |
430 | ||
431 | /** | |
e2f12a4f TJ |
432 | * Internal function to determine the maximum packet size. |
433 | * \param ftdi pointer to ftdi_context | |
434 | * \param dev libusb usb_dev to use | |
435 | * \retval Maximum packet size for this device | |
436 | */ | |
437 | static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, struct usb_device *dev) | |
438 | { | |
439 | unsigned int packet_size; | |
440 | ||
8970ed7e TJ |
441 | // Sanity check |
442 | if (ftdi == NULL || dev == NULL) | |
443 | return 64; | |
444 | ||
e2f12a4f TJ |
445 | // Determine maximum packet size. Init with default value. |
446 | // New hi-speed devices from FTDI use a packet size of 512 bytes | |
447 | // but could be connected to a normal speed USB hub -> 64 bytes packet size. | |
448 | if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H) | |
449 | packet_size = 512; | |
450 | else | |
451 | packet_size = 64; | |
452 | ||
453 | if (dev->descriptor.bNumConfigurations > 0 && dev->config) | |
454 | { | |
455 | struct usb_config_descriptor config = dev->config[0]; | |
456 | ||
457 | if (ftdi->interface < config.bNumInterfaces) | |
458 | { | |
459 | struct usb_interface interface = config.interface[ftdi->interface]; | |
460 | if (interface.num_altsetting > 0) | |
461 | { | |
462 | struct usb_interface_descriptor descriptor = interface.altsetting[0]; | |
463 | if (descriptor.bNumEndpoints > 0) | |
464 | { | |
465 | packet_size = descriptor.endpoint[0].wMaxPacketSize; | |
466 | } | |
467 | } | |
468 | } | |
469 | } | |
470 | ||
471 | return packet_size; | |
472 | } | |
473 | ||
474 | /** | |
418aaa72 | 475 | Opens a ftdi device given by an usb_device. |
7b18bef6 | 476 | |
1941414d TJ |
477 | \param ftdi pointer to ftdi_context |
478 | \param dev libusb usb_dev to use | |
479 | ||
480 | \retval 0: all fine | |
23b1798d | 481 | \retval -3: unable to config device |
1941414d TJ |
482 | \retval -4: unable to open device |
483 | \retval -5: unable to claim device | |
484 | \retval -6: reset failed | |
485 | \retval -7: set baudrate failed | |
8970ed7e | 486 | \retval -8: ftdi context invalid |
7b18bef6 TJ |
487 | */ |
488 | int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev) | |
489 | { | |
d2f10023 | 490 | int detach_errno = 0; |
a56ba2bd | 491 | int config_val = 1; |
8970ed7e TJ |
492 | |
493 | if (ftdi == NULL) | |
494 | ftdi_error_return(-8, "ftdi context invalid"); | |
495 | ||
7b18bef6 TJ |
496 | if (!(ftdi->usb_dev = usb_open(dev))) |
497 | ftdi_error_return(-4, "usb_open() failed"); | |
d2f10023 TJ |
498 | |
499 | #ifdef LIBUSB_HAS_GET_DRIVER_NP | |
22592e17 TJ |
500 | // Try to detach ftdi_sio kernel module. |
501 | // Returns ENODATA if driver is not loaded. | |
502 | // | |
503 | // The return code is kept in a separate variable and only parsed | |
504 | // if usb_set_configuration() or usb_claim_interface() fails as the | |
505 | // detach operation might be denied and everything still works fine. | |
506 | // Likely scenario is a static ftdi_sio kernel module. | |
d2f10023 TJ |
507 | if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA) |
508 | detach_errno = errno; | |
509 | #endif | |
510 | ||
8af5bbc7 | 511 | #ifdef __WIN32__ |
b57aedfd GE |
512 | // set configuration (needed especially for windows) |
513 | // tolerate EBUSY: one device with one configuration, but two interfaces | |
514 | // and libftdi sessions to both interfaces (e.g. FT2232) | |
a56ba2bd TJ |
515 | |
516 | if (dev->descriptor.bNumConfigurations > 0) | |
b57aedfd | 517 | { |
a56ba2bd TJ |
518 | // libusb-win32 on Windows 64 can return a null pointer for a valid device |
519 | if (dev->config) | |
520 | config_val = dev->config[0].bConfigurationValue; | |
521 | ||
522 | if (usb_set_configuration(ftdi->usb_dev, config_val) && | |
523 | errno != EBUSY) | |
22d12cda | 524 | { |
a56ba2bd TJ |
525 | ftdi_usb_close_internal (ftdi); |
526 | if (detach_errno == EPERM) | |
527 | { | |
528 | ftdi_error_return(-8, "inappropriate permissions on device!"); | |
529 | } | |
530 | else | |
531 | { | |
8cb36628 | 532 | ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use"); |
a56ba2bd | 533 | } |
23b1798d TJ |
534 | } |
535 | } | |
8af5bbc7 | 536 | #endif |
23b1798d | 537 | |
22d12cda TJ |
538 | if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) |
539 | { | |
f3f81007 | 540 | ftdi_usb_close_internal (ftdi); |
22d12cda TJ |
541 | if (detach_errno == EPERM) |
542 | { | |
d2f10023 | 543 | ftdi_error_return(-8, "inappropriate permissions on device!"); |
22d12cda TJ |
544 | } |
545 | else | |
546 | { | |
8cb36628 | 547 | ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use"); |
d2f10023 | 548 | } |
7b18bef6 TJ |
549 | } |
550 | ||
22d12cda TJ |
551 | if (ftdi_usb_reset (ftdi) != 0) |
552 | { | |
f3f81007 | 553 | ftdi_usb_close_internal (ftdi); |
7b18bef6 TJ |
554 | ftdi_error_return(-6, "ftdi_usb_reset failed"); |
555 | } | |
556 | ||
7b18bef6 TJ |
557 | // Try to guess chip type |
558 | // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0 | |
559 | if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200 | |
560 | && dev->descriptor.iSerialNumber == 0)) | |
561 | ftdi->type = TYPE_BM; | |
562 | else if (dev->descriptor.bcdDevice == 0x200) | |
563 | ftdi->type = TYPE_AM; | |
22d12cda | 564 | else if (dev->descriptor.bcdDevice == 0x500) |
7b18bef6 | 565 | ftdi->type = TYPE_2232C; |
22d12cda | 566 | else if (dev->descriptor.bcdDevice == 0x600) |
cb6250fa | 567 | ftdi->type = TYPE_R; |
0beb9686 TJ |
568 | else if (dev->descriptor.bcdDevice == 0x700) |
569 | ftdi->type = TYPE_2232H; | |
570 | else if (dev->descriptor.bcdDevice == 0x800) | |
571 | ftdi->type = TYPE_4232H; | |
7b18bef6 | 572 | |
f9d69895 AH |
573 | // Set default interface on dual/quad type chips |
574 | switch(ftdi->type) | |
575 | { | |
576 | case TYPE_2232C: | |
577 | case TYPE_2232H: | |
578 | case TYPE_4232H: | |
579 | if (!ftdi->index) | |
580 | ftdi->index = INTERFACE_A; | |
581 | break; | |
582 | default: | |
583 | break; | |
584 | } | |
585 | ||
e2f12a4f TJ |
586 | // Determine maximum packet size |
587 | ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev); | |
588 | ||
ef6f4838 TE |
589 | if (ftdi_set_baudrate (ftdi, 9600) != 0) |
590 | { | |
591 | ftdi_usb_close_internal (ftdi); | |
592 | ftdi_error_return(-7, "set baudrate failed"); | |
593 | } | |
594 | ||
7b18bef6 TJ |
595 | ftdi_error_return(0, "all fine"); |
596 | } | |
597 | ||
1941414d TJ |
598 | /** |
599 | Opens the first device with a given vendor and product ids. | |
600 | ||
601 | \param ftdi pointer to ftdi_context | |
602 | \param vendor Vendor ID | |
603 | \param product Product ID | |
604 | ||
9bec2387 | 605 | \retval same as ftdi_usb_open_desc() |
1941414d | 606 | */ |
edb82cbf TJ |
607 | int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) |
608 | { | |
609 | return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL); | |
610 | } | |
611 | ||
1941414d TJ |
612 | /** |
613 | Opens the first device with a given, vendor id, product id, | |
614 | description and serial. | |
615 | ||
616 | \param ftdi pointer to ftdi_context | |
617 | \param vendor Vendor ID | |
618 | \param product Product ID | |
619 | \param description Description to search for. Use NULL if not needed. | |
620 | \param serial Serial to search for. Use NULL if not needed. | |
621 | ||
622 | \retval 0: all fine | |
623 | \retval -1: usb_find_busses() failed | |
624 | \retval -2: usb_find_devices() failed | |
625 | \retval -3: usb device not found | |
626 | \retval -4: unable to open device | |
627 | \retval -5: unable to claim device | |
628 | \retval -6: reset failed | |
629 | \retval -7: set baudrate failed | |
630 | \retval -8: get product description failed | |
631 | \retval -9: get serial number failed | |
632 | \retval -10: unable to close device | |
a3da1d95 | 633 | */ |
04e1ea0a | 634 | int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, |
a8f46ddc TJ |
635 | const char* description, const char* serial) |
636 | { | |
5ebbdab9 GE |
637 | return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0); |
638 | } | |
639 | ||
640 | /** | |
641 | Opens the index-th device with a given, vendor id, product id, | |
642 | description and serial. | |
643 | ||
644 | \param ftdi pointer to ftdi_context | |
645 | \param vendor Vendor ID | |
646 | \param product Product ID | |
647 | \param description Description to search for. Use NULL if not needed. | |
648 | \param serial Serial to search for. Use NULL if not needed. | |
649 | \param index Number of matching device to open if there are more than one, starts with 0. | |
650 | ||
651 | \retval 0: all fine | |
652 | \retval -1: usb_find_busses() failed | |
653 | \retval -2: usb_find_devices() failed | |
654 | \retval -3: usb device not found | |
655 | \retval -4: unable to open device | |
656 | \retval -5: unable to claim device | |
657 | \retval -6: reset failed | |
658 | \retval -7: set baudrate failed | |
659 | \retval -8: get product description failed | |
660 | \retval -9: get serial number failed | |
661 | \retval -10: unable to close device | |
8970ed7e | 662 | \retval -11: ftdi context invalid |
5ebbdab9 GE |
663 | */ |
664 | int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product, | |
665 | const char* description, const char* serial, unsigned int index) | |
666 | { | |
98452d97 TJ |
667 | struct usb_bus *bus; |
668 | struct usb_device *dev; | |
c3d95b87 | 669 | char string[256]; |
98452d97 TJ |
670 | |
671 | usb_init(); | |
672 | ||
c3d95b87 TJ |
673 | if (usb_find_busses() < 0) |
674 | ftdi_error_return(-1, "usb_find_busses() failed"); | |
c3d95b87 | 675 | if (usb_find_devices() < 0) |
edb82cbf | 676 | ftdi_error_return(-2, "usb_find_devices() failed"); |
a3da1d95 | 677 | |
8970ed7e TJ |
678 | if (ftdi == NULL) |
679 | ftdi_error_return(-11, "ftdi context invalid"); | |
680 | ||
22d12cda TJ |
681 | for (bus = usb_get_busses(); bus; bus = bus->next) |
682 | { | |
683 | for (dev = bus->devices; dev; dev = dev->next) | |
684 | { | |
a8f46ddc | 685 | if (dev->descriptor.idVendor == vendor |
22d12cda TJ |
686 | && dev->descriptor.idProduct == product) |
687 | { | |
c3d95b87 TJ |
688 | if (!(ftdi->usb_dev = usb_open(dev))) |
689 | ftdi_error_return(-4, "usb_open() failed"); | |
690 | ||
22d12cda TJ |
691 | if (description != NULL) |
692 | { | |
693 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) | |
694 | { | |
f3f81007 | 695 | ftdi_usb_close_internal (ftdi); |
c3d95b87 | 696 | ftdi_error_return(-8, "unable to fetch product description"); |
98452d97 | 697 | } |
22d12cda TJ |
698 | if (strncmp(string, description, sizeof(string)) != 0) |
699 | { | |
f3f81007 | 700 | if (ftdi_usb_close_internal (ftdi) != 0) |
edb82cbf | 701 | ftdi_error_return(-10, "unable to close device"); |
a8f46ddc TJ |
702 | continue; |
703 | } | |
704 | } | |
22d12cda TJ |
705 | if (serial != NULL) |
706 | { | |
707 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) | |
708 | { | |
f3f81007 | 709 | ftdi_usb_close_internal (ftdi); |
c3d95b87 | 710 | ftdi_error_return(-9, "unable to fetch serial number"); |
a8f46ddc | 711 | } |
22d12cda TJ |
712 | if (strncmp(string, serial, sizeof(string)) != 0) |
713 | { | |
f3f81007 | 714 | if (ftdi_usb_close_internal (ftdi) != 0) |
edb82cbf | 715 | ftdi_error_return(-10, "unable to close device"); |
a8f46ddc TJ |
716 | continue; |
717 | } | |
718 | } | |
98452d97 | 719 | |
f3f81007 | 720 | if (ftdi_usb_close_internal (ftdi) != 0) |
edb82cbf | 721 | ftdi_error_return(-10, "unable to close device"); |
d2f10023 | 722 | |
5ebbdab9 GE |
723 | if (index > 0) |
724 | { | |
725 | index--; | |
726 | continue; | |
727 | } | |
728 | ||
edb82cbf | 729 | return ftdi_usb_open_dev(ftdi, dev); |
98452d97 TJ |
730 | } |
731 | } | |
98452d97 | 732 | } |
a3da1d95 | 733 | |
98452d97 | 734 | // device not found |
c3d95b87 | 735 | ftdi_error_return(-3, "device not found"); |
a3da1d95 GE |
736 | } |
737 | ||
1941414d | 738 | /** |
5ebbdab9 GE |
739 | Opens the ftdi-device described by a description-string. |
740 | Intended to be used for parsing a device-description given as commandline argument. | |
741 | ||
742 | \param ftdi pointer to ftdi_context | |
743 | \param description NULL-terminated description-string, using this format: | |
744 | \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/) | |
745 | \li <tt>i:\<vendor>:\<product></tt> first device with given vendor and product id, ids can be decimal, octal (preceded by "0") or hex (preceded by "0x") | |
746 | \li <tt>i:\<vendor>:\<product>:\<index></tt> as above with index being the number of the device (starting with 0) if there are more than one | |
747 | \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string | |
748 | ||
749 | \note The description format may be extended in later versions. | |
750 | ||
751 | \retval 0: all fine | |
752 | \retval -1: usb_find_busses() failed | |
753 | \retval -2: usb_find_devices() failed | |
754 | \retval -3: usb device not found | |
755 | \retval -4: unable to open device | |
756 | \retval -5: unable to claim device | |
757 | \retval -6: reset failed | |
758 | \retval -7: set baudrate failed | |
759 | \retval -8: get product description failed | |
760 | \retval -9: get serial number failed | |
761 | \retval -10: unable to close device | |
762 | \retval -11: illegal description format | |
8970ed7e | 763 | \retval -12: ftdi context invalid |
5ebbdab9 GE |
764 | */ |
765 | int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description) | |
766 | { | |
8970ed7e TJ |
767 | if (ftdi == NULL) |
768 | ftdi_error_return(-12, "ftdi context invalid"); | |
769 | ||
5ebbdab9 GE |
770 | if (description[0] == 0 || description[1] != ':') |
771 | ftdi_error_return(-11, "illegal description format"); | |
772 | ||
773 | if (description[0] == 'd') | |
774 | { | |
775 | struct usb_bus *bus; | |
776 | struct usb_device *dev; | |
5ebbdab9 GE |
777 | |
778 | usb_init(); | |
779 | ||
780 | if (usb_find_busses() < 0) | |
781 | ftdi_error_return(-1, "usb_find_busses() failed"); | |
782 | if (usb_find_devices() < 0) | |
783 | ftdi_error_return(-2, "usb_find_devices() failed"); | |
784 | ||
785 | for (bus = usb_get_busses(); bus; bus = bus->next) | |
786 | { | |
787 | for (dev = bus->devices; dev; dev = dev->next) | |
788 | { | |
3d0099ee MF |
789 | /* XXX: This doesn't handle symlinks/odd paths/etc... */ |
790 | const char *desc = description + 2; | |
791 | size_t len = strlen(bus->dirname); | |
792 | if (strncmp(desc, bus->dirname, len)) | |
793 | continue; | |
794 | desc += len; | |
795 | if (desc[0] != '/') | |
796 | continue; | |
797 | ++desc; | |
798 | if (strcmp(desc, dev->filename)) | |
799 | continue; | |
800 | return ftdi_usb_open_dev(ftdi, dev); | |
5ebbdab9 GE |
801 | } |
802 | } | |
803 | ||
804 | // device not found | |
805 | ftdi_error_return(-3, "device not found"); | |
806 | } | |
807 | else if (description[0] == 'i' || description[0] == 's') | |
808 | { | |
809 | unsigned int vendor; | |
810 | unsigned int product; | |
811 | unsigned int index=0; | |
0e6cf62b | 812 | const char *serial=NULL; |
5ebbdab9 GE |
813 | const char *startp, *endp; |
814 | ||
815 | errno=0; | |
816 | startp=description+2; | |
817 | vendor=strtoul((char*)startp,(char**)&endp,0); | |
818 | if (*endp != ':' || endp == startp || errno != 0) | |
819 | ftdi_error_return(-11, "illegal description format"); | |
820 | ||
821 | startp=endp+1; | |
822 | product=strtoul((char*)startp,(char**)&endp,0); | |
823 | if (endp == startp || errno != 0) | |
824 | ftdi_error_return(-11, "illegal description format"); | |
825 | ||
826 | if (description[0] == 'i' && *endp != 0) | |
827 | { | |
828 | /* optional index field in i-mode */ | |
829 | if (*endp != ':') | |
830 | ftdi_error_return(-11, "illegal description format"); | |
831 | ||
832 | startp=endp+1; | |
833 | index=strtoul((char*)startp,(char**)&endp,0); | |
834 | if (*endp != 0 || endp == startp || errno != 0) | |
835 | ftdi_error_return(-11, "illegal description format"); | |
836 | } | |
837 | if (description[0] == 's') | |
838 | { | |
839 | if (*endp != ':') | |
840 | ftdi_error_return(-11, "illegal description format"); | |
841 | ||
842 | /* rest of the description is the serial */ | |
843 | serial=endp+1; | |
844 | } | |
845 | ||
846 | return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index); | |
847 | } | |
848 | else | |
849 | { | |
850 | ftdi_error_return(-11, "illegal description format"); | |
851 | } | |
852 | } | |
853 | ||
854 | /** | |
1941414d | 855 | Resets the ftdi device. |
a3da1d95 | 856 | |
1941414d TJ |
857 | \param ftdi pointer to ftdi_context |
858 | ||
859 | \retval 0: all fine | |
860 | \retval -1: FTDI reset failed | |
8970ed7e | 861 | \retval -2: USB device unavailable |
4837f98a | 862 | */ |
edb82cbf | 863 | int ftdi_usb_reset(struct ftdi_context *ftdi) |
a8f46ddc | 864 | { |
8970ed7e TJ |
865 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
866 | ftdi_error_return(-2, "USB device unavailable"); | |
867 | ||
a5e1bd8c MK |
868 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
869 | SIO_RESET_REQUEST, SIO_RESET_SIO, | |
870 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) | |
22d12cda | 871 | ftdi_error_return(-1,"FTDI reset failed"); |
c3d95b87 | 872 | |
545820ce | 873 | // Invalidate data in the readbuffer |
bfcee05b TJ |
874 | ftdi->readbuffer_offset = 0; |
875 | ftdi->readbuffer_remaining = 0; | |
876 | ||
a3da1d95 GE |
877 | return 0; |
878 | } | |
879 | ||
1941414d | 880 | /** |
1189b11a | 881 | Clears the read buffer on the chip and the internal read buffer. |
1941414d TJ |
882 | |
883 | \param ftdi pointer to ftdi_context | |
4837f98a | 884 | |
1941414d | 885 | \retval 0: all fine |
1189b11a | 886 | \retval -1: read buffer purge failed |
8970ed7e | 887 | \retval -2: USB device unavailable |
4837f98a | 888 | */ |
1189b11a | 889 | int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi) |
a8f46ddc | 890 | { |
8970ed7e TJ |
891 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
892 | ftdi_error_return(-2, "USB device unavailable"); | |
893 | ||
22d12cda TJ |
894 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
895 | SIO_RESET_REQUEST, SIO_RESET_PURGE_RX, | |
896 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) | |
c3d95b87 TJ |
897 | ftdi_error_return(-1, "FTDI purge of RX buffer failed"); |
898 | ||
545820ce | 899 | // Invalidate data in the readbuffer |
bfcee05b TJ |
900 | ftdi->readbuffer_offset = 0; |
901 | ftdi->readbuffer_remaining = 0; | |
a60be878 | 902 | |
1189b11a TJ |
903 | return 0; |
904 | } | |
905 | ||
906 | /** | |
907 | Clears the write buffer on the chip. | |
908 | ||
909 | \param ftdi pointer to ftdi_context | |
910 | ||
911 | \retval 0: all fine | |
912 | \retval -1: write buffer purge failed | |
8970ed7e | 913 | \retval -2: USB device unavailable |
1189b11a TJ |
914 | */ |
915 | int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi) | |
916 | { | |
8970ed7e TJ |
917 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
918 | ftdi_error_return(-2, "USB device unavailable"); | |
919 | ||
22d12cda TJ |
920 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
921 | SIO_RESET_REQUEST, SIO_RESET_PURGE_TX, | |
922 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) | |
1189b11a TJ |
923 | ftdi_error_return(-1, "FTDI purge of TX buffer failed"); |
924 | ||
925 | return 0; | |
926 | } | |
927 | ||
928 | /** | |
929 | Clears the buffers on the chip and the internal read buffer. | |
930 | ||
931 | \param ftdi pointer to ftdi_context | |
932 | ||
933 | \retval 0: all fine | |
934 | \retval -1: read buffer purge failed | |
935 | \retval -2: write buffer purge failed | |
8970ed7e | 936 | \retval -3: USB device unavailable |
1189b11a TJ |
937 | */ |
938 | int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) | |
939 | { | |
940 | int result; | |
941 | ||
8970ed7e TJ |
942 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
943 | ftdi_error_return(-3, "USB device unavailable"); | |
944 | ||
1189b11a | 945 | result = ftdi_usb_purge_rx_buffer(ftdi); |
5a2b51cb | 946 | if (result < 0) |
1189b11a TJ |
947 | return -1; |
948 | ||
949 | result = ftdi_usb_purge_tx_buffer(ftdi); | |
5a2b51cb | 950 | if (result < 0) |
1189b11a | 951 | return -2; |
545820ce | 952 | |
a60be878 TJ |
953 | return 0; |
954 | } | |
a3da1d95 | 955 | |
f3f81007 TJ |
956 | |
957 | ||
1941414d TJ |
958 | /** |
959 | Closes the ftdi device. Call ftdi_deinit() if you're cleaning up. | |
960 | ||
961 | \param ftdi pointer to ftdi_context | |
962 | ||
963 | \retval 0: all fine | |
964 | \retval -1: usb_release failed | |
965 | \retval -2: usb_close failed | |
8970ed7e | 966 | \retval -3: ftdi context invalid |
a3da1d95 | 967 | */ |
a8f46ddc TJ |
968 | int ftdi_usb_close(struct ftdi_context *ftdi) |
969 | { | |
a3da1d95 GE |
970 | int rtn = 0; |
971 | ||
8970ed7e TJ |
972 | if (ftdi == NULL) |
973 | ftdi_error_return(-3, "ftdi context invalid"); | |
974 | ||
f01d7ca6 | 975 | #ifdef LIBFTDI_LINUX_ASYNC_MODE |
7cc9950e GE |
976 | /* try to release some kernel resources */ |
977 | ftdi_async_complete(ftdi,1); | |
f01d7ca6 | 978 | #endif |
7cc9950e | 979 | |
dff4fdb0 NF |
980 | if (ftdi->usb_dev != NULL) |
981 | if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0) | |
982 | rtn = -1; | |
98452d97 | 983 | |
f3f81007 | 984 | if (ftdi_usb_close_internal (ftdi) != 0) |
a3da1d95 | 985 | rtn = -2; |
98452d97 | 986 | |
a3da1d95 GE |
987 | return rtn; |
988 | } | |
989 | ||
418aaa72 | 990 | /** |
53ad271d TJ |
991 | ftdi_convert_baudrate returns nearest supported baud rate to that requested. |
992 | Function is only used internally | |
b5ec1820 | 993 | \internal |
53ad271d | 994 | */ |
0126d22e | 995 | static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, |
a8f46ddc TJ |
996 | unsigned short *value, unsigned short *index) |
997 | { | |
53ad271d TJ |
998 | static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1}; |
999 | static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3}; | |
1000 | static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7}; | |
1001 | int divisor, best_divisor, best_baud, best_baud_diff; | |
1002 | unsigned long encoded_divisor; | |
1003 | int i; | |
1004 | ||
22d12cda TJ |
1005 | if (baudrate <= 0) |
1006 | { | |
53ad271d TJ |
1007 | // Return error |
1008 | return -1; | |
1009 | } | |
1010 | ||
1011 | divisor = 24000000 / baudrate; | |
1012 | ||
22d12cda TJ |
1013 | if (ftdi->type == TYPE_AM) |
1014 | { | |
53ad271d TJ |
1015 | // Round down to supported fraction (AM only) |
1016 | divisor -= am_adjust_dn[divisor & 7]; | |
1017 | } | |
1018 | ||
1019 | // Try this divisor and the one above it (because division rounds down) | |
1020 | best_divisor = 0; | |
1021 | best_baud = 0; | |
1022 | best_baud_diff = 0; | |
22d12cda TJ |
1023 | for (i = 0; i < 2; i++) |
1024 | { | |
53ad271d TJ |
1025 | int try_divisor = divisor + i; |
1026 | int baud_estimate; | |
1027 | int baud_diff; | |
1028 | ||
1029 | // Round up to supported divisor value | |
22d12cda TJ |
1030 | if (try_divisor <= 8) |
1031 | { | |
53ad271d TJ |
1032 | // Round up to minimum supported divisor |
1033 | try_divisor = 8; | |
22d12cda TJ |
1034 | } |
1035 | else if (ftdi->type != TYPE_AM && try_divisor < 12) | |
1036 | { | |
53ad271d TJ |
1037 | // BM doesn't support divisors 9 through 11 inclusive |
1038 | try_divisor = 12; | |
22d12cda TJ |
1039 | } |
1040 | else if (divisor < 16) | |
1041 | { | |
53ad271d TJ |
1042 | // AM doesn't support divisors 9 through 15 inclusive |
1043 | try_divisor = 16; | |
22d12cda TJ |
1044 | } |
1045 | else | |
1046 | { | |
1047 | if (ftdi->type == TYPE_AM) | |
1048 | { | |
53ad271d TJ |
1049 | // Round up to supported fraction (AM only) |
1050 | try_divisor += am_adjust_up[try_divisor & 7]; | |
22d12cda TJ |
1051 | if (try_divisor > 0x1FFF8) |
1052 | { | |
53ad271d TJ |
1053 | // Round down to maximum supported divisor value (for AM) |
1054 | try_divisor = 0x1FFF8; | |
1055 | } | |
22d12cda TJ |
1056 | } |
1057 | else | |
1058 | { | |
1059 | if (try_divisor > 0x1FFFF) | |
1060 | { | |
53ad271d TJ |
1061 | // Round down to maximum supported divisor value (for BM) |
1062 | try_divisor = 0x1FFFF; | |
1063 | } | |
1064 | } | |
1065 | } | |
1066 | // Get estimated baud rate (to nearest integer) | |
1067 | baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor; | |
1068 | // Get absolute difference from requested baud rate | |
22d12cda TJ |
1069 | if (baud_estimate < baudrate) |
1070 | { | |
53ad271d | 1071 | baud_diff = baudrate - baud_estimate; |
22d12cda TJ |
1072 | } |
1073 | else | |
1074 | { | |
53ad271d TJ |
1075 | baud_diff = baud_estimate - baudrate; |
1076 | } | |
22d12cda TJ |
1077 | if (i == 0 || baud_diff < best_baud_diff) |
1078 | { | |
53ad271d TJ |
1079 | // Closest to requested baud rate so far |
1080 | best_divisor = try_divisor; | |
1081 | best_baud = baud_estimate; | |
1082 | best_baud_diff = baud_diff; | |
22d12cda TJ |
1083 | if (baud_diff == 0) |
1084 | { | |
53ad271d TJ |
1085 | // Spot on! No point trying |
1086 | break; | |
1087 | } | |
1088 | } | |
1089 | } | |
1090 | // Encode the best divisor value | |
1091 | encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14); | |
1092 | // Deal with special cases for encoded value | |
22d12cda TJ |
1093 | if (encoded_divisor == 1) |
1094 | { | |
4837f98a | 1095 | encoded_divisor = 0; // 3000000 baud |
22d12cda TJ |
1096 | } |
1097 | else if (encoded_divisor == 0x4001) | |
1098 | { | |
4837f98a | 1099 | encoded_divisor = 1; // 2000000 baud (BM only) |
53ad271d TJ |
1100 | } |
1101 | // Split into "value" and "index" values | |
1102 | *value = (unsigned short)(encoded_divisor & 0xFFFF); | |
1416eb14 | 1103 | if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H) |
22d12cda | 1104 | { |
0126d22e TJ |
1105 | *index = (unsigned short)(encoded_divisor >> 8); |
1106 | *index &= 0xFF00; | |
a9c57c05 | 1107 | *index |= ftdi->index; |
0126d22e TJ |
1108 | } |
1109 | else | |
1110 | *index = (unsigned short)(encoded_divisor >> 16); | |
c3d95b87 | 1111 | |
53ad271d TJ |
1112 | // Return the nearest baud rate |
1113 | return best_baud; | |
1114 | } | |
1115 | ||
1941414d | 1116 | /** |
9bec2387 | 1117 | Sets the chip baud rate |
1941414d TJ |
1118 | |
1119 | \param ftdi pointer to ftdi_context | |
9bec2387 | 1120 | \param baudrate baud rate to set |
1941414d TJ |
1121 | |
1122 | \retval 0: all fine | |
1123 | \retval -1: invalid baudrate | |
1124 | \retval -2: setting baudrate failed | |
8970ed7e | 1125 | \retval -3: USB device unavailable |
a3da1d95 | 1126 | */ |
a8f46ddc TJ |
1127 | int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) |
1128 | { | |
53ad271d TJ |
1129 | unsigned short value, index; |
1130 | int actual_baudrate; | |
a3da1d95 | 1131 | |
8970ed7e TJ |
1132 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1133 | ftdi_error_return(-3, "USB device unavailable"); | |
1134 | ||
22d12cda TJ |
1135 | if (ftdi->bitbang_enabled) |
1136 | { | |
a3da1d95 GE |
1137 | baudrate = baudrate*4; |
1138 | } | |
1139 | ||
25707904 | 1140 | actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index); |
c3d95b87 TJ |
1141 | if (actual_baudrate <= 0) |
1142 | ftdi_error_return (-1, "Silly baudrate <= 0."); | |
a3da1d95 | 1143 | |
53ad271d TJ |
1144 | // Check within tolerance (about 5%) |
1145 | if ((actual_baudrate * 2 < baudrate /* Catch overflows */ ) | |
1146 | || ((actual_baudrate < baudrate) | |
1147 | ? (actual_baudrate * 21 < baudrate * 20) | |
c3d95b87 TJ |
1148 | : (baudrate * 21 < actual_baudrate * 20))) |
1149 | ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4"); | |
545820ce | 1150 | |
a5e1bd8c | 1151 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
9ecfef2a TJ |
1152 | SIO_SET_BAUDRATE_REQUEST, value, |
1153 | index, NULL, 0, ftdi->usb_write_timeout) != 0) | |
c3d95b87 | 1154 | ftdi_error_return (-2, "Setting new baudrate failed"); |
a3da1d95 GE |
1155 | |
1156 | ftdi->baudrate = baudrate; | |
1157 | return 0; | |
1158 | } | |
1159 | ||
1941414d | 1160 | /** |
6c32e222 TJ |
1161 | Set (RS232) line characteristics. |
1162 | The break type can only be set via ftdi_set_line_property2() | |
1163 | and defaults to "off". | |
4837f98a | 1164 | |
1941414d TJ |
1165 | \param ftdi pointer to ftdi_context |
1166 | \param bits Number of bits | |
1167 | \param sbit Number of stop bits | |
1168 | \param parity Parity mode | |
1169 | ||
1170 | \retval 0: all fine | |
1171 | \retval -1: Setting line property failed | |
2f73e59f TJ |
1172 | */ |
1173 | int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits, | |
d2f10023 | 1174 | enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity) |
2f73e59f | 1175 | { |
6c32e222 TJ |
1176 | return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF); |
1177 | } | |
1178 | ||
1179 | /** | |
1180 | Set (RS232) line characteristics | |
1181 | ||
1182 | \param ftdi pointer to ftdi_context | |
1183 | \param bits Number of bits | |
1184 | \param sbit Number of stop bits | |
1185 | \param parity Parity mode | |
1186 | \param break_type Break type | |
1187 | ||
1188 | \retval 0: all fine | |
1189 | \retval -1: Setting line property failed | |
8970ed7e | 1190 | \retval -2: USB device unavailable |
6c32e222 TJ |
1191 | */ |
1192 | int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits, | |
22d12cda TJ |
1193 | enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity, |
1194 | enum ftdi_break_type break_type) | |
6c32e222 | 1195 | { |
2f73e59f TJ |
1196 | unsigned short value = bits; |
1197 | ||
8970ed7e TJ |
1198 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1199 | ftdi_error_return(-2, "USB device unavailable"); | |
1200 | ||
22d12cda TJ |
1201 | switch (parity) |
1202 | { | |
1203 | case NONE: | |
1204 | value |= (0x00 << 8); | |
1205 | break; | |
1206 | case ODD: | |
1207 | value |= (0x01 << 8); | |
1208 | break; | |
1209 | case EVEN: | |
1210 | value |= (0x02 << 8); | |
1211 | break; | |
1212 | case MARK: | |
1213 | value |= (0x03 << 8); | |
1214 | break; | |
1215 | case SPACE: | |
1216 | value |= (0x04 << 8); | |
1217 | break; | |
2f73e59f | 1218 | } |
d2f10023 | 1219 | |
22d12cda TJ |
1220 | switch (sbit) |
1221 | { | |
1222 | case STOP_BIT_1: | |
1223 | value |= (0x00 << 11); | |
1224 | break; | |
1225 | case STOP_BIT_15: | |
1226 | value |= (0x01 << 11); | |
1227 | break; | |
1228 | case STOP_BIT_2: | |
1229 | value |= (0x02 << 11); | |
1230 | break; | |
2f73e59f | 1231 | } |
d2f10023 | 1232 | |
22d12cda TJ |
1233 | switch (break_type) |
1234 | { | |
1235 | case BREAK_OFF: | |
1236 | value |= (0x00 << 14); | |
1237 | break; | |
1238 | case BREAK_ON: | |
1239 | value |= (0x01 << 14); | |
1240 | break; | |
6c32e222 TJ |
1241 | } |
1242 | ||
a5e1bd8c | 1243 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
9ecfef2a TJ |
1244 | SIO_SET_DATA_REQUEST, value, |
1245 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) | |
2f73e59f | 1246 | ftdi_error_return (-1, "Setting new line property failed"); |
d2f10023 | 1247 | |
2f73e59f TJ |
1248 | return 0; |
1249 | } | |
a3da1d95 | 1250 | |
1941414d TJ |
1251 | /** |
1252 | Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip | |
1253 | ||
1254 | \param ftdi pointer to ftdi_context | |
1255 | \param buf Buffer with the data | |
1256 | \param size Size of the buffer | |
1257 | ||
8970ed7e | 1258 | \retval -666: USB device unavailable |
1941414d TJ |
1259 | \retval <0: error code from usb_bulk_write() |
1260 | \retval >0: number of bytes written | |
1261 | */ | |
a8f46ddc TJ |
1262 | int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
1263 | { | |
a3da1d95 GE |
1264 | int ret; |
1265 | int offset = 0; | |
545820ce | 1266 | int total_written = 0; |
c3d95b87 | 1267 | |
8970ed7e TJ |
1268 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1269 | ftdi_error_return(-666, "USB device unavailable"); | |
1270 | ||
22d12cda TJ |
1271 | while (offset < size) |
1272 | { | |
948f9ada | 1273 | int write_size = ftdi->writebuffer_chunksize; |
a3da1d95 GE |
1274 | |
1275 | if (offset+write_size > size) | |
1276 | write_size = size-offset; | |
1277 | ||
98452d97 | 1278 | ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout); |
c3d95b87 TJ |
1279 | if (ret < 0) |
1280 | ftdi_error_return(ret, "usb bulk write failed"); | |
a3da1d95 | 1281 | |
c3d95b87 | 1282 | total_written += ret; |
a3da1d95 GE |
1283 | offset += write_size; |
1284 | } | |
1285 | ||
545820ce | 1286 | return total_written; |
a3da1d95 GE |
1287 | } |
1288 | ||
f01d7ca6 | 1289 | #ifdef LIBFTDI_LINUX_ASYNC_MODE |
e59bc450 CW |
1290 | #ifdef USB_CLASS_PTP |
1291 | #error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1! | |
1292 | #endif | |
4c9e3812 GE |
1293 | /* this is strongly dependent on libusb using the same struct layout. If libusb |
1294 | changes in some later version this may break horribly (this is for libusb 0.1.12) */ | |
22d12cda TJ |
1295 | struct usb_dev_handle |
1296 | { | |
1297 | int fd; | |
1298 | // some other stuff coming here we don't need | |
4c9e3812 GE |
1299 | }; |
1300 | ||
84f85aaa | 1301 | /** |
c201f80f TJ |
1302 | Check for pending async urbs |
1303 | \internal | |
1304 | */ | |
1305 | static int _usb_get_async_urbs_pending(struct ftdi_context *ftdi) | |
7cc9950e GE |
1306 | { |
1307 | struct usbdevfs_urb *urb; | |
1308 | int pending=0; | |
bf35baa0 | 1309 | unsigned int i; |
7cc9950e | 1310 | |
22d12cda TJ |
1311 | for (i=0; i < ftdi->async_usb_buffer_size; i++) |
1312 | { | |
7cc9950e GE |
1313 | urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i]; |
1314 | if (urb->usercontext != FTDI_URB_USERCONTEXT_COOKIE) | |
1315 | pending++; | |
1316 | } | |
1317 | ||
1318 | return pending; | |
1319 | } | |
1320 | ||
84f85aaa GE |
1321 | /** |
1322 | Wait until one or more async URBs are completed by the kernel and mark their | |
1323 | positions in the async-buffer as unused | |
1324 | ||
1325 | \param ftdi pointer to ftdi_context | |
1326 | \param wait_for_more if != 0 wait for more than one write to complete | |
1327 | \param timeout_msec max milliseconds to wait | |
1328 | ||
c201f80f TJ |
1329 | \internal |
1330 | */ | |
1331 | static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int timeout_msec) | |
7cc9950e | 1332 | { |
22d12cda | 1333 | struct timeval tv; |
236e16d1 | 1334 | struct usbdevfs_urb *urb; |
22d12cda TJ |
1335 | int ret; |
1336 | fd_set writefds; | |
1337 | int keep_going=0; | |
1338 | ||
1339 | FD_ZERO(&writefds); | |
1340 | FD_SET(ftdi->usb_dev->fd, &writefds); | |
1341 | ||
1342 | /* init timeout only once, select writes time left after call */ | |
1343 | tv.tv_sec = timeout_msec / 1000; | |
1344 | tv.tv_usec = (timeout_msec % 1000) * 1000; | |
1345 | ||
1346 | do | |
7cc9950e | 1347 | { |
236e16d1 TJ |
1348 | ret = -1; |
1349 | urb = NULL; | |
1350 | ||
22d12cda TJ |
1351 | while (_usb_get_async_urbs_pending(ftdi) |
1352 | && (ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_REAPURBNDELAY, &urb)) == -1 | |
1353 | && errno == EAGAIN) | |
1354 | { | |
1355 | if (keep_going && !wait_for_more) | |
1356 | { | |
1357 | /* don't wait if repeating only for keep_going */ | |
1358 | keep_going=0; | |
1359 | break; | |
1360 | } | |
7cc9950e | 1361 | |
22d12cda TJ |
1362 | /* wait for timeout msec or something written ready */ |
1363 | select(ftdi->usb_dev->fd+1, NULL, &writefds, NULL, &tv); | |
1364 | } | |
1365 | ||
1366 | if (ret == 0 && urb != NULL) | |
1367 | { | |
1368 | /* got a free urb, mark it */ | |
1369 | urb->usercontext = FTDI_URB_USERCONTEXT_COOKIE; | |
7cc9950e | 1370 | |
22d12cda | 1371 | /* try to get more urbs that are ready now, but don't wait anymore */ |
22d12cda TJ |
1372 | keep_going=1; |
1373 | } | |
1374 | else | |
1375 | { | |
1376 | /* no more urbs waiting */ | |
1377 | keep_going=0; | |
1378 | } | |
7cc9950e | 1379 | } |
22d12cda | 1380 | while (keep_going); |
7cc9950e GE |
1381 | } |
1382 | ||
1383 | /** | |
84f85aaa GE |
1384 | Wait until one or more async URBs are completed by the kernel and mark their |
1385 | positions in the async-buffer as unused. | |
7cc9950e GE |
1386 | |
1387 | \param ftdi pointer to ftdi_context | |
1388 | \param wait_for_more if != 0 wait for more than one write to complete (until write timeout) | |
1389 | */ | |
1390 | void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more) | |
1391 | { | |
22d12cda | 1392 | _usb_async_cleanup(ftdi,wait_for_more,ftdi->usb_write_timeout); |
7cc9950e | 1393 | } |
4c9e3812 GE |
1394 | |
1395 | /** | |
1396 | Stupid libusb does not offer async writes nor does it allow | |
1397 | access to its fd - so we need some hacks here. | |
c201f80f | 1398 | \internal |
4c9e3812 | 1399 | */ |
c201f80f | 1400 | static int _usb_bulk_write_async(struct ftdi_context *ftdi, int ep, char *bytes, int size) |
4c9e3812 | 1401 | { |
22d12cda TJ |
1402 | struct usbdevfs_urb *urb; |
1403 | int bytesdone = 0, requested; | |
bf35baa0 TJ |
1404 | int ret, cleanup_count; |
1405 | unsigned int i; | |
22d12cda TJ |
1406 | |
1407 | do | |
7cc9950e | 1408 | { |
22d12cda | 1409 | /* find a free urb buffer we can use */ |
3af1ac09 | 1410 | i = 0; |
22d12cda TJ |
1411 | urb=NULL; |
1412 | for (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++) | |
1413 | { | |
1414 | if (i==ftdi->async_usb_buffer_size) | |
1415 | { | |
1416 | /* wait until some buffers are free */ | |
1417 | _usb_async_cleanup(ftdi,0,ftdi->usb_write_timeout); | |
1418 | } | |
7cc9950e | 1419 | |
22d12cda TJ |
1420 | for (i=0; i < ftdi->async_usb_buffer_size; i++) |
1421 | { | |
1422 | urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i]; | |
1423 | if (urb->usercontext == FTDI_URB_USERCONTEXT_COOKIE) | |
1424 | break; /* found a free urb position */ | |
1425 | urb=NULL; | |
1426 | } | |
7cc9950e | 1427 | } |
7cc9950e | 1428 | |
22d12cda TJ |
1429 | /* no free urb position found */ |
1430 | if (urb==NULL) | |
1431 | return -1; | |
1432 | ||
1433 | requested = size - bytesdone; | |
1434 | if (requested > 4096) | |
1435 | requested = 4096; | |
4c9e3812 | 1436 | |
22d12cda TJ |
1437 | memset(urb,0,sizeof(urb)); |
1438 | ||
1439 | urb->type = USBDEVFS_URB_TYPE_BULK; | |
1440 | urb->endpoint = ep; | |
1441 | urb->flags = 0; | |
1442 | urb->buffer = bytes + bytesdone; | |
1443 | urb->buffer_length = requested; | |
1444 | urb->signr = 0; | |
1445 | urb->actual_length = 0; | |
1446 | urb->number_of_packets = 0; | |
1447 | urb->usercontext = 0; | |
1448 | ||
1449 | do | |
1450 | { | |
1451 | ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_SUBMITURB, urb); | |
1452 | } | |
1453 | while (ret < 0 && errno == EINTR); | |
1454 | if (ret < 0) | |
1455 | return ret; /* the caller can read errno to get more info */ | |
1456 | ||
1457 | bytesdone += requested; | |
1458 | } | |
1459 | while (bytesdone < size); | |
1460 | return bytesdone; | |
4c9e3812 GE |
1461 | } |
1462 | ||
1463 | /** | |
1464 | Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip. | |
1465 | Does not wait for completion of the transfer nor does it make sure that | |
1466 | the transfer was successful. | |
1467 | ||
1468 | This function could be extended to use signals and callbacks to inform the | |
1469 | caller of completion or error - but this is not done yet, volunteers welcome. | |
1470 | ||
1471 | Works around libusb and directly accesses functions only available on Linux. | |
cef378aa | 1472 | Only available if compiled with --with-async-mode. |
4c9e3812 GE |
1473 | |
1474 | \param ftdi pointer to ftdi_context | |
1475 | \param buf Buffer with the data | |
1476 | \param size Size of the buffer | |
1477 | ||
8970ed7e | 1478 | \retval -666: USB device unavailable |
4c9e3812 GE |
1479 | \retval <0: error code from usb_bulk_write() |
1480 | \retval >0: number of bytes written | |
1481 | */ | |
1482 | int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size) | |
1483 | { | |
1484 | int ret; | |
1485 | int offset = 0; | |
1486 | int total_written = 0; | |
1487 | ||
8970ed7e TJ |
1488 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1489 | ftdi_error_return(-666, "USB device unavailable"); | |
1490 | ||
22d12cda TJ |
1491 | while (offset < size) |
1492 | { | |
4c9e3812 GE |
1493 | int write_size = ftdi->writebuffer_chunksize; |
1494 | ||
1495 | if (offset+write_size > size) | |
1496 | write_size = size-offset; | |
1497 | ||
c201f80f | 1498 | ret = _usb_bulk_write_async(ftdi, ftdi->in_ep, buf+offset, write_size); |
4c9e3812 GE |
1499 | if (ret < 0) |
1500 | ftdi_error_return(ret, "usb bulk write async failed"); | |
1501 | ||
1502 | total_written += ret; | |
1503 | offset += write_size; | |
1504 | } | |
1505 | ||
1506 | return total_written; | |
1507 | } | |
f01d7ca6 | 1508 | #endif // LIBFTDI_LINUX_ASYNC_MODE |
4c9e3812 | 1509 | |
1941414d TJ |
1510 | /** |
1511 | Configure write buffer chunk size. | |
1512 | Default is 4096. | |
1513 | ||
1514 | \param ftdi pointer to ftdi_context | |
1515 | \param chunksize Chunk size | |
a3da1d95 | 1516 | |
1941414d | 1517 | \retval 0: all fine |
8970ed7e | 1518 | \retval -1: ftdi context invalid |
1941414d | 1519 | */ |
a8f46ddc TJ |
1520 | int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
1521 | { | |
8970ed7e TJ |
1522 | if (ftdi == NULL) |
1523 | ftdi_error_return(-1, "ftdi context invalid"); | |
1524 | ||
948f9ada TJ |
1525 | ftdi->writebuffer_chunksize = chunksize; |
1526 | return 0; | |
1527 | } | |
1528 | ||
1941414d TJ |
1529 | /** |
1530 | Get write buffer chunk size. | |
1531 | ||
1532 | \param ftdi pointer to ftdi_context | |
1533 | \param chunksize Pointer to store chunk size in | |
948f9ada | 1534 | |
1941414d | 1535 | \retval 0: all fine |
8970ed7e | 1536 | \retval -1: ftdi context invalid |
1941414d | 1537 | */ |
a8f46ddc TJ |
1538 | int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
1539 | { | |
8970ed7e TJ |
1540 | if (ftdi == NULL) |
1541 | ftdi_error_return(-1, "ftdi context invalid"); | |
1542 | ||
948f9ada TJ |
1543 | *chunksize = ftdi->writebuffer_chunksize; |
1544 | return 0; | |
1545 | } | |
cbabb7d3 | 1546 | |
1941414d TJ |
1547 | /** |
1548 | Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip. | |
1549 | ||
d48caea1 | 1550 | Returns when at least one byte is available or when the latency timer has elapsed |
1941414d | 1551 | Automatically strips the two modem status bytes transfered during every read. |
948f9ada | 1552 | |
1941414d TJ |
1553 | \param ftdi pointer to ftdi_context |
1554 | \param buf Buffer to store data in | |
1555 | \param size Size of the buffer | |
1556 | ||
8970ed7e | 1557 | \retval -666: USB device unavailable |
1941414d | 1558 | \retval <0: error code from usb_bulk_read() |
d77b0e94 | 1559 | \retval 0: no data was available |
1941414d TJ |
1560 | \retval >0: number of bytes read |
1561 | ||
1941414d | 1562 | */ |
a8f46ddc TJ |
1563 | int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
1564 | { | |
1c733d33 | 1565 | int offset = 0, ret = 1, i, num_of_chunks, chunk_remains; |
8970ed7e | 1566 | int packet_size; |
3b92d479 | 1567 | struct timeval tv_start, tv_current; |
f2f00cb5 | 1568 | |
8970ed7e TJ |
1569 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1570 | ftdi_error_return(-666, "USB device unavailable"); | |
1571 | ||
1572 | packet_size = ftdi->max_packet_size; | |
e2f12a4f TJ |
1573 | // Packet size sanity check (avoid division by zero) |
1574 | if (packet_size == 0) | |
1575 | ftdi_error_return(-1, "max_packet_size is bogus (zero)"); | |
d9f0cce7 | 1576 | |
948f9ada | 1577 | // everything we want is still in the readbuffer? |
22d12cda TJ |
1578 | if (size <= ftdi->readbuffer_remaining) |
1579 | { | |
d9f0cce7 TJ |
1580 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size); |
1581 | ||
1582 | // Fix offsets | |
1583 | ftdi->readbuffer_remaining -= size; | |
1584 | ftdi->readbuffer_offset += size; | |
1585 | ||
545820ce | 1586 | /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */ |
d9f0cce7 TJ |
1587 | |
1588 | return size; | |
979a145c | 1589 | } |
948f9ada | 1590 | // something still in the readbuffer, but not enough to satisfy 'size'? |
22d12cda TJ |
1591 | if (ftdi->readbuffer_remaining != 0) |
1592 | { | |
d9f0cce7 | 1593 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining); |
979a145c | 1594 | |
d9f0cce7 TJ |
1595 | // Fix offset |
1596 | offset += ftdi->readbuffer_remaining; | |
948f9ada | 1597 | } |
948f9ada | 1598 | // do the actual USB read |
3b92d479 | 1599 | gettimeofday(&tv_start,NULL); |
22d12cda TJ |
1600 | while (offset < size && ret > 0) |
1601 | { | |
d9f0cce7 TJ |
1602 | ftdi->readbuffer_remaining = 0; |
1603 | ftdi->readbuffer_offset = 0; | |
98452d97 TJ |
1604 | /* returns how much received */ |
1605 | ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout); | |
c3d95b87 TJ |
1606 | if (ret < 0) |
1607 | ftdi_error_return(ret, "usb bulk read failed"); | |
98452d97 | 1608 | |
22d12cda TJ |
1609 | if (ret > 2) |
1610 | { | |
d9f0cce7 TJ |
1611 | // skip FTDI status bytes. |
1612 | // Maybe stored in the future to enable modem use | |
f2f00cb5 DC |
1613 | num_of_chunks = ret / packet_size; |
1614 | chunk_remains = ret % packet_size; | |
1c733d33 TJ |
1615 | //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset); |
1616 | ||
d9f0cce7 TJ |
1617 | ftdi->readbuffer_offset += 2; |
1618 | ret -= 2; | |
1c733d33 | 1619 | |
f2f00cb5 | 1620 | if (ret > packet_size - 2) |
22d12cda | 1621 | { |
1c733d33 | 1622 | for (i = 1; i < num_of_chunks; i++) |
f2f00cb5 DC |
1623 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i, |
1624 | ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i, | |
1625 | packet_size - 2); | |
22d12cda TJ |
1626 | if (chunk_remains > 2) |
1627 | { | |
f2f00cb5 DC |
1628 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i, |
1629 | ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i, | |
1c733d33 TJ |
1630 | chunk_remains-2); |
1631 | ret -= 2*num_of_chunks; | |
22d12cda TJ |
1632 | } |
1633 | else | |
1c733d33 TJ |
1634 | ret -= 2*(num_of_chunks-1)+chunk_remains; |
1635 | } | |
d9f0cce7 | 1636 | // data still fits in buf? |
22d12cda TJ |
1637 | if (offset+ret <= size) |
1638 | { | |
d9f0cce7 | 1639 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret); |
545820ce | 1640 | //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]); |
d9f0cce7 TJ |
1641 | offset += ret; |
1642 | ||
53ad271d | 1643 | /* Did we read exactly the right amount of bytes? */ |
d9f0cce7 | 1644 | if (offset == size) |
c4446c36 TJ |
1645 | //printf("read_data exact rem %d offset %d\n", |
1646 | //ftdi->readbuffer_remaining, offset); | |
d9f0cce7 | 1647 | return offset; |
22d12cda TJ |
1648 | } |
1649 | else | |
1650 | { | |
d9f0cce7 TJ |
1651 | // only copy part of the data or size <= readbuffer_chunksize |
1652 | int part_size = size-offset; | |
1653 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size); | |
98452d97 | 1654 | |
d9f0cce7 TJ |
1655 | ftdi->readbuffer_offset += part_size; |
1656 | ftdi->readbuffer_remaining = ret-part_size; | |
1657 | offset += part_size; | |
1658 | ||
53ad271d TJ |
1659 | /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n", |
1660 | part_size, size, offset, ret, ftdi->readbuffer_remaining); */ | |
d9f0cce7 | 1661 | |
3094872b | 1662 | return offset; |
d9f0cce7 TJ |
1663 | } |
1664 | } | |
3b92d479 UB |
1665 | gettimeofday(&tv_current,NULL); |
1666 | if(((tv_current.tv_sec - tv_start.tv_sec)*1000000+(tv_current.tv_usec - tv_start.tv_usec)) | |
1667 | > ftdi->usb_read_timeout) | |
1668 | return offset; | |
cbabb7d3 | 1669 | } |
948f9ada | 1670 | // never reached |
29c4af7f | 1671 | return -127; |
a3da1d95 GE |
1672 | } |
1673 | ||
1941414d TJ |
1674 | /** |
1675 | Configure read buffer chunk size. | |
1676 | Default is 4096. | |
1677 | ||
1678 | Automatically reallocates the buffer. | |
a3da1d95 | 1679 | |
1941414d TJ |
1680 | \param ftdi pointer to ftdi_context |
1681 | \param chunksize Chunk size | |
1682 | ||
1683 | \retval 0: all fine | |
8970ed7e | 1684 | \retval -1: ftdi context invalid |
1941414d | 1685 | */ |
a8f46ddc TJ |
1686 | int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
1687 | { | |
29c4af7f TJ |
1688 | unsigned char *new_buf; |
1689 | ||
8970ed7e TJ |
1690 | if (ftdi == NULL) |
1691 | ftdi_error_return(-1, "ftdi context invalid"); | |
1692 | ||
948f9ada TJ |
1693 | // Invalidate all remaining data |
1694 | ftdi->readbuffer_offset = 0; | |
1695 | ftdi->readbuffer_remaining = 0; | |
1696 | ||
c3d95b87 TJ |
1697 | if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) |
1698 | ftdi_error_return(-1, "out of memory for readbuffer"); | |
d9f0cce7 | 1699 | |
948f9ada TJ |
1700 | ftdi->readbuffer = new_buf; |
1701 | ftdi->readbuffer_chunksize = chunksize; | |
1702 | ||
1703 | return 0; | |
1704 | } | |
1705 | ||
1941414d TJ |
1706 | /** |
1707 | Get read buffer chunk size. | |
948f9ada | 1708 | |
1941414d TJ |
1709 | \param ftdi pointer to ftdi_context |
1710 | \param chunksize Pointer to store chunk size in | |
1711 | ||
1712 | \retval 0: all fine | |
8970ed7e | 1713 | \retval -1: FTDI context invalid |
1941414d | 1714 | */ |
a8f46ddc TJ |
1715 | int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
1716 | { | |
8970ed7e TJ |
1717 | if (ftdi == NULL) |
1718 | ftdi_error_return(-1, "FTDI context invalid"); | |
1719 | ||
948f9ada TJ |
1720 | *chunksize = ftdi->readbuffer_chunksize; |
1721 | return 0; | |
1722 | } | |
1723 | ||
1724 | ||
1941414d TJ |
1725 | /** |
1726 | Enable bitbang mode. | |
948f9ada | 1727 | |
fd282db3 | 1728 | \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead |
1941414d TJ |
1729 | |
1730 | \param ftdi pointer to ftdi_context | |
1731 | \param bitmask Bitmask to configure lines. | |
1732 | HIGH/ON value configures a line as output. | |
1733 | ||
1734 | \retval 0: all fine | |
1735 | \retval -1: can't enable bitbang mode | |
8970ed7e | 1736 | \retval -2: USB device unavailable |
1941414d | 1737 | */ |
a8f46ddc TJ |
1738 | int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) |
1739 | { | |
a3da1d95 GE |
1740 | unsigned short usb_val; |
1741 | ||
8970ed7e TJ |
1742 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1743 | ftdi_error_return(-2, "USB device unavailable"); | |
1744 | ||
d9f0cce7 | 1745 | usb_val = bitmask; // low byte: bitmask |
3119537f TJ |
1746 | /* FT2232C: Set bitbang_mode to 2 to enable SPI */ |
1747 | usb_val |= (ftdi->bitbang_mode << 8); | |
1748 | ||
22d12cda TJ |
1749 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
1750 | SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, | |
a5e1bd8c | 1751 | NULL, 0, ftdi->usb_write_timeout) != 0) |
c3d95b87 TJ |
1752 | ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?"); |
1753 | ||
a3da1d95 GE |
1754 | ftdi->bitbang_enabled = 1; |
1755 | return 0; | |
1756 | } | |
1757 | ||
1941414d TJ |
1758 | /** |
1759 | Disable bitbang mode. | |
a3da1d95 | 1760 | |
1941414d TJ |
1761 | \param ftdi pointer to ftdi_context |
1762 | ||
1763 | \retval 0: all fine | |
1764 | \retval -1: can't disable bitbang mode | |
8970ed7e | 1765 | \retval -2: USB device unavailable |
1941414d | 1766 | */ |
a8f46ddc TJ |
1767 | int ftdi_disable_bitbang(struct ftdi_context *ftdi) |
1768 | { | |
8970ed7e TJ |
1769 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1770 | ftdi_error_return(-2, "USB device unavailable"); | |
1771 | ||
a5e1bd8c | 1772 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
c3d95b87 | 1773 | ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?"); |
a3da1d95 GE |
1774 | |
1775 | ftdi->bitbang_enabled = 0; | |
1776 | return 0; | |
1777 | } | |
1778 | ||
1941414d | 1779 | /** |
418aaa72 | 1780 | Enable/disable bitbang modes. |
a3da1d95 | 1781 | |
1941414d TJ |
1782 | \param ftdi pointer to ftdi_context |
1783 | \param bitmask Bitmask to configure lines. | |
1784 | HIGH/ON value configures a line as output. | |
fd282db3 | 1785 | \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode |
1941414d TJ |
1786 | |
1787 | \retval 0: all fine | |
1788 | \retval -1: can't enable bitbang mode | |
8970ed7e | 1789 | \retval -2: USB device unavailable |
1941414d | 1790 | */ |
c4446c36 TJ |
1791 | int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode) |
1792 | { | |
1793 | unsigned short usb_val; | |
1794 | ||
8970ed7e TJ |
1795 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1796 | ftdi_error_return(-2, "USB device unavailable"); | |
1797 | ||
c4446c36 TJ |
1798 | usb_val = bitmask; // low byte: bitmask |
1799 | usb_val |= (mode << 8); | |
a5e1bd8c | 1800 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
418aaa72 | 1801 | ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps selected mode not supported on your chip?"); |
c4446c36 TJ |
1802 | |
1803 | ftdi->bitbang_mode = mode; | |
418aaa72 | 1804 | ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1; |
c4446c36 TJ |
1805 | return 0; |
1806 | } | |
1807 | ||
1941414d | 1808 | /** |
418aaa72 | 1809 | Directly read pin state, circumventing the read buffer. Useful for bitbang mode. |
1941414d TJ |
1810 | |
1811 | \param ftdi pointer to ftdi_context | |
1812 | \param pins Pointer to store pins into | |
1813 | ||
1814 | \retval 0: all fine | |
1815 | \retval -1: read pins failed | |
8970ed7e | 1816 | \retval -2: USB device unavailable |
1941414d | 1817 | */ |
a8f46ddc TJ |
1818 | int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) |
1819 | { | |
8970ed7e TJ |
1820 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1821 | ftdi_error_return(-2, "USB device unavailable"); | |
1822 | ||
a5e1bd8c | 1823 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_PINS_REQUEST, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1) |
c3d95b87 | 1824 | ftdi_error_return(-1, "read pins failed"); |
a3da1d95 | 1825 | |
a3da1d95 GE |
1826 | return 0; |
1827 | } | |
1828 | ||
1941414d TJ |
1829 | /** |
1830 | Set latency timer | |
1831 | ||
1832 | The FTDI chip keeps data in the internal buffer for a specific | |
1833 | amount of time if the buffer is not full yet to decrease | |
1834 | load on the usb bus. | |
a3da1d95 | 1835 | |
1941414d TJ |
1836 | \param ftdi pointer to ftdi_context |
1837 | \param latency Value between 1 and 255 | |
1838 | ||
1839 | \retval 0: all fine | |
1840 | \retval -1: latency out of range | |
1841 | \retval -2: unable to set latency timer | |
8970ed7e | 1842 | \retval -3: USB device unavailable |
1941414d | 1843 | */ |
a8f46ddc TJ |
1844 | int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) |
1845 | { | |
a3da1d95 GE |
1846 | unsigned short usb_val; |
1847 | ||
c3d95b87 TJ |
1848 | if (latency < 1) |
1849 | ftdi_error_return(-1, "latency out of range. Only valid for 1-255"); | |
a3da1d95 | 1850 | |
8970ed7e TJ |
1851 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1852 | ftdi_error_return(-3, "USB device unavailable"); | |
1853 | ||
d79d2e68 | 1854 | usb_val = latency; |
a5e1bd8c | 1855 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_LATENCY_TIMER_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
c3d95b87 TJ |
1856 | ftdi_error_return(-2, "unable to set latency timer"); |
1857 | ||
a3da1d95 GE |
1858 | return 0; |
1859 | } | |
1860 | ||
1941414d TJ |
1861 | /** |
1862 | Get latency timer | |
a3da1d95 | 1863 | |
1941414d TJ |
1864 | \param ftdi pointer to ftdi_context |
1865 | \param latency Pointer to store latency value in | |
1866 | ||
1867 | \retval 0: all fine | |
1868 | \retval -1: unable to get latency timer | |
8970ed7e | 1869 | \retval -2: USB device unavailable |
1941414d | 1870 | */ |
a8f46ddc TJ |
1871 | int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) |
1872 | { | |
a3da1d95 | 1873 | unsigned short usb_val; |
8970ed7e TJ |
1874 | |
1875 | if (ftdi == NULL || ftdi->usb_dev == NULL) | |
1876 | ftdi_error_return(-2, "USB device unavailable"); | |
1877 | ||
a5e1bd8c | 1878 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_GET_LATENCY_TIMER_REQUEST, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) |
c3d95b87 | 1879 | ftdi_error_return(-1, "reading latency timer failed"); |
a3da1d95 GE |
1880 | |
1881 | *latency = (unsigned char)usb_val; | |
1882 | return 0; | |
1883 | } | |
1884 | ||
1941414d | 1885 | /** |
1189b11a TJ |
1886 | Poll modem status information |
1887 | ||
1888 | This function allows the retrieve the two status bytes of the device. | |
1889 | The device sends these bytes also as a header for each read access | |
1890 | where they are discarded by ftdi_read_data(). The chip generates | |
1891 | the two stripped status bytes in the absence of data every 40 ms. | |
1892 | ||
1893 | Layout of the first byte: | |
1894 | - B0..B3 - must be 0 | |
1895 | - B4 Clear to send (CTS) | |
1896 | 0 = inactive | |
1897 | 1 = active | |
1898 | - B5 Data set ready (DTS) | |
1899 | 0 = inactive | |
1900 | 1 = active | |
1901 | - B6 Ring indicator (RI) | |
1902 | 0 = inactive | |
1903 | 1 = active | |
1904 | - B7 Receive line signal detect (RLSD) | |
1905 | 0 = inactive | |
1906 | 1 = active | |
1907 | ||
1908 | Layout of the second byte: | |
1909 | - B0 Data ready (DR) | |
1910 | - B1 Overrun error (OE) | |
1911 | - B2 Parity error (PE) | |
1912 | - B3 Framing error (FE) | |
1913 | - B4 Break interrupt (BI) | |
1914 | - B5 Transmitter holding register (THRE) | |
1915 | - B6 Transmitter empty (TEMT) | |
1916 | - B7 Error in RCVR FIFO | |
1917 | ||
1918 | \param ftdi pointer to ftdi_context | |
1919 | \param status Pointer to store status information in. Must be two bytes. | |
1920 | ||
1921 | \retval 0: all fine | |
1922 | \retval -1: unable to retrieve status information | |
8970ed7e | 1923 | \retval -2: USB device unavailable |
1189b11a TJ |
1924 | */ |
1925 | int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status) | |
1926 | { | |
1927 | char usb_val[2]; | |
1928 | ||
8970ed7e TJ |
1929 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1930 | ftdi_error_return(-2, "USB device unavailable"); | |
1931 | ||
a5e1bd8c | 1932 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, usb_val, 2, ftdi->usb_read_timeout) != 2) |
1189b11a TJ |
1933 | ftdi_error_return(-1, "getting modem status failed"); |
1934 | ||
1935 | *status = (usb_val[1] << 8) | usb_val[0]; | |
1936 | ||
1937 | return 0; | |
1938 | } | |
1939 | ||
a7fb8440 TJ |
1940 | /** |
1941 | Set flowcontrol for ftdi chip | |
1942 | ||
1943 | \param ftdi pointer to ftdi_context | |
22d12cda TJ |
1944 | \param flowctrl flow control to use. should be |
1945 | SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS | |
a7fb8440 TJ |
1946 | |
1947 | \retval 0: all fine | |
1948 | \retval -1: set flow control failed | |
8970ed7e | 1949 | \retval -2: USB device unavailable |
a7fb8440 TJ |
1950 | */ |
1951 | int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl) | |
1952 | { | |
8970ed7e TJ |
1953 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1954 | ftdi_error_return(-2, "USB device unavailable"); | |
1955 | ||
a5e1bd8c | 1956 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
9ecfef2a | 1957 | SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index), |
a7fb8440 TJ |
1958 | NULL, 0, ftdi->usb_write_timeout) != 0) |
1959 | ftdi_error_return(-1, "set flow control failed"); | |
1960 | ||
1961 | return 0; | |
1962 | } | |
1963 | ||
1964 | /** | |
1965 | Set dtr line | |
1966 | ||
1967 | \param ftdi pointer to ftdi_context | |
1968 | \param state state to set line to (1 or 0) | |
1969 | ||
1970 | \retval 0: all fine | |
1971 | \retval -1: set dtr failed | |
8970ed7e | 1972 | \retval -2: USB device unavailable |
a7fb8440 TJ |
1973 | */ |
1974 | int ftdi_setdtr(struct ftdi_context *ftdi, int state) | |
1975 | { | |
1976 | unsigned short usb_val; | |
1977 | ||
8970ed7e TJ |
1978 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1979 | ftdi_error_return(-2, "USB device unavailable"); | |
1980 | ||
a7fb8440 TJ |
1981 | if (state) |
1982 | usb_val = SIO_SET_DTR_HIGH; | |
1983 | else | |
1984 | usb_val = SIO_SET_DTR_LOW; | |
1985 | ||
a5e1bd8c | 1986 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
9ecfef2a | 1987 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index, |
a7fb8440 TJ |
1988 | NULL, 0, ftdi->usb_write_timeout) != 0) |
1989 | ftdi_error_return(-1, "set dtr failed"); | |
1990 | ||
1991 | return 0; | |
1992 | } | |
1993 | ||
1994 | /** | |
1995 | Set rts line | |
1996 | ||
1997 | \param ftdi pointer to ftdi_context | |
1998 | \param state state to set line to (1 or 0) | |
1999 | ||
2000 | \retval 0: all fine | |
8970ed7e TJ |
2001 | \retval -1: set rts failed |
2002 | \retval -2: USB device unavailable | |
a7fb8440 TJ |
2003 | */ |
2004 | int ftdi_setrts(struct ftdi_context *ftdi, int state) | |
2005 | { | |
2006 | unsigned short usb_val; | |
2007 | ||
8970ed7e TJ |
2008 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2009 | ftdi_error_return(-2, "USB device unavailable"); | |
2010 | ||
a7fb8440 TJ |
2011 | if (state) |
2012 | usb_val = SIO_SET_RTS_HIGH; | |
2013 | else | |
2014 | usb_val = SIO_SET_RTS_LOW; | |
2015 | ||
a5e1bd8c | 2016 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
9ecfef2a | 2017 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index, |
a7fb8440 TJ |
2018 | NULL, 0, ftdi->usb_write_timeout) != 0) |
2019 | ftdi_error_return(-1, "set of rts failed"); | |
2020 | ||
2021 | return 0; | |
2022 | } | |
2023 | ||
1189b11a | 2024 | /** |
8970ed7e | 2025 | Set dtr and rts line in one pass |
9ecfef2a | 2026 | |
8970ed7e TJ |
2027 | \param ftdi pointer to ftdi_context |
2028 | \param dtr DTR state to set line to (1 or 0) | |
2029 | \param rts RTS state to set line to (1 or 0) | |
9ecfef2a | 2030 | |
8970ed7e TJ |
2031 | \retval 0: all fine |
2032 | \retval -1: set dtr/rts failed | |
2033 | \retval -2: USB device unavailable | |
9ecfef2a TJ |
2034 | */ |
2035 | int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts) | |
2036 | { | |
2037 | unsigned short usb_val; | |
2038 | ||
8970ed7e TJ |
2039 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2040 | ftdi_error_return(-2, "USB device unavailable"); | |
2041 | ||
9ecfef2a | 2042 | if (dtr) |
22d12cda | 2043 | usb_val = SIO_SET_DTR_HIGH; |
9ecfef2a | 2044 | else |
22d12cda | 2045 | usb_val = SIO_SET_DTR_LOW; |
9ecfef2a TJ |
2046 | |
2047 | if (rts) | |
22d12cda | 2048 | usb_val |= SIO_SET_RTS_HIGH; |
9ecfef2a | 2049 | else |
22d12cda | 2050 | usb_val |= SIO_SET_RTS_LOW; |
9ecfef2a | 2051 | |
a5e1bd8c | 2052 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
9ecfef2a TJ |
2053 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index, |
2054 | NULL, 0, ftdi->usb_write_timeout) != 0) | |
22d12cda | 2055 | ftdi_error_return(-1, "set of rts/dtr failed"); |
9ecfef2a TJ |
2056 | |
2057 | return 0; | |
2058 | } | |
2059 | ||
2060 | /** | |
1189b11a TJ |
2061 | Set the special event character |
2062 | ||
2063 | \param ftdi pointer to ftdi_context | |
2064 | \param eventch Event character | |
2065 | \param enable 0 to disable the event character, non-zero otherwise | |
2066 | ||
2067 | \retval 0: all fine | |
2068 | \retval -1: unable to set event character | |
8970ed7e | 2069 | \retval -2: USB device unavailable |
1189b11a TJ |
2070 | */ |
2071 | int ftdi_set_event_char(struct ftdi_context *ftdi, | |
22d12cda | 2072 | unsigned char eventch, unsigned char enable) |
1189b11a TJ |
2073 | { |
2074 | unsigned short usb_val; | |
2075 | ||
8970ed7e TJ |
2076 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2077 | ftdi_error_return(-2, "USB device unavailable"); | |
2078 | ||
1189b11a TJ |
2079 | usb_val = eventch; |
2080 | if (enable) | |
2081 | usb_val |= 1 << 8; | |
2082 | ||
a5e1bd8c | 2083 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_EVENT_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
1189b11a TJ |
2084 | ftdi_error_return(-1, "setting event character failed"); |
2085 | ||
2086 | return 0; | |
2087 | } | |
2088 | ||
2089 | /** | |
2090 | Set error character | |
2091 | ||
2092 | \param ftdi pointer to ftdi_context | |
2093 | \param errorch Error character | |
2094 | \param enable 0 to disable the error character, non-zero otherwise | |
2095 | ||
2096 | \retval 0: all fine | |
2097 | \retval -1: unable to set error character | |
8970ed7e | 2098 | \retval -2: USB device unavailable |
1189b11a TJ |
2099 | */ |
2100 | int ftdi_set_error_char(struct ftdi_context *ftdi, | |
22d12cda | 2101 | unsigned char errorch, unsigned char enable) |
1189b11a TJ |
2102 | { |
2103 | unsigned short usb_val; | |
2104 | ||
8970ed7e TJ |
2105 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2106 | ftdi_error_return(-2, "USB device unavailable"); | |
2107 | ||
1189b11a TJ |
2108 | usb_val = errorch; |
2109 | if (enable) | |
2110 | usb_val |= 1 << 8; | |
2111 | ||
a5e1bd8c | 2112 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_ERROR_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
1189b11a TJ |
2113 | ftdi_error_return(-1, "setting error character failed"); |
2114 | ||
2115 | return 0; | |
2116 | } | |
2117 | ||
2118 | /** | |
c201f80f TJ |
2119 | Set the eeprom size |
2120 | ||
2121 | \param ftdi pointer to ftdi_context | |
2122 | \param eeprom Pointer to ftdi_eeprom | |
2123 | \param size | |
2124 | ||
2125 | */ | |
2126 | void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size) | |
2127 | { | |
8970ed7e TJ |
2128 | if (ftdi == NULL) |
2129 | return; | |
2130 | ||
22d12cda TJ |
2131 | ftdi->eeprom_size=size; |
2132 | eeprom->size=size; | |
c201f80f TJ |
2133 | } |
2134 | ||
2135 | /** | |
1941414d | 2136 | Init eeprom with default values. |
a3da1d95 | 2137 | |
1941414d TJ |
2138 | \param eeprom Pointer to ftdi_eeprom |
2139 | */ | |
a8f46ddc TJ |
2140 | void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) |
2141 | { | |
2ff8b87c HK |
2142 | int i; |
2143 | ||
8970ed7e TJ |
2144 | if (eeprom == NULL) |
2145 | return; | |
2146 | ||
f396dbad TJ |
2147 | eeprom->vendor_id = 0x0403; |
2148 | eeprom->product_id = 0x6001; | |
d9f0cce7 | 2149 | |
b8aa7b35 TJ |
2150 | eeprom->self_powered = 1; |
2151 | eeprom->remote_wakeup = 1; | |
2ff8b87c | 2152 | eeprom->chip_type = TYPE_BM; |
d9f0cce7 | 2153 | |
b8aa7b35 TJ |
2154 | eeprom->in_is_isochronous = 0; |
2155 | eeprom->out_is_isochronous = 0; | |
2156 | eeprom->suspend_pull_downs = 0; | |
d9f0cce7 | 2157 | |
b8aa7b35 TJ |
2158 | eeprom->use_serial = 0; |
2159 | eeprom->change_usb_version = 0; | |
f396dbad | 2160 | eeprom->usb_version = 0x0200; |
b8aa7b35 | 2161 | eeprom->max_power = 0; |
d9f0cce7 | 2162 | |
b8aa7b35 TJ |
2163 | eeprom->manufacturer = NULL; |
2164 | eeprom->product = NULL; | |
2165 | eeprom->serial = NULL; | |
2ff8b87c HK |
2166 | for (i=0; i < 5; i++) |
2167 | { | |
2168 | eeprom->cbus_function[i] = 0; | |
2169 | } | |
2170 | eeprom->high_current = 0; | |
2171 | eeprom->invert = 0; | |
c201f80f TJ |
2172 | |
2173 | eeprom->size = FTDI_DEFAULT_EEPROM_SIZE; | |
b8aa7b35 TJ |
2174 | } |
2175 | ||
1941414d | 2176 | /** |
4aee4ad2 WH |
2177 | Frees allocated memory in eeprom. |
2178 | ||
2179 | \param eeprom Pointer to ftdi_eeprom | |
2180 | */ | |
2181 | void ftdi_eeprom_free(struct ftdi_eeprom *eeprom) | |
2182 | { | |
c2909fee TJ |
2183 | if (!eeprom) |
2184 | return; | |
2185 | ||
4aee4ad2 WH |
2186 | if (eeprom->manufacturer != 0) { |
2187 | free(eeprom->manufacturer); | |
2188 | eeprom->manufacturer = 0; | |
2189 | } | |
2190 | if (eeprom->product != 0) { | |
2191 | free(eeprom->product); | |
2192 | eeprom->product = 0; | |
2193 | } | |
2194 | if (eeprom->serial != 0) { | |
2195 | free(eeprom->serial); | |
2196 | eeprom->serial = 0; | |
2197 | } | |
2198 | } | |
2199 | ||
2200 | /** | |
8970ed7e TJ |
2201 | Build binary output from ftdi_eeprom structure. |
2202 | Output is suitable for ftdi_write_eeprom(). | |
b8aa7b35 | 2203 | |
2ff8b87c | 2204 | \note This function doesn't handle FT2232x devices. Only FT232x. |
8970ed7e TJ |
2205 | \param eeprom Pointer to ftdi_eeprom |
2206 | \param output Buffer of 128 bytes to store eeprom image to | |
1941414d | 2207 | |
2ff8b87c | 2208 | \retval >0: free eeprom size |
8970ed7e TJ |
2209 | \retval -1: eeprom size (128 bytes) exceeded by custom strings |
2210 | \retval -2: Invalid eeprom pointer | |
2ff8b87c HK |
2211 | \retval -3: Invalid cbus function setting |
2212 | \retval -4: Chip doesn't support invert | |
2213 | \retval -5: Chip doesn't support high current drive | |
b8aa7b35 | 2214 | */ |
a8f46ddc TJ |
2215 | int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) |
2216 | { | |
b8aa7b35 TJ |
2217 | unsigned char i, j; |
2218 | unsigned short checksum, value; | |
2219 | unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0; | |
2220 | int size_check; | |
2ff8b87c | 2221 | const int cbus_max[5] = {13, 13, 13, 13, 9}; |
b8aa7b35 | 2222 | |
8970ed7e TJ |
2223 | if (eeprom == NULL) |
2224 | return -2; | |
2225 | ||
b8aa7b35 | 2226 | if (eeprom->manufacturer != NULL) |
d9f0cce7 | 2227 | manufacturer_size = strlen(eeprom->manufacturer); |
b8aa7b35 | 2228 | if (eeprom->product != NULL) |
d9f0cce7 | 2229 | product_size = strlen(eeprom->product); |
b8aa7b35 | 2230 | if (eeprom->serial != NULL) |
d9f0cce7 | 2231 | serial_size = strlen(eeprom->serial); |
b8aa7b35 | 2232 | |
2ff8b87c HK |
2233 | // highest allowed cbus value |
2234 | for (i = 0; i < 5; i++) | |
2235 | { | |
2236 | if ((eeprom->cbus_function[i] > cbus_max[i]) || | |
2237 | (eeprom->cbus_function[i] && eeprom->chip_type != TYPE_R)) return -3; | |
2238 | } | |
2239 | if (eeprom->chip_type != TYPE_R) | |
2240 | { | |
2241 | if (eeprom->invert) return -4; | |
2242 | if (eeprom->high_current) return -5; | |
2243 | } | |
2244 | ||
c201f80f | 2245 | size_check = eeprom->size; |
d9f0cce7 | 2246 | size_check -= 28; // 28 are always in use (fixed) |
c201f80f | 2247 | |
22d12cda | 2248 | // Top half of a 256byte eeprom is used just for strings and checksum |
c201f80f TJ |
2249 | // it seems that the FTDI chip will not read these strings from the lower half |
2250 | // Each string starts with two bytes; offset and type (0x03 for string) | |
2251 | // the checksum needs two bytes, so without the string data that 8 bytes from the top half | |
2ff8b87c | 2252 | if (eeprom->size>=256) size_check = 120; |
b8aa7b35 TJ |
2253 | size_check -= manufacturer_size*2; |
2254 | size_check -= product_size*2; | |
2255 | size_check -= serial_size*2; | |
2256 | ||
2257 | // eeprom size exceeded? | |
2258 | if (size_check < 0) | |
d9f0cce7 | 2259 | return (-1); |
b8aa7b35 TJ |
2260 | |
2261 | // empty eeprom | |
c201f80f | 2262 | memset (output, 0, eeprom->size); |
b8aa7b35 | 2263 | |
2ff8b87c HK |
2264 | // Addr 00: High current IO |
2265 | output[0x00] = eeprom->high_current ? HIGH_CURRENT_DRIVE : 0; | |
2266 | // Addr 01: IN endpoint size (for R type devices, different for FT2232) | |
2267 | if (eeprom->chip_type == TYPE_R) { | |
2268 | output[0x01] = 0x40; | |
2269 | } | |
b8aa7b35 TJ |
2270 | // Addr 02: Vendor ID |
2271 | output[0x02] = eeprom->vendor_id; | |
2272 | output[0x03] = eeprom->vendor_id >> 8; | |
2273 | ||
2274 | // Addr 04: Product ID | |
2275 | output[0x04] = eeprom->product_id; | |
2276 | output[0x05] = eeprom->product_id >> 8; | |
2277 | ||
2278 | // Addr 06: Device release number (0400h for BM features) | |
2279 | output[0x06] = 0x00; | |
2ff8b87c HK |
2280 | switch (eeprom->chip_type) { |
2281 | case TYPE_AM: | |
2282 | output[0x07] = 0x02; | |
2283 | break; | |
2284 | case TYPE_BM: | |
2285 | output[0x07] = 0x04; | |
2286 | break; | |
2287 | case TYPE_2232C: | |
2288 | output[0x07] = 0x05; | |
2289 | break; | |
2290 | case TYPE_R: | |
2291 | output[0x07] = 0x06; | |
2292 | break; | |
2293 | default: | |
2294 | output[0x07] = 0x00; | |
2295 | } | |
b8aa7b35 TJ |
2296 | |
2297 | // Addr 08: Config descriptor | |
8fae3e8e TJ |
2298 | // Bit 7: always 1 |
2299 | // Bit 6: 1 if this device is self powered, 0 if bus powered | |
2300 | // Bit 5: 1 if this device uses remote wakeup | |
2301 | // Bit 4: 1 if this device is battery powered | |
5a1dcd55 | 2302 | j = 0x80; |
b8aa7b35 | 2303 | if (eeprom->self_powered == 1) |
5a1dcd55 | 2304 | j |= 0x40; |
b8aa7b35 | 2305 | if (eeprom->remote_wakeup == 1) |
5a1dcd55 | 2306 | j |= 0x20; |
b8aa7b35 TJ |
2307 | output[0x08] = j; |
2308 | ||
2309 | // Addr 09: Max power consumption: max power = value * 2 mA | |
d9f0cce7 | 2310 | output[0x09] = eeprom->max_power; |
d9f0cce7 | 2311 | |
b8aa7b35 TJ |
2312 | // Addr 0A: Chip configuration |
2313 | // Bit 7: 0 - reserved | |
2314 | // Bit 6: 0 - reserved | |
2315 | // Bit 5: 0 - reserved | |
2316 | // Bit 4: 1 - Change USB version | |
2317 | // Bit 3: 1 - Use the serial number string | |
2318 | // Bit 2: 1 - Enable suspend pull downs for lower power | |
2319 | // Bit 1: 1 - Out EndPoint is Isochronous | |
2320 | // Bit 0: 1 - In EndPoint is Isochronous | |
2321 | // | |
2322 | j = 0; | |
2323 | if (eeprom->in_is_isochronous == 1) | |
d9f0cce7 | 2324 | j = j | 1; |
b8aa7b35 | 2325 | if (eeprom->out_is_isochronous == 1) |
d9f0cce7 | 2326 | j = j | 2; |
b8aa7b35 | 2327 | if (eeprom->suspend_pull_downs == 1) |
d9f0cce7 | 2328 | j = j | 4; |
b8aa7b35 | 2329 | if (eeprom->use_serial == 1) |
d9f0cce7 | 2330 | j = j | 8; |
b8aa7b35 | 2331 | if (eeprom->change_usb_version == 1) |
d9f0cce7 | 2332 | j = j | 16; |
b8aa7b35 | 2333 | output[0x0A] = j; |
d9f0cce7 | 2334 | |
2ff8b87c HK |
2335 | // Addr 0B: Invert data lines |
2336 | output[0x0B] = eeprom->invert & 0xff; | |
d9f0cce7 | 2337 | |
b8aa7b35 TJ |
2338 | // Addr 0C: USB version low byte when 0x0A bit 4 is set |
2339 | // Addr 0D: USB version high byte when 0x0A bit 4 is set | |
22d12cda TJ |
2340 | if (eeprom->change_usb_version == 1) |
2341 | { | |
b8aa7b35 | 2342 | output[0x0C] = eeprom->usb_version; |
d9f0cce7 | 2343 | output[0x0D] = eeprom->usb_version >> 8; |
b8aa7b35 TJ |
2344 | } |
2345 | ||
2346 | ||
c201f80f | 2347 | // Addr 0E: Offset of the manufacturer string + 0x80, calculated later |
b8aa7b35 TJ |
2348 | // Addr 0F: Length of manufacturer string |
2349 | output[0x0F] = manufacturer_size*2 + 2; | |
2350 | ||
2351 | // Addr 10: Offset of the product string + 0x80, calculated later | |
2352 | // Addr 11: Length of product string | |
2353 | output[0x11] = product_size*2 + 2; | |
2354 | ||
2355 | // Addr 12: Offset of the serial string + 0x80, calculated later | |
2356 | // Addr 13: Length of serial string | |
2357 | output[0x13] = serial_size*2 + 2; | |
2358 | ||
2ff8b87c HK |
2359 | // Addr 14: CBUS function: CBUS0, CBUS1 |
2360 | // Addr 15: CBUS function: CBUS2, CBUS3 | |
2361 | // Addr 16: CBUS function: CBUS5 | |
2362 | output[0x14] = eeprom->cbus_function[0] | (eeprom->cbus_function[1] << 4); | |
2363 | output[0x15] = eeprom->cbus_function[2] | (eeprom->cbus_function[3] << 4); | |
2364 | output[0x16] = eeprom->cbus_function[4]; | |
2365 | // Addr 17: Unknown | |
2366 | ||
b8aa7b35 | 2367 | // Dynamic content |
2ff8b87c HK |
2368 | // In images produced by FTDI's FT_Prog for FT232R strings start at 0x18 |
2369 | // Space till 0x18 should be considered as reserved. | |
2370 | if (eeprom->chip_type >= TYPE_R) { | |
2371 | i = 0x18; | |
2372 | } else { | |
2373 | i = 0x14; | |
2374 | } | |
2375 | if (eeprom->size >= 256) i = 0x80; | |
f01d7ca6 | 2376 | |
c201f80f | 2377 | |
22d12cda | 2378 | // Output manufacturer |
c201f80f TJ |
2379 | output[0x0E] = i | 0x80; // calculate offset |
2380 | output[i++] = manufacturer_size*2 + 2; | |
2381 | output[i++] = 0x03; // type: string | |
22d12cda TJ |
2382 | for (j = 0; j < manufacturer_size; j++) |
2383 | { | |
d9f0cce7 TJ |
2384 | output[i] = eeprom->manufacturer[j], i++; |
2385 | output[i] = 0x00, i++; | |
b8aa7b35 TJ |
2386 | } |
2387 | ||
2388 | // Output product name | |
c201f80f | 2389 | output[0x10] = i | 0x80; // calculate offset |
b8aa7b35 TJ |
2390 | output[i] = product_size*2 + 2, i++; |
2391 | output[i] = 0x03, i++; | |
22d12cda TJ |
2392 | for (j = 0; j < product_size; j++) |
2393 | { | |
d9f0cce7 TJ |
2394 | output[i] = eeprom->product[j], i++; |
2395 | output[i] = 0x00, i++; | |
b8aa7b35 | 2396 | } |
d9f0cce7 | 2397 | |
b8aa7b35 | 2398 | // Output serial |
c201f80f | 2399 | output[0x12] = i | 0x80; // calculate offset |
b8aa7b35 TJ |
2400 | output[i] = serial_size*2 + 2, i++; |
2401 | output[i] = 0x03, i++; | |
22d12cda TJ |
2402 | for (j = 0; j < serial_size; j++) |
2403 | { | |
d9f0cce7 TJ |
2404 | output[i] = eeprom->serial[j], i++; |
2405 | output[i] = 0x00, i++; | |
b8aa7b35 TJ |
2406 | } |
2407 | ||
2408 | // calculate checksum | |
2409 | checksum = 0xAAAA; | |
d9f0cce7 | 2410 | |
22d12cda TJ |
2411 | for (i = 0; i < eeprom->size/2-1; i++) |
2412 | { | |
d9f0cce7 TJ |
2413 | value = output[i*2]; |
2414 | value += output[(i*2)+1] << 8; | |
b8aa7b35 | 2415 | |
d9f0cce7 TJ |
2416 | checksum = value^checksum; |
2417 | checksum = (checksum << 1) | (checksum >> 15); | |
b8aa7b35 TJ |
2418 | } |
2419 | ||
c201f80f TJ |
2420 | output[eeprom->size-2] = checksum; |
2421 | output[eeprom->size-1] = checksum >> 8; | |
b8aa7b35 | 2422 | |
8ed61121 | 2423 | return size_check; |
b8aa7b35 TJ |
2424 | } |
2425 | ||
4af1d1bb MK |
2426 | /** |
2427 | Decode binary EEPROM image into an ftdi_eeprom structure. | |
2428 | ||
2429 | \param eeprom Pointer to ftdi_eeprom which will be filled in. | |
1bbaf1ce | 2430 | \param buf Buffer of \a size bytes of raw eeprom data |
4af1d1bb MK |
2431 | \param size size size of eeprom data in bytes |
2432 | ||
2433 | \retval 0: all fine | |
2434 | \retval -1: something went wrong | |
2435 | ||
2436 | FIXME: How to pass size? How to handle size field in ftdi_eeprom? | |
2437 | FIXME: Strings are malloc'ed here and should be freed somewhere | |
2438 | */ | |
49c5ac72 | 2439 | int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size) |
b56d5a64 MK |
2440 | { |
2441 | unsigned char i, j; | |
2442 | unsigned short checksum, eeprom_checksum, value; | |
2443 | unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0; | |
2444 | int size_check; | |
2445 | int eeprom_size = 128; | |
8970ed7e TJ |
2446 | |
2447 | if (eeprom == NULL) | |
2448 | return -1; | |
b56d5a64 MK |
2449 | #if 0 |
2450 | size_check = eeprom->size; | |
2451 | size_check -= 28; // 28 are always in use (fixed) | |
2452 | ||
22d12cda | 2453 | // Top half of a 256byte eeprom is used just for strings and checksum |
b56d5a64 MK |
2454 | // it seems that the FTDI chip will not read these strings from the lower half |
2455 | // Each string starts with two bytes; offset and type (0x03 for string) | |
2456 | // the checksum needs two bytes, so without the string data that 8 bytes from the top half | |
22d12cda | 2457 | if (eeprom->size>=256)size_check = 120; |
b56d5a64 MK |
2458 | size_check -= manufacturer_size*2; |
2459 | size_check -= product_size*2; | |
2460 | size_check -= serial_size*2; | |
2461 | ||
2462 | // eeprom size exceeded? | |
2463 | if (size_check < 0) | |
2464 | return (-1); | |
2465 | #endif | |
2466 | ||
2467 | // empty eeprom struct | |
4af1d1bb | 2468 | memset(eeprom, 0, sizeof(struct ftdi_eeprom)); |
b56d5a64 | 2469 | |
2ff8b87c HK |
2470 | // Addr 00: High current IO |
2471 | eeprom->high_current = (buf[0x02] & HIGH_CURRENT_DRIVE); | |
b56d5a64 MK |
2472 | |
2473 | // Addr 02: Vendor ID | |
2474 | eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8); | |
2475 | ||
2476 | // Addr 04: Product ID | |
2477 | eeprom->product_id = buf[0x04] + (buf[0x05] << 8); | |
22d12cda | 2478 | |
6335545d TJ |
2479 | value = buf[0x06] + (buf[0x07]<<8); |
2480 | switch (value) | |
22d12cda | 2481 | { |
2ff8b87c HK |
2482 | case 0x0600: |
2483 | eeprom->chip_type = TYPE_R; | |
2484 | break; | |
22d12cda | 2485 | case 0x0400: |
2ff8b87c | 2486 | eeprom->chip_type = TYPE_BM; |
22d12cda TJ |
2487 | break; |
2488 | case 0x0200: | |
2ff8b87c | 2489 | eeprom->chip_type = TYPE_AM; |
22d12cda TJ |
2490 | break; |
2491 | default: // Unknown device | |
2ff8b87c | 2492 | eeprom->chip_type = 0; |
22d12cda | 2493 | break; |
4af1d1bb | 2494 | } |
b56d5a64 MK |
2495 | |
2496 | // Addr 08: Config descriptor | |
2497 | // Bit 7: always 1 | |
2498 | // Bit 6: 1 if this device is self powered, 0 if bus powered | |
2499 | // Bit 5: 1 if this device uses remote wakeup | |
2500 | // Bit 4: 1 if this device is battery powered | |
2501 | j = buf[0x08]; | |
b56d5a64 MK |
2502 | if (j&0x40) eeprom->self_powered = 1; |
2503 | if (j&0x20) eeprom->remote_wakeup = 1; | |
2504 | ||
2505 | // Addr 09: Max power consumption: max power = value * 2 mA | |
2506 | eeprom->max_power = buf[0x09]; | |
2507 | ||
2508 | // Addr 0A: Chip configuration | |
2509 | // Bit 7: 0 - reserved | |
2510 | // Bit 6: 0 - reserved | |
2511 | // Bit 5: 0 - reserved | |
2512 | // Bit 4: 1 - Change USB version | |
2513 | // Bit 3: 1 - Use the serial number string | |
2514 | // Bit 2: 1 - Enable suspend pull downs for lower power | |
2515 | // Bit 1: 1 - Out EndPoint is Isochronous | |
2516 | // Bit 0: 1 - In EndPoint is Isochronous | |
2517 | // | |
2518 | j = buf[0x0A]; | |
4af1d1bb MK |
2519 | if (j&0x01) eeprom->in_is_isochronous = 1; |
2520 | if (j&0x02) eeprom->out_is_isochronous = 1; | |
2521 | if (j&0x04) eeprom->suspend_pull_downs = 1; | |
2522 | if (j&0x08) eeprom->use_serial = 1; | |
2523 | if (j&0x10) eeprom->change_usb_version = 1; | |
b56d5a64 | 2524 | |
2ff8b87c HK |
2525 | // Addr 0B: Invert data lines |
2526 | eeprom->invert = buf[0x0B]; | |
b56d5a64 MK |
2527 | |
2528 | // Addr 0C: USB version low byte when 0x0A bit 4 is set | |
2529 | // Addr 0D: USB version high byte when 0x0A bit 4 is set | |
22d12cda TJ |
2530 | if (eeprom->change_usb_version == 1) |
2531 | { | |
2532 | eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8); | |
b56d5a64 MK |
2533 | } |
2534 | ||
2535 | // Addr 0E: Offset of the manufacturer string + 0x80, calculated later | |
2536 | // Addr 0F: Length of manufacturer string | |
2537 | manufacturer_size = buf[0x0F]/2; | |
2538 | if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size); | |
2539 | else eeprom->manufacturer = NULL; | |
2540 | ||
2541 | // Addr 10: Offset of the product string + 0x80, calculated later | |
2542 | // Addr 11: Length of product string | |
2543 | product_size = buf[0x11]/2; | |
2544 | if (product_size > 0) eeprom->product = malloc(product_size); | |
2545 | else eeprom->product = NULL; | |
2546 | ||
2547 | // Addr 12: Offset of the serial string + 0x80, calculated later | |
2548 | // Addr 13: Length of serial string | |
2549 | serial_size = buf[0x13]/2; | |
2550 | if (serial_size > 0) eeprom->serial = malloc(serial_size); | |
2551 | else eeprom->serial = NULL; | |
2552 | ||
2ff8b87c HK |
2553 | // Addr 14: CBUS function: CBUS0, CBUS1 |
2554 | // Addr 15: CBUS function: CBUS2, CBUS3 | |
2555 | // Addr 16: CBUS function: CBUS5 | |
2556 | if (eeprom->chip_type == TYPE_R) { | |
2557 | eeprom->cbus_function[0] = buf[0x14] & 0x0f; | |
2558 | eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f; | |
2559 | eeprom->cbus_function[2] = buf[0x15] & 0x0f; | |
2560 | eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f; | |
2561 | eeprom->cbus_function[4] = buf[0x16] & 0x0f; | |
2562 | } else { | |
2563 | for (j=0; j<5; j++) eeprom->cbus_function[j] = 0; | |
2564 | } | |
2565 | ||
22d12cda | 2566 | // Decode manufacturer |
b56d5a64 | 2567 | i = buf[0x0E] & 0x7f; // offset |
22d12cda TJ |
2568 | for (j=0;j<manufacturer_size-1;j++) |
2569 | { | |
2570 | eeprom->manufacturer[j] = buf[2*j+i+2]; | |
b56d5a64 MK |
2571 | } |
2572 | eeprom->manufacturer[j] = '\0'; | |
2573 | ||
2574 | // Decode product name | |
2575 | i = buf[0x10] & 0x7f; // offset | |
22d12cda TJ |
2576 | for (j=0;j<product_size-1;j++) |
2577 | { | |
2578 | eeprom->product[j] = buf[2*j+i+2]; | |
b56d5a64 MK |
2579 | } |
2580 | eeprom->product[j] = '\0'; | |
2581 | ||
2582 | // Decode serial | |
2583 | i = buf[0x12] & 0x7f; // offset | |
22d12cda TJ |
2584 | for (j=0;j<serial_size-1;j++) |
2585 | { | |
2586 | eeprom->serial[j] = buf[2*j+i+2]; | |
b56d5a64 MK |
2587 | } |
2588 | eeprom->serial[j] = '\0'; | |
2589 | ||
2590 | // verify checksum | |
2591 | checksum = 0xAAAA; | |
2592 | ||
22d12cda TJ |
2593 | for (i = 0; i < eeprom_size/2-1; i++) |
2594 | { | |
b56d5a64 MK |
2595 | value = buf[i*2]; |
2596 | value += buf[(i*2)+1] << 8; | |
2597 | ||
2598 | checksum = value^checksum; | |
2599 | checksum = (checksum << 1) | (checksum >> 15); | |
2600 | } | |
2601 | ||
2602 | eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8); | |
2603 | ||
22d12cda TJ |
2604 | if (eeprom_checksum != checksum) |
2605 | { | |
2606 | fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum); | |
2607 | return -1; | |
4af1d1bb MK |
2608 | } |
2609 | ||
2610 | return 0; | |
b56d5a64 MK |
2611 | } |
2612 | ||
1941414d | 2613 | /** |
c1c70e13 OS |
2614 | Read eeprom location |
2615 | ||
2616 | \param ftdi pointer to ftdi_context | |
2617 | \param eeprom_addr Address of eeprom location to be read | |
2618 | \param eeprom_val Pointer to store read eeprom location | |
2619 | ||
2620 | \retval 0: all fine | |
2621 | \retval -1: read failed | |
8970ed7e | 2622 | \retval -2: USB device unavailable |
c1c70e13 OS |
2623 | */ |
2624 | int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val) | |
2625 | { | |
8970ed7e TJ |
2626 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2627 | ftdi_error_return(-2, "USB device unavailable"); | |
2628 | ||
c1c70e13 OS |
2629 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2) |
2630 | ftdi_error_return(-1, "reading eeprom failed"); | |
2631 | ||
2632 | return 0; | |
2633 | } | |
2634 | ||
2635 | /** | |
1941414d TJ |
2636 | Read eeprom |
2637 | ||
2638 | \param ftdi pointer to ftdi_context | |
2639 | \param eeprom Pointer to store eeprom into | |
b8aa7b35 | 2640 | |
1941414d TJ |
2641 | \retval 0: all fine |
2642 | \retval -1: read failed | |
8970ed7e | 2643 | \retval -2: USB device unavailable |
1941414d | 2644 | */ |
a8f46ddc TJ |
2645 | int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) |
2646 | { | |
a3da1d95 GE |
2647 | int i; |
2648 | ||
8970ed7e TJ |
2649 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2650 | ftdi_error_return(-2, "USB device unavailable"); | |
2651 | ||
22d12cda TJ |
2652 | for (i = 0; i < ftdi->eeprom_size/2; i++) |
2653 | { | |
a5e1bd8c | 2654 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) |
c3d95b87 | 2655 | ftdi_error_return(-1, "reading eeprom failed"); |
a3da1d95 GE |
2656 | } |
2657 | ||
2658 | return 0; | |
2659 | } | |
2660 | ||
cb6250fa TJ |
2661 | /* |
2662 | ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID | |
2663 | Function is only used internally | |
2664 | \internal | |
2665 | */ | |
2666 | static unsigned char ftdi_read_chipid_shift(unsigned char value) | |
2667 | { | |
2668 | return ((value & 1) << 1) | | |
22d12cda TJ |
2669 | ((value & 2) << 5) | |
2670 | ((value & 4) >> 2) | | |
2671 | ((value & 8) << 4) | | |
2672 | ((value & 16) >> 1) | | |
2673 | ((value & 32) >> 1) | | |
2674 | ((value & 64) >> 4) | | |
2675 | ((value & 128) >> 2); | |
cb6250fa TJ |
2676 | } |
2677 | ||
2678 | /** | |
2679 | Read the FTDIChip-ID from R-type devices | |
2680 | ||
2681 | \param ftdi pointer to ftdi_context | |
2682 | \param chipid Pointer to store FTDIChip-ID | |
2683 | ||
2684 | \retval 0: all fine | |
2685 | \retval -1: read failed | |
8970ed7e | 2686 | \retval -2: USB device unavailable |
cb6250fa TJ |
2687 | */ |
2688 | int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid) | |
2689 | { | |
c7eb3112 | 2690 | unsigned int a = 0, b = 0; |
cb6250fa | 2691 | |
8970ed7e TJ |
2692 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2693 | ftdi_error_return(-2, "USB device unavailable"); | |
2694 | ||
a5e1bd8c | 2695 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x43, (char *)&a, 2, ftdi->usb_read_timeout) == 2) |
cb6250fa TJ |
2696 | { |
2697 | a = a << 8 | a >> 8; | |
a5e1bd8c | 2698 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x44, (char *)&b, 2, ftdi->usb_read_timeout) == 2) |
cb6250fa TJ |
2699 | { |
2700 | b = b << 8 | b >> 8; | |
5230676f | 2701 | a = (a << 16) | (b & 0xFFFF); |
912d50ca TJ |
2702 | a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8 |
2703 | | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24; | |
cb6250fa | 2704 | *chipid = a ^ 0xa5f0f7d1; |
c7eb3112 | 2705 | return 0; |
cb6250fa TJ |
2706 | } |
2707 | } | |
2708 | ||
c7eb3112 | 2709 | ftdi_error_return(-1, "read of FTDIChip-ID failed"); |
cb6250fa TJ |
2710 | } |
2711 | ||
1941414d | 2712 | /** |
8970ed7e TJ |
2713 | Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom |
2714 | Call this function then do a write then call again to see if size changes, if so write again. | |
c201f80f | 2715 | |
8970ed7e TJ |
2716 | \param ftdi pointer to ftdi_context |
2717 | \param eeprom Pointer to store eeprom into | |
2718 | \param maxsize the size of the buffer to read into | |
c201f80f | 2719 | |
8970ed7e TJ |
2720 | \retval -1: eeprom read failed |
2721 | \retval -2: USB device unavailable | |
2722 | \retval >=0: size of eeprom | |
c201f80f TJ |
2723 | */ |
2724 | int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize) | |
2725 | { | |
2726 | int i=0,j,minsize=32; | |
2727 | int size=minsize; | |
2728 | ||
8970ed7e TJ |
2729 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2730 | ftdi_error_return(-2, "USB device unavailable"); | |
2731 | ||
22d12cda TJ |
2732 | do |
2733 | { | |
2734 | for (j = 0; i < maxsize/2 && j<size; j++) | |
2735 | { | |
2736 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, | |
2737 | SIO_READ_EEPROM_REQUEST, 0, i, | |
2738 | eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) | |
8970ed7e | 2739 | ftdi_error_return(-1, "eeprom read failed"); |
22d12cda TJ |
2740 | i++; |
2741 | } | |
2742 | size*=2; | |
2743 | } | |
2744 | while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0); | |
c201f80f TJ |
2745 | |
2746 | return size/2; | |
2747 | } | |
2748 | ||
2749 | /** | |
c1c70e13 OS |
2750 | Write eeprom location |
2751 | ||
2752 | \param ftdi pointer to ftdi_context | |
2753 | \param eeprom_addr Address of eeprom location to be written | |
2754 | \param eeprom_val Value to be written | |
2755 | ||
2756 | \retval 0: all fine | |
2757 | \retval -1: read failed | |
8970ed7e | 2758 | \retval -2: USB device unavailable |
c1c70e13 OS |
2759 | */ |
2760 | int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val) | |
2761 | { | |
8970ed7e TJ |
2762 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2763 | ftdi_error_return(-2, "USB device unavailable"); | |
2764 | ||
c1c70e13 OS |
2765 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
2766 | SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr, | |
2767 | NULL, 0, ftdi->usb_write_timeout) != 0) | |
2768 | ftdi_error_return(-1, "unable to write eeprom"); | |
2769 | ||
2770 | return 0; | |
2771 | } | |
2772 | ||
2773 | /** | |
1941414d | 2774 | Write eeprom |
a3da1d95 | 2775 | |
1941414d TJ |
2776 | \param ftdi pointer to ftdi_context |
2777 | \param eeprom Pointer to read eeprom from | |
2778 | ||
2779 | \retval 0: all fine | |
2780 | \retval -1: read failed | |
8970ed7e | 2781 | \retval -2: USB device unavailable |
1941414d | 2782 | */ |
a8f46ddc TJ |
2783 | int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) |
2784 | { | |
ba5329be | 2785 | unsigned short usb_val, status; |
e30da501 | 2786 | int i, ret; |
a3da1d95 | 2787 | |
8970ed7e TJ |
2788 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2789 | ftdi_error_return(-2, "USB device unavailable"); | |
2790 | ||
ba5329be | 2791 | /* These commands were traced while running MProg */ |
e30da501 TJ |
2792 | if ((ret = ftdi_usb_reset(ftdi)) != 0) |
2793 | return ret; | |
2794 | if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0) | |
2795 | return ret; | |
2796 | if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0) | |
2797 | return ret; | |
ba5329be | 2798 | |
22d12cda TJ |
2799 | for (i = 0; i < ftdi->eeprom_size/2; i++) |
2800 | { | |
d9f0cce7 TJ |
2801 | usb_val = eeprom[i*2]; |
2802 | usb_val += eeprom[(i*2)+1] << 8; | |
a5e1bd8c | 2803 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
22d12cda | 2804 | SIO_WRITE_EEPROM_REQUEST, usb_val, i, |
a5e1bd8c | 2805 | NULL, 0, ftdi->usb_write_timeout) != 0) |
c3d95b87 | 2806 | ftdi_error_return(-1, "unable to write eeprom"); |
a3da1d95 GE |
2807 | } |
2808 | ||
2809 | return 0; | |
2810 | } | |
2811 | ||
1941414d TJ |
2812 | /** |
2813 | Erase eeprom | |
a3da1d95 | 2814 | |
a5e1bd8c MK |
2815 | This is not supported on FT232R/FT245R according to the MProg manual from FTDI. |
2816 | ||
1941414d TJ |
2817 | \param ftdi pointer to ftdi_context |
2818 | ||
2819 | \retval 0: all fine | |
2820 | \retval -1: erase failed | |
8970ed7e | 2821 | \retval -2: USB device unavailable |
1941414d | 2822 | */ |
a8f46ddc TJ |
2823 | int ftdi_erase_eeprom(struct ftdi_context *ftdi) |
2824 | { | |
8970ed7e TJ |
2825 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2826 | ftdi_error_return(-2, "USB device unavailable"); | |
2827 | ||
a5e1bd8c | 2828 | if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) |
c3d95b87 | 2829 | ftdi_error_return(-1, "unable to erase eeprom"); |
a3da1d95 GE |
2830 | |
2831 | return 0; | |
2832 | } | |
c3d95b87 | 2833 | |
1941414d TJ |
2834 | /** |
2835 | Get string representation for last error code | |
c3d95b87 | 2836 | |
1941414d TJ |
2837 | \param ftdi pointer to ftdi_context |
2838 | ||
2839 | \retval Pointer to error string | |
2840 | */ | |
c3d95b87 TJ |
2841 | char *ftdi_get_error_string (struct ftdi_context *ftdi) |
2842 | { | |
8970ed7e TJ |
2843 | if (ftdi == NULL) |
2844 | return ""; | |
2845 | ||
c3d95b87 TJ |
2846 | return ftdi->error_str; |
2847 | } | |
a01d31e2 | 2848 | |
b5ec1820 | 2849 | /* @} end of doxygen libftdi group */ |