Commit | Line | Data |
---|---|---|
a3da1d95 GE |
1 | /*************************************************************************** |
2 | ftdi.c - description | |
3 | ------------------- | |
4 | begin : Fri Apr 4 2003 | |
79646368 | 5 | copyright : (C) 2003-2017 by Intra2net AG and the libftdi developers |
5fdb1cb1 | 6 | email : opensource@intra2net.com |
5b110dec | 7 | SPDX-License-Identifier: LGPL-2.1-only |
a3da1d95 GE |
8 | ***************************************************************************/ |
9 | ||
10 | /*************************************************************************** | |
11 | * * | |
12 | * This program is free software; you can redistribute it and/or modify * | |
13 | * it under the terms of the GNU Lesser General Public License * | |
14 | * version 2.1 as published by the Free Software Foundation; * | |
15 | * * | |
16 | ***************************************************************************/ | |
d9f0cce7 | 17 | |
b5ec1820 TJ |
18 | /** |
19 | \mainpage libftdi API documentation | |
20 | ||
ad397a4b | 21 | Library to talk to FTDI chips. You find the latest versions of libftdi at |
79646368 | 22 | https://www.intra2net.com/en/developer/libftdi/ |
b5ec1820 | 23 | |
ad397a4b TJ |
24 | The library is easy to use. Have a look at this short example: |
25 | \include simple.c | |
26 | ||
27 | More examples can be found in the "examples" directory. | |
b5ec1820 TJ |
28 | */ |
29 | /** \addtogroup libftdi */ | |
30 | /* @{ */ | |
31 | ||
579b006f | 32 | #include <libusb.h> |
a8f46ddc | 33 | #include <string.h> |
d2f10023 | 34 | #include <errno.h> |
b56d5a64 | 35 | #include <stdio.h> |
579b006f | 36 | #include <stdlib.h> |
0e302db6 | 37 | |
b790d38e | 38 | #include "ftdi_i.h" |
ed46f09c ES |
39 | /* Prevent deprecated messages when building library */ |
40 | #define _FTDI_DISABLE_DEPRECATED | |
98452d97 | 41 | #include "ftdi.h" |
0220adfa | 42 | #include "ftdi_version_i.h" |
a3da1d95 | 43 | |
21abaf2e | 44 | #define ftdi_error_return(code, str) do { \ |
b0a50459 PS |
45 | if ( ftdi ) \ |
46 | ftdi->error_str = str; \ | |
47 | else \ | |
48 | fprintf(stderr, str); \ | |
21abaf2e | 49 | return code; \ |
d2f10023 | 50 | } while(0); |
c3d95b87 | 51 | |
99650502 UB |
52 | #define ftdi_error_return_free_device_list(code, str, devs) do { \ |
53 | libusb_free_device_list(devs,1); \ | |
54 | ftdi->error_str = str; \ | |
55 | return code; \ | |
56 | } while(0); | |
57 | ||
418aaa72 | 58 | |
f3f81007 TJ |
59 | /** |
60 | Internal function to close usb device pointer. | |
61 | Sets ftdi->usb_dev to NULL. | |
62 | \internal | |
63 | ||
64 | \param ftdi pointer to ftdi_context | |
65 | ||
579b006f | 66 | \retval none |
f3f81007 | 67 | */ |
579b006f | 68 | static void ftdi_usb_close_internal (struct ftdi_context *ftdi) |
dff4fdb0 | 69 | { |
22a1b5c1 | 70 | if (ftdi && ftdi->usb_dev) |
dff4fdb0 | 71 | { |
56ac0383 TJ |
72 | libusb_close (ftdi->usb_dev); |
73 | ftdi->usb_dev = NULL; | |
44f41f11 UB |
74 | if(ftdi->eeprom) |
75 | ftdi->eeprom->initialized_for_connected_device = 0; | |
dff4fdb0 | 76 | } |
dff4fdb0 | 77 | } |
c3d95b87 | 78 | |
1941414d TJ |
79 | /** |
80 | Initializes a ftdi_context. | |
4837f98a | 81 | |
1941414d | 82 | \param ftdi pointer to ftdi_context |
4837f98a | 83 | |
1941414d TJ |
84 | \retval 0: all fine |
85 | \retval -1: couldn't allocate read buffer | |
a35aa9bd | 86 | \retval -2: couldn't allocate struct buffer |
3a284749 | 87 | \retval -3: libusb_init() failed |
1941414d TJ |
88 | |
89 | \remark This should be called before all functions | |
948f9ada | 90 | */ |
a8f46ddc TJ |
91 | int ftdi_init(struct ftdi_context *ftdi) |
92 | { | |
3b3a9614 | 93 | struct ftdi_eeprom* eeprom; |
02212d8e | 94 | ftdi->usb_ctx = NULL; |
98452d97 | 95 | ftdi->usb_dev = NULL; |
545820ce TJ |
96 | ftdi->usb_read_timeout = 5000; |
97 | ftdi->usb_write_timeout = 5000; | |
a3da1d95 | 98 | |
53ad271d | 99 | ftdi->type = TYPE_BM; /* chip type */ |
a3da1d95 | 100 | ftdi->baudrate = -1; |
418aaa72 | 101 | ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */ |
a3da1d95 | 102 | |
948f9ada TJ |
103 | ftdi->readbuffer = NULL; |
104 | ftdi->readbuffer_offset = 0; | |
105 | ftdi->readbuffer_remaining = 0; | |
106 | ftdi->writebuffer_chunksize = 4096; | |
e2f12a4f | 107 | ftdi->max_packet_size = 0; |
3a284749 TJ |
108 | ftdi->error_str = NULL; |
109 | ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE; | |
110 | ||
111 | if (libusb_init(&ftdi->usb_ctx) < 0) | |
112 | ftdi_error_return(-3, "libusb_init() failed"); | |
948f9ada | 113 | |
ac0af8ec | 114 | ftdi_set_interface(ftdi, INTERFACE_ANY); |
418aaa72 | 115 | ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */ |
53ad271d | 116 | |
3b3a9614 | 117 | eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom)); |
a35aa9bd UB |
118 | if (eeprom == 0) |
119 | ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom"); | |
b4d19dea | 120 | memset(eeprom, 0, sizeof(struct ftdi_eeprom)); |
a35aa9bd | 121 | ftdi->eeprom = eeprom; |
c201f80f | 122 | |
1c733d33 TJ |
123 | /* All fine. Now allocate the readbuffer */ |
124 | return ftdi_read_data_set_chunksize(ftdi, 4096); | |
948f9ada | 125 | } |
4837f98a | 126 | |
1941414d | 127 | /** |
cef378aa TJ |
128 | Allocate and initialize a new ftdi_context |
129 | ||
130 | \return a pointer to a new ftdi_context, or NULL on failure | |
131 | */ | |
672ac008 | 132 | struct ftdi_context *ftdi_new(void) |
cef378aa TJ |
133 | { |
134 | struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context)); | |
135 | ||
22d12cda TJ |
136 | if (ftdi == NULL) |
137 | { | |
cef378aa TJ |
138 | return NULL; |
139 | } | |
140 | ||
22d12cda TJ |
141 | if (ftdi_init(ftdi) != 0) |
142 | { | |
cef378aa | 143 | free(ftdi); |
cdf448f6 | 144 | return NULL; |
cef378aa TJ |
145 | } |
146 | ||
147 | return ftdi; | |
148 | } | |
149 | ||
150 | /** | |
1941414d TJ |
151 | Open selected channels on a chip, otherwise use first channel. |
152 | ||
153 | \param ftdi pointer to ftdi_context | |
f9d69895 | 154 | \param interface Interface to use for FT2232C/2232H/4232H chips. |
1941414d TJ |
155 | |
156 | \retval 0: all fine | |
157 | \retval -1: unknown interface | |
22a1b5c1 | 158 | \retval -2: USB device unavailable |
1c5fa36b | 159 | \retval -3: Device already open, interface can't be set in that state |
c4446c36 | 160 | */ |
0ce2f5fa | 161 | int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface) |
c4446c36 | 162 | { |
1971c26d | 163 | if (ftdi == NULL) |
22a1b5c1 TJ |
164 | ftdi_error_return(-2, "USB device unavailable"); |
165 | ||
1c5fa36b TJ |
166 | if (ftdi->usb_dev != NULL) |
167 | { | |
168 | int check_interface = interface; | |
169 | if (check_interface == INTERFACE_ANY) | |
170 | check_interface = INTERFACE_A; | |
171 | ||
172 | if (ftdi->index != check_interface) | |
173 | ftdi_error_return(-3, "Interface can not be changed on an already open device"); | |
174 | } | |
175 | ||
22d12cda TJ |
176 | switch (interface) |
177 | { | |
178 | case INTERFACE_ANY: | |
179 | case INTERFACE_A: | |
ac0af8ec VY |
180 | ftdi->interface = 0; |
181 | ftdi->index = INTERFACE_A; | |
182 | ftdi->in_ep = 0x02; | |
183 | ftdi->out_ep = 0x81; | |
22d12cda TJ |
184 | break; |
185 | case INTERFACE_B: | |
186 | ftdi->interface = 1; | |
187 | ftdi->index = INTERFACE_B; | |
188 | ftdi->in_ep = 0x04; | |
189 | ftdi->out_ep = 0x83; | |
190 | break; | |
f9d69895 AH |
191 | case INTERFACE_C: |
192 | ftdi->interface = 2; | |
193 | ftdi->index = INTERFACE_C; | |
194 | ftdi->in_ep = 0x06; | |
195 | ftdi->out_ep = 0x85; | |
196 | break; | |
197 | case INTERFACE_D: | |
198 | ftdi->interface = 3; | |
199 | ftdi->index = INTERFACE_D; | |
200 | ftdi->in_ep = 0x08; | |
201 | ftdi->out_ep = 0x87; | |
202 | break; | |
22d12cda TJ |
203 | default: |
204 | ftdi_error_return(-1, "Unknown interface"); | |
c4446c36 TJ |
205 | } |
206 | return 0; | |
207 | } | |
948f9ada | 208 | |
1941414d TJ |
209 | /** |
210 | Deinitializes a ftdi_context. | |
4837f98a | 211 | |
1941414d | 212 | \param ftdi pointer to ftdi_context |
4837f98a | 213 | */ |
a8f46ddc TJ |
214 | void ftdi_deinit(struct ftdi_context *ftdi) |
215 | { | |
22a1b5c1 TJ |
216 | if (ftdi == NULL) |
217 | return; | |
218 | ||
f3f81007 | 219 | ftdi_usb_close_internal (ftdi); |
dff4fdb0 | 220 | |
22d12cda TJ |
221 | if (ftdi->readbuffer != NULL) |
222 | { | |
d9f0cce7 TJ |
223 | free(ftdi->readbuffer); |
224 | ftdi->readbuffer = NULL; | |
948f9ada | 225 | } |
a35aa9bd UB |
226 | |
227 | if (ftdi->eeprom != NULL) | |
228 | { | |
74e8e79d UB |
229 | if (ftdi->eeprom->manufacturer != 0) |
230 | { | |
231 | free(ftdi->eeprom->manufacturer); | |
232 | ftdi->eeprom->manufacturer = 0; | |
233 | } | |
234 | if (ftdi->eeprom->product != 0) | |
235 | { | |
236 | free(ftdi->eeprom->product); | |
237 | ftdi->eeprom->product = 0; | |
238 | } | |
239 | if (ftdi->eeprom->serial != 0) | |
240 | { | |
241 | free(ftdi->eeprom->serial); | |
242 | ftdi->eeprom->serial = 0; | |
243 | } | |
a35aa9bd UB |
244 | free(ftdi->eeprom); |
245 | ftdi->eeprom = NULL; | |
246 | } | |
3a284749 TJ |
247 | |
248 | if (ftdi->usb_ctx) | |
249 | { | |
250 | libusb_exit(ftdi->usb_ctx); | |
251 | ftdi->usb_ctx = NULL; | |
252 | } | |
a3da1d95 GE |
253 | } |
254 | ||
1941414d | 255 | /** |
cef378aa TJ |
256 | Deinitialize and free an ftdi_context. |
257 | ||
258 | \param ftdi pointer to ftdi_context | |
259 | */ | |
260 | void ftdi_free(struct ftdi_context *ftdi) | |
261 | { | |
262 | ftdi_deinit(ftdi); | |
263 | free(ftdi); | |
264 | } | |
265 | ||
266 | /** | |
1941414d TJ |
267 | Use an already open libusb device. |
268 | ||
269 | \param ftdi pointer to ftdi_context | |
579b006f | 270 | \param usb libusb libusb_device_handle to use |
4837f98a | 271 | */ |
579b006f | 272 | void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb) |
a8f46ddc | 273 | { |
22a1b5c1 TJ |
274 | if (ftdi == NULL) |
275 | return; | |
276 | ||
98452d97 TJ |
277 | ftdi->usb_dev = usb; |
278 | } | |
279 | ||
0220adfa TJ |
280 | /** |
281 | * @brief Get libftdi library version | |
282 | * | |
283 | * @return ftdi_version_info Library version information | |
284 | **/ | |
bd6941fd | 285 | struct ftdi_version_info ftdi_get_library_version(void) |
0220adfa TJ |
286 | { |
287 | struct ftdi_version_info ver; | |
288 | ||
289 | ver.major = FTDI_MAJOR_VERSION; | |
290 | ver.minor = FTDI_MINOR_VERSION; | |
291 | ver.micro = FTDI_MICRO_VERSION; | |
292 | ver.version_str = FTDI_VERSION_STRING; | |
293 | ver.snapshot_str = FTDI_SNAPSHOT_VERSION; | |
294 | ||
295 | return ver; | |
296 | } | |
98452d97 | 297 | |
1941414d | 298 | /** |
7879216a UB |
299 | Finds all ftdi devices with given VID:PID on the usb bus. Creates a new |
300 | ftdi_device_list which needs to be deallocated by ftdi_list_free() after | |
301 | use. With VID:PID 0:0, search for the default devices | |
809d711d | 302 | (0x403:0x6001, 0x403:0x6010, 0x403:0x6011, 0x403:0x6014, 0x403:0x6015) |
1941414d TJ |
303 | |
304 | \param ftdi pointer to ftdi_context | |
305 | \param devlist Pointer where to store list of found devices | |
306 | \param vendor Vendor ID to search for | |
307 | \param product Product ID to search for | |
edb82cbf | 308 | |
1941414d | 309 | \retval >0: number of devices found |
1941414d | 310 | \retval -3: out of memory |
579b006f JZ |
311 | \retval -5: libusb_get_device_list() failed |
312 | \retval -6: libusb_get_device_descriptor() failed | |
edb82cbf | 313 | */ |
d2f10023 | 314 | int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product) |
edb82cbf TJ |
315 | { |
316 | struct ftdi_device_list **curdev; | |
579b006f JZ |
317 | libusb_device *dev; |
318 | libusb_device **devs; | |
edb82cbf | 319 | int count = 0; |
579b006f JZ |
320 | int i = 0; |
321 | ||
02212d8e | 322 | if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0) |
579b006f | 323 | ftdi_error_return(-5, "libusb_get_device_list() failed"); |
edb82cbf TJ |
324 | |
325 | curdev = devlist; | |
6db32169 | 326 | *curdev = NULL; |
579b006f JZ |
327 | |
328 | while ((dev = devs[i++]) != NULL) | |
22d12cda | 329 | { |
579b006f | 330 | struct libusb_device_descriptor desc; |
d2f10023 | 331 | |
579b006f | 332 | if (libusb_get_device_descriptor(dev, &desc) < 0) |
77377af7 | 333 | ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs); |
edb82cbf | 334 | |
8de26dde | 335 | if (((vendor || product) && |
74387f27 | 336 | desc.idVendor == vendor && desc.idProduct == product) || |
8de26dde | 337 | (!(vendor || product) && |
74387f27 | 338 | (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010 |
809d711d TJ |
339 | || desc.idProduct == 0x6011 || desc.idProduct == 0x6014 |
340 | || desc.idProduct == 0x6015))) | |
579b006f JZ |
341 | { |
342 | *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list)); | |
343 | if (!*curdev) | |
77377af7 | 344 | ftdi_error_return_free_device_list(-3, "out of memory", devs); |
56ac0383 | 345 | |
579b006f JZ |
346 | (*curdev)->next = NULL; |
347 | (*curdev)->dev = dev; | |
0c33162c | 348 | libusb_ref_device(dev); |
579b006f JZ |
349 | curdev = &(*curdev)->next; |
350 | count++; | |
edb82cbf TJ |
351 | } |
352 | } | |
77377af7 | 353 | libusb_free_device_list(devs,1); |
edb82cbf TJ |
354 | return count; |
355 | } | |
356 | ||
1941414d TJ |
357 | /** |
358 | Frees a usb device list. | |
edb82cbf | 359 | |
1941414d | 360 | \param devlist USB device list created by ftdi_usb_find_all() |
edb82cbf | 361 | */ |
d2f10023 | 362 | void ftdi_list_free(struct ftdi_device_list **devlist) |
edb82cbf | 363 | { |
6db32169 TJ |
364 | struct ftdi_device_list *curdev, *next; |
365 | ||
22d12cda TJ |
366 | for (curdev = *devlist; curdev != NULL;) |
367 | { | |
6db32169 | 368 | next = curdev->next; |
0c33162c | 369 | libusb_unref_device(curdev->dev); |
6db32169 TJ |
370 | free(curdev); |
371 | curdev = next; | |
edb82cbf TJ |
372 | } |
373 | ||
6db32169 | 374 | *devlist = NULL; |
edb82cbf TJ |
375 | } |
376 | ||
1941414d | 377 | /** |
cef378aa TJ |
378 | Frees a usb device list. |
379 | ||
380 | \param devlist USB device list created by ftdi_usb_find_all() | |
381 | */ | |
382 | void ftdi_list_free2(struct ftdi_device_list *devlist) | |
383 | { | |
384 | ftdi_list_free(&devlist); | |
385 | } | |
386 | ||
387 | /** | |
474786c0 TJ |
388 | Return device ID strings from the usb device. |
389 | ||
390 | The parameters manufacturer, description and serial may be NULL | |
391 | or pointer to buffers to store the fetched strings. | |
392 | ||
898c34dd TJ |
393 | \note Use this function only in combination with ftdi_usb_find_all() |
394 | as it closes the internal "usb_dev" after use. | |
395 | ||
474786c0 TJ |
396 | \param ftdi pointer to ftdi_context |
397 | \param dev libusb usb_dev to use | |
398 | \param manufacturer Store manufacturer string here if not NULL | |
399 | \param mnf_len Buffer size of manufacturer string | |
400 | \param description Store product description string here if not NULL | |
401 | \param desc_len Buffer size of product description string | |
402 | \param serial Store serial string here if not NULL | |
403 | \param serial_len Buffer size of serial string | |
404 | ||
405 | \retval 0: all fine | |
406 | \retval -1: wrong arguments | |
407 | \retval -4: unable to open device | |
408 | \retval -7: get product manufacturer failed | |
409 | \retval -8: get product description failed | |
410 | \retval -9: get serial number failed | |
579b006f | 411 | \retval -11: libusb_get_device_descriptor() failed |
474786c0 | 412 | */ |
15079e78 FH |
413 | int ftdi_usb_get_strings(struct ftdi_context *ftdi, |
414 | struct libusb_device *dev, | |
415 | char *manufacturer, int mnf_len, | |
416 | char *description, int desc_len, | |
417 | char *serial, int serial_len) | |
474786c0 | 418 | { |
15079e78 | 419 | int ret; |
579b006f | 420 | |
474786c0 TJ |
421 | if ((ftdi==NULL) || (dev==NULL)) |
422 | return -1; | |
423 | ||
bc384123 | 424 | if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0) |
15079e78 FH |
425 | ftdi_error_return(-4, "libusb_open() failed"); |
426 | ||
427 | // ftdi->usb_dev will not be NULL when entering ftdi_usb_get_strings2(), so | |
428 | // it won't be closed either. This allows us to close it whether we actually | |
429 | // called libusb_open() up above or not. This matches the expected behavior | |
430 | // (and note) for ftdi_usb_get_strings(). | |
431 | ret = ftdi_usb_get_strings2(ftdi, dev, | |
432 | manufacturer, mnf_len, | |
433 | description, desc_len, | |
434 | serial, serial_len); | |
435 | ||
436 | // only close it if it was successful, as all other return codes close | |
437 | // before returning already. | |
438 | if (ret == 0) | |
439 | ftdi_usb_close_internal(ftdi); | |
440 | ||
441 | return ret; | |
442 | } | |
443 | ||
444 | /** | |
445 | Return device ID strings from the usb device. | |
446 | ||
447 | The parameters manufacturer, description and serial may be NULL | |
448 | or pointer to buffers to store the fetched strings. | |
449 | ||
450 | \note The old function ftdi_usb_get_strings() always closes the device. | |
451 | This version only closes the device if it was opened by it. | |
452 | ||
453 | \param ftdi pointer to ftdi_context | |
454 | \param dev libusb usb_dev to use | |
455 | \param manufacturer Store manufacturer string here if not NULL | |
456 | \param mnf_len Buffer size of manufacturer string | |
457 | \param description Store product description string here if not NULL | |
458 | \param desc_len Buffer size of product description string | |
459 | \param serial Store serial string here if not NULL | |
460 | \param serial_len Buffer size of serial string | |
461 | ||
462 | \retval 0: all fine | |
463 | \retval -1: wrong arguments | |
464 | \retval -4: unable to open device | |
465 | \retval -7: get product manufacturer failed | |
466 | \retval -8: get product description failed | |
467 | \retval -9: get serial number failed | |
468 | \retval -11: libusb_get_device_descriptor() failed | |
469 | */ | |
470 | int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev, | |
471 | char *manufacturer, int mnf_len, | |
472 | char *description, int desc_len, | |
473 | char *serial, int serial_len) | |
474 | { | |
475 | struct libusb_device_descriptor desc; | |
c45d2630 | 476 | char need_open; |
15079e78 FH |
477 | |
478 | if ((ftdi==NULL) || (dev==NULL)) | |
479 | return -1; | |
480 | ||
c45d2630 | 481 | need_open = (ftdi->usb_dev == NULL); |
15079e78 FH |
482 | if (need_open && libusb_open(dev, &ftdi->usb_dev) < 0) |
483 | ftdi_error_return(-4, "libusb_open() failed"); | |
579b006f JZ |
484 | |
485 | if (libusb_get_device_descriptor(dev, &desc) < 0) | |
486 | ftdi_error_return(-11, "libusb_get_device_descriptor() failed"); | |
474786c0 | 487 | |
22d12cda TJ |
488 | if (manufacturer != NULL) |
489 | { | |
579b006f | 490 | if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0) |
22d12cda | 491 | { |
f3f81007 | 492 | ftdi_usb_close_internal (ftdi); |
579b006f | 493 | ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed"); |
474786c0 TJ |
494 | } |
495 | } | |
496 | ||
22d12cda TJ |
497 | if (description != NULL) |
498 | { | |
579b006f | 499 | if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0) |
22d12cda | 500 | { |
f3f81007 | 501 | ftdi_usb_close_internal (ftdi); |
579b006f | 502 | ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed"); |
474786c0 TJ |
503 | } |
504 | } | |
505 | ||
22d12cda TJ |
506 | if (serial != NULL) |
507 | { | |
579b006f | 508 | if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0) |
22d12cda | 509 | { |
f3f81007 | 510 | ftdi_usb_close_internal (ftdi); |
579b006f | 511 | ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed"); |
474786c0 TJ |
512 | } |
513 | } | |
514 | ||
15079e78 FH |
515 | if (need_open) |
516 | ftdi_usb_close_internal (ftdi); | |
474786c0 TJ |
517 | |
518 | return 0; | |
519 | } | |
520 | ||
521 | /** | |
e2f12a4f TJ |
522 | * Internal function to determine the maximum packet size. |
523 | * \param ftdi pointer to ftdi_context | |
524 | * \param dev libusb usb_dev to use | |
525 | * \retval Maximum packet size for this device | |
526 | */ | |
579b006f | 527 | static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev) |
e2f12a4f | 528 | { |
579b006f JZ |
529 | struct libusb_device_descriptor desc; |
530 | struct libusb_config_descriptor *config0; | |
e2f12a4f TJ |
531 | unsigned int packet_size; |
532 | ||
22a1b5c1 TJ |
533 | // Sanity check |
534 | if (ftdi == NULL || dev == NULL) | |
535 | return 64; | |
536 | ||
e2f12a4f TJ |
537 | // Determine maximum packet size. Init with default value. |
538 | // New hi-speed devices from FTDI use a packet size of 512 bytes | |
539 | // but could be connected to a normal speed USB hub -> 64 bytes packet size. | |
6ae693b2 | 540 | if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H) |
e2f12a4f TJ |
541 | packet_size = 512; |
542 | else | |
543 | packet_size = 64; | |
544 | ||
579b006f JZ |
545 | if (libusb_get_device_descriptor(dev, &desc) < 0) |
546 | return packet_size; | |
547 | ||
548 | if (libusb_get_config_descriptor(dev, 0, &config0) < 0) | |
549 | return packet_size; | |
e2f12a4f | 550 | |
579b006f JZ |
551 | if (desc.bNumConfigurations > 0) |
552 | { | |
553 | if (ftdi->interface < config0->bNumInterfaces) | |
e2f12a4f | 554 | { |
579b006f | 555 | struct libusb_interface interface = config0->interface[ftdi->interface]; |
e2f12a4f TJ |
556 | if (interface.num_altsetting > 0) |
557 | { | |
579b006f | 558 | struct libusb_interface_descriptor descriptor = interface.altsetting[0]; |
e2f12a4f TJ |
559 | if (descriptor.bNumEndpoints > 0) |
560 | { | |
561 | packet_size = descriptor.endpoint[0].wMaxPacketSize; | |
562 | } | |
563 | } | |
564 | } | |
565 | } | |
566 | ||
579b006f | 567 | libusb_free_config_descriptor (config0); |
e2f12a4f TJ |
568 | return packet_size; |
569 | } | |
570 | ||
571 | /** | |
418aaa72 | 572 | Opens a ftdi device given by an usb_device. |
7b18bef6 | 573 | |
1941414d TJ |
574 | \param ftdi pointer to ftdi_context |
575 | \param dev libusb usb_dev to use | |
576 | ||
577 | \retval 0: all fine | |
23b1798d | 578 | \retval -3: unable to config device |
1941414d TJ |
579 | \retval -4: unable to open device |
580 | \retval -5: unable to claim device | |
581 | \retval -6: reset failed | |
582 | \retval -7: set baudrate failed | |
22a1b5c1 | 583 | \retval -8: ftdi context invalid |
579b006f JZ |
584 | \retval -9: libusb_get_device_descriptor() failed |
585 | \retval -10: libusb_get_config_descriptor() failed | |
e375e6cb | 586 | \retval -11: libusb_detach_kernel_driver() failed |
579b006f | 587 | \retval -12: libusb_get_configuration() failed |
7b18bef6 | 588 | */ |
579b006f | 589 | int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev) |
7b18bef6 | 590 | { |
579b006f JZ |
591 | struct libusb_device_descriptor desc; |
592 | struct libusb_config_descriptor *config0; | |
43aee24f | 593 | int cfg, cfg0, detach_errno = 0; |
579b006f | 594 | |
22a1b5c1 TJ |
595 | if (ftdi == NULL) |
596 | ftdi_error_return(-8, "ftdi context invalid"); | |
597 | ||
579b006f JZ |
598 | if (libusb_open(dev, &ftdi->usb_dev) < 0) |
599 | ftdi_error_return(-4, "libusb_open() failed"); | |
600 | ||
601 | if (libusb_get_device_descriptor(dev, &desc) < 0) | |
602 | ftdi_error_return(-9, "libusb_get_device_descriptor() failed"); | |
603 | ||
604 | if (libusb_get_config_descriptor(dev, 0, &config0) < 0) | |
605 | ftdi_error_return(-10, "libusb_get_config_descriptor() failed"); | |
606 | cfg0 = config0->bConfigurationValue; | |
607 | libusb_free_config_descriptor (config0); | |
d2f10023 | 608 | |
22592e17 | 609 | // Try to detach ftdi_sio kernel module. |
22592e17 TJ |
610 | // |
611 | // The return code is kept in a separate variable and only parsed | |
612 | // if usb_set_configuration() or usb_claim_interface() fails as the | |
613 | // detach operation might be denied and everything still works fine. | |
614 | // Likely scenario is a static ftdi_sio kernel module. | |
a3d86bdb TJ |
615 | if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE) |
616 | { | |
617 | if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0) | |
618 | detach_errno = errno; | |
619 | } | |
5bf1c1e3 RM |
620 | else if (ftdi->module_detach_mode == AUTO_DETACH_REATACH_SIO_MODULE) |
621 | { | |
622 | if (libusb_set_auto_detach_kernel_driver(ftdi->usb_dev, 1) != LIBUSB_SUCCESS) | |
623 | detach_errno = errno; | |
624 | } | |
d2f10023 | 625 | |
579b006f JZ |
626 | if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0) |
627 | ftdi_error_return(-12, "libusb_get_configuration () failed"); | |
b57aedfd GE |
628 | // set configuration (needed especially for windows) |
629 | // tolerate EBUSY: one device with one configuration, but two interfaces | |
630 | // and libftdi sessions to both interfaces (e.g. FT2232) | |
579b006f | 631 | if (desc.bNumConfigurations > 0 && cfg != cfg0) |
b57aedfd | 632 | { |
579b006f | 633 | if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0) |
22d12cda | 634 | { |
a56ba2bd | 635 | ftdi_usb_close_internal (ftdi); |
56ac0383 | 636 | if (detach_errno == EPERM) |
43aee24f UB |
637 | { |
638 | ftdi_error_return(-8, "inappropriate permissions on device!"); | |
639 | } | |
640 | else | |
641 | { | |
c16b162d | 642 | ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use"); |
43aee24f | 643 | } |
23b1798d TJ |
644 | } |
645 | } | |
646 | ||
579b006f | 647 | if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0) |
22d12cda | 648 | { |
f3f81007 | 649 | ftdi_usb_close_internal (ftdi); |
56ac0383 | 650 | if (detach_errno == EPERM) |
43aee24f UB |
651 | { |
652 | ftdi_error_return(-8, "inappropriate permissions on device!"); | |
653 | } | |
654 | else | |
655 | { | |
c16b162d | 656 | ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use"); |
43aee24f | 657 | } |
7b18bef6 TJ |
658 | } |
659 | ||
22d12cda TJ |
660 | if (ftdi_usb_reset (ftdi) != 0) |
661 | { | |
f3f81007 | 662 | ftdi_usb_close_internal (ftdi); |
7b18bef6 TJ |
663 | ftdi_error_return(-6, "ftdi_usb_reset failed"); |
664 | } | |
665 | ||
7b18bef6 TJ |
666 | // Try to guess chip type |
667 | // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0 | |
579b006f | 668 | if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200 |
56ac0383 | 669 | && desc.iSerialNumber == 0)) |
7b18bef6 | 670 | ftdi->type = TYPE_BM; |
579b006f | 671 | else if (desc.bcdDevice == 0x200) |
7b18bef6 | 672 | ftdi->type = TYPE_AM; |
579b006f | 673 | else if (desc.bcdDevice == 0x500) |
7b18bef6 | 674 | ftdi->type = TYPE_2232C; |
579b006f | 675 | else if (desc.bcdDevice == 0x600) |
cb6250fa | 676 | ftdi->type = TYPE_R; |
579b006f | 677 | else if (desc.bcdDevice == 0x700) |
0beb9686 | 678 | ftdi->type = TYPE_2232H; |
579b006f | 679 | else if (desc.bcdDevice == 0x800) |
0beb9686 | 680 | ftdi->type = TYPE_4232H; |
c7e4c09e UB |
681 | else if (desc.bcdDevice == 0x900) |
682 | ftdi->type = TYPE_232H; | |
2f80efc2 NP |
683 | else if (desc.bcdDevice == 0x1000) |
684 | ftdi->type = TYPE_230X; | |
7b18bef6 | 685 | |
e2f12a4f TJ |
686 | // Determine maximum packet size |
687 | ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev); | |
688 | ||
ef6f4838 TE |
689 | if (ftdi_set_baudrate (ftdi, 9600) != 0) |
690 | { | |
691 | ftdi_usb_close_internal (ftdi); | |
692 | ftdi_error_return(-7, "set baudrate failed"); | |
693 | } | |
694 | ||
7b18bef6 TJ |
695 | ftdi_error_return(0, "all fine"); |
696 | } | |
697 | ||
1941414d TJ |
698 | /** |
699 | Opens the first device with a given vendor and product ids. | |
700 | ||
701 | \param ftdi pointer to ftdi_context | |
702 | \param vendor Vendor ID | |
703 | \param product Product ID | |
704 | ||
9bec2387 | 705 | \retval same as ftdi_usb_open_desc() |
1941414d | 706 | */ |
edb82cbf TJ |
707 | int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) |
708 | { | |
709 | return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL); | |
710 | } | |
711 | ||
1941414d TJ |
712 | /** |
713 | Opens the first device with a given, vendor id, product id, | |
714 | description and serial. | |
715 | ||
716 | \param ftdi pointer to ftdi_context | |
717 | \param vendor Vendor ID | |
718 | \param product Product ID | |
719 | \param description Description to search for. Use NULL if not needed. | |
720 | \param serial Serial to search for. Use NULL if not needed. | |
721 | ||
722 | \retval 0: all fine | |
1941414d TJ |
723 | \retval -3: usb device not found |
724 | \retval -4: unable to open device | |
725 | \retval -5: unable to claim device | |
726 | \retval -6: reset failed | |
727 | \retval -7: set baudrate failed | |
728 | \retval -8: get product description failed | |
729 | \retval -9: get serial number failed | |
579b006f JZ |
730 | \retval -12: libusb_get_device_list() failed |
731 | \retval -13: libusb_get_device_descriptor() failed | |
a3da1d95 | 732 | */ |
04e1ea0a | 733 | int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, |
a8f46ddc TJ |
734 | const char* description, const char* serial) |
735 | { | |
5ebbdab9 GE |
736 | return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0); |
737 | } | |
738 | ||
739 | /** | |
740 | Opens the index-th device with a given, vendor id, product id, | |
741 | description and serial. | |
742 | ||
743 | \param ftdi pointer to ftdi_context | |
744 | \param vendor Vendor ID | |
745 | \param product Product ID | |
746 | \param description Description to search for. Use NULL if not needed. | |
747 | \param serial Serial to search for. Use NULL if not needed. | |
748 | \param index Number of matching device to open if there are more than one, starts with 0. | |
749 | ||
750 | \retval 0: all fine | |
751 | \retval -1: usb_find_busses() failed | |
752 | \retval -2: usb_find_devices() failed | |
753 | \retval -3: usb device not found | |
754 | \retval -4: unable to open device | |
755 | \retval -5: unable to claim device | |
756 | \retval -6: reset failed | |
757 | \retval -7: set baudrate failed | |
758 | \retval -8: get product description failed | |
759 | \retval -9: get serial number failed | |
760 | \retval -10: unable to close device | |
22a1b5c1 | 761 | \retval -11: ftdi context invalid |
4fe1a3f0 | 762 | \retval -12: libusb_get_device_list() failed |
5ebbdab9 GE |
763 | */ |
764 | int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product, | |
56ac0383 | 765 | const char* description, const char* serial, unsigned int index) |
5ebbdab9 | 766 | { |
579b006f JZ |
767 | libusb_device *dev; |
768 | libusb_device **devs; | |
c3d95b87 | 769 | char string[256]; |
579b006f | 770 | int i = 0; |
98452d97 | 771 | |
22a1b5c1 TJ |
772 | if (ftdi == NULL) |
773 | ftdi_error_return(-11, "ftdi context invalid"); | |
774 | ||
02212d8e | 775 | if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0) |
99650502 UB |
776 | ftdi_error_return(-12, "libusb_get_device_list() failed"); |
777 | ||
579b006f | 778 | while ((dev = devs[i++]) != NULL) |
22d12cda | 779 | { |
579b006f | 780 | struct libusb_device_descriptor desc; |
99650502 | 781 | int res; |
579b006f JZ |
782 | |
783 | if (libusb_get_device_descriptor(dev, &desc) < 0) | |
99650502 | 784 | ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs); |
579b006f JZ |
785 | |
786 | if (desc.idVendor == vendor && desc.idProduct == product) | |
22d12cda | 787 | { |
579b006f | 788 | if (libusb_open(dev, &ftdi->usb_dev) < 0) |
99650502 | 789 | ftdi_error_return_free_device_list(-4, "usb_open() failed", devs); |
c3d95b87 | 790 | |
579b006f JZ |
791 | if (description != NULL) |
792 | { | |
793 | if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0) | |
22d12cda | 794 | { |
d4afae5f | 795 | ftdi_usb_close_internal (ftdi); |
99650502 | 796 | ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs); |
a8f46ddc | 797 | } |
579b006f | 798 | if (strncmp(string, description, sizeof(string)) != 0) |
22d12cda | 799 | { |
d4afae5f | 800 | ftdi_usb_close_internal (ftdi); |
579b006f | 801 | continue; |
a8f46ddc | 802 | } |
579b006f JZ |
803 | } |
804 | if (serial != NULL) | |
805 | { | |
806 | if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0) | |
807 | { | |
808 | ftdi_usb_close_internal (ftdi); | |
99650502 | 809 | ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs); |
579b006f JZ |
810 | } |
811 | if (strncmp(string, serial, sizeof(string)) != 0) | |
812 | { | |
813 | ftdi_usb_close_internal (ftdi); | |
814 | continue; | |
815 | } | |
816 | } | |
98452d97 | 817 | |
579b006f | 818 | ftdi_usb_close_internal (ftdi); |
d2f10023 | 819 | |
56ac0383 TJ |
820 | if (index > 0) |
821 | { | |
822 | index--; | |
823 | continue; | |
824 | } | |
5ebbdab9 | 825 | |
99650502 UB |
826 | res = ftdi_usb_open_dev(ftdi, dev); |
827 | libusb_free_device_list(devs,1); | |
814e69f5 MD |
828 | return res; |
829 | } | |
830 | } | |
831 | ||
832 | // device not found | |
833 | ftdi_error_return_free_device_list(-3, "device not found", devs); | |
834 | } | |
835 | ||
836 | /** | |
30ea3095 | 837 | Opens the device at a given USB bus and device address. |
814e69f5 MD |
838 | |
839 | \param ftdi pointer to ftdi_context | |
840 | \param bus Bus number | |
30ea3095 | 841 | \param addr Device address |
814e69f5 MD |
842 | |
843 | \retval 0: all fine | |
844 | \retval -1: usb_find_busses() failed | |
845 | \retval -2: usb_find_devices() failed | |
846 | \retval -3: usb device not found | |
847 | \retval -4: unable to open device | |
848 | \retval -5: unable to claim device | |
849 | \retval -6: reset failed | |
850 | \retval -7: set baudrate failed | |
851 | \retval -8: get product description failed | |
852 | \retval -9: get serial number failed | |
853 | \retval -10: unable to close device | |
854 | \retval -11: ftdi context invalid | |
4fe1a3f0 | 855 | \retval -12: libusb_get_device_list() failed |
814e69f5 | 856 | */ |
30ea3095 | 857 | int ftdi_usb_open_bus_addr(struct ftdi_context *ftdi, uint8_t bus, uint8_t addr) |
814e69f5 MD |
858 | { |
859 | libusb_device *dev; | |
860 | libusb_device **devs; | |
861 | int i = 0; | |
862 | ||
863 | if (ftdi == NULL) | |
864 | ftdi_error_return(-11, "ftdi context invalid"); | |
865 | ||
866 | if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0) | |
867 | ftdi_error_return(-12, "libusb_get_device_list() failed"); | |
868 | ||
869 | while ((dev = devs[i++]) != NULL) | |
870 | { | |
30ea3095 | 871 | if (libusb_get_bus_number(dev) == bus && libusb_get_device_address(dev) == addr) |
814e69f5 MD |
872 | { |
873 | int res; | |
874 | res = ftdi_usb_open_dev(ftdi, dev); | |
875 | libusb_free_device_list(devs,1); | |
99650502 | 876 | return res; |
98452d97 | 877 | } |
98452d97 | 878 | } |
a3da1d95 | 879 | |
98452d97 | 880 | // device not found |
99650502 | 881 | ftdi_error_return_free_device_list(-3, "device not found", devs); |
a3da1d95 GE |
882 | } |
883 | ||
1941414d | 884 | /** |
5ebbdab9 GE |
885 | Opens the ftdi-device described by a description-string. |
886 | Intended to be used for parsing a device-description given as commandline argument. | |
887 | ||
888 | \param ftdi pointer to ftdi_context | |
889 | \param description NULL-terminated description-string, using this format: | |
890 | \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/) | |
891 | \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") | |
892 | \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 | |
893 | \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string | |
894 | ||
895 | \note The description format may be extended in later versions. | |
896 | ||
897 | \retval 0: all fine | |
579b006f | 898 | \retval -2: libusb_get_device_list() failed |
5ebbdab9 GE |
899 | \retval -3: usb device not found |
900 | \retval -4: unable to open device | |
901 | \retval -5: unable to claim device | |
902 | \retval -6: reset failed | |
903 | \retval -7: set baudrate failed | |
904 | \retval -8: get product description failed | |
905 | \retval -9: get serial number failed | |
906 | \retval -10: unable to close device | |
907 | \retval -11: illegal description format | |
22a1b5c1 | 908 | \retval -12: ftdi context invalid |
5ebbdab9 GE |
909 | */ |
910 | int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description) | |
911 | { | |
22a1b5c1 TJ |
912 | if (ftdi == NULL) |
913 | ftdi_error_return(-12, "ftdi context invalid"); | |
914 | ||
5ebbdab9 GE |
915 | if (description[0] == 0 || description[1] != ':') |
916 | ftdi_error_return(-11, "illegal description format"); | |
917 | ||
918 | if (description[0] == 'd') | |
919 | { | |
579b006f JZ |
920 | libusb_device *dev; |
921 | libusb_device **devs; | |
56ac0383 TJ |
922 | unsigned int bus_number, device_address; |
923 | int i = 0; | |
579b006f | 924 | |
56ac0383 TJ |
925 | if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0) |
926 | ftdi_error_return(-2, "libusb_get_device_list() failed"); | |
5ebbdab9 | 927 | |
579b006f JZ |
928 | /* XXX: This doesn't handle symlinks/odd paths/etc... */ |
929 | if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2) | |
56ac0383 | 930 | ftdi_error_return_free_device_list(-11, "illegal description format", devs); |
5ebbdab9 | 931 | |
56ac0383 | 932 | while ((dev = devs[i++]) != NULL) |
5ebbdab9 | 933 | { |
99650502 | 934 | int ret; |
56ac0383 TJ |
935 | if (bus_number == libusb_get_bus_number (dev) |
936 | && device_address == libusb_get_device_address (dev)) | |
99650502 UB |
937 | { |
938 | ret = ftdi_usb_open_dev(ftdi, dev); | |
939 | libusb_free_device_list(devs,1); | |
940 | return ret; | |
941 | } | |
5ebbdab9 GE |
942 | } |
943 | ||
944 | // device not found | |
99650502 | 945 | ftdi_error_return_free_device_list(-3, "device not found", devs); |
5ebbdab9 GE |
946 | } |
947 | else if (description[0] == 'i' || description[0] == 's') | |
948 | { | |
949 | unsigned int vendor; | |
950 | unsigned int product; | |
951 | unsigned int index=0; | |
0e6cf62b | 952 | const char *serial=NULL; |
5ebbdab9 GE |
953 | const char *startp, *endp; |
954 | ||
955 | errno=0; | |
956 | startp=description+2; | |
957 | vendor=strtoul((char*)startp,(char**)&endp,0); | |
958 | if (*endp != ':' || endp == startp || errno != 0) | |
959 | ftdi_error_return(-11, "illegal description format"); | |
960 | ||
961 | startp=endp+1; | |
962 | product=strtoul((char*)startp,(char**)&endp,0); | |
963 | if (endp == startp || errno != 0) | |
964 | ftdi_error_return(-11, "illegal description format"); | |
965 | ||
966 | if (description[0] == 'i' && *endp != 0) | |
967 | { | |
968 | /* optional index field in i-mode */ | |
969 | if (*endp != ':') | |
970 | ftdi_error_return(-11, "illegal description format"); | |
971 | ||
972 | startp=endp+1; | |
973 | index=strtoul((char*)startp,(char**)&endp,0); | |
974 | if (*endp != 0 || endp == startp || errno != 0) | |
975 | ftdi_error_return(-11, "illegal description format"); | |
976 | } | |
977 | if (description[0] == 's') | |
978 | { | |
979 | if (*endp != ':') | |
980 | ftdi_error_return(-11, "illegal description format"); | |
981 | ||
982 | /* rest of the description is the serial */ | |
983 | serial=endp+1; | |
984 | } | |
985 | ||
986 | return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index); | |
987 | } | |
988 | else | |
989 | { | |
990 | ftdi_error_return(-11, "illegal description format"); | |
991 | } | |
992 | } | |
993 | ||
994 | /** | |
1941414d | 995 | Resets the ftdi device. |
a3da1d95 | 996 | |
1941414d TJ |
997 | \param ftdi pointer to ftdi_context |
998 | ||
999 | \retval 0: all fine | |
1000 | \retval -1: FTDI reset failed | |
22a1b5c1 | 1001 | \retval -2: USB device unavailable |
4837f98a | 1002 | */ |
edb82cbf | 1003 | int ftdi_usb_reset(struct ftdi_context *ftdi) |
a8f46ddc | 1004 | { |
22a1b5c1 TJ |
1005 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1006 | ftdi_error_return(-2, "USB device unavailable"); | |
1007 | ||
579b006f JZ |
1008 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
1009 | SIO_RESET_REQUEST, SIO_RESET_SIO, | |
1010 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) | |
22d12cda | 1011 | ftdi_error_return(-1,"FTDI reset failed"); |
c3d95b87 | 1012 | |
545820ce | 1013 | // Invalidate data in the readbuffer |
bfcee05b TJ |
1014 | ftdi->readbuffer_offset = 0; |
1015 | ftdi->readbuffer_remaining = 0; | |
1016 | ||
a3da1d95 GE |
1017 | return 0; |
1018 | } | |
1019 | ||
1941414d | 1020 | /** |
1189b11a | 1021 | Clears the read buffer on the chip and the internal read buffer. |
ed46f09c | 1022 | This is the correct behavior for an RX flush. |
1941414d TJ |
1023 | |
1024 | \param ftdi pointer to ftdi_context | |
4837f98a | 1025 | |
1941414d | 1026 | \retval 0: all fine |
1189b11a | 1027 | \retval -1: read buffer purge failed |
22a1b5c1 | 1028 | \retval -2: USB device unavailable |
4837f98a | 1029 | */ |
ed46f09c ES |
1030 | int ftdi_tciflush(struct ftdi_context *ftdi) |
1031 | { | |
1032 | if (ftdi == NULL || ftdi->usb_dev == NULL) | |
1033 | ftdi_error_return(-2, "USB device unavailable"); | |
1034 | ||
1035 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, | |
1036 | SIO_RESET_REQUEST, SIO_TCIFLUSH, | |
1037 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) | |
1038 | ftdi_error_return(-1, "FTDI purge of RX buffer failed"); | |
1039 | ||
1040 | // Invalidate data in the readbuffer | |
1041 | ftdi->readbuffer_offset = 0; | |
1042 | ftdi->readbuffer_remaining = 0; | |
1043 | ||
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | ||
1048 | /** | |
1049 | Clears the write buffer on the chip and the internal read buffer. | |
1050 | This is incorrect behavior for an RX flush. | |
1051 | ||
1052 | \param ftdi pointer to ftdi_context | |
1053 | ||
1054 | \retval 0: all fine | |
1055 | \retval -1: write buffer purge failed | |
1056 | \retval -2: USB device unavailable | |
1057 | ||
1058 | \deprecated Use \ref ftdi_tciflush(struct ftdi_context *ftdi) | |
1059 | */ | |
1189b11a | 1060 | int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi) |
a8f46ddc | 1061 | { |
22a1b5c1 TJ |
1062 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1063 | ftdi_error_return(-2, "USB device unavailable"); | |
1064 | ||
579b006f JZ |
1065 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
1066 | SIO_RESET_REQUEST, SIO_RESET_PURGE_RX, | |
1067 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) | |
c3d95b87 TJ |
1068 | ftdi_error_return(-1, "FTDI purge of RX buffer failed"); |
1069 | ||
545820ce | 1070 | // Invalidate data in the readbuffer |
bfcee05b TJ |
1071 | ftdi->readbuffer_offset = 0; |
1072 | ftdi->readbuffer_remaining = 0; | |
a60be878 | 1073 | |
1189b11a TJ |
1074 | return 0; |
1075 | } | |
1076 | ||
1077 | /** | |
1078 | Clears the write buffer on the chip. | |
ed46f09c | 1079 | This is correct behavior for a TX flush. |
1189b11a TJ |
1080 | |
1081 | \param ftdi pointer to ftdi_context | |
1082 | ||
1083 | \retval 0: all fine | |
1084 | \retval -1: write buffer purge failed | |
22a1b5c1 | 1085 | \retval -2: USB device unavailable |
1189b11a | 1086 | */ |
ed46f09c ES |
1087 | int ftdi_tcoflush(struct ftdi_context *ftdi) |
1088 | { | |
1089 | if (ftdi == NULL || ftdi->usb_dev == NULL) | |
1090 | ftdi_error_return(-2, "USB device unavailable"); | |
1091 | ||
1092 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, | |
1093 | SIO_RESET_REQUEST, SIO_TCOFLUSH, | |
1094 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) | |
1095 | ftdi_error_return(-1, "FTDI purge of TX buffer failed"); | |
1096 | ||
1097 | return 0; | |
1098 | } | |
1099 | ||
1100 | ||
1101 | /** | |
1102 | Clears the read buffer on the chip. | |
1103 | This is incorrect behavior for a TX flush. | |
1104 | ||
1105 | \param ftdi pointer to ftdi_context | |
1106 | ||
1107 | \retval 0: all fine | |
1108 | \retval -1: read buffer purge failed | |
1109 | \retval -2: USB device unavailable | |
1110 | ||
1111 | \deprecated Use \ref ftdi_tcoflush(struct ftdi_context *ftdi) | |
1112 | */ | |
1189b11a TJ |
1113 | int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi) |
1114 | { | |
22a1b5c1 TJ |
1115 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1116 | ftdi_error_return(-2, "USB device unavailable"); | |
1117 | ||
579b006f JZ |
1118 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
1119 | SIO_RESET_REQUEST, SIO_RESET_PURGE_TX, | |
1120 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) | |
1189b11a TJ |
1121 | ftdi_error_return(-1, "FTDI purge of TX buffer failed"); |
1122 | ||
1123 | return 0; | |
1124 | } | |
1125 | ||
1126 | /** | |
ed46f09c ES |
1127 | Clears the RX and TX FIFOs on the chip and the internal read buffer. |
1128 | This is correct behavior for both RX and TX flush. | |
1129 | ||
1130 | \param ftdi pointer to ftdi_context | |
1131 | ||
1132 | \retval 0: all fine | |
1133 | \retval -1: read buffer purge failed | |
1134 | \retval -2: write buffer purge failed | |
1135 | \retval -3: USB device unavailable | |
1136 | */ | |
1137 | int ftdi_tcioflush(struct ftdi_context *ftdi) | |
1138 | { | |
1139 | int result; | |
1140 | ||
1141 | if (ftdi == NULL || ftdi->usb_dev == NULL) | |
1142 | ftdi_error_return(-3, "USB device unavailable"); | |
1143 | ||
1144 | result = ftdi_tcoflush(ftdi); | |
1145 | if (result < 0) | |
1146 | return -1; | |
1147 | ||
1148 | result = ftdi_tciflush(ftdi); | |
1149 | if (result < 0) | |
1150 | return -2; | |
1151 | ||
1152 | return 0; | |
1153 | } | |
1154 | ||
1155 | /** | |
1189b11a | 1156 | Clears the buffers on the chip and the internal read buffer. |
ed46f09c | 1157 | While coded incorrectly, the result is satisfactory. |
1189b11a TJ |
1158 | |
1159 | \param ftdi pointer to ftdi_context | |
1160 | ||
1161 | \retval 0: all fine | |
1162 | \retval -1: read buffer purge failed | |
1163 | \retval -2: write buffer purge failed | |
22a1b5c1 | 1164 | \retval -3: USB device unavailable |
ed46f09c ES |
1165 | |
1166 | \deprecated Use \ref ftdi_tcioflush(struct ftdi_context *ftdi) | |
1189b11a TJ |
1167 | */ |
1168 | int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) | |
1169 | { | |
1170 | int result; | |
1171 | ||
22a1b5c1 TJ |
1172 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1173 | ftdi_error_return(-3, "USB device unavailable"); | |
1174 | ||
1189b11a | 1175 | result = ftdi_usb_purge_rx_buffer(ftdi); |
5a2b51cb | 1176 | if (result < 0) |
1189b11a TJ |
1177 | return -1; |
1178 | ||
1179 | result = ftdi_usb_purge_tx_buffer(ftdi); | |
5a2b51cb | 1180 | if (result < 0) |
1189b11a | 1181 | return -2; |
545820ce | 1182 | |
a60be878 TJ |
1183 | return 0; |
1184 | } | |
a3da1d95 | 1185 | |
f3f81007 TJ |
1186 | |
1187 | ||
1941414d TJ |
1188 | /** |
1189 | Closes the ftdi device. Call ftdi_deinit() if you're cleaning up. | |
1190 | ||
1191 | \param ftdi pointer to ftdi_context | |
1192 | ||
1193 | \retval 0: all fine | |
1194 | \retval -1: usb_release failed | |
22a1b5c1 | 1195 | \retval -3: ftdi context invalid |
a3da1d95 | 1196 | */ |
a8f46ddc TJ |
1197 | int ftdi_usb_close(struct ftdi_context *ftdi) |
1198 | { | |
a3da1d95 GE |
1199 | int rtn = 0; |
1200 | ||
22a1b5c1 TJ |
1201 | if (ftdi == NULL) |
1202 | ftdi_error_return(-3, "ftdi context invalid"); | |
1203 | ||
dff4fdb0 | 1204 | if (ftdi->usb_dev != NULL) |
579b006f | 1205 | if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0) |
dff4fdb0 | 1206 | rtn = -1; |
98452d97 | 1207 | |
579b006f | 1208 | ftdi_usb_close_internal (ftdi); |
98452d97 | 1209 | |
a3da1d95 GE |
1210 | return rtn; |
1211 | } | |
1212 | ||
74387f27 | 1213 | /* ftdi_to_clkbits_AM For the AM device, convert a requested baudrate |
f15786e4 | 1214 | to encoded divisor and the achievable baudrate |
53ad271d | 1215 | Function is only used internally |
b5ec1820 | 1216 | \internal |
f15786e4 UB |
1217 | |
1218 | See AN120 | |
1219 | clk/1 -> 0 | |
1220 | clk/1.5 -> 1 | |
1221 | clk/2 -> 2 | |
1222 | From /2, 0.125/ 0.25 and 0.5 steps may be taken | |
1223 | The fractional part has frac_code encoding | |
53ad271d | 1224 | */ |
f15786e4 UB |
1225 | static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor) |
1226 | ||
a8f46ddc | 1227 | { |
f15786e4 | 1228 | static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7}; |
53ad271d TJ |
1229 | static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1}; |
1230 | static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3}; | |
53ad271d | 1231 | int divisor, best_divisor, best_baud, best_baud_diff; |
f15786e4 | 1232 | int i; |
32e2d8b0 | 1233 | divisor = 24000000 / baudrate; |
53ad271d | 1234 | |
f15786e4 UB |
1235 | // Round down to supported fraction (AM only) |
1236 | divisor -= am_adjust_dn[divisor & 7]; | |
53ad271d TJ |
1237 | |
1238 | // Try this divisor and the one above it (because division rounds down) | |
1239 | best_divisor = 0; | |
1240 | best_baud = 0; | |
1241 | best_baud_diff = 0; | |
22d12cda TJ |
1242 | for (i = 0; i < 2; i++) |
1243 | { | |
53ad271d TJ |
1244 | int try_divisor = divisor + i; |
1245 | int baud_estimate; | |
1246 | int baud_diff; | |
1247 | ||
1248 | // Round up to supported divisor value | |
22d12cda TJ |
1249 | if (try_divisor <= 8) |
1250 | { | |
53ad271d TJ |
1251 | // Round up to minimum supported divisor |
1252 | try_divisor = 8; | |
22d12cda | 1253 | } |
22d12cda TJ |
1254 | else if (divisor < 16) |
1255 | { | |
53ad271d TJ |
1256 | // AM doesn't support divisors 9 through 15 inclusive |
1257 | try_divisor = 16; | |
22d12cda TJ |
1258 | } |
1259 | else | |
1260 | { | |
f15786e4 UB |
1261 | // Round up to supported fraction (AM only) |
1262 | try_divisor += am_adjust_up[try_divisor & 7]; | |
1263 | if (try_divisor > 0x1FFF8) | |
22d12cda | 1264 | { |
f15786e4 UB |
1265 | // Round down to maximum supported divisor value (for AM) |
1266 | try_divisor = 0x1FFF8; | |
53ad271d TJ |
1267 | } |
1268 | } | |
1269 | // Get estimated baud rate (to nearest integer) | |
1270 | baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor; | |
1271 | // Get absolute difference from requested baud rate | |
22d12cda TJ |
1272 | if (baud_estimate < baudrate) |
1273 | { | |
53ad271d | 1274 | baud_diff = baudrate - baud_estimate; |
22d12cda TJ |
1275 | } |
1276 | else | |
1277 | { | |
53ad271d TJ |
1278 | baud_diff = baud_estimate - baudrate; |
1279 | } | |
22d12cda TJ |
1280 | if (i == 0 || baud_diff < best_baud_diff) |
1281 | { | |
53ad271d TJ |
1282 | // Closest to requested baud rate so far |
1283 | best_divisor = try_divisor; | |
1284 | best_baud = baud_estimate; | |
1285 | best_baud_diff = baud_diff; | |
22d12cda TJ |
1286 | if (baud_diff == 0) |
1287 | { | |
53ad271d TJ |
1288 | // Spot on! No point trying |
1289 | break; | |
1290 | } | |
1291 | } | |
1292 | } | |
1293 | // Encode the best divisor value | |
f15786e4 | 1294 | *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14); |
53ad271d | 1295 | // Deal with special cases for encoded value |
f15786e4 | 1296 | if (*encoded_divisor == 1) |
22d12cda | 1297 | { |
f15786e4 | 1298 | *encoded_divisor = 0; // 3000000 baud |
22d12cda | 1299 | } |
f15786e4 UB |
1300 | else if (*encoded_divisor == 0x4001) |
1301 | { | |
1302 | *encoded_divisor = 1; // 2000000 baud (BM only) | |
1303 | } | |
1304 | return best_baud; | |
1305 | } | |
1306 | ||
1307 | /* ftdi_to_clkbits Convert a requested baudrate for a given system clock and predivisor | |
1308 | to encoded divisor and the achievable baudrate | |
1309 | Function is only used internally | |
1310 | \internal | |
1311 | ||
1312 | See AN120 | |
1313 | clk/1 -> 0 | |
1314 | clk/1.5 -> 1 | |
1315 | clk/2 -> 2 | |
1316 | From /2, 0.125 steps may be taken. | |
1317 | The fractional part has frac_code encoding | |
9956d428 UB |
1318 | |
1319 | value[13:0] of value is the divisor | |
1320 | index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else | |
1321 | ||
1322 | H Type have all features above with | |
1323 | {index[8],value[15:14]} is the encoded subdivisor | |
1324 | ||
74387f27 | 1325 | FT232R, FT2232 and FT232BM have no option for 12 MHz and with |
9956d428 UB |
1326 | {index[0],value[15:14]} is the encoded subdivisor |
1327 | ||
1328 | AM Type chips have only four fractional subdivisors at value[15:14] | |
1329 | for subdivisors 0, 0.5, 0.25, 0.125 | |
f15786e4 UB |
1330 | */ |
1331 | static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor) | |
1332 | { | |
1333 | static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7}; | |
1334 | int best_baud = 0; | |
1335 | int divisor, best_divisor; | |
1336 | if (baudrate >= clk/clk_div) | |
1337 | { | |
1338 | *encoded_divisor = 0; | |
1339 | best_baud = clk/clk_div; | |
1340 | } | |
1341 | else if (baudrate >= clk/(clk_div + clk_div/2)) | |
1342 | { | |
1343 | *encoded_divisor = 1; | |
1344 | best_baud = clk/(clk_div + clk_div/2); | |
1345 | } | |
1346 | else if (baudrate >= clk/(2*clk_div)) | |
1347 | { | |
1348 | *encoded_divisor = 2; | |
1349 | best_baud = clk/(2*clk_div); | |
1350 | } | |
1351 | else | |
1352 | { | |
1353 | /* We divide by 16 to have 3 fractional bits and one bit for rounding */ | |
1354 | divisor = clk*16/clk_div / baudrate; | |
1355 | if (divisor & 1) /* Decide if to round up or down*/ | |
1356 | best_divisor = divisor /2 +1; | |
1357 | else | |
1358 | best_divisor = divisor/2; | |
1359 | if(best_divisor > 0x20000) | |
1360 | best_divisor = 0x1ffff; | |
aae08071 UB |
1361 | best_baud = clk*16/clk_div/best_divisor; |
1362 | if (best_baud & 1) /* Decide if to round up or down*/ | |
1363 | best_baud = best_baud /2 +1; | |
1364 | else | |
1365 | best_baud = best_baud /2; | |
f15786e4 UB |
1366 | *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14); |
1367 | } | |
1368 | return best_baud; | |
74387f27 | 1369 | } |
f15786e4 UB |
1370 | /** |
1371 | ftdi_convert_baudrate returns nearest supported baud rate to that requested. | |
1372 | Function is only used internally | |
1373 | \internal | |
1374 | */ | |
1375 | static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, | |
1376 | unsigned short *value, unsigned short *index) | |
1377 | { | |
1378 | int best_baud; | |
1379 | unsigned long encoded_divisor; | |
1380 | ||
1381 | if (baudrate <= 0) | |
1382 | { | |
1383 | // Return error | |
1384 | return -1; | |
1385 | } | |
1386 | ||
1387 | #define H_CLK 120000000 | |
1388 | #define C_CLK 48000000 | |
6ae693b2 | 1389 | if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H)) |
f15786e4 UB |
1390 | { |
1391 | if(baudrate*10 > H_CLK /0x3fff) | |
1392 | { | |
1393 | /* On H Devices, use 12 000 000 Baudrate when possible | |
74387f27 | 1394 | We have a 14 bit divisor, a 1 bit divisor switch (10 or 16) |
f15786e4 UB |
1395 | three fractional bits and a 120 MHz clock |
1396 | Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for | |
1397 | DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/ | |
1398 | best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor); | |
1399 | encoded_divisor |= 0x20000; /* switch on CLK/10*/ | |
1400 | } | |
1401 | else | |
1402 | best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor); | |
1403 | } | |
913ca54f | 1404 | else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X)) |
f15786e4 UB |
1405 | { |
1406 | best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor); | |
1407 | } | |
1408 | else | |
22d12cda | 1409 | { |
f15786e4 | 1410 | best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor); |
53ad271d TJ |
1411 | } |
1412 | // Split into "value" and "index" values | |
1413 | *value = (unsigned short)(encoded_divisor & 0xFFFF); | |
6ae693b2 | 1414 | if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H) |
22d12cda | 1415 | { |
0126d22e TJ |
1416 | *index = (unsigned short)(encoded_divisor >> 8); |
1417 | *index &= 0xFF00; | |
a9c57c05 | 1418 | *index |= ftdi->index; |
0126d22e TJ |
1419 | } |
1420 | else | |
1421 | *index = (unsigned short)(encoded_divisor >> 16); | |
c3d95b87 | 1422 | |
53ad271d TJ |
1423 | // Return the nearest baud rate |
1424 | return best_baud; | |
1425 | } | |
1426 | ||
1941414d | 1427 | /** |
ac6944cc TJ |
1428 | * @brief Wrapper function to export ftdi_convert_baudrate() to the unit test |
1429 | * Do not use, it's only for the unit test framework | |
1430 | **/ | |
1431 | int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi, | |
74387f27 | 1432 | unsigned short *value, unsigned short *index) |
ac6944cc TJ |
1433 | { |
1434 | return ftdi_convert_baudrate(baudrate, ftdi, value, index); | |
1435 | } | |
1436 | ||
1437 | /** | |
9bec2387 | 1438 | Sets the chip baud rate |
1941414d TJ |
1439 | |
1440 | \param ftdi pointer to ftdi_context | |
9bec2387 | 1441 | \param baudrate baud rate to set |
1941414d TJ |
1442 | |
1443 | \retval 0: all fine | |
1444 | \retval -1: invalid baudrate | |
1445 | \retval -2: setting baudrate failed | |
22a1b5c1 | 1446 | \retval -3: USB device unavailable |
a3da1d95 | 1447 | */ |
a8f46ddc TJ |
1448 | int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) |
1449 | { | |
53ad271d TJ |
1450 | unsigned short value, index; |
1451 | int actual_baudrate; | |
a3da1d95 | 1452 | |
22a1b5c1 TJ |
1453 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1454 | ftdi_error_return(-3, "USB device unavailable"); | |
1455 | ||
22d12cda TJ |
1456 | if (ftdi->bitbang_enabled) |
1457 | { | |
a3da1d95 GE |
1458 | baudrate = baudrate*4; |
1459 | } | |
1460 | ||
25707904 | 1461 | actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index); |
c3d95b87 TJ |
1462 | if (actual_baudrate <= 0) |
1463 | ftdi_error_return (-1, "Silly baudrate <= 0."); | |
a3da1d95 | 1464 | |
53ad271d TJ |
1465 | // Check within tolerance (about 5%) |
1466 | if ((actual_baudrate * 2 < baudrate /* Catch overflows */ ) | |
1467 | || ((actual_baudrate < baudrate) | |
1468 | ? (actual_baudrate * 21 < baudrate * 20) | |
c3d95b87 TJ |
1469 | : (baudrate * 21 < actual_baudrate * 20))) |
1470 | ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4"); | |
545820ce | 1471 | |
579b006f JZ |
1472 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
1473 | SIO_SET_BAUDRATE_REQUEST, value, | |
1474 | index, NULL, 0, ftdi->usb_write_timeout) < 0) | |
c3d95b87 | 1475 | ftdi_error_return (-2, "Setting new baudrate failed"); |
a3da1d95 GE |
1476 | |
1477 | ftdi->baudrate = baudrate; | |
1478 | return 0; | |
1479 | } | |
1480 | ||
1941414d | 1481 | /** |
6c32e222 TJ |
1482 | Set (RS232) line characteristics. |
1483 | The break type can only be set via ftdi_set_line_property2() | |
1484 | and defaults to "off". | |
4837f98a | 1485 | |
1941414d TJ |
1486 | \param ftdi pointer to ftdi_context |
1487 | \param bits Number of bits | |
1488 | \param sbit Number of stop bits | |
1489 | \param parity Parity mode | |
1490 | ||
1491 | \retval 0: all fine | |
1492 | \retval -1: Setting line property failed | |
2f73e59f TJ |
1493 | */ |
1494 | int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits, | |
d2f10023 | 1495 | enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity) |
2f73e59f | 1496 | { |
6c32e222 TJ |
1497 | return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF); |
1498 | } | |
1499 | ||
1500 | /** | |
1501 | Set (RS232) line characteristics | |
1502 | ||
1503 | \param ftdi pointer to ftdi_context | |
1504 | \param bits Number of bits | |
1505 | \param sbit Number of stop bits | |
1506 | \param parity Parity mode | |
1507 | \param break_type Break type | |
1508 | ||
1509 | \retval 0: all fine | |
1510 | \retval -1: Setting line property failed | |
22a1b5c1 | 1511 | \retval -2: USB device unavailable |
6c32e222 TJ |
1512 | */ |
1513 | int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits, | |
22d12cda TJ |
1514 | enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity, |
1515 | enum ftdi_break_type break_type) | |
6c32e222 | 1516 | { |
2f73e59f TJ |
1517 | unsigned short value = bits; |
1518 | ||
22a1b5c1 TJ |
1519 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1520 | ftdi_error_return(-2, "USB device unavailable"); | |
1521 | ||
22d12cda TJ |
1522 | switch (parity) |
1523 | { | |
1524 | case NONE: | |
1525 | value |= (0x00 << 8); | |
1526 | break; | |
1527 | case ODD: | |
1528 | value |= (0x01 << 8); | |
1529 | break; | |
1530 | case EVEN: | |
1531 | value |= (0x02 << 8); | |
1532 | break; | |
1533 | case MARK: | |
1534 | value |= (0x03 << 8); | |
1535 | break; | |
1536 | case SPACE: | |
1537 | value |= (0x04 << 8); | |
1538 | break; | |
2f73e59f | 1539 | } |
d2f10023 | 1540 | |
22d12cda TJ |
1541 | switch (sbit) |
1542 | { | |
1543 | case STOP_BIT_1: | |
1544 | value |= (0x00 << 11); | |
1545 | break; | |
1546 | case STOP_BIT_15: | |
1547 | value |= (0x01 << 11); | |
1548 | break; | |
1549 | case STOP_BIT_2: | |
1550 | value |= (0x02 << 11); | |
1551 | break; | |
2f73e59f | 1552 | } |
d2f10023 | 1553 | |
22d12cda TJ |
1554 | switch (break_type) |
1555 | { | |
1556 | case BREAK_OFF: | |
1557 | value |= (0x00 << 14); | |
1558 | break; | |
1559 | case BREAK_ON: | |
1560 | value |= (0x01 << 14); | |
1561 | break; | |
6c32e222 TJ |
1562 | } |
1563 | ||
579b006f JZ |
1564 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
1565 | SIO_SET_DATA_REQUEST, value, | |
1566 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) | |
2f73e59f | 1567 | ftdi_error_return (-1, "Setting new line property failed"); |
d2f10023 | 1568 | |
2f73e59f TJ |
1569 | return 0; |
1570 | } | |
a3da1d95 | 1571 | |
1941414d TJ |
1572 | /** |
1573 | Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip | |
1574 | ||
1575 | \param ftdi pointer to ftdi_context | |
1576 | \param buf Buffer with the data | |
1577 | \param size Size of the buffer | |
1578 | ||
22a1b5c1 | 1579 | \retval -666: USB device unavailable |
1941414d TJ |
1580 | \retval <0: error code from usb_bulk_write() |
1581 | \retval >0: number of bytes written | |
1582 | */ | |
276750c1 | 1583 | int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size) |
a8f46ddc | 1584 | { |
a3da1d95 | 1585 | int offset = 0; |
579b006f | 1586 | int actual_length; |
c3d95b87 | 1587 | |
22a1b5c1 TJ |
1588 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1589 | ftdi_error_return(-666, "USB device unavailable"); | |
1590 | ||
22d12cda TJ |
1591 | while (offset < size) |
1592 | { | |
948f9ada | 1593 | int write_size = ftdi->writebuffer_chunksize; |
a3da1d95 GE |
1594 | |
1595 | if (offset+write_size > size) | |
1596 | write_size = size-offset; | |
1597 | ||
276750c1 | 1598 | if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0) |
579b006f | 1599 | ftdi_error_return(-1, "usb bulk write failed"); |
a3da1d95 | 1600 | |
579b006f | 1601 | offset += actual_length; |
a3da1d95 GE |
1602 | } |
1603 | ||
579b006f | 1604 | return offset; |
a3da1d95 GE |
1605 | } |
1606 | ||
32e2d8b0 | 1607 | static void LIBUSB_CALL ftdi_read_data_cb(struct libusb_transfer *transfer) |
22d12cda | 1608 | { |
579b006f JZ |
1609 | struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data; |
1610 | struct ftdi_context *ftdi = tc->ftdi; | |
1611 | int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret; | |
4c9e3812 | 1612 | |
b1139150 | 1613 | packet_size = ftdi->max_packet_size; |
579b006f JZ |
1614 | |
1615 | actual_length = transfer->actual_length; | |
1616 | ||
1617 | if (actual_length > 2) | |
1618 | { | |
1619 | // skip FTDI status bytes. | |
1620 | // Maybe stored in the future to enable modem use | |
1621 | num_of_chunks = actual_length / packet_size; | |
1622 | chunk_remains = actual_length % packet_size; | |
1623 | //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset); | |
1624 | ||
1625 | ftdi->readbuffer_offset += 2; | |
1626 | actual_length -= 2; | |
1627 | ||
1628 | if (actual_length > packet_size - 2) | |
1629 | { | |
1630 | for (i = 1; i < num_of_chunks; i++) | |
56ac0383 TJ |
1631 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i, |
1632 | ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i, | |
1633 | packet_size - 2); | |
579b006f JZ |
1634 | if (chunk_remains > 2) |
1635 | { | |
1636 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i, | |
1637 | ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i, | |
1638 | chunk_remains-2); | |
1639 | actual_length -= 2*num_of_chunks; | |
1640 | } | |
1641 | else | |
56ac0383 | 1642 | actual_length -= 2*(num_of_chunks-1)+chunk_remains; |
579b006f JZ |
1643 | } |
1644 | ||
1645 | if (actual_length > 0) | |
1646 | { | |
1647 | // data still fits in buf? | |
1648 | if (tc->offset + actual_length <= tc->size) | |
1649 | { | |
1650 | memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length); | |
1651 | //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]); | |
1652 | tc->offset += actual_length; | |
1653 | ||
1654 | ftdi->readbuffer_offset = 0; | |
1655 | ftdi->readbuffer_remaining = 0; | |
1656 | ||
1657 | /* Did we read exactly the right amount of bytes? */ | |
1658 | if (tc->offset == tc->size) | |
1659 | { | |
1660 | //printf("read_data exact rem %d offset %d\n", | |
1661 | //ftdi->readbuffer_remaining, offset); | |
1662 | tc->completed = 1; | |
1663 | return; | |
1664 | } | |
1665 | } | |
1666 | else | |
1667 | { | |
1668 | // only copy part of the data or size <= readbuffer_chunksize | |
1669 | int part_size = tc->size - tc->offset; | |
1670 | memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size); | |
1671 | tc->offset += part_size; | |
1672 | ||
1673 | ftdi->readbuffer_offset += part_size; | |
1674 | ftdi->readbuffer_remaining = actual_length - part_size; | |
1675 | ||
1676 | /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n", | |
1677 | part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */ | |
1678 | tc->completed = 1; | |
1679 | return; | |
1680 | } | |
1681 | } | |
1682 | } | |
1b1bf7e4 EH |
1683 | |
1684 | if (transfer->status == LIBUSB_TRANSFER_CANCELLED) | |
1685 | tc->completed = LIBUSB_TRANSFER_CANCELLED; | |
1686 | else | |
1687 | { | |
1688 | ret = libusb_submit_transfer (transfer); | |
1689 | if (ret < 0) | |
1690 | tc->completed = 1; | |
1691 | } | |
579b006f JZ |
1692 | } |
1693 | ||
1694 | ||
32e2d8b0 | 1695 | static void LIBUSB_CALL ftdi_write_data_cb(struct libusb_transfer *transfer) |
7cc9950e | 1696 | { |
579b006f JZ |
1697 | struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data; |
1698 | struct ftdi_context *ftdi = tc->ftdi; | |
56ac0383 | 1699 | |
90ef163e | 1700 | tc->offset += transfer->actual_length; |
56ac0383 | 1701 | |
579b006f | 1702 | if (tc->offset == tc->size) |
22d12cda | 1703 | { |
579b006f | 1704 | tc->completed = 1; |
7cc9950e | 1705 | } |
579b006f JZ |
1706 | else |
1707 | { | |
1708 | int write_size = ftdi->writebuffer_chunksize; | |
1709 | int ret; | |
7cc9950e | 1710 | |
579b006f JZ |
1711 | if (tc->offset + write_size > tc->size) |
1712 | write_size = tc->size - tc->offset; | |
1713 | ||
1714 | transfer->length = write_size; | |
1715 | transfer->buffer = tc->buf + tc->offset; | |
1b1bf7e4 EH |
1716 | |
1717 | if (transfer->status == LIBUSB_TRANSFER_CANCELLED) | |
1718 | tc->completed = LIBUSB_TRANSFER_CANCELLED; | |
1719 | else | |
1720 | { | |
1721 | ret = libusb_submit_transfer (transfer); | |
1722 | if (ret < 0) | |
1723 | tc->completed = 1; | |
1724 | } | |
579b006f | 1725 | } |
7cc9950e GE |
1726 | } |
1727 | ||
579b006f | 1728 | |
84f85aaa | 1729 | /** |
579b006f JZ |
1730 | Writes data to the chip. Does not wait for completion of the transfer |
1731 | nor does it make sure that the transfer was successful. | |
1732 | ||
249888c8 | 1733 | Use libusb 1.0 asynchronous API. |
84f85aaa GE |
1734 | |
1735 | \param ftdi pointer to ftdi_context | |
579b006f JZ |
1736 | \param buf Buffer with the data |
1737 | \param size Size of the buffer | |
84f85aaa | 1738 | |
579b006f JZ |
1739 | \retval NULL: Some error happens when submit transfer |
1740 | \retval !NULL: Pointer to a ftdi_transfer_control | |
c201f80f | 1741 | */ |
579b006f JZ |
1742 | |
1743 | struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size) | |
7cc9950e | 1744 | { |
579b006f | 1745 | struct ftdi_transfer_control *tc; |
5e77e870 | 1746 | struct libusb_transfer *transfer; |
579b006f | 1747 | int write_size, ret; |
22d12cda | 1748 | |
22a1b5c1 | 1749 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
22a1b5c1 | 1750 | return NULL; |
22a1b5c1 | 1751 | |
579b006f | 1752 | tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc)); |
5e77e870 TJ |
1753 | if (!tc) |
1754 | return NULL; | |
22d12cda | 1755 | |
5e77e870 TJ |
1756 | transfer = libusb_alloc_transfer(0); |
1757 | if (!transfer) | |
1758 | { | |
1759 | free(tc); | |
579b006f | 1760 | return NULL; |
5e77e870 | 1761 | } |
22d12cda | 1762 | |
579b006f JZ |
1763 | tc->ftdi = ftdi; |
1764 | tc->completed = 0; | |
1765 | tc->buf = buf; | |
1766 | tc->size = size; | |
1767 | tc->offset = 0; | |
7cc9950e | 1768 | |
9e44fc94 | 1769 | if (size < (int)ftdi->writebuffer_chunksize) |
56ac0383 | 1770 | write_size = size; |
579b006f | 1771 | else |
56ac0383 | 1772 | write_size = ftdi->writebuffer_chunksize; |
22d12cda | 1773 | |
90ef163e YSL |
1774 | libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf, |
1775 | write_size, ftdi_write_data_cb, tc, | |
1776 | ftdi->usb_write_timeout); | |
579b006f | 1777 | transfer->type = LIBUSB_TRANSFER_TYPE_BULK; |
7cc9950e | 1778 | |
579b006f JZ |
1779 | ret = libusb_submit_transfer(transfer); |
1780 | if (ret < 0) | |
1781 | { | |
1782 | libusb_free_transfer(transfer); | |
5e77e870 | 1783 | free(tc); |
579b006f | 1784 | return NULL; |
7cc9950e | 1785 | } |
579b006f JZ |
1786 | tc->transfer = transfer; |
1787 | ||
1788 | return tc; | |
7cc9950e GE |
1789 | } |
1790 | ||
1791 | /** | |
579b006f JZ |
1792 | Reads data from the chip. Does not wait for completion of the transfer |
1793 | nor does it make sure that the transfer was successful. | |
1794 | ||
249888c8 | 1795 | Use libusb 1.0 asynchronous API. |
7cc9950e GE |
1796 | |
1797 | \param ftdi pointer to ftdi_context | |
579b006f JZ |
1798 | \param buf Buffer with the data |
1799 | \param size Size of the buffer | |
4c9e3812 | 1800 | |
579b006f JZ |
1801 | \retval NULL: Some error happens when submit transfer |
1802 | \retval !NULL: Pointer to a ftdi_transfer_control | |
4c9e3812 | 1803 | */ |
579b006f JZ |
1804 | |
1805 | struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size) | |
4c9e3812 | 1806 | { |
579b006f JZ |
1807 | struct ftdi_transfer_control *tc; |
1808 | struct libusb_transfer *transfer; | |
1809 | int ret; | |
22d12cda | 1810 | |
22a1b5c1 TJ |
1811 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
1812 | return NULL; | |
1813 | ||
579b006f JZ |
1814 | tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc)); |
1815 | if (!tc) | |
1816 | return NULL; | |
1817 | ||
1818 | tc->ftdi = ftdi; | |
1819 | tc->buf = buf; | |
1820 | tc->size = size; | |
1821 | ||
9e44fc94 | 1822 | if (size <= (int)ftdi->readbuffer_remaining) |
7cc9950e | 1823 | { |
579b006f | 1824 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size); |
7cc9950e | 1825 | |
579b006f JZ |
1826 | // Fix offsets |
1827 | ftdi->readbuffer_remaining -= size; | |
1828 | ftdi->readbuffer_offset += size; | |
7cc9950e | 1829 | |
579b006f | 1830 | /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */ |
22d12cda | 1831 | |
579b006f JZ |
1832 | tc->completed = 1; |
1833 | tc->offset = size; | |
1834 | tc->transfer = NULL; | |
1835 | return tc; | |
1836 | } | |
4c9e3812 | 1837 | |
579b006f JZ |
1838 | tc->completed = 0; |
1839 | if (ftdi->readbuffer_remaining != 0) | |
1840 | { | |
1841 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining); | |
22d12cda | 1842 | |
579b006f JZ |
1843 | tc->offset = ftdi->readbuffer_remaining; |
1844 | } | |
1845 | else | |
1846 | tc->offset = 0; | |
22d12cda | 1847 | |
579b006f JZ |
1848 | transfer = libusb_alloc_transfer(0); |
1849 | if (!transfer) | |
1850 | { | |
1851 | free (tc); | |
1852 | return NULL; | |
1853 | } | |
22d12cda | 1854 | |
579b006f JZ |
1855 | ftdi->readbuffer_remaining = 0; |
1856 | ftdi->readbuffer_offset = 0; | |
1857 | ||
1858 | libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi_read_data_cb, tc, ftdi->usb_read_timeout); | |
1859 | transfer->type = LIBUSB_TRANSFER_TYPE_BULK; | |
1860 | ||
1861 | ret = libusb_submit_transfer(transfer); | |
1862 | if (ret < 0) | |
1863 | { | |
1864 | libusb_free_transfer(transfer); | |
1865 | free (tc); | |
1866 | return NULL; | |
22d12cda | 1867 | } |
579b006f JZ |
1868 | tc->transfer = transfer; |
1869 | ||
1870 | return tc; | |
4c9e3812 GE |
1871 | } |
1872 | ||
1873 | /** | |
579b006f | 1874 | Wait for completion of the transfer. |
4c9e3812 | 1875 | |
249888c8 | 1876 | Use libusb 1.0 asynchronous API. |
4c9e3812 | 1877 | |
579b006f | 1878 | \param tc pointer to ftdi_transfer_control |
4c9e3812 | 1879 | |
579b006f JZ |
1880 | \retval < 0: Some error happens |
1881 | \retval >= 0: Data size transferred | |
4c9e3812 | 1882 | */ |
579b006f JZ |
1883 | |
1884 | int ftdi_transfer_data_done(struct ftdi_transfer_control *tc) | |
4c9e3812 GE |
1885 | { |
1886 | int ret; | |
1b1bf7e4 | 1887 | struct timeval to = { 0, 0 }; |
579b006f | 1888 | while (!tc->completed) |
22d12cda | 1889 | { |
1b1bf7e4 EH |
1890 | ret = libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx, |
1891 | &to, &tc->completed); | |
4c9e3812 | 1892 | if (ret < 0) |
579b006f JZ |
1893 | { |
1894 | if (ret == LIBUSB_ERROR_INTERRUPTED) | |
1895 | continue; | |
1896 | libusb_cancel_transfer(tc->transfer); | |
1897 | while (!tc->completed) | |
1b1bf7e4 EH |
1898 | if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx, |
1899 | &to, &tc->completed) < 0) | |
579b006f JZ |
1900 | break; |
1901 | libusb_free_transfer(tc->transfer); | |
1902 | free (tc); | |
579b006f JZ |
1903 | return ret; |
1904 | } | |
4c9e3812 GE |
1905 | } |
1906 | ||
90ef163e YSL |
1907 | ret = tc->offset; |
1908 | /** | |
1909 | * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)" | |
ef15fab5 | 1910 | * at ftdi_read_data_submit(). Therefore, we need to check it here. |
90ef163e | 1911 | **/ |
ef15fab5 TJ |
1912 | if (tc->transfer) |
1913 | { | |
1914 | if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED) | |
1915 | ret = -1; | |
1916 | libusb_free_transfer(tc->transfer); | |
90ef163e | 1917 | } |
579b006f JZ |
1918 | free(tc); |
1919 | return ret; | |
4c9e3812 | 1920 | } |
579b006f | 1921 | |
1941414d | 1922 | /** |
1b1bf7e4 EH |
1923 | Cancel transfer and wait for completion. |
1924 | ||
1925 | Use libusb 1.0 asynchronous API. | |
1926 | ||
1927 | \param tc pointer to ftdi_transfer_control | |
1928 | \param to pointer to timeout value or NULL for infinite | |
1929 | */ | |
1930 | ||
1931 | void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc, | |
1932 | struct timeval * to) | |
1933 | { | |
1934 | struct timeval tv = { 0, 0 }; | |
1935 | ||
1936 | if (!tc->completed && tc->transfer != NULL) | |
1937 | { | |
1938 | if (to == NULL) | |
1939 | to = &tv; | |
1940 | ||
1941 | libusb_cancel_transfer(tc->transfer); | |
1942 | while (!tc->completed) | |
1943 | { | |
1944 | if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx, to, &tc->completed) < 0) | |
1945 | break; | |
1946 | } | |
1947 | } | |
1948 | ||
1949 | if (tc->transfer) | |
1950 | libusb_free_transfer(tc->transfer); | |
1951 | ||
1952 | free (tc); | |
1953 | } | |
1954 | ||
1955 | /** | |
1941414d TJ |
1956 | Configure write buffer chunk size. |
1957 | Default is 4096. | |
1958 | ||
1959 | \param ftdi pointer to ftdi_context | |
1960 | \param chunksize Chunk size | |
a3da1d95 | 1961 | |
1941414d | 1962 | \retval 0: all fine |
22a1b5c1 | 1963 | \retval -1: ftdi context invalid |
1941414d | 1964 | */ |
a8f46ddc TJ |
1965 | int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
1966 | { | |
22a1b5c1 TJ |
1967 | if (ftdi == NULL) |
1968 | ftdi_error_return(-1, "ftdi context invalid"); | |
1969 | ||
948f9ada TJ |
1970 | ftdi->writebuffer_chunksize = chunksize; |
1971 | return 0; | |
1972 | } | |
1973 | ||
1941414d TJ |
1974 | /** |
1975 | Get write buffer chunk size. | |
1976 | ||
1977 | \param ftdi pointer to ftdi_context | |
1978 | \param chunksize Pointer to store chunk size in | |
948f9ada | 1979 | |
1941414d | 1980 | \retval 0: all fine |
22a1b5c1 | 1981 | \retval -1: ftdi context invalid |
1941414d | 1982 | */ |
a8f46ddc TJ |
1983 | int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
1984 | { | |
22a1b5c1 TJ |
1985 | if (ftdi == NULL) |
1986 | ftdi_error_return(-1, "ftdi context invalid"); | |
1987 | ||
948f9ada TJ |
1988 | *chunksize = ftdi->writebuffer_chunksize; |
1989 | return 0; | |
1990 | } | |
cbabb7d3 | 1991 | |
1941414d TJ |
1992 | /** |
1993 | Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip. | |
1994 | ||
db9c7eba | 1995 | Automatically strips the two modem status bytes transferred during every read. |
948f9ada | 1996 | |
1941414d TJ |
1997 | \param ftdi pointer to ftdi_context |
1998 | \param buf Buffer to store data in | |
1999 | \param size Size of the buffer | |
2000 | ||
22a1b5c1 | 2001 | \retval -666: USB device unavailable |
579b006f | 2002 | \retval <0: error code from libusb_bulk_transfer() |
d77b0e94 | 2003 | \retval 0: no data was available |
1941414d TJ |
2004 | \retval >0: number of bytes read |
2005 | ||
1941414d | 2006 | */ |
a8f46ddc TJ |
2007 | int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
2008 | { | |
579b006f | 2009 | int offset = 0, ret, i, num_of_chunks, chunk_remains; |
5193cc23 | 2010 | int packet_size; |
579b006f | 2011 | int actual_length = 1; |
f2f00cb5 | 2012 | |
22a1b5c1 TJ |
2013 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2014 | ftdi_error_return(-666, "USB device unavailable"); | |
2015 | ||
e2f12a4f | 2016 | // Packet size sanity check (avoid division by zero) |
5193cc23 | 2017 | packet_size = ftdi->max_packet_size; |
e2f12a4f TJ |
2018 | if (packet_size == 0) |
2019 | ftdi_error_return(-1, "max_packet_size is bogus (zero)"); | |
d9f0cce7 | 2020 | |
948f9ada | 2021 | // everything we want is still in the readbuffer? |
9e44fc94 | 2022 | if (size <= (int)ftdi->readbuffer_remaining) |
22d12cda | 2023 | { |
d9f0cce7 TJ |
2024 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size); |
2025 | ||
2026 | // Fix offsets | |
2027 | ftdi->readbuffer_remaining -= size; | |
2028 | ftdi->readbuffer_offset += size; | |
2029 | ||
545820ce | 2030 | /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */ |
d9f0cce7 TJ |
2031 | |
2032 | return size; | |
979a145c | 2033 | } |
948f9ada | 2034 | // something still in the readbuffer, but not enough to satisfy 'size'? |
22d12cda TJ |
2035 | if (ftdi->readbuffer_remaining != 0) |
2036 | { | |
d9f0cce7 | 2037 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining); |
979a145c | 2038 | |
d9f0cce7 TJ |
2039 | // Fix offset |
2040 | offset += ftdi->readbuffer_remaining; | |
948f9ada | 2041 | } |
948f9ada | 2042 | // do the actual USB read |
579b006f | 2043 | while (offset < size && actual_length > 0) |
22d12cda | 2044 | { |
d9f0cce7 TJ |
2045 | ftdi->readbuffer_remaining = 0; |
2046 | ftdi->readbuffer_offset = 0; | |
98452d97 | 2047 | /* returns how much received */ |
579b006f | 2048 | ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout); |
c3d95b87 TJ |
2049 | if (ret < 0) |
2050 | ftdi_error_return(ret, "usb bulk read failed"); | |
98452d97 | 2051 | |
579b006f | 2052 | if (actual_length > 2) |
22d12cda | 2053 | { |
d9f0cce7 TJ |
2054 | // skip FTDI status bytes. |
2055 | // Maybe stored in the future to enable modem use | |
579b006f JZ |
2056 | num_of_chunks = actual_length / packet_size; |
2057 | chunk_remains = actual_length % packet_size; | |
2058 | //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset); | |
1c733d33 | 2059 | |
d9f0cce7 | 2060 | ftdi->readbuffer_offset += 2; |
579b006f | 2061 | actual_length -= 2; |
1c733d33 | 2062 | |
579b006f | 2063 | if (actual_length > packet_size - 2) |
22d12cda | 2064 | { |
1c733d33 | 2065 | for (i = 1; i < num_of_chunks; i++) |
f2f00cb5 DC |
2066 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i, |
2067 | ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i, | |
2068 | packet_size - 2); | |
22d12cda TJ |
2069 | if (chunk_remains > 2) |
2070 | { | |
f2f00cb5 DC |
2071 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i, |
2072 | ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i, | |
1c733d33 | 2073 | chunk_remains-2); |
579b006f | 2074 | actual_length -= 2*num_of_chunks; |
22d12cda TJ |
2075 | } |
2076 | else | |
579b006f | 2077 | actual_length -= 2*(num_of_chunks-1)+chunk_remains; |
1c733d33 | 2078 | } |
22d12cda | 2079 | } |
579b006f | 2080 | else if (actual_length <= 2) |
22d12cda | 2081 | { |
d9f0cce7 TJ |
2082 | // no more data to read? |
2083 | return offset; | |
2084 | } | |
579b006f | 2085 | if (actual_length > 0) |
22d12cda | 2086 | { |
d9f0cce7 | 2087 | // data still fits in buf? |
579b006f | 2088 | if (offset+actual_length <= size) |
22d12cda | 2089 | { |
579b006f | 2090 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length); |
545820ce | 2091 | //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]); |
579b006f | 2092 | offset += actual_length; |
d9f0cce7 | 2093 | |
53ad271d | 2094 | /* Did we read exactly the right amount of bytes? */ |
d9f0cce7 | 2095 | if (offset == size) |
c4446c36 TJ |
2096 | //printf("read_data exact rem %d offset %d\n", |
2097 | //ftdi->readbuffer_remaining, offset); | |
d9f0cce7 | 2098 | return offset; |
22d12cda TJ |
2099 | } |
2100 | else | |
2101 | { | |
d9f0cce7 TJ |
2102 | // only copy part of the data or size <= readbuffer_chunksize |
2103 | int part_size = size-offset; | |
2104 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size); | |
98452d97 | 2105 | |
d9f0cce7 | 2106 | ftdi->readbuffer_offset += part_size; |
579b006f | 2107 | ftdi->readbuffer_remaining = actual_length-part_size; |
d9f0cce7 TJ |
2108 | offset += part_size; |
2109 | ||
579b006f JZ |
2110 | /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n", |
2111 | part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */ | |
d9f0cce7 TJ |
2112 | |
2113 | return offset; | |
2114 | } | |
2115 | } | |
cbabb7d3 | 2116 | } |
948f9ada | 2117 | // never reached |
29c4af7f | 2118 | return -127; |
a3da1d95 GE |
2119 | } |
2120 | ||
1941414d TJ |
2121 | /** |
2122 | Configure read buffer chunk size. | |
2123 | Default is 4096. | |
2124 | ||
2125 | Automatically reallocates the buffer. | |
a3da1d95 | 2126 | |
1941414d TJ |
2127 | \param ftdi pointer to ftdi_context |
2128 | \param chunksize Chunk size | |
2129 | ||
2130 | \retval 0: all fine | |
22a1b5c1 | 2131 | \retval -1: ftdi context invalid |
1941414d | 2132 | */ |
a8f46ddc TJ |
2133 | int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
2134 | { | |
29c4af7f TJ |
2135 | unsigned char *new_buf; |
2136 | ||
22a1b5c1 TJ |
2137 | if (ftdi == NULL) |
2138 | ftdi_error_return(-1, "ftdi context invalid"); | |
2139 | ||
948f9ada TJ |
2140 | // Invalidate all remaining data |
2141 | ftdi->readbuffer_offset = 0; | |
2142 | ftdi->readbuffer_remaining = 0; | |
8de6eea4 JZ |
2143 | #ifdef __linux__ |
2144 | /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH, | |
2145 | which is defined in libusb-1.0. Otherwise, each USB read request will | |
2e685a1f | 2146 | be divided into multiple URBs. This will cause issues on Linux kernel |
8de6eea4 JZ |
2147 | older than 2.6.32. */ |
2148 | if (chunksize > 16384) | |
2149 | chunksize = 16384; | |
2150 | #endif | |
948f9ada | 2151 | |
c3d95b87 TJ |
2152 | if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) |
2153 | ftdi_error_return(-1, "out of memory for readbuffer"); | |
d9f0cce7 | 2154 | |
948f9ada TJ |
2155 | ftdi->readbuffer = new_buf; |
2156 | ftdi->readbuffer_chunksize = chunksize; | |
2157 | ||
2158 | return 0; | |
2159 | } | |
2160 | ||
1941414d TJ |
2161 | /** |
2162 | Get read buffer chunk size. | |
948f9ada | 2163 | |
1941414d TJ |
2164 | \param ftdi pointer to ftdi_context |
2165 | \param chunksize Pointer to store chunk size in | |
2166 | ||
2167 | \retval 0: all fine | |
22a1b5c1 | 2168 | \retval -1: FTDI context invalid |
1941414d | 2169 | */ |
a8f46ddc TJ |
2170 | int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
2171 | { | |
22a1b5c1 TJ |
2172 | if (ftdi == NULL) |
2173 | ftdi_error_return(-1, "FTDI context invalid"); | |
2174 | ||
948f9ada TJ |
2175 | *chunksize = ftdi->readbuffer_chunksize; |
2176 | return 0; | |
2177 | } | |
2178 | ||
1941414d | 2179 | /** |
2d790e37 | 2180 | Enable/disable bitbang modes. |
1941414d TJ |
2181 | |
2182 | \param ftdi pointer to ftdi_context | |
2183 | \param bitmask Bitmask to configure lines. | |
2184 | HIGH/ON value configures a line as output. | |
2d790e37 | 2185 | \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode |
1941414d TJ |
2186 | |
2187 | \retval 0: all fine | |
2188 | \retval -1: can't enable bitbang mode | |
22a1b5c1 | 2189 | \retval -2: USB device unavailable |
1941414d | 2190 | */ |
2d790e37 | 2191 | int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode) |
a8f46ddc | 2192 | { |
a3da1d95 GE |
2193 | unsigned short usb_val; |
2194 | ||
22a1b5c1 TJ |
2195 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2196 | ftdi_error_return(-2, "USB device unavailable"); | |
2197 | ||
d9f0cce7 | 2198 | usb_val = bitmask; // low byte: bitmask |
2d790e37 TJ |
2199 | usb_val |= (mode << 8); |
2200 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) | |
2201 | ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?"); | |
c3d95b87 | 2202 | |
2d790e37 TJ |
2203 | ftdi->bitbang_mode = mode; |
2204 | ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1; | |
a3da1d95 GE |
2205 | return 0; |
2206 | } | |
2207 | ||
1941414d TJ |
2208 | /** |
2209 | Disable bitbang mode. | |
a3da1d95 | 2210 | |
1941414d TJ |
2211 | \param ftdi pointer to ftdi_context |
2212 | ||
2213 | \retval 0: all fine | |
2214 | \retval -1: can't disable bitbang mode | |
22a1b5c1 | 2215 | \retval -2: USB device unavailable |
1941414d | 2216 | */ |
a8f46ddc TJ |
2217 | int ftdi_disable_bitbang(struct ftdi_context *ftdi) |
2218 | { | |
22a1b5c1 TJ |
2219 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2220 | ftdi_error_return(-2, "USB device unavailable"); | |
2221 | ||
579b006f | 2222 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) |
c3d95b87 | 2223 | ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?"); |
a3da1d95 GE |
2224 | |
2225 | ftdi->bitbang_enabled = 0; | |
2226 | return 0; | |
2227 | } | |
2228 | ||
c4446c36 | 2229 | |
1941414d | 2230 | /** |
418aaa72 | 2231 | Directly read pin state, circumventing the read buffer. Useful for bitbang mode. |
1941414d TJ |
2232 | |
2233 | \param ftdi pointer to ftdi_context | |
2234 | \param pins Pointer to store pins into | |
2235 | ||
2236 | \retval 0: all fine | |
2237 | \retval -1: read pins failed | |
22a1b5c1 | 2238 | \retval -2: USB device unavailable |
1941414d | 2239 | */ |
a8f46ddc TJ |
2240 | int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) |
2241 | { | |
22a1b5c1 TJ |
2242 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2243 | ftdi_error_return(-2, "USB device unavailable"); | |
2244 | ||
579b006f | 2245 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_PINS_REQUEST, 0, ftdi->index, (unsigned char *)pins, 1, ftdi->usb_read_timeout) != 1) |
c3d95b87 | 2246 | ftdi_error_return(-1, "read pins failed"); |
a3da1d95 | 2247 | |
a3da1d95 GE |
2248 | return 0; |
2249 | } | |
2250 | ||
1941414d TJ |
2251 | /** |
2252 | Set latency timer | |
2253 | ||
2254 | The FTDI chip keeps data in the internal buffer for a specific | |
2255 | amount of time if the buffer is not full yet to decrease | |
2256 | load on the usb bus. | |
a3da1d95 | 2257 | |
1941414d TJ |
2258 | \param ftdi pointer to ftdi_context |
2259 | \param latency Value between 1 and 255 | |
2260 | ||
2261 | \retval 0: all fine | |
2262 | \retval -1: latency out of range | |
2263 | \retval -2: unable to set latency timer | |
22a1b5c1 | 2264 | \retval -3: USB device unavailable |
1941414d | 2265 | */ |
a8f46ddc TJ |
2266 | int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) |
2267 | { | |
a3da1d95 GE |
2268 | unsigned short usb_val; |
2269 | ||
c3d95b87 TJ |
2270 | if (latency < 1) |
2271 | ftdi_error_return(-1, "latency out of range. Only valid for 1-255"); | |
a3da1d95 | 2272 | |
22a1b5c1 TJ |
2273 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2274 | ftdi_error_return(-3, "USB device unavailable"); | |
2275 | ||
d79d2e68 | 2276 | usb_val = latency; |
579b006f | 2277 | if (libusb_control_transfer(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 |
2278 | ftdi_error_return(-2, "unable to set latency timer"); |
2279 | ||
a3da1d95 GE |
2280 | return 0; |
2281 | } | |
2282 | ||
1941414d TJ |
2283 | /** |
2284 | Get latency timer | |
a3da1d95 | 2285 | |
1941414d TJ |
2286 | \param ftdi pointer to ftdi_context |
2287 | \param latency Pointer to store latency value in | |
2288 | ||
2289 | \retval 0: all fine | |
2290 | \retval -1: unable to get latency timer | |
22a1b5c1 | 2291 | \retval -2: USB device unavailable |
1941414d | 2292 | */ |
a8f46ddc TJ |
2293 | int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) |
2294 | { | |
a3da1d95 | 2295 | unsigned short usb_val; |
22a1b5c1 TJ |
2296 | |
2297 | if (ftdi == NULL || ftdi->usb_dev == NULL) | |
2298 | ftdi_error_return(-2, "USB device unavailable"); | |
2299 | ||
579b006f | 2300 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_GET_LATENCY_TIMER_REQUEST, 0, ftdi->index, (unsigned char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) |
c3d95b87 | 2301 | ftdi_error_return(-1, "reading latency timer failed"); |
a3da1d95 GE |
2302 | |
2303 | *latency = (unsigned char)usb_val; | |
2304 | return 0; | |
2305 | } | |
2306 | ||
1941414d | 2307 | /** |
1189b11a TJ |
2308 | Poll modem status information |
2309 | ||
2310 | This function allows the retrieve the two status bytes of the device. | |
2311 | The device sends these bytes also as a header for each read access | |
2312 | where they are discarded by ftdi_read_data(). The chip generates | |
2313 | the two stripped status bytes in the absence of data every 40 ms. | |
2314 | ||
2315 | Layout of the first byte: | |
2316 | - B0..B3 - must be 0 | |
2317 | - B4 Clear to send (CTS) | |
2318 | 0 = inactive | |
2319 | 1 = active | |
2320 | - B5 Data set ready (DTS) | |
2321 | 0 = inactive | |
2322 | 1 = active | |
2323 | - B6 Ring indicator (RI) | |
2324 | 0 = inactive | |
2325 | 1 = active | |
2326 | - B7 Receive line signal detect (RLSD) | |
2327 | 0 = inactive | |
2328 | 1 = active | |
2329 | ||
2330 | Layout of the second byte: | |
2331 | - B0 Data ready (DR) | |
2332 | - B1 Overrun error (OE) | |
2333 | - B2 Parity error (PE) | |
2334 | - B3 Framing error (FE) | |
2335 | - B4 Break interrupt (BI) | |
2336 | - B5 Transmitter holding register (THRE) | |
2337 | - B6 Transmitter empty (TEMT) | |
2338 | - B7 Error in RCVR FIFO | |
2339 | ||
2340 | \param ftdi pointer to ftdi_context | |
2341 | \param status Pointer to store status information in. Must be two bytes. | |
2342 | ||
2343 | \retval 0: all fine | |
2344 | \retval -1: unable to retrieve status information | |
22a1b5c1 | 2345 | \retval -2: USB device unavailable |
1189b11a TJ |
2346 | */ |
2347 | int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status) | |
2348 | { | |
2349 | char usb_val[2]; | |
2350 | ||
22a1b5c1 TJ |
2351 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2352 | ftdi_error_return(-2, "USB device unavailable"); | |
2353 | ||
579b006f | 2354 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, (unsigned char *)usb_val, 2, ftdi->usb_read_timeout) != 2) |
1189b11a TJ |
2355 | ftdi_error_return(-1, "getting modem status failed"); |
2356 | ||
dc09eaa8 | 2357 | *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF); |
1189b11a TJ |
2358 | |
2359 | return 0; | |
2360 | } | |
2361 | ||
a7fb8440 TJ |
2362 | /** |
2363 | Set flowcontrol for ftdi chip | |
2364 | ||
fdb93a5e PJ |
2365 | Note: Do not use this function to enable XON/XOFF mode, use ftdi_setflowctrl_xonxoff() instead. |
2366 | ||
a7fb8440 | 2367 | \param ftdi pointer to ftdi_context |
22d12cda | 2368 | \param flowctrl flow control to use. should be |
fdb93a5e | 2369 | SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS |
a7fb8440 TJ |
2370 | |
2371 | \retval 0: all fine | |
2372 | \retval -1: set flow control failed | |
22a1b5c1 | 2373 | \retval -2: USB device unavailable |
a7fb8440 TJ |
2374 | */ |
2375 | int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl) | |
2376 | { | |
22a1b5c1 TJ |
2377 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2378 | ftdi_error_return(-2, "USB device unavailable"); | |
2379 | ||
579b006f JZ |
2380 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
2381 | SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index), | |
2382 | NULL, 0, ftdi->usb_write_timeout) < 0) | |
a7fb8440 TJ |
2383 | ftdi_error_return(-1, "set flow control failed"); |
2384 | ||
2385 | return 0; | |
2386 | } | |
2387 | ||
2388 | /** | |
fdb93a5e PJ |
2389 | Set XON/XOFF flowcontrol for ftdi chip |
2390 | ||
2391 | \param ftdi pointer to ftdi_context | |
2392 | \param xon character code used to resume transmission | |
2393 | \param xoff character code used to pause transmission | |
2394 | ||
2395 | \retval 0: all fine | |
2396 | \retval -1: set flow control failed | |
2397 | \retval -2: USB device unavailable | |
2398 | */ | |
2399 | int ftdi_setflowctrl_xonxoff(struct ftdi_context *ftdi, unsigned char xon, unsigned char xoff) | |
2400 | { | |
2401 | if (ftdi == NULL || ftdi->usb_dev == NULL) | |
2402 | ftdi_error_return(-2, "USB device unavailable"); | |
2403 | ||
2404 | uint16_t xonxoff = xon | (xoff << 8); | |
2405 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, | |
2406 | SIO_SET_FLOW_CTRL_REQUEST, xonxoff, (SIO_XON_XOFF_HS | ftdi->index), | |
2407 | NULL, 0, ftdi->usb_write_timeout) < 0) | |
2408 | ftdi_error_return(-1, "set flow control failed"); | |
2409 | ||
2410 | return 0; | |
2411 | } | |
2412 | ||
2413 | /** | |
a7fb8440 TJ |
2414 | Set dtr line |
2415 | ||
2416 | \param ftdi pointer to ftdi_context | |
2417 | \param state state to set line to (1 or 0) | |
2418 | ||
2419 | \retval 0: all fine | |
2420 | \retval -1: set dtr failed | |
22a1b5c1 | 2421 | \retval -2: USB device unavailable |
a7fb8440 TJ |
2422 | */ |
2423 | int ftdi_setdtr(struct ftdi_context *ftdi, int state) | |
2424 | { | |
2425 | unsigned short usb_val; | |
2426 | ||
22a1b5c1 TJ |
2427 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2428 | ftdi_error_return(-2, "USB device unavailable"); | |
2429 | ||
a7fb8440 TJ |
2430 | if (state) |
2431 | usb_val = SIO_SET_DTR_HIGH; | |
2432 | else | |
2433 | usb_val = SIO_SET_DTR_LOW; | |
2434 | ||
579b006f JZ |
2435 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
2436 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index, | |
2437 | NULL, 0, ftdi->usb_write_timeout) < 0) | |
a7fb8440 TJ |
2438 | ftdi_error_return(-1, "set dtr failed"); |
2439 | ||
2440 | return 0; | |
2441 | } | |
2442 | ||
2443 | /** | |
2444 | Set rts line | |
2445 | ||
2446 | \param ftdi pointer to ftdi_context | |
2447 | \param state state to set line to (1 or 0) | |
2448 | ||
2449 | \retval 0: all fine | |
22a1b5c1 TJ |
2450 | \retval -1: set rts failed |
2451 | \retval -2: USB device unavailable | |
a7fb8440 TJ |
2452 | */ |
2453 | int ftdi_setrts(struct ftdi_context *ftdi, int state) | |
2454 | { | |
2455 | unsigned short usb_val; | |
2456 | ||
22a1b5c1 TJ |
2457 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2458 | ftdi_error_return(-2, "USB device unavailable"); | |
2459 | ||
a7fb8440 TJ |
2460 | if (state) |
2461 | usb_val = SIO_SET_RTS_HIGH; | |
2462 | else | |
2463 | usb_val = SIO_SET_RTS_LOW; | |
2464 | ||
579b006f JZ |
2465 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
2466 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index, | |
2467 | NULL, 0, ftdi->usb_write_timeout) < 0) | |
a7fb8440 TJ |
2468 | ftdi_error_return(-1, "set of rts failed"); |
2469 | ||
2470 | return 0; | |
2471 | } | |
2472 | ||
1189b11a | 2473 | /** |
22a1b5c1 | 2474 | Set dtr and rts line in one pass |
9ecfef2a | 2475 | |
22a1b5c1 TJ |
2476 | \param ftdi pointer to ftdi_context |
2477 | \param dtr DTR state to set line to (1 or 0) | |
2478 | \param rts RTS state to set line to (1 or 0) | |
9ecfef2a | 2479 | |
22a1b5c1 TJ |
2480 | \retval 0: all fine |
2481 | \retval -1: set dtr/rts failed | |
2482 | \retval -2: USB device unavailable | |
9ecfef2a TJ |
2483 | */ |
2484 | int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts) | |
2485 | { | |
2486 | unsigned short usb_val; | |
2487 | ||
22a1b5c1 TJ |
2488 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2489 | ftdi_error_return(-2, "USB device unavailable"); | |
2490 | ||
9ecfef2a | 2491 | if (dtr) |
22d12cda | 2492 | usb_val = SIO_SET_DTR_HIGH; |
9ecfef2a | 2493 | else |
22d12cda | 2494 | usb_val = SIO_SET_DTR_LOW; |
9ecfef2a TJ |
2495 | |
2496 | if (rts) | |
22d12cda | 2497 | usb_val |= SIO_SET_RTS_HIGH; |
9ecfef2a | 2498 | else |
22d12cda | 2499 | usb_val |= SIO_SET_RTS_LOW; |
9ecfef2a | 2500 | |
579b006f JZ |
2501 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
2502 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index, | |
2503 | NULL, 0, ftdi->usb_write_timeout) < 0) | |
22d12cda | 2504 | ftdi_error_return(-1, "set of rts/dtr failed"); |
9ecfef2a TJ |
2505 | |
2506 | return 0; | |
2507 | } | |
2508 | ||
2509 | /** | |
1189b11a TJ |
2510 | Set the special event character |
2511 | ||
2512 | \param ftdi pointer to ftdi_context | |
2513 | \param eventch Event character | |
2514 | \param enable 0 to disable the event character, non-zero otherwise | |
2515 | ||
2516 | \retval 0: all fine | |
2517 | \retval -1: unable to set event character | |
22a1b5c1 | 2518 | \retval -2: USB device unavailable |
1189b11a TJ |
2519 | */ |
2520 | int ftdi_set_event_char(struct ftdi_context *ftdi, | |
22d12cda | 2521 | unsigned char eventch, unsigned char enable) |
1189b11a TJ |
2522 | { |
2523 | unsigned short usb_val; | |
2524 | ||
22a1b5c1 TJ |
2525 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2526 | ftdi_error_return(-2, "USB device unavailable"); | |
2527 | ||
1189b11a TJ |
2528 | usb_val = eventch; |
2529 | if (enable) | |
2530 | usb_val |= 1 << 8; | |
2531 | ||
579b006f | 2532 | if (libusb_control_transfer(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 |
2533 | ftdi_error_return(-1, "setting event character failed"); |
2534 | ||
2535 | return 0; | |
2536 | } | |
2537 | ||
2538 | /** | |
2539 | Set error character | |
2540 | ||
2541 | \param ftdi pointer to ftdi_context | |
2542 | \param errorch Error character | |
2543 | \param enable 0 to disable the error character, non-zero otherwise | |
2544 | ||
2545 | \retval 0: all fine | |
2546 | \retval -1: unable to set error character | |
22a1b5c1 | 2547 | \retval -2: USB device unavailable |
1189b11a TJ |
2548 | */ |
2549 | int ftdi_set_error_char(struct ftdi_context *ftdi, | |
22d12cda | 2550 | unsigned char errorch, unsigned char enable) |
1189b11a TJ |
2551 | { |
2552 | unsigned short usb_val; | |
2553 | ||
22a1b5c1 TJ |
2554 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
2555 | ftdi_error_return(-2, "USB device unavailable"); | |
2556 | ||
1189b11a TJ |
2557 | usb_val = errorch; |
2558 | if (enable) | |
2559 | usb_val |= 1 << 8; | |
2560 | ||
579b006f | 2561 | if (libusb_control_transfer(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 |
2562 | ftdi_error_return(-1, "setting error character failed"); |
2563 | ||
2564 | return 0; | |
2565 | } | |
2566 | ||
2567 | /** | |
44f41f11 | 2568 | Init eeprom with default values for the connected device |
a35aa9bd | 2569 | \param ftdi pointer to ftdi_context |
f14f84d3 UB |
2570 | \param manufacturer String to use as Manufacturer |
2571 | \param product String to use as Product description | |
2572 | \param serial String to use as Serial number description | |
4e74064b | 2573 | |
f14f84d3 UB |
2574 | \retval 0: all fine |
2575 | \retval -1: No struct ftdi_context | |
2576 | \retval -2: No struct ftdi_eeprom | |
44f41f11 | 2577 | \retval -3: No connected device or device not yet opened |
1941414d | 2578 | */ |
f14f84d3 | 2579 | int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer, |
56ac0383 | 2580 | char * product, char * serial) |
a8f46ddc | 2581 | { |
c0a96aed | 2582 | struct ftdi_eeprom *eeprom; |
f505134f | 2583 | |
c0a96aed | 2584 | if (ftdi == NULL) |
f14f84d3 | 2585 | ftdi_error_return(-1, "No struct ftdi_context"); |
c0a96aed UB |
2586 | |
2587 | if (ftdi->eeprom == NULL) | |
56ac0383 | 2588 | ftdi_error_return(-2,"No struct ftdi_eeprom"); |
22a1b5c1 | 2589 | |
c0a96aed | 2590 | eeprom = ftdi->eeprom; |
a02587d5 | 2591 | memset(eeprom, 0, sizeof(struct ftdi_eeprom)); |
c0a96aed | 2592 | |
44f41f11 UB |
2593 | if (ftdi->usb_dev == NULL) |
2594 | ftdi_error_return(-3, "No connected device or device not yet opened"); | |
2595 | ||
f396dbad | 2596 | eeprom->vendor_id = 0x0403; |
d4b5af27 | 2597 | eeprom->use_serial = 1; |
56ac0383 TJ |
2598 | if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) || |
2599 | (ftdi->type == TYPE_R)) | |
a02587d5 | 2600 | eeprom->product_id = 0x6001; |
c7e4c09e UB |
2601 | else if (ftdi->type == TYPE_4232H) |
2602 | eeprom->product_id = 0x6011; | |
2603 | else if (ftdi->type == TYPE_232H) | |
2604 | eeprom->product_id = 0x6014; | |
2f80efc2 NP |
2605 | else if (ftdi->type == TYPE_230X) |
2606 | eeprom->product_id = 0x6015; | |
a02587d5 UB |
2607 | else |
2608 | eeprom->product_id = 0x6010; | |
2f80efc2 | 2609 | |
b1859923 UB |
2610 | if (ftdi->type == TYPE_AM) |
2611 | eeprom->usb_version = 0x0101; | |
2612 | else | |
2613 | eeprom->usb_version = 0x0200; | |
a886436a | 2614 | eeprom->max_power = 100; |
d9f0cce7 | 2615 | |
74e8e79d UB |
2616 | if (eeprom->manufacturer) |
2617 | free (eeprom->manufacturer); | |
b8aa7b35 | 2618 | eeprom->manufacturer = NULL; |
74e8e79d UB |
2619 | if (manufacturer) |
2620 | { | |
c45d2630 | 2621 | eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1); |
74e8e79d UB |
2622 | if (eeprom->manufacturer) |
2623 | strcpy(eeprom->manufacturer, manufacturer); | |
2624 | } | |
2625 | ||
2626 | if (eeprom->product) | |
2627 | free (eeprom->product); | |
b8aa7b35 | 2628 | eeprom->product = NULL; |
10771971 | 2629 | if(product) |
74e8e79d | 2630 | { |
c45d2630 | 2631 | eeprom->product = (char *)malloc(strlen(product)+1); |
74e8e79d UB |
2632 | if (eeprom->product) |
2633 | strcpy(eeprom->product, product); | |
2634 | } | |
6a6fcd89 UB |
2635 | else |
2636 | { | |
2637 | const char* default_product; | |
2638 | switch(ftdi->type) | |
2639 | { | |
74387f27 TJ |
2640 | case TYPE_AM: default_product = "AM"; break; |
2641 | case TYPE_BM: default_product = "BM"; break; | |
2642 | case TYPE_2232C: default_product = "Dual RS232"; break; | |
2643 | case TYPE_R: default_product = "FT232R USB UART"; break; | |
2644 | case TYPE_2232H: default_product = "Dual RS232-HS"; break; | |
2645 | case TYPE_4232H: default_product = "FT4232H"; break; | |
2646 | case TYPE_232H: default_product = "Single-RS232-HS"; break; | |
2647 | case TYPE_230X: default_product = "FT230X Basic UART"; break; | |
2648 | default: | |
2649 | ftdi_error_return(-3, "Unknown chip type"); | |
6a6fcd89 | 2650 | } |
c45d2630 | 2651 | eeprom->product = (char *)malloc(strlen(default_product) +1); |
6a6fcd89 UB |
2652 | if (eeprom->product) |
2653 | strcpy(eeprom->product, default_product); | |
2654 | } | |
74e8e79d UB |
2655 | |
2656 | if (eeprom->serial) | |
2657 | free (eeprom->serial); | |
b8aa7b35 | 2658 | eeprom->serial = NULL; |
74e8e79d UB |
2659 | if (serial) |
2660 | { | |
c45d2630 | 2661 | eeprom->serial = (char *)malloc(strlen(serial)+1); |
74e8e79d UB |
2662 | if (eeprom->serial) |
2663 | strcpy(eeprom->serial, serial); | |
2664 | } | |
2665 | ||
56ac0383 | 2666 | if (ftdi->type == TYPE_R) |
a4980043 | 2667 | { |
a886436a | 2668 | eeprom->max_power = 90; |
a02587d5 | 2669 | eeprom->size = 0x80; |
a4980043 UB |
2670 | eeprom->cbus_function[0] = CBUS_TXLED; |
2671 | eeprom->cbus_function[1] = CBUS_RXLED; | |
2672 | eeprom->cbus_function[2] = CBUS_TXDEN; | |
2673 | eeprom->cbus_function[3] = CBUS_PWREN; | |
2674 | eeprom->cbus_function[4] = CBUS_SLEEP; | |
2675 | } | |
2f80efc2 NP |
2676 | else if (ftdi->type == TYPE_230X) |
2677 | { | |
2678 | eeprom->max_power = 90; | |
2679 | eeprom->size = 0x100; | |
add00ad6 RH |
2680 | eeprom->cbus_function[0] = CBUSX_TXDEN; |
2681 | eeprom->cbus_function[1] = CBUSX_RXLED; | |
2682 | eeprom->cbus_function[2] = CBUSX_TXLED; | |
2683 | eeprom->cbus_function[3] = CBUSX_SLEEP; | |
2f80efc2 | 2684 | } |
a02587d5 | 2685 | else |
263d3ba0 UB |
2686 | { |
2687 | if(ftdi->type == TYPE_232H) | |
2688 | { | |
2689 | int i; | |
2690 | for (i=0; i<10; i++) | |
2691 | eeprom->cbus_function[i] = CBUSH_TRISTATE; | |
2692 | } | |
a02587d5 | 2693 | eeprom->size = -1; |
263d3ba0 | 2694 | } |
68e78641 JS |
2695 | switch (ftdi->type) |
2696 | { | |
2697 | case TYPE_AM: | |
2698 | eeprom->release_number = 0x0200; | |
2699 | break; | |
2700 | case TYPE_BM: | |
2701 | eeprom->release_number = 0x0400; | |
2702 | break; | |
2703 | case TYPE_2232C: | |
2704 | eeprom->release_number = 0x0500; | |
2705 | break; | |
2706 | case TYPE_R: | |
2707 | eeprom->release_number = 0x0600; | |
2708 | break; | |
2709 | case TYPE_2232H: | |
2710 | eeprom->release_number = 0x0700; | |
2711 | break; | |
2712 | case TYPE_4232H: | |
2713 | eeprom->release_number = 0x0800; | |
2714 | break; | |
2715 | case TYPE_232H: | |
2716 | eeprom->release_number = 0x0900; | |
2717 | break; | |
2f80efc2 NP |
2718 | case TYPE_230X: |
2719 | eeprom->release_number = 0x1000; | |
2720 | break; | |
68e78641 JS |
2721 | default: |
2722 | eeprom->release_number = 0x00; | |
2723 | } | |
f14f84d3 | 2724 | return 0; |
b8aa7b35 | 2725 | } |
878f0c6a | 2726 | |
ae3d154b DD |
2727 | int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, const char * manufacturer, |
2728 | const char * product, const char * serial) | |
878f0c6a NP |
2729 | { |
2730 | struct ftdi_eeprom *eeprom; | |
2731 | ||
2732 | if (ftdi == NULL) | |
2733 | ftdi_error_return(-1, "No struct ftdi_context"); | |
2734 | ||
2735 | if (ftdi->eeprom == NULL) | |
2736 | ftdi_error_return(-2,"No struct ftdi_eeprom"); | |
2737 | ||
2738 | eeprom = ftdi->eeprom; | |
2739 | ||
2740 | if (ftdi->usb_dev == NULL) | |
2741 | ftdi_error_return(-3, "No connected device or device not yet opened"); | |
2742 | ||
74387f27 TJ |
2743 | if (manufacturer) |
2744 | { | |
878f0c6a NP |
2745 | if (eeprom->manufacturer) |
2746 | free (eeprom->manufacturer); | |
c45d2630 | 2747 | eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1); |
878f0c6a NP |
2748 | if (eeprom->manufacturer) |
2749 | strcpy(eeprom->manufacturer, manufacturer); | |
2750 | } | |
2751 | ||
74387f27 TJ |
2752 | if(product) |
2753 | { | |
878f0c6a NP |
2754 | if (eeprom->product) |
2755 | free (eeprom->product); | |
c45d2630 | 2756 | eeprom->product = (char *)malloc(strlen(product)+1); |
878f0c6a NP |
2757 | if (eeprom->product) |
2758 | strcpy(eeprom->product, product); | |
2759 | } | |
2760 | ||
74387f27 TJ |
2761 | if (serial) |
2762 | { | |
878f0c6a NP |
2763 | if (eeprom->serial) |
2764 | free (eeprom->serial); | |
c45d2630 | 2765 | eeprom->serial = (char *)malloc(strlen(serial)+1); |
74387f27 TJ |
2766 | if (eeprom->serial) |
2767 | { | |
878f0c6a NP |
2768 | strcpy(eeprom->serial, serial); |
2769 | eeprom->use_serial = 1; | |
2770 | } | |
2771 | } | |
2772 | return 0; | |
2773 | } | |
2774 | ||
934173a3 TJ |
2775 | /** |
2776 | Return device ID strings from the eeprom. Device needs to be connected. | |
2777 | ||
2778 | The parameters manufacturer, description and serial may be NULL | |
2779 | or pointer to buffers to store the fetched strings. | |
2780 | ||
2781 | \param ftdi pointer to ftdi_context | |
2782 | \param manufacturer Store manufacturer string here if not NULL | |
2783 | \param mnf_len Buffer size of manufacturer string | |
2784 | \param product Store product description string here if not NULL | |
2785 | \param prod_len Buffer size of product description string | |
2786 | \param serial Store serial string here if not NULL | |
2787 | \param serial_len Buffer size of serial string | |
2788 | ||
2789 | \retval 0: all fine | |
2790 | \retval -1: ftdi context invalid | |
2791 | \retval -2: ftdi eeprom buffer invalid | |
934173a3 | 2792 | */ |
c9eeb2f1 AM |
2793 | int ftdi_eeprom_get_strings(struct ftdi_context *ftdi, |
2794 | char *manufacturer, int mnf_len, | |
2795 | char *product, int prod_len, | |
2796 | char *serial, int serial_len) | |
2797 | { | |
2798 | struct ftdi_eeprom *eeprom; | |
2799 | ||
2800 | if (ftdi == NULL) | |
2801 | ftdi_error_return(-1, "No struct ftdi_context"); | |
c9eeb2f1 | 2802 | if (ftdi->eeprom == NULL) |
4effe148 | 2803 | ftdi_error_return(-2, "No struct ftdi_eeprom"); |
c9eeb2f1 AM |
2804 | |
2805 | eeprom = ftdi->eeprom; | |
2806 | ||
c9eeb2f1 AM |
2807 | if (manufacturer) |
2808 | { | |
2809 | strncpy(manufacturer, eeprom->manufacturer, mnf_len); | |
2810 | if (mnf_len > 0) | |
2811 | manufacturer[mnf_len - 1] = '\0'; | |
2812 | } | |
2813 | ||
2814 | if (product) | |
2815 | { | |
2816 | strncpy(product, eeprom->product, prod_len); | |
2817 | if (prod_len > 0) | |
2818 | product[prod_len - 1] = '\0'; | |
2819 | } | |
2820 | ||
2821 | if (serial) | |
2822 | { | |
2823 | strncpy(serial, eeprom->serial, serial_len); | |
2824 | if (serial_len > 0) | |
2825 | serial[serial_len - 1] = '\0'; | |
2826 | } | |
2827 | ||
2828 | return 0; | |
2829 | } | |
878f0c6a | 2830 | |
add00ad6 | 2831 | /*FTD2XX doesn't check for values not fitting in the ACBUS Signal options*/ |
263d3ba0 UB |
2832 | void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output) |
2833 | { | |
2834 | int i; | |
74387f27 | 2835 | for(i=0; i<5; i++) |
263d3ba0 UB |
2836 | { |
2837 | int mode_low, mode_high; | |
2838 | if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5) | |
2839 | mode_low = CBUSH_TRISTATE; | |
2840 | else | |
2841 | mode_low = eeprom->cbus_function[2*i]; | |
2842 | if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5) | |
2843 | mode_high = CBUSH_TRISTATE; | |
2844 | else | |
f37a1524 | 2845 | mode_high = eeprom->cbus_function[2*i+1]; |
b8aa7b35 | 2846 | |
f37a1524 | 2847 | output[0x18+i] = (mode_high <<4) | mode_low; |
263d3ba0 UB |
2848 | } |
2849 | } | |
c8f69686 UB |
2850 | /* Return the bits for the encoded EEPROM Structure of a requested Mode |
2851 | * | |
2852 | */ | |
2853 | static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip) | |
2854 | { | |
2855 | switch (chip) | |
2856 | { | |
74387f27 TJ |
2857 | case TYPE_2232H: |
2858 | case TYPE_2232C: | |
c8f69686 | 2859 | { |
74387f27 TJ |
2860 | switch (type) |
2861 | { | |
2862 | case CHANNEL_IS_UART: return 0; | |
2863 | case CHANNEL_IS_FIFO: return 0x01; | |
2864 | case CHANNEL_IS_OPTO: return 0x02; | |
2865 | case CHANNEL_IS_CPU : return 0x04; | |
2866 | default: return 0; | |
2867 | } | |
c8f69686 | 2868 | } |
74387f27 | 2869 | case TYPE_232H: |
c8f69686 | 2870 | { |
74387f27 TJ |
2871 | switch (type) |
2872 | { | |
2873 | case CHANNEL_IS_UART : return 0; | |
2874 | case CHANNEL_IS_FIFO : return 0x01; | |
2875 | case CHANNEL_IS_OPTO : return 0x02; | |
2876 | case CHANNEL_IS_CPU : return 0x04; | |
2877 | case CHANNEL_IS_FT1284 : return 0x08; | |
2878 | default: return 0; | |
2879 | } | |
c8f69686 | 2880 | } |
6f9f969d RF |
2881 | case TYPE_R: |
2882 | { | |
2883 | switch (type) | |
2884 | { | |
2885 | case CHANNEL_IS_UART : return 0; | |
2886 | case CHANNEL_IS_FIFO : return 0x01; | |
2887 | default: return 0; | |
2888 | } | |
2889 | } | |
74387f27 TJ |
2890 | case TYPE_230X: /* FT230X is only UART */ |
2891 | default: return 0; | |
c8f69686 UB |
2892 | } |
2893 | return 0; | |
74387f27 | 2894 | } |
c8f69686 | 2895 | |
1941414d | 2896 | /** |
a35aa9bd | 2897 | Build binary buffer from ftdi_eeprom structure. |
22a1b5c1 | 2898 | Output is suitable for ftdi_write_eeprom(). |
b8aa7b35 | 2899 | |
a35aa9bd | 2900 | \param ftdi pointer to ftdi_context |
1941414d | 2901 | |
516ebfb1 | 2902 | \retval >=0: size of eeprom user area in bytes |
22a1b5c1 | 2903 | \retval -1: eeprom size (128 bytes) exceeded by custom strings |
2c1e2bde TJ |
2904 | \retval -2: Invalid eeprom or ftdi pointer |
2905 | \retval -3: Invalid cbus function setting (FIXME: Not in the code?) | |
2906 | \retval -4: Chip doesn't support invert (FIXME: Not in the code?) | |
2907 | \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?) | |
2b9a3c82 | 2908 | \retval -6: No connected EEPROM or EEPROM Type unknown |
b8aa7b35 | 2909 | */ |
a35aa9bd | 2910 | int ftdi_eeprom_build(struct ftdi_context *ftdi) |
a8f46ddc | 2911 | { |
e2bbd9af | 2912 | unsigned char i, j, eeprom_size_mask; |
b8aa7b35 TJ |
2913 | unsigned short checksum, value; |
2914 | unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0; | |
6e962b9a | 2915 | int user_area_size, free_start, free_end; |
c0a96aed | 2916 | struct ftdi_eeprom *eeprom; |
a35aa9bd | 2917 | unsigned char * output; |
b8aa7b35 | 2918 | |
c0a96aed | 2919 | if (ftdi == NULL) |
cc9c9d58 | 2920 | ftdi_error_return(-2,"No context"); |
c0a96aed | 2921 | if (ftdi->eeprom == NULL) |
cc9c9d58 | 2922 | ftdi_error_return(-2,"No eeprom structure"); |
c0a96aed UB |
2923 | |
2924 | eeprom= ftdi->eeprom; | |
a35aa9bd | 2925 | output = eeprom->buf; |
22a1b5c1 | 2926 | |
56ac0383 | 2927 | if (eeprom->chip == -1) |
2c1e2bde | 2928 | ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown"); |
2b9a3c82 | 2929 | |
74387f27 TJ |
2930 | if (eeprom->size == -1) |
2931 | { | |
2f80efc2 NP |
2932 | if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66)) |
2933 | eeprom->size = 0x100; | |
2934 | else | |
2935 | eeprom->size = 0x80; | |
2936 | } | |
f75bf139 | 2937 | |
b8aa7b35 | 2938 | if (eeprom->manufacturer != NULL) |
d9f0cce7 | 2939 | manufacturer_size = strlen(eeprom->manufacturer); |
b8aa7b35 | 2940 | if (eeprom->product != NULL) |
d9f0cce7 | 2941 | product_size = strlen(eeprom->product); |
b8aa7b35 | 2942 | if (eeprom->serial != NULL) |
d9f0cce7 | 2943 | serial_size = strlen(eeprom->serial); |
b8aa7b35 | 2944 | |
814710ba TJ |
2945 | // eeprom size check |
2946 | switch (ftdi->type) | |
2947 | { | |
2948 | case TYPE_AM: | |
2949 | case TYPE_BM: | |
6e962b9a | 2950 | case TYPE_R: |
814710ba TJ |
2951 | user_area_size = 96; // base size for strings (total of 48 characters) |
2952 | break; | |
2953 | case TYPE_2232C: | |
56ac0383 TJ |
2954 | user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff |
2955 | break; | |
2f80efc2 | 2956 | case TYPE_230X: |
56ac0383 TJ |
2957 | user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff |
2958 | break; | |
814710ba TJ |
2959 | case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff |
2960 | case TYPE_4232H: | |
56ac0383 | 2961 | user_area_size = 86; |
118c4561 | 2962 | break; |
c1c3d564 UB |
2963 | case TYPE_232H: |
2964 | user_area_size = 80; | |
2965 | break; | |
2c1e2bde TJ |
2966 | default: |
2967 | user_area_size = 0; | |
56ac0383 | 2968 | break; |
665cda04 UB |
2969 | } |
2970 | user_area_size -= (manufacturer_size + product_size + serial_size) * 2; | |
814710ba | 2971 | |
516ebfb1 TJ |
2972 | if (user_area_size < 0) |
2973 | ftdi_error_return(-1,"eeprom size exceeded"); | |
b8aa7b35 TJ |
2974 | |
2975 | // empty eeprom | |
74387f27 TJ |
2976 | if (ftdi->type == TYPE_230X) |
2977 | { | |
2f80efc2 NP |
2978 | /* FT230X have a reserved section in the middle of the MTP, |
2979 | which cannot be written to, but must be included in the checksum */ | |
2980 | memset(ftdi->eeprom->buf, 0, 0x80); | |
2981 | memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0)); | |
74387f27 TJ |
2982 | } |
2983 | else | |
2984 | { | |
2f80efc2 NP |
2985 | memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE); |
2986 | } | |
b8aa7b35 | 2987 | |
93738c79 UB |
2988 | // Bytes and Bits set for all Types |
2989 | ||
b8aa7b35 TJ |
2990 | // Addr 02: Vendor ID |
2991 | output[0x02] = eeprom->vendor_id; | |
2992 | output[0x03] = eeprom->vendor_id >> 8; | |
2993 | ||
2994 | // Addr 04: Product ID | |
2995 | output[0x04] = eeprom->product_id; | |
2996 | output[0x05] = eeprom->product_id >> 8; | |
2997 | ||
2998 | // Addr 06: Device release number (0400h for BM features) | |
68e78641 JS |
2999 | output[0x06] = eeprom->release_number; |
3000 | output[0x07] = eeprom->release_number >> 8; | |
b8aa7b35 TJ |
3001 | |
3002 | // Addr 08: Config descriptor | |
8fae3e8e TJ |
3003 | // Bit 7: always 1 |
3004 | // Bit 6: 1 if this device is self powered, 0 if bus powered | |
3005 | // Bit 5: 1 if this device uses remote wakeup | |
37186e34 | 3006 | // Bit 4-0: reserved - 0 |
5a1dcd55 | 3007 | j = 0x80; |
afb90824 | 3008 | if (eeprom->self_powered) |
5a1dcd55 | 3009 | j |= 0x40; |
afb90824 | 3010 | if (eeprom->remote_wakeup) |
5a1dcd55 | 3011 | j |= 0x20; |
b8aa7b35 TJ |
3012 | output[0x08] = j; |
3013 | ||
3014 | // Addr 09: Max power consumption: max power = value * 2 mA | |
a7c32c59 | 3015 | output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT; |
d9f0cce7 | 3016 | |
2f80efc2 | 3017 | if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X)) |
93738c79 UB |
3018 | { |
3019 | // Addr 0A: Chip configuration | |
3020 | // Bit 7: 0 - reserved | |
3021 | // Bit 6: 0 - reserved | |
3022 | // Bit 5: 0 - reserved | |
56ac0383 | 3023 | // Bit 4: 1 - Change USB version |
93738c79 UB |
3024 | // Bit 3: 1 - Use the serial number string |
3025 | // Bit 2: 1 - Enable suspend pull downs for lower power | |
3026 | // Bit 1: 1 - Out EndPoint is Isochronous | |
3027 | // Bit 0: 1 - In EndPoint is Isochronous | |
3028 | // | |
3029 | j = 0; | |
afb90824 | 3030 | if (eeprom->in_is_isochronous) |
93738c79 | 3031 | j = j | 1; |
afb90824 | 3032 | if (eeprom->out_is_isochronous) |
93738c79 UB |
3033 | j = j | 2; |
3034 | output[0x0A] = j; | |
3035 | } | |
f505134f | 3036 | |
b8aa7b35 | 3037 | // Dynamic content |
93738c79 UB |
3038 | // Strings start at 0x94 (TYPE_AM, TYPE_BM) |
3039 | // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H) | |
c7e4c09e | 3040 | // 0xa0 (TYPE_232H) |
93738c79 | 3041 | i = 0; |
56ac0383 TJ |
3042 | switch (ftdi->type) |
3043 | { | |
3044 | case TYPE_2232H: | |
3045 | case TYPE_4232H: | |
3046 | i += 2; | |
3047 | case TYPE_R: | |
3048 | i += 2; | |
3049 | case TYPE_2232C: | |
3050 | i += 2; | |
3051 | case TYPE_AM: | |
3052 | case TYPE_BM: | |
3053 | i += 0x94; | |
2f80efc2 | 3054 | break; |
fa3032f0 | 3055 | case TYPE_232H: |
2f80efc2 NP |
3056 | case TYPE_230X: |
3057 | i = 0xa0; | |
3058 | break; | |
f505134f | 3059 | } |
93738c79 | 3060 | /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */ |
e2bbd9af | 3061 | eeprom_size_mask = eeprom->size -1; |
6e962b9a | 3062 | free_end = i & eeprom_size_mask; |
c201f80f | 3063 | |
93738c79 UB |
3064 | // Addr 0E: Offset of the manufacturer string + 0x80, calculated later |
3065 | // Addr 0F: Length of manufacturer string | |
22d12cda | 3066 | // Output manufacturer |
93738c79 | 3067 | output[0x0E] = i; // calculate offset |
e2bbd9af TJ |
3068 | output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++; |
3069 | output[i & eeprom_size_mask] = 0x03, i++; // type: string | |
22d12cda TJ |
3070 | for (j = 0; j < manufacturer_size; j++) |
3071 | { | |
e2bbd9af TJ |
3072 | output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++; |
3073 | output[i & eeprom_size_mask] = 0x00, i++; | |
b8aa7b35 | 3074 | } |
93738c79 | 3075 | output[0x0F] = manufacturer_size*2 + 2; |
b8aa7b35 | 3076 | |
93738c79 UB |
3077 | // Addr 10: Offset of the product string + 0x80, calculated later |
3078 | // Addr 11: Length of product string | |
c201f80f | 3079 | output[0x10] = i | 0x80; // calculate offset |
e2bbd9af TJ |
3080 | output[i & eeprom_size_mask] = product_size*2 + 2, i++; |
3081 | output[i & eeprom_size_mask] = 0x03, i++; | |
22d12cda TJ |
3082 | for (j = 0; j < product_size; j++) |
3083 | { | |
e2bbd9af TJ |
3084 | output[i & eeprom_size_mask] = eeprom->product[j], i++; |
3085 | output[i & eeprom_size_mask] = 0x00, i++; | |
b8aa7b35 | 3086 | } |
93738c79 | 3087 | output[0x11] = product_size*2 + 2; |
37186e34 | 3088 | |
93738c79 UB |
3089 | // Addr 12: Offset of the serial string + 0x80, calculated later |
3090 | // Addr 13: Length of serial string | |
c201f80f | 3091 | output[0x12] = i | 0x80; // calculate offset |
e2bbd9af TJ |
3092 | output[i & eeprom_size_mask] = serial_size*2 + 2, i++; |
3093 | output[i & eeprom_size_mask] = 0x03, i++; | |
22d12cda TJ |
3094 | for (j = 0; j < serial_size; j++) |
3095 | { | |
e2bbd9af TJ |
3096 | output[i & eeprom_size_mask] = eeprom->serial[j], i++; |
3097 | output[i & eeprom_size_mask] = 0x00, i++; | |
b8aa7b35 | 3098 | } |
c2700d6d TJ |
3099 | |
3100 | // Legacy port name and PnP fields for FT2232 and newer chips | |
3101 | if (ftdi->type > TYPE_BM) | |
3102 | { | |
3103 | output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */ | |
3104 | i++; | |
3105 | output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */ | |
3106 | i++; | |
3107 | output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */ | |
3108 | i++; | |
3109 | } | |
802a949e | 3110 | |
93738c79 | 3111 | output[0x13] = serial_size*2 + 2; |
b8aa7b35 | 3112 | |
56ac0383 | 3113 | if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */ |
bf2f6ef7 | 3114 | { |
d4b5af27 | 3115 | if (eeprom->use_serial) |
bf2f6ef7 UB |
3116 | output[0x0A] |= USE_SERIAL_NUM; |
3117 | else | |
3118 | output[0x0A] &= ~USE_SERIAL_NUM; | |
3119 | } | |
3802140c UB |
3120 | |
3121 | /* Bytes and Bits specific to (some) types | |
3122 | Write linear, as this allows easier fixing*/ | |
56ac0383 TJ |
3123 | switch (ftdi->type) |
3124 | { | |
3125 | case TYPE_AM: | |
3126 | break; | |
3127 | case TYPE_BM: | |
3128 | output[0x0C] = eeprom->usb_version & 0xff; | |
3129 | output[0x0D] = (eeprom->usb_version>>8) & 0xff; | |
3986243d | 3130 | if (eeprom->use_usb_version) |
56ac0383 TJ |
3131 | output[0x0A] |= USE_USB_VERSION_BIT; |
3132 | else | |
3133 | output[0x0A] &= ~USE_USB_VERSION_BIT; | |
caec1294 | 3134 | |
56ac0383 TJ |
3135 | break; |
3136 | case TYPE_2232C: | |
3802140c | 3137 | |
c8f69686 | 3138 | output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C); |
46ed84b3 | 3139 | if (eeprom->channel_a_driver == DRIVER_VCP) |
56ac0383 TJ |
3140 | output[0x00] |= DRIVER_VCP; |
3141 | else | |
3142 | output[0x00] &= ~DRIVER_VCP; | |
4e74064b | 3143 | |
46ed84b3 | 3144 | if (eeprom->high_current_a) |
56ac0383 TJ |
3145 | output[0x00] |= HIGH_CURRENT_DRIVE; |
3146 | else | |
3147 | output[0x00] &= ~HIGH_CURRENT_DRIVE; | |
3802140c | 3148 | |
c8f69686 | 3149 | output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C); |
46ed84b3 | 3150 | if (eeprom->channel_b_driver == DRIVER_VCP) |
56ac0383 TJ |
3151 | output[0x01] |= DRIVER_VCP; |
3152 | else | |
3153 | output[0x01] &= ~DRIVER_VCP; | |
4e74064b | 3154 | |
46ed84b3 | 3155 | if (eeprom->high_current_b) |
56ac0383 TJ |
3156 | output[0x01] |= HIGH_CURRENT_DRIVE; |
3157 | else | |
3158 | output[0x01] &= ~HIGH_CURRENT_DRIVE; | |
3802140c | 3159 | |
afb90824 | 3160 | if (eeprom->in_is_isochronous) |
56ac0383 TJ |
3161 | output[0x0A] |= 0x1; |
3162 | else | |
3163 | output[0x0A] &= ~0x1; | |
afb90824 | 3164 | if (eeprom->out_is_isochronous) |
56ac0383 TJ |
3165 | output[0x0A] |= 0x2; |
3166 | else | |
3167 | output[0x0A] &= ~0x2; | |
afb90824 | 3168 | if (eeprom->suspend_pull_downs) |
56ac0383 TJ |
3169 | output[0x0A] |= 0x4; |
3170 | else | |
3171 | output[0x0A] &= ~0x4; | |
3986243d | 3172 | if (eeprom->use_usb_version) |
56ac0383 TJ |
3173 | output[0x0A] |= USE_USB_VERSION_BIT; |
3174 | else | |
3175 | output[0x0A] &= ~USE_USB_VERSION_BIT; | |
4e74064b | 3176 | |
56ac0383 TJ |
3177 | output[0x0C] = eeprom->usb_version & 0xff; |
3178 | output[0x0D] = (eeprom->usb_version>>8) & 0xff; | |
3179 | output[0x14] = eeprom->chip; | |
3180 | break; | |
3181 | case TYPE_R: | |
6f9f969d | 3182 | output[0x00] = type2bit(eeprom->channel_a_type, TYPE_R); |
46ed84b3 | 3183 | if (eeprom->high_current) |
56ac0383 | 3184 | output[0x00] |= HIGH_CURRENT_DRIVE_R; |
4296ba2a H |
3185 | if (eeprom->channel_a_driver == DRIVER_VCP) |
3186 | output[0x00] |= DRIVER_VCP; | |
08518f8e RA |
3187 | if (eeprom->external_oscillator) |
3188 | output[0x00] |= 0x02; | |
56ac0383 | 3189 | output[0x01] = 0x40; /* Hard coded Endpoint Size*/ |
4e74064b | 3190 | |
afb90824 | 3191 | if (eeprom->suspend_pull_downs) |
56ac0383 TJ |
3192 | output[0x0A] |= 0x4; |
3193 | else | |
3194 | output[0x0A] &= ~0x4; | |
3195 | output[0x0B] = eeprom->invert; | |
3196 | output[0x0C] = eeprom->usb_version & 0xff; | |
3197 | output[0x0D] = (eeprom->usb_version>>8) & 0xff; | |
4e74064b | 3198 | |
add00ad6 | 3199 | if (eeprom->cbus_function[0] > CBUS_BB_RD) |
56ac0383 TJ |
3200 | output[0x14] = CBUS_TXLED; |
3201 | else | |
3202 | output[0x14] = eeprom->cbus_function[0]; | |
4e74064b | 3203 | |
add00ad6 | 3204 | if (eeprom->cbus_function[1] > CBUS_BB_RD) |
56ac0383 TJ |
3205 | output[0x14] |= CBUS_RXLED<<4; |
3206 | else | |
3207 | output[0x14] |= eeprom->cbus_function[1]<<4; | |
4e74064b | 3208 | |
add00ad6 | 3209 | if (eeprom->cbus_function[2] > CBUS_BB_RD) |
56ac0383 TJ |
3210 | output[0x15] = CBUS_TXDEN; |
3211 | else | |
3212 | output[0x15] = eeprom->cbus_function[2]; | |
4e74064b | 3213 | |
add00ad6 | 3214 | if (eeprom->cbus_function[3] > CBUS_BB_RD) |
56ac0383 TJ |
3215 | output[0x15] |= CBUS_PWREN<<4; |
3216 | else | |
3217 | output[0x15] |= eeprom->cbus_function[3]<<4; | |
4e74064b | 3218 | |
56ac0383 TJ |
3219 | if (eeprom->cbus_function[4] > CBUS_CLK6) |
3220 | output[0x16] = CBUS_SLEEP; | |
3221 | else | |
3222 | output[0x16] = eeprom->cbus_function[4]; | |
3223 | break; | |
3224 | case TYPE_2232H: | |
c8f69686 | 3225 | output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H); |
56ac0383 TJ |
3226 | if ( eeprom->channel_a_driver == DRIVER_VCP) |
3227 | output[0x00] |= DRIVER_VCP; | |
3228 | else | |
3229 | output[0x00] &= ~DRIVER_VCP; | |
6e6a1c3f | 3230 | |
c8f69686 | 3231 | output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H); |
56ac0383 TJ |
3232 | if ( eeprom->channel_b_driver == DRIVER_VCP) |
3233 | output[0x01] |= DRIVER_VCP; | |
3234 | else | |
3235 | output[0x01] &= ~DRIVER_VCP; | |
3236 | if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT) | |
3237 | output[0x01] |= SUSPEND_DBUS7_BIT; | |
3238 | else | |
3239 | output[0x01] &= ~SUSPEND_DBUS7_BIT; | |
3240 | ||
afb90824 | 3241 | if (eeprom->suspend_pull_downs) |
56ac0383 TJ |
3242 | output[0x0A] |= 0x4; |
3243 | else | |
3244 | output[0x0A] &= ~0x4; | |
3245 | ||
3246 | if (eeprom->group0_drive > DRIVE_16MA) | |
3247 | output[0x0c] |= DRIVE_16MA; | |
3248 | else | |
3249 | output[0x0c] |= eeprom->group0_drive; | |
3250 | if (eeprom->group0_schmitt == IS_SCHMITT) | |
3251 | output[0x0c] |= IS_SCHMITT; | |
3252 | if (eeprom->group0_slew == SLOW_SLEW) | |
3253 | output[0x0c] |= SLOW_SLEW; | |
3254 | ||
3255 | if (eeprom->group1_drive > DRIVE_16MA) | |
3256 | output[0x0c] |= DRIVE_16MA<<4; | |
3257 | else | |
3258 | output[0x0c] |= eeprom->group1_drive<<4; | |
3259 | if (eeprom->group1_schmitt == IS_SCHMITT) | |
3260 | output[0x0c] |= IS_SCHMITT<<4; | |
3261 | if (eeprom->group1_slew == SLOW_SLEW) | |
3262 | output[0x0c] |= SLOW_SLEW<<4; | |
3263 | ||
3264 | if (eeprom->group2_drive > DRIVE_16MA) | |
3265 | output[0x0d] |= DRIVE_16MA; | |
3266 | else | |
3267 | output[0x0d] |= eeprom->group2_drive; | |
3268 | if (eeprom->group2_schmitt == IS_SCHMITT) | |
3269 | output[0x0d] |= IS_SCHMITT; | |
3270 | if (eeprom->group2_slew == SLOW_SLEW) | |
3271 | output[0x0d] |= SLOW_SLEW; | |
3272 | ||
3273 | if (eeprom->group3_drive > DRIVE_16MA) | |
3274 | output[0x0d] |= DRIVE_16MA<<4; | |
3275 | else | |
3276 | output[0x0d] |= eeprom->group3_drive<<4; | |
3277 | if (eeprom->group3_schmitt == IS_SCHMITT) | |
3278 | output[0x0d] |= IS_SCHMITT<<4; | |
3279 | if (eeprom->group3_slew == SLOW_SLEW) | |
3280 | output[0x0d] |= SLOW_SLEW<<4; | |
3802140c | 3281 | |
56ac0383 | 3282 | output[0x18] = eeprom->chip; |
3802140c | 3283 | |
56ac0383 TJ |
3284 | break; |
3285 | case TYPE_4232H: | |
be4bae37 AL |
3286 | if (eeprom->channel_a_driver == DRIVER_VCP) |
3287 | output[0x00] |= DRIVER_VCP; | |
3288 | else | |
3289 | output[0x00] &= ~DRIVER_VCP; | |
3290 | if (eeprom->channel_b_driver == DRIVER_VCP) | |
3291 | output[0x01] |= DRIVER_VCP; | |
3292 | else | |
3293 | output[0x01] &= ~DRIVER_VCP; | |
3294 | if (eeprom->channel_c_driver == DRIVER_VCP) | |
3295 | output[0x00] |= (DRIVER_VCP << 4); | |
3296 | else | |
3297 | output[0x00] &= ~(DRIVER_VCP << 4); | |
3298 | if (eeprom->channel_d_driver == DRIVER_VCP) | |
3299 | output[0x01] |= (DRIVER_VCP << 4); | |
3300 | else | |
3301 | output[0x01] &= ~(DRIVER_VCP << 4); | |
3302 | ||
afb90824 | 3303 | if (eeprom->suspend_pull_downs) |
be4bae37 AL |
3304 | output[0x0a] |= 0x4; |
3305 | else | |
3306 | output[0x0a] &= ~0x4; | |
3307 | ||
3308 | if (eeprom->channel_a_rs485enable) | |
3309 | output[0x0b] |= CHANNEL_IS_RS485 << 0; | |
3310 | else | |
3311 | output[0x0b] &= ~(CHANNEL_IS_RS485 << 0); | |
3312 | if (eeprom->channel_b_rs485enable) | |
3313 | output[0x0b] |= CHANNEL_IS_RS485 << 1; | |
3314 | else | |
3315 | output[0x0b] &= ~(CHANNEL_IS_RS485 << 1); | |
3316 | if (eeprom->channel_c_rs485enable) | |
3317 | output[0x0b] |= CHANNEL_IS_RS485 << 2; | |
3318 | else | |
3319 | output[0x0b] &= ~(CHANNEL_IS_RS485 << 2); | |
3320 | if (eeprom->channel_d_rs485enable) | |
3321 | output[0x0b] |= CHANNEL_IS_RS485 << 3; | |
3322 | else | |
3323 | output[0x0b] &= ~(CHANNEL_IS_RS485 << 3); | |
3324 | ||
3325 | if (eeprom->group0_drive > DRIVE_16MA) | |
3326 | output[0x0c] |= DRIVE_16MA; | |
3327 | else | |
3328 | output[0x0c] |= eeprom->group0_drive; | |
3329 | if (eeprom->group0_schmitt == IS_SCHMITT) | |
3330 | output[0x0c] |= IS_SCHMITT; | |
3331 | if (eeprom->group0_slew == SLOW_SLEW) | |
3332 | output[0x0c] |= SLOW_SLEW; | |
3333 | ||
3334 | if (eeprom->group1_drive > DRIVE_16MA) | |
3335 | output[0x0c] |= DRIVE_16MA<<4; | |
3336 | else | |
3337 | output[0x0c] |= eeprom->group1_drive<<4; | |
3338 | if (eeprom->group1_schmitt == IS_SCHMITT) | |
3339 | output[0x0c] |= IS_SCHMITT<<4; | |
3340 | if (eeprom->group1_slew == SLOW_SLEW) | |
3341 | output[0x0c] |= SLOW_SLEW<<4; | |
3342 | ||
3343 | if (eeprom->group2_drive > DRIVE_16MA) | |
3344 | output[0x0d] |= DRIVE_16MA; | |
3345 | else | |
3346 | output[0x0d] |= eeprom->group2_drive; | |
3347 | if (eeprom->group2_schmitt == IS_SCHMITT) | |
3348 | output[0x0d] |= IS_SCHMITT; | |
3349 | if (eeprom->group2_slew == SLOW_SLEW) | |
3350 | output[0x0d] |= SLOW_SLEW; | |
3351 | ||
3352 | if (eeprom->group3_drive > DRIVE_16MA) | |
3353 | output[0x0d] |= DRIVE_16MA<<4; | |
3354 | else | |
3355 | output[0x0d] |= eeprom->group3_drive<<4; | |
3356 | if (eeprom->group3_schmitt == IS_SCHMITT) | |
3357 | output[0x0d] |= IS_SCHMITT<<4; | |
3358 | if (eeprom->group3_slew == SLOW_SLEW) | |
3359 | output[0x0d] |= SLOW_SLEW<<4; | |
3360 | ||
c7e4c09e | 3361 | output[0x18] = eeprom->chip; |
be4bae37 | 3362 | |
c7e4c09e UB |
3363 | break; |
3364 | case TYPE_232H: | |
c8f69686 | 3365 | output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H); |
ac4a82a5 UB |
3366 | if ( eeprom->channel_a_driver == DRIVER_VCP) |
3367 | output[0x00] |= DRIVER_VCPH; | |
3368 | else | |
3369 | output[0x00] &= ~DRIVER_VCPH; | |
837a71d6 UB |
3370 | if (eeprom->powersave) |
3371 | output[0x01] |= POWER_SAVE_DISABLE_H; | |
3372 | else | |
3373 | output[0x01] &= ~POWER_SAVE_DISABLE_H; | |
a7e05353 DM |
3374 | |
3375 | if (eeprom->suspend_pull_downs) | |
3376 | output[0x0a] |= 0x4; | |
3377 | else | |
3378 | output[0x0a] &= ~0x4; | |
3379 | ||
18199b76 UB |
3380 | if (eeprom->clock_polarity) |
3381 | output[0x01] |= FT1284_CLK_IDLE_STATE; | |
3382 | else | |
3383 | output[0x01] &= ~FT1284_CLK_IDLE_STATE; | |
3384 | if (eeprom->data_order) | |
3385 | output[0x01] |= FT1284_DATA_LSB; | |
3386 | else | |
3387 | output[0x01] &= ~FT1284_DATA_LSB; | |
3388 | if (eeprom->flow_control) | |
3389 | output[0x01] |= FT1284_FLOW_CONTROL; | |
3390 | else | |
3391 | output[0x01] &= ~FT1284_FLOW_CONTROL; | |
91d7a201 UB |
3392 | if (eeprom->group0_drive > DRIVE_16MA) |
3393 | output[0x0c] |= DRIVE_16MA; | |
3394 | else | |
3395 | output[0x0c] |= eeprom->group0_drive; | |
3396 | if (eeprom->group0_schmitt == IS_SCHMITT) | |
3397 | output[0x0c] |= IS_SCHMITT; | |
3398 | if (eeprom->group0_slew == SLOW_SLEW) | |
3399 | output[0x0c] |= SLOW_SLEW; | |
3400 | ||
3401 | if (eeprom->group1_drive > DRIVE_16MA) | |
3402 | output[0x0d] |= DRIVE_16MA; | |
3403 | else | |
3404 | output[0x0d] |= eeprom->group1_drive; | |
3405 | if (eeprom->group1_schmitt == IS_SCHMITT) | |
3406 | output[0x0d] |= IS_SCHMITT; | |
3407 | if (eeprom->group1_slew == SLOW_SLEW) | |
3408 | output[0x0d] |= SLOW_SLEW; | |
3409 | ||
263d3ba0 UB |
3410 | set_ft232h_cbus(eeprom, output); |
3411 | ||
c7e4c09e UB |
3412 | output[0x1e] = eeprom->chip; |
3413 | fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n"); | |
3414 | break; | |
2f80efc2 NP |
3415 | case TYPE_230X: |
3416 | output[0x00] = 0x80; /* Actually, leave the default value */ | |
e659737a UB |
3417 | /*FIXME: Make DBUS & CBUS Control configurable*/ |
3418 | output[0x0c] = 0; /* DBUS drive 4mA, CBUS drive 4 mA like factory default */ | |
74387f27 TJ |
3419 | for (j = 0; j <= 6; j++) |
3420 | { | |
2f80efc2 NP |
3421 | output[0x1a + j] = eeprom->cbus_function[j]; |
3422 | } | |
347d87e5 | 3423 | output[0x0b] = eeprom->invert; |
2f80efc2 | 3424 | break; |
3802140c UB |
3425 | } |
3426 | ||
6e962b9a SET |
3427 | /* First address without use */ |
3428 | free_start = 0; | |
3429 | switch (ftdi->type) | |
3430 | { | |
3431 | case TYPE_230X: | |
3432 | free_start += 2; | |
3433 | case TYPE_232H: | |
3434 | free_start += 6; | |
3435 | case TYPE_2232H: | |
3436 | case TYPE_4232H: | |
3437 | free_start += 2; | |
3438 | case TYPE_R: | |
3439 | free_start += 2; | |
3440 | case TYPE_2232C: | |
3441 | free_start++; | |
3442 | case TYPE_AM: | |
3443 | case TYPE_BM: | |
3444 | free_start += 0x14; | |
3445 | } | |
3446 | ||
3447 | /* Arbitrary user data */ | |
3448 | if (eeprom->user_data && eeprom->user_data_size >= 0) | |
3449 | { | |
3450 | if (eeprom->user_data_addr < free_start) | |
3451 | fprintf(stderr,"Warning, user data starts inside the generated data!\n"); | |
3452 | if (eeprom->user_data_addr + eeprom->user_data_size >= free_end) | |
3453 | fprintf(stderr,"Warning, user data overlaps the strings area!\n"); | |
3454 | if (eeprom->user_data_addr + eeprom->user_data_size > eeprom->size) | |
3455 | ftdi_error_return(-1,"eeprom size exceeded"); | |
3456 | memcpy(output + eeprom->user_data_addr, eeprom->user_data, eeprom->user_data_size); | |
3457 | } | |
3458 | ||
cbf65673 | 3459 | // calculate checksum |
b8aa7b35 | 3460 | checksum = 0xAAAA; |
d9f0cce7 | 3461 | |
22d12cda TJ |
3462 | for (i = 0; i < eeprom->size/2-1; i++) |
3463 | { | |
74387f27 TJ |
3464 | if ((ftdi->type == TYPE_230X) && (i == 0x12)) |
3465 | { | |
2f80efc2 NP |
3466 | /* FT230X has a user section in the MTP which is not part of the checksum */ |
3467 | i = 0x40; | |
3468 | } | |
519bbce1 UB |
3469 | if ((ftdi->type == TYPE_230X) && (i >= 0x40) && (i < 0x50)) { |
3470 | uint16_t data; | |
3471 | if (ftdi_read_eeprom_location(ftdi, i, &data)) { | |
3472 | fprintf(stderr, "Reading Factory Configuration Data failed\n"); | |
3473 | i = 0x50; | |
3474 | } | |
3475 | value = data; | |
3476 | } | |
3477 | else { | |
3478 | value = output[i*2]; | |
3479 | value += output[(i*2)+1] << 8; | |
3480 | } | |
d9f0cce7 TJ |
3481 | checksum = value^checksum; |
3482 | checksum = (checksum << 1) | (checksum >> 15); | |
b8aa7b35 TJ |
3483 | } |
3484 | ||
c201f80f TJ |
3485 | output[eeprom->size-2] = checksum; |
3486 | output[eeprom->size-1] = checksum >> 8; | |
b8aa7b35 | 3487 | |
68e78641 | 3488 | eeprom->initialized_for_connected_device = 1; |
516ebfb1 | 3489 | return user_area_size; |
b8aa7b35 | 3490 | } |
74387f27 | 3491 | /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted |
c8f69686 UB |
3492 | * EEPROM structure |
3493 | * | |
3494 | * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we | |
3495 | */ | |
3496 | static unsigned char bit2type(unsigned char bits) | |
0fc2170c UB |
3497 | { |
3498 | switch (bits) | |
3499 | { | |
74387f27 TJ |
3500 | case 0: return CHANNEL_IS_UART; |
3501 | case 1: return CHANNEL_IS_FIFO; | |
3502 | case 2: return CHANNEL_IS_OPTO; | |
3503 | case 4: return CHANNEL_IS_CPU; | |
3504 | case 8: return CHANNEL_IS_FT1284; | |
3505 | default: | |
3506 | fprintf(stderr," Unexpected value %d for Hardware Interface type\n", | |
3507 | bits); | |
0fc2170c UB |
3508 | } |
3509 | return 0; | |
3510 | } | |
1ad9e4cc TJ |
3511 | /* Decode 230X / 232R type chips invert bits |
3512 | * Prints directly to stdout. | |
3513 | */ | |
3514 | static void print_inverted_bits(int invert) | |
3515 | { | |
c45d2630 | 3516 | const char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"}; |
1ad9e4cc TJ |
3517 | int i; |
3518 | ||
3519 | fprintf(stdout,"Inverted bits:"); | |
3520 | for (i=0; i<8; i++) | |
3521 | if ((invert & (1<<i)) == (1<<i)) | |
3522 | fprintf(stdout," %s",r_bits[i]); | |
3523 | ||
3524 | fprintf(stdout,"\n"); | |
3525 | } | |
4af1d1bb MK |
3526 | /** |
3527 | Decode binary EEPROM image into an ftdi_eeprom structure. | |
3528 | ||
e659737a UB |
3529 | For FT-X devices use AN_201 FT-X MTP memory Configuration to decode. |
3530 | ||
a35aa9bd UB |
3531 | \param ftdi pointer to ftdi_context |
3532 | \param verbose Decode EEPROM on stdout | |
56ac0383 | 3533 | |
4af1d1bb MK |
3534 | \retval 0: all fine |
3535 | \retval -1: something went wrong | |
3536 | ||
3537 | FIXME: How to pass size? How to handle size field in ftdi_eeprom? | |
3538 | FIXME: Strings are malloc'ed here and should be freed somewhere | |
3539 | */ | |
a35aa9bd | 3540 | int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose) |
b56d5a64 | 3541 | { |
3fca5ea9 | 3542 | int i, j; |
b56d5a64 MK |
3543 | unsigned short checksum, eeprom_checksum, value; |
3544 | unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0; | |
f2cd9fd5 | 3545 | int eeprom_size; |
c0a96aed | 3546 | struct ftdi_eeprom *eeprom; |
3bc0387e | 3547 | unsigned char *buf = NULL; |
22a1b5c1 | 3548 | |
c0a96aed | 3549 | if (ftdi == NULL) |
cc9c9d58 | 3550 | ftdi_error_return(-1,"No context"); |
c0a96aed | 3551 | if (ftdi->eeprom == NULL) |
6cd4f922 | 3552 | ftdi_error_return(-1,"No eeprom structure"); |
56ac0383 | 3553 | |
c0a96aed | 3554 | eeprom = ftdi->eeprom; |
a35aa9bd | 3555 | eeprom_size = eeprom->size; |
3bc0387e | 3556 | buf = ftdi->eeprom->buf; |
b56d5a64 | 3557 | |
b56d5a64 MK |
3558 | // Addr 02: Vendor ID |
3559 | eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8); | |
3560 | ||
3561 | // Addr 04: Product ID | |
3562 | eeprom->product_id = buf[0x04] + (buf[0x05] << 8); | |
22d12cda | 3563 | |
68e78641 JS |
3564 | // Addr 06: Device release number |
3565 | eeprom->release_number = buf[0x06] + (buf[0x07]<<8); | |
b56d5a64 MK |
3566 | |
3567 | // Addr 08: Config descriptor | |
3568 | // Bit 7: always 1 | |
3569 | // Bit 6: 1 if this device is self powered, 0 if bus powered | |
3570 | // Bit 5: 1 if this device uses remote wakeup | |
f6ef2983 | 3571 | eeprom->self_powered = buf[0x08] & 0x40; |
814710ba | 3572 | eeprom->remote_wakeup = buf[0x08] & 0x20; |
b56d5a64 MK |
3573 | |
3574 | // Addr 09: Max power consumption: max power = value * 2 mA | |
a7c32c59 | 3575 | eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09]; |
b56d5a64 MK |
3576 | |
3577 | // Addr 0A: Chip configuration | |
3578 | // Bit 7: 0 - reserved | |
3579 | // Bit 6: 0 - reserved | |
3580 | // Bit 5: 0 - reserved | |
caec1294 | 3581 | // Bit 4: 1 - Change USB version on BM and 2232C |
b56d5a64 MK |
3582 | // Bit 3: 1 - Use the serial number string |
3583 | // Bit 2: 1 - Enable suspend pull downs for lower power | |
3584 | // Bit 1: 1 - Out EndPoint is Isochronous | |
3585 | // Bit 0: 1 - In EndPoint is Isochronous | |
3586 | // | |
8d3fe5c9 UB |
3587 | eeprom->in_is_isochronous = buf[0x0A]&0x01; |
3588 | eeprom->out_is_isochronous = buf[0x0A]&0x02; | |
3589 | eeprom->suspend_pull_downs = buf[0x0A]&0x04; | |
3986243d TS |
3590 | eeprom->use_serial = !!(buf[0x0A] & USE_SERIAL_NUM); |
3591 | eeprom->use_usb_version = !!(buf[0x0A] & USE_USB_VERSION_BIT); | |
b56d5a64 | 3592 | |
b1859923 | 3593 | // Addr 0C: USB version low byte when 0x0A |
56ac0383 | 3594 | // Addr 0D: USB version high byte when 0x0A |
b1859923 | 3595 | eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8); |
b56d5a64 MK |
3596 | |
3597 | // Addr 0E: Offset of the manufacturer string + 0x80, calculated later | |
3598 | // Addr 0F: Length of manufacturer string | |
3599 | manufacturer_size = buf[0x0F]/2; | |
56ac0383 | 3600 | if (eeprom->manufacturer) |
74e8e79d | 3601 | free(eeprom->manufacturer); |
56ac0383 | 3602 | if (manufacturer_size > 0) |
acc1fa05 | 3603 | { |
c45d2630 | 3604 | eeprom->manufacturer = (char *)malloc(manufacturer_size); |
acc1fa05 UB |
3605 | if (eeprom->manufacturer) |
3606 | { | |
3607 | // Decode manufacturer | |
84ec032f | 3608 | i = buf[0x0E] & (eeprom_size -1); // offset |
74387f27 | 3609 | for (j=0; j<manufacturer_size-1; j++) |
acc1fa05 UB |
3610 | { |
3611 | eeprom->manufacturer[j] = buf[2*j+i+2]; | |
3612 | } | |
3613 | eeprom->manufacturer[j] = '\0'; | |
3614 | } | |
3615 | } | |
b56d5a64 MK |
3616 | else eeprom->manufacturer = NULL; |
3617 | ||
3618 | // Addr 10: Offset of the product string + 0x80, calculated later | |
3619 | // Addr 11: Length of product string | |
56ac0383 | 3620 | if (eeprom->product) |
74e8e79d | 3621 | free(eeprom->product); |
b56d5a64 | 3622 | product_size = buf[0x11]/2; |
acc1fa05 UB |
3623 | if (product_size > 0) |
3624 | { | |
c45d2630 | 3625 | eeprom->product = (char *)malloc(product_size); |
56ac0383 | 3626 | if (eeprom->product) |
acc1fa05 UB |
3627 | { |
3628 | // Decode product name | |
84ec032f | 3629 | i = buf[0x10] & (eeprom_size -1); // offset |
74387f27 | 3630 | for (j=0; j<product_size-1; j++) |
acc1fa05 UB |
3631 | { |
3632 | eeprom->product[j] = buf[2*j+i+2]; | |
3633 | } | |
3634 | eeprom->product[j] = '\0'; | |
3635 | } | |
3636 | } | |
b56d5a64 MK |
3637 | else eeprom->product = NULL; |
3638 | ||
3639 | // Addr 12: Offset of the serial string + 0x80, calculated later | |
3640 | // Addr 13: Length of serial string | |
56ac0383 | 3641 | if (eeprom->serial) |
74e8e79d | 3642 | free(eeprom->serial); |
b56d5a64 | 3643 | serial_size = buf[0x13]/2; |
acc1fa05 UB |
3644 | if (serial_size > 0) |
3645 | { | |
c45d2630 | 3646 | eeprom->serial = (char *)malloc(serial_size); |
56ac0383 | 3647 | if (eeprom->serial) |
acc1fa05 UB |
3648 | { |
3649 | // Decode serial | |
84ec032f | 3650 | i = buf[0x12] & (eeprom_size -1); // offset |
74387f27 | 3651 | for (j=0; j<serial_size-1; j++) |
acc1fa05 UB |
3652 | { |
3653 | eeprom->serial[j] = buf[2*j+i+2]; | |
3654 | } | |
3655 | eeprom->serial[j] = '\0'; | |
3656 | } | |
3657 | } | |
b56d5a64 MK |
3658 | else eeprom->serial = NULL; |
3659 | ||
b56d5a64 MK |
3660 | // verify checksum |
3661 | checksum = 0xAAAA; | |
3662 | ||
22d12cda TJ |
3663 | for (i = 0; i < eeprom_size/2-1; i++) |
3664 | { | |
74387f27 TJ |
3665 | if ((ftdi->type == TYPE_230X) && (i == 0x12)) |
3666 | { | |
2f80efc2 NP |
3667 | /* FT230X has a user section in the MTP which is not part of the checksum */ |
3668 | i = 0x40; | |
3669 | } | |
b56d5a64 MK |
3670 | value = buf[i*2]; |
3671 | value += buf[(i*2)+1] << 8; | |
3672 | ||
3673 | checksum = value^checksum; | |
3674 | checksum = (checksum << 1) | (checksum >> 15); | |
3675 | } | |
3676 | ||
3677 | eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8); | |
3678 | ||
22d12cda TJ |
3679 | if (eeprom_checksum != checksum) |
3680 | { | |
3681 | fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum); | |
cc9c9d58 | 3682 | ftdi_error_return(-1,"EEPROM checksum error"); |
4af1d1bb MK |
3683 | } |
3684 | ||
eb498cff | 3685 | eeprom->channel_a_type = 0; |
aa099f46 | 3686 | if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM)) |
f6ef2983 | 3687 | { |
6cd4f922 | 3688 | eeprom->chip = -1; |
f6ef2983 | 3689 | } |
56ac0383 | 3690 | else if (ftdi->type == TYPE_2232C) |
f6ef2983 | 3691 | { |
0fc2170c | 3692 | eeprom->channel_a_type = bit2type(buf[0x00] & 0x7); |
2cde7c52 UB |
3693 | eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP; |
3694 | eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE; | |
3695 | eeprom->channel_b_type = buf[0x01] & 0x7; | |
3696 | eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP; | |
3697 | eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE; | |
6cd4f922 | 3698 | eeprom->chip = buf[0x14]; |
065edc58 | 3699 | } |
56ac0383 | 3700 | else if (ftdi->type == TYPE_R) |
564b2716 | 3701 | { |
2cde7c52 | 3702 | /* TYPE_R flags D2XX, not VCP as all others*/ |
be4bae37 | 3703 | eeprom->channel_a_driver = ~buf[0x00] & DRIVER_VCP; |
2cde7c52 | 3704 | eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R; |
08518f8e | 3705 | eeprom->external_oscillator = buf[0x00] & 0x02; |
56ac0383 TJ |
3706 | if ( (buf[0x01]&0x40) != 0x40) |
3707 | fprintf(stderr, | |
3708 | "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size." | |
3709 | " If this happened with the\n" | |
3710 | " EEPROM programmed by FTDI tools, please report " | |
3711 | "to libftdi@developer.intra2net.com\n"); | |
2cde7c52 | 3712 | |
6cd4f922 | 3713 | eeprom->chip = buf[0x16]; |
cecb9cb2 UB |
3714 | // Addr 0B: Invert data lines |
3715 | // Works only on FT232R, not FT245R, but no way to distinguish | |
07851949 UB |
3716 | eeprom->invert = buf[0x0B]; |
3717 | // Addr 14: CBUS function: CBUS0, CBUS1 | |
3718 | // Addr 15: CBUS function: CBUS2, CBUS3 | |
3719 | // Addr 16: CBUS function: CBUS5 | |
3720 | eeprom->cbus_function[0] = buf[0x14] & 0x0f; | |
3721 | eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f; | |
3722 | eeprom->cbus_function[2] = buf[0x15] & 0x0f; | |
3723 | eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f; | |
3724 | eeprom->cbus_function[4] = buf[0x16] & 0x0f; | |
564b2716 | 3725 | } |
be4bae37 | 3726 | else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H)) |
db099ec5 | 3727 | { |
2cde7c52 | 3728 | eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP; |
2cde7c52 UB |
3729 | eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP; |
3730 | ||
56ac0383 | 3731 | if (ftdi->type == TYPE_2232H) |
be4bae37 AL |
3732 | { |
3733 | eeprom->channel_a_type = bit2type(buf[0x00] & 0x7); | |
3734 | eeprom->channel_b_type = bit2type(buf[0x01] & 0x7); | |
ec0dcd3f | 3735 | eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT; |
be4bae37 AL |
3736 | } |
3737 | else | |
3738 | { | |
3739 | eeprom->channel_c_driver = (buf[0x00] >> 4) & DRIVER_VCP; | |
3740 | eeprom->channel_d_driver = (buf[0x01] >> 4) & DRIVER_VCP; | |
3741 | eeprom->channel_a_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 0); | |
3742 | eeprom->channel_b_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 1); | |
3743 | eeprom->channel_c_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 2); | |
3744 | eeprom->channel_d_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 3); | |
3745 | } | |
2cde7c52 | 3746 | |
6cd4f922 | 3747 | eeprom->chip = buf[0x18]; |
db099ec5 UB |
3748 | eeprom->group0_drive = buf[0x0c] & DRIVE_16MA; |
3749 | eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT; | |
3750 | eeprom->group0_slew = buf[0x0c] & SLOW_SLEW; | |
3751 | eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3; | |
3752 | eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT; | |
3753 | eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW; | |
3754 | eeprom->group2_drive = buf[0x0d] & DRIVE_16MA; | |
3755 | eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT; | |
3756 | eeprom->group2_slew = buf[0x0d] & SLOW_SLEW; | |
3757 | eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA; | |
3758 | eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT; | |
3759 | eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW; | |
947d9552 | 3760 | } |
c7e4c09e UB |
3761 | else if (ftdi->type == TYPE_232H) |
3762 | { | |
ac4a82a5 UB |
3763 | eeprom->channel_a_type = buf[0x00] & 0xf; |
3764 | eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0; | |
18199b76 UB |
3765 | eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE; |
3766 | eeprom->data_order = buf[0x01] & FT1284_DATA_LSB; | |
3767 | eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL; | |
837a71d6 | 3768 | eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H; |
91d7a201 UB |
3769 | eeprom->group0_drive = buf[0x0c] & DRIVE_16MA; |
3770 | eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT; | |
3771 | eeprom->group0_slew = buf[0x0c] & SLOW_SLEW; | |
3772 | eeprom->group1_drive = buf[0x0d] & DRIVE_16MA; | |
3773 | eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT; | |
3774 | eeprom->group1_slew = buf[0x0d] & SLOW_SLEW; | |
3775 | ||
263d3ba0 UB |
3776 | for(i=0; i<5; i++) |
3777 | { | |
3778 | eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f; | |
3779 | eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f; | |
3780 | } | |
c7e4c09e UB |
3781 | eeprom->chip = buf[0x1e]; |
3782 | /*FIXME: Decipher more values*/ | |
3783 | } | |
2f80efc2 NP |
3784 | else if (ftdi->type == TYPE_230X) |
3785 | { | |
74387f27 TJ |
3786 | for(i=0; i<4; i++) |
3787 | { | |
2f80efc2 NP |
3788 | eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF; |
3789 | } | |
3790 | eeprom->group0_drive = buf[0x0c] & 0x03; | |
3791 | eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT; | |
3792 | eeprom->group0_slew = buf[0x0c] & SLOW_SLEW; | |
3793 | eeprom->group1_drive = (buf[0x0c] >> 4) & 0x03; | |
3794 | eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT; | |
3795 | eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW; | |
34b79ac7 | 3796 | |
347d87e5 | 3797 | eeprom->invert = buf[0xb]; |
2f80efc2 | 3798 | } |
56ac0383 TJ |
3799 | |
3800 | if (verbose) | |
f6ef2983 | 3801 | { |
c45d2630 | 3802 | const char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"}; |
f6ef2983 UB |
3803 | fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id); |
3804 | fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id); | |
68e78641 | 3805 | fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number); |
f6ef2983 | 3806 | |
56ac0383 | 3807 | if (eeprom->self_powered) |
f6ef2983 UB |
3808 | fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n"); |
3809 | else | |
a7c32c59 | 3810 | fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power, |
f6ef2983 | 3811 | (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n"); |
56ac0383 | 3812 | if (eeprom->manufacturer) |
f6ef2983 | 3813 | fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer); |
56ac0383 | 3814 | if (eeprom->product) |
f6ef2983 | 3815 | fprintf(stdout, "Product: %s\n",eeprom->product); |
56ac0383 | 3816 | if (eeprom->serial) |
f6ef2983 | 3817 | fprintf(stdout, "Serial: %s\n",eeprom->serial); |
e107f509 | 3818 | fprintf(stdout, "Checksum : %04x\n", checksum); |
08518f8e | 3819 | if (ftdi->type == TYPE_R) { |
6cd4f922 | 3820 | fprintf(stdout, "Internal EEPROM\n"); |
08518f8e RA |
3821 | fprintf(stdout,"Oscillator: %s\n", eeprom->external_oscillator?"External":"Internal"); |
3822 | } | |
6cd4f922 UB |
3823 | else if (eeprom->chip >= 0x46) |
3824 | fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip); | |
56ac0383 TJ |
3825 | if (eeprom->suspend_dbus7) |
3826 | fprintf(stdout, "Suspend on DBUS7\n"); | |
3827 | if (eeprom->suspend_pull_downs) | |
fb9bfdd1 | 3828 | fprintf(stdout, "Pull IO pins low during suspend\n"); |
837a71d6 UB |
3829 | if(eeprom->powersave) |
3830 | { | |
3831 | if(ftdi->type >= TYPE_232H) | |
3832 | fprintf(stdout,"Enter low power state on ACBUS7\n"); | |
74387f27 | 3833 | } |
56ac0383 | 3834 | if (eeprom->remote_wakeup) |
fb9bfdd1 | 3835 | fprintf(stdout, "Enable Remote Wake Up\n"); |
802a949e | 3836 | fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1); |
db099ec5 | 3837 | if (ftdi->type >= TYPE_2232C) |
56ac0383 | 3838 | fprintf(stdout,"Channel A has Mode %s%s%s\n", |
e107f509 | 3839 | channel_mode[eeprom->channel_a_type], |
2cde7c52 UB |
3840 | (eeprom->channel_a_driver)?" VCP":"", |
3841 | (eeprom->high_current_a)?" High Current IO":""); | |
f45f4237 | 3842 | if (ftdi->type == TYPE_232H) |
18199b76 UB |
3843 | { |
3844 | fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n", | |
3845 | (eeprom->clock_polarity)?"HIGH":"LOW", | |
3846 | (eeprom->data_order)?"LSB":"MSB", | |
3847 | (eeprom->flow_control)?"":"No "); | |
74387f27 | 3848 | } |
f45f4237 | 3849 | if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H)) |
56ac0383 | 3850 | fprintf(stdout,"Channel B has Mode %s%s%s\n", |
e107f509 | 3851 | channel_mode[eeprom->channel_b_type], |
2cde7c52 UB |
3852 | (eeprom->channel_b_driver)?" VCP":"", |
3853 | (eeprom->high_current_b)?" High Current IO":""); | |
caec1294 | 3854 | if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) && |
3986243d | 3855 | eeprom->use_usb_version) |
caec1294 UB |
3856 | fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version); |
3857 | ||
56ac0383 | 3858 | if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H)) |
db099ec5 UB |
3859 | { |
3860 | fprintf(stdout,"%s has %d mA drive%s%s\n", | |
3861 | (ftdi->type == TYPE_2232H)?"AL":"A", | |
3862 | (eeprom->group0_drive+1) *4, | |
3863 | (eeprom->group0_schmitt)?" Schmitt Input":"", | |
3864 | (eeprom->group0_slew)?" Slow Slew":""); | |
3865 | fprintf(stdout,"%s has %d mA drive%s%s\n", | |
3866 | (ftdi->type == TYPE_2232H)?"AH":"B", | |
3867 | (eeprom->group1_drive+1) *4, | |
3868 | (eeprom->group1_schmitt)?" Schmitt Input":"", | |
3869 | (eeprom->group1_slew)?" Slow Slew":""); | |
3870 | fprintf(stdout,"%s has %d mA drive%s%s\n", | |
3871 | (ftdi->type == TYPE_2232H)?"BL":"C", | |
3872 | (eeprom->group2_drive+1) *4, | |
3873 | (eeprom->group2_schmitt)?" Schmitt Input":"", | |
3874 | (eeprom->group2_slew)?" Slow Slew":""); | |
3875 | fprintf(stdout,"%s has %d mA drive%s%s\n", | |
3876 | (ftdi->type == TYPE_2232H)?"BH":"D", | |
3877 | (eeprom->group3_drive+1) *4, | |
3878 | (eeprom->group3_schmitt)?" Schmitt Input":"", | |
3879 | (eeprom->group3_slew)?" Slow Slew":""); | |
3880 | } | |
91d7a201 UB |
3881 | else if (ftdi->type == TYPE_232H) |
3882 | { | |
c45d2630 | 3883 | const char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN", |
74387f27 TJ |
3884 | "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN", |
3885 | "CLK30","CLK15","CLK7_5" | |
3886 | }; | |
91d7a201 UB |
3887 | fprintf(stdout,"ACBUS has %d mA drive%s%s\n", |
3888 | (eeprom->group0_drive+1) *4, | |
3889 | (eeprom->group0_schmitt)?" Schmitt Input":"", | |
3890 | (eeprom->group0_slew)?" Slow Slew":""); | |
3891 | fprintf(stdout,"ADBUS has %d mA drive%s%s\n", | |
3892 | (eeprom->group1_drive+1) *4, | |
3893 | (eeprom->group1_schmitt)?" Schmitt Input":"", | |
3894 | (eeprom->group1_slew)?" Slow Slew":""); | |
263d3ba0 UB |
3895 | for (i=0; i<10; i++) |
3896 | { | |
3897 | if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 ) | |
3898 | fprintf(stdout,"C%d Function: %s\n", i, | |
3899 | cbush_mux[eeprom->cbus_function[i]]); | |
3900 | } | |
91d7a201 | 3901 | } |
2f80efc2 NP |
3902 | else if (ftdi->type == TYPE_230X) |
3903 | { | |
c45d2630 | 3904 | const char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN", |
74387f27 TJ |
3905 | "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN", |
3906 | "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#", | |
3907 | "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#", | |
3908 | "BBRD#", "TIME_STAMP", "AWAKE#", | |
3909 | }; | |
f45f4237 | 3910 | fprintf(stdout,"DBUS has %d mA drive%s%s\n", |
2f80efc2 NP |
3911 | (eeprom->group0_drive+1) *4, |
3912 | (eeprom->group0_schmitt)?" Schmitt Input":"", | |
3913 | (eeprom->group0_slew)?" Slow Slew":""); | |
3914 | fprintf(stdout,"CBUS has %d mA drive%s%s\n", | |
3915 | (eeprom->group1_drive+1) *4, | |
3916 | (eeprom->group1_schmitt)?" Schmitt Input":"", | |
3917 | (eeprom->group1_slew)?" Slow Slew":""); | |
3918 | for (i=0; i<4; i++) | |
3919 | { | |
add00ad6 RH |
3920 | if (eeprom->cbus_function[i]<= CBUSX_AWAKE) |
3921 | fprintf(stdout,"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->cbus_function[i]]); | |
2f80efc2 | 3922 | } |
1ad9e4cc TJ |
3923 | |
3924 | if (eeprom->invert) | |
3925 | print_inverted_bits(eeprom->invert); | |
2f80efc2 | 3926 | } |
91d7a201 | 3927 | |
a4980043 UB |
3928 | if (ftdi->type == TYPE_R) |
3929 | { | |
c45d2630 | 3930 | const char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED", |
13f00d3c | 3931 | "SLEEP","CLK48","CLK24","CLK12","CLK6", |
56ac0383 TJ |
3932 | "IOMODE","BB_WR","BB_RD" |
3933 | }; | |
c45d2630 | 3934 | const char *cbus_BB[] = {"RXF","TXE","RD", "WR"}; |
56ac0383 TJ |
3935 | |
3936 | if (eeprom->invert) | |
1ad9e4cc | 3937 | print_inverted_bits(eeprom->invert); |
13ea50d2 | 3938 | |
56ac0383 | 3939 | for (i=0; i<5; i++) |
a4980043 | 3940 | { |
add00ad6 | 3941 | if (eeprom->cbus_function[i]<=CBUS_BB_RD) |
a4980043 UB |
3942 | fprintf(stdout,"C%d Function: %s\n", i, |
3943 | cbus_mux[eeprom->cbus_function[i]]); | |
3944 | else | |
17431287 | 3945 | { |
598b2334 UB |
3946 | if (i < 4) |
3947 | /* Running MPROG show that C0..3 have fixed function Synchronous | |
3948 | Bit Bang mode */ | |
3949 | fprintf(stdout,"C%d BB Function: %s\n", i, | |
3950 | cbus_BB[i]); | |
3951 | else | |
3952 | fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n"); | |
17431287 | 3953 | } |
a4980043 UB |
3954 | } |
3955 | } | |
f6ef2983 | 3956 | } |
4af1d1bb | 3957 | return 0; |
b56d5a64 MK |
3958 | } |
3959 | ||
1941414d | 3960 | /** |
44ef02bd UB |
3961 | Get a value from the decoded EEPROM structure |
3962 | ||
735e81ea TJ |
3963 | \param ftdi pointer to ftdi_context |
3964 | \param value_name Enum of the value to query | |
3965 | \param value Pointer to store read value | |
44ef02bd | 3966 | |
735e81ea TJ |
3967 | \retval 0: all fine |
3968 | \retval -1: Value doesn't exist | |
44ef02bd UB |
3969 | */ |
3970 | int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value) | |
3971 | { | |
3972 | switch (value_name) | |
3973 | { | |
56ac0383 TJ |
3974 | case VENDOR_ID: |
3975 | *value = ftdi->eeprom->vendor_id; | |
3976 | break; | |
3977 | case PRODUCT_ID: | |
3978 | *value = ftdi->eeprom->product_id; | |
3979 | break; | |
68e78641 JS |
3980 | case RELEASE_NUMBER: |
3981 | *value = ftdi->eeprom->release_number; | |
3982 | break; | |
56ac0383 TJ |
3983 | case SELF_POWERED: |
3984 | *value = ftdi->eeprom->self_powered; | |
3985 | break; | |
3986 | case REMOTE_WAKEUP: | |
3987 | *value = ftdi->eeprom->remote_wakeup; | |
3988 | break; | |
3989 | case IS_NOT_PNP: | |
3990 | *value = ftdi->eeprom->is_not_pnp; | |
3991 | break; | |
3992 | case SUSPEND_DBUS7: | |
3993 | *value = ftdi->eeprom->suspend_dbus7; | |
3994 | break; | |
3995 | case IN_IS_ISOCHRONOUS: | |
3996 | *value = ftdi->eeprom->in_is_isochronous; | |
3997 | break; | |
cffed9f5 UB |
3998 | case OUT_IS_ISOCHRONOUS: |
3999 | *value = ftdi->eeprom->out_is_isochronous; | |
4000 | break; | |
56ac0383 TJ |
4001 | case SUSPEND_PULL_DOWNS: |
4002 | *value = ftdi->eeprom->suspend_pull_downs; | |
4003 | break; | |
4004 | case USE_SERIAL: | |
4005 | *value = ftdi->eeprom->use_serial; | |
4006 | break; | |
4007 | case USB_VERSION: | |
4008 | *value = ftdi->eeprom->usb_version; | |
4009 | break; | |
cffed9f5 UB |
4010 | case USE_USB_VERSION: |
4011 | *value = ftdi->eeprom->use_usb_version; | |
4012 | break; | |
56ac0383 TJ |
4013 | case MAX_POWER: |
4014 | *value = ftdi->eeprom->max_power; | |
4015 | break; | |
4016 | case CHANNEL_A_TYPE: | |
4017 | *value = ftdi->eeprom->channel_a_type; | |
4018 | break; | |
4019 | case CHANNEL_B_TYPE: | |
4020 | *value = ftdi->eeprom->channel_b_type; | |
4021 | break; | |
4022 | case CHANNEL_A_DRIVER: | |
4023 | *value = ftdi->eeprom->channel_a_driver; | |
4024 | break; | |
4025 | case CHANNEL_B_DRIVER: | |
4026 | *value = ftdi->eeprom->channel_b_driver; | |
4027 | break; | |
be4bae37 AL |
4028 | case CHANNEL_C_DRIVER: |
4029 | *value = ftdi->eeprom->channel_c_driver; | |
4030 | break; | |
4031 | case CHANNEL_D_DRIVER: | |
4032 | *value = ftdi->eeprom->channel_d_driver; | |
4033 | break; | |
4034 | case CHANNEL_A_RS485: | |
4035 | *value = ftdi->eeprom->channel_a_rs485enable; | |
4036 | break; | |
4037 | case CHANNEL_B_RS485: | |
4038 | *value = ftdi->eeprom->channel_b_rs485enable; | |
4039 | break; | |
4040 | case CHANNEL_C_RS485: | |
4041 | *value = ftdi->eeprom->channel_c_rs485enable; | |
4042 | break; | |
4043 | case CHANNEL_D_RS485: | |
4044 | *value = ftdi->eeprom->channel_d_rs485enable; | |
4045 | break; | |
56ac0383 TJ |
4046 | case CBUS_FUNCTION_0: |
4047 | *value = ftdi->eeprom->cbus_function[0]; | |
4048 | break; | |
4049 | case CBUS_FUNCTION_1: | |
4050 | *value = ftdi->eeprom->cbus_function[1]; | |
4051 | break; | |
4052 | case CBUS_FUNCTION_2: | |
4053 | *value = ftdi->eeprom->cbus_function[2]; | |
4054 | break; | |
4055 | case CBUS_FUNCTION_3: | |
4056 | *value = ftdi->eeprom->cbus_function[3]; | |
4057 | break; | |
4058 | case CBUS_FUNCTION_4: | |
4059 | *value = ftdi->eeprom->cbus_function[4]; | |
4060 | break; | |
263d3ba0 UB |
4061 | case CBUS_FUNCTION_5: |
4062 | *value = ftdi->eeprom->cbus_function[5]; | |
4063 | break; | |
4064 | case CBUS_FUNCTION_6: | |
4065 | *value = ftdi->eeprom->cbus_function[6]; | |
4066 | break; | |
4067 | case CBUS_FUNCTION_7: | |
4068 | *value = ftdi->eeprom->cbus_function[7]; | |
4069 | break; | |
4070 | case CBUS_FUNCTION_8: | |
4071 | *value = ftdi->eeprom->cbus_function[8]; | |
4072 | break; | |
4073 | case CBUS_FUNCTION_9: | |
1162549f | 4074 | *value = ftdi->eeprom->cbus_function[9]; |
263d3ba0 | 4075 | break; |
56ac0383 TJ |
4076 | case HIGH_CURRENT: |
4077 | *value = ftdi->eeprom->high_current; | |
4078 | break; | |
4079 | case HIGH_CURRENT_A: | |
4080 | *value = ftdi->eeprom->high_current_a; | |
4081 | break; | |
4082 | case HIGH_CURRENT_B: | |
4083 | *value = ftdi->eeprom->high_current_b; | |
4084 | break; | |
4085 | case INVERT: | |
4086 | *value = ftdi->eeprom->invert; | |
4087 | break; | |
4088 | case GROUP0_DRIVE: | |
4089 | *value = ftdi->eeprom->group0_drive; | |
4090 | break; | |
4091 | case GROUP0_SCHMITT: | |
4092 | *value = ftdi->eeprom->group0_schmitt; | |
4093 | break; | |
4094 | case GROUP0_SLEW: | |
4095 | *value = ftdi->eeprom->group0_slew; | |
4096 | break; | |
4097 | case GROUP1_DRIVE: | |
4098 | *value = ftdi->eeprom->group1_drive; | |
4099 | break; | |
4100 | case GROUP1_SCHMITT: | |
4101 | *value = ftdi->eeprom->group1_schmitt; | |
4102 | break; | |
4103 | case GROUP1_SLEW: | |
4104 | *value = ftdi->eeprom->group1_slew; | |
4105 | break; | |
4106 | case GROUP2_DRIVE: | |
4107 | *value = ftdi->eeprom->group2_drive; | |
4108 | break; | |
4109 | case GROUP2_SCHMITT: | |
4110 | *value = ftdi->eeprom->group2_schmitt; | |
4111 | break; | |
4112 | case GROUP2_SLEW: | |
4113 | *value = ftdi->eeprom->group2_slew; | |
4114 | break; | |
4115 | case GROUP3_DRIVE: | |
4116 | *value = ftdi->eeprom->group3_drive; | |
4117 | break; | |
4118 | case GROUP3_SCHMITT: | |
4119 | *value = ftdi->eeprom->group3_schmitt; | |
4120 | break; | |
4121 | case GROUP3_SLEW: | |
4122 | *value = ftdi->eeprom->group3_slew; | |
4123 | break; | |
74387f27 | 4124 | case POWER_SAVE: |
837a71d6 UB |
4125 | *value = ftdi->eeprom->powersave; |
4126 | break; | |
74387f27 | 4127 | case CLOCK_POLARITY: |
18199b76 UB |
4128 | *value = ftdi->eeprom->clock_polarity; |
4129 | break; | |
74387f27 | 4130 | case DATA_ORDER: |
18199b76 UB |
4131 | *value = ftdi->eeprom->data_order; |
4132 | break; | |
74387f27 | 4133 | case FLOW_CONTROL: |
18199b76 UB |
4134 | *value = ftdi->eeprom->flow_control; |
4135 | break; | |
74387f27 | 4136 | case CHIP_TYPE: |
56ac0383 TJ |
4137 | *value = ftdi->eeprom->chip; |
4138 | break; | |
4139 | case CHIP_SIZE: | |
4140 | *value = ftdi->eeprom->size; | |
4141 | break; | |
08518f8e RA |
4142 | case EXTERNAL_OSCILLATOR: |
4143 | *value = ftdi->eeprom->external_oscillator; | |
4144 | break; | |
56ac0383 TJ |
4145 | default: |
4146 | ftdi_error_return(-1, "Request for unknown EEPROM value"); | |
44ef02bd UB |
4147 | } |
4148 | return 0; | |
4149 | } | |
4150 | ||
4151 | /** | |
4152 | Set a value in the decoded EEPROM Structure | |
4153 | No parameter checking is performed | |
4154 | ||
735e81ea | 4155 | \param ftdi pointer to ftdi_context |
545f9df9 | 4156 | \param value_name Enum of the value to set |
735e81ea | 4157 | \param value to set |
44ef02bd | 4158 | |
735e81ea TJ |
4159 | \retval 0: all fine |
4160 | \retval -1: Value doesn't exist | |
4161 | \retval -2: Value not user settable | |
44ef02bd UB |
4162 | */ |
4163 | int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value) | |
4164 | { | |
4165 | switch (value_name) | |
4166 | { | |
56ac0383 TJ |
4167 | case VENDOR_ID: |
4168 | ftdi->eeprom->vendor_id = value; | |
4169 | break; | |
4170 | case PRODUCT_ID: | |
4171 | ftdi->eeprom->product_id = value; | |
4172 | break; | |
68e78641 JS |
4173 | case RELEASE_NUMBER: |
4174 | ftdi->eeprom->release_number = value; | |
4175 | break; | |
56ac0383 TJ |
4176 | case SELF_POWERED: |
4177 | ftdi->eeprom->self_powered = value; | |
4178 | break; | |
4179 | case REMOTE_WAKEUP: | |
4180 | ftdi->eeprom->remote_wakeup = value; | |
4181 | break; | |
4182 | case IS_NOT_PNP: | |
4183 | ftdi->eeprom->is_not_pnp = value; | |
4184 | break; | |
4185 | case SUSPEND_DBUS7: | |
4186 | ftdi->eeprom->suspend_dbus7 = value; | |
4187 | break; | |
4188 | case IN_IS_ISOCHRONOUS: | |
4189 | ftdi->eeprom->in_is_isochronous = value; | |
4190 | break; | |
cffed9f5 UB |
4191 | case OUT_IS_ISOCHRONOUS: |
4192 | ftdi->eeprom->out_is_isochronous = value; | |
4193 | break; | |
56ac0383 TJ |
4194 | case SUSPEND_PULL_DOWNS: |
4195 | ftdi->eeprom->suspend_pull_downs = value; | |
4196 | break; | |
4197 | case USE_SERIAL: | |
4198 | ftdi->eeprom->use_serial = value; | |
4199 | break; | |
4200 | case USB_VERSION: | |
4201 | ftdi->eeprom->usb_version = value; | |
4202 | break; | |
cffed9f5 UB |
4203 | case USE_USB_VERSION: |
4204 | ftdi->eeprom->use_usb_version = value; | |
4205 | break; | |
56ac0383 TJ |
4206 | case MAX_POWER: |
4207 | ftdi->eeprom->max_power = value; | |
4208 | break; | |
4209 | case CHANNEL_A_TYPE: | |
4210 | ftdi->eeprom->channel_a_type = value; | |
4211 | break; | |
4212 | case CHANNEL_B_TYPE: | |
4213 | ftdi->eeprom->channel_b_type = value; | |
4214 | break; | |
4215 | case CHANNEL_A_DRIVER: | |
4216 | ftdi->eeprom->channel_a_driver = value; | |
4217 | break; | |
4218 | case CHANNEL_B_DRIVER: | |
4219 | ftdi->eeprom->channel_b_driver = value; | |
4220 | break; | |
be4bae37 AL |
4221 | case CHANNEL_C_DRIVER: |
4222 | ftdi->eeprom->channel_c_driver = value; | |
4223 | break; | |
4224 | case CHANNEL_D_DRIVER: | |
4225 | ftdi->eeprom->channel_d_driver = value; | |
4226 | break; | |
4227 | case CHANNEL_A_RS485: | |
4228 | ftdi->eeprom->channel_a_rs485enable = value; | |
4229 | break; | |
4230 | case CHANNEL_B_RS485: | |
4231 | ftdi->eeprom->channel_b_rs485enable = value; | |
4232 | break; | |
4233 | case CHANNEL_C_RS485: | |
4234 | ftdi->eeprom->channel_c_rs485enable = value; | |
4235 | break; | |
4236 | case CHANNEL_D_RS485: | |
4237 | ftdi->eeprom->channel_d_rs485enable = value; | |
4238 | break; | |
56ac0383 TJ |
4239 | case CBUS_FUNCTION_0: |
4240 | ftdi->eeprom->cbus_function[0] = value; | |
4241 | break; | |
4242 | case CBUS_FUNCTION_1: | |
4243 | ftdi->eeprom->cbus_function[1] = value; | |
4244 | break; | |
4245 | case CBUS_FUNCTION_2: | |
4246 | ftdi->eeprom->cbus_function[2] = value; | |
4247 | break; | |
4248 | case CBUS_FUNCTION_3: | |
4249 | ftdi->eeprom->cbus_function[3] = value; | |
4250 | break; | |
4251 | case CBUS_FUNCTION_4: | |
4252 | ftdi->eeprom->cbus_function[4] = value; | |
4253 | break; | |
263d3ba0 UB |
4254 | case CBUS_FUNCTION_5: |
4255 | ftdi->eeprom->cbus_function[5] = value; | |
4256 | break; | |
4257 | case CBUS_FUNCTION_6: | |
4258 | ftdi->eeprom->cbus_function[6] = value; | |
4259 | break; | |
4260 | case CBUS_FUNCTION_7: | |
4261 | ftdi->eeprom->cbus_function[7] = value; | |
4262 | break; | |
4263 | case CBUS_FUNCTION_8: | |
4264 | ftdi->eeprom->cbus_function[8] = value; | |
4265 | break; | |
4266 | case CBUS_FUNCTION_9: | |
4267 | ftdi->eeprom->cbus_function[9] = value; | |
4268 | break; | |
56ac0383 TJ |
4269 | case HIGH_CURRENT: |
4270 | ftdi->eeprom->high_current = value; | |
4271 | break; | |
4272 | case HIGH_CURRENT_A: | |
4273 | ftdi->eeprom->high_current_a = value; | |
4274 | break; | |
4275 | case HIGH_CURRENT_B: | |
4276 | ftdi->eeprom->high_current_b = value; | |
4277 | break; | |
4278 | case INVERT: | |
4279 | ftdi->eeprom->invert = value; | |
4280 | break; | |
4281 | case GROUP0_DRIVE: | |
4282 | ftdi->eeprom->group0_drive = value; | |
4283 | break; | |
4284 | case GROUP0_SCHMITT: | |
4285 | ftdi->eeprom->group0_schmitt = value; | |
4286 | break; | |
4287 | case GROUP0_SLEW: | |
4288 | ftdi->eeprom->group0_slew = value; | |
4289 | break; | |
4290 | case GROUP1_DRIVE: | |
4291 | ftdi->eeprom->group1_drive = value; | |
4292 | break; | |
4293 | case GROUP1_SCHMITT: | |
4294 | ftdi->eeprom->group1_schmitt = value; | |
4295 | break; | |
4296 | case GROUP1_SLEW: | |
4297 | ftdi->eeprom->group1_slew = value; | |
4298 | break; | |
4299 | case GROUP2_DRIVE: | |
4300 | ftdi->eeprom->group2_drive = value; | |
4301 | break; | |
4302 | case GROUP2_SCHMITT: | |
4303 | ftdi->eeprom->group2_schmitt = value; | |
4304 | break; | |
4305 | case GROUP2_SLEW: | |
4306 | ftdi->eeprom->group2_slew = value; | |
4307 | break; | |
4308 | case GROUP3_DRIVE: | |
4309 | ftdi->eeprom->group3_drive = value; | |
4310 | break; | |
4311 | case GROUP3_SCHMITT: | |
4312 | ftdi->eeprom->group3_schmitt = value; | |
4313 | break; | |
4314 | case GROUP3_SLEW: | |
4315 | ftdi->eeprom->group3_slew = value; | |
4316 | break; | |
4317 | case CHIP_TYPE: | |
4318 | ftdi->eeprom->chip = value; | |
4319 | break; | |
74387f27 | 4320 | case POWER_SAVE: |
837a71d6 UB |
4321 | ftdi->eeprom->powersave = value; |
4322 | break; | |
74387f27 | 4323 | case CLOCK_POLARITY: |
18199b76 UB |
4324 | ftdi->eeprom->clock_polarity = value; |
4325 | break; | |
74387f27 | 4326 | case DATA_ORDER: |
18199b76 UB |
4327 | ftdi->eeprom->data_order = value; |
4328 | break; | |
74387f27 | 4329 | case FLOW_CONTROL: |
18199b76 UB |
4330 | ftdi->eeprom->flow_control = value; |
4331 | break; | |
56ac0383 TJ |
4332 | case CHIP_SIZE: |
4333 | ftdi_error_return(-2, "EEPROM Value can't be changed"); | |
34b79ac7 | 4334 | break; |
08518f8e RA |
4335 | case EXTERNAL_OSCILLATOR: |
4336 | ftdi->eeprom->external_oscillator = value; | |
4337 | break; | |
6e962b9a SET |
4338 | case USER_DATA_ADDR: |
4339 | ftdi->eeprom->user_data_addr = value; | |
4340 | break; | |
34b79ac7 | 4341 | |
56ac0383 TJ |
4342 | default : |
4343 | ftdi_error_return(-1, "Request to unknown EEPROM value"); | |
44ef02bd | 4344 | } |
45a3ebd5 | 4345 | ftdi->eeprom->initialized_for_connected_device = 0; |
44ef02bd UB |
4346 | return 0; |
4347 | } | |
4348 | ||
4349 | /** Get the read-only buffer to the binary EEPROM content | |
4350 | ||
4351 | \param ftdi pointer to ftdi_context | |
735e81ea | 4352 | \param buf buffer to receive EEPROM content |
44ef02bd UB |
4353 | \param size Size of receiving buffer |
4354 | ||
4355 | \retval 0: All fine | |
4356 | \retval -1: struct ftdi_contxt or ftdi_eeprom missing | |
200bd3ed | 4357 | \retval -2: Not enough room to store eeprom |
44ef02bd | 4358 | */ |
56ac0383 TJ |
4359 | int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size) |
4360 | { | |
4361 | if (!ftdi || !(ftdi->eeprom)) | |
4362 | ftdi_error_return(-1, "No appropriate structure"); | |
b95e4654 | 4363 | |
200bd3ed TJ |
4364 | if (!buf || size < ftdi->eeprom->size) |
4365 | ftdi_error_return(-1, "Not enough room to store eeprom"); | |
4366 | ||
b95e4654 TJ |
4367 | // Only copy up to FTDI_MAX_EEPROM_SIZE bytes |
4368 | if (size > FTDI_MAX_EEPROM_SIZE) | |
4369 | size = FTDI_MAX_EEPROM_SIZE; | |
4370 | ||
56ac0383 | 4371 | memcpy(buf, ftdi->eeprom->buf, size); |
b95e4654 | 4372 | |
56ac0383 TJ |
4373 | return 0; |
4374 | } | |
44ef02bd | 4375 | |
672fd368 UB |
4376 | /** Set the EEPROM content from the user-supplied prefilled buffer |
4377 | ||
4378 | \param ftdi pointer to ftdi_context | |
4379 | \param buf buffer to read EEPROM content | |
4380 | \param size Size of buffer | |
4381 | ||
4382 | \retval 0: All fine | |
6e962b9a | 4383 | \retval -1: struct ftdi_context or ftdi_eeprom or buf missing |
672fd368 UB |
4384 | */ |
4385 | int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size) | |
4386 | { | |
4387 | if (!ftdi || !(ftdi->eeprom) || !buf) | |
4388 | ftdi_error_return(-1, "No appropriate structure"); | |
4389 | ||
4390 | // Only copy up to FTDI_MAX_EEPROM_SIZE bytes | |
4391 | if (size > FTDI_MAX_EEPROM_SIZE) | |
4392 | size = FTDI_MAX_EEPROM_SIZE; | |
4393 | ||
4394 | memcpy(ftdi->eeprom->buf, buf, size); | |
4395 | ||
4396 | return 0; | |
4397 | } | |
4398 | ||
6e962b9a SET |
4399 | /** Set the EEPROM user data content from the user-supplied prefilled buffer |
4400 | ||
4401 | \param ftdi pointer to ftdi_context | |
4402 | \param buf buffer to read EEPROM user data content | |
4403 | \param size Size of buffer | |
4404 | ||
4405 | \retval 0: All fine | |
4406 | \retval -1: struct ftdi_context or ftdi_eeprom or buf missing | |
4407 | */ | |
4408 | int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char * buf, int size) | |
4409 | { | |
4410 | if (!ftdi || !(ftdi->eeprom) || !buf) | |
4411 | ftdi_error_return(-1, "No appropriate structure"); | |
4412 | ||
4413 | ftdi->eeprom->user_data_size = size; | |
4414 | ftdi->eeprom->user_data = buf; | |
4415 | return 0; | |
4416 | } | |
4417 | ||
44ef02bd | 4418 | /** |
c1c70e13 OS |
4419 | Read eeprom location |
4420 | ||
4421 | \param ftdi pointer to ftdi_context | |
4422 | \param eeprom_addr Address of eeprom location to be read | |
4423 | \param eeprom_val Pointer to store read eeprom location | |
4424 | ||
4425 | \retval 0: all fine | |
4426 | \retval -1: read failed | |
22a1b5c1 | 4427 | \retval -2: USB device unavailable |
c1c70e13 OS |
4428 | */ |
4429 | int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val) | |
4430 | { | |
1a3cb7f8 YY |
4431 | unsigned char buf[2]; |
4432 | ||
22a1b5c1 TJ |
4433 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
4434 | ftdi_error_return(-2, "USB device unavailable"); | |
4435 | ||
1a3cb7f8 | 4436 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, buf, 2, ftdi->usb_read_timeout) != 2) |
c1c70e13 OS |
4437 | ftdi_error_return(-1, "reading eeprom failed"); |
4438 | ||
1a3cb7f8 YY |
4439 | *eeprom_val = (0xff & buf[0]) | (buf[1] << 8); |
4440 | ||
c1c70e13 OS |
4441 | return 0; |
4442 | } | |
4443 | ||
4444 | /** | |
1941414d TJ |
4445 | Read eeprom |
4446 | ||
4447 | \param ftdi pointer to ftdi_context | |
b8aa7b35 | 4448 | |
1941414d TJ |
4449 | \retval 0: all fine |
4450 | \retval -1: read failed | |
22a1b5c1 | 4451 | \retval -2: USB device unavailable |
1941414d | 4452 | */ |
a35aa9bd | 4453 | int ftdi_read_eeprom(struct ftdi_context *ftdi) |
a8f46ddc | 4454 | { |
a3da1d95 | 4455 | int i; |
a35aa9bd | 4456 | unsigned char *buf; |
a3da1d95 | 4457 | |
22a1b5c1 TJ |
4458 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
4459 | ftdi_error_return(-2, "USB device unavailable"); | |
a35aa9bd | 4460 | buf = ftdi->eeprom->buf; |
22a1b5c1 | 4461 | |
2d543486 | 4462 | for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++) |
22d12cda | 4463 | { |
a35aa9bd | 4464 | if (libusb_control_transfer( |
56ac0383 TJ |
4465 | ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i, |
4466 | buf+(i*2), 2, ftdi->usb_read_timeout) != 2) | |
c3d95b87 | 4467 | ftdi_error_return(-1, "reading eeprom failed"); |
a3da1d95 GE |
4468 | } |
4469 | ||
2d543486 | 4470 | if (ftdi->type == TYPE_R) |
a35aa9bd | 4471 | ftdi->eeprom->size = 0x80; |
56ac0383 | 4472 | /* Guesses size of eeprom by comparing halves |
2d543486 | 4473 | - will not work with blank eeprom */ |
a35aa9bd | 4474 | else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1)) |
2d543486 | 4475 | ftdi->eeprom->size = -1; |
56ac0383 | 4476 | else if (memcmp(buf,&buf[0x80],0x80) == 0) |
2d543486 | 4477 | ftdi->eeprom->size = 0x80; |
56ac0383 | 4478 | else if (memcmp(buf,&buf[0x40],0x40) == 0) |
2d543486 UB |
4479 | ftdi->eeprom->size = 0x40; |
4480 | else | |
4481 | ftdi->eeprom->size = 0x100; | |
a3da1d95 GE |
4482 | return 0; |
4483 | } | |
4484 | ||
cb6250fa TJ |
4485 | /* |
4486 | ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID | |
4487 | Function is only used internally | |
4488 | \internal | |
4489 | */ | |
4490 | static unsigned char ftdi_read_chipid_shift(unsigned char value) | |
4491 | { | |
4492 | return ((value & 1) << 1) | | |
22d12cda TJ |
4493 | ((value & 2) << 5) | |
4494 | ((value & 4) >> 2) | | |
4495 | ((value & 8) << 4) | | |
4496 | ((value & 16) >> 1) | | |
4497 | ((value & 32) >> 1) | | |
4498 | ((value & 64) >> 4) | | |
4499 | ((value & 128) >> 2); | |
cb6250fa TJ |
4500 | } |
4501 | ||
4502 | /** | |
4503 | Read the FTDIChip-ID from R-type devices | |
4504 | ||
4505 | \param ftdi pointer to ftdi_context | |
4506 | \param chipid Pointer to store FTDIChip-ID | |
4507 | ||
4508 | \retval 0: all fine | |
4509 | \retval -1: read failed | |
22a1b5c1 | 4510 | \retval -2: USB device unavailable |
cb6250fa TJ |
4511 | */ |
4512 | int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid) | |
4513 | { | |
c7eb3112 | 4514 | unsigned int a = 0, b = 0; |
cb6250fa | 4515 | |
22a1b5c1 TJ |
4516 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
4517 | ftdi_error_return(-2, "USB device unavailable"); | |
4518 | ||
579b006f | 4519 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x43, (unsigned char *)&a, 2, ftdi->usb_read_timeout) == 2) |
cb6250fa TJ |
4520 | { |
4521 | a = a << 8 | a >> 8; | |
579b006f | 4522 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x44, (unsigned char *)&b, 2, ftdi->usb_read_timeout) == 2) |
cb6250fa TJ |
4523 | { |
4524 | b = b << 8 | b >> 8; | |
5230676f | 4525 | a = (a << 16) | (b & 0xFFFF); |
912d50ca TJ |
4526 | a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8 |
4527 | | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24; | |
cb6250fa | 4528 | *chipid = a ^ 0xa5f0f7d1; |
c7eb3112 | 4529 | return 0; |
cb6250fa TJ |
4530 | } |
4531 | } | |
4532 | ||
c7eb3112 | 4533 | ftdi_error_return(-1, "read of FTDIChip-ID failed"); |
cb6250fa TJ |
4534 | } |
4535 | ||
1941414d | 4536 | /** |
c1c70e13 OS |
4537 | Write eeprom location |
4538 | ||
4539 | \param ftdi pointer to ftdi_context | |
4540 | \param eeprom_addr Address of eeprom location to be written | |
4541 | \param eeprom_val Value to be written | |
4542 | ||
4543 | \retval 0: all fine | |
a661e3e4 | 4544 | \retval -1: write failed |
22a1b5c1 | 4545 | \retval -2: USB device unavailable |
a661e3e4 UB |
4546 | \retval -3: Invalid access to checksum protected area below 0x80 |
4547 | \retval -4: Device can't access unprotected area | |
4548 | \retval -5: Reading chip type failed | |
c1c70e13 | 4549 | */ |
56ac0383 | 4550 | int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, |
a661e3e4 | 4551 | unsigned short eeprom_val) |
c1c70e13 | 4552 | { |
a661e3e4 UB |
4553 | int chip_type_location; |
4554 | unsigned short chip_type; | |
4555 | ||
22a1b5c1 TJ |
4556 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
4557 | ftdi_error_return(-2, "USB device unavailable"); | |
4558 | ||
56ac0383 | 4559 | if (eeprom_addr <0x80) |
a661e3e4 UB |
4560 | ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80"); |
4561 | ||
4562 | ||
4563 | switch (ftdi->type) | |
4564 | { | |
56ac0383 TJ |
4565 | case TYPE_BM: |
4566 | case TYPE_2232C: | |
4567 | chip_type_location = 0x14; | |
4568 | break; | |
4569 | case TYPE_2232H: | |
4570 | case TYPE_4232H: | |
4571 | chip_type_location = 0x18; | |
4572 | break; | |
c7e4c09e UB |
4573 | case TYPE_232H: |
4574 | chip_type_location = 0x1e; | |
4575 | break; | |
56ac0383 TJ |
4576 | default: |
4577 | ftdi_error_return(-4, "Device can't access unprotected area"); | |
a661e3e4 UB |
4578 | } |
4579 | ||
56ac0383 | 4580 | if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type)) |
a00c0a85 | 4581 | ftdi_error_return(-5, "Reading failed"); |
56ac0383 TJ |
4582 | fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type); |
4583 | if ((chip_type & 0xff) != 0x66) | |
a661e3e4 UB |
4584 | { |
4585 | ftdi_error_return(-6, "EEPROM is not of 93x66"); | |
4586 | } | |
4587 | ||
579b006f | 4588 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
56ac0383 TJ |
4589 | SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr, |
4590 | NULL, 0, ftdi->usb_write_timeout) != 0) | |
c1c70e13 OS |
4591 | ftdi_error_return(-1, "unable to write eeprom"); |
4592 | ||
4593 | return 0; | |
4594 | } | |
4595 | ||
4596 | /** | |
1941414d | 4597 | Write eeprom |
a3da1d95 | 4598 | |
1941414d | 4599 | \param ftdi pointer to ftdi_context |
56ac0383 | 4600 | |
1941414d TJ |
4601 | \retval 0: all fine |
4602 | \retval -1: read failed | |
22a1b5c1 | 4603 | \retval -2: USB device unavailable |
44f41f11 | 4604 | \retval -3: EEPROM not initialized for the connected device; |
1941414d | 4605 | */ |
a35aa9bd | 4606 | int ftdi_write_eeprom(struct ftdi_context *ftdi) |
a8f46ddc | 4607 | { |
ba5329be | 4608 | unsigned short usb_val, status; |
e30da501 | 4609 | int i, ret; |
a35aa9bd | 4610 | unsigned char *eeprom; |
a3da1d95 | 4611 | |
22a1b5c1 TJ |
4612 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
4613 | ftdi_error_return(-2, "USB device unavailable"); | |
44f41f11 UB |
4614 | |
4615 | if(ftdi->eeprom->initialized_for_connected_device == 0) | |
4616 | ftdi_error_return(-3, "EEPROM not initialized for the connected device"); | |
4617 | ||
a35aa9bd | 4618 | eeprom = ftdi->eeprom->buf; |
22a1b5c1 | 4619 | |
ba5329be | 4620 | /* These commands were traced while running MProg */ |
e30da501 TJ |
4621 | if ((ret = ftdi_usb_reset(ftdi)) != 0) |
4622 | return ret; | |
4623 | if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0) | |
4624 | return ret; | |
4625 | if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0) | |
4626 | return ret; | |
ba5329be | 4627 | |
c0a96aed | 4628 | for (i = 0; i < ftdi->eeprom->size/2; i++) |
22d12cda | 4629 | { |
2f80efc2 | 4630 | /* Do not try to write to reserved area */ |
74387f27 TJ |
4631 | if ((ftdi->type == TYPE_230X) && (i == 0x40)) |
4632 | { | |
2f80efc2 NP |
4633 | i = 0x50; |
4634 | } | |
d9f0cce7 TJ |
4635 | usb_val = eeprom[i*2]; |
4636 | usb_val += eeprom[(i*2)+1] << 8; | |
579b006f JZ |
4637 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
4638 | SIO_WRITE_EEPROM_REQUEST, usb_val, i, | |
4639 | NULL, 0, ftdi->usb_write_timeout) < 0) | |
c3d95b87 | 4640 | ftdi_error_return(-1, "unable to write eeprom"); |
a3da1d95 GE |
4641 | } |
4642 | ||
4643 | return 0; | |
4644 | } | |
4645 | ||
1941414d TJ |
4646 | /** |
4647 | Erase eeprom | |
a3da1d95 | 4648 | |
a5e1bd8c MK |
4649 | This is not supported on FT232R/FT245R according to the MProg manual from FTDI. |
4650 | ||
1941414d TJ |
4651 | \param ftdi pointer to ftdi_context |
4652 | ||
4653 | \retval 0: all fine | |
4654 | \retval -1: erase failed | |
22a1b5c1 | 4655 | \retval -2: USB device unavailable |
99404ad5 UB |
4656 | \retval -3: Writing magic failed |
4657 | \retval -4: Read EEPROM failed | |
4658 | \retval -5: Unexpected EEPROM value | |
1941414d | 4659 | */ |
99404ad5 | 4660 | #define MAGIC 0x55aa |
a8f46ddc TJ |
4661 | int ftdi_erase_eeprom(struct ftdi_context *ftdi) |
4662 | { | |
99404ad5 | 4663 | unsigned short eeprom_value; |
22a1b5c1 TJ |
4664 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
4665 | ftdi_error_return(-2, "USB device unavailable"); | |
4666 | ||
519bbce1 | 4667 | if ((ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X)) |
99404ad5 UB |
4668 | { |
4669 | ftdi->eeprom->chip = 0; | |
4670 | return 0; | |
4671 | } | |
4672 | ||
56ac0383 | 4673 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, |
99404ad5 | 4674 | 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0) |
c3d95b87 | 4675 | ftdi_error_return(-1, "unable to erase eeprom"); |
a3da1d95 | 4676 | |
56ac0383 | 4677 | |
99404ad5 UB |
4678 | /* detect chip type by writing 0x55AA as magic at word position 0xc0 |
4679 | Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40 | |
4680 | Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80 | |
4681 | Chip is 93x66 if magic is only read at word position 0xc0*/ | |
10186c1f | 4682 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
56ac0383 TJ |
4683 | SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0, |
4684 | NULL, 0, ftdi->usb_write_timeout) != 0) | |
99404ad5 | 4685 | ftdi_error_return(-3, "Writing magic failed"); |
56ac0383 | 4686 | if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value)) |
a00c0a85 | 4687 | ftdi_error_return(-4, "Reading failed"); |
56ac0383 | 4688 | if (eeprom_value == MAGIC) |
99404ad5 UB |
4689 | { |
4690 | ftdi->eeprom->chip = 0x46; | |
4691 | } | |
56ac0383 | 4692 | else |
99404ad5 | 4693 | { |
56ac0383 | 4694 | if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value)) |
a00c0a85 | 4695 | ftdi_error_return(-4, "Reading failed"); |
56ac0383 | 4696 | if (eeprom_value == MAGIC) |
99404ad5 | 4697 | ftdi->eeprom->chip = 0x56; |
56ac0383 | 4698 | else |
99404ad5 | 4699 | { |
56ac0383 | 4700 | if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value)) |
a00c0a85 | 4701 | ftdi_error_return(-4, "Reading failed"); |
56ac0383 | 4702 | if (eeprom_value == MAGIC) |
99404ad5 UB |
4703 | ftdi->eeprom->chip = 0x66; |
4704 | else | |
4705 | { | |
4706 | ftdi->eeprom->chip = -1; | |
4707 | } | |
4708 | } | |
4709 | } | |
56ac0383 | 4710 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, |
99404ad5 UB |
4711 | 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0) |
4712 | ftdi_error_return(-1, "unable to erase eeprom"); | |
a3da1d95 GE |
4713 | return 0; |
4714 | } | |
c3d95b87 | 4715 | |
1941414d TJ |
4716 | /** |
4717 | Get string representation for last error code | |
c3d95b87 | 4718 | |
1941414d TJ |
4719 | \param ftdi pointer to ftdi_context |
4720 | ||
4721 | \retval Pointer to error string | |
4722 | */ | |
c45d2630 | 4723 | const char *ftdi_get_error_string (struct ftdi_context *ftdi) |
c3d95b87 | 4724 | { |
22a1b5c1 TJ |
4725 | if (ftdi == NULL) |
4726 | return ""; | |
4727 | ||
c3d95b87 TJ |
4728 | return ftdi->error_str; |
4729 | } | |
a01d31e2 | 4730 | |
b5ec1820 | 4731 | /* @} end of doxygen libftdi group */ |