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