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