Added ioctls for get/set default outputs and levels. The ioctls seem to
[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_HSPIO_COMMAND 0xb2
10 #define QUICKUSB_BREQUEST_GPPIO         0xb3
11 #define QUICKUSB_BREQUEST_HSPIO         0xb7
12
13 #define QUICKUSB_BREQUESTTYPE_READ      0xc0
14 #define QUICKUSB_BREQUESTTYPE_WRITE     0x40
15
16 #define QUICKUSB_BULK_OUT_EP            0x02
17 #define QUICKUSB_BULK_IN_EP             0x86
18
19 #define QUICKUSB_MAX_DATA_LEN           64
20 #define QUICKUSB_MAX_BULK_DATA_LEN      512
21
22 #define QUICKUSB_WINDEX_GPPIO_DIR       0
23 #define QUICKUSB_WINDEX_GPPIO_DATA      1
24
25 #define QUICKUSB_SETTING_GPPIO(port) ( 9 + (port) )
26
27 #define QUICKUSB_TIMEOUT ( 1 * HZ )
28
29 /**
30  * quickusb_read_setting - read device setting
31  *
32  * @usb: USB device
33  * @address: Setting address
34  * @setting: Value of the setting
35  *
36  * Returns 0 for success, or negative error number
37  */
38 static inline int quickusb_read_setting ( struct usb_device *usb,
39                                           unsigned int address,
40                                           uint16_t *setting ) {
41         uint16_t setting_le;
42         int ret;
43
44         ret = usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
45                                 QUICKUSB_BREQUEST_SETTING,
46                                 QUICKUSB_BREQUESTTYPE_READ,
47                                 0, address,
48                                 &setting_le, sizeof ( setting_le ),
49                                 QUICKUSB_TIMEOUT );
50         if ( ret < 0 )
51                 return ret;
52
53         *setting = le16_to_cpu ( setting_le );
54         return 0;
55 }
56
57 /**
58  * quickusb_write_setting - write device setting
59  *
60  * @usb: USB device
61  * @address: Setting address
62  * @setting: Value of the setting
63  *
64  * Returns 0 for success, or negative error number
65  */
66 static inline int quickusb_write_setting ( struct usb_device *usb,
67                                            unsigned int address,
68                                            uint16_t setting ) {
69         uint16_t setting_le = cpu_to_le16 ( setting );
70         int ret;
71
72         ret = usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
73                                 QUICKUSB_BREQUEST_SETTING,
74                                 QUICKUSB_BREQUESTTYPE_WRITE,
75                                 0, address,
76                                 &setting_le, sizeof ( setting_le ),
77                                 QUICKUSB_TIMEOUT );
78         if ( ret < 0 )
79                 return ret;
80
81         return 0;
82 }
83
84 /**
85  * quickusb_read_command - read HSPIO port with a command cycle
86  *
87  * @usb: USB device
88  * @address: Starting address
89  * @data: Data buffer
90  * @len: Length of data to read (max QUICKUSB_MAX_DATA_LEN)
91  *
92  * Returns 0 for success, or negative error number
93  */
94 static inline int quickusb_read_command ( struct usb_device *usb,
95                                           uint16_t address,
96                                           void *data, size_t len ) {
97         int ret;
98         
99         ret = usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
100                                 QUICKUSB_BREQUEST_HSPIO_COMMAND,
101                                 QUICKUSB_BREQUESTTYPE_READ,
102                                 len, address,
103                                 data, len, QUICKUSB_TIMEOUT );
104         if ( ret < 0 )
105                 return ret;
106
107         return 0;
108 }
109
110 /**
111  * quickusb_write_command - write HSPIO port with a command cycle
112  *
113  * @usb: USB device
114  * @address: Starting address
115  * @data: Data to be written
116  * @len: Length of data to write (max QUICKUSB_MAX_DATA_LEN)
117  *
118  * Returns 0 for success, or negative error number
119  */
120 static inline int quickusb_write_command ( struct usb_device *usb,
121                                            uint16_t address,
122                                            void *data, size_t len ) {
123         int ret;
124         
125         ret = usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
126                                 QUICKUSB_BREQUEST_HSPIO_COMMAND,
127                                 QUICKUSB_BREQUESTTYPE_WRITE,
128                                 len, address,
129                                 data, len, QUICKUSB_TIMEOUT );
130         if ( ret < 0 )
131                 return ret;
132
133         return 0;
134 }
135
136 /**
137  * quickusb_read_data - read HSPIO port with a data cycle
138  *
139  * @usb: USB device
140  * @data: Data buffer
141  * @len: Length of data to read (max QUICKUSB_MAX_BULK_DATA_LEN)
142  *
143  * Returns 0 for success, or negative error number
144  */
145 static inline int quickusb_read_data ( struct usb_device *usb,
146                                        void *data, size_t len ) {
147         uint32_t len_le = cpu_to_le32 ( len );
148         int actual_length;
149         int ret;
150         
151         ret = usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
152                                 QUICKUSB_BREQUEST_HSPIO,
153                                 QUICKUSB_BREQUESTTYPE_WRITE,
154                                 0, 0,
155                                 &len_le, sizeof ( len_le ),
156                                 QUICKUSB_TIMEOUT );
157         if ( ret < 0 )
158                 return ret;
159
160         ret = usb_bulk_msg ( usb,
161                              usb_rcvbulkpipe ( usb, QUICKUSB_BULK_IN_EP ),
162                              data, len, &actual_length, QUICKUSB_TIMEOUT );
163         if ( ret < 0 )
164                 return ret;
165
166         return 0;
167 }
168
169 /**
170  * quickusb_write_data - write HSPIO port with a data cycle
171  *
172  * @usb: USB device
173  * @data: Data to be written
174  * @len: Length of data to write (max QUICKUSB_MAX_BULK_DATA_LEN)
175  *
176  * Returns 0 for success, or negative error number
177  */
178 static inline int quickusb_write_data ( struct usb_device *usb,
179                                         void *data, size_t len ) {
180         int actual_length;
181         int ret;
182         
183         ret = usb_bulk_msg ( usb,
184                              usb_sndbulkpipe ( usb, QUICKUSB_BULK_OUT_EP ),
185                              data, len, &actual_length, QUICKUSB_TIMEOUT );
186         if ( ret < 0 )
187                 return ret;
188
189         return 0;
190 }
191
192 /**
193  * quickusb_read_port_dir - read GPPIO port output enables
194  *
195  * @usb: USB device
196  * @address: Port number
197  * @outputs: Output bit mask
198  *
199  * Returns 0 for success, or negative error number
200  */
201 static inline int quickusb_read_port_dir ( struct usb_device *usb,
202                                            unsigned int address,
203                                            uint8_t *outputs ) {
204         int ret;
205         
206         ret = usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
207                                 QUICKUSB_BREQUEST_GPPIO,
208                                 QUICKUSB_BREQUESTTYPE_READ,
209                                 address, QUICKUSB_WINDEX_GPPIO_DIR,
210                                 outputs, sizeof ( *outputs ),
211                                 QUICKUSB_TIMEOUT );
212         if ( ret < 0 )
213                 return ret;
214
215         return 0;
216 }
217
218 /**
219  * quickusb_write_port_dir - set GPPIO port output enables
220  *
221  * @usb: USB device
222  * @address: Port number
223  * @outputs: Output bit mask
224  *
225  * Returns 0 for success, or negative error number
226  */
227 static inline int quickusb_write_port_dir ( struct usb_device *usb,
228                                             unsigned int address,
229                                             uint8_t outputs ) {
230         int ret;
231
232         ret = usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
233                                 QUICKUSB_BREQUEST_GPPIO,
234                                 QUICKUSB_BREQUESTTYPE_WRITE,
235                                 address, QUICKUSB_WINDEX_GPPIO_DIR,
236                                 &outputs, sizeof ( outputs ),
237                                 QUICKUSB_TIMEOUT );
238         if ( ret < 0 )
239                 return ret;
240
241         return 0;
242 }
243
244 /**
245  * quickusb_read_port - read data from GPPIO port
246  *
247  * @usb: USB device
248  * @address: Port number
249  * @data: Data buffer
250  * @len: Length of data to read (max QUICKUSB_MAX_DATA_LEN)
251  *
252  * Returns 0 for success, or negative error number
253  */
254 static inline int quickusb_read_port ( struct usb_device *usb,
255                                        unsigned int address,
256                                        void *data, size_t len ) {
257         int ret;
258         
259         ret = usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
260                                 QUICKUSB_BREQUEST_GPPIO,
261                                 QUICKUSB_BREQUESTTYPE_READ,
262                                 address, QUICKUSB_WINDEX_GPPIO_DATA,
263                                 data, len, QUICKUSB_TIMEOUT );
264         if ( ret < 0 )
265                 return ret;
266
267         return 0;
268 }
269
270 /**
271  * quickusb_write_port - write data to GPPIO port
272  *
273  * @usb: USB device
274  * @address: Port number
275  * @data: Data to be written
276  * @len: Length of data to write (max QUICKUSB_MAX_DATA_LEN)
277  *
278  * Returns 0 for success, or negative error number
279  */
280 static inline int quickusb_write_port ( struct usb_device *usb,
281                                         unsigned int address,
282                                         void *data, size_t len ) {
283         int ret;
284
285         ret = usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
286                                 QUICKUSB_BREQUEST_GPPIO,
287                                 QUICKUSB_BREQUESTTYPE_WRITE,
288                                 address, QUICKUSB_WINDEX_GPPIO_DATA,
289                                 data, len, QUICKUSB_TIMEOUT );
290         if ( ret < 0 )
291                 return ret;
292
293         return 0;
294 }
295
296 #endif /* __KERNEL__ */
297
298 /****************************************************************************
299  *
300  * ioctls
301  *
302  */
303
304 typedef uint32_t quickusb_gppio_ioctl_data_t;
305
306 #define QUICKUSB_IOC_GPPIO_GET_OUTPUTS \
307         _IOR ( 'Q', 0x00, quickusb_gppio_ioctl_data_t )
308 #define QUICKUSB_IOC_GPPIO_SET_OUTPUTS \
309         _IOW ( 'Q', 0x01, quickusb_gppio_ioctl_data_t )
310 #define QUICKUSB_IOC_GPPIO_GET_DEFAULT_OUTPUTS \
311         _IOR ( 'Q', 0x02, quickusb_gppio_ioctl_data_t )
312 #define QUICKUSB_IOC_GPPIO_SET_DEFAULT_OUTPUTS \
313         _IOW ( 'Q', 0x03, quickusb_gppio_ioctl_data_t )
314 #define QUICKUSB_IOC_GPPIO_GET_DEFAULT_LEVELS \
315         _IOR ( 'Q', 0x04, quickusb_gppio_ioctl_data_t )
316 #define QUICKUSB_IOC_GPPIO_SET_DEFAULT_LEVELS \
317         _IOW ( 'Q', 0x05, quickusb_gppio_ioctl_data_t )
318
319 #endif /* QUICKUSB_H */