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