• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * rtl8712_cmd.c
3  *
4  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5  * Linux device driver for RTL8192SU
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * Modifications for inclusion into the Linux staging tree are
21  * Copyright(c) 2010 Larry Finger. All rights reserved.
22  *
23  * Contact information:
24  * WLAN FAE <wlanfae@realtek.com>.
25  * Larry Finger <Larry.Finger@lwfinger.net>
26  *
27  ******************************************************************************/
28 
29 #define _RTL8712_CMD_C_
30 
31 #include <linux/compiler.h>
32 #include <linux/kernel.h>
33 #include <linux/errno.h>
34 #include <linux/slab.h>
35 #include <linux/sched/signal.h>
36 #include <linux/module.h>
37 #include <linux/kref.h>
38 #include <linux/netdevice.h>
39 #include <linux/skbuff.h>
40 #include <linux/usb.h>
41 #include <linux/usb/ch9.h>
42 #include <linux/circ_buf.h>
43 #include <linux/uaccess.h>
44 #include <asm/byteorder.h>
45 #include <linux/atomic.h>
46 #include <linux/semaphore.h>
47 #include <linux/rtnetlink.h>
48 
49 #include "osdep_service.h"
50 #include "drv_types.h"
51 #include "recv_osdep.h"
52 #include "mlme_osdep.h"
53 #include "rtl871x_ioctl_set.h"
54 
check_hw_pbc(struct _adapter * padapter)55 static void check_hw_pbc(struct _adapter *padapter)
56 {
57 	u8	tmp1byte;
58 
59 	r8712_write8(padapter, MAC_PINMUX_CTRL, (GPIOMUX_EN | GPIOSEL_GPIO));
60 	tmp1byte = r8712_read8(padapter, GPIO_IO_SEL);
61 	tmp1byte &= ~(HAL_8192S_HW_GPIO_WPS_BIT);
62 	r8712_write8(padapter, GPIO_IO_SEL, tmp1byte);
63 	tmp1byte = r8712_read8(padapter, GPIO_CTRL);
64 	if (tmp1byte == 0xff)
65 		return;
66 	if (tmp1byte & HAL_8192S_HW_GPIO_WPS_BIT) {
67 		/* Here we only set bPbcPressed to true
68 		 * After trigger PBC, the variable will be set to false
69 		 */
70 		DBG_8712("CheckPbcGPIO - PBC is pressed !!!!\n");
71 		/* 0 is the default value and it means the application monitors
72 		 * the HW PBC doesn't provide its pid to driver.
73 		 */
74 		if (padapter->pid == 0)
75 			return;
76 		kill_pid(find_vpid(padapter->pid), SIGUSR1, 1);
77 	}
78 }
79 
80 /* query rx phy status from fw.
81  * Adhoc mode: beacon.
82  * Infrastructure mode: beacon , data.
83  */
query_fw_rx_phy_status(struct _adapter * padapter)84 static void query_fw_rx_phy_status(struct _adapter *padapter)
85 {
86 	u32 val32 = 0;
87 	int pollingcnts = 50;
88 
89 	if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
90 		r8712_write32(padapter, IOCMD_CTRL_REG, 0xf4000001);
91 		msleep(100);
92 		/* Wait FW complete IO Cmd */
93 		while ((r8712_read32(padapter, IOCMD_CTRL_REG)) &&
94 		       (pollingcnts > 0)) {
95 			pollingcnts--;
96 			msleep(20);
97 		}
98 		if (pollingcnts != 0)
99 			val32 = r8712_read32(padapter, IOCMD_DATA_REG);
100 		else /* time out */
101 			val32 = 0;
102 		val32 >>= 4;
103 		padapter->recvpriv.fw_rssi =
104 			 (u8)r8712_signal_scale_mapping(val32);
105 	}
106 }
107 
108 /* check mlme, hw, phy, or dynamic algorithm status. */
StatusWatchdogCallback(struct _adapter * padapter)109 static void StatusWatchdogCallback(struct _adapter *padapter)
110 {
111 	check_hw_pbc(padapter);
112 	query_fw_rx_phy_status(padapter);
113 }
114 
r871x_internal_cmd_hdl(struct _adapter * padapter,u8 * pbuf)115 static void r871x_internal_cmd_hdl(struct _adapter *padapter, u8 *pbuf)
116 {
117 	struct drvint_cmd_parm *pdrvcmd;
118 
119 	if (!pbuf)
120 		return;
121 	pdrvcmd = (struct drvint_cmd_parm *)pbuf;
122 	switch (pdrvcmd->i_cid) {
123 	case WDG_WK_CID:
124 		StatusWatchdogCallback(padapter);
125 		break;
126 	default:
127 		break;
128 	}
129 	kfree(pdrvcmd->pbuf);
130 }
131 
read_macreg_hdl(struct _adapter * padapter,u8 * pbuf)132 static u8 read_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
133 {
134 	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj	*pcmd);
135 	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
136 
137 	/*  invoke cmd->callback function */
138 	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
139 	if (!pcmd_callback)
140 		r8712_free_cmd_obj(pcmd);
141 	else
142 		pcmd_callback(padapter, pcmd);
143 	return H2C_SUCCESS;
144 }
145 
write_macreg_hdl(struct _adapter * padapter,u8 * pbuf)146 static u8 write_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
147 {
148 	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj	*pcmd);
149 	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
150 
151 	/*  invoke cmd->callback function */
152 	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
153 	if (!pcmd_callback)
154 		r8712_free_cmd_obj(pcmd);
155 	else
156 		pcmd_callback(padapter, pcmd);
157 	return H2C_SUCCESS;
158 }
159 
read_bbreg_hdl(struct _adapter * padapter,u8 * pbuf)160 static u8 read_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
161 {
162 	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
163 
164 	r8712_free_cmd_obj(pcmd);
165 	return H2C_SUCCESS;
166 }
167 
write_bbreg_hdl(struct _adapter * padapter,u8 * pbuf)168 static u8 write_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
169 {
170 	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
171 	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
172 
173 	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
174 	if (!pcmd_callback)
175 		r8712_free_cmd_obj(pcmd);
176 	else
177 		pcmd_callback(padapter, pcmd);
178 	return H2C_SUCCESS;
179 }
180 
read_rfreg_hdl(struct _adapter * padapter,u8 * pbuf)181 static u8 read_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
182 {
183 	u32 val;
184 	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
185 	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
186 
187 	if (pcmd->rsp && pcmd->rspsz > 0)
188 		memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
189 	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
190 	if (!pcmd_callback)
191 		r8712_free_cmd_obj(pcmd);
192 	else
193 		pcmd_callback(padapter, pcmd);
194 	return H2C_SUCCESS;
195 }
196 
write_rfreg_hdl(struct _adapter * padapter,u8 * pbuf)197 static u8 write_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
198 {
199 	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
200 	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
201 
202 	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
203 	if (!pcmd_callback)
204 		r8712_free_cmd_obj(pcmd);
205 	else
206 		pcmd_callback(padapter, pcmd);
207 	return H2C_SUCCESS;
208 }
209 
sys_suspend_hdl(struct _adapter * padapter,u8 * pbuf)210 static u8 sys_suspend_hdl(struct _adapter *padapter, u8 *pbuf)
211 {
212 	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
213 
214 	r8712_free_cmd_obj(pcmd);
215 	return H2C_SUCCESS;
216 }
217 
cmd_hdl_filter(struct _adapter * padapter,struct cmd_obj * pcmd)218 static struct cmd_obj *cmd_hdl_filter(struct _adapter *padapter,
219 				      struct cmd_obj *pcmd)
220 {
221 	struct cmd_obj *pcmd_r;
222 
223 	if (!pcmd)
224 		return pcmd;
225 	pcmd_r = NULL;
226 
227 	switch (pcmd->cmdcode) {
228 	case GEN_CMD_CODE(_Read_MACREG):
229 		read_macreg_hdl(padapter, (u8 *)pcmd);
230 		pcmd_r = pcmd;
231 		break;
232 	case GEN_CMD_CODE(_Write_MACREG):
233 		write_macreg_hdl(padapter, (u8 *)pcmd);
234 		pcmd_r = pcmd;
235 		break;
236 	case GEN_CMD_CODE(_Read_BBREG):
237 		read_bbreg_hdl(padapter, (u8 *)pcmd);
238 		break;
239 	case GEN_CMD_CODE(_Write_BBREG):
240 		write_bbreg_hdl(padapter, (u8 *)pcmd);
241 		break;
242 	case GEN_CMD_CODE(_Read_RFREG):
243 		read_rfreg_hdl(padapter, (u8 *)pcmd);
244 		break;
245 	case GEN_CMD_CODE(_Write_RFREG):
246 		write_rfreg_hdl(padapter, (u8 *)pcmd);
247 		break;
248 	case GEN_CMD_CODE(_SetUsbSuspend):
249 		sys_suspend_hdl(padapter, (u8 *)pcmd);
250 		break;
251 	case GEN_CMD_CODE(_JoinBss):
252 		r8712_joinbss_reset(padapter);
253 		/* Before set JoinBss_CMD to FW, driver must ensure FW is in
254 		 * PS_MODE_ACTIVE. Directly write rpwm to radio on and assign
255 		 * new pwr_mode to Driver, instead of use workitem to change
256 		 * state.
257 		 */
258 		if (padapter->pwrctrlpriv.pwr_mode > PS_MODE_ACTIVE) {
259 			padapter->pwrctrlpriv.pwr_mode = PS_MODE_ACTIVE;
260 			mutex_lock(&padapter->pwrctrlpriv.mutex_lock);
261 			r8712_set_rpwm(padapter, PS_STATE_S4);
262 			mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
263 		}
264 		pcmd_r = pcmd;
265 		break;
266 	case _DRV_INT_CMD_:
267 		r871x_internal_cmd_hdl(padapter, pcmd->parmbuf);
268 		r8712_free_cmd_obj(pcmd);
269 		pcmd_r = NULL;
270 		break;
271 	default:
272 		pcmd_r = pcmd;
273 		break;
274 	}
275 	return pcmd_r; /* if returning pcmd_r == NULL, pcmd must be free. */
276 }
277 
check_cmd_fifo(struct _adapter * padapter,uint sz)278 static u8 check_cmd_fifo(struct _adapter *padapter, uint sz)
279 {
280 	return _SUCCESS;
281 }
282 
r8712_fw_cmd(struct _adapter * pAdapter,u32 cmd)283 u8 r8712_fw_cmd(struct _adapter *pAdapter, u32 cmd)
284 {
285 	int pollingcnts = 50;
286 
287 	r8712_write32(pAdapter, IOCMD_CTRL_REG, cmd);
288 	msleep(100);
289 	while ((r8712_read32(pAdapter, IOCMD_CTRL_REG != 0)) &&
290 	       (pollingcnts > 0)) {
291 		pollingcnts--;
292 		msleep(20);
293 	}
294 	if (pollingcnts == 0)
295 		return false;
296 	return true;
297 }
298 
r8712_fw_cmd_data(struct _adapter * pAdapter,u32 * value,u8 flag)299 void r8712_fw_cmd_data(struct _adapter *pAdapter, u32 *value, u8 flag)
300 {
301 	if (flag == 0)	/* set */
302 		r8712_write32(pAdapter, IOCMD_DATA_REG, *value);
303 	else		/* query */
304 		*value = r8712_read32(pAdapter, IOCMD_DATA_REG);
305 }
306 
r8712_cmd_thread(void * context)307 int r8712_cmd_thread(void *context)
308 {
309 	struct cmd_obj *pcmd;
310 	unsigned int cmdsz, wr_sz;
311 	__le32 *pcmdbuf;
312 	struct tx_desc *pdesc;
313 	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
314 	struct _adapter *padapter = context;
315 	struct	cmd_priv	*pcmdpriv = &(padapter->cmdpriv);
316 
317 	allow_signal(SIGTERM);
318 	while (1) {
319 		if (wait_for_completion_interruptible(&pcmdpriv->cmd_queue_comp))
320 			break;
321 		if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
322 			break;
323 		if (r8712_register_cmd_alive(padapter) != _SUCCESS)
324 			continue;
325 _next:
326 		pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
327 		if (!(pcmd)) {
328 			r8712_unregister_cmd_alive(padapter);
329 			continue;
330 		}
331 		pcmdbuf = (__le32 *)pcmdpriv->cmd_buf;
332 		pdesc = (struct tx_desc *)pcmdbuf;
333 		memset(pdesc, 0, TXDESC_SIZE);
334 		pcmd = cmd_hdl_filter(padapter, pcmd);
335 		if (pcmd) { /* if pcmd != NULL, cmd will be handled by f/w */
336 			struct dvobj_priv *pdvobj = &padapter->dvobjpriv;
337 			u8 blnPending = 0;
338 
339 			pcmdpriv->cmd_issued_cnt++;
340 			cmdsz = round_up(pcmd->cmdsz, 8);
341 			wr_sz = TXDESC_SIZE + 8 + cmdsz;
342 			pdesc->txdw0 |= cpu_to_le32((wr_sz - TXDESC_SIZE) &
343 						     0x0000ffff);
344 			if (pdvobj->ishighspeed) {
345 				if ((wr_sz % 512) == 0)
346 					blnPending = 1;
347 			} else {
348 				if ((wr_sz % 64) == 0)
349 					blnPending = 1;
350 			}
351 			if (blnPending) /* 32 bytes for TX Desc - 8 offset */
352 				pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
353 						OFFSET_SZ + 8) << OFFSET_SHT) &
354 						0x00ff0000);
355 			else {
356 				pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
357 							      OFFSET_SZ) <<
358 							      OFFSET_SHT) &
359 							      0x00ff0000);
360 			}
361 			pdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
362 			pdesc->txdw1 |= cpu_to_le32((0x13 << QSEL_SHT) &
363 						    0x00001f00);
364 			pcmdbuf += (TXDESC_SIZE >> 2);
365 			*pcmdbuf = cpu_to_le32((cmdsz & 0x0000ffff) |
366 					       (pcmd->cmdcode << 16) |
367 					       (pcmdpriv->cmd_seq << 24));
368 			pcmdbuf += 2; /* 8 bytes alignment */
369 			memcpy((u8 *)pcmdbuf, pcmd->parmbuf, pcmd->cmdsz);
370 			while (check_cmd_fifo(padapter, wr_sz) == _FAIL) {
371 				if (padapter->bDriverStopped ||
372 				    padapter->bSurpriseRemoved)
373 					break;
374 				msleep(100);
375 				continue;
376 			}
377 			if (blnPending)
378 				wr_sz += 8;   /* Append 8 bytes */
379 			r8712_write_mem(padapter, RTL8712_DMA_H2CCMD, wr_sz,
380 					(u8 *)pdesc);
381 			pcmdpriv->cmd_seq++;
382 			if (pcmd->cmdcode == GEN_CMD_CODE(_CreateBss)) {
383 				pcmd->res = H2C_SUCCESS;
384 				pcmd_callback = cmd_callback[pcmd->
385 						cmdcode].callback;
386 				if (pcmd_callback)
387 					pcmd_callback(padapter, pcmd);
388 				continue;
389 			}
390 			if (pcmd->cmdcode == GEN_CMD_CODE(_SetPwrMode)) {
391 				if (padapter->pwrctrlpriv.bSleep) {
392 					mutex_lock(&padapter->
393 						       pwrctrlpriv.mutex_lock);
394 					r8712_set_rpwm(padapter, PS_STATE_S2);
395 					mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
396 				}
397 			}
398 			r8712_free_cmd_obj(pcmd);
399 			if (list_empty(&pcmdpriv->cmd_queue.queue)) {
400 				r8712_unregister_cmd_alive(padapter);
401 				continue;
402 			} else {
403 				goto _next;
404 			}
405 		} else {
406 			goto _next;
407 		}
408 		flush_signals_thread();
409 	}
410 	/* free all cmd_obj resources */
411 	do {
412 		pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
413 		if (!pcmd)
414 			break;
415 		r8712_free_cmd_obj(pcmd);
416 	} while (1);
417 	complete(&pcmdpriv->terminate_cmdthread_comp);
418 	thread_exit();
419 }
420 
r8712_event_handle(struct _adapter * padapter,__le32 * peventbuf)421 void r8712_event_handle(struct _adapter *padapter, __le32 *peventbuf)
422 {
423 	u8 evt_code, evt_seq;
424 	u16 evt_sz;
425 	void (*event_callback)(struct _adapter *dev, u8 *pbuf);
426 	struct	evt_priv *pevt_priv = &(padapter->evtpriv);
427 
428 	if (!peventbuf)
429 		goto _abort_event_;
430 	evt_sz = (u16)(le32_to_cpu(*peventbuf) & 0xffff);
431 	evt_seq = (u8)((le32_to_cpu(*peventbuf) >> 24) & 0x7f);
432 	evt_code = (u8)((le32_to_cpu(*peventbuf) >> 16) & 0xff);
433 	/* checking event sequence... */
434 	if ((evt_seq & 0x7f) != pevt_priv->event_seq) {
435 		pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
436 		goto _abort_event_;
437 	}
438 	/* checking if event code is valid */
439 	if (evt_code >= MAX_C2HEVT) {
440 		pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
441 		goto _abort_event_;
442 	} else if ((evt_code == GEN_EVT_CODE(_Survey)) &&
443 		   (evt_sz > sizeof(struct wlan_bssid_ex))) {
444 		pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
445 		goto _abort_event_;
446 	}
447 	/* checking if event size match the event parm size */
448 	if ((wlanevents[evt_code].parmsize) &&
449 	    (wlanevents[evt_code].parmsize != evt_sz)) {
450 		pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
451 		goto _abort_event_;
452 	} else if ((evt_sz == 0) && (evt_code != GEN_EVT_CODE(_WPS_PBC))) {
453 		pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
454 		goto _abort_event_;
455 	}
456 	pevt_priv->event_seq++;	/* update evt_seq */
457 	if (pevt_priv->event_seq > 127)
458 		pevt_priv->event_seq = 0;
459 	/* move to event content, 8 bytes alignment */
460 	peventbuf = peventbuf + 2;
461 	event_callback = wlanevents[evt_code].event_callback;
462 	if (event_callback)
463 		event_callback(padapter, (u8 *)peventbuf);
464 	pevt_priv->evt_done_cnt++;
465 _abort_event_:
466 	return;
467 }
468