xref: /linux/drivers/staging/rtl8723bs/hal/hal_intf.c (revision 211b4ed0)
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #include <drv_types.h>
8 #include <hal_data.h>
9 
rtw_hal_chip_configure(struct adapter * padapter)10 void rtw_hal_chip_configure(struct adapter *padapter)
11 {
12 	if (padapter->HalFunc.intf_chip_configure)
13 		padapter->HalFunc.intf_chip_configure(padapter);
14 }
15 
rtw_hal_read_chip_info(struct adapter * padapter)16 void rtw_hal_read_chip_info(struct adapter *padapter)
17 {
18 	if (padapter->HalFunc.read_adapter_info)
19 		padapter->HalFunc.read_adapter_info(padapter);
20 }
21 
rtw_hal_read_chip_version(struct adapter * padapter)22 void rtw_hal_read_chip_version(struct adapter *padapter)
23 {
24 	if (padapter->HalFunc.read_chip_version)
25 		padapter->HalFunc.read_chip_version(padapter);
26 }
27 
rtw_hal_def_value_init(struct adapter * padapter)28 void rtw_hal_def_value_init(struct adapter *padapter)
29 {
30 	if (is_primary_adapter(padapter))
31 		if (padapter->HalFunc.init_default_value)
32 			padapter->HalFunc.init_default_value(padapter);
33 }
34 
rtw_hal_free_data(struct adapter * padapter)35 void rtw_hal_free_data(struct adapter *padapter)
36 {
37 	/* free HAL Data */
38 	rtw_hal_data_deinit(padapter);
39 
40 	if (is_primary_adapter(padapter))
41 		if (padapter->HalFunc.free_hal_data)
42 			padapter->HalFunc.free_hal_data(padapter);
43 }
44 
rtw_hal_dm_init(struct adapter * padapter)45 void rtw_hal_dm_init(struct adapter *padapter)
46 {
47 	if (is_primary_adapter(padapter))
48 		if (padapter->HalFunc.dm_init)
49 			padapter->HalFunc.dm_init(padapter);
50 }
51 
rtw_hal_dm_deinit(struct adapter * padapter)52 void rtw_hal_dm_deinit(struct adapter *padapter)
53 {
54 	/*  cancel dm  timer */
55 	if (is_primary_adapter(padapter))
56 		if (padapter->HalFunc.dm_deinit)
57 			padapter->HalFunc.dm_deinit(padapter);
58 }
59 
rtw_hal_init_opmode(struct adapter * padapter)60 static void rtw_hal_init_opmode(struct adapter *padapter)
61 {
62 	enum ndis_802_11_network_infrastructure networkType = Ndis802_11InfrastructureMax;
63 	struct  mlme_priv *pmlmepriv = &(padapter->mlmepriv);
64 	signed int fw_state;
65 
66 	fw_state = get_fwstate(pmlmepriv);
67 
68 	if (fw_state & WIFI_ADHOC_STATE)
69 		networkType = Ndis802_11IBSS;
70 	else if (fw_state & WIFI_STATION_STATE)
71 		networkType = Ndis802_11Infrastructure;
72 	else if (fw_state & WIFI_AP_STATE)
73 		networkType = Ndis802_11APMode;
74 	else
75 		return;
76 
77 	rtw_setopmode_cmd(padapter, networkType, false);
78 }
79 
rtw_hal_init(struct adapter * padapter)80 uint rtw_hal_init(struct adapter *padapter)
81 {
82 	uint status;
83 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
84 
85 	status = padapter->HalFunc.hal_init(padapter);
86 
87 	if (status == _SUCCESS) {
88 		rtw_hal_init_opmode(padapter);
89 
90 		dvobj->padapters->hw_init_completed = true;
91 
92 		if (padapter->registrypriv.notch_filter == 1)
93 			rtw_hal_notch_filter(padapter, 1);
94 
95 		rtw_hal_reset_security_engine(padapter);
96 
97 		rtw_sec_restore_wep_key(dvobj->padapters);
98 
99 		init_hw_mlme_ext(padapter);
100 
101 		rtw_bb_rf_gain_offset(padapter);
102 	} else {
103 		dvobj->padapters->hw_init_completed = false;
104 	}
105 
106 	return status;
107 }
108 
rtw_hal_deinit(struct adapter * padapter)109 uint rtw_hal_deinit(struct adapter *padapter)
110 {
111 	uint status = _SUCCESS;
112 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
113 
114 	status = padapter->HalFunc.hal_deinit(padapter);
115 
116 	if (status == _SUCCESS) {
117 		padapter = dvobj->padapters;
118 		padapter->hw_init_completed = false;
119 	}
120 
121 	return status;
122 }
123 
rtw_hal_set_hwreg(struct adapter * padapter,u8 variable,u8 * val)124 void rtw_hal_set_hwreg(struct adapter *padapter, u8 variable, u8 *val)
125 {
126 	if (padapter->HalFunc.SetHwRegHandler)
127 		padapter->HalFunc.SetHwRegHandler(padapter, variable, val);
128 }
129 
rtw_hal_get_hwreg(struct adapter * padapter,u8 variable,u8 * val)130 void rtw_hal_get_hwreg(struct adapter *padapter, u8 variable, u8 *val)
131 {
132 	if (padapter->HalFunc.GetHwRegHandler)
133 		padapter->HalFunc.GetHwRegHandler(padapter, variable, val);
134 }
135 
rtw_hal_set_hwreg_with_buf(struct adapter * padapter,u8 variable,u8 * pbuf,int len)136 void rtw_hal_set_hwreg_with_buf(struct adapter *padapter, u8 variable, u8 *pbuf, int len)
137 {
138 	if (padapter->HalFunc.SetHwRegHandlerWithBuf)
139 		padapter->HalFunc.SetHwRegHandlerWithBuf(padapter, variable, pbuf, len);
140 }
141 
rtw_hal_set_def_var(struct adapter * padapter,enum hal_def_variable eVariable,void * pValue)142 u8 rtw_hal_set_def_var(struct adapter *padapter, enum hal_def_variable eVariable, void *pValue)
143 {
144 	if (padapter->HalFunc.SetHalDefVarHandler)
145 		return padapter->HalFunc.SetHalDefVarHandler(padapter, eVariable, pValue);
146 	return _FAIL;
147 }
148 
rtw_hal_get_def_var(struct adapter * padapter,enum hal_def_variable eVariable,void * pValue)149 u8 rtw_hal_get_def_var(struct adapter *padapter, enum hal_def_variable eVariable, void *pValue)
150 {
151 	if (padapter->HalFunc.GetHalDefVarHandler)
152 		return padapter->HalFunc.GetHalDefVarHandler(padapter, eVariable, pValue);
153 	return _FAIL;
154 }
155 
rtw_hal_set_odm_var(struct adapter * padapter,enum hal_odm_variable eVariable,void * pValue1,bool bSet)156 void rtw_hal_set_odm_var(struct adapter *padapter, enum hal_odm_variable eVariable, void *pValue1, bool bSet)
157 {
158 	if (padapter->HalFunc.SetHalODMVarHandler)
159 		padapter->HalFunc.SetHalODMVarHandler(padapter, eVariable, pValue1, bSet);
160 }
161 
rtw_hal_enable_interrupt(struct adapter * padapter)162 void rtw_hal_enable_interrupt(struct adapter *padapter)
163 {
164 	if (padapter->HalFunc.enable_interrupt)
165 		padapter->HalFunc.enable_interrupt(padapter);
166 }
167 
rtw_hal_disable_interrupt(struct adapter * padapter)168 void rtw_hal_disable_interrupt(struct adapter *padapter)
169 {
170 	if (padapter->HalFunc.disable_interrupt)
171 		padapter->HalFunc.disable_interrupt(padapter);
172 }
173 
rtw_hal_check_ips_status(struct adapter * padapter)174 u8 rtw_hal_check_ips_status(struct adapter *padapter)
175 {
176 	u8 val = false;
177 
178 	if (padapter->HalFunc.check_ips_status)
179 		val = padapter->HalFunc.check_ips_status(padapter);
180 
181 	return val;
182 }
183 
rtw_hal_xmitframe_enqueue(struct adapter * padapter,struct xmit_frame * pxmitframe)184 s32	rtw_hal_xmitframe_enqueue(struct adapter *padapter, struct xmit_frame *pxmitframe)
185 {
186 	if (padapter->HalFunc.hal_xmitframe_enqueue)
187 		return padapter->HalFunc.hal_xmitframe_enqueue(padapter, pxmitframe);
188 
189 	return false;
190 }
191 
rtw_hal_xmit(struct adapter * padapter,struct xmit_frame * pxmitframe)192 s32	rtw_hal_xmit(struct adapter *padapter, struct xmit_frame *pxmitframe)
193 {
194 	if (padapter->HalFunc.hal_xmit)
195 		return padapter->HalFunc.hal_xmit(padapter, pxmitframe);
196 
197 	return false;
198 }
199 
200 /*
201  * [IMPORTANT] This function would be run in interrupt context.
202  */
rtw_hal_mgnt_xmit(struct adapter * padapter,struct xmit_frame * pmgntframe)203 s32	rtw_hal_mgnt_xmit(struct adapter *padapter, struct xmit_frame *pmgntframe)
204 {
205 	s32 ret = _FAIL;
206 
207 	update_mgntframe_attrib_addr(padapter, pmgntframe);
208 	/* pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET; */
209 	/* pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; */
210 	/* memcpy(pmgntframe->attrib.ra, pwlanhdr->addr1, ETH_ALEN); */
211 
212 	if (padapter->securitypriv.binstallBIPkey == true) {
213 		if (is_multicast_ether_addr(pmgntframe->attrib.ra)) {
214 			pmgntframe->attrib.encrypt = _BIP_;
215 			/* pmgntframe->attrib.bswenc = true; */
216 		} else {
217 			pmgntframe->attrib.encrypt = _AES_;
218 			pmgntframe->attrib.bswenc = true;
219 		}
220 		rtw_mgmt_xmitframe_coalesce(padapter, pmgntframe->pkt, pmgntframe);
221 	}
222 
223 	if (padapter->HalFunc.mgnt_xmit)
224 		ret = padapter->HalFunc.mgnt_xmit(padapter, pmgntframe);
225 	return ret;
226 }
227 
rtw_hal_init_xmit_priv(struct adapter * padapter)228 s32	rtw_hal_init_xmit_priv(struct adapter *padapter)
229 {
230 	if (padapter->HalFunc.init_xmit_priv)
231 		return padapter->HalFunc.init_xmit_priv(padapter);
232 	return _FAIL;
233 }
234 
rtw_hal_free_xmit_priv(struct adapter * padapter)235 void rtw_hal_free_xmit_priv(struct adapter *padapter)
236 {
237 	if (padapter->HalFunc.free_xmit_priv)
238 		padapter->HalFunc.free_xmit_priv(padapter);
239 }
240 
rtw_hal_init_recv_priv(struct adapter * padapter)241 s32	rtw_hal_init_recv_priv(struct adapter *padapter)
242 {
243 	if (padapter->HalFunc.init_recv_priv)
244 		return padapter->HalFunc.init_recv_priv(padapter);
245 
246 	return _FAIL;
247 }
248 
rtw_hal_free_recv_priv(struct adapter * padapter)249 void rtw_hal_free_recv_priv(struct adapter *padapter)
250 {
251 	if (padapter->HalFunc.free_recv_priv)
252 		padapter->HalFunc.free_recv_priv(padapter);
253 }
254 
rtw_hal_update_ra_mask(struct sta_info * psta,u8 rssi_level)255 void rtw_hal_update_ra_mask(struct sta_info *psta, u8 rssi_level)
256 {
257 	struct adapter *padapter;
258 	struct mlme_priv *pmlmepriv;
259 
260 	if (!psta)
261 		return;
262 
263 	padapter = psta->padapter;
264 
265 	pmlmepriv = &(padapter->mlmepriv);
266 
267 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
268 		add_RATid(padapter, psta, rssi_level);
269 	else {
270 		if (padapter->HalFunc.UpdateRAMaskHandler)
271 			padapter->HalFunc.UpdateRAMaskHandler(padapter, psta->mac_id, rssi_level);
272 	}
273 }
274 
rtw_hal_add_ra_tid(struct adapter * padapter,u32 bitmap,u8 * arg,u8 rssi_level)275 void rtw_hal_add_ra_tid(struct adapter *padapter, u32 bitmap, u8 *arg, u8 rssi_level)
276 {
277 	if (padapter->HalFunc.Add_RateATid)
278 		padapter->HalFunc.Add_RateATid(padapter, bitmap, arg, rssi_level);
279 }
280 
281 /*Start specifical interface thread		*/
rtw_hal_start_thread(struct adapter * padapter)282 void rtw_hal_start_thread(struct adapter *padapter)
283 {
284 	if (padapter->HalFunc.run_thread)
285 		padapter->HalFunc.run_thread(padapter);
286 }
287 /*Start specifical interface thread		*/
rtw_hal_stop_thread(struct adapter * padapter)288 void rtw_hal_stop_thread(struct adapter *padapter)
289 {
290 	if (padapter->HalFunc.cancel_thread)
291 		padapter->HalFunc.cancel_thread(padapter);
292 }
293 
rtw_hal_read_bbreg(struct adapter * padapter,u32 RegAddr,u32 BitMask)294 u32 rtw_hal_read_bbreg(struct adapter *padapter, u32 RegAddr, u32 BitMask)
295 {
296 	u32 data = 0;
297 
298 	if (padapter->HalFunc.read_bbreg)
299 		 data = padapter->HalFunc.read_bbreg(padapter, RegAddr, BitMask);
300 	return data;
301 }
rtw_hal_write_bbreg(struct adapter * padapter,u32 RegAddr,u32 BitMask,u32 Data)302 void rtw_hal_write_bbreg(struct adapter *padapter, u32 RegAddr, u32 BitMask, u32 Data)
303 {
304 	if (padapter->HalFunc.write_bbreg)
305 		padapter->HalFunc.write_bbreg(padapter, RegAddr, BitMask, Data);
306 }
307 
rtw_hal_read_rfreg(struct adapter * padapter,u32 eRFPath,u32 RegAddr,u32 BitMask)308 u32 rtw_hal_read_rfreg(struct adapter *padapter, u32 eRFPath, u32 RegAddr, u32 BitMask)
309 {
310 	u32 data = 0;
311 
312 	if (padapter->HalFunc.read_rfreg)
313 		data = padapter->HalFunc.read_rfreg(padapter, eRFPath, RegAddr, BitMask);
314 	return data;
315 }
rtw_hal_write_rfreg(struct adapter * padapter,u32 eRFPath,u32 RegAddr,u32 BitMask,u32 Data)316 void rtw_hal_write_rfreg(struct adapter *padapter, u32 eRFPath, u32 RegAddr, u32 BitMask, u32 Data)
317 {
318 	if (padapter->HalFunc.write_rfreg)
319 		padapter->HalFunc.write_rfreg(padapter, eRFPath, RegAddr, BitMask, Data);
320 }
321 
rtw_hal_set_chan(struct adapter * padapter,u8 channel)322 void rtw_hal_set_chan(struct adapter *padapter, u8 channel)
323 {
324 	if (padapter->HalFunc.set_channel_handler)
325 		padapter->HalFunc.set_channel_handler(padapter, channel);
326 }
327 
rtw_hal_set_chnl_bw(struct adapter * padapter,u8 channel,enum channel_width Bandwidth,u8 Offset40,u8 Offset80)328 void rtw_hal_set_chnl_bw(struct adapter *padapter, u8 channel,
329 			 enum channel_width Bandwidth, u8 Offset40, u8 Offset80)
330 {
331 	if (padapter->HalFunc.set_chnl_bw_handler)
332 		padapter->HalFunc.set_chnl_bw_handler(padapter, channel,
333 						      Bandwidth, Offset40,
334 						      Offset80);
335 }
336 
rtw_hal_dm_watchdog(struct adapter * padapter)337 void rtw_hal_dm_watchdog(struct adapter *padapter)
338 {
339 	if (padapter->HalFunc.hal_dm_watchdog)
340 		padapter->HalFunc.hal_dm_watchdog(padapter);
341 }
342 
rtw_hal_dm_watchdog_in_lps(struct adapter * padapter)343 void rtw_hal_dm_watchdog_in_lps(struct adapter *padapter)
344 {
345 	if (adapter_to_pwrctl(padapter)->fw_current_in_ps_mode) {
346 		if (padapter->HalFunc.hal_dm_watchdog_in_lps)
347 			padapter->HalFunc.hal_dm_watchdog_in_lps(padapter); /* this function caller is in interrupt context */
348 	}
349 }
350 
beacon_timing_control(struct adapter * padapter)351 void beacon_timing_control(struct adapter *padapter)
352 {
353 	if (padapter->HalFunc.SetBeaconRelatedRegistersHandler)
354 		padapter->HalFunc.SetBeaconRelatedRegistersHandler(padapter);
355 }
356 
357 
rtw_hal_xmit_thread_handler(struct adapter * padapter)358 s32 rtw_hal_xmit_thread_handler(struct adapter *padapter)
359 {
360 	if (padapter->HalFunc.xmit_thread_handler)
361 		return padapter->HalFunc.xmit_thread_handler(padapter);
362 	return _FAIL;
363 }
364 
rtw_hal_notch_filter(struct adapter * adapter,bool enable)365 void rtw_hal_notch_filter(struct adapter *adapter, bool enable)
366 {
367 	if (adapter->HalFunc.hal_notch_filter)
368 		adapter->HalFunc.hal_notch_filter(adapter, enable);
369 }
370 
rtw_hal_reset_security_engine(struct adapter * adapter)371 void rtw_hal_reset_security_engine(struct adapter *adapter)
372 {
373 	if (adapter->HalFunc.hal_reset_security_engine)
374 		adapter->HalFunc.hal_reset_security_engine(adapter);
375 }
376 
rtw_hal_c2h_valid(struct adapter * adapter,u8 * buf)377 bool rtw_hal_c2h_valid(struct adapter *adapter, u8 *buf)
378 {
379 	return c2h_evt_valid((struct c2h_evt_hdr_88xx *)buf);
380 }
381 
rtw_hal_c2h_handler(struct adapter * adapter,u8 * c2h_evt)382 s32 rtw_hal_c2h_handler(struct adapter *adapter, u8 *c2h_evt)
383 {
384 	s32 ret = _FAIL;
385 
386 	if (adapter->HalFunc.c2h_handler)
387 		ret = adapter->HalFunc.c2h_handler(adapter, c2h_evt);
388 	return ret;
389 }
390 
rtw_hal_c2h_id_filter_ccx(struct adapter * adapter)391 c2h_id_filter rtw_hal_c2h_id_filter_ccx(struct adapter *adapter)
392 {
393 	return adapter->HalFunc.c2h_id_filter_ccx;
394 }
395 
rtw_hal_macid_sleep(struct adapter * padapter,u32 macid)396 s32 rtw_hal_macid_sleep(struct adapter *padapter, u32 macid)
397 {
398 	u8 support;
399 
400 	support = false;
401 	rtw_hal_get_def_var(padapter, HAL_DEF_MACID_SLEEP, &support);
402 	if (false == support)
403 		return _FAIL;
404 
405 	rtw_hal_set_hwreg(padapter, HW_VAR_MACID_SLEEP, (u8 *)&macid);
406 
407 	return _SUCCESS;
408 }
409 
rtw_hal_macid_wakeup(struct adapter * padapter,u32 macid)410 s32 rtw_hal_macid_wakeup(struct adapter *padapter, u32 macid)
411 {
412 	u8 support;
413 
414 	support = false;
415 	rtw_hal_get_def_var(padapter, HAL_DEF_MACID_SLEEP, &support);
416 	if (false == support)
417 		return _FAIL;
418 
419 	rtw_hal_set_hwreg(padapter, HW_VAR_MACID_WAKEUP, (u8 *)&macid);
420 
421 	return _SUCCESS;
422 }
423 
rtw_hal_fill_h2c_cmd(struct adapter * padapter,u8 ElementID,u32 CmdLen,u8 * pCmdBuffer)424 s32 rtw_hal_fill_h2c_cmd(struct adapter *padapter, u8 ElementID, u32 CmdLen, u8 *pCmdBuffer)
425 {
426 	s32 ret = _FAIL;
427 
428 	if (padapter->HalFunc.fill_h2c_cmd)
429 		ret = padapter->HalFunc.fill_h2c_cmd(padapter, ElementID, CmdLen, pCmdBuffer);
430 
431 	return ret;
432 }
433