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