1 /******************************************************************************
4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5 * Linux device driver for RTL8192SU
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.
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
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
20 * Modifications for inclusion into the Linux staging tree are
21 * Copyright(c) 2010 Larry Finger. All rights reserved.
23 * Contact information:
27 ******************************************************************************/
29 #define _RTL8712_CMD_C_
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>
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"
55 static void check_hw_pbc(struct _adapter *padapter)
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);
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
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.
74 if (padapter->pid == 0)
76 kill_pid(find_vpid(padapter->pid), SIGUSR1, 1);
80 /* query rx phy status from fw.
82 * Infrastructure mode: beacon , data.
84 static void query_fw_rx_phy_status(struct _adapter *padapter)
89 if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
90 r8712_write32(padapter, IOCMD_CTRL_REG, 0xf4000001);
92 /* Wait FW complete IO Cmd */
93 while ((r8712_read32(padapter, IOCMD_CTRL_REG)) &&
99 val32 = r8712_read32(padapter, IOCMD_DATA_REG);
103 padapter->recvpriv.fw_rssi =
104 (u8)r8712_signal_scale_mapping(val32);
108 /* check mlme, hw, phy, or dynamic algorithm status. */
109 static void StatusWatchdogCallback(struct _adapter *padapter)
111 check_hw_pbc(padapter);
112 query_fw_rx_phy_status(padapter);
115 static void r871x_internal_cmd_hdl(struct _adapter *padapter, u8 *pbuf)
117 struct drvint_cmd_parm *pdrvcmd;
121 pdrvcmd = (struct drvint_cmd_parm *)pbuf;
122 switch (pdrvcmd->i_cid) {
124 StatusWatchdogCallback(padapter);
129 kfree(pdrvcmd->pbuf);
132 static u8 read_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
134 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
135 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
137 /* invoke cmd->callback function */
138 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
140 r8712_free_cmd_obj(pcmd);
142 pcmd_callback(padapter, pcmd);
146 static u8 write_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
148 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
149 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
151 /* invoke cmd->callback function */
152 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
154 r8712_free_cmd_obj(pcmd);
156 pcmd_callback(padapter, pcmd);
160 static u8 read_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
163 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
164 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
166 if (pcmd->rsp && pcmd->rspsz > 0)
167 memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
168 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
170 r8712_free_cmd_obj(pcmd);
172 pcmd_callback(padapter, pcmd);
176 static u8 write_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
178 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
179 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
181 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
183 r8712_free_cmd_obj(pcmd);
185 pcmd_callback(padapter, pcmd);
189 static u8 read_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
192 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
193 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
195 if (pcmd->rsp && pcmd->rspsz > 0)
196 memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
197 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
199 r8712_free_cmd_obj(pcmd);
201 pcmd_callback(padapter, pcmd);
205 static u8 write_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
207 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
208 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
210 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
212 r8712_free_cmd_obj(pcmd);
214 pcmd_callback(padapter, pcmd);
218 static u8 sys_suspend_hdl(struct _adapter *padapter, u8 *pbuf)
220 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
222 r8712_free_cmd_obj(pcmd);
226 static struct cmd_obj *cmd_hdl_filter(struct _adapter *padapter,
227 struct cmd_obj *pcmd)
229 struct cmd_obj *pcmd_r;
235 switch (pcmd->cmdcode) {
236 case GEN_CMD_CODE(_Read_MACREG):
237 read_macreg_hdl(padapter, (u8 *)pcmd);
240 case GEN_CMD_CODE(_Write_MACREG):
241 write_macreg_hdl(padapter, (u8 *)pcmd);
244 case GEN_CMD_CODE(_Read_BBREG):
245 read_bbreg_hdl(padapter, (u8 *)pcmd);
247 case GEN_CMD_CODE(_Write_BBREG):
248 write_bbreg_hdl(padapter, (u8 *)pcmd);
250 case GEN_CMD_CODE(_Read_RFREG):
251 read_rfreg_hdl(padapter, (u8 *)pcmd);
253 case GEN_CMD_CODE(_Write_RFREG):
254 write_rfreg_hdl(padapter, (u8 *)pcmd);
256 case GEN_CMD_CODE(_SetUsbSuspend):
257 sys_suspend_hdl(padapter, (u8 *)pcmd);
259 case GEN_CMD_CODE(_JoinBss):
260 r8712_joinbss_reset(padapter);
261 /* Before set JoinBss_CMD to FW, driver must ensure FW is in
262 * PS_MODE_ACTIVE. Directly write rpwm to radio on and assign
263 * new pwr_mode to Driver, instead of use workitem to change
266 if (padapter->pwrctrlpriv.pwr_mode > PS_MODE_ACTIVE) {
267 padapter->pwrctrlpriv.pwr_mode = PS_MODE_ACTIVE;
268 mutex_lock(&padapter->pwrctrlpriv.mutex_lock);
269 r8712_set_rpwm(padapter, PS_STATE_S4);
270 mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
275 r871x_internal_cmd_hdl(padapter, pcmd->parmbuf);
276 r8712_free_cmd_obj(pcmd);
283 return pcmd_r; /* if returning pcmd_r == NULL, pcmd must be free. */
286 static u8 check_cmd_fifo(struct _adapter *padapter, uint sz)
291 u8 r8712_fw_cmd(struct _adapter *pAdapter, u32 cmd)
293 int pollingcnts = 50;
295 r8712_write32(pAdapter, IOCMD_CTRL_REG, cmd);
297 while ((r8712_read32(pAdapter, IOCMD_CTRL_REG != 0)) &&
302 if (pollingcnts == 0)
307 void r8712_fw_cmd_data(struct _adapter *pAdapter, u32 *value, u8 flag)
309 if (flag == 0) /* set */
310 r8712_write32(pAdapter, IOCMD_DATA_REG, *value);
312 *value = r8712_read32(pAdapter, IOCMD_DATA_REG);
315 int r8712_cmd_thread(void *context)
317 struct cmd_obj *pcmd;
318 unsigned int cmdsz, wr_sz;
320 struct tx_desc *pdesc;
321 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
322 struct _adapter *padapter = context;
323 struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
324 struct completion *cmd_queue_comp =
325 &pcmdpriv->cmd_queue_comp;
326 struct mutex *pwctrl_lock = &padapter->pwrctrlpriv.mutex_lock;
328 allow_signal(SIGTERM);
330 if (wait_for_completion_interruptible(cmd_queue_comp))
332 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
334 if (r8712_register_cmd_alive(padapter) != _SUCCESS)
337 pcmd = r8712_dequeue_cmd(&pcmdpriv->cmd_queue);
339 r8712_unregister_cmd_alive(padapter);
342 pcmdbuf = (__le32 *)pcmdpriv->cmd_buf;
343 pdesc = (struct tx_desc *)pcmdbuf;
344 memset(pdesc, 0, TXDESC_SIZE);
345 pcmd = cmd_hdl_filter(padapter, pcmd);
346 if (pcmd) { /* if pcmd != NULL, cmd will be handled by f/w */
347 struct dvobj_priv *pdvobj = &padapter->dvobjpriv;
349 u16 cmdcode = pcmd->cmdcode;
351 pcmdpriv->cmd_issued_cnt++;
352 cmdsz = round_up(pcmd->cmdsz, 8);
353 wr_sz = TXDESC_SIZE + 8 + cmdsz;
354 pdesc->txdw0 |= cpu_to_le32((wr_sz - TXDESC_SIZE) &
356 if (pdvobj->ishighspeed) {
357 if ((wr_sz % 512) == 0)
360 if ((wr_sz % 64) == 0)
363 if (blnPending) { /* 32 bytes for TX Desc - 8 offset */
364 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
365 OFFSET_SZ + 8) << OFFSET_SHT) &
368 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
373 pdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
374 pdesc->txdw1 |= cpu_to_le32((0x13 << QSEL_SHT) &
376 pcmdbuf += (TXDESC_SIZE >> 2);
377 *pcmdbuf = cpu_to_le32((cmdsz & 0x0000ffff) |
378 (pcmd->cmdcode << 16) |
379 (pcmdpriv->cmd_seq << 24));
380 pcmdbuf += 2; /* 8 bytes alignment */
381 memcpy((u8 *)pcmdbuf, pcmd->parmbuf, pcmd->cmdsz);
382 while (check_cmd_fifo(padapter, wr_sz) == _FAIL) {
383 if (padapter->bDriverStopped ||
384 padapter->bSurpriseRemoved)
390 wr_sz += 8; /* Append 8 bytes */
391 r8712_write_mem(padapter, RTL8712_DMA_H2CCMD, wr_sz,
394 if (cmdcode == GEN_CMD_CODE(_CreateBss)) {
395 pcmd->res = H2C_SUCCESS;
396 pcmd_callback = cmd_callback[cmdcode].callback;
398 pcmd_callback(padapter, pcmd);
401 if (cmdcode == GEN_CMD_CODE(_SetPwrMode)) {
402 if (padapter->pwrctrlpriv.bSleep) {
403 mutex_lock(pwctrl_lock);
404 r8712_set_rpwm(padapter, PS_STATE_S2);
405 mutex_unlock(pwctrl_lock);
408 r8712_free_cmd_obj(pcmd);
409 if (list_empty(&pcmdpriv->cmd_queue.queue)) {
410 r8712_unregister_cmd_alive(padapter);
418 flush_signals_thread();
420 /* free all cmd_obj resources */
422 pcmd = r8712_dequeue_cmd(&pcmdpriv->cmd_queue);
425 r8712_free_cmd_obj(pcmd);
427 complete(&pcmdpriv->terminate_cmdthread_comp);
431 void r8712_event_handle(struct _adapter *padapter, __le32 *peventbuf)
433 u8 evt_code, evt_seq;
435 void (*event_callback)(struct _adapter *dev, u8 *pbuf);
436 struct evt_priv *pevt_priv = &padapter->evtpriv;
440 evt_sz = (u16)(le32_to_cpu(*peventbuf) & 0xffff);
441 evt_seq = (u8)((le32_to_cpu(*peventbuf) >> 24) & 0x7f);
442 evt_code = (u8)((le32_to_cpu(*peventbuf) >> 16) & 0xff);
443 /* checking event sequence... */
444 if ((evt_seq & 0x7f) != pevt_priv->event_seq) {
445 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
448 /* checking if event code is valid */
449 if (evt_code >= MAX_C2HEVT) {
450 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
452 } else if ((evt_code == GEN_EVT_CODE(_Survey)) &&
453 (evt_sz > sizeof(struct wlan_bssid_ex))) {
454 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
457 /* checking if event size match the event parm size */
458 if ((wlanevents[evt_code].parmsize) &&
459 (wlanevents[evt_code].parmsize != evt_sz)) {
460 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
462 } else if ((evt_sz == 0) && (evt_code != GEN_EVT_CODE(_WPS_PBC))) {
463 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
466 pevt_priv->event_seq++; /* update evt_seq */
467 if (pevt_priv->event_seq > 127)
468 pevt_priv->event_seq = 0;
469 /* move to event content, 8 bytes alignment */
470 peventbuf = peventbuf + 2;
471 event_callback = wlanevents[evt_code].event_callback;
473 event_callback(padapter, (u8 *)peventbuf);
474 pevt_priv->evt_done_cnt++;