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