Made arguments more sane and less libquickusb-like.
[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
12 #define QUICKUSB_BREQUESTTYPE_READ      0xc0
13 #define QUICKUSB_BREQUESTTYPE_WRITE     0x40
14
15 #define QUICKUSB_MAX_DATA_LEN 64
16
17 #define QUICKUSB_WINDEX_GPPIO_DIR 0
18 #define QUICKUSB_WINDEX_GPPIO_DATA 1
19
20 #define QUICKUSB_TIMEOUT ( 1 * HZ )
21
22 /**
23  * quickusb_read_setting - read device setting
24  *
25  * @usb: USB device
26  * @address: Setting address
27  * @setting: Value of the setting
28  *
29  * Returns 0 for success, or negative error number
30  */
31 static inline int quickusb_read_setting ( struct usb_device *usb,
32                                           unsigned int address,
33                                           uint16_t *setting ) {
34         uint16_t setting_le;
35         int ret;
36
37         ret =  usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
38                                  QUICKUSB_BREQUEST_SETTING,
39                                  QUICKUSB_BREQUESTTYPE_READ,
40                                  0, address,
41                                  &setting_le, sizeof ( setting_le ),
42                                  QUICKUSB_TIMEOUT );
43
44         *setting = le16_to_cpu ( setting_le );
45         return ( ret > 0 ) ? 0 : ret;
46 }
47
48 /**
49  * quickusb_write_setting - write device setting
50  *
51  * @usb: USB device
52  * @address: Setting address
53  * @setting: Value of the setting
54  *
55  * Returns 0 for success, or negative error number
56  */
57 static inline int quickusb_write_setting ( struct usb_device *usb,
58                                            unsigned int address,
59                                            uint16_t setting ) {
60         uint16_t setting_le = cpu_to_le16 ( setting );
61         int ret;
62
63         ret =  usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
64                                  QUICKUSB_BREQUEST_SETTING,
65                                  QUICKUSB_BREQUESTTYPE_WRITE,
66                                  0, address,
67                                  &setting_le, sizeof ( setting_le ),
68                                  QUICKUSB_TIMEOUT );
69
70         return ( ret > 0 ) ? 0 : ret;
71 }
72
73 /**
74  * quickusb_read_command - read HSPIO port with a command cycle
75  *
76  * @usb: USB device
77  * @address: Starting address
78  * @data: Data buffer
79  * @len: Length of data to read (max QUICKUSB_MAX_DATA_LEN)
80  *
81  * Returns 0 for success, or negative error number
82  */
83 static inline int quickusb_read_command ( struct usb_device *usb,
84                                           uint16_t address,
85                                           void *data, size_t len ) {
86         int ret;
87         
88         ret =  usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
89                                  QUICKUSB_BREQUEST_HSPIO_COMMAND,
90                                  QUICKUSB_BREQUESTTYPE_READ,
91                                  len, address,
92                                  data, len, QUICKUSB_TIMEOUT );
93
94         return ( ret > 0 ) ? 0 : ret;
95 }
96
97 /**
98  * quickusb_write_command - write HSPIO port with a command cycle
99  *
100  * @usb: USB device
101  * @address: Starting address
102  * @data: Data to be written
103  * @len: Length of data to write (max QUICKUSB_MAX_DATA_LEN)
104  *
105  * Returns 0 for success, or negative error number
106  */
107 static inline int quickusb_write_command ( struct usb_device *usb,
108                                            uint16_t address,
109                                            void *data, size_t len ) {
110         int ret;
111         
112         ret =  usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
113                                  QUICKUSB_BREQUEST_HSPIO_COMMAND,
114                                  QUICKUSB_BREQUESTTYPE_WRITE,
115                                  len, address,
116                                  data, len, QUICKUSB_TIMEOUT );
117
118         return ( ret > 0 ) ? 0 : ret;
119 }
120
121 /**
122  * quickusb_read_port_dir - read GPPIO port output enables
123  *
124  * @usb: USB device
125  * @address: Port number
126  * @outputs: Output bit mask
127  *
128  * Returns 0 for success, or negative error number
129  */
130 static inline int quickusb_read_port_dir ( struct usb_device *usb,
131                                            unsigned int address,
132                                            uint8_t *outputs ) {
133         int ret;
134         
135         ret =  usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
136                                  QUICKUSB_BREQUEST_GPPIO,
137                                  QUICKUSB_BREQUESTTYPE_READ,
138                                  address, QUICKUSB_WINDEX_GPPIO_DIR,
139                                  outputs, sizeof ( *outputs ),
140                                  QUICKUSB_TIMEOUT );
141
142         return ( ret > 0 ) ? 0 : ret;
143 }
144
145 /**
146  * quickusb_write_port_dir - set GPPIO port output enables
147  *
148  * @usb: USB device
149  * @address: Port number
150  * @outputs: Output bit mask
151  *
152  * Returns 0 for success, or negative error number
153  */
154 static inline int quickusb_write_port_dir ( struct usb_device *usb,
155                                             unsigned int address,
156                                             uint8_t outputs ) {
157         int ret;
158
159         ret =  usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
160                                  QUICKUSB_BREQUEST_GPPIO,
161                                  QUICKUSB_BREQUESTTYPE_WRITE,
162                                  address, QUICKUSB_WINDEX_GPPIO_DIR,
163                                  &outputs, sizeof ( outputs ),
164                                  QUICKUSB_TIMEOUT );
165
166         if ( ret > 0 ) {
167                 ret = 0;
168         }
169
170         return ret;
171 }
172
173 /**
174  * quickusb_read_port - read data from GPPIO port
175  *
176  * @usb: USB device
177  * @address: Port number
178  * @data: Data buffer
179  * @len: Length of data to read (max QUICKUSB_MAX_DATA_LEN)
180  *
181  * Returns 0 for success, or negative error number
182  */
183 static inline int quickusb_read_port ( struct usb_device *usb,
184                                        unsigned int address,
185                                        void *data, size_t len ) {
186         int ret;
187         
188         ret =  usb_control_msg ( usb, usb_rcvctrlpipe ( usb, 0 ),
189                                  QUICKUSB_BREQUEST_GPPIO,
190                                  QUICKUSB_BREQUESTTYPE_READ,
191                                  address, QUICKUSB_WINDEX_GPPIO_DATA,
192                                  data, len, QUICKUSB_TIMEOUT );
193
194         return ( ret > 0 ) ? 0 : ret;
195 }
196
197 /**
198  * quickusb_write_port - write data to GPPIO port
199  *
200  * @usb: USB device
201  * @address: Port number
202  * @data: Data to be written
203  * @len: Length of data to write (max QUICKUSB_MAX_DATA_LEN)
204  *
205  * Returns 0 for success, or negative error number
206  */
207 static inline int quickusb_write_port ( struct usb_device *usb,
208                                         unsigned int address,
209                                         void *data, size_t len ) {
210         int ret;
211
212         ret =  usb_control_msg ( usb, usb_sndctrlpipe ( usb, 0 ),
213                                  QUICKUSB_BREQUEST_GPPIO,
214                                  QUICKUSB_BREQUESTTYPE_WRITE,
215                                  address, QUICKUSB_WINDEX_GPPIO_DATA,
216                                  data, len, QUICKUSB_TIMEOUT );
217         
218         return ( ret > 0 ) ? 0 : ret;
219 }
220
221 #endif /* __KERNEL__ */
222
223 /****************************************************************************
224  *
225  * ioctls
226  *
227  */
228
229 typedef uint32_t quickusb_gppio_ioctl_data_t;
230
231 #define QUICKUSB_IOC_GPPIO_GET_OUTPUTS \
232         _IOR ( 'Q', 0x00, quickusb_gppio_ioctl_data_t )
233 #define QUICKUSB_IOC_GPPIO_SET_OUTPUTS \
234         _IOW ( 'Q', 0x01, quickusb_gppio_ioctl_data_t )
235
236 #endif /* QUICKUSB_H */