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