rtl8712_cmd.c 13.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
/******************************************************************************
 * rtl8712_cmd.c
 *
 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
 * Linux device driver for RTL8192SU
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 * Modifications for inclusion into the Linux staging tree are
 * Copyright(c) 2010 Larry Finger. All rights reserved.
 *
 * Contact information:
 * WLAN FAE <wlanfae@realtek.com>.
 * Larry Finger <Larry.Finger@lwfinger.net>
 *
 ******************************************************************************/

#define _RTL8712_CMD_C_

#include <linux/compiler.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kref.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/usb.h>
#include <linux/usb/ch9.h>
#include <linux/circ_buf.h>
#include <linux/uaccess.h>
#include <asm/byteorder.h>
#include <linux/atomic.h>
#include <linux/semaphore.h>
#include <linux/rtnetlink.h>

#include "osdep_service.h"
#include "drv_types.h"
#include "recv_osdep.h"
#include "mlme_osdep.h"
#include "rtl871x_byteorder.h"
#include "rtl871x_ioctl_set.h"

static void check_hw_pbc(struct _adapter *padapter)
{
	u8	tmp1byte;

	r8712_write8(padapter, MAC_PINMUX_CTRL, (GPIOMUX_EN | GPIOSEL_GPIO));
	tmp1byte = r8712_read8(padapter, GPIO_IO_SEL);
	tmp1byte &= ~(HAL_8192S_HW_GPIO_WPS_BIT);
	r8712_write8(padapter, GPIO_IO_SEL, tmp1byte);
	tmp1byte = r8712_read8(padapter, GPIO_CTRL);
	if (tmp1byte == 0xff)
		return ;
	if (tmp1byte&HAL_8192S_HW_GPIO_WPS_BIT) {
		/* Here we only set bPbcPressed to true
		 * After trigger PBC, the variable will be set to false */
		DBG_8712("CheckPbcGPIO - PBC is pressed !!!!\n");
		/* 0 is the default value and it means the application monitors
		 * the HW PBC doesn't privde its pid to driver. */
		if (padapter->pid == 0)
			return;
		kill_pid(find_vpid(padapter->pid), SIGUSR1, 1);
	}
}

/* query rx phy status from fw.
 * Adhoc mode: beacon.
 * Infrastructure mode: beacon , data. */
static void query_fw_rx_phy_status(struct _adapter *padapter)
{
	u32 val32 = 0;
	int pollingcnts = 50;

	if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) == true) {
		r8712_write32(padapter, IOCMD_CTRL_REG, 0xf4000001);
		msleep(100);
		/* Wait FW complete IO Cmd */
		while ((r8712_read32(padapter, IOCMD_CTRL_REG)) &&
		       (pollingcnts > 0)) {
			pollingcnts--;
			msleep(20);
		}
		if (pollingcnts != 0)
			val32 = r8712_read32(padapter, IOCMD_DATA_REG);
		else /* time out */
			val32 = 0;
		val32 = val32 >> 4;
		padapter->recvpriv.fw_rssi =
			 (u8)r8712_signal_scale_mapping(val32);
	}
}

/* check mlme, hw, phy, or dynamic algorithm status. */
static void StatusWatchdogCallback(struct _adapter *padapter)
{
	check_hw_pbc(padapter);
	query_fw_rx_phy_status(padapter);
}

static void r871x_internal_cmd_hdl(struct _adapter *padapter, u8 *pbuf)
{
	struct drvint_cmd_parm *pdrvcmd;

	if (!pbuf)
		return;
	pdrvcmd = (struct drvint_cmd_parm *)pbuf;
	switch (pdrvcmd->i_cid) {
	case WDG_WK_CID:
		StatusWatchdogCallback(padapter);
		break;
	default:
		break;
	}
	kfree(pdrvcmd->pbuf);
}

static u8 read_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
{
	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj	*pcmd);
	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;

	/*  invoke cmd->callback function */
	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
	if (pcmd_callback == NULL)
		r8712_free_cmd_obj(pcmd);
	else
		pcmd_callback(padapter, pcmd);
	return H2C_SUCCESS;
}

static u8 write_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
{
	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj	*pcmd);
	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;

	/*  invoke cmd->callback function */
	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
	if (pcmd_callback == NULL)
		r8712_free_cmd_obj(pcmd);
	else
		pcmd_callback(padapter, pcmd);
	return H2C_SUCCESS;
}

static u8 read_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
{
	u32 val;
	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj	*pcmd);
	struct readBB_parm *prdbbparm;
	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;

	prdbbparm = (struct readBB_parm *)pcmd->parmbuf;
	if (pcmd->rsp && pcmd->rspsz > 0)
		memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
	if (pcmd_callback == NULL)
		r8712_free_cmd_obj(pcmd);
	else
		pcmd_callback(padapter, pcmd);
	return H2C_SUCCESS;
}

static u8 write_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
{
	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
	struct writeBB_parm *pwritebbparm;
	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;

	pwritebbparm = (struct writeBB_parm *)pcmd->parmbuf;
	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
	if (pcmd_callback == NULL)
		r8712_free_cmd_obj(pcmd);
	else
		pcmd_callback(padapter, pcmd);
	return H2C_SUCCESS;
}

static u8 read_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
{
	u32 val;
	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
	struct readRF_parm *prdrfparm;
	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;

	prdrfparm = (struct readRF_parm *)pcmd->parmbuf;
	if (pcmd->rsp && pcmd->rspsz > 0)
		memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
	if (pcmd_callback == NULL)
		r8712_free_cmd_obj(pcmd);
	else
		pcmd_callback(padapter, pcmd);
	return H2C_SUCCESS;
}

static u8 write_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
{
	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
	struct writeRF_parm *pwriterfparm;
	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;

	pwriterfparm = (struct writeRF_parm *)pcmd->parmbuf;
	pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
	if (pcmd_callback == NULL)
		r8712_free_cmd_obj(pcmd);
	else
		pcmd_callback(padapter, pcmd);
	return H2C_SUCCESS;
}

static u8 sys_suspend_hdl(struct _adapter *padapter, u8 *pbuf)
{
	struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
	struct usb_suspend_parm *psetusbsuspend;

	psetusbsuspend = (struct usb_suspend_parm *)pcmd->parmbuf;
	r8712_free_cmd_obj(pcmd);
	return H2C_SUCCESS;
}

static struct cmd_obj *cmd_hdl_filter(struct _adapter *padapter,
				      struct cmd_obj *pcmd)
{
	struct cmd_obj *pcmd_r;

	if (pcmd == NULL)
		return pcmd;
	pcmd_r = NULL;

	switch (pcmd->cmdcode) {
	case GEN_CMD_CODE(_Read_MACREG):
		read_macreg_hdl(padapter, (u8 *)pcmd);
		pcmd_r = pcmd;
		break;
	case GEN_CMD_CODE(_Write_MACREG):
		write_macreg_hdl(padapter, (u8 *)pcmd);
		pcmd_r = pcmd;
		break;
	case GEN_CMD_CODE(_Read_BBREG):
		read_bbreg_hdl(padapter, (u8 *)pcmd);
		break;
	case GEN_CMD_CODE(_Write_BBREG):
		write_bbreg_hdl(padapter, (u8 *)pcmd);
		break;
	case GEN_CMD_CODE(_Read_RFREG):
		read_rfreg_hdl(padapter, (u8 *)pcmd);
		break;
	case GEN_CMD_CODE(_Write_RFREG):
		write_rfreg_hdl(padapter, (u8 *)pcmd);
		break;
	case GEN_CMD_CODE(_SetUsbSuspend):
		sys_suspend_hdl(padapter, (u8 *)pcmd);
		break;
	case GEN_CMD_CODE(_JoinBss):
		r8712_joinbss_reset(padapter);
		/* Before set JoinBss_CMD to FW, driver must ensure FW is in
		 * PS_MODE_ACTIVE. Directly write rpwm to radio on and assign
		 * new pwr_mode to Driver, instead of use workitem to change
		 * state. */
		if (padapter->pwrctrlpriv.pwr_mode > PS_MODE_ACTIVE) {
			padapter->pwrctrlpriv.pwr_mode = PS_MODE_ACTIVE;
			_enter_pwrlock(&(padapter->pwrctrlpriv.lock));
			r8712_set_rpwm(padapter, PS_STATE_S4);
			up(&(padapter->pwrctrlpriv.lock));
		}
		pcmd_r = pcmd;
		break;
	case _DRV_INT_CMD_:
		r871x_internal_cmd_hdl(padapter, pcmd->parmbuf);
		r8712_free_cmd_obj(pcmd);
		pcmd_r = NULL;
		break;
	default:
		pcmd_r = pcmd;
		break;
	}
	return pcmd_r; /* if returning pcmd_r == NULL, pcmd must be free. */
}

static u8 check_cmd_fifo(struct _adapter *padapter, uint sz)
{
	u8 res = _SUCCESS;
	return res;
}

u8 r8712_fw_cmd(struct _adapter *pAdapter, u32 cmd)
{
	int pollingcnts = 50;

	r8712_write32(pAdapter, IOCMD_CTRL_REG, cmd);
	msleep(100);
	while ((0 != r8712_read32(pAdapter, IOCMD_CTRL_REG)) &&
	       (pollingcnts > 0)) {
		pollingcnts--;
		msleep(20);
	}
	if (pollingcnts == 0)
		return false;
	return true;
}

void r8712_fw_cmd_data(struct _adapter *pAdapter, u32 *value, u8 flag)
{
	if (flag == 0)	/* set */
		r8712_write32(pAdapter, IOCMD_DATA_REG, *value);
	else		/* query */
		*value = r8712_read32(pAdapter, IOCMD_DATA_REG);
}

int r8712_cmd_thread(void *context)
{
	struct cmd_obj *pcmd;
	unsigned int cmdsz, wr_sz, *pcmdbuf, *prspbuf;
	struct tx_desc *pdesc;
	void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
	struct _adapter *padapter = (struct _adapter *)context;
	struct	cmd_priv	*pcmdpriv = &(padapter->cmdpriv);

	thread_enter(padapter);
	while (1) {
		if ((_down_sema(&(pcmdpriv->cmd_queue_sema))) == _FAIL)
			break;
		if ((padapter->bDriverStopped == true) ||
		    (padapter->bSurpriseRemoved == true))
			break;
		if (r8712_register_cmd_alive(padapter) != _SUCCESS)
			continue;
_next:
		pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
		if (!(pcmd)) {
			r8712_unregister_cmd_alive(padapter);
			continue;
		}
		pcmdbuf = (unsigned int *)pcmdpriv->cmd_buf;
		prspbuf = (unsigned int *)pcmdpriv->rsp_buf;
		pdesc = (struct tx_desc *)pcmdbuf;
		memset(pdesc, 0, TXDESC_SIZE);
		pcmd = cmd_hdl_filter(padapter, pcmd);
		if (pcmd) { /* if pcmd != NULL, cmd will be handled by f/w */
			struct dvobj_priv *pdvobj = (struct dvobj_priv *)
						    &padapter->dvobjpriv;
			u8 blnPending = 0;
			pcmdpriv->cmd_issued_cnt++;
			cmdsz = _RND8((pcmd->cmdsz)); /* _RND8	*/
			wr_sz = TXDESC_SIZE + 8 + cmdsz;
			pdesc->txdw0 |= cpu_to_le32((wr_sz-TXDESC_SIZE) &
						     0x0000ffff);
			if (pdvobj->ishighspeed) {
				if ((wr_sz % 512) == 0)
					blnPending = 1;
			} else {
				if ((wr_sz % 64) == 0)
					blnPending = 1;
			}
			if (blnPending) /* 32 bytes for TX Desc - 8 offset */
				pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
						OFFSET_SZ + 8) << OFFSET_SHT) &
						0x00ff0000);
			else {
				pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
							      OFFSET_SZ) <<
							      OFFSET_SHT) &
							      0x00ff0000);
			}
			pdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
			pdesc->txdw1 |= cpu_to_le32((0x13 << QSEL_SHT) &
						    0x00001f00);
			pcmdbuf += (TXDESC_SIZE >> 2);
			*pcmdbuf = cpu_to_le32((cmdsz & 0x0000ffff) |
					       (pcmd->cmdcode << 16) |
					       (pcmdpriv->cmd_seq << 24));
			pcmdbuf += 2 ; /* 8 bytes aligment */
			memcpy((u8 *)pcmdbuf, pcmd->parmbuf, pcmd->cmdsz);
			while (check_cmd_fifo(padapter, wr_sz) == _FAIL) {
				if ((padapter->bDriverStopped == true) ||
				    (padapter->bSurpriseRemoved == true))
					break;
				msleep(100);
				continue;
			}
			if (blnPending)
				wr_sz += 8;   /* Append 8 bytes */
			r8712_write_mem(padapter, RTL8712_DMA_H2CCMD, wr_sz,
				       (u8 *)pdesc);
			pcmdpriv->cmd_seq++;
			if (pcmd->cmdcode == GEN_CMD_CODE(_CreateBss)) {
				pcmd->res = H2C_SUCCESS;
				pcmd_callback = cmd_callback[pcmd->
						cmdcode].callback;
				if (pcmd_callback)
					pcmd_callback(padapter, pcmd);
				continue;
			}
			if (pcmd->cmdcode == GEN_CMD_CODE(_SetPwrMode)) {
				if (padapter->pwrctrlpriv.bSleep) {
					_enter_pwrlock(&(padapter->
						       pwrctrlpriv.lock));
					r8712_set_rpwm(padapter, PS_STATE_S2);
					up(&padapter->pwrctrlpriv.lock);
				}
			}
			r8712_free_cmd_obj(pcmd);
			if (_queue_empty(&(pcmdpriv->cmd_queue))) {
				r8712_unregister_cmd_alive(padapter);
				continue;
			} else
				goto _next;
		} else
			goto _next;
		flush_signals_thread();
	}
	/* free all cmd_obj resources */
	do {
		pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
		if (pcmd == NULL)
			break;
		r8712_free_cmd_obj(pcmd);
	} while (1);
	up(&pcmdpriv->terminate_cmdthread_sema);
	thread_exit();
}

void r8712_event_handle(struct _adapter *padapter, uint *peventbuf)
{
	u8 evt_code, evt_seq;
	u16 evt_sz;
	void (*event_callback)(struct _adapter *dev, u8 *pbuf);
	struct	evt_priv *pevt_priv = &(padapter->evtpriv);

	if (peventbuf == NULL)
		goto _abort_event_;
	evt_sz = (u16)(le32_to_cpu(*peventbuf) & 0xffff);
	evt_seq = (u8)((le32_to_cpu(*peventbuf) >> 24) & 0x7f);
	evt_code = (u8)((le32_to_cpu(*peventbuf) >> 16) & 0xff);
	/* checking event sequence... */
	if ((evt_seq & 0x7f) != pevt_priv->event_seq) {
		pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
		goto _abort_event_;
	}
	/* checking if event code is valid */
	if (evt_code >= MAX_C2HEVT) {
		pevt_priv->event_seq = ((evt_seq+1) & 0x7f);
		goto _abort_event_;
	} else if ((evt_code == GEN_EVT_CODE(_Survey)) &&
		   (evt_sz > sizeof(struct wlan_bssid_ex))) {
		pevt_priv->event_seq = ((evt_seq+1)&0x7f);
		goto _abort_event_;
	}
	/* checking if event size match the event parm size */
	if ((wlanevents[evt_code].parmsize) &&
	    (wlanevents[evt_code].parmsize != evt_sz)) {
		pevt_priv->event_seq = ((evt_seq+1)&0x7f);
		goto _abort_event_;
	} else if ((evt_sz == 0) && (evt_code != GEN_EVT_CODE(_WPS_PBC))) {
		pevt_priv->event_seq = ((evt_seq+1)&0x7f);
		goto _abort_event_;
	}
	pevt_priv->event_seq++;	/* update evt_seq */
	if (pevt_priv->event_seq > 127)
		pevt_priv->event_seq = 0;
	peventbuf = peventbuf + 2; /* move to event content, 8 bytes aligment */
	if (peventbuf) {
		event_callback = wlanevents[evt_code].event_callback;
		if (event_callback)
			event_callback(padapter, (u8 *)peventbuf);
	}
	pevt_priv->evt_done_cnt++;
_abort_event_:
	return;
}