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