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