1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3 
4 /*
5 
6 The purpose of rtw_io.c
7 
8 a. provides the API
9 
10 b. provides the protocol engine
11 
12 c. provides the software interface between caller and the hardware interface
13 
14 Compiler Flag Option:
15 
16 USB:
17    a. USE_ASYNC_IRP: Both sync/async operations are provided.
18 
19 Only sync read/rtw_write_mem operations are provided.
20 
21 jackson@realtek.com.tw
22 
23 */
24 
25 #define _RTW_IO_C_
26 #include "../include/osdep_service.h"
27 #include "../include/drv_types.h"
28 #include "../include/rtw_io.h"
29 #include "../include/osdep_intf.h"
30 #include "../include/usb_ops.h"
31 
32 #define rtw_le16_to_cpu(val)		le16_to_cpu(val)
33 #define rtw_le32_to_cpu(val)		le32_to_cpu(val)
34 #define rtw_cpu_to_le16(val)		cpu_to_le16(val)
35 #define rtw_cpu_to_le32(val)		cpu_to_le32(val)
36 
_rtw_read8(struct adapter * adapter,u32 addr)37 u8 _rtw_read8(struct adapter *adapter, u32 addr)
38 {
39 	u8 r_val;
40 	struct io_priv *pio_priv = &adapter->iopriv;
41 	struct	intf_hdl *pintfhdl = &pio_priv->intf;
42 	u8 (*_read8)(struct intf_hdl *pintfhdl, u32 addr);
43 
44 
45 	_read8 = pintfhdl->io_ops._read8;
46 	r_val = _read8(pintfhdl, addr);
47 
48 	return r_val;
49 }
50 
_rtw_read16(struct adapter * adapter,u32 addr)51 u16 _rtw_read16(struct adapter *adapter, u32 addr)
52 {
53 	u16 r_val;
54 	struct io_priv *pio_priv = &adapter->iopriv;
55 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
56 	u16 (*_read16)(struct intf_hdl *pintfhdl, u32 addr);
57 
58 	_read16 = pintfhdl->io_ops._read16;
59 
60 	r_val = _read16(pintfhdl, addr);
61 
62 	return r_val;
63 }
64 
_rtw_read32(struct adapter * adapter,u32 addr)65 u32 _rtw_read32(struct adapter *adapter, u32 addr)
66 {
67 	u32 r_val;
68 	struct io_priv *pio_priv = &adapter->iopriv;
69 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
70 	u32	(*_read32)(struct intf_hdl *pintfhdl, u32 addr);
71 
72 	_read32 = pintfhdl->io_ops._read32;
73 
74 	r_val = _read32(pintfhdl, addr);
75 
76 	return r_val;
77 }
78 
_rtw_write8(struct adapter * adapter,u32 addr,u8 val)79 int _rtw_write8(struct adapter *adapter, u32 addr, u8 val)
80 {
81 	struct io_priv *pio_priv = &adapter->iopriv;
82 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
83 	int (*_write8)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
84 	int ret;
85 
86 	_write8 = pintfhdl->io_ops._write8;
87 
88 	ret = _write8(pintfhdl, addr, val);
89 
90 
91 	return RTW_STATUS_CODE(ret);
92 }
93 
_rtw_write16(struct adapter * adapter,u32 addr,u16 val)94 int _rtw_write16(struct adapter *adapter, u32 addr, u16 val)
95 {
96 	struct io_priv *pio_priv = &adapter->iopriv;
97 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
98 	int (*_write16)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
99 	int ret;
100 
101 	_write16 = pintfhdl->io_ops._write16;
102 
103 	ret = _write16(pintfhdl, addr, val);
104 
105 
106 	return RTW_STATUS_CODE(ret);
107 }
_rtw_write32(struct adapter * adapter,u32 addr,u32 val)108 int _rtw_write32(struct adapter *adapter, u32 addr, u32 val)
109 {
110 	struct io_priv *pio_priv = &adapter->iopriv;
111 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
112 	int (*_write32)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
113 	int ret;
114 
115 	_write32 = pintfhdl->io_ops._write32;
116 
117 	ret = _write32(pintfhdl, addr, val);
118 
119 
120 	return RTW_STATUS_CODE(ret);
121 }
122 
_rtw_writeN(struct adapter * adapter,u32 addr,u32 length,u8 * pdata)123 int _rtw_writeN(struct adapter *adapter, u32 addr, u32 length, u8 *pdata)
124 {
125 	struct io_priv *pio_priv = &adapter->iopriv;
126 	struct	intf_hdl *pintfhdl = (struct intf_hdl *)(&pio_priv->intf);
127 	int (*_writeN)(struct intf_hdl *pintfhdl, u32 addr, u32 length, u8 *pdata);
128 	int ret;
129 
130 	_writeN = pintfhdl->io_ops._writeN;
131 
132 	ret = _writeN(pintfhdl, addr, length, pdata);
133 
134 
135 	return RTW_STATUS_CODE(ret);
136 }
_rtw_write8_async(struct adapter * adapter,u32 addr,u8 val)137 int _rtw_write8_async(struct adapter *adapter, u32 addr, u8 val)
138 {
139 	struct io_priv *pio_priv = &adapter->iopriv;
140 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
141 	int (*_write8_async)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
142 	int ret;
143 
144 	_write8_async = pintfhdl->io_ops._write8_async;
145 
146 	ret = _write8_async(pintfhdl, addr, val);
147 
148 
149 	return RTW_STATUS_CODE(ret);
150 }
151 
_rtw_write16_async(struct adapter * adapter,u32 addr,u16 val)152 int _rtw_write16_async(struct adapter *adapter, u32 addr, u16 val)
153 {
154 	struct io_priv *pio_priv = &adapter->iopriv;
155 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
156 	int (*_write16_async)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
157 	int ret;
158 
159 	_write16_async = pintfhdl->io_ops._write16_async;
160 	ret = _write16_async(pintfhdl, addr, val);
161 
162 	return RTW_STATUS_CODE(ret);
163 }
164 
_rtw_write32_async(struct adapter * adapter,u32 addr,u32 val)165 int _rtw_write32_async(struct adapter *adapter, u32 addr, u32 val)
166 {
167 	struct io_priv *pio_priv = &adapter->iopriv;
168 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
169 	int (*_write32_async)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
170 	int ret;
171 
172 	_write32_async = pintfhdl->io_ops._write32_async;
173 	ret = _write32_async(pintfhdl, addr, val);
174 
175 	return RTW_STATUS_CODE(ret);
176 }
177 
_rtw_read_mem(struct adapter * adapter,u32 addr,u32 cnt,u8 * pmem)178 void _rtw_read_mem(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
179 {
180 	void (*_read_mem)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
181 	struct io_priv *pio_priv = &adapter->iopriv;
182 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
183 
184 
185 	if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
186 	     return;
187 	_read_mem = pintfhdl->io_ops._read_mem;
188 	_read_mem(pintfhdl, addr, cnt, pmem);
189 
190 }
191 
_rtw_write_mem(struct adapter * adapter,u32 addr,u32 cnt,u8 * pmem)192 void _rtw_write_mem(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
193 {
194 	void (*_write_mem)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
195 	struct io_priv *pio_priv = &adapter->iopriv;
196 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
197 
198 
199 
200 	_write_mem = pintfhdl->io_ops._write_mem;
201 
202 	_write_mem(pintfhdl, addr, cnt, pmem);
203 
204 
205 }
206 
_rtw_read_port(struct adapter * adapter,u32 addr,u32 cnt,u8 * pmem)207 void _rtw_read_port(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
208 {
209 	u32 (*_read_port)(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;
212 
213 
214 
215 	if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
216 	     return;
217 
218 	_read_port = pintfhdl->io_ops._read_port;
219 
220 	_read_port(pintfhdl, addr, cnt, pmem);
221 
222 
223 }
224 
_rtw_read_port_cancel(struct adapter * adapter)225 void _rtw_read_port_cancel(struct adapter *adapter)
226 {
227 	void (*_read_port_cancel)(struct intf_hdl *pintfhdl);
228 	struct io_priv *pio_priv = &adapter->iopriv;
229 	struct intf_hdl *pintfhdl = &pio_priv->intf;
230 
231 	_read_port_cancel = pintfhdl->io_ops._read_port_cancel;
232 
233 	if (_read_port_cancel)
234 		_read_port_cancel(pintfhdl);
235 }
236 
_rtw_write_port(struct adapter * adapter,u32 addr,u32 cnt,u8 * pmem)237 u32 _rtw_write_port(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
238 {
239 	u32 (*_write_port)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
240 	struct io_priv *pio_priv = &adapter->iopriv;
241 	struct	intf_hdl		*pintfhdl = &pio_priv->intf;
242 	u32 ret = _SUCCESS;
243 
244 
245 
246 	_write_port = pintfhdl->io_ops._write_port;
247 
248 	ret = _write_port(pintfhdl, addr, cnt, pmem);
249 
250 
251 
252 	return ret;
253 }
254 
_rtw_write_port_and_wait(struct adapter * adapter,u32 addr,u32 cnt,u8 * pmem,int timeout_ms)255 u32 _rtw_write_port_and_wait(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem, int timeout_ms)
256 {
257 	int ret = _SUCCESS;
258 	struct xmit_buf *pxmitbuf = (struct xmit_buf *)pmem;
259 	struct submit_ctx sctx;
260 
261 	rtw_sctx_init(&sctx, timeout_ms);
262 	pxmitbuf->sctx = &sctx;
263 
264 	ret = _rtw_write_port(adapter, addr, cnt, pmem);
265 
266 	if (ret == _SUCCESS)
267 		ret = rtw_sctx_wait(&sctx);
268 
269 	return ret;
270 }
271 
_rtw_write_port_cancel(struct adapter * adapter)272 void _rtw_write_port_cancel(struct adapter *adapter)
273 {
274 	void (*_write_port_cancel)(struct intf_hdl *pintfhdl);
275 	struct io_priv *pio_priv = &adapter->iopriv;
276 	struct intf_hdl *pintfhdl = &pio_priv->intf;
277 
278 	_write_port_cancel = pintfhdl->io_ops._write_port_cancel;
279 
280 	if (_write_port_cancel)
281 		_write_port_cancel(pintfhdl);
282 }
283 
rtw_init_io_priv(struct adapter * padapter,void (* set_intf_ops)(struct _io_ops * pops))284 int rtw_init_io_priv(struct adapter *padapter, void (*set_intf_ops)(struct _io_ops *pops))
285 {
286 	struct io_priv	*piopriv = &padapter->iopriv;
287 	struct intf_hdl *pintf = &piopriv->intf;
288 
289 	if (!set_intf_ops)
290 		return _FAIL;
291 
292 	piopriv->padapter = padapter;
293 	pintf->padapter = padapter;
294 	pintf->pintf_dev = adapter_to_dvobj(padapter);
295 
296 	set_intf_ops(&pintf->io_ops);
297 
298 	return _SUCCESS;
299 }
300