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