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