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