Merge tag 'stable/for-linus-3.14-rc2-tag' of git://git.kernel.org/pub/scm/linux/kerne...
[cascardo/linux.git] / drivers / staging / rtl8188eu / core / rtw_io.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 /*
21
22 The purpose of rtw_io.c
23
24 a. provides the API
25
26 b. provides the protocol engine
27
28 c. provides the software interface between caller and the hardware interface
29
30
31 Compiler Flag Option:
32
33 USB:
34    a. USE_ASYNC_IRP: Both sync/async operations are provided.
35
36 Only sync read/rtw_write_mem operations are provided.
37
38 jackson@realtek.com.tw
39
40 */
41
42 #define _RTW_IO_C_
43 #include <osdep_service.h>
44 #include <drv_types.h>
45 #include <rtw_io.h>
46 #include <osdep_intf.h>
47 #include <usb_ops.h>
48
49 #define rtw_le16_to_cpu(val)            le16_to_cpu(val)
50 #define rtw_le32_to_cpu(val)            le32_to_cpu(val)
51 #define rtw_cpu_to_le16(val)            cpu_to_le16(val)
52 #define rtw_cpu_to_le32(val)            cpu_to_le32(val)
53
54
55 u8 _rtw_read8(struct adapter *adapter, u32 addr)
56 {
57         u8 r_val;
58         struct io_priv *pio_priv = &adapter->iopriv;
59         struct  intf_hdl *pintfhdl = &(pio_priv->intf);
60         u8 (*_read8)(struct intf_hdl *pintfhdl, u32 addr);
61
62         _func_enter_;
63         _read8 = pintfhdl->io_ops._read8;
64         r_val = _read8(pintfhdl, addr);
65         _func_exit_;
66         return r_val;
67 }
68
69 u16 _rtw_read16(struct adapter *adapter, u32 addr)
70 {
71         u16 r_val;
72         struct io_priv *pio_priv = &adapter->iopriv;
73         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
74         u16 (*_read16)(struct intf_hdl *pintfhdl, u32 addr);
75 _func_enter_;
76         _read16 = pintfhdl->io_ops._read16;
77
78         r_val = _read16(pintfhdl, addr);
79 _func_exit_;
80         return r_val;
81 }
82
83 u32 _rtw_read32(struct adapter *adapter, u32 addr)
84 {
85         u32 r_val;
86         struct io_priv *pio_priv = &adapter->iopriv;
87         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
88         u32     (*_read32)(struct intf_hdl *pintfhdl, u32 addr);
89 _func_enter_;
90         _read32 = pintfhdl->io_ops._read32;
91
92         r_val = _read32(pintfhdl, addr);
93 _func_exit_;
94         return r_val;
95 }
96
97 int _rtw_write8(struct adapter *adapter, u32 addr, u8 val)
98 {
99         struct io_priv *pio_priv = &adapter->iopriv;
100         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
101         int (*_write8)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
102         int ret;
103         _func_enter_;
104         _write8 = pintfhdl->io_ops._write8;
105
106         ret = _write8(pintfhdl, addr, val);
107         _func_exit_;
108
109         return RTW_STATUS_CODE(ret);
110 }
111
112 int _rtw_write16(struct adapter *adapter, u32 addr, u16 val)
113 {
114         struct io_priv *pio_priv = &adapter->iopriv;
115         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
116         int (*_write16)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
117         int ret;
118         _func_enter_;
119         _write16 = pintfhdl->io_ops._write16;
120
121         ret = _write16(pintfhdl, addr, val);
122         _func_exit_;
123
124         return RTW_STATUS_CODE(ret);
125 }
126 int _rtw_write32(struct adapter *adapter, u32 addr, u32 val)
127 {
128         struct io_priv *pio_priv = &adapter->iopriv;
129         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
130         int (*_write32)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
131         int ret;
132         _func_enter_;
133         _write32 = pintfhdl->io_ops._write32;
134
135         ret = _write32(pintfhdl, addr, val);
136         _func_exit_;
137
138         return RTW_STATUS_CODE(ret);
139 }
140
141 int _rtw_writeN(struct adapter *adapter, u32 addr , u32 length , u8 *pdata)
142 {
143         struct io_priv *pio_priv = &adapter->iopriv;
144         struct  intf_hdl *pintfhdl = (struct intf_hdl *)(&(pio_priv->intf));
145         int (*_writeN)(struct intf_hdl *pintfhdl, u32 addr, u32 length, u8 *pdata);
146         int ret;
147         _func_enter_;
148         _writeN = pintfhdl->io_ops._writeN;
149
150         ret = _writeN(pintfhdl, addr, length, pdata);
151         _func_exit_;
152
153         return RTW_STATUS_CODE(ret);
154 }
155 int _rtw_write8_async(struct adapter *adapter, u32 addr, u8 val)
156 {
157         struct io_priv *pio_priv = &adapter->iopriv;
158         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
159         int (*_write8_async)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
160         int ret;
161         _func_enter_;
162         _write8_async = pintfhdl->io_ops._write8_async;
163
164         ret = _write8_async(pintfhdl, addr, val);
165         _func_exit_;
166
167         return RTW_STATUS_CODE(ret);
168 }
169
170 int _rtw_write16_async(struct adapter *adapter, u32 addr, u16 val)
171 {
172         struct io_priv *pio_priv = &adapter->iopriv;
173         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
174         int (*_write16_async)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
175         int ret;
176
177 _func_enter_;
178         _write16_async = pintfhdl->io_ops._write16_async;
179         ret = _write16_async(pintfhdl, addr, val);
180 _func_exit_;
181
182         return RTW_STATUS_CODE(ret);
183 }
184
185 int _rtw_write32_async(struct adapter *adapter, u32 addr, u32 val)
186 {
187         struct io_priv *pio_priv = &adapter->iopriv;
188         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
189         int (*_write32_async)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
190         int ret;
191
192 _func_enter_;
193         _write32_async = pintfhdl->io_ops._write32_async;
194         ret = _write32_async(pintfhdl, addr, val);
195 _func_exit_;
196
197         return RTW_STATUS_CODE(ret);
198 }
199
200 void _rtw_read_mem(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
201 {
202         void (*_read_mem)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
203         struct io_priv *pio_priv = &adapter->iopriv;
204         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
205
206         _func_enter_;
207         if (adapter->bDriverStopped || adapter->bSurpriseRemoved) {
208                 RT_TRACE(_module_rtl871x_io_c_, _drv_info_,
209                          ("rtw_read_mem:bDriverStopped(%d) OR bSurpriseRemoved(%d)",
210                          adapter->bDriverStopped, adapter->bSurpriseRemoved));
211              return;
212         }
213         _read_mem = pintfhdl->io_ops._read_mem;
214         _read_mem(pintfhdl, addr, cnt, pmem);
215         _func_exit_;
216 }
217
218 void _rtw_write_mem(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
219 {
220         void (*_write_mem)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
221         struct io_priv *pio_priv = &adapter->iopriv;
222         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
223
224         _func_enter_;
225
226         _write_mem = pintfhdl->io_ops._write_mem;
227
228         _write_mem(pintfhdl, addr, cnt, pmem);
229
230         _func_exit_;
231 }
232
233 void _rtw_read_port(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
234 {
235         u32 (*_read_port)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
236         struct io_priv *pio_priv = &adapter->iopriv;
237         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
238
239         _func_enter_;
240
241         if (adapter->bDriverStopped || adapter->bSurpriseRemoved) {
242                 RT_TRACE(_module_rtl871x_io_c_, _drv_info_,
243                          ("rtw_read_port:bDriverStopped(%d) OR bSurpriseRemoved(%d)",
244                          adapter->bDriverStopped, adapter->bSurpriseRemoved));
245              return;
246         }
247
248         _read_port = pintfhdl->io_ops._read_port;
249
250         _read_port(pintfhdl, addr, cnt, pmem);
251
252         _func_exit_;
253 }
254
255 void _rtw_read_port_cancel(struct adapter *adapter)
256 {
257         void (*_read_port_cancel)(struct intf_hdl *pintfhdl);
258         struct io_priv *pio_priv = &adapter->iopriv;
259         struct intf_hdl *pintfhdl = &(pio_priv->intf);
260
261         _read_port_cancel = pintfhdl->io_ops._read_port_cancel;
262
263         if (_read_port_cancel)
264                 _read_port_cancel(pintfhdl);
265 }
266
267 u32 _rtw_write_port(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
268 {
269         u32 (*_write_port)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
270         struct io_priv *pio_priv = &adapter->iopriv;
271         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
272         u32 ret = _SUCCESS;
273
274         _func_enter_;
275
276         _write_port = pintfhdl->io_ops._write_port;
277
278         ret = _write_port(pintfhdl, addr, cnt, pmem);
279
280          _func_exit_;
281
282         return ret;
283 }
284
285 u32 _rtw_write_port_and_wait(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem, int timeout_ms)
286 {
287         int ret = _SUCCESS;
288         struct xmit_buf *pxmitbuf = (struct xmit_buf *)pmem;
289         struct submit_ctx sctx;
290
291         rtw_sctx_init(&sctx, timeout_ms);
292         pxmitbuf->sctx = &sctx;
293
294         ret = _rtw_write_port(adapter, addr, cnt, pmem);
295
296         if (ret == _SUCCESS)
297                 ret = rtw_sctx_wait(&sctx);
298
299         return ret;
300 }
301
302 void _rtw_write_port_cancel(struct adapter *adapter)
303 {
304         void (*_write_port_cancel)(struct intf_hdl *pintfhdl);
305         struct io_priv *pio_priv = &adapter->iopriv;
306         struct intf_hdl *pintfhdl = &(pio_priv->intf);
307
308         _write_port_cancel = pintfhdl->io_ops._write_port_cancel;
309
310         if (_write_port_cancel)
311                 _write_port_cancel(pintfhdl);
312 }
313
314 int rtw_init_io_priv(struct adapter *padapter, void (*set_intf_ops)(struct _io_ops *pops))
315 {
316         struct io_priv  *piopriv = &padapter->iopriv;
317         struct intf_hdl *pintf = &piopriv->intf;
318
319         if (set_intf_ops == NULL)
320                 return _FAIL;
321
322         piopriv->padapter = padapter;
323         pintf->padapter = padapter;
324         pintf->pintf_dev = adapter_to_dvobj(padapter);
325
326         set_intf_ops(&pintf->io_ops);
327
328         return _SUCCESS;
329 }