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
33 a. USE_ASYNC_IRP: Both sync/async operations are provided.
36 b. USE_SYNC_IRP: Only sync operations are provided.
38 Only sync read/rtw_write_mem operations are provided.
40 jackson@realtek.com.tw
46 #include <osdep_service.h>
47 #include <drv_types.h>
49 #include <osdep_intf.h>
51 #ifdef CONFIG_GSPI_HCI
59 #define rtw_le16_to_cpu(val) le16_to_cpu(val)
60 #define rtw_le32_to_cpu(val) le32_to_cpu(val)
61 #define rtw_cpu_to_le16(val) cpu_to_le16(val)
62 #define rtw_cpu_to_le32(val) cpu_to_le32(val)
63 u8 _rtw_read8(_adapter *adapter, u32 addr)
66 struct io_priv *pio_priv = &adapter->iopriv;
67 struct intf_hdl *pintfhdl = &(pio_priv->intf);
68 u8 (*_read8)(struct intf_hdl *pintfhdl, u32 addr);
70 _read8 = pintfhdl->io_ops._read8;
72 r_val = _read8(pintfhdl, addr);
77 u16 _rtw_read16(_adapter *adapter, u32 addr)
80 struct io_priv *pio_priv = &adapter->iopriv;
81 struct intf_hdl *pintfhdl = &(pio_priv->intf);
82 u16 (*_read16)(struct intf_hdl *pintfhdl, u32 addr);
84 _read16 = pintfhdl->io_ops._read16;
86 r_val = _read16(pintfhdl, addr);
88 return rtw_le16_to_cpu(r_val);
91 u32 _rtw_read32(_adapter *adapter, u32 addr)
94 struct io_priv *pio_priv = &adapter->iopriv;
95 struct intf_hdl *pintfhdl = &(pio_priv->intf);
96 u32 (*_read32)(struct intf_hdl *pintfhdl, u32 addr);
98 _read32 = pintfhdl->io_ops._read32;
100 r_val = _read32(pintfhdl, addr);
102 return rtw_le32_to_cpu(r_val);
105 int _rtw_write8(_adapter *adapter, u32 addr, u8 val)
107 struct io_priv *pio_priv = &adapter->iopriv;
108 struct intf_hdl *pintfhdl = &(pio_priv->intf);
109 int (*_write8)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
112 _write8 = pintfhdl->io_ops._write8;
114 ret = _write8(pintfhdl, addr, val);
117 return RTW_STATUS_CODE(ret);
119 int _rtw_write16(_adapter *adapter, u32 addr, u16 val)
121 struct io_priv *pio_priv = &adapter->iopriv;
122 struct intf_hdl *pintfhdl = &(pio_priv->intf);
123 int (*_write16)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
126 _write16 = pintfhdl->io_ops._write16;
128 val = rtw_cpu_to_le16(val);
129 ret = _write16(pintfhdl, addr, val);
132 return RTW_STATUS_CODE(ret);
134 int _rtw_write32(_adapter *adapter, u32 addr, u32 val)
136 struct io_priv *pio_priv = &adapter->iopriv;
137 struct intf_hdl *pintfhdl = &(pio_priv->intf);
138 int (*_write32)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
141 _write32 = pintfhdl->io_ops._write32;
143 val = rtw_cpu_to_le32(val);
144 ret = _write32(pintfhdl, addr, val);
147 return RTW_STATUS_CODE(ret);
150 int _rtw_writeN(_adapter *adapter, u32 addr ,u32 length , u8 *pdata)
152 struct io_priv *pio_priv = &adapter->iopriv;
153 struct intf_hdl *pintfhdl = (struct intf_hdl*)(&(pio_priv->intf));
154 int (*_writeN)(struct intf_hdl *pintfhdl, u32 addr,u32 length, u8 *pdata);
157 _writeN = pintfhdl->io_ops._writeN;
159 ret = _writeN(pintfhdl, addr,length,pdata);
162 return RTW_STATUS_CODE(ret);
164 int _rtw_write8_async(_adapter *adapter, u32 addr, u8 val)
166 struct io_priv *pio_priv = &adapter->iopriv;
167 struct intf_hdl *pintfhdl = &(pio_priv->intf);
168 int (*_write8_async)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
171 _write8_async = pintfhdl->io_ops._write8_async;
173 ret = _write8_async(pintfhdl, addr, val);
176 return RTW_STATUS_CODE(ret);
178 int _rtw_write16_async(_adapter *adapter, u32 addr, u16 val)
180 struct io_priv *pio_priv = &adapter->iopriv;
181 struct intf_hdl *pintfhdl = &(pio_priv->intf);
182 int (*_write16_async)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
185 _write16_async = pintfhdl->io_ops._write16_async;
186 val = rtw_cpu_to_le16(val);
187 ret = _write16_async(pintfhdl, addr, val);
190 return RTW_STATUS_CODE(ret);
192 int _rtw_write32_async(_adapter *adapter, u32 addr, u32 val)
194 struct io_priv *pio_priv = &adapter->iopriv;
195 struct intf_hdl *pintfhdl = &(pio_priv->intf);
196 int (*_write32_async)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
199 _write32_async = pintfhdl->io_ops._write32_async;
200 val = rtw_cpu_to_le32(val);
201 ret = _write32_async(pintfhdl, addr, val);
204 return RTW_STATUS_CODE(ret);
207 void _rtw_read_mem(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
209 void (*_read_mem)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
210 struct io_priv *pio_priv = &adapter->iopriv;
211 struct intf_hdl *pintfhdl = &(pio_priv->intf);
215 if( (adapter->bDriverStopped ==_TRUE) || (adapter->bSurpriseRemoved == _TRUE))
217 RT_TRACE(_module_rtl871x_io_c_, _drv_info_, ("rtw_read_mem:bDriverStopped(%d) OR bSurpriseRemoved(%d)", adapter->bDriverStopped, adapter->bSurpriseRemoved));
221 _read_mem = pintfhdl->io_ops._read_mem;
223 _read_mem(pintfhdl, addr, cnt, pmem);
228 void _rtw_write_mem(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
230 void (*_write_mem)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
231 struct io_priv *pio_priv = &adapter->iopriv;
232 struct intf_hdl *pintfhdl = &(pio_priv->intf);
236 _write_mem = pintfhdl->io_ops._write_mem;
238 _write_mem(pintfhdl, addr, cnt, pmem);
243 void _rtw_read_port(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
245 u32 (*_read_port)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
246 struct io_priv *pio_priv = &adapter->iopriv;
247 struct intf_hdl *pintfhdl = &(pio_priv->intf);
251 if( (adapter->bDriverStopped ==_TRUE) || (adapter->bSurpriseRemoved == _TRUE))
253 RT_TRACE(_module_rtl871x_io_c_, _drv_info_, ("rtw_read_port:bDriverStopped(%d) OR bSurpriseRemoved(%d)", adapter->bDriverStopped, adapter->bSurpriseRemoved));
257 _read_port = pintfhdl->io_ops._read_port;
259 _read_port(pintfhdl, addr, cnt, pmem);
264 void _rtw_read_port_cancel(_adapter *adapter)
266 void (*_read_port_cancel)(struct intf_hdl *pintfhdl);
267 struct io_priv *pio_priv = &adapter->iopriv;
268 struct intf_hdl *pintfhdl = &(pio_priv->intf);
270 _read_port_cancel = pintfhdl->io_ops._read_port_cancel;
272 if(_read_port_cancel)
273 _read_port_cancel(pintfhdl);
276 u32 _rtw_write_port(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
278 u32 (*_write_port)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
279 struct io_priv *pio_priv = &adapter->iopriv;
280 struct intf_hdl *pintfhdl = &(pio_priv->intf);
285 _write_port = pintfhdl->io_ops._write_port;
287 ret = _write_port(pintfhdl, addr, cnt, pmem);
294 u32 _rtw_write_port_and_wait(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem, int timeout_ms)
297 struct xmit_buf *pxmitbuf = (struct xmit_buf *)pmem;
298 struct submit_ctx sctx;
300 rtw_sctx_init(&sctx, timeout_ms);
301 pxmitbuf->sctx = &sctx;
303 ret = _rtw_write_port(adapter, addr, cnt, pmem);
306 ret = rtw_sctx_wait(&sctx);
311 void _rtw_write_port_cancel(_adapter *adapter)
313 void (*_write_port_cancel)(struct intf_hdl *pintfhdl);
314 struct io_priv *pio_priv = &adapter->iopriv;
315 struct intf_hdl *pintfhdl = &(pio_priv->intf);
317 _write_port_cancel = pintfhdl->io_ops._write_port_cancel;
319 if(_write_port_cancel)
320 _write_port_cancel(pintfhdl);
323 int rtw_init_io_priv(_adapter *padapter, void (*set_intf_ops)(struct _io_ops *pops))
325 struct io_priv *piopriv = &padapter->iopriv;
326 struct intf_hdl *pintf = &piopriv->intf;
328 if (set_intf_ops == NULL)
331 piopriv->padapter = padapter;
332 pintf->padapter = padapter;
333 pintf->pintf_dev = adapter_to_dvobj(padapter);
335 set_intf_ops(&pintf->io_ops);
342 u16 read_sniff_ranges[][2] = {
346 u16 write_sniff_ranges[][2] = {
351 int read_sniff_num = sizeof(read_sniff_ranges)/sizeof(u16)/2;
352 int write_sniff_num = sizeof(write_sniff_ranges)/sizeof(u16)/2;
354 bool match_read_sniff_ranges(u16 addr, u16 len)
357 for (i = 0; i<read_sniff_num; i++) {
358 if (addr + len > read_sniff_ranges[i][0] && addr <= read_sniff_ranges[i][1])
365 bool match_write_sniff_ranges(u16 addr, u16 len)
368 for (i = 0; i<write_sniff_num; i++) {
369 if (addr + len > write_sniff_ranges[i][0] && addr <= write_sniff_ranges[i][1])
376 u8 dbg_rtw_read8(_adapter *adapter, u32 addr, const char *caller, const int line)
378 u8 val = _rtw_read8(adapter, addr);
380 if (match_read_sniff_ranges(addr, 1))
381 DBG_8723A("DBG_IO %s:%d rtw_read8(0x%04x) return 0x%02x\n", caller, line, addr, val);
386 u16 dbg_rtw_read16(_adapter *adapter, u32 addr, const char *caller, const int line)
388 u16 val = _rtw_read16(adapter, addr);
390 if (match_read_sniff_ranges(addr, 2))
391 DBG_8723A("DBG_IO %s:%d rtw_read16(0x%04x) return 0x%04x\n", caller, line, addr, val);
396 u32 dbg_rtw_read32(_adapter *adapter, u32 addr, const char *caller, const int line)
398 u32 val = _rtw_read32(adapter, addr);
400 if (match_read_sniff_ranges(addr, 4))
401 DBG_8723A("DBG_IO %s:%d rtw_read32(0x%04x) return 0x%08x\n", caller, line, addr, val);
406 int dbg_rtw_write8(_adapter *adapter, u32 addr, u8 val, const char *caller, const int line)
408 if (match_write_sniff_ranges(addr, 1))
409 DBG_8723A("DBG_IO %s:%d rtw_write8(0x%04x, 0x%02x)\n", caller, line, addr, val);
411 return _rtw_write8(adapter, addr, val);
413 int dbg_rtw_write16(_adapter *adapter, u32 addr, u16 val, const char *caller, const int line)
415 if (match_write_sniff_ranges(addr, 2))
416 DBG_8723A("DBG_IO %s:%d rtw_write16(0x%04x, 0x%04x)\n", caller, line, addr, val);
418 return _rtw_write16(adapter, addr, val);
420 int dbg_rtw_write32(_adapter *adapter, u32 addr, u32 val, const char *caller, const int line)
422 if (match_write_sniff_ranges(addr, 4))
423 DBG_8723A("DBG_IO %s:%d rtw_write32(0x%04x, 0x%08x)\n", caller, line, addr, val);
425 return _rtw_write32(adapter, addr, val);
427 int dbg_rtw_writeN(_adapter *adapter, u32 addr ,u32 length , u8 *data, const char *caller, const int line)
429 if (match_write_sniff_ranges(addr, length))
430 DBG_8723A("DBG_IO %s:%d rtw_writeN(0x%04x, %u)\n", caller, line, addr, length);
432 return _rtw_writeN(adapter, addr, length, data);