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