libftdi: (tomj) refactored USB part of libftdi, doesn't depend
[libftdi] / ftdi / ftdi.h
1 /***************************************************************************
2                           ftdi.h  -  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
17 #ifndef __libftdi_h__
18 #define __libftdi_h__
19
20 #include <sys/types.h>
21 #include <limits.h>
22
23 /* Kernel USB headers */
24 #include <linux/usb.h>
25 #include <linux/usbdevice_fs.h>
26
27 enum ftdi_chip_type { TYPE_AM=0, TYPE_BM=1, TYPE_2232C=2 };
28
29 struct ftdi_context {
30     // USB specific
31     int usb_fd;
32     int usb_read_timeout;
33     int usb_write_timeout;
34     struct usbdevfs_urb *urb;
35
36     // FTDI specific
37     enum ftdi_chip_type type;
38     int baudrate;
39     unsigned char bitbang_enabled;
40     unsigned char *readbuffer;
41     unsigned int readbuffer_offset;
42     unsigned int readbuffer_remaining;
43     unsigned int readbuffer_chunksize;
44     unsigned int writebuffer_chunksize;
45
46     // FTDI FT2232C requirecments
47     int interface;   // 0 or 1
48     int index;       // 1 or 2
49     // Endpoints
50     int in_ep;
51     int out_ep;      // 1 or 2
52     
53     /* 1: (default) Normal bitbang mode, 2: FT2232C SPI bitbang mode */
54     unsigned char bitbang_mode;
55
56     // misc
57     char *error_str;
58 };
59
60 struct ftdi_eeprom {
61     int vendor_id;
62     int product_id;
63
64     int self_powered;
65     int remote_wakeup;
66     int BM_type_chip;
67
68     int in_is_isochronous;
69     int out_is_isochronous;
70     int suspend_pull_downs;
71
72     int use_serial;
73     int change_usb_version;
74     int usb_version;
75     int max_power;
76
77     char *manufacturer;
78     char *product;
79     char *serial;
80 };
81
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85
86     int ftdi_init(struct ftdi_context *ftdi);
87     void ftdi_deinit(struct ftdi_context *ftdi);
88     int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
89     int ftdi_usb_close(struct ftdi_context *ftdi);
90     int ftdi_usb_reset(struct ftdi_context *ftdi);
91     int ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
92
93     int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
94
95     int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
96     int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
97     int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
98
99     int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
100     int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
101     int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
102
103     int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask);
104     int ftdi_disable_bitbang(struct ftdi_context *ftdi);
105     int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
106
107     int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
108     int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
109
110     /* init and build eeprom from ftdi_eeprom structure */
111     void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom);
112     int  ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output);
113
114     /* "eeprom" needs to be valid 128 byte eeprom (generated by the eeprom generator)
115        the checksum of the eeprom is valided */
116     int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
117     int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
118     int ftdi_erase_eeprom(struct ftdi_context *ftdi);
119
120 #ifdef __cplusplus
121 }
122 #endif
123
124 #endif /* __libftdi_h__ */