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> |
792418f1 | 19 | #include <sys/utsname.h> |
0e302db6 | 20 | |
98452d97 | 21 | #include "ftdi.h" |
a3da1d95 | 22 | |
948f9ada TJ |
23 | /* ftdi_init return codes: |
24 | 0: all fine | |
6d9aa99f | 25 | -1: couldn't allocate read buffer |
948f9ada | 26 | */ |
a8f46ddc TJ |
27 | int ftdi_init(struct ftdi_context *ftdi) |
28 | { | |
98452d97 | 29 | ftdi->usb_dev = NULL; |
545820ce TJ |
30 | ftdi->usb_read_timeout = 5000; |
31 | ftdi->usb_write_timeout = 5000; | |
a3da1d95 | 32 | |
53ad271d | 33 | ftdi->type = TYPE_BM; /* chip type */ |
a3da1d95 GE |
34 | ftdi->baudrate = -1; |
35 | ftdi->bitbang_enabled = 0; | |
36 | ||
948f9ada TJ |
37 | ftdi->readbuffer = NULL; |
38 | ftdi->readbuffer_offset = 0; | |
39 | ftdi->readbuffer_remaining = 0; | |
40 | ftdi->writebuffer_chunksize = 4096; | |
41 | ||
545820ce TJ |
42 | ftdi->interface = 0; |
43 | ftdi->index = 0; | |
44 | ftdi->in_ep = 0x02; | |
45 | ftdi->out_ep = 0x81; | |
3119537f | 46 | ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */ |
53ad271d | 47 | |
a3da1d95 GE |
48 | ftdi->error_str = NULL; |
49 | ||
1c733d33 TJ |
50 | /* All fine. Now allocate the readbuffer */ |
51 | return ftdi_read_data_set_chunksize(ftdi, 4096); | |
948f9ada TJ |
52 | } |
53 | ||
54 | ||
a8f46ddc TJ |
55 | void ftdi_deinit(struct ftdi_context *ftdi) |
56 | { | |
948f9ada | 57 | if (ftdi->readbuffer != NULL) { |
d9f0cce7 TJ |
58 | free(ftdi->readbuffer); |
59 | ftdi->readbuffer = NULL; | |
948f9ada | 60 | } |
a3da1d95 GE |
61 | } |
62 | ||
98452d97 | 63 | |
a8f46ddc TJ |
64 | void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) |
65 | { | |
98452d97 TJ |
66 | ftdi->usb_dev = usb; |
67 | } | |
68 | ||
69 | ||
a3da1d95 GE |
70 | /* ftdi_usb_open return codes: |
71 | 0: all fine | |
98452d97 TJ |
72 | -1: usb_find_busses() failed |
73 | -2: usb_find_devices() failed | |
74 | -3: usb device not found | |
75 | -4: unable to open device | |
76 | -5: unable to claim device | |
77 | -6: reset failed | |
78 | -7: set baudrate failed | |
a8f46ddc TJ |
79 | -8: get product description failed |
80 | -9: get serial number failed | |
81 | -10: unable to close device | |
a3da1d95 | 82 | */ |
a8f46ddc TJ |
83 | int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) |
84 | { | |
04e1ea0a | 85 | return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL); |
a8f46ddc TJ |
86 | } |
87 | ||
04e1ea0a | 88 | int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, |
a8f46ddc TJ |
89 | const char* description, const char* serial) |
90 | { | |
98452d97 TJ |
91 | struct usb_bus *bus; |
92 | struct usb_device *dev; | |
93 | ||
94 | usb_init(); | |
95 | ||
96 | if (usb_find_busses() < 0) { | |
97 | ftdi->error_str = "usb_find_busses() failed"; | |
98 | return -1; | |
99 | } | |
100 | ||
101 | if (usb_find_devices() < 0) { | |
102 | ftdi->error_str = "usb_find_devices() failed"; | |
a3da1d95 GE |
103 | return -2; |
104 | } | |
105 | ||
98452d97 TJ |
106 | for (bus = usb_busses; bus; bus = bus->next) { |
107 | for (dev = bus->devices; dev; dev = dev->next) { | |
a8f46ddc TJ |
108 | if (dev->descriptor.idVendor == vendor |
109 | && dev->descriptor.idProduct == product) { | |
110 | if (!(ftdi->usb_dev = usb_open(dev))) { | |
111 | ftdi->error_str = "usb_open() failed"; | |
112 | return -4; | |
113 | } | |
114 | ||
115 | char string[256]; | |
116 | if (description != NULL) { | |
117 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) { | |
118 | ftdi->error_str = "unable to fetch product description\n"; | |
119 | if (usb_close (ftdi->usb_dev) != 0) | |
120 | return -10; | |
121 | return -8; | |
98452d97 | 122 | } |
a8f46ddc TJ |
123 | if (strncmp(string, description, sizeof(string)) != 0) { |
124 | ftdi->error_str = "product description not matching\n"; | |
125 | if (usb_close (ftdi->usb_dev) != 0) | |
126 | return -10; | |
127 | continue; | |
128 | } | |
129 | } | |
130 | if (serial != NULL) { | |
131 | if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) { | |
132 | ftdi->error_str = "unable to fetch serial number\n"; | |
133 | if (usb_close (ftdi->usb_dev) != 0) | |
134 | return -10; | |
135 | return -9; | |
136 | } | |
137 | if (strncmp(string, serial, sizeof(string)) != 0) { | |
138 | ftdi->error_str = "serial number not matching\n"; | |
139 | if (usb_close (ftdi->usb_dev) != 0) | |
140 | return -10; | |
141 | continue; | |
142 | } | |
143 | } | |
98452d97 | 144 | |
a8f46ddc TJ |
145 | if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) { |
146 | ftdi->error_str = "unable to claim usb device. Make sure ftdi_sio is unloaded!"; | |
147 | if (usb_close (ftdi->usb_dev) != 0) | |
148 | return -10; | |
149 | return -5; | |
150 | } | |
98452d97 | 151 | |
a8f46ddc TJ |
152 | if (ftdi_usb_reset (ftdi) != 0) { |
153 | if (usb_close (ftdi->usb_dev) != 0) | |
154 | return -10; | |
155 | return -6; | |
156 | } | |
157 | ||
158 | if (ftdi_set_baudrate (ftdi, 9600) != 0) { | |
159 | if (usb_close (ftdi->usb_dev) != 0) | |
160 | return -10; | |
161 | return -7; | |
98452d97 | 162 | } |
a8f46ddc TJ |
163 | |
164 | // Try to guess chip type | |
165 | // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0 | |
166 | if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200 | |
167 | && dev->descriptor.iSerialNumber == 0)) | |
168 | ftdi->type = TYPE_BM; | |
169 | else if (dev->descriptor.bcdDevice == 0x200) | |
170 | ftdi->type = TYPE_AM; | |
171 | else if (dev->descriptor.bcdDevice == 0x500) | |
172 | ftdi->type = TYPE_2232C; | |
173 | ||
174 | return 0; | |
98452d97 TJ |
175 | } |
176 | } | |
98452d97 | 177 | } |
a3da1d95 | 178 | |
98452d97 TJ |
179 | // device not found |
180 | return -3; | |
a3da1d95 GE |
181 | } |
182 | ||
183 | ||
a8f46ddc TJ |
184 | int ftdi_usb_reset(struct ftdi_context *ftdi) |
185 | { | |
792418f1 TJ |
186 | #if defined(__linux__) |
187 | struct utsname kernelver; | |
188 | int k_major, k_minor, k_myver; | |
189 | #endif | |
190 | ||
98452d97 | 191 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a3da1d95 GE |
192 | ftdi->error_str = "FTDI reset failed"; |
193 | return -1; | |
194 | } | |
792418f1 TJ |
195 | |
196 | #if defined(__linux__) | |
197 | /* Kernel 2.6 (maybe higher versions, too) need an additional usb_reset */ | |
198 | if (uname(&kernelver) == 0 && sscanf(kernelver.release, "%d.%d", &k_major, &k_minor) == 2) { | |
199 | k_myver = k_major*10 + k_minor; | |
200 | ||
201 | if (k_myver >= 26 && usb_reset(ftdi->usb_dev) != 0) { | |
202 | ftdi->error_str = "USB reset failed"; | |
203 | return -2; | |
204 | } | |
5cd68e23 | 205 | } |
792418f1 TJ |
206 | #endif |
207 | ||
545820ce | 208 | // Invalidate data in the readbuffer |
bfcee05b TJ |
209 | ftdi->readbuffer_offset = 0; |
210 | ftdi->readbuffer_remaining = 0; | |
211 | ||
a3da1d95 GE |
212 | return 0; |
213 | } | |
214 | ||
a8f46ddc TJ |
215 | int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) |
216 | { | |
98452d97 | 217 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a60be878 TJ |
218 | ftdi->error_str = "FTDI purge of RX buffer failed"; |
219 | return -1; | |
220 | } | |
545820ce | 221 | // Invalidate data in the readbuffer |
bfcee05b TJ |
222 | ftdi->readbuffer_offset = 0; |
223 | ftdi->readbuffer_remaining = 0; | |
a60be878 | 224 | |
98452d97 | 225 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a60be878 TJ |
226 | ftdi->error_str = "FTDI purge of TX buffer failed"; |
227 | return -1; | |
228 | } | |
229 | ||
545820ce | 230 | |
a60be878 TJ |
231 | return 0; |
232 | } | |
a3da1d95 GE |
233 | |
234 | /* ftdi_usb_close return codes | |
235 | 0: all fine | |
98452d97 TJ |
236 | -1: usb_release failed |
237 | -2: usb_close failed | |
a3da1d95 | 238 | */ |
a8f46ddc TJ |
239 | int ftdi_usb_close(struct ftdi_context *ftdi) |
240 | { | |
a3da1d95 GE |
241 | int rtn = 0; |
242 | ||
98452d97 | 243 | if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0) |
a3da1d95 | 244 | rtn = -1; |
98452d97 TJ |
245 | |
246 | if (usb_close (ftdi->usb_dev) != 0) | |
a3da1d95 | 247 | rtn = -2; |
98452d97 | 248 | |
a3da1d95 GE |
249 | return rtn; |
250 | } | |
251 | ||
252 | ||
253 | /* | |
53ad271d TJ |
254 | ftdi_convert_baudrate returns nearest supported baud rate to that requested. |
255 | Function is only used internally | |
256 | */ | |
0126d22e | 257 | static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, |
a8f46ddc TJ |
258 | unsigned short *value, unsigned short *index) |
259 | { | |
53ad271d TJ |
260 | static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1}; |
261 | static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3}; | |
262 | static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7}; | |
263 | int divisor, best_divisor, best_baud, best_baud_diff; | |
264 | unsigned long encoded_divisor; | |
265 | int i; | |
266 | ||
267 | if (baudrate <= 0) { | |
268 | // Return error | |
269 | return -1; | |
270 | } | |
271 | ||
272 | divisor = 24000000 / baudrate; | |
273 | ||
0126d22e | 274 | if (ftdi->type == TYPE_AM) { |
53ad271d TJ |
275 | // Round down to supported fraction (AM only) |
276 | divisor -= am_adjust_dn[divisor & 7]; | |
277 | } | |
278 | ||
279 | // Try this divisor and the one above it (because division rounds down) | |
280 | best_divisor = 0; | |
281 | best_baud = 0; | |
282 | best_baud_diff = 0; | |
283 | for (i = 0; i < 2; i++) { | |
284 | int try_divisor = divisor + i; | |
285 | int baud_estimate; | |
286 | int baud_diff; | |
287 | ||
288 | // Round up to supported divisor value | |
289 | if (try_divisor < 8) { | |
290 | // Round up to minimum supported divisor | |
291 | try_divisor = 8; | |
0126d22e | 292 | } else if (ftdi->type != TYPE_AM && try_divisor < 12) { |
53ad271d TJ |
293 | // BM doesn't support divisors 9 through 11 inclusive |
294 | try_divisor = 12; | |
295 | } else if (divisor < 16) { | |
296 | // AM doesn't support divisors 9 through 15 inclusive | |
297 | try_divisor = 16; | |
298 | } else { | |
0126d22e | 299 | if (ftdi->type == TYPE_AM) { |
53ad271d TJ |
300 | // Round up to supported fraction (AM only) |
301 | try_divisor += am_adjust_up[try_divisor & 7]; | |
302 | if (try_divisor > 0x1FFF8) { | |
303 | // Round down to maximum supported divisor value (for AM) | |
304 | try_divisor = 0x1FFF8; | |
305 | } | |
306 | } else { | |
307 | if (try_divisor > 0x1FFFF) { | |
308 | // Round down to maximum supported divisor value (for BM) | |
309 | try_divisor = 0x1FFFF; | |
310 | } | |
311 | } | |
312 | } | |
313 | // Get estimated baud rate (to nearest integer) | |
314 | baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor; | |
315 | // Get absolute difference from requested baud rate | |
316 | if (baud_estimate < baudrate) { | |
317 | baud_diff = baudrate - baud_estimate; | |
318 | } else { | |
319 | baud_diff = baud_estimate - baudrate; | |
320 | } | |
321 | if (i == 0 || baud_diff < best_baud_diff) { | |
322 | // Closest to requested baud rate so far | |
323 | best_divisor = try_divisor; | |
324 | best_baud = baud_estimate; | |
325 | best_baud_diff = baud_diff; | |
326 | if (baud_diff == 0) { | |
327 | // Spot on! No point trying | |
328 | break; | |
329 | } | |
330 | } | |
331 | } | |
332 | // Encode the best divisor value | |
333 | encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14); | |
334 | // Deal with special cases for encoded value | |
335 | if (encoded_divisor == 1) { | |
336 | encoded_divisor = 0; // 3000000 baud | |
337 | } else if (encoded_divisor == 0x4001) { | |
338 | encoded_divisor = 1; // 2000000 baud (BM only) | |
339 | } | |
340 | // Split into "value" and "index" values | |
341 | *value = (unsigned short)(encoded_divisor & 0xFFFF); | |
de22df10 | 342 | if(ftdi->type == TYPE_2232C) { |
0126d22e TJ |
343 | *index = (unsigned short)(encoded_divisor >> 8); |
344 | *index &= 0xFF00; | |
345 | *index |= ftdi->interface; | |
346 | } | |
347 | else | |
348 | *index = (unsigned short)(encoded_divisor >> 16); | |
349 | ||
53ad271d TJ |
350 | // Return the nearest baud rate |
351 | return best_baud; | |
352 | } | |
353 | ||
354 | /* | |
a3da1d95 GE |
355 | ftdi_set_baudrate return codes: |
356 | 0: all fine | |
357 | -1: invalid baudrate | |
358 | -2: setting baudrate failed | |
359 | */ | |
a8f46ddc TJ |
360 | int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) |
361 | { | |
53ad271d TJ |
362 | unsigned short value, index; |
363 | int actual_baudrate; | |
a3da1d95 GE |
364 | |
365 | if (ftdi->bitbang_enabled) { | |
366 | baudrate = baudrate*4; | |
367 | } | |
368 | ||
25707904 | 369 | actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index); |
53ad271d TJ |
370 | if (actual_baudrate <= 0) { |
371 | ftdi->error_str = "Silly baudrate <= 0."; | |
a3da1d95 GE |
372 | return -1; |
373 | } | |
374 | ||
53ad271d TJ |
375 | // Check within tolerance (about 5%) |
376 | if ((actual_baudrate * 2 < baudrate /* Catch overflows */ ) | |
377 | || ((actual_baudrate < baudrate) | |
378 | ? (actual_baudrate * 21 < baudrate * 20) | |
379 | : (baudrate * 21 < actual_baudrate * 20))) { | |
380 | ftdi->error_str = "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4"; | |
381 | return -1; | |
382 | } | |
545820ce | 383 | |
98452d97 | 384 | if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a3da1d95 GE |
385 | ftdi->error_str = "Setting new baudrate failed"; |
386 | return -2; | |
387 | } | |
388 | ||
389 | ftdi->baudrate = baudrate; | |
390 | return 0; | |
391 | } | |
392 | ||
393 | ||
a8f46ddc TJ |
394 | int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
395 | { | |
a3da1d95 GE |
396 | int ret; |
397 | int offset = 0; | |
545820ce | 398 | int total_written = 0; |
a3da1d95 | 399 | while (offset < size) { |
948f9ada | 400 | int write_size = ftdi->writebuffer_chunksize; |
a3da1d95 GE |
401 | |
402 | if (offset+write_size > size) | |
403 | write_size = size-offset; | |
404 | ||
98452d97 | 405 | ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout); |
29c4af7f TJ |
406 | if (ret < 0) { |
407 | if (ret == -1) | |
408 | ftdi->error_str = "bulk write failed"; | |
409 | else | |
410 | ftdi->error_str = "usb failed"; | |
411 | return ret; | |
d9f0cce7 | 412 | } |
545820ce | 413 | total_written += ret; |
a3da1d95 GE |
414 | |
415 | offset += write_size; | |
416 | } | |
417 | ||
545820ce | 418 | return total_written; |
a3da1d95 GE |
419 | } |
420 | ||
421 | ||
a8f46ddc TJ |
422 | int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
423 | { | |
948f9ada TJ |
424 | ftdi->writebuffer_chunksize = chunksize; |
425 | return 0; | |
426 | } | |
427 | ||
428 | ||
a8f46ddc TJ |
429 | int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
430 | { | |
948f9ada TJ |
431 | *chunksize = ftdi->writebuffer_chunksize; |
432 | return 0; | |
433 | } | |
cbabb7d3 | 434 | |
948f9ada | 435 | |
a8f46ddc TJ |
436 | int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
437 | { | |
1c733d33 | 438 | int offset = 0, ret = 1, i, num_of_chunks, chunk_remains; |
d9f0cce7 | 439 | |
948f9ada TJ |
440 | // everything we want is still in the readbuffer? |
441 | if (size <= ftdi->readbuffer_remaining) { | |
d9f0cce7 TJ |
442 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size); |
443 | ||
444 | // Fix offsets | |
445 | ftdi->readbuffer_remaining -= size; | |
446 | ftdi->readbuffer_offset += size; | |
447 | ||
545820ce | 448 | /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */ |
d9f0cce7 TJ |
449 | |
450 | return size; | |
979a145c | 451 | } |
948f9ada TJ |
452 | // something still in the readbuffer, but not enough to satisfy 'size'? |
453 | if (ftdi->readbuffer_remaining != 0) { | |
d9f0cce7 | 454 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining); |
979a145c | 455 | |
d9f0cce7 TJ |
456 | // Fix offset |
457 | offset += ftdi->readbuffer_remaining; | |
948f9ada | 458 | } |
948f9ada | 459 | // do the actual USB read |
cbabb7d3 | 460 | while (offset < size && ret > 0) { |
d9f0cce7 TJ |
461 | ftdi->readbuffer_remaining = 0; |
462 | ftdi->readbuffer_offset = 0; | |
98452d97 TJ |
463 | /* returns how much received */ |
464 | ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout); | |
465 | ||
29c4af7f TJ |
466 | if (ret < 0) { |
467 | if (ret == -1) | |
468 | ftdi->error_str = "bulk read failed"; | |
469 | else | |
470 | ftdi->error_str = "usb failed"; | |
471 | return ret; | |
0e302db6 | 472 | } |
98452d97 | 473 | |
d9f0cce7 TJ |
474 | if (ret > 2) { |
475 | // skip FTDI status bytes. | |
476 | // Maybe stored in the future to enable modem use | |
1c733d33 TJ |
477 | num_of_chunks = ret / 64; |
478 | chunk_remains = ret % 64; | |
479 | //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset); | |
480 | ||
d9f0cce7 TJ |
481 | ftdi->readbuffer_offset += 2; |
482 | ret -= 2; | |
1c733d33 TJ |
483 | |
484 | if (ret > 64) { | |
485 | for (i = 1; i < num_of_chunks; i++) | |
486 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i, | |
487 | ftdi->readbuffer+ftdi->readbuffer_offset+64*i, | |
488 | 62); | |
489 | if (chunk_remains > 2) { | |
490 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i, | |
491 | ftdi->readbuffer+ftdi->readbuffer_offset+64*i, | |
492 | chunk_remains-2); | |
493 | ret -= 2*num_of_chunks; | |
494 | } else | |
495 | ret -= 2*(num_of_chunks-1)+chunk_remains; | |
496 | } | |
d9f0cce7 TJ |
497 | } else if (ret <= 2) { |
498 | // no more data to read? | |
499 | return offset; | |
500 | } | |
d9f0cce7 TJ |
501 | if (ret > 0) { |
502 | // data still fits in buf? | |
503 | if (offset+ret <= size) { | |
504 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret); | |
545820ce | 505 | //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]); |
d9f0cce7 TJ |
506 | offset += ret; |
507 | ||
53ad271d | 508 | /* Did we read exactly the right amount of bytes? */ |
d9f0cce7 TJ |
509 | if (offset == size) |
510 | return offset; | |
511 | } else { | |
512 | // only copy part of the data or size <= readbuffer_chunksize | |
513 | int part_size = size-offset; | |
514 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size); | |
98452d97 | 515 | |
d9f0cce7 TJ |
516 | ftdi->readbuffer_offset += part_size; |
517 | ftdi->readbuffer_remaining = ret-part_size; | |
518 | offset += part_size; | |
519 | ||
53ad271d TJ |
520 | /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n", |
521 | part_size, size, offset, ret, ftdi->readbuffer_remaining); */ | |
d9f0cce7 TJ |
522 | |
523 | return offset; | |
524 | } | |
525 | } | |
cbabb7d3 | 526 | } |
948f9ada | 527 | // never reached |
29c4af7f | 528 | return -127; |
a3da1d95 GE |
529 | } |
530 | ||
531 | ||
a8f46ddc TJ |
532 | int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
533 | { | |
29c4af7f TJ |
534 | unsigned char *new_buf; |
535 | ||
948f9ada TJ |
536 | // Invalidate all remaining data |
537 | ftdi->readbuffer_offset = 0; | |
538 | ftdi->readbuffer_remaining = 0; | |
539 | ||
948f9ada | 540 | if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) { |
d9f0cce7 TJ |
541 | ftdi->error_str = "out of memory for readbuffer"; |
542 | return -1; | |
948f9ada | 543 | } |
d9f0cce7 | 544 | |
948f9ada TJ |
545 | ftdi->readbuffer = new_buf; |
546 | ftdi->readbuffer_chunksize = chunksize; | |
547 | ||
548 | return 0; | |
549 | } | |
550 | ||
551 | ||
a8f46ddc TJ |
552 | int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
553 | { | |
948f9ada TJ |
554 | *chunksize = ftdi->readbuffer_chunksize; |
555 | return 0; | |
556 | } | |
557 | ||
558 | ||
559 | ||
a8f46ddc TJ |
560 | int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) |
561 | { | |
a3da1d95 GE |
562 | unsigned short usb_val; |
563 | ||
d9f0cce7 | 564 | usb_val = bitmask; // low byte: bitmask |
3119537f TJ |
565 | /* FT2232C: Set bitbang_mode to 2 to enable SPI */ |
566 | usb_val |= (ftdi->bitbang_mode << 8); | |
567 | ||
98452d97 | 568 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a3da1d95 GE |
569 | ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?"; |
570 | return -1; | |
571 | } | |
a3da1d95 GE |
572 | ftdi->bitbang_enabled = 1; |
573 | return 0; | |
574 | } | |
575 | ||
576 | ||
a8f46ddc TJ |
577 | int ftdi_disable_bitbang(struct ftdi_context *ftdi) |
578 | { | |
98452d97 | 579 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a3da1d95 GE |
580 | ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?"; |
581 | return -1; | |
582 | } | |
583 | ||
584 | ftdi->bitbang_enabled = 0; | |
585 | return 0; | |
586 | } | |
587 | ||
588 | ||
a8f46ddc TJ |
589 | int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) |
590 | { | |
a3da1d95 | 591 | unsigned short usb_val; |
98452d97 | 592 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) { |
a3da1d95 GE |
593 | ftdi->error_str = "Read pins failed"; |
594 | return -1; | |
595 | } | |
596 | ||
597 | *pins = (unsigned char)usb_val; | |
598 | return 0; | |
599 | } | |
600 | ||
601 | ||
a8f46ddc TJ |
602 | int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) |
603 | { | |
a3da1d95 GE |
604 | unsigned short usb_val; |
605 | ||
606 | if (latency < 1) { | |
d9f0cce7 TJ |
607 | ftdi->error_str = "Latency out of range. Only valid for 1-255"; |
608 | return -1; | |
a3da1d95 GE |
609 | } |
610 | ||
d79d2e68 | 611 | usb_val = latency; |
98452d97 | 612 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) { |
d9f0cce7 TJ |
613 | ftdi->error_str = "Unable to set latency timer"; |
614 | return -2; | |
a3da1d95 GE |
615 | } |
616 | return 0; | |
617 | } | |
618 | ||
619 | ||
a8f46ddc TJ |
620 | int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) |
621 | { | |
a3da1d95 | 622 | unsigned short usb_val; |
98452d97 | 623 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) { |
a3da1d95 GE |
624 | ftdi->error_str = "Reading latency timer failed"; |
625 | return -1; | |
626 | } | |
627 | ||
628 | *latency = (unsigned char)usb_val; | |
629 | return 0; | |
630 | } | |
631 | ||
632 | ||
a8f46ddc TJ |
633 | void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) |
634 | { | |
f396dbad TJ |
635 | eeprom->vendor_id = 0x0403; |
636 | eeprom->product_id = 0x6001; | |
d9f0cce7 | 637 | |
b8aa7b35 TJ |
638 | eeprom->self_powered = 1; |
639 | eeprom->remote_wakeup = 1; | |
640 | eeprom->BM_type_chip = 1; | |
d9f0cce7 | 641 | |
b8aa7b35 TJ |
642 | eeprom->in_is_isochronous = 0; |
643 | eeprom->out_is_isochronous = 0; | |
644 | eeprom->suspend_pull_downs = 0; | |
d9f0cce7 | 645 | |
b8aa7b35 TJ |
646 | eeprom->use_serial = 0; |
647 | eeprom->change_usb_version = 0; | |
f396dbad | 648 | eeprom->usb_version = 0x0200; |
b8aa7b35 | 649 | eeprom->max_power = 0; |
d9f0cce7 | 650 | |
b8aa7b35 TJ |
651 | eeprom->manufacturer = NULL; |
652 | eeprom->product = NULL; | |
653 | eeprom->serial = NULL; | |
654 | } | |
655 | ||
656 | ||
657 | /* | |
658 | ftdi_eeprom_build return codes: | |
8ed61121 | 659 | positive value: used eeprom size |
b8aa7b35 TJ |
660 | -1: eeprom size (128 bytes) exceeded by custom strings |
661 | */ | |
a8f46ddc TJ |
662 | int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) |
663 | { | |
b8aa7b35 TJ |
664 | unsigned char i, j; |
665 | unsigned short checksum, value; | |
666 | unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0; | |
667 | int size_check; | |
668 | ||
669 | if (eeprom->manufacturer != NULL) | |
d9f0cce7 | 670 | manufacturer_size = strlen(eeprom->manufacturer); |
b8aa7b35 | 671 | if (eeprom->product != NULL) |
d9f0cce7 | 672 | product_size = strlen(eeprom->product); |
b8aa7b35 | 673 | if (eeprom->serial != NULL) |
d9f0cce7 | 674 | serial_size = strlen(eeprom->serial); |
b8aa7b35 | 675 | |
d9f0cce7 TJ |
676 | size_check = 128; // eeprom is 128 bytes |
677 | size_check -= 28; // 28 are always in use (fixed) | |
b8aa7b35 TJ |
678 | size_check -= manufacturer_size*2; |
679 | size_check -= product_size*2; | |
680 | size_check -= serial_size*2; | |
681 | ||
682 | // eeprom size exceeded? | |
683 | if (size_check < 0) | |
d9f0cce7 | 684 | return (-1); |
b8aa7b35 TJ |
685 | |
686 | // empty eeprom | |
687 | memset (output, 0, 128); | |
688 | ||
689 | // Addr 00: Stay 00 00 | |
690 | // Addr 02: Vendor ID | |
691 | output[0x02] = eeprom->vendor_id; | |
692 | output[0x03] = eeprom->vendor_id >> 8; | |
693 | ||
694 | // Addr 04: Product ID | |
695 | output[0x04] = eeprom->product_id; | |
696 | output[0x05] = eeprom->product_id >> 8; | |
697 | ||
698 | // Addr 06: Device release number (0400h for BM features) | |
699 | output[0x06] = 0x00; | |
d9f0cce7 | 700 | |
b8aa7b35 | 701 | if (eeprom->BM_type_chip == 1) |
d9f0cce7 | 702 | output[0x07] = 0x04; |
b8aa7b35 | 703 | else |
d9f0cce7 | 704 | output[0x07] = 0x02; |
b8aa7b35 TJ |
705 | |
706 | // Addr 08: Config descriptor | |
707 | // Bit 1: remote wakeup if 1 | |
708 | // Bit 0: self powered if 1 | |
709 | // | |
710 | j = 0; | |
711 | if (eeprom->self_powered == 1) | |
d9f0cce7 | 712 | j = j | 1; |
b8aa7b35 | 713 | if (eeprom->remote_wakeup == 1) |
d9f0cce7 | 714 | j = j | 2; |
b8aa7b35 TJ |
715 | output[0x08] = j; |
716 | ||
717 | // Addr 09: Max power consumption: max power = value * 2 mA | |
d9f0cce7 TJ |
718 | output[0x09] = eeprom->max_power; |
719 | ; | |
720 | ||
b8aa7b35 TJ |
721 | // Addr 0A: Chip configuration |
722 | // Bit 7: 0 - reserved | |
723 | // Bit 6: 0 - reserved | |
724 | // Bit 5: 0 - reserved | |
725 | // Bit 4: 1 - Change USB version | |
726 | // Bit 3: 1 - Use the serial number string | |
727 | // Bit 2: 1 - Enable suspend pull downs for lower power | |
728 | // Bit 1: 1 - Out EndPoint is Isochronous | |
729 | // Bit 0: 1 - In EndPoint is Isochronous | |
730 | // | |
731 | j = 0; | |
732 | if (eeprom->in_is_isochronous == 1) | |
d9f0cce7 | 733 | j = j | 1; |
b8aa7b35 | 734 | if (eeprom->out_is_isochronous == 1) |
d9f0cce7 | 735 | j = j | 2; |
b8aa7b35 | 736 | if (eeprom->suspend_pull_downs == 1) |
d9f0cce7 | 737 | j = j | 4; |
b8aa7b35 | 738 | if (eeprom->use_serial == 1) |
d9f0cce7 | 739 | j = j | 8; |
b8aa7b35 | 740 | if (eeprom->change_usb_version == 1) |
d9f0cce7 | 741 | j = j | 16; |
b8aa7b35 | 742 | output[0x0A] = j; |
d9f0cce7 | 743 | |
b8aa7b35 TJ |
744 | // Addr 0B: reserved |
745 | output[0x0B] = 0x00; | |
d9f0cce7 | 746 | |
b8aa7b35 TJ |
747 | // Addr 0C: USB version low byte when 0x0A bit 4 is set |
748 | // Addr 0D: USB version high byte when 0x0A bit 4 is set | |
749 | if (eeprom->change_usb_version == 1) { | |
750 | output[0x0C] = eeprom->usb_version; | |
d9f0cce7 | 751 | output[0x0D] = eeprom->usb_version >> 8; |
b8aa7b35 TJ |
752 | } |
753 | ||
754 | ||
755 | // Addr 0E: Offset of the manufacturer string + 0x80 | |
756 | output[0x0E] = 0x14 + 0x80; | |
757 | ||
758 | // Addr 0F: Length of manufacturer string | |
759 | output[0x0F] = manufacturer_size*2 + 2; | |
760 | ||
761 | // Addr 10: Offset of the product string + 0x80, calculated later | |
762 | // Addr 11: Length of product string | |
763 | output[0x11] = product_size*2 + 2; | |
764 | ||
765 | // Addr 12: Offset of the serial string + 0x80, calculated later | |
766 | // Addr 13: Length of serial string | |
767 | output[0x13] = serial_size*2 + 2; | |
768 | ||
769 | // Dynamic content | |
a862ddcf | 770 | output[0x14] = manufacturer_size*2 + 2; |
d9f0cce7 TJ |
771 | output[0x15] = 0x03; // type: string |
772 | ||
b8aa7b35 | 773 | i = 0x16, j = 0; |
d9f0cce7 | 774 | |
b8aa7b35 TJ |
775 | // Output manufacturer |
776 | for (j = 0; j < manufacturer_size; j++) { | |
d9f0cce7 TJ |
777 | output[i] = eeprom->manufacturer[j], i++; |
778 | output[i] = 0x00, i++; | |
b8aa7b35 TJ |
779 | } |
780 | ||
781 | // Output product name | |
d9f0cce7 | 782 | output[0x10] = i + 0x80; // calculate offset |
b8aa7b35 TJ |
783 | output[i] = product_size*2 + 2, i++; |
784 | output[i] = 0x03, i++; | |
785 | for (j = 0; j < product_size; j++) { | |
d9f0cce7 TJ |
786 | output[i] = eeprom->product[j], i++; |
787 | output[i] = 0x00, i++; | |
b8aa7b35 | 788 | } |
d9f0cce7 | 789 | |
b8aa7b35 | 790 | // Output serial |
d9f0cce7 | 791 | output[0x12] = i + 0x80; // calculate offset |
b8aa7b35 TJ |
792 | output[i] = serial_size*2 + 2, i++; |
793 | output[i] = 0x03, i++; | |
794 | for (j = 0; j < serial_size; j++) { | |
d9f0cce7 TJ |
795 | output[i] = eeprom->serial[j], i++; |
796 | output[i] = 0x00, i++; | |
b8aa7b35 TJ |
797 | } |
798 | ||
799 | // calculate checksum | |
800 | checksum = 0xAAAA; | |
d9f0cce7 | 801 | |
b8aa7b35 | 802 | for (i = 0; i < 63; i++) { |
d9f0cce7 TJ |
803 | value = output[i*2]; |
804 | value += output[(i*2)+1] << 8; | |
b8aa7b35 | 805 | |
d9f0cce7 TJ |
806 | checksum = value^checksum; |
807 | checksum = (checksum << 1) | (checksum >> 15); | |
b8aa7b35 TJ |
808 | } |
809 | ||
810 | output[0x7E] = checksum; | |
d9f0cce7 | 811 | output[0x7F] = checksum >> 8; |
b8aa7b35 | 812 | |
8ed61121 | 813 | return size_check; |
b8aa7b35 TJ |
814 | } |
815 | ||
816 | ||
a8f46ddc TJ |
817 | int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) |
818 | { | |
a3da1d95 GE |
819 | int i; |
820 | ||
821 | for (i = 0; i < 64; i++) { | |
98452d97 | 822 | if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) { |
d9f0cce7 TJ |
823 | ftdi->error_str = "Reading eeprom failed"; |
824 | return -1; | |
a3da1d95 GE |
825 | } |
826 | } | |
827 | ||
828 | return 0; | |
829 | } | |
830 | ||
831 | ||
a8f46ddc TJ |
832 | int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) |
833 | { | |
a3da1d95 GE |
834 | unsigned short usb_val; |
835 | int i; | |
836 | ||
837 | for (i = 0; i < 64; i++) { | |
d9f0cce7 TJ |
838 | usb_val = eeprom[i*2]; |
839 | usb_val += eeprom[(i*2)+1] << 8; | |
98452d97 | 840 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) { |
d9f0cce7 TJ |
841 | ftdi->error_str = "Unable to write eeprom"; |
842 | return -1; | |
843 | } | |
a3da1d95 GE |
844 | } |
845 | ||
846 | return 0; | |
847 | } | |
848 | ||
849 | ||
a8f46ddc TJ |
850 | int ftdi_erase_eeprom(struct ftdi_context *ftdi) |
851 | { | |
98452d97 | 852 | if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) { |
a3da1d95 GE |
853 | ftdi->error_str = "Unable to erase eeprom"; |
854 | return -1; | |
855 | } | |
856 | ||
857 | return 0; | |
858 | } |