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