Commit | Line | Data |
---|---|---|
a3da1d95 GE |
1 | /*************************************************************************** |
2 | ftdi.c - description | |
3 | ------------------- | |
4 | begin : Fri Apr 4 2003 | |
5 | copyright : (C) 2003 by Intra2net AG | |
5fdb1cb1 | 6 | email : opensource@intra2net.com |
a3da1d95 GE |
7 | ***************************************************************************/ |
8 | ||
9 | /*************************************************************************** | |
10 | * * | |
11 | * This program is free software; you can redistribute it and/or modify * | |
12 | * it under the terms of the GNU Lesser General Public License * | |
13 | * version 2.1 as published by the Free Software Foundation; * | |
14 | * * | |
15 | ***************************************************************************/ | |
d9f0cce7 | 16 | |
98452d97 | 17 | #include <usb.h> |
a8f46ddc | 18 | #include <string.h> |
d2f10023 | 19 | #include <errno.h> |
0e302db6 | 20 | |
98452d97 | 21 | #include "ftdi.h" |
a3da1d95 | 22 | |
21abaf2e | 23 | #define ftdi_error_return(code, str) do { \ |
2f73e59f | 24 | ftdi->error_str = str; \ |
21abaf2e | 25 | return code; \ |
d2f10023 | 26 | } while(0); |
c3d95b87 TJ |
27 | |
28 | ||
4837f98a TJ |
29 | /* ftdi_init |
30 | ||
60b7513e | 31 | Initializes a ftdi_context. |
4837f98a TJ |
32 | |
33 | Return codes: | |
34 | 0: All fine | |
35 | -1: Couldn't allocate read buffer | |
948f9ada | 36 | */ |
a8f46ddc TJ |
37 | int ftdi_init(struct ftdi_context *ftdi) |
38 | { | |
98452d97 | 39 | ftdi->usb_dev = NULL; |
545820ce TJ |
40 | ftdi->usb_read_timeout = 5000; |
41 | ftdi->usb_write_timeout = 5000; | |
a3da1d95 | 42 | |
53ad271d | 43 | ftdi->type = TYPE_BM; /* chip type */ |
a3da1d95 GE |
44 | ftdi->baudrate = -1; |
45 | ftdi->bitbang_enabled = 0; | |
46 | ||
948f9ada TJ |
47 | ftdi->readbuffer = NULL; |
48 | ftdi->readbuffer_offset = 0; | |
49 | ftdi->readbuffer_remaining = 0; | |
50 | ftdi->writebuffer_chunksize = 4096; | |
51 | ||
545820ce TJ |
52 | ftdi->interface = 0; |
53 | ftdi->index = 0; | |
54 | ftdi->in_ep = 0x02; | |
55 | ftdi->out_ep = 0x81; | |
3119537f | 56 | ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */ |
53ad271d | 57 | |
a3da1d95 GE |
58 | ftdi->error_str = NULL; |
59 | ||
1c733d33 TJ |
60 | /* All fine. Now allocate the readbuffer */ |
61 | return ftdi_read_data_set_chunksize(ftdi, 4096); | |
948f9ada | 62 | } |
4837f98a | 63 | |
46860c4c | 64 | /* ftdi_set_interface |
4837f98a | 65 | |
c4446c36 | 66 | Call after ftdi_init |
4837f98a | 67 | |
c4446c36 | 68 | Open selected channels on a chip, otherwise use first channel |
0ce2f5fa TJ |
69 | 0: all fine |
70 | -1: unknown interface | |
c4446c36 | 71 | */ |
0ce2f5fa | 72 | int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface) |
c4446c36 TJ |
73 | { |
74 | switch (interface) { | |
75 | case INTERFACE_ANY: | |
76 | case INTERFACE_A: | |
0ce2f5fa | 77 | /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */ |
c4446c36 TJ |
78 | break; |
79 | case INTERFACE_B: | |
80 | ftdi->interface = 1; | |
81 | ftdi->index = INTERFACE_B; | |
82 | ftdi->in_ep = 0x04; | |
83 | ftdi->out_ep = 0x83; | |
84 | break; | |
85 | default: | |
86 | ftdi_error_return(-1, "Unknown interface"); | |
87 | } | |
88 | return 0; | |
89 | } | |
948f9ada | 90 | |
4837f98a TJ |
91 | /* ftdi_deinit |
92 | ||
93 | Deinitializes a ftdi_context. | |
94 | */ | |
a8f46ddc TJ |
95 | void ftdi_deinit(struct ftdi_context *ftdi) |
96 | { | |
948f9ada | 97 | if (ftdi->readbuffer != NULL) { |
d9f0cce7 TJ |
98 | free(ftdi->readbuffer); |
99 | ftdi->readbuffer = NULL; | |
948f9ada | 100 | } |
a3da1d95 GE |
101 | } |
102 | ||
4837f98a TJ |
103 | /* ftdi_set_usbdev |
104 | ||
105 | Use an already open device. | |
106 | */ | |
a8f46ddc TJ |
107 | void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) |
108 | { | |
98452d97 TJ |
109 | ftdi->usb_dev = usb; |
110 | } | |
111 | ||
112 | ||
edb82cbf TJ |
113 | /* ftdi_usb_find_all |
114 | ||
115 | Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which | |
116 | needs to be deallocated by ftdi_list_free after use. | |
117 | ||
118 | Return codes: | |
119 | >0: number of devices found | |
120 | -1: usb_find_busses() failed | |
121 | -2: usb_find_devices() failed | |
122 | -3: out of memory | |
123 | */ | |
d2f10023 | 124 | int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product) |
edb82cbf TJ |
125 | { |
126 | struct ftdi_device_list **curdev; | |
127 | struct usb_bus *bus; | |
128 | struct usb_device *dev; | |
129 | int count = 0; | |
d2f10023 | 130 | |
edb82cbf TJ |
131 | usb_init(); |
132 | if (usb_find_busses() < 0) | |
133 | ftdi_error_return(-1, "usb_find_busses() failed"); | |
134 | if (usb_find_devices() < 0) | |
135 | ftdi_error_return(-2, "usb_find_devices() failed"); | |
136 | ||
137 | curdev = devlist; | |
138 | for (bus = usb_busses; bus; bus = bus->next) { | |
139 | for (dev = bus->devices; dev; dev = dev->next) { | |
140 | if (dev->descriptor.idVendor == vendor | |
141 | && dev->descriptor.idProduct == product) | |
142 | { | |
143 | *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list)); | |
144 | if (!*curdev) | |
145 | ftdi_error_return(-3, "out of memory"); | |
d2f10023 | 146 | |
edb82cbf TJ |
147 | (*curdev)->next = NULL; |
148 | (*curdev)->dev = dev; | |
149 | ||
150 | curdev = &(*curdev)->next; | |
151 | count++; | |
152 | } | |
153 | } | |
154 | } | |
d2f10023 | 155 | |
edb82cbf TJ |
156 | return count; |
157 | } | |
158 | ||
159 | /* ftdi_list_free | |
160 | ||
161 | Frees a created device list. | |
162 | */ | |
d2f10023 | 163 | void ftdi_list_free(struct ftdi_device_list **devlist) |
edb82cbf TJ |
164 | { |
165 | struct ftdi_device_list **curdev; | |
166 | for (; *devlist == NULL; devlist = curdev) { | |
167 | curdev = &(*devlist)->next; | |
168 | free(*devlist); | |
169 | } | |
170 | ||
171 | devlist = NULL; | |
172 | } | |
173 | ||
d2f10023 | 174 | /* ftdi_usb_open_dev |
7b18bef6 TJ |
175 | |
176 | Opens a ftdi device given by a usb_device. | |
177 | ||
178 | Return codes: | |
179 | 0: all fine | |
180 | -4: unable to open device | |
181 | -5: unable to claim device | |
182 | -6: reset failed | |
183 | -7: set baudrate failed | |
184 | */ | |
185 | int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev) | |
186 | { | |
d2f10023 | 187 | int detach_errno = 0; |
7b18bef6 TJ |
188 | if (!(ftdi->usb_dev = usb_open(dev))) |
189 | ftdi_error_return(-4, "usb_open() failed"); | |
d2f10023 TJ |
190 | |
191 | #ifdef LIBUSB_HAS_GET_DRIVER_NP | |
192 | // Try to detach ftdi_sio kernel module | |
193 | // Returns ENODATA if driver is not loaded | |
194 | if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA) | |
195 | detach_errno = errno; | |
196 | #endif | |
197 | ||
7b18bef6 TJ |
198 | if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) { |
199 | usb_close (ftdi->usb_dev); | |
d2f10023 TJ |
200 | if (detach_errno == EPERM) { |
201 | ftdi_error_return(-8, "inappropriate permissions on device!"); | |
202 | } else { | |
203 | ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!"); | |
204 | } | |
7b18bef6 TJ |
205 | } |
206 | ||
207 | if (ftdi_usb_reset (ftdi) != 0) { | |
208 | usb_close (ftdi->usb_dev); | |
209 | ftdi_error_return(-6, "ftdi_usb_reset failed"); | |
210 | } | |
211 | ||
212 | if (ftdi_set_baudrate (ftdi, 9600) != 0) { | |
213 | usb_close (ftdi->usb_dev); | |
214 | ftdi_error_return(-7, "set baudrate failed"); | |
215 | } | |
216 | ||
217 | // Try to guess chip type | |
218 | // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0 | |
219 | if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200 | |
220 | && dev->descriptor.iSerialNumber == 0)) | |
221 | ftdi->type = TYPE_BM; | |
222 | else if (dev->descriptor.bcdDevice == 0x200) | |
223 | ftdi->type = TYPE_AM; | |
224 | else if (dev->descriptor.bcdDevice == 0x500) { | |
225 | ftdi->type = TYPE_2232C; | |
226 | if (!ftdi->index) | |
227 | ftdi->index = INTERFACE_A; | |
228 | } | |
229 | ||
230 | ftdi_error_return(0, "all fine"); | |
231 | } | |
232 | ||
9b1c7f18 | 233 | /* ftdi_usb_open |
e6408702 TJ |
234 | |
235 | Opens the first device with a given vendor and product ids. | |
9b1c7f18 TJ |
236 | |
237 | Return codes: | |
238 | See ftdi_usb_open_desc() | |
239 | */ | |
edb82cbf TJ |
240 | int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) |
241 | { | |
242 | return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL); | |
243 | } | |
244 | ||
245 | /* ftdi_usb_open_desc | |
246 | ||
247 | Opens the first device with a given, vendor id, product id, | |
248 | description and serial. | |
4837f98a TJ |
249 | |
250 | Return codes: | |
251 | 0: all fine | |
252 | -1: usb_find_busses() failed | |
253 | -2: usb_find_devices() failed | |
254 | -3: usb device not found | |
255 | -4: unable to open device | |
256 | -5: unable to claim device | |
257 | -6: reset failed | |
258 | -7: set baudrate failed | |
259 | -8: get product description failed | |
260 | -9: get serial number failed | |
edb82cbf | 261 | -10: unable to close device |
a3da1d95 | 262 | */ |
04e1ea0a | 263 | int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, |
a8f46ddc TJ |
264 | const char* description, const char* serial) |
265 | { | |
98452d97 TJ |
266 | struct usb_bus *bus; |
267 | struct usb_device *dev; | |
c3d95b87 | 268 | char string[256]; |
98452d97 TJ |
269 | |
270 | usb_init(); | |
271 | ||
c3d95b87 TJ |
272 | if (usb_find_busses() < 0) |
273 | ftdi_error_return(-1, "usb_find_busses() failed"); | |
c3d95b87 | 274 | if (usb_find_devices() < 0) |
edb82cbf | 275 | ftdi_error_return(-2, "usb_find_devices() failed"); |
a3da1d95 | 276 | |
98452d97 TJ |
277 | for (bus = usb_busses; bus; bus = bus->next) { |
278 | for (dev = bus->devices; dev; dev = dev->next) { | |
a8f46ddc | 279 | if (dev->descriptor.idVendor == vendor |
c3d95b87 TJ |
280 | && dev->descriptor.idProduct == product) { |
281 | if (!(ftdi->usb_dev = usb_open(dev))) | |
282 | ftdi_error_return(-4, "usb_open() failed"); | |
283 | ||
a8f46ddc TJ |
284 | if (description != NULL) { |
285 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) { | |
c3d95b87 TJ |
286 | usb_close (ftdi->usb_dev); |
287 | ftdi_error_return(-8, "unable to fetch product description"); | |
98452d97 | 288 | } |
a8f46ddc | 289 | if (strncmp(string, description, sizeof(string)) != 0) { |
edb82cbf TJ |
290 | if (usb_close (ftdi->usb_dev) != 0) |
291 | ftdi_error_return(-10, "unable to close device"); | |
a8f46ddc TJ |
292 | continue; |
293 | } | |
294 | } | |
295 | if (serial != NULL) { | |
296 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) { | |
c3d95b87 TJ |
297 | usb_close (ftdi->usb_dev); |
298 | ftdi_error_return(-9, "unable to fetch serial number"); | |
a8f46ddc TJ |
299 | } |
300 | if (strncmp(string, serial, sizeof(string)) != 0) { | |
a8f46ddc | 301 | if (usb_close (ftdi->usb_dev) != 0) |
edb82cbf | 302 | ftdi_error_return(-10, "unable to close device"); |
a8f46ddc TJ |
303 | continue; |
304 | } | |
305 | } | |
98452d97 | 306 | |
edb82cbf TJ |
307 | if (usb_close (ftdi->usb_dev) != 0) |
308 | ftdi_error_return(-10, "unable to close device"); | |
d2f10023 | 309 | |
edb82cbf | 310 | return ftdi_usb_open_dev(ftdi, dev); |
98452d97 TJ |
311 | } |
312 | } | |
98452d97 | 313 | } |
a3da1d95 | 314 | |
98452d97 | 315 | // device not found |
c3d95b87 | 316 | ftdi_error_return(-3, "device not found"); |
a3da1d95 GE |
317 | } |
318 | ||
4837f98a | 319 | /* ftdi_usb_reset |
a3da1d95 | 320 | |
4837f98a TJ |
321 | Resets the ftdi device. |
322 | ||
323 | Return codes: | |
324 | 0: all fine | |
325 | -1: FTDI reset failed | |
326 | */ | |
edb82cbf | 327 | int ftdi_usb_reset(struct ftdi_context *ftdi) |
a8f46ddc | 328 | { |
c3d95b87 TJ |
329 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
330 | ftdi_error_return(-1,"FTDI reset failed"); | |
331 | ||
545820ce | 332 | // Invalidate data in the readbuffer |
bfcee05b TJ |
333 | ftdi->readbuffer_offset = 0; |
334 | ftdi->readbuffer_remaining = 0; | |
335 | ||
a3da1d95 GE |
336 | return 0; |
337 | } | |
338 | ||
4837f98a TJ |
339 | /* ftdi_usb_purge_buffers |
340 | ||
341 | Cleans the buffers of the ftdi device. | |
342 | ||
343 | Return codes: | |
344 | 0: all fine | |
345 | -1: write buffer purge failed | |
346 | -2: read buffer purge failed | |
347 | */ | |
a8f46ddc TJ |
348 | int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) |
349 | { | |
c3d95b87 TJ |
350 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
351 | ftdi_error_return(-1, "FTDI purge of RX buffer failed"); | |
352 | ||
545820ce | 353 | // Invalidate data in the readbuffer |
bfcee05b TJ |
354 | ftdi->readbuffer_offset = 0; |
355 | ftdi->readbuffer_remaining = 0; | |
a60be878 | 356 | |
c3d95b87 TJ |
357 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
358 | ftdi_error_return(-2, "FTDI purge of TX buffer failed"); | |
545820ce | 359 | |
a60be878 TJ |
360 | return 0; |
361 | } | |
a3da1d95 | 362 | |
4837f98a TJ |
363 | /* ftdi_usb_close |
364 | ||
365 | Closes the ftdi device. | |
366 | ||
367 | Return codes: | |
368 | 0: all fine | |
369 | -1: usb_release failed | |
370 | -2: usb_close failed | |
a3da1d95 | 371 | */ |
a8f46ddc TJ |
372 | int ftdi_usb_close(struct ftdi_context *ftdi) |
373 | { | |
a3da1d95 GE |
374 | int rtn = 0; |
375 | ||
98452d97 | 376 | if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0) |
a3da1d95 | 377 | rtn = -1; |
98452d97 TJ |
378 | |
379 | if (usb_close (ftdi->usb_dev) != 0) | |
a3da1d95 | 380 | rtn = -2; |
98452d97 | 381 | |
a3da1d95 GE |
382 | return rtn; |
383 | } | |
384 | ||
385 | ||
386 | /* | |
53ad271d TJ |
387 | ftdi_convert_baudrate returns nearest supported baud rate to that requested. |
388 | Function is only used internally | |
389 | */ | |
0126d22e | 390 | static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, |
a8f46ddc TJ |
391 | unsigned short *value, unsigned short *index) |
392 | { | |
53ad271d TJ |
393 | static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1}; |
394 | static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3}; | |
395 | static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7}; | |
396 | int divisor, best_divisor, best_baud, best_baud_diff; | |
397 | unsigned long encoded_divisor; | |
398 | int i; | |
399 | ||
400 | if (baudrate <= 0) { | |
401 | // Return error | |
402 | return -1; | |
403 | } | |
404 | ||
405 | divisor = 24000000 / baudrate; | |
406 | ||
0126d22e | 407 | if (ftdi->type == TYPE_AM) { |
53ad271d TJ |
408 | // Round down to supported fraction (AM only) |
409 | divisor -= am_adjust_dn[divisor & 7]; | |
410 | } | |
411 | ||
412 | // Try this divisor and the one above it (because division rounds down) | |
413 | best_divisor = 0; | |
414 | best_baud = 0; | |
415 | best_baud_diff = 0; | |
416 | for (i = 0; i < 2; i++) { | |
417 | int try_divisor = divisor + i; | |
418 | int baud_estimate; | |
419 | int baud_diff; | |
420 | ||
421 | // Round up to supported divisor value | |
df612d35 | 422 | if (try_divisor <= 8) { |
53ad271d TJ |
423 | // Round up to minimum supported divisor |
424 | try_divisor = 8; | |
0126d22e | 425 | } else if (ftdi->type != TYPE_AM && try_divisor < 12) { |
53ad271d TJ |
426 | // BM doesn't support divisors 9 through 11 inclusive |
427 | try_divisor = 12; | |
428 | } else if (divisor < 16) { | |
429 | // AM doesn't support divisors 9 through 15 inclusive | |
430 | try_divisor = 16; | |
431 | } else { | |
0126d22e | 432 | if (ftdi->type == TYPE_AM) { |
53ad271d TJ |
433 | // Round up to supported fraction (AM only) |
434 | try_divisor += am_adjust_up[try_divisor & 7]; | |
435 | if (try_divisor > 0x1FFF8) { | |
436 | // Round down to maximum supported divisor value (for AM) | |
437 | try_divisor = 0x1FFF8; | |
438 | } | |
439 | } else { | |
440 | if (try_divisor > 0x1FFFF) { | |
441 | // Round down to maximum supported divisor value (for BM) | |
442 | try_divisor = 0x1FFFF; | |
443 | } | |
444 | } | |
445 | } | |
446 | // Get estimated baud rate (to nearest integer) | |
447 | baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor; | |
448 | // Get absolute difference from requested baud rate | |
449 | if (baud_estimate < baudrate) { | |
450 | baud_diff = baudrate - baud_estimate; | |
451 | } else { | |
452 | baud_diff = baud_estimate - baudrate; | |
453 | } | |
454 | if (i == 0 || baud_diff < best_baud_diff) { | |
455 | // Closest to requested baud rate so far | |
456 | best_divisor = try_divisor; | |
457 | best_baud = baud_estimate; | |
458 | best_baud_diff = baud_diff; | |
459 | if (baud_diff == 0) { | |
460 | // Spot on! No point trying | |
461 | break; | |
462 | } | |
463 | } | |
464 | } | |
465 | // Encode the best divisor value | |
466 | encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14); | |
467 | // Deal with special cases for encoded value | |
468 | if (encoded_divisor == 1) { | |
4837f98a | 469 | encoded_divisor = 0; // 3000000 baud |
53ad271d | 470 | } else if (encoded_divisor == 0x4001) { |
4837f98a | 471 | encoded_divisor = 1; // 2000000 baud (BM only) |
53ad271d TJ |
472 | } |
473 | // Split into "value" and "index" values | |
474 | *value = (unsigned short)(encoded_divisor & 0xFFFF); | |
de22df10 | 475 | if(ftdi->type == TYPE_2232C) { |
0126d22e TJ |
476 | *index = (unsigned short)(encoded_divisor >> 8); |
477 | *index &= 0xFF00; | |
a9c57c05 | 478 | *index |= ftdi->index; |
0126d22e TJ |
479 | } |
480 | else | |
481 | *index = (unsigned short)(encoded_divisor >> 16); | |
c3d95b87 | 482 | |
53ad271d TJ |
483 | // Return the nearest baud rate |
484 | return best_baud; | |
485 | } | |
486 | ||
487 | /* | |
4837f98a TJ |
488 | ftdi_set_baudrate |
489 | ||
490 | Sets the chip baudrate | |
491 | ||
492 | Return codes: | |
a3da1d95 GE |
493 | 0: all fine |
494 | -1: invalid baudrate | |
495 | -2: setting baudrate failed | |
496 | */ | |
a8f46ddc TJ |
497 | int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) |
498 | { | |
53ad271d TJ |
499 | unsigned short value, index; |
500 | int actual_baudrate; | |
a3da1d95 GE |
501 | |
502 | if (ftdi->bitbang_enabled) { | |
503 | baudrate = baudrate*4; | |
504 | } | |
505 | ||
25707904 | 506 | actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index); |
c3d95b87 TJ |
507 | if (actual_baudrate <= 0) |
508 | ftdi_error_return (-1, "Silly baudrate <= 0."); | |
a3da1d95 | 509 | |
53ad271d TJ |
510 | // Check within tolerance (about 5%) |
511 | if ((actual_baudrate * 2 < baudrate /* Catch overflows */ ) | |
512 | || ((actual_baudrate < baudrate) | |
513 | ? (actual_baudrate * 21 < baudrate * 20) | |
c3d95b87 TJ |
514 | : (baudrate * 21 < actual_baudrate * 20))) |
515 | ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4"); | |
545820ce | 516 | |
c3d95b87 TJ |
517 | if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0) |
518 | ftdi_error_return (-2, "Setting new baudrate failed"); | |
a3da1d95 GE |
519 | |
520 | ftdi->baudrate = baudrate; | |
521 | return 0; | |
522 | } | |
523 | ||
2f73e59f | 524 | /* |
4837f98a TJ |
525 | ftdi_set_line_property |
526 | ||
527 | set (RS232) line characteristics by Alain Abbas | |
528 | ||
529 | Return codes: | |
530 | 0: all fine | |
531 | -1: Setting line property failed | |
2f73e59f TJ |
532 | */ |
533 | int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits, | |
d2f10023 | 534 | enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity) |
2f73e59f TJ |
535 | { |
536 | unsigned short value = bits; | |
537 | ||
538 | switch(parity) { | |
539 | case NONE: | |
540 | value |= (0x00 << 8); | |
541 | break; | |
542 | case ODD: | |
543 | value |= (0x01 << 8); | |
544 | break; | |
545 | case EVEN: | |
546 | value |= (0x02 << 8); | |
547 | break; | |
548 | case MARK: | |
549 | value |= (0x03 << 8); | |
550 | break; | |
551 | case SPACE: | |
552 | value |= (0x04 << 8); | |
553 | break; | |
554 | } | |
d2f10023 | 555 | |
2f73e59f TJ |
556 | switch(sbit) { |
557 | case STOP_BIT_1: | |
558 | value |= (0x00 << 11); | |
559 | break; | |
560 | case STOP_BIT_15: | |
561 | value |= (0x01 << 11); | |
562 | break; | |
563 | case STOP_BIT_2: | |
564 | value |= (0x02 << 11); | |
565 | break; | |
566 | } | |
d2f10023 | 567 | |
2f73e59f TJ |
568 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
569 | ftdi_error_return (-1, "Setting new line property failed"); | |
d2f10023 | 570 | |
2f73e59f TJ |
571 | return 0; |
572 | } | |
a3da1d95 | 573 | |
a8f46ddc TJ |
574 | int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
575 | { | |
a3da1d95 GE |
576 | int ret; |
577 | int offset = 0; | |
545820ce | 578 | int total_written = 0; |
c3d95b87 | 579 | |
a3da1d95 | 580 | while (offset < size) { |
948f9ada | 581 | int write_size = ftdi->writebuffer_chunksize; |
a3da1d95 GE |
582 | |
583 | if (offset+write_size > size) | |
584 | write_size = size-offset; | |
585 | ||
98452d97 | 586 | ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout); |
c3d95b87 TJ |
587 | if (ret < 0) |
588 | ftdi_error_return(ret, "usb bulk write failed"); | |
a3da1d95 | 589 | |
c3d95b87 | 590 | total_written += ret; |
a3da1d95 GE |
591 | offset += write_size; |
592 | } | |
593 | ||
545820ce | 594 | return total_written; |
a3da1d95 GE |
595 | } |
596 | ||
597 | ||
a8f46ddc TJ |
598 | int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
599 | { | |
948f9ada TJ |
600 | ftdi->writebuffer_chunksize = chunksize; |
601 | return 0; | |
602 | } | |
603 | ||
604 | ||
a8f46ddc TJ |
605 | int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
606 | { | |
948f9ada TJ |
607 | *chunksize = ftdi->writebuffer_chunksize; |
608 | return 0; | |
609 | } | |
cbabb7d3 | 610 | |
948f9ada | 611 | |
a8f46ddc TJ |
612 | int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
613 | { | |
1c733d33 | 614 | int offset = 0, ret = 1, i, num_of_chunks, chunk_remains; |
d9f0cce7 | 615 | |
948f9ada TJ |
616 | // everything we want is still in the readbuffer? |
617 | if (size <= ftdi->readbuffer_remaining) { | |
d9f0cce7 TJ |
618 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size); |
619 | ||
620 | // Fix offsets | |
621 | ftdi->readbuffer_remaining -= size; | |
622 | ftdi->readbuffer_offset += size; | |
623 | ||
545820ce | 624 | /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */ |
d9f0cce7 TJ |
625 | |
626 | return size; | |
979a145c | 627 | } |
948f9ada TJ |
628 | // something still in the readbuffer, but not enough to satisfy 'size'? |
629 | if (ftdi->readbuffer_remaining != 0) { | |
d9f0cce7 | 630 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining); |
979a145c | 631 | |
d9f0cce7 TJ |
632 | // Fix offset |
633 | offset += ftdi->readbuffer_remaining; | |
948f9ada | 634 | } |
948f9ada | 635 | // do the actual USB read |
cbabb7d3 | 636 | while (offset < size && ret > 0) { |
d9f0cce7 TJ |
637 | ftdi->readbuffer_remaining = 0; |
638 | ftdi->readbuffer_offset = 0; | |
98452d97 TJ |
639 | /* returns how much received */ |
640 | ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout); | |
c3d95b87 TJ |
641 | if (ret < 0) |
642 | ftdi_error_return(ret, "usb bulk read failed"); | |
98452d97 | 643 | |
d9f0cce7 TJ |
644 | if (ret > 2) { |
645 | // skip FTDI status bytes. | |
646 | // Maybe stored in the future to enable modem use | |
1c733d33 TJ |
647 | num_of_chunks = ret / 64; |
648 | chunk_remains = ret % 64; | |
649 | //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset); | |
650 | ||
d9f0cce7 TJ |
651 | ftdi->readbuffer_offset += 2; |
652 | ret -= 2; | |
1c733d33 | 653 | |
fde0a89e | 654 | if (ret > 62) { |
1c733d33 TJ |
655 | for (i = 1; i < num_of_chunks; i++) |
656 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i, | |
657 | ftdi->readbuffer+ftdi->readbuffer_offset+64*i, | |
658 | 62); | |
659 | if (chunk_remains > 2) { | |
660 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i, | |
661 | ftdi->readbuffer+ftdi->readbuffer_offset+64*i, | |
662 | chunk_remains-2); | |
663 | ret -= 2*num_of_chunks; | |
664 | } else | |
665 | ret -= 2*(num_of_chunks-1)+chunk_remains; | |
666 | } | |
d9f0cce7 TJ |
667 | } else if (ret <= 2) { |
668 | // no more data to read? | |
669 | return offset; | |
670 | } | |
d9f0cce7 TJ |
671 | if (ret > 0) { |
672 | // data still fits in buf? | |
673 | if (offset+ret <= size) { | |
674 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret); | |
545820ce | 675 | //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]); |
d9f0cce7 TJ |
676 | offset += ret; |
677 | ||
53ad271d | 678 | /* Did we read exactly the right amount of bytes? */ |
d9f0cce7 | 679 | if (offset == size) |
c4446c36 TJ |
680 | //printf("read_data exact rem %d offset %d\n", |
681 | //ftdi->readbuffer_remaining, offset); | |
d9f0cce7 TJ |
682 | return offset; |
683 | } else { | |
684 | // only copy part of the data or size <= readbuffer_chunksize | |
685 | int part_size = size-offset; | |
686 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size); | |
98452d97 | 687 | |
d9f0cce7 TJ |
688 | ftdi->readbuffer_offset += part_size; |
689 | ftdi->readbuffer_remaining = ret-part_size; | |
690 | offset += part_size; | |
691 | ||
53ad271d TJ |
692 | /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n", |
693 | part_size, size, offset, ret, ftdi->readbuffer_remaining); */ | |
d9f0cce7 TJ |
694 | |
695 | return offset; | |
696 | } | |
697 | } | |
cbabb7d3 | 698 | } |
948f9ada | 699 | // never reached |
29c4af7f | 700 | return -127; |
a3da1d95 GE |
701 | } |
702 | ||
703 | ||
a8f46ddc TJ |
704 | int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
705 | { | |
29c4af7f TJ |
706 | unsigned char *new_buf; |
707 | ||
948f9ada TJ |
708 | // Invalidate all remaining data |
709 | ftdi->readbuffer_offset = 0; | |
710 | ftdi->readbuffer_remaining = 0; | |
711 | ||
c3d95b87 TJ |
712 | if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) |
713 | ftdi_error_return(-1, "out of memory for readbuffer"); | |
d9f0cce7 | 714 | |
948f9ada TJ |
715 | ftdi->readbuffer = new_buf; |
716 | ftdi->readbuffer_chunksize = chunksize; | |
717 | ||
718 | return 0; | |
719 | } | |
720 | ||
721 | ||
a8f46ddc TJ |
722 | int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
723 | { | |
948f9ada TJ |
724 | *chunksize = ftdi->readbuffer_chunksize; |
725 | return 0; | |
726 | } | |
727 | ||
728 | ||
729 | ||
a8f46ddc TJ |
730 | int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) |
731 | { | |
a3da1d95 GE |
732 | unsigned short usb_val; |
733 | ||
d9f0cce7 | 734 | usb_val = bitmask; // low byte: bitmask |
3119537f TJ |
735 | /* FT2232C: Set bitbang_mode to 2 to enable SPI */ |
736 | usb_val |= (ftdi->bitbang_mode << 8); | |
737 | ||
c3d95b87 TJ |
738 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
739 | ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?"); | |
740 | ||
a3da1d95 GE |
741 | ftdi->bitbang_enabled = 1; |
742 | return 0; | |
743 | } | |
744 | ||
745 | ||
a8f46ddc TJ |
746 | int ftdi_disable_bitbang(struct ftdi_context *ftdi) |
747 | { | |
c3d95b87 TJ |
748 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
749 | ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?"); | |
a3da1d95 GE |
750 | |
751 | ftdi->bitbang_enabled = 0; | |
752 | return 0; | |
753 | } | |
754 | ||
755 | ||
c4446c36 TJ |
756 | int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode) |
757 | { | |
758 | unsigned short usb_val; | |
759 | ||
760 | usb_val = bitmask; // low byte: bitmask | |
761 | usb_val |= (mode << 8); | |
762 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) | |
763 | ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?"); | |
764 | ||
765 | ftdi->bitbang_mode = mode; | |
766 | ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0; | |
767 | return 0; | |
768 | } | |
769 | ||
a8f46ddc TJ |
770 | int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) |
771 | { | |
85f3c596 | 772 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1) |
c3d95b87 | 773 | ftdi_error_return(-1, "read pins failed"); |
a3da1d95 | 774 | |
a3da1d95 GE |
775 | return 0; |
776 | } | |
777 | ||
778 | ||
a8f46ddc TJ |
779 | int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) |
780 | { | |
a3da1d95 GE |
781 | unsigned short usb_val; |
782 | ||
c3d95b87 TJ |
783 | if (latency < 1) |
784 | ftdi_error_return(-1, "latency out of range. Only valid for 1-255"); | |
a3da1d95 | 785 | |
d79d2e68 | 786 | usb_val = latency; |
c3d95b87 TJ |
787 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) |
788 | ftdi_error_return(-2, "unable to set latency timer"); | |
789 | ||
a3da1d95 GE |
790 | return 0; |
791 | } | |
792 | ||
793 | ||
a8f46ddc TJ |
794 | int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) |
795 | { | |
a3da1d95 | 796 | unsigned short usb_val; |
c3d95b87 TJ |
797 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) |
798 | ftdi_error_return(-1, "reading latency timer failed"); | |
a3da1d95 GE |
799 | |
800 | *latency = (unsigned char)usb_val; | |
801 | return 0; | |
802 | } | |
803 | ||
804 | ||
a8f46ddc TJ |
805 | void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) |
806 | { | |
f396dbad TJ |
807 | eeprom->vendor_id = 0x0403; |
808 | eeprom->product_id = 0x6001; | |
d9f0cce7 | 809 | |
b8aa7b35 TJ |
810 | eeprom->self_powered = 1; |
811 | eeprom->remote_wakeup = 1; | |
812 | eeprom->BM_type_chip = 1; | |
d9f0cce7 | 813 | |
b8aa7b35 TJ |
814 | eeprom->in_is_isochronous = 0; |
815 | eeprom->out_is_isochronous = 0; | |
816 | eeprom->suspend_pull_downs = 0; | |
d9f0cce7 | 817 | |
b8aa7b35 TJ |
818 | eeprom->use_serial = 0; |
819 | eeprom->change_usb_version = 0; | |
f396dbad | 820 | eeprom->usb_version = 0x0200; |
b8aa7b35 | 821 | eeprom->max_power = 0; |
d9f0cce7 | 822 | |
b8aa7b35 TJ |
823 | eeprom->manufacturer = NULL; |
824 | eeprom->product = NULL; | |
825 | eeprom->serial = NULL; | |
826 | } | |
827 | ||
828 | ||
829 | /* | |
4837f98a TJ |
830 | ftdi_eeprom_build |
831 | ||
832 | Build binary output from ftdi_eeprom structure. | |
833 | Output is suitable for ftdi_write_eeprom. | |
834 | ||
835 | Return codes: | |
8ed61121 | 836 | positive value: used eeprom size |
b8aa7b35 TJ |
837 | -1: eeprom size (128 bytes) exceeded by custom strings |
838 | */ | |
a8f46ddc TJ |
839 | int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) |
840 | { | |
b8aa7b35 TJ |
841 | unsigned char i, j; |
842 | unsigned short checksum, value; | |
843 | unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0; | |
844 | int size_check; | |
845 | ||
846 | if (eeprom->manufacturer != NULL) | |
d9f0cce7 | 847 | manufacturer_size = strlen(eeprom->manufacturer); |
b8aa7b35 | 848 | if (eeprom->product != NULL) |
d9f0cce7 | 849 | product_size = strlen(eeprom->product); |
b8aa7b35 | 850 | if (eeprom->serial != NULL) |
d9f0cce7 | 851 | serial_size = strlen(eeprom->serial); |
b8aa7b35 | 852 | |
d9f0cce7 TJ |
853 | size_check = 128; // eeprom is 128 bytes |
854 | size_check -= 28; // 28 are always in use (fixed) | |
b8aa7b35 TJ |
855 | size_check -= manufacturer_size*2; |
856 | size_check -= product_size*2; | |
857 | size_check -= serial_size*2; | |
858 | ||
859 | // eeprom size exceeded? | |
860 | if (size_check < 0) | |
d9f0cce7 | 861 | return (-1); |
b8aa7b35 TJ |
862 | |
863 | // empty eeprom | |
864 | memset (output, 0, 128); | |
865 | ||
866 | // Addr 00: Stay 00 00 | |
867 | // Addr 02: Vendor ID | |
868 | output[0x02] = eeprom->vendor_id; | |
869 | output[0x03] = eeprom->vendor_id >> 8; | |
870 | ||
871 | // Addr 04: Product ID | |
872 | output[0x04] = eeprom->product_id; | |
873 | output[0x05] = eeprom->product_id >> 8; | |
874 | ||
875 | // Addr 06: Device release number (0400h for BM features) | |
876 | output[0x06] = 0x00; | |
d9f0cce7 | 877 | |
b8aa7b35 | 878 | if (eeprom->BM_type_chip == 1) |
d9f0cce7 | 879 | output[0x07] = 0x04; |
b8aa7b35 | 880 | else |
d9f0cce7 | 881 | output[0x07] = 0x02; |
b8aa7b35 TJ |
882 | |
883 | // Addr 08: Config descriptor | |
884 | // Bit 1: remote wakeup if 1 | |
885 | // Bit 0: self powered if 1 | |
886 | // | |
887 | j = 0; | |
888 | if (eeprom->self_powered == 1) | |
d9f0cce7 | 889 | j = j | 1; |
b8aa7b35 | 890 | if (eeprom->remote_wakeup == 1) |
d9f0cce7 | 891 | j = j | 2; |
b8aa7b35 TJ |
892 | output[0x08] = j; |
893 | ||
894 | // Addr 09: Max power consumption: max power = value * 2 mA | |
d9f0cce7 TJ |
895 | output[0x09] = eeprom->max_power; |
896 | ; | |
897 | ||
b8aa7b35 TJ |
898 | // Addr 0A: Chip configuration |
899 | // Bit 7: 0 - reserved | |
900 | // Bit 6: 0 - reserved | |
901 | // Bit 5: 0 - reserved | |
902 | // Bit 4: 1 - Change USB version | |
903 | // Bit 3: 1 - Use the serial number string | |
904 | // Bit 2: 1 - Enable suspend pull downs for lower power | |
905 | // Bit 1: 1 - Out EndPoint is Isochronous | |
906 | // Bit 0: 1 - In EndPoint is Isochronous | |
907 | // | |
908 | j = 0; | |
909 | if (eeprom->in_is_isochronous == 1) | |
d9f0cce7 | 910 | j = j | 1; |
b8aa7b35 | 911 | if (eeprom->out_is_isochronous == 1) |
d9f0cce7 | 912 | j = j | 2; |
b8aa7b35 | 913 | if (eeprom->suspend_pull_downs == 1) |
d9f0cce7 | 914 | j = j | 4; |
b8aa7b35 | 915 | if (eeprom->use_serial == 1) |
d9f0cce7 | 916 | j = j | 8; |
b8aa7b35 | 917 | if (eeprom->change_usb_version == 1) |
d9f0cce7 | 918 | j = j | 16; |
b8aa7b35 | 919 | output[0x0A] = j; |
d9f0cce7 | 920 | |
b8aa7b35 TJ |
921 | // Addr 0B: reserved |
922 | output[0x0B] = 0x00; | |
d9f0cce7 | 923 | |
b8aa7b35 TJ |
924 | // Addr 0C: USB version low byte when 0x0A bit 4 is set |
925 | // Addr 0D: USB version high byte when 0x0A bit 4 is set | |
926 | if (eeprom->change_usb_version == 1) { | |
927 | output[0x0C] = eeprom->usb_version; | |
d9f0cce7 | 928 | output[0x0D] = eeprom->usb_version >> 8; |
b8aa7b35 TJ |
929 | } |
930 | ||
931 | ||
932 | // Addr 0E: Offset of the manufacturer string + 0x80 | |
933 | output[0x0E] = 0x14 + 0x80; | |
934 | ||
935 | // Addr 0F: Length of manufacturer string | |
936 | output[0x0F] = manufacturer_size*2 + 2; | |
937 | ||
938 | // Addr 10: Offset of the product string + 0x80, calculated later | |
939 | // Addr 11: Length of product string | |
940 | output[0x11] = product_size*2 + 2; | |
941 | ||
942 | // Addr 12: Offset of the serial string + 0x80, calculated later | |
943 | // Addr 13: Length of serial string | |
944 | output[0x13] = serial_size*2 + 2; | |
945 | ||
946 | // Dynamic content | |
a862ddcf | 947 | output[0x14] = manufacturer_size*2 + 2; |
d9f0cce7 TJ |
948 | output[0x15] = 0x03; // type: string |
949 | ||
b8aa7b35 | 950 | i = 0x16, j = 0; |
d9f0cce7 | 951 | |
b8aa7b35 TJ |
952 | // Output manufacturer |
953 | for (j = 0; j < manufacturer_size; j++) { | |
d9f0cce7 TJ |
954 | output[i] = eeprom->manufacturer[j], i++; |
955 | output[i] = 0x00, i++; | |
b8aa7b35 TJ |
956 | } |
957 | ||
958 | // Output product name | |
d9f0cce7 | 959 | output[0x10] = i + 0x80; // calculate offset |
b8aa7b35 TJ |
960 | output[i] = product_size*2 + 2, i++; |
961 | output[i] = 0x03, i++; | |
962 | for (j = 0; j < product_size; j++) { | |
d9f0cce7 TJ |
963 | output[i] = eeprom->product[j], i++; |
964 | output[i] = 0x00, i++; | |
b8aa7b35 | 965 | } |
d9f0cce7 | 966 | |
b8aa7b35 | 967 | // Output serial |
d9f0cce7 | 968 | output[0x12] = i + 0x80; // calculate offset |
b8aa7b35 TJ |
969 | output[i] = serial_size*2 + 2, i++; |
970 | output[i] = 0x03, i++; | |
971 | for (j = 0; j < serial_size; j++) { | |
d9f0cce7 TJ |
972 | output[i] = eeprom->serial[j], i++; |
973 | output[i] = 0x00, i++; | |
b8aa7b35 TJ |
974 | } |
975 | ||
976 | // calculate checksum | |
977 | checksum = 0xAAAA; | |
d9f0cce7 | 978 | |
b8aa7b35 | 979 | for (i = 0; i < 63; i++) { |
d9f0cce7 TJ |
980 | value = output[i*2]; |
981 | value += output[(i*2)+1] << 8; | |
b8aa7b35 | 982 | |
d9f0cce7 TJ |
983 | checksum = value^checksum; |
984 | checksum = (checksum << 1) | (checksum >> 15); | |
b8aa7b35 TJ |
985 | } |
986 | ||
987 | output[0x7E] = checksum; | |
d9f0cce7 | 988 | output[0x7F] = checksum >> 8; |
b8aa7b35 | 989 | |
8ed61121 | 990 | return size_check; |
b8aa7b35 TJ |
991 | } |
992 | ||
993 | ||
a8f46ddc TJ |
994 | int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) |
995 | { | |
a3da1d95 GE |
996 | int i; |
997 | ||
998 | for (i = 0; i < 64; i++) { | |
c3d95b87 TJ |
999 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) |
1000 | ftdi_error_return(-1, "reading eeprom failed"); | |
a3da1d95 GE |
1001 | } |
1002 | ||
1003 | return 0; | |
1004 | } | |
1005 | ||
1006 | ||
a8f46ddc TJ |
1007 | int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) |
1008 | { | |
a3da1d95 GE |
1009 | unsigned short usb_val; |
1010 | int i; | |
1011 | ||
1012 | for (i = 0; i < 64; i++) { | |
d9f0cce7 TJ |
1013 | usb_val = eeprom[i*2]; |
1014 | usb_val += eeprom[(i*2)+1] << 8; | |
c3d95b87 TJ |
1015 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) |
1016 | ftdi_error_return(-1, "unable to write eeprom"); | |
a3da1d95 GE |
1017 | } |
1018 | ||
1019 | return 0; | |
1020 | } | |
1021 | ||
1022 | ||
a8f46ddc TJ |
1023 | int ftdi_erase_eeprom(struct ftdi_context *ftdi) |
1024 | { | |
c3d95b87 TJ |
1025 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) |
1026 | ftdi_error_return(-1, "unable to erase eeprom"); | |
a3da1d95 GE |
1027 | |
1028 | return 0; | |
1029 | } | |
c3d95b87 TJ |
1030 | |
1031 | ||
1032 | char *ftdi_get_error_string (struct ftdi_context *ftdi) | |
1033 | { | |
1034 | return ftdi->error_str; | |
1035 | } | |
a01d31e2 TJ |
1036 | |
1037 | ||
1038 | int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl) | |
1039 | { | |
1040 | if (usb_control_msg(ftdi->usb_dev, SIO_SET_FLOW_CTRL_REQUEST_TYPE, | |
d2f10023 TJ |
1041 | SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->interface), |
1042 | NULL, 0, ftdi->usb_write_timeout) != 0) | |
1043 | ftdi_error_return(-1, "set flow control failed"); | |
a01d31e2 TJ |
1044 | |
1045 | return 0; | |
1046 | } | |
1047 | ||
1048 | int ftdi_setdtr(struct ftdi_context *ftdi, int state) | |
1049 | { | |
1050 | unsigned short usb_val; | |
1051 | ||
d2f10023 | 1052 | if (state) |
a01d31e2 TJ |
1053 | usb_val = SIO_SET_DTR_HIGH; |
1054 | else | |
1055 | usb_val = SIO_SET_DTR_LOW; | |
1056 | ||
1057 | if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE, | |
d2f10023 TJ |
1058 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface, |
1059 | NULL, 0, ftdi->usb_write_timeout) != 0) | |
1060 | ftdi_error_return(-1, "set dtr failed"); | |
a01d31e2 TJ |
1061 | |
1062 | return 0; | |
1063 | } | |
1064 | ||
1065 | int ftdi_setrts(struct ftdi_context *ftdi, int state) | |
1066 | { | |
1067 | unsigned short usb_val; | |
1068 | ||
d2f10023 | 1069 | if (state) |
a01d31e2 TJ |
1070 | usb_val = SIO_SET_RTS_HIGH; |
1071 | else | |
1072 | usb_val = SIO_SET_RTS_LOW; | |
1073 | ||
d2f10023 TJ |
1074 | if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE, |
1075 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface, | |
1076 | NULL, 0, ftdi->usb_write_timeout) != 0) | |
1077 | ftdi_error_return(-1, "set of rts failed"); | |
a01d31e2 TJ |
1078 | |
1079 | return 0; | |
1080 | } |