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