1 /******************************************************************************
3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
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.
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
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
19 ******************************************************************************/
22 The purpose of rtw_io.c
26 b. provides the protocol engine
28 c. provides the software interface between caller and the hardware interface
34 a. USE_SYNC_IRP: Only sync operations are provided.
35 b. USE_ASYNC_IRP:Both sync/async operations are provided.
38 a. USE_ASYNC_IRP: Both sync/async operations are provided.
41 b. USE_SYNC_IRP: Only sync operations are provided.
44 Only sync read/rtw_write_mem operations are provided.
46 jackson@realtek.com.tw
52 #include <osdep_service.h>
53 #include <drv_types.h>
55 #include <osdep_intf.h>
57 #if defined (PLATFORM_LINUX) && defined (PLATFORM_WINDOWS)
58 #error "Shall be Linux or Windows, but not both!\n"
61 #ifdef CONFIG_SDIO_HCI
74 u8 _rtw_read8(_adapter *adapter, u32 addr)
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);
82 _read8 = pintfhdl->io_ops._read8;
84 r_val = _read8(pintfhdl, addr);
89 u16 _rtw_read16(_adapter *adapter, u32 addr)
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);
97 _read16 = pintfhdl->io_ops._read16;
99 r_val = _read16(pintfhdl, addr);
104 u32 _rtw_read32(_adapter *adapter, u32 addr)
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);
112 _read32 = pintfhdl->io_ops._read32;
114 r_val = _read32(pintfhdl, addr);
120 int _rtw_write8(_adapter *adapter, u32 addr, u8 val)
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);
128 _write8 = pintfhdl->io_ops._write8;
130 ret = _write8(pintfhdl, addr, val);
133 return RTW_STATUS_CODE(ret);
135 int _rtw_write16(_adapter *adapter, u32 addr, u16 val)
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);
143 _write16 = pintfhdl->io_ops._write16;
145 ret = _write16(pintfhdl, addr, val);
148 return RTW_STATUS_CODE(ret);
150 int _rtw_write32(_adapter *adapter, u32 addr, u32 val)
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);
158 _write32 = pintfhdl->io_ops._write32;
160 ret = _write32(pintfhdl, addr, val);
163 return RTW_STATUS_CODE(ret);
166 int _rtw_writeN(_adapter *adapter, u32 addr ,u32 length , u8 *pdata)
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);
174 _writeN = pintfhdl->io_ops._writeN;
176 ret = _writeN(pintfhdl, addr,length,pdata);
179 return RTW_STATUS_CODE(ret);
181 int _rtw_write8_async(_adapter *adapter, u32 addr, u8 val)
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);
189 _write8_async = pintfhdl->io_ops._write8_async;
191 ret = _write8_async(pintfhdl, addr, val);
194 return RTW_STATUS_CODE(ret);
196 int _rtw_write16_async(_adapter *adapter, u32 addr, u16 val)
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);
204 _write16_async = pintfhdl->io_ops._write16_async;
206 ret = _write16_async(pintfhdl, addr, val);
209 return RTW_STATUS_CODE(ret);
211 int _rtw_write32_async(_adapter *adapter, u32 addr, u32 val)
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);
219 _write32_async = pintfhdl->io_ops._write32_async;
221 ret = _write32_async(pintfhdl, addr, val);
224 return RTW_STATUS_CODE(ret);
226 void _rtw_read_mem(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
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);
235 if( (adapter->bDriverStopped ==_TRUE) || (adapter->bSurpriseRemoved == _TRUE))
237 RT_TRACE(_module_rtl871x_io_c_, _drv_info_, ("rtw_read_mem:bDriverStopped(%d) OR bSurpriseRemoved(%d)", adapter->bDriverStopped, adapter->bSurpriseRemoved));
241 _read_mem = pintfhdl->io_ops._read_mem;
243 _read_mem(pintfhdl, addr, cnt, pmem);
249 void _rtw_write_mem(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
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);
258 _write_mem = pintfhdl->io_ops._write_mem;
260 _write_mem(pintfhdl, addr, cnt, pmem);
266 void _rtw_read_port(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
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);
275 if( (adapter->bDriverStopped ==_TRUE) || (adapter->bSurpriseRemoved == _TRUE))
277 RT_TRACE(_module_rtl871x_io_c_, _drv_info_, ("rtw_read_port:bDriverStopped(%d) OR bSurpriseRemoved(%d)", adapter->bDriverStopped, adapter->bSurpriseRemoved));
281 _read_port = pintfhdl->io_ops._read_port;
283 _read_port(pintfhdl, addr, cnt, pmem);
289 void _rtw_read_port_cancel(_adapter *adapter)
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);
295 _read_port_cancel = pintfhdl->io_ops._read_port_cancel;
297 if(_read_port_cancel)
298 _read_port_cancel(pintfhdl);
302 u32 _rtw_write_port(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
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);
312 _write_port = pintfhdl->io_ops._write_port;
314 ret = _write_port(pintfhdl, addr, cnt, pmem);
321 u32 _rtw_write_port_and_wait(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem, int timeout_ms)
324 struct xmit_buf *pxmitbuf = (struct xmit_buf *)pmem;
325 struct submit_ctx sctx;
327 rtw_sctx_init(&sctx, timeout_ms);
328 pxmitbuf->sctx = &sctx;
330 ret = _rtw_write_port(adapter, addr, cnt, pmem);
333 ret = rtw_sctx_wait(&sctx);
338 void _rtw_write_port_cancel(_adapter *adapter)
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);
344 _write_port_cancel = pintfhdl->io_ops._write_port_cancel;
346 if(_write_port_cancel)
347 _write_port_cancel(pintfhdl);
351 int rtw_init_io_priv(_adapter *padapter, void (*set_intf_ops)(struct _io_ops *pops))
353 struct io_priv *piopriv = &padapter->iopriv;
354 struct intf_hdl *pintf = &piopriv->intf;
356 if (set_intf_ops == NULL)
359 piopriv->padapter = padapter;
360 pintf->padapter = padapter;
361 pintf->pintf_dev = adapter_to_dvobj(padapter);
363 set_intf_ops(&pintf->io_ops);
370 u16 read_sniff_ranges[][2] = {
374 u16 write_sniff_ranges[][2] = {
379 int read_sniff_num = sizeof(read_sniff_ranges)/sizeof(u16)/2;
380 int write_sniff_num = sizeof(write_sniff_ranges)/sizeof(u16)/2;
382 bool match_read_sniff_ranges(u16 addr, u16 len)
385 for (i = 0; i<read_sniff_num; i++) {
386 if (addr + len > read_sniff_ranges[i][0] && addr <= read_sniff_ranges[i][1])
393 bool match_write_sniff_ranges(u16 addr, u16 len)
396 for (i = 0; i<write_sniff_num; i++) {
397 if (addr + len > write_sniff_ranges[i][0] && addr <= write_sniff_ranges[i][1])
404 u8 dbg_rtw_read8(_adapter *adapter, u32 addr, const char *caller, const int line)
406 u8 val = _rtw_read8(adapter, addr);
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);
414 u16 dbg_rtw_read16(_adapter *adapter, u32 addr, const char *caller, const int line)
416 u16 val = _rtw_read16(adapter, addr);
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);
424 u32 dbg_rtw_read32(_adapter *adapter, u32 addr, const char *caller, const int line)
426 u32 val = _rtw_read32(adapter, addr);
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);
434 int dbg_rtw_write8(_adapter *adapter, u32 addr, u8 val, const char *caller, const int line)
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);
439 return _rtw_write8(adapter, addr, val);
441 int dbg_rtw_write16(_adapter *adapter, u32 addr, u16 val, const char *caller, const int line)
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);
446 return _rtw_write16(adapter, addr, val);
448 int dbg_rtw_write32(_adapter *adapter, u32 addr, u32 val, const char *caller, const int line)
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);
453 return _rtw_write32(adapter, addr, val);
455 int dbg_rtw_writeN(_adapter *adapter, u32 addr ,u32 length , u8 *data, const char *caller, const int line)
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);
460 return _rtw_writeN(adapter, addr, length, data);