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