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