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