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