wifi: renew patch drivers/net/wireless
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rtl8192cu / 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 1. CONFIG_SDIO_HCI:
34     a. USE_SYNC_IRP:  Only sync operations are provided.
35     b. USE_ASYNC_IRP:Both sync/async operations are provided.
36
37 2. CONFIG_USB_HCI:
38    a. USE_ASYNC_IRP: Both sync/async operations are provided.
39
40 3. CONFIG_CFIO_HCI:
41    b. USE_SYNC_IRP: Only sync operations are provided.
42
43
44 Only sync read/rtw_write_mem operations are provided.
45
46 jackson@realtek.com.tw
47
48 */
49
50 #define _RTW_IO_C_
51 #include <drv_conf.h>
52 #include <osdep_service.h>
53 #include <drv_types.h>
54 #include <rtw_io.h>
55 #include <osdep_intf.h>
56
57 #if defined (PLATFORM_LINUX) && defined (PLATFORM_WINDOWS)
58 #error "Shall be Linux or Windows, but not both!\n"
59 #endif
60
61 #ifdef CONFIG_SDIO_HCI
62 #include <sdio_ops.h>
63 #endif
64
65 #ifdef CONFIG_USB_HCI
66 #include <usb_ops.h>
67 #endif
68
69 #ifdef CONFIG_PCI_HCI
70 #include <pci_ops.h>
71 #endif
72
73
74 u8 _rtw_read8(_adapter *adapter, u32 addr)
75 {
76         u8 r_val;
77         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
78         struct io_priv *pio_priv = &adapter->iopriv;
79         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
80         u8 (*_read8)(struct intf_hdl *pintfhdl, u32 addr);
81         _func_enter_;
82         _read8 = pintfhdl->io_ops._read8;
83
84         r_val = _read8(pintfhdl, addr);
85         _func_exit_;
86         return r_val;
87 }
88
89 u16 _rtw_read16(_adapter *adapter, u32 addr)
90 {
91         u16 r_val;
92         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
93         struct io_priv *pio_priv = &adapter->iopriv;
94         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
95         u16     (*_read16)(struct intf_hdl *pintfhdl, u32 addr);
96         _func_enter_;
97         _read16 = pintfhdl->io_ops._read16;
98
99         r_val = _read16(pintfhdl, addr);
100         _func_exit_;
101         return r_val;
102 }
103
104 u32 _rtw_read32(_adapter *adapter, u32 addr)
105 {
106         u32 r_val;
107         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
108         struct io_priv *pio_priv = &adapter->iopriv;
109         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
110         u32     (*_read32)(struct intf_hdl *pintfhdl, u32 addr);
111         _func_enter_;
112         _read32 = pintfhdl->io_ops._read32;
113
114         r_val = _read32(pintfhdl, addr);
115         _func_exit_;
116         return r_val;
117
118 }
119
120 int _rtw_write8(_adapter *adapter, u32 addr, u8 val)
121 {
122         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
123         struct io_priv *pio_priv = &adapter->iopriv;
124         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
125         int (*_write8)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
126         int ret;
127         _func_enter_;
128         _write8 = pintfhdl->io_ops._write8;
129
130         ret = _write8(pintfhdl, addr, val);
131         _func_exit_;
132         
133         return RTW_STATUS_CODE(ret);
134 }
135 int _rtw_write16(_adapter *adapter, u32 addr, u16 val)
136 {
137         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
138         struct io_priv *pio_priv = &adapter->iopriv;
139         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
140         int (*_write16)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
141         int ret;
142         _func_enter_;
143         _write16 = pintfhdl->io_ops._write16;
144
145         ret = _write16(pintfhdl, addr, val);
146         _func_exit_;
147
148         return RTW_STATUS_CODE(ret);
149 }
150 int _rtw_write32(_adapter *adapter, u32 addr, u32 val)
151 {
152         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
153         struct io_priv *pio_priv = &adapter->iopriv;
154         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
155         int (*_write32)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
156         int ret;
157         _func_enter_;
158         _write32 = pintfhdl->io_ops._write32;
159
160         ret = _write32(pintfhdl, addr, val);
161         _func_exit_;
162
163         return RTW_STATUS_CODE(ret);
164 }
165
166 int _rtw_writeN(_adapter *adapter, u32 addr ,u32 length , u8 *pdata)
167 {
168         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
169         struct io_priv *pio_priv = &adapter->iopriv;
170         struct  intf_hdl        *pintfhdl = (struct intf_hdl*)(&(pio_priv->intf));
171         int (*_writeN)(struct intf_hdl *pintfhdl, u32 addr,u32 length, u8 *pdata);
172         int ret;
173         _func_enter_;
174         _writeN = pintfhdl->io_ops._writeN;
175
176         ret = _writeN(pintfhdl, addr,length,pdata);
177         _func_exit_;
178
179         return RTW_STATUS_CODE(ret);
180 }
181 int _rtw_write8_async(_adapter *adapter, u32 addr, u8 val)
182 {
183         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
184         struct io_priv *pio_priv = &adapter->iopriv;
185         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
186         int (*_write8_async)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
187         int ret;
188         _func_enter_;
189         _write8_async = pintfhdl->io_ops._write8_async;
190
191         ret = _write8_async(pintfhdl, addr, val);
192         _func_exit_;
193
194         return RTW_STATUS_CODE(ret);
195 }
196 int _rtw_write16_async(_adapter *adapter, u32 addr, u16 val)
197 {
198         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
199         struct io_priv *pio_priv = &adapter->iopriv;
200         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
201         int (*_write16_async)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
202         int ret;
203         _func_enter_;
204         _write16_async = pintfhdl->io_ops._write16_async;
205
206         ret = _write16_async(pintfhdl, addr, val);
207         _func_exit_;
208
209         return RTW_STATUS_CODE(ret);
210 }
211 int _rtw_write32_async(_adapter *adapter, u32 addr, u32 val)
212 {
213         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
214         struct io_priv *pio_priv = &adapter->iopriv;
215         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
216         int (*_write32_async)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
217         int ret;
218         _func_enter_;
219         _write32_async = pintfhdl->io_ops._write32_async;
220
221         ret = _write32_async(pintfhdl, addr, val);
222         _func_exit_;
223
224         return RTW_STATUS_CODE(ret);
225 }
226 void _rtw_read_mem(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
227 {
228         void (*_read_mem)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
229         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
230         struct io_priv *pio_priv = &adapter->iopriv;
231         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
232
233         _func_enter_;
234
235         if( (adapter->bDriverStopped ==_TRUE) || (adapter->bSurpriseRemoved == _TRUE))
236         {
237              RT_TRACE(_module_rtl871x_io_c_, _drv_info_, ("rtw_read_mem:bDriverStopped(%d) OR bSurpriseRemoved(%d)", adapter->bDriverStopped, adapter->bSurpriseRemoved));          
238              return;
239         }
240
241         _read_mem = pintfhdl->io_ops._read_mem;
242
243         _read_mem(pintfhdl, addr, cnt, pmem);
244
245         _func_exit_;
246
247 }
248
249 void _rtw_write_mem(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
250 {
251         void (*_write_mem)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
252         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
253         struct io_priv *pio_priv = &adapter->iopriv;
254         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
255
256         _func_enter_;
257
258         _write_mem = pintfhdl->io_ops._write_mem;
259
260         _write_mem(pintfhdl, addr, cnt, pmem);
261
262         _func_exit_;
263
264 }
265
266 void _rtw_read_port(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
267 {
268         u32 (*_read_port)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
269         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
270         struct io_priv *pio_priv = &adapter->iopriv;
271         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
272
273         _func_enter_;
274
275         if( (adapter->bDriverStopped ==_TRUE) || (adapter->bSurpriseRemoved == _TRUE))
276         {
277              RT_TRACE(_module_rtl871x_io_c_, _drv_info_, ("rtw_read_port:bDriverStopped(%d) OR bSurpriseRemoved(%d)", adapter->bDriverStopped, adapter->bSurpriseRemoved));         
278              return;
279         }
280
281         _read_port = pintfhdl->io_ops._read_port;
282
283         _read_port(pintfhdl, addr, cnt, pmem);
284
285         _func_exit_;
286
287 }
288
289 void _rtw_read_port_cancel(_adapter *adapter)
290 {
291         void (*_read_port_cancel)(struct intf_hdl *pintfhdl);
292         struct io_priv *pio_priv = &adapter->iopriv;
293         struct intf_hdl *pintfhdl = &(pio_priv->intf);
294
295         _read_port_cancel = pintfhdl->io_ops._read_port_cancel;
296
297         if(_read_port_cancel)
298                 _read_port_cancel(pintfhdl);
299
300 }
301
302 u32 _rtw_write_port(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
303 {
304         u32 (*_write_port)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
305         //struct        io_queue        *pio_queue = (struct io_queue *)adapter->pio_queue;
306         struct io_priv *pio_priv = &adapter->iopriv;
307         struct  intf_hdl                *pintfhdl = &(pio_priv->intf);
308         u32 ret = _SUCCESS;
309
310         _func_enter_;
311
312         _write_port = pintfhdl->io_ops._write_port;
313         
314         ret = _write_port(pintfhdl, addr, cnt, pmem);
315
316          _func_exit_;
317
318         return ret;
319 }
320
321 u32 _rtw_write_port_and_wait(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem, int timeout_ms)
322 {
323         int ret = _SUCCESS;
324         struct xmit_buf *pxmitbuf = (struct xmit_buf *)pmem;
325         struct submit_ctx sctx;
326
327         rtw_sctx_init(&sctx, timeout_ms);
328         pxmitbuf->sctx = &sctx;
329
330         ret = _rtw_write_port(adapter, addr, cnt, pmem);
331
332         if (ret == _SUCCESS)
333                 ret = rtw_sctx_wait(&sctx);
334
335          return ret;
336 }
337
338 void _rtw_write_port_cancel(_adapter *adapter)
339 {
340         void (*_write_port_cancel)(struct intf_hdl *pintfhdl);
341         struct io_priv *pio_priv = &adapter->iopriv;
342         struct intf_hdl *pintfhdl = &(pio_priv->intf);
343
344         _write_port_cancel = pintfhdl->io_ops._write_port_cancel;
345
346         if(_write_port_cancel)
347                 _write_port_cancel(pintfhdl);
348
349 }
350
351 int rtw_init_io_priv(_adapter *padapter, void (*set_intf_ops)(struct _io_ops *pops))
352 {
353         struct io_priv  *piopriv = &padapter->iopriv;
354         struct intf_hdl *pintf = &piopriv->intf;
355
356         if (set_intf_ops == NULL)
357                 return _FAIL;
358
359         piopriv->padapter = padapter;
360         pintf->padapter = padapter;
361         pintf->pintf_dev = adapter_to_dvobj(padapter);
362
363         set_intf_ops(&pintf->io_ops);
364
365         return _SUCCESS;
366 }
367
368 #ifdef DBG_IO
369
370 u16 read_sniff_ranges[][2] = {
371         //{0x550, 0x551},
372 }; 
373
374 u16 write_sniff_ranges[][2] = {
375         //{0x550, 0x551},
376         //{0x4c, 0x4c},
377 }; 
378
379 int read_sniff_num = sizeof(read_sniff_ranges)/sizeof(u16)/2;
380 int write_sniff_num = sizeof(write_sniff_ranges)/sizeof(u16)/2;
381
382 bool match_read_sniff_ranges(u16 addr, u16 len)
383 {
384         int i;
385         for (i = 0; i<read_sniff_num; i++) {
386                 if (addr + len > read_sniff_ranges[i][0] && addr <= read_sniff_ranges[i][1])
387                         return _TRUE;
388         }
389         
390         return _FALSE;
391 }
392
393 bool match_write_sniff_ranges(u16 addr, u16 len)
394 {
395         int i;
396         for (i = 0; i<write_sniff_num; i++) {
397                 if (addr + len > write_sniff_ranges[i][0] && addr <= write_sniff_ranges[i][1])
398                         return _TRUE;
399         }
400         
401         return _FALSE;
402 }
403
404 u8 dbg_rtw_read8(_adapter *adapter, u32 addr, const char *caller, const int line)
405 {
406         u8 val = _rtw_read8(adapter, addr);
407
408         if (match_read_sniff_ranges(addr, 1))
409                 DBG_871X("DBG_IO %s:%d rtw_read8(0x%04x) return 0x%02x\n", caller, line, addr, val);
410
411         return val;
412 }
413
414 u16 dbg_rtw_read16(_adapter *adapter, u32 addr, const char *caller, const int line)
415 {
416         u16 val = _rtw_read16(adapter, addr);
417         
418         if (match_read_sniff_ranges(addr, 2))
419                 DBG_871X("DBG_IO %s:%d rtw_read16(0x%04x) return 0x%04x\n", caller, line, addr, val);
420
421         return val;
422 }
423
424 u32 dbg_rtw_read32(_adapter *adapter, u32 addr, const char *caller, const int line)
425 {
426         u32 val = _rtw_read32(adapter, addr);
427         
428         if (match_read_sniff_ranges(addr, 4))
429                 DBG_871X("DBG_IO %s:%d rtw_read32(0x%04x) return 0x%08x\n", caller, line, addr, val);
430
431         return val;
432 }
433
434 int dbg_rtw_write8(_adapter *adapter, u32 addr, u8 val, const char *caller, const int line)
435 {
436         if (match_write_sniff_ranges(addr, 1))
437                 DBG_871X("DBG_IO %s:%d rtw_write8(0x%04x, 0x%02x)\n", caller, line, addr, val);
438         
439         return _rtw_write8(adapter, addr, val);
440 }
441 int dbg_rtw_write16(_adapter *adapter, u32 addr, u16 val, const char *caller, const int line)
442 {
443         if (match_write_sniff_ranges(addr, 2))
444                 DBG_871X("DBG_IO %s:%d rtw_write16(0x%04x, 0x%04x)\n", caller, line, addr, val);
445         
446         return _rtw_write16(adapter, addr, val);
447 }
448 int dbg_rtw_write32(_adapter *adapter, u32 addr, u32 val, const char *caller, const int line)
449 {
450         if (match_write_sniff_ranges(addr, 4))
451                 DBG_871X("DBG_IO %s:%d rtw_write32(0x%04x, 0x%08x)\n", caller, line, addr, val);
452         
453         return _rtw_write32(adapter, addr, val);
454 }
455 int dbg_rtw_writeN(_adapter *adapter, u32 addr ,u32 length , u8 *data, const char *caller, const int line)
456 {
457         if (match_write_sniff_ranges(addr, length))
458                 DBG_871X("DBG_IO %s:%d rtw_writeN(0x%04x, %u)\n", caller, line, addr, length);
459
460         return _rtw_writeN(adapter, addr, length, data);
461 }
462 #endif
463
464