89aae16aaec190650e1e6d7c62d322531898ce2a
[people/mcb30/quickusb.git] / kernel / quickusb.h
1 #ifndef QUICKUSB_H
2 #define QUICKUSB_H
3
4 #ifdef __KERNEL__
5
6 #include <linux/ioctl.h>
7
8 #define QUICKUSB_BREQUEST_SETTING 0xb0
9 #define QUICKUSB_BREQUEST_GPPIO 0xb3
10 #define QUICKUSB_BREQUESTTYPE_READ 0xc0
11 #define QUICKUSB_BREQUESTTYPE_WRITE 0x40
12 #define QUICKUSB_MAX_DATA_LEN 64
13
14 #define QUICKUSB_WINDEX_GPPIO_DIR 0
15 #define QUICKUSB_WINDEX_GPPIO_DATA 1
16
17 #define QUICKUSB_TIMEOUT ( 1 * HZ )
18
19 /**
20  * quickusb_read_setting - read device setting
21  *
22  * @usb: USB device
23  * @address: Setting address
24  * @setting: Value of the setting
25  *
26  * Returns 0 for success, or negative error number
27  */
28 static inline int quickusb_read_setting ( struct usb_device *usb,
29                                           unsigned int address,
30                                           uint16_t *setting ) {
31         int ret;
32
33         ret =  usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
34                                  QUICKUSB_BREQUEST_SETTING,
35                                  QUICKUSB_BREQUESTTYPE_READ,
36                                  0, address,
37                                  setting, sizeof ( *setting ),
38                                  QUICKUSB_TIMEOUT );
39         if ( ret > 0 ) {
40                 ret = 0;
41         }
42
43         return ret;
44 }
45
46 /**
47  * quickusb_write_setting - write device setting
48  *
49  * @usb: USB device
50  * @address: Setting address
51  * @setting: Value of the setting
52  *
53  * Returns 0 for success, or negative error number
54  */
55 static inline int quickusb_write_setting ( struct usb_device *usb,
56                                            unsigned int address,
57                                            uint16_t setting ) {
58         int ret;
59
60         ret =  usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
61                                  QUICKUSB_BREQUEST_SETTING,
62                                  QUICKUSB_BREQUESTTYPE_WRITE,
63                                  0, address,
64                                  &setting, sizeof ( setting ),
65                                  QUICKUSB_TIMEOUT );
66         if ( ret > 0 ) {
67                 ret = 0;
68         }
69
70         return ret;
71 }
72
73 /**
74  * quickusb_read_port_dir - read GPPIO port output enables
75  *
76  * @usb: USB device
77  * @address: Port number
78  * @outputs: Output bit mask
79  *
80  * Returns 0 for success, or negative error number
81  */
82 static inline int quickusb_read_port_dir ( struct usb_device *usb,
83                                            unsigned int address,
84                                            uint8_t *outputs ) {
85         int ret;
86         
87         ret =  usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
88                                  QUICKUSB_BREQUEST_GPPIO,
89                                  QUICKUSB_BREQUESTTYPE_READ,
90                                  address, QUICKUSB_WINDEX_GPPIO_DIR,
91                                  outputs, sizeof ( *outputs ),
92                                  QUICKUSB_TIMEOUT );
93         if ( ret > 0 ) {
94                 ret = 0;
95         }
96
97         return ret;
98 }
99
100 /**
101  * quickusb_write_port_dir - set GPPIO port output enables
102  *
103  * @usb: USB device
104  * @address: Port number
105  * @outputs: Output bit mask
106  *
107  * Returns 0 for success, or negative error number
108  */
109 static inline int quickusb_write_port_dir ( struct usb_device *usb,
110                                             unsigned int address,
111                                             uint8_t outputs ) {
112         int ret;
113
114         ret =  usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
115                                  QUICKUSB_BREQUEST_GPPIO,
116                                  QUICKUSB_BREQUESTTYPE_WRITE,
117                                  address, QUICKUSB_WINDEX_GPPIO_DIR,
118                                  &outputs, sizeof ( outputs ),
119                                  QUICKUSB_TIMEOUT );
120
121         if ( ret > 0 ) {
122                 ret = 0;
123         }
124
125         return ret;
126 }
127
128 /**
129  * quickusb_read_port - read data from GPPIO port
130  *
131  * @usb: USB device
132  * @address: Port number
133  * @data: Data buffer
134  * @len: Length of data to read (max QUICKUSB_MAX_DATA_LEN)
135  *
136  * Returns 0 for success, or negative error number
137  */
138 static inline int quickusb_read_port ( struct usb_device *usb,
139                                        unsigned int address,
140                                        void *data, size_t *len ) {
141         int ret;
142         
143         ret =  usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
144                                  QUICKUSB_BREQUEST_GPPIO,
145                                  QUICKUSB_BREQUESTTYPE_READ,
146                                  address, QUICKUSB_WINDEX_GPPIO_DATA,
147                                  data, *len, QUICKUSB_TIMEOUT );
148         if ( ret > 0 ) {
149                 *len = ret;
150                 ret = 0;
151         }
152
153         return ret;
154 }
155
156 /**
157  * quickusb_write_port - write data to GPPIO port
158  *
159  * @usb: USB device
160  * @address: Port number
161  * @data: Data to be written
162  * @len: Length of data to write (max QUICKUSB_MAX_DATA_LEN)
163  *
164  * Returns 0 for success, or negative error number
165  */
166 static inline int quickusb_write_port ( struct usb_device *usb,
167                                         unsigned int address,
168                                         void *data, size_t *len ) {
169         int ret;
170
171         ret =  usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
172                                  QUICKUSB_BREQUEST_GPPIO,
173                                  QUICKUSB_BREQUESTTYPE_WRITE,
174                                  address, QUICKUSB_WINDEX_GPPIO_DATA,
175                                  data, *len, QUICKUSB_TIMEOUT );
176
177         if ( ret > 0 ) {
178                 *len = ret;
179                 ret = 0;
180         }
181
182         return ret;
183 }
184
185 #endif /* __KERNEL__ */
186
187 /****************************************************************************
188  *
189  * ioctls
190  *
191  */
192
193 typedef uint32_t quickusb_gppio_ioctl_data_t;
194
195 #define QUICKUSB_IOC_GPPIO_GET_OUTPUTS \
196         _IOR ( 'Q', 0x00, quickusb_gppio_ioctl_data_t )
197 #define QUICKUSB_IOC_GPPIO_SET_OUTPUTS \
198         _IOW ( 'Q', 0x01, quickusb_gppio_ioctl_data_t )
199
200 #endif /* QUICKUSB_H */