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