Commit | Line | Data |
---|---|---|
a3da1d95 GE |
1 | /*************************************************************************** |
2 | ftdi.c - description | |
3 | ------------------- | |
4 | begin : Fri Apr 4 2003 | |
5 | copyright : (C) 2003 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 | |
21abaf2e | 37 | #define ftdi_error_return(code, str) do { \ |
2f73e59f | 38 | ftdi->error_str = str; \ |
21abaf2e | 39 | return code; \ |
d2f10023 | 40 | } while(0); |
c3d95b87 TJ |
41 | |
42 | ||
1941414d TJ |
43 | /** |
44 | Initializes a ftdi_context. | |
4837f98a | 45 | |
1941414d | 46 | \param ftdi pointer to ftdi_context |
4837f98a | 47 | |
1941414d TJ |
48 | \retval 0: all fine |
49 | \retval -1: couldn't allocate read buffer | |
50 | ||
51 | \remark This should be called before all functions | |
948f9ada | 52 | */ |
a8f46ddc TJ |
53 | int ftdi_init(struct ftdi_context *ftdi) |
54 | { | |
98452d97 | 55 | ftdi->usb_dev = NULL; |
545820ce TJ |
56 | ftdi->usb_read_timeout = 5000; |
57 | ftdi->usb_write_timeout = 5000; | |
a3da1d95 | 58 | |
53ad271d | 59 | ftdi->type = TYPE_BM; /* chip type */ |
a3da1d95 GE |
60 | ftdi->baudrate = -1; |
61 | ftdi->bitbang_enabled = 0; | |
62 | ||
948f9ada TJ |
63 | ftdi->readbuffer = NULL; |
64 | ftdi->readbuffer_offset = 0; | |
65 | ftdi->readbuffer_remaining = 0; | |
66 | ftdi->writebuffer_chunksize = 4096; | |
67 | ||
545820ce TJ |
68 | ftdi->interface = 0; |
69 | ftdi->index = 0; | |
70 | ftdi->in_ep = 0x02; | |
71 | ftdi->out_ep = 0x81; | |
3119537f | 72 | ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */ |
53ad271d | 73 | |
a3da1d95 GE |
74 | ftdi->error_str = NULL; |
75 | ||
1c733d33 TJ |
76 | /* All fine. Now allocate the readbuffer */ |
77 | return ftdi_read_data_set_chunksize(ftdi, 4096); | |
948f9ada | 78 | } |
4837f98a | 79 | |
1941414d TJ |
80 | /** |
81 | Open selected channels on a chip, otherwise use first channel. | |
82 | ||
83 | \param ftdi pointer to ftdi_context | |
84 | \param interface Interface to use for FT2232C chips. | |
85 | ||
86 | \retval 0: all fine | |
87 | \retval -1: unknown interface | |
c4446c36 | 88 | */ |
0ce2f5fa | 89 | int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface) |
c4446c36 TJ |
90 | { |
91 | switch (interface) { | |
92 | case INTERFACE_ANY: | |
93 | case INTERFACE_A: | |
0ce2f5fa | 94 | /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */ |
c4446c36 TJ |
95 | break; |
96 | case INTERFACE_B: | |
97 | ftdi->interface = 1; | |
98 | ftdi->index = INTERFACE_B; | |
99 | ftdi->in_ep = 0x04; | |
100 | ftdi->out_ep = 0x83; | |
101 | break; | |
102 | default: | |
103 | ftdi_error_return(-1, "Unknown interface"); | |
104 | } | |
105 | return 0; | |
106 | } | |
948f9ada | 107 | |
1941414d TJ |
108 | /** |
109 | Deinitializes a ftdi_context. | |
4837f98a | 110 | |
1941414d | 111 | \param ftdi pointer to ftdi_context |
4837f98a | 112 | */ |
a8f46ddc TJ |
113 | void ftdi_deinit(struct ftdi_context *ftdi) |
114 | { | |
948f9ada | 115 | if (ftdi->readbuffer != NULL) { |
d9f0cce7 TJ |
116 | free(ftdi->readbuffer); |
117 | ftdi->readbuffer = NULL; | |
948f9ada | 118 | } |
a3da1d95 GE |
119 | } |
120 | ||
1941414d TJ |
121 | /** |
122 | Use an already open libusb device. | |
123 | ||
124 | \param ftdi pointer to ftdi_context | |
125 | \param usb libusb usb_dev_handle to use | |
4837f98a | 126 | */ |
a8f46ddc TJ |
127 | void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) |
128 | { | |
98452d97 TJ |
129 | ftdi->usb_dev = usb; |
130 | } | |
131 | ||
132 | ||
1941414d TJ |
133 | /** |
134 | Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which | |
135 | needs to be deallocated by ftdi_list_free() after use. | |
136 | ||
137 | \param ftdi pointer to ftdi_context | |
138 | \param devlist Pointer where to store list of found devices | |
139 | \param vendor Vendor ID to search for | |
140 | \param product Product ID to search for | |
edb82cbf | 141 | |
1941414d TJ |
142 | \retval >0: number of devices found |
143 | \retval -1: usb_find_busses() failed | |
144 | \retval -2: usb_find_devices() failed | |
145 | \retval -3: out of memory | |
edb82cbf | 146 | */ |
d2f10023 | 147 | int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product) |
edb82cbf TJ |
148 | { |
149 | struct ftdi_device_list **curdev; | |
150 | struct usb_bus *bus; | |
151 | struct usb_device *dev; | |
152 | int count = 0; | |
d2f10023 | 153 | |
edb82cbf TJ |
154 | usb_init(); |
155 | if (usb_find_busses() < 0) | |
156 | ftdi_error_return(-1, "usb_find_busses() failed"); | |
157 | if (usb_find_devices() < 0) | |
158 | ftdi_error_return(-2, "usb_find_devices() failed"); | |
159 | ||
160 | curdev = devlist; | |
6db32169 | 161 | *curdev = NULL; |
edb82cbf TJ |
162 | for (bus = usb_busses; bus; bus = bus->next) { |
163 | for (dev = bus->devices; dev; dev = dev->next) { | |
164 | if (dev->descriptor.idVendor == vendor | |
165 | && dev->descriptor.idProduct == product) | |
166 | { | |
167 | *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list)); | |
168 | if (!*curdev) | |
169 | ftdi_error_return(-3, "out of memory"); | |
d2f10023 | 170 | |
edb82cbf TJ |
171 | (*curdev)->next = NULL; |
172 | (*curdev)->dev = dev; | |
173 | ||
174 | curdev = &(*curdev)->next; | |
175 | count++; | |
176 | } | |
177 | } | |
178 | } | |
d2f10023 | 179 | |
edb82cbf TJ |
180 | return count; |
181 | } | |
182 | ||
1941414d TJ |
183 | /** |
184 | Frees a usb device list. | |
edb82cbf | 185 | |
1941414d | 186 | \param devlist USB device list created by ftdi_usb_find_all() |
edb82cbf | 187 | */ |
d2f10023 | 188 | void ftdi_list_free(struct ftdi_device_list **devlist) |
edb82cbf | 189 | { |
6db32169 TJ |
190 | struct ftdi_device_list *curdev, *next; |
191 | ||
192 | for (curdev = *devlist; curdev != NULL;) { | |
193 | next = curdev->next; | |
194 | free(curdev); | |
195 | curdev = next; | |
edb82cbf TJ |
196 | } |
197 | ||
6db32169 | 198 | *devlist = NULL; |
edb82cbf TJ |
199 | } |
200 | ||
1941414d | 201 | /** |
474786c0 TJ |
202 | Return device ID strings from the usb device. |
203 | ||
204 | The parameters manufacturer, description and serial may be NULL | |
205 | or pointer to buffers to store the fetched strings. | |
206 | ||
898c34dd TJ |
207 | \note Use this function only in combination with ftdi_usb_find_all() |
208 | as it closes the internal "usb_dev" after use. | |
209 | ||
474786c0 TJ |
210 | \param ftdi pointer to ftdi_context |
211 | \param dev libusb usb_dev to use | |
212 | \param manufacturer Store manufacturer string here if not NULL | |
213 | \param mnf_len Buffer size of manufacturer string | |
214 | \param description Store product description string here if not NULL | |
215 | \param desc_len Buffer size of product description string | |
216 | \param serial Store serial string here if not NULL | |
217 | \param serial_len Buffer size of serial string | |
218 | ||
219 | \retval 0: all fine | |
220 | \retval -1: wrong arguments | |
221 | \retval -4: unable to open device | |
222 | \retval -7: get product manufacturer failed | |
223 | \retval -8: get product description failed | |
224 | \retval -9: get serial number failed | |
225 | \retval -10: unable to close device | |
226 | */ | |
227 | int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev, | |
228 | char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len) | |
229 | { | |
230 | if ((ftdi==NULL) || (dev==NULL)) | |
231 | return -1; | |
232 | ||
233 | if (!(ftdi->usb_dev = usb_open(dev))) | |
234 | ftdi_error_return(-4, usb_strerror()); | |
235 | ||
236 | if (manufacturer != NULL) { | |
237 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0) { | |
238 | usb_close (ftdi->usb_dev); | |
239 | ftdi_error_return(-7, usb_strerror()); | |
240 | } | |
241 | } | |
242 | ||
243 | if (description != NULL) { | |
244 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0) { | |
245 | usb_close (ftdi->usb_dev); | |
246 | ftdi_error_return(-8, usb_strerror()); | |
247 | } | |
248 | } | |
249 | ||
250 | if (serial != NULL) { | |
251 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0) { | |
252 | usb_close (ftdi->usb_dev); | |
253 | ftdi_error_return(-9, usb_strerror()); | |
254 | } | |
255 | } | |
256 | ||
257 | if (usb_close (ftdi->usb_dev) != 0) | |
258 | ftdi_error_return(-10, usb_strerror()); | |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
263 | /** | |
1941414d | 264 | Opens a ftdi device given by a usb_device. |
7b18bef6 | 265 | |
1941414d TJ |
266 | \param ftdi pointer to ftdi_context |
267 | \param dev libusb usb_dev to use | |
268 | ||
269 | \retval 0: all fine | |
270 | \retval -4: unable to open device | |
271 | \retval -5: unable to claim device | |
272 | \retval -6: reset failed | |
273 | \retval -7: set baudrate failed | |
7b18bef6 TJ |
274 | */ |
275 | int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev) | |
276 | { | |
d2f10023 | 277 | int detach_errno = 0; |
7b18bef6 TJ |
278 | if (!(ftdi->usb_dev = usb_open(dev))) |
279 | ftdi_error_return(-4, "usb_open() failed"); | |
d2f10023 TJ |
280 | |
281 | #ifdef LIBUSB_HAS_GET_DRIVER_NP | |
282 | // Try to detach ftdi_sio kernel module | |
283 | // Returns ENODATA if driver is not loaded | |
284 | if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA) | |
285 | detach_errno = errno; | |
286 | #endif | |
287 | ||
7b18bef6 TJ |
288 | if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) { |
289 | usb_close (ftdi->usb_dev); | |
d2f10023 TJ |
290 | if (detach_errno == EPERM) { |
291 | ftdi_error_return(-8, "inappropriate permissions on device!"); | |
292 | } else { | |
293 | ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!"); | |
294 | } | |
7b18bef6 TJ |
295 | } |
296 | ||
297 | if (ftdi_usb_reset (ftdi) != 0) { | |
298 | usb_close (ftdi->usb_dev); | |
299 | ftdi_error_return(-6, "ftdi_usb_reset failed"); | |
300 | } | |
301 | ||
302 | if (ftdi_set_baudrate (ftdi, 9600) != 0) { | |
303 | usb_close (ftdi->usb_dev); | |
304 | ftdi_error_return(-7, "set baudrate failed"); | |
305 | } | |
306 | ||
307 | // Try to guess chip type | |
308 | // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0 | |
309 | if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200 | |
310 | && dev->descriptor.iSerialNumber == 0)) | |
311 | ftdi->type = TYPE_BM; | |
312 | else if (dev->descriptor.bcdDevice == 0x200) | |
313 | ftdi->type = TYPE_AM; | |
314 | else if (dev->descriptor.bcdDevice == 0x500) { | |
315 | ftdi->type = TYPE_2232C; | |
316 | if (!ftdi->index) | |
317 | ftdi->index = INTERFACE_A; | |
cb6250fa TJ |
318 | } else if (dev->descriptor.bcdDevice == 0x600) |
319 | ftdi->type = TYPE_R; | |
7b18bef6 TJ |
320 | |
321 | ftdi_error_return(0, "all fine"); | |
322 | } | |
323 | ||
1941414d TJ |
324 | /** |
325 | Opens the first device with a given vendor and product ids. | |
326 | ||
327 | \param ftdi pointer to ftdi_context | |
328 | \param vendor Vendor ID | |
329 | \param product Product ID | |
330 | ||
9bec2387 | 331 | \retval same as ftdi_usb_open_desc() |
1941414d | 332 | */ |
edb82cbf TJ |
333 | int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) |
334 | { | |
335 | return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL); | |
336 | } | |
337 | ||
1941414d TJ |
338 | /** |
339 | Opens the first device with a given, vendor id, product id, | |
340 | description and serial. | |
341 | ||
342 | \param ftdi pointer to ftdi_context | |
343 | \param vendor Vendor ID | |
344 | \param product Product ID | |
345 | \param description Description to search for. Use NULL if not needed. | |
346 | \param serial Serial to search for. Use NULL if not needed. | |
347 | ||
348 | \retval 0: all fine | |
349 | \retval -1: usb_find_busses() failed | |
350 | \retval -2: usb_find_devices() failed | |
351 | \retval -3: usb device not found | |
352 | \retval -4: unable to open device | |
353 | \retval -5: unable to claim device | |
354 | \retval -6: reset failed | |
355 | \retval -7: set baudrate failed | |
356 | \retval -8: get product description failed | |
357 | \retval -9: get serial number failed | |
358 | \retval -10: unable to close device | |
a3da1d95 | 359 | */ |
04e1ea0a | 360 | int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, |
a8f46ddc TJ |
361 | const char* description, const char* serial) |
362 | { | |
98452d97 TJ |
363 | struct usb_bus *bus; |
364 | struct usb_device *dev; | |
c3d95b87 | 365 | char string[256]; |
98452d97 TJ |
366 | |
367 | usb_init(); | |
368 | ||
c3d95b87 TJ |
369 | if (usb_find_busses() < 0) |
370 | ftdi_error_return(-1, "usb_find_busses() failed"); | |
c3d95b87 | 371 | if (usb_find_devices() < 0) |
edb82cbf | 372 | ftdi_error_return(-2, "usb_find_devices() failed"); |
a3da1d95 | 373 | |
98452d97 TJ |
374 | for (bus = usb_busses; bus; bus = bus->next) { |
375 | for (dev = bus->devices; dev; dev = dev->next) { | |
a8f46ddc | 376 | if (dev->descriptor.idVendor == vendor |
c3d95b87 TJ |
377 | && dev->descriptor.idProduct == product) { |
378 | if (!(ftdi->usb_dev = usb_open(dev))) | |
379 | ftdi_error_return(-4, "usb_open() failed"); | |
380 | ||
a8f46ddc TJ |
381 | if (description != NULL) { |
382 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) { | |
c3d95b87 TJ |
383 | usb_close (ftdi->usb_dev); |
384 | ftdi_error_return(-8, "unable to fetch product description"); | |
98452d97 | 385 | } |
a8f46ddc | 386 | if (strncmp(string, description, sizeof(string)) != 0) { |
edb82cbf TJ |
387 | if (usb_close (ftdi->usb_dev) != 0) |
388 | ftdi_error_return(-10, "unable to close device"); | |
a8f46ddc TJ |
389 | continue; |
390 | } | |
391 | } | |
392 | if (serial != NULL) { | |
393 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) { | |
c3d95b87 TJ |
394 | usb_close (ftdi->usb_dev); |
395 | ftdi_error_return(-9, "unable to fetch serial number"); | |
a8f46ddc TJ |
396 | } |
397 | if (strncmp(string, serial, sizeof(string)) != 0) { | |
a8f46ddc | 398 | if (usb_close (ftdi->usb_dev) != 0) |
edb82cbf | 399 | ftdi_error_return(-10, "unable to close device"); |
a8f46ddc TJ |
400 | continue; |
401 | } | |
402 | } | |
98452d97 | 403 | |
edb82cbf TJ |
404 | if (usb_close (ftdi->usb_dev) != 0) |
405 | ftdi_error_return(-10, "unable to close device"); | |
d2f10023 | 406 | |
edb82cbf | 407 | return ftdi_usb_open_dev(ftdi, dev); |
98452d97 TJ |
408 | } |
409 | } | |
98452d97 | 410 | } |
a3da1d95 | 411 | |
98452d97 | 412 | // device not found |
c3d95b87 | 413 | ftdi_error_return(-3, "device not found"); |
a3da1d95 GE |
414 | } |
415 | ||
1941414d TJ |
416 | /** |
417 | Resets the ftdi device. | |
a3da1d95 | 418 | |
1941414d TJ |
419 | \param ftdi pointer to ftdi_context |
420 | ||
421 | \retval 0: all fine | |
422 | \retval -1: FTDI reset failed | |
4837f98a | 423 | */ |
edb82cbf | 424 | int ftdi_usb_reset(struct ftdi_context *ftdi) |
a8f46ddc | 425 | { |
c3d95b87 TJ |
426 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
427 | ftdi_error_return(-1,"FTDI reset failed"); | |
428 | ||
545820ce | 429 | // Invalidate data in the readbuffer |
bfcee05b TJ |
430 | ftdi->readbuffer_offset = 0; |
431 | ftdi->readbuffer_remaining = 0; | |
432 | ||
a3da1d95 GE |
433 | return 0; |
434 | } | |
435 | ||
1941414d TJ |
436 | /** |
437 | Clears the buffers on the chip. | |
438 | ||
439 | \param ftdi pointer to ftdi_context | |
4837f98a | 440 | |
1941414d TJ |
441 | \retval 0: all fine |
442 | \retval -1: write buffer purge failed | |
443 | \retval -2: read buffer purge failed | |
4837f98a | 444 | */ |
a8f46ddc TJ |
445 | int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) |
446 | { | |
c3d95b87 TJ |
447 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
448 | ftdi_error_return(-1, "FTDI purge of RX buffer failed"); | |
449 | ||
545820ce | 450 | // Invalidate data in the readbuffer |
bfcee05b TJ |
451 | ftdi->readbuffer_offset = 0; |
452 | ftdi->readbuffer_remaining = 0; | |
a60be878 | 453 | |
c3d95b87 TJ |
454 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
455 | ftdi_error_return(-2, "FTDI purge of TX buffer failed"); | |
545820ce | 456 | |
a60be878 TJ |
457 | return 0; |
458 | } | |
a3da1d95 | 459 | |
1941414d TJ |
460 | /** |
461 | Closes the ftdi device. Call ftdi_deinit() if you're cleaning up. | |
462 | ||
463 | \param ftdi pointer to ftdi_context | |
464 | ||
465 | \retval 0: all fine | |
466 | \retval -1: usb_release failed | |
467 | \retval -2: usb_close failed | |
a3da1d95 | 468 | */ |
a8f46ddc TJ |
469 | int ftdi_usb_close(struct ftdi_context *ftdi) |
470 | { | |
a3da1d95 GE |
471 | int rtn = 0; |
472 | ||
98452d97 | 473 | if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0) |
a3da1d95 | 474 | rtn = -1; |
98452d97 TJ |
475 | |
476 | if (usb_close (ftdi->usb_dev) != 0) | |
a3da1d95 | 477 | rtn = -2; |
98452d97 | 478 | |
a3da1d95 GE |
479 | return rtn; |
480 | } | |
481 | ||
a3da1d95 | 482 | /* |
53ad271d TJ |
483 | ftdi_convert_baudrate returns nearest supported baud rate to that requested. |
484 | Function is only used internally | |
b5ec1820 | 485 | \internal |
53ad271d | 486 | */ |
0126d22e | 487 | static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, |
a8f46ddc TJ |
488 | unsigned short *value, unsigned short *index) |
489 | { | |
53ad271d TJ |
490 | static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1}; |
491 | static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3}; | |
492 | static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7}; | |
493 | int divisor, best_divisor, best_baud, best_baud_diff; | |
494 | unsigned long encoded_divisor; | |
495 | int i; | |
496 | ||
497 | if (baudrate <= 0) { | |
498 | // Return error | |
499 | return -1; | |
500 | } | |
501 | ||
502 | divisor = 24000000 / baudrate; | |
503 | ||
0126d22e | 504 | if (ftdi->type == TYPE_AM) { |
53ad271d TJ |
505 | // Round down to supported fraction (AM only) |
506 | divisor -= am_adjust_dn[divisor & 7]; | |
507 | } | |
508 | ||
509 | // Try this divisor and the one above it (because division rounds down) | |
510 | best_divisor = 0; | |
511 | best_baud = 0; | |
512 | best_baud_diff = 0; | |
513 | for (i = 0; i < 2; i++) { | |
514 | int try_divisor = divisor + i; | |
515 | int baud_estimate; | |
516 | int baud_diff; | |
517 | ||
518 | // Round up to supported divisor value | |
df612d35 | 519 | if (try_divisor <= 8) { |
53ad271d TJ |
520 | // Round up to minimum supported divisor |
521 | try_divisor = 8; | |
0126d22e | 522 | } else if (ftdi->type != TYPE_AM && try_divisor < 12) { |
53ad271d TJ |
523 | // BM doesn't support divisors 9 through 11 inclusive |
524 | try_divisor = 12; | |
525 | } else if (divisor < 16) { | |
526 | // AM doesn't support divisors 9 through 15 inclusive | |
527 | try_divisor = 16; | |
528 | } else { | |
0126d22e | 529 | if (ftdi->type == TYPE_AM) { |
53ad271d TJ |
530 | // Round up to supported fraction (AM only) |
531 | try_divisor += am_adjust_up[try_divisor & 7]; | |
532 | if (try_divisor > 0x1FFF8) { | |
533 | // Round down to maximum supported divisor value (for AM) | |
534 | try_divisor = 0x1FFF8; | |
535 | } | |
536 | } else { | |
537 | if (try_divisor > 0x1FFFF) { | |
538 | // Round down to maximum supported divisor value (for BM) | |
539 | try_divisor = 0x1FFFF; | |
540 | } | |
541 | } | |
542 | } | |
543 | // Get estimated baud rate (to nearest integer) | |
544 | baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor; | |
545 | // Get absolute difference from requested baud rate | |
546 | if (baud_estimate < baudrate) { | |
547 | baud_diff = baudrate - baud_estimate; | |
548 | } else { | |
549 | baud_diff = baud_estimate - baudrate; | |
550 | } | |
551 | if (i == 0 || baud_diff < best_baud_diff) { | |
552 | // Closest to requested baud rate so far | |
553 | best_divisor = try_divisor; | |
554 | best_baud = baud_estimate; | |
555 | best_baud_diff = baud_diff; | |
556 | if (baud_diff == 0) { | |
557 | // Spot on! No point trying | |
558 | break; | |
559 | } | |
560 | } | |
561 | } | |
562 | // Encode the best divisor value | |
563 | encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14); | |
564 | // Deal with special cases for encoded value | |
565 | if (encoded_divisor == 1) { | |
4837f98a | 566 | encoded_divisor = 0; // 3000000 baud |
53ad271d | 567 | } else if (encoded_divisor == 0x4001) { |
4837f98a | 568 | encoded_divisor = 1; // 2000000 baud (BM only) |
53ad271d TJ |
569 | } |
570 | // Split into "value" and "index" values | |
571 | *value = (unsigned short)(encoded_divisor & 0xFFFF); | |
de22df10 | 572 | if(ftdi->type == TYPE_2232C) { |
0126d22e TJ |
573 | *index = (unsigned short)(encoded_divisor >> 8); |
574 | *index &= 0xFF00; | |
a9c57c05 | 575 | *index |= ftdi->index; |
0126d22e TJ |
576 | } |
577 | else | |
578 | *index = (unsigned short)(encoded_divisor >> 16); | |
c3d95b87 | 579 | |
53ad271d TJ |
580 | // Return the nearest baud rate |
581 | return best_baud; | |
582 | } | |
583 | ||
1941414d | 584 | /** |
9bec2387 | 585 | Sets the chip baud rate |
1941414d TJ |
586 | |
587 | \param ftdi pointer to ftdi_context | |
9bec2387 | 588 | \param baudrate baud rate to set |
1941414d TJ |
589 | |
590 | \retval 0: all fine | |
591 | \retval -1: invalid baudrate | |
592 | \retval -2: setting baudrate failed | |
a3da1d95 | 593 | */ |
a8f46ddc TJ |
594 | int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) |
595 | { | |
53ad271d TJ |
596 | unsigned short value, index; |
597 | int actual_baudrate; | |
a3da1d95 GE |
598 | |
599 | if (ftdi->bitbang_enabled) { | |
600 | baudrate = baudrate*4; | |
601 | } | |
602 | ||
25707904 | 603 | actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index); |
c3d95b87 TJ |
604 | if (actual_baudrate <= 0) |
605 | ftdi_error_return (-1, "Silly baudrate <= 0."); | |
a3da1d95 | 606 | |
53ad271d TJ |
607 | // Check within tolerance (about 5%) |
608 | if ((actual_baudrate * 2 < baudrate /* Catch overflows */ ) | |
609 | || ((actual_baudrate < baudrate) | |
610 | ? (actual_baudrate * 21 < baudrate * 20) | |
c3d95b87 TJ |
611 | : (baudrate * 21 < actual_baudrate * 20))) |
612 | ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4"); | |
545820ce | 613 | |
c3d95b87 TJ |
614 | if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0) |
615 | ftdi_error_return (-2, "Setting new baudrate failed"); | |
a3da1d95 GE |
616 | |
617 | ftdi->baudrate = baudrate; | |
618 | return 0; | |
619 | } | |
620 | ||
1941414d TJ |
621 | /** |
622 | Set (RS232) line characteristics by Alain Abbas | |
4837f98a | 623 | |
1941414d TJ |
624 | \param ftdi pointer to ftdi_context |
625 | \param bits Number of bits | |
626 | \param sbit Number of stop bits | |
627 | \param parity Parity mode | |
628 | ||
629 | \retval 0: all fine | |
630 | \retval -1: Setting line property failed | |
2f73e59f TJ |
631 | */ |
632 | int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits, | |
d2f10023 | 633 | enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity) |
2f73e59f TJ |
634 | { |
635 | unsigned short value = bits; | |
636 | ||
637 | switch(parity) { | |
638 | case NONE: | |
639 | value |= (0x00 << 8); | |
640 | break; | |
641 | case ODD: | |
642 | value |= (0x01 << 8); | |
643 | break; | |
644 | case EVEN: | |
645 | value |= (0x02 << 8); | |
646 | break; | |
647 | case MARK: | |
648 | value |= (0x03 << 8); | |
649 | break; | |
650 | case SPACE: | |
651 | value |= (0x04 << 8); | |
652 | break; | |
653 | } | |
d2f10023 | 654 | |
2f73e59f TJ |
655 | switch(sbit) { |
656 | case STOP_BIT_1: | |
657 | value |= (0x00 << 11); | |
658 | break; | |
659 | case STOP_BIT_15: | |
660 | value |= (0x01 << 11); | |
661 | break; | |
662 | case STOP_BIT_2: | |
663 | value |= (0x02 << 11); | |
664 | break; | |
665 | } | |
d2f10023 | 666 | |
2f73e59f TJ |
667 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
668 | ftdi_error_return (-1, "Setting new line property failed"); | |
d2f10023 | 669 | |
2f73e59f TJ |
670 | return 0; |
671 | } | |
a3da1d95 | 672 | |
1941414d TJ |
673 | /** |
674 | Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip | |
675 | ||
676 | \param ftdi pointer to ftdi_context | |
677 | \param buf Buffer with the data | |
678 | \param size Size of the buffer | |
679 | ||
680 | \retval <0: error code from usb_bulk_write() | |
681 | \retval >0: number of bytes written | |
682 | */ | |
a8f46ddc TJ |
683 | int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
684 | { | |
a3da1d95 GE |
685 | int ret; |
686 | int offset = 0; | |
545820ce | 687 | int total_written = 0; |
c3d95b87 | 688 | |
a3da1d95 | 689 | while (offset < size) { |
948f9ada | 690 | int write_size = ftdi->writebuffer_chunksize; |
a3da1d95 GE |
691 | |
692 | if (offset+write_size > size) | |
693 | write_size = size-offset; | |
694 | ||
98452d97 | 695 | ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout); |
c3d95b87 TJ |
696 | if (ret < 0) |
697 | ftdi_error_return(ret, "usb bulk write failed"); | |
a3da1d95 | 698 | |
c3d95b87 | 699 | total_written += ret; |
a3da1d95 GE |
700 | offset += write_size; |
701 | } | |
702 | ||
545820ce | 703 | return total_written; |
a3da1d95 GE |
704 | } |
705 | ||
1941414d TJ |
706 | /** |
707 | Configure write buffer chunk size. | |
708 | Default is 4096. | |
709 | ||
710 | \param ftdi pointer to ftdi_context | |
711 | \param chunksize Chunk size | |
a3da1d95 | 712 | |
1941414d TJ |
713 | \retval 0: all fine |
714 | */ | |
a8f46ddc TJ |
715 | int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
716 | { | |
948f9ada TJ |
717 | ftdi->writebuffer_chunksize = chunksize; |
718 | return 0; | |
719 | } | |
720 | ||
1941414d TJ |
721 | /** |
722 | Get write buffer chunk size. | |
723 | ||
724 | \param ftdi pointer to ftdi_context | |
725 | \param chunksize Pointer to store chunk size in | |
948f9ada | 726 | |
1941414d TJ |
727 | \retval 0: all fine |
728 | */ | |
a8f46ddc TJ |
729 | int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
730 | { | |
948f9ada TJ |
731 | *chunksize = ftdi->writebuffer_chunksize; |
732 | return 0; | |
733 | } | |
cbabb7d3 | 734 | |
1941414d TJ |
735 | /** |
736 | Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip. | |
737 | ||
738 | Automatically strips the two modem status bytes transfered during every read. | |
948f9ada | 739 | |
1941414d TJ |
740 | \param ftdi pointer to ftdi_context |
741 | \param buf Buffer to store data in | |
742 | \param size Size of the buffer | |
743 | ||
744 | \retval <0: error code from usb_bulk_read() | |
d77b0e94 | 745 | \retval 0: no data was available |
1941414d TJ |
746 | \retval >0: number of bytes read |
747 | ||
748 | \remark This function is not useful in bitbang mode. | |
749 | Use ftdi_read_pins() to get the current state of the pins. | |
750 | */ | |
a8f46ddc TJ |
751 | int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
752 | { | |
1c733d33 | 753 | int offset = 0, ret = 1, i, num_of_chunks, chunk_remains; |
d9f0cce7 | 754 | |
948f9ada TJ |
755 | // everything we want is still in the readbuffer? |
756 | if (size <= ftdi->readbuffer_remaining) { | |
d9f0cce7 TJ |
757 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size); |
758 | ||
759 | // Fix offsets | |
760 | ftdi->readbuffer_remaining -= size; | |
761 | ftdi->readbuffer_offset += size; | |
762 | ||
545820ce | 763 | /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */ |
d9f0cce7 TJ |
764 | |
765 | return size; | |
979a145c | 766 | } |
948f9ada TJ |
767 | // something still in the readbuffer, but not enough to satisfy 'size'? |
768 | if (ftdi->readbuffer_remaining != 0) { | |
d9f0cce7 | 769 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining); |
979a145c | 770 | |
d9f0cce7 TJ |
771 | // Fix offset |
772 | offset += ftdi->readbuffer_remaining; | |
948f9ada | 773 | } |
948f9ada | 774 | // do the actual USB read |
cbabb7d3 | 775 | while (offset < size && ret > 0) { |
d9f0cce7 TJ |
776 | ftdi->readbuffer_remaining = 0; |
777 | ftdi->readbuffer_offset = 0; | |
98452d97 TJ |
778 | /* returns how much received */ |
779 | ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout); | |
c3d95b87 TJ |
780 | if (ret < 0) |
781 | ftdi_error_return(ret, "usb bulk read failed"); | |
98452d97 | 782 | |
d9f0cce7 TJ |
783 | if (ret > 2) { |
784 | // skip FTDI status bytes. | |
785 | // Maybe stored in the future to enable modem use | |
1c733d33 TJ |
786 | num_of_chunks = ret / 64; |
787 | chunk_remains = ret % 64; | |
788 | //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset); | |
789 | ||
d9f0cce7 TJ |
790 | ftdi->readbuffer_offset += 2; |
791 | ret -= 2; | |
1c733d33 | 792 | |
fde0a89e | 793 | if (ret > 62) { |
1c733d33 TJ |
794 | for (i = 1; i < num_of_chunks; i++) |
795 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i, | |
796 | ftdi->readbuffer+ftdi->readbuffer_offset+64*i, | |
797 | 62); | |
798 | if (chunk_remains > 2) { | |
799 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i, | |
800 | ftdi->readbuffer+ftdi->readbuffer_offset+64*i, | |
801 | chunk_remains-2); | |
802 | ret -= 2*num_of_chunks; | |
803 | } else | |
804 | ret -= 2*(num_of_chunks-1)+chunk_remains; | |
805 | } | |
d9f0cce7 TJ |
806 | } else if (ret <= 2) { |
807 | // no more data to read? | |
808 | return offset; | |
809 | } | |
d9f0cce7 TJ |
810 | if (ret > 0) { |
811 | // data still fits in buf? | |
812 | if (offset+ret <= size) { | |
813 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret); | |
545820ce | 814 | //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]); |
d9f0cce7 TJ |
815 | offset += ret; |
816 | ||
53ad271d | 817 | /* Did we read exactly the right amount of bytes? */ |
d9f0cce7 | 818 | if (offset == size) |
c4446c36 TJ |
819 | //printf("read_data exact rem %d offset %d\n", |
820 | //ftdi->readbuffer_remaining, offset); | |
d9f0cce7 TJ |
821 | return offset; |
822 | } else { | |
823 | // only copy part of the data or size <= readbuffer_chunksize | |
824 | int part_size = size-offset; | |
825 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size); | |
98452d97 | 826 | |
d9f0cce7 TJ |
827 | ftdi->readbuffer_offset += part_size; |
828 | ftdi->readbuffer_remaining = ret-part_size; | |
829 | offset += part_size; | |
830 | ||
53ad271d TJ |
831 | /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n", |
832 | part_size, size, offset, ret, ftdi->readbuffer_remaining); */ | |
d9f0cce7 TJ |
833 | |
834 | return offset; | |
835 | } | |
836 | } | |
cbabb7d3 | 837 | } |
948f9ada | 838 | // never reached |
29c4af7f | 839 | return -127; |
a3da1d95 GE |
840 | } |
841 | ||
1941414d TJ |
842 | /** |
843 | Configure read buffer chunk size. | |
844 | Default is 4096. | |
845 | ||
846 | Automatically reallocates the buffer. | |
a3da1d95 | 847 | |
1941414d TJ |
848 | \param ftdi pointer to ftdi_context |
849 | \param chunksize Chunk size | |
850 | ||
851 | \retval 0: all fine | |
852 | */ | |
a8f46ddc TJ |
853 | int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
854 | { | |
29c4af7f TJ |
855 | unsigned char *new_buf; |
856 | ||
948f9ada TJ |
857 | // Invalidate all remaining data |
858 | ftdi->readbuffer_offset = 0; | |
859 | ftdi->readbuffer_remaining = 0; | |
860 | ||
c3d95b87 TJ |
861 | if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) |
862 | ftdi_error_return(-1, "out of memory for readbuffer"); | |
d9f0cce7 | 863 | |
948f9ada TJ |
864 | ftdi->readbuffer = new_buf; |
865 | ftdi->readbuffer_chunksize = chunksize; | |
866 | ||
867 | return 0; | |
868 | } | |
869 | ||
1941414d TJ |
870 | /** |
871 | Get read buffer chunk size. | |
948f9ada | 872 | |
1941414d TJ |
873 | \param ftdi pointer to ftdi_context |
874 | \param chunksize Pointer to store chunk size in | |
875 | ||
876 | \retval 0: all fine | |
877 | */ | |
a8f46ddc TJ |
878 | int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
879 | { | |
948f9ada TJ |
880 | *chunksize = ftdi->readbuffer_chunksize; |
881 | return 0; | |
882 | } | |
883 | ||
884 | ||
1941414d TJ |
885 | /** |
886 | Enable bitbang mode. | |
948f9ada | 887 | |
1941414d TJ |
888 | For advanced bitbang modes of the FT2232C chip use ftdi_set_bitmode(). |
889 | ||
890 | \param ftdi pointer to ftdi_context | |
891 | \param bitmask Bitmask to configure lines. | |
892 | HIGH/ON value configures a line as output. | |
893 | ||
894 | \retval 0: all fine | |
895 | \retval -1: can't enable bitbang mode | |
896 | */ | |
a8f46ddc TJ |
897 | int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) |
898 | { | |
a3da1d95 GE |
899 | unsigned short usb_val; |
900 | ||
d9f0cce7 | 901 | usb_val = bitmask; // low byte: bitmask |
3119537f TJ |
902 | /* FT2232C: Set bitbang_mode to 2 to enable SPI */ |
903 | usb_val |= (ftdi->bitbang_mode << 8); | |
904 | ||
c3d95b87 TJ |
905 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
906 | ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?"); | |
907 | ||
a3da1d95 GE |
908 | ftdi->bitbang_enabled = 1; |
909 | return 0; | |
910 | } | |
911 | ||
1941414d TJ |
912 | /** |
913 | Disable bitbang mode. | |
a3da1d95 | 914 | |
1941414d TJ |
915 | \param ftdi pointer to ftdi_context |
916 | ||
917 | \retval 0: all fine | |
918 | \retval -1: can't disable bitbang mode | |
919 | */ | |
a8f46ddc TJ |
920 | int ftdi_disable_bitbang(struct ftdi_context *ftdi) |
921 | { | |
c3d95b87 TJ |
922 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
923 | ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?"); | |
a3da1d95 GE |
924 | |
925 | ftdi->bitbang_enabled = 0; | |
926 | return 0; | |
927 | } | |
928 | ||
1941414d TJ |
929 | /** |
930 | Enable advanced bitbang mode for FT2232C chips. | |
a3da1d95 | 931 | |
1941414d TJ |
932 | \param ftdi pointer to ftdi_context |
933 | \param bitmask Bitmask to configure lines. | |
934 | HIGH/ON value configures a line as output. | |
935 | \param mode Bitbang mode: 1 for normal mode, 2 for SPI mode | |
936 | ||
937 | \retval 0: all fine | |
938 | \retval -1: can't enable bitbang mode | |
939 | */ | |
c4446c36 TJ |
940 | int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode) |
941 | { | |
942 | unsigned short usb_val; | |
943 | ||
944 | usb_val = bitmask; // low byte: bitmask | |
945 | usb_val |= (mode << 8); | |
946 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) | |
947 | ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?"); | |
948 | ||
949 | ftdi->bitbang_mode = mode; | |
950 | ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0; | |
951 | return 0; | |
952 | } | |
953 | ||
1941414d TJ |
954 | /** |
955 | Directly read pin state. Useful for bitbang mode. | |
956 | ||
957 | \param ftdi pointer to ftdi_context | |
958 | \param pins Pointer to store pins into | |
959 | ||
960 | \retval 0: all fine | |
961 | \retval -1: read pins failed | |
962 | */ | |
a8f46ddc TJ |
963 | int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) |
964 | { | |
85f3c596 | 965 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1) |
c3d95b87 | 966 | ftdi_error_return(-1, "read pins failed"); |
a3da1d95 | 967 | |
a3da1d95 GE |
968 | return 0; |
969 | } | |
970 | ||
1941414d TJ |
971 | /** |
972 | Set latency timer | |
973 | ||
974 | The FTDI chip keeps data in the internal buffer for a specific | |
975 | amount of time if the buffer is not full yet to decrease | |
976 | load on the usb bus. | |
a3da1d95 | 977 | |
1941414d TJ |
978 | \param ftdi pointer to ftdi_context |
979 | \param latency Value between 1 and 255 | |
980 | ||
981 | \retval 0: all fine | |
982 | \retval -1: latency out of range | |
983 | \retval -2: unable to set latency timer | |
984 | */ | |
a8f46ddc TJ |
985 | int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) |
986 | { | |
a3da1d95 GE |
987 | unsigned short usb_val; |
988 | ||
c3d95b87 TJ |
989 | if (latency < 1) |
990 | ftdi_error_return(-1, "latency out of range. Only valid for 1-255"); | |
a3da1d95 | 991 | |
d79d2e68 | 992 | usb_val = latency; |
c3d95b87 TJ |
993 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
994 | ftdi_error_return(-2, "unable to set latency timer"); | |
995 | ||
a3da1d95 GE |
996 | return 0; |
997 | } | |
998 | ||
1941414d TJ |
999 | /** |
1000 | Get latency timer | |
a3da1d95 | 1001 | |
1941414d TJ |
1002 | \param ftdi pointer to ftdi_context |
1003 | \param latency Pointer to store latency value in | |
1004 | ||
1005 | \retval 0: all fine | |
1006 | \retval -1: unable to get latency timer | |
1007 | */ | |
a8f46ddc TJ |
1008 | int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) |
1009 | { | |
a3da1d95 | 1010 | unsigned short usb_val; |
c3d95b87 TJ |
1011 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) |
1012 | ftdi_error_return(-1, "reading latency timer failed"); | |
a3da1d95 GE |
1013 | |
1014 | *latency = (unsigned char)usb_val; | |
1015 | return 0; | |
1016 | } | |
1017 | ||
1941414d TJ |
1018 | /** |
1019 | Init eeprom with default values. | |
a3da1d95 | 1020 | |
1941414d TJ |
1021 | \param eeprom Pointer to ftdi_eeprom |
1022 | */ | |
a8f46ddc TJ |
1023 | void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) |
1024 | { | |
f396dbad TJ |
1025 | eeprom->vendor_id = 0x0403; |
1026 | eeprom->product_id = 0x6001; | |
d9f0cce7 | 1027 | |
b8aa7b35 TJ |
1028 | eeprom->self_powered = 1; |
1029 | eeprom->remote_wakeup = 1; | |
1030 | eeprom->BM_type_chip = 1; | |
d9f0cce7 | 1031 | |
b8aa7b35 TJ |
1032 | eeprom->in_is_isochronous = 0; |
1033 | eeprom->out_is_isochronous = 0; | |
1034 | eeprom->suspend_pull_downs = 0; | |
d9f0cce7 | 1035 | |
b8aa7b35 TJ |
1036 | eeprom->use_serial = 0; |
1037 | eeprom->change_usb_version = 0; | |
f396dbad | 1038 | eeprom->usb_version = 0x0200; |
b8aa7b35 | 1039 | eeprom->max_power = 0; |
d9f0cce7 | 1040 | |
b8aa7b35 TJ |
1041 | eeprom->manufacturer = NULL; |
1042 | eeprom->product = NULL; | |
1043 | eeprom->serial = NULL; | |
1044 | } | |
1045 | ||
1941414d TJ |
1046 | /** |
1047 | Build binary output from ftdi_eeprom structure. | |
1048 | Output is suitable for ftdi_write_eeprom(). | |
b8aa7b35 | 1049 | |
1941414d TJ |
1050 | \param eeprom Pointer to ftdi_eeprom |
1051 | \param output Buffer of 128 bytes to store eeprom image to | |
1052 | ||
1053 | \retval >0: used eeprom size | |
1054 | \retval -1: eeprom size (128 bytes) exceeded by custom strings | |
b8aa7b35 | 1055 | */ |
a8f46ddc TJ |
1056 | int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) |
1057 | { | |
b8aa7b35 TJ |
1058 | unsigned char i, j; |
1059 | unsigned short checksum, value; | |
1060 | unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0; | |
1061 | int size_check; | |
1062 | ||
1063 | if (eeprom->manufacturer != NULL) | |
d9f0cce7 | 1064 | manufacturer_size = strlen(eeprom->manufacturer); |
b8aa7b35 | 1065 | if (eeprom->product != NULL) |
d9f0cce7 | 1066 | product_size = strlen(eeprom->product); |
b8aa7b35 | 1067 | if (eeprom->serial != NULL) |
d9f0cce7 | 1068 | serial_size = strlen(eeprom->serial); |
b8aa7b35 | 1069 | |
d9f0cce7 TJ |
1070 | size_check = 128; // eeprom is 128 bytes |
1071 | size_check -= 28; // 28 are always in use (fixed) | |
b8aa7b35 TJ |
1072 | size_check -= manufacturer_size*2; |
1073 | size_check -= product_size*2; | |
1074 | size_check -= serial_size*2; | |
1075 | ||
1076 | // eeprom size exceeded? | |
1077 | if (size_check < 0) | |
d9f0cce7 | 1078 | return (-1); |
b8aa7b35 TJ |
1079 | |
1080 | // empty eeprom | |
1081 | memset (output, 0, 128); | |
1082 | ||
1083 | // Addr 00: Stay 00 00 | |
1084 | // Addr 02: Vendor ID | |
1085 | output[0x02] = eeprom->vendor_id; | |
1086 | output[0x03] = eeprom->vendor_id >> 8; | |
1087 | ||
1088 | // Addr 04: Product ID | |
1089 | output[0x04] = eeprom->product_id; | |
1090 | output[0x05] = eeprom->product_id >> 8; | |
1091 | ||
1092 | // Addr 06: Device release number (0400h for BM features) | |
1093 | output[0x06] = 0x00; | |
d9f0cce7 | 1094 | |
b8aa7b35 | 1095 | if (eeprom->BM_type_chip == 1) |
d9f0cce7 | 1096 | output[0x07] = 0x04; |
b8aa7b35 | 1097 | else |
d9f0cce7 | 1098 | output[0x07] = 0x02; |
b8aa7b35 TJ |
1099 | |
1100 | // Addr 08: Config descriptor | |
1101 | // Bit 1: remote wakeup if 1 | |
1102 | // Bit 0: self powered if 1 | |
1103 | // | |
1104 | j = 0; | |
1105 | if (eeprom->self_powered == 1) | |
d9f0cce7 | 1106 | j = j | 1; |
b8aa7b35 | 1107 | if (eeprom->remote_wakeup == 1) |
d9f0cce7 | 1108 | j = j | 2; |
b8aa7b35 TJ |
1109 | output[0x08] = j; |
1110 | ||
1111 | // Addr 09: Max power consumption: max power = value * 2 mA | |
d9f0cce7 TJ |
1112 | output[0x09] = eeprom->max_power; |
1113 | ; | |
1114 | ||
b8aa7b35 TJ |
1115 | // Addr 0A: Chip configuration |
1116 | // Bit 7: 0 - reserved | |
1117 | // Bit 6: 0 - reserved | |
1118 | // Bit 5: 0 - reserved | |
1119 | // Bit 4: 1 - Change USB version | |
1120 | // Bit 3: 1 - Use the serial number string | |
1121 | // Bit 2: 1 - Enable suspend pull downs for lower power | |
1122 | // Bit 1: 1 - Out EndPoint is Isochronous | |
1123 | // Bit 0: 1 - In EndPoint is Isochronous | |
1124 | // | |
1125 | j = 0; | |
1126 | if (eeprom->in_is_isochronous == 1) | |
d9f0cce7 | 1127 | j = j | 1; |
b8aa7b35 | 1128 | if (eeprom->out_is_isochronous == 1) |
d9f0cce7 | 1129 | j = j | 2; |
b8aa7b35 | 1130 | if (eeprom->suspend_pull_downs == 1) |
d9f0cce7 | 1131 | j = j | 4; |
b8aa7b35 | 1132 | if (eeprom->use_serial == 1) |
d9f0cce7 | 1133 | j = j | 8; |
b8aa7b35 | 1134 | if (eeprom->change_usb_version == 1) |
d9f0cce7 | 1135 | j = j | 16; |
b8aa7b35 | 1136 | output[0x0A] = j; |
d9f0cce7 | 1137 | |
b8aa7b35 TJ |
1138 | // Addr 0B: reserved |
1139 | output[0x0B] = 0x00; | |
d9f0cce7 | 1140 | |
b8aa7b35 TJ |
1141 | // Addr 0C: USB version low byte when 0x0A bit 4 is set |
1142 | // Addr 0D: USB version high byte when 0x0A bit 4 is set | |
1143 | if (eeprom->change_usb_version == 1) { | |
1144 | output[0x0C] = eeprom->usb_version; | |
d9f0cce7 | 1145 | output[0x0D] = eeprom->usb_version >> 8; |
b8aa7b35 TJ |
1146 | } |
1147 | ||
1148 | ||
1149 | // Addr 0E: Offset of the manufacturer string + 0x80 | |
1150 | output[0x0E] = 0x14 + 0x80; | |
1151 | ||
1152 | // Addr 0F: Length of manufacturer string | |
1153 | output[0x0F] = manufacturer_size*2 + 2; | |
1154 | ||
1155 | // Addr 10: Offset of the product string + 0x80, calculated later | |
1156 | // Addr 11: Length of product string | |
1157 | output[0x11] = product_size*2 + 2; | |
1158 | ||
1159 | // Addr 12: Offset of the serial string + 0x80, calculated later | |
1160 | // Addr 13: Length of serial string | |
1161 | output[0x13] = serial_size*2 + 2; | |
1162 | ||
1163 | // Dynamic content | |
a862ddcf | 1164 | output[0x14] = manufacturer_size*2 + 2; |
d9f0cce7 TJ |
1165 | output[0x15] = 0x03; // type: string |
1166 | ||
b8aa7b35 | 1167 | i = 0x16, j = 0; |
d9f0cce7 | 1168 | |
b8aa7b35 TJ |
1169 | // Output manufacturer |
1170 | for (j = 0; j < manufacturer_size; j++) { | |
d9f0cce7 TJ |
1171 | output[i] = eeprom->manufacturer[j], i++; |
1172 | output[i] = 0x00, i++; | |
b8aa7b35 TJ |
1173 | } |
1174 | ||
1175 | // Output product name | |
d9f0cce7 | 1176 | output[0x10] = i + 0x80; // calculate offset |
b8aa7b35 TJ |
1177 | output[i] = product_size*2 + 2, i++; |
1178 | output[i] = 0x03, i++; | |
1179 | for (j = 0; j < product_size; j++) { | |
d9f0cce7 TJ |
1180 | output[i] = eeprom->product[j], i++; |
1181 | output[i] = 0x00, i++; | |
b8aa7b35 | 1182 | } |
d9f0cce7 | 1183 | |
b8aa7b35 | 1184 | // Output serial |
d9f0cce7 | 1185 | output[0x12] = i + 0x80; // calculate offset |
b8aa7b35 TJ |
1186 | output[i] = serial_size*2 + 2, i++; |
1187 | output[i] = 0x03, i++; | |
1188 | for (j = 0; j < serial_size; j++) { | |
d9f0cce7 TJ |
1189 | output[i] = eeprom->serial[j], i++; |
1190 | output[i] = 0x00, i++; | |
b8aa7b35 TJ |
1191 | } |
1192 | ||
1193 | // calculate checksum | |
1194 | checksum = 0xAAAA; | |
d9f0cce7 | 1195 | |
b8aa7b35 | 1196 | for (i = 0; i < 63; i++) { |
d9f0cce7 TJ |
1197 | value = output[i*2]; |
1198 | value += output[(i*2)+1] << 8; | |
b8aa7b35 | 1199 | |
d9f0cce7 TJ |
1200 | checksum = value^checksum; |
1201 | checksum = (checksum << 1) | (checksum >> 15); | |
b8aa7b35 TJ |
1202 | } |
1203 | ||
1204 | output[0x7E] = checksum; | |
d9f0cce7 | 1205 | output[0x7F] = checksum >> 8; |
b8aa7b35 | 1206 | |
8ed61121 | 1207 | return size_check; |
b8aa7b35 TJ |
1208 | } |
1209 | ||
1941414d TJ |
1210 | /** |
1211 | Read eeprom | |
1212 | ||
1213 | \param ftdi pointer to ftdi_context | |
1214 | \param eeprom Pointer to store eeprom into | |
b8aa7b35 | 1215 | |
1941414d TJ |
1216 | \retval 0: all fine |
1217 | \retval -1: read failed | |
1218 | */ | |
a8f46ddc TJ |
1219 | int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) |
1220 | { | |
a3da1d95 GE |
1221 | int i; |
1222 | ||
1223 | for (i = 0; i < 64; i++) { | |
c3d95b87 TJ |
1224 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) |
1225 | ftdi_error_return(-1, "reading eeprom failed"); | |
a3da1d95 GE |
1226 | } |
1227 | ||
1228 | return 0; | |
1229 | } | |
1230 | ||
cb6250fa TJ |
1231 | /* |
1232 | ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID | |
1233 | Function is only used internally | |
1234 | \internal | |
1235 | */ | |
1236 | static unsigned char ftdi_read_chipid_shift(unsigned char value) | |
1237 | { | |
1238 | return ((value & 1) << 1) | | |
1239 | ((value & 2) << 5) | | |
1240 | ((value & 4) >> 2) | | |
1241 | ((value & 8) << 4) | | |
1242 | ((value & 16) >> 1) | | |
1243 | ((value & 32) >> 1) | | |
1244 | ((value & 64) >> 4) | | |
1245 | ((value & 128) >> 2); | |
1246 | } | |
1247 | ||
1248 | /** | |
1249 | Read the FTDIChip-ID from R-type devices | |
1250 | ||
1251 | \param ftdi pointer to ftdi_context | |
1252 | \param chipid Pointer to store FTDIChip-ID | |
1253 | ||
1254 | \retval 0: all fine | |
1255 | \retval -1: read failed | |
1256 | */ | |
1257 | int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid) | |
1258 | { | |
1259 | unsigned int a = 0, b = 0, result = -1; | |
1260 | ||
1261 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, 0x43, (char *)&a, 2, ftdi->usb_read_timeout) == 2) | |
1262 | { | |
1263 | a = a << 8 | a >> 8; | |
1264 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, 0x44, (char *)&b, 2, ftdi->usb_read_timeout) == 2) | |
1265 | { | |
1266 | b = b << 8 | b >> 8; | |
1267 | a = (a << 16) | b; | |
912d50ca TJ |
1268 | a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8 |
1269 | | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24; | |
cb6250fa TJ |
1270 | *chipid = a ^ 0xa5f0f7d1; |
1271 | result = 0; | |
1272 | } | |
1273 | } | |
1274 | ||
1275 | if (result != 0) | |
1276 | ftdi_error_return(result, "read of FTDIChip-ID failed"); | |
1277 | ||
1278 | return 0; | |
1279 | } | |
1280 | ||
1941414d TJ |
1281 | /** |
1282 | Write eeprom | |
a3da1d95 | 1283 | |
1941414d TJ |
1284 | \param ftdi pointer to ftdi_context |
1285 | \param eeprom Pointer to read eeprom from | |
1286 | ||
1287 | \retval 0: all fine | |
1288 | \retval -1: read failed | |
1289 | */ | |
a8f46ddc TJ |
1290 | int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) |
1291 | { | |
a3da1d95 GE |
1292 | unsigned short usb_val; |
1293 | int i; | |
1294 | ||
1295 | for (i = 0; i < 64; i++) { | |
d9f0cce7 TJ |
1296 | usb_val = eeprom[i*2]; |
1297 | usb_val += eeprom[(i*2)+1] << 8; | |
c3d95b87 TJ |
1298 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) |
1299 | ftdi_error_return(-1, "unable to write eeprom"); | |
a3da1d95 GE |
1300 | } |
1301 | ||
1302 | return 0; | |
1303 | } | |
1304 | ||
1941414d TJ |
1305 | /** |
1306 | Erase eeprom | |
a3da1d95 | 1307 | |
1941414d TJ |
1308 | \param ftdi pointer to ftdi_context |
1309 | ||
1310 | \retval 0: all fine | |
1311 | \retval -1: erase failed | |
1312 | */ | |
a8f46ddc TJ |
1313 | int ftdi_erase_eeprom(struct ftdi_context *ftdi) |
1314 | { | |
c3d95b87 TJ |
1315 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) |
1316 | ftdi_error_return(-1, "unable to erase eeprom"); | |
a3da1d95 GE |
1317 | |
1318 | return 0; | |
1319 | } | |
c3d95b87 | 1320 | |
1941414d TJ |
1321 | /** |
1322 | Get string representation for last error code | |
c3d95b87 | 1323 | |
1941414d TJ |
1324 | \param ftdi pointer to ftdi_context |
1325 | ||
1326 | \retval Pointer to error string | |
1327 | */ | |
c3d95b87 TJ |
1328 | char *ftdi_get_error_string (struct ftdi_context *ftdi) |
1329 | { | |
1330 | return ftdi->error_str; | |
1331 | } | |
a01d31e2 | 1332 | |
9bec2387 TJ |
1333 | /* |
1334 | Flow control code by Lorenz Moesenlechner (lorenz@hcilab.org) | |
1335 | and Matthias Kranz (matthias@hcilab.org) | |
1336 | */ | |
1941414d TJ |
1337 | /** |
1338 | Set flowcontrol for ftdi chip | |
a01d31e2 | 1339 | |
1941414d TJ |
1340 | \param ftdi pointer to ftdi_context |
1341 | \param flowctrl flow control to use. should be | |
1342 | SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS | |
1343 | ||
1344 | \retval 0: all fine | |
1345 | \retval -1: set flow control failed | |
1346 | */ | |
a01d31e2 TJ |
1347 | int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl) |
1348 | { | |
1349 | if (usb_control_msg(ftdi->usb_dev, SIO_SET_FLOW_CTRL_REQUEST_TYPE, | |
d2f10023 TJ |
1350 | SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->interface), |
1351 | NULL, 0, ftdi->usb_write_timeout) != 0) | |
1352 | ftdi_error_return(-1, "set flow control failed"); | |
a01d31e2 TJ |
1353 | |
1354 | return 0; | |
1355 | } | |
1356 | ||
1941414d TJ |
1357 | /** |
1358 | Set dtr line | |
1359 | ||
1360 | \param ftdi pointer to ftdi_context | |
1361 | \param state state to set line to (1 or 0) | |
1362 | ||
1363 | \retval 0: all fine | |
1364 | \retval -1: set dtr failed | |
1365 | */ | |
a01d31e2 TJ |
1366 | int ftdi_setdtr(struct ftdi_context *ftdi, int state) |
1367 | { | |
1368 | unsigned short usb_val; | |
1369 | ||
d2f10023 | 1370 | if (state) |
a01d31e2 TJ |
1371 | usb_val = SIO_SET_DTR_HIGH; |
1372 | else | |
1373 | usb_val = SIO_SET_DTR_LOW; | |
1374 | ||
1375 | if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE, | |
d2f10023 TJ |
1376 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface, |
1377 | NULL, 0, ftdi->usb_write_timeout) != 0) | |
1378 | ftdi_error_return(-1, "set dtr failed"); | |
a01d31e2 TJ |
1379 | |
1380 | return 0; | |
1381 | } | |
1382 | ||
1941414d TJ |
1383 | /** |
1384 | Set rts line | |
1385 | ||
1386 | \param ftdi pointer to ftdi_context | |
1387 | \param state state to set line to (1 or 0) | |
1388 | ||
1389 | \retval 0: all fine | |
1390 | \retval -1 set rts failed | |
1391 | */ | |
a01d31e2 TJ |
1392 | int ftdi_setrts(struct ftdi_context *ftdi, int state) |
1393 | { | |
1394 | unsigned short usb_val; | |
1395 | ||
d2f10023 | 1396 | if (state) |
a01d31e2 TJ |
1397 | usb_val = SIO_SET_RTS_HIGH; |
1398 | else | |
1399 | usb_val = SIO_SET_RTS_LOW; | |
1400 | ||
d2f10023 TJ |
1401 | if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE, |
1402 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface, | |
1403 | NULL, 0, ftdi->usb_write_timeout) != 0) | |
1404 | ftdi_error_return(-1, "set of rts failed"); | |
a01d31e2 TJ |
1405 | |
1406 | return 0; | |
1407 | } | |
b5ec1820 TJ |
1408 | |
1409 | /* @} end of doxygen libftdi group */ |