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:
24 * WLAN FAE <wlanfae@realtek.com>.
25 * Larry Finger <Larry.Finger@lwfinger.net>
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/module.h>
36 #include <linux/kref.h>
37 #include <linux/netdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/usb.h>
40 #include <linux/usb/ch9.h>
41 #include <linux/circ_buf.h>
42 #include <linux/uaccess.h>
43 #include <asm/byteorder.h>
44 #include <linux/atomic.h>
45 #include <linux/semaphore.h>
46 #include <linux/rtnetlink.h>
48 #include "osdep_service.h"
49 #include "drv_types.h"
50 #include "recv_osdep.h"
51 #include "mlme_osdep.h"
52 #include "rtl871x_ioctl_set.h"
54 static void check_hw_pbc(struct _adapter *padapter)
58 r8712_write8(padapter, MAC_PINMUX_CTRL, (GPIOMUX_EN | GPIOSEL_GPIO));
59 tmp1byte = r8712_read8(padapter, GPIO_IO_SEL);
60 tmp1byte &= ~(HAL_8192S_HW_GPIO_WPS_BIT);
61 r8712_write8(padapter, GPIO_IO_SEL, tmp1byte);
62 tmp1byte = r8712_read8(padapter, GPIO_CTRL);
65 if (tmp1byte & HAL_8192S_HW_GPIO_WPS_BIT) {
66 /* Here we only set bPbcPressed to true
67 * After trigger PBC, the variable will be set to false
69 DBG_8712("CheckPbcGPIO - PBC is pressed !!!!\n");
70 /* 0 is the default value and it means the application monitors
71 * the HW PBC doesn't provide its pid to driver.
73 if (padapter->pid == 0)
75 kill_pid(find_vpid(padapter->pid), SIGUSR1, 1);
79 /* query rx phy status from fw.
81 * Infrastructure mode: beacon , data.
83 static void query_fw_rx_phy_status(struct _adapter *padapter)
88 if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
89 r8712_write32(padapter, IOCMD_CTRL_REG, 0xf4000001);
91 /* Wait FW complete IO Cmd */
92 while ((r8712_read32(padapter, IOCMD_CTRL_REG)) &&
98 val32 = r8712_read32(padapter, IOCMD_DATA_REG);
102 padapter->recvpriv.fw_rssi =
103 (u8)r8712_signal_scale_mapping(val32);
107 /* check mlme, hw, phy, or dynamic algorithm status. */
108 static void StatusWatchdogCallback(struct _adapter *padapter)
110 check_hw_pbc(padapter);
111 query_fw_rx_phy_status(padapter);
114 static void r871x_internal_cmd_hdl(struct _adapter *padapter, u8 *pbuf)
116 struct drvint_cmd_parm *pdrvcmd;
120 pdrvcmd = (struct drvint_cmd_parm *)pbuf;
121 switch (pdrvcmd->i_cid) {
123 StatusWatchdogCallback(padapter);
128 kfree(pdrvcmd->pbuf);
131 static u8 read_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
133 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
134 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
136 /* invoke cmd->callback function */
137 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
139 r8712_free_cmd_obj(pcmd);
141 pcmd_callback(padapter, pcmd);
145 static u8 write_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
147 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
148 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
150 /* invoke cmd->callback function */
151 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
153 r8712_free_cmd_obj(pcmd);
155 pcmd_callback(padapter, pcmd);
159 static u8 read_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
161 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
163 r8712_free_cmd_obj(pcmd);
167 static u8 write_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
169 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
170 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
172 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
174 r8712_free_cmd_obj(pcmd);
176 pcmd_callback(padapter, pcmd);
180 static u8 read_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
183 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
184 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
186 if (pcmd->rsp && pcmd->rspsz > 0)
187 memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
188 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
190 r8712_free_cmd_obj(pcmd);
192 pcmd_callback(padapter, pcmd);
196 static u8 write_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
198 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
199 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
201 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
203 r8712_free_cmd_obj(pcmd);
205 pcmd_callback(padapter, pcmd);
209 static u8 sys_suspend_hdl(struct _adapter *padapter, u8 *pbuf)
211 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
213 r8712_free_cmd_obj(pcmd);
217 static struct cmd_obj *cmd_hdl_filter(struct _adapter *padapter,
218 struct cmd_obj *pcmd)
220 struct cmd_obj *pcmd_r;
226 switch (pcmd->cmdcode) {
227 case GEN_CMD_CODE(_Read_MACREG):
228 read_macreg_hdl(padapter, (u8 *)pcmd);
231 case GEN_CMD_CODE(_Write_MACREG):
232 write_macreg_hdl(padapter, (u8 *)pcmd);
235 case GEN_CMD_CODE(_Read_BBREG):
236 read_bbreg_hdl(padapter, (u8 *)pcmd);
238 case GEN_CMD_CODE(_Write_BBREG):
239 write_bbreg_hdl(padapter, (u8 *)pcmd);
241 case GEN_CMD_CODE(_Read_RFREG):
242 read_rfreg_hdl(padapter, (u8 *)pcmd);
244 case GEN_CMD_CODE(_Write_RFREG):
245 write_rfreg_hdl(padapter, (u8 *)pcmd);
247 case GEN_CMD_CODE(_SetUsbSuspend):
248 sys_suspend_hdl(padapter, (u8 *)pcmd);
250 case GEN_CMD_CODE(_JoinBss):
251 r8712_joinbss_reset(padapter);
252 /* Before set JoinBss_CMD to FW, driver must ensure FW is in
253 * PS_MODE_ACTIVE. Directly write rpwm to radio on and assign
254 * new pwr_mode to Driver, instead of use workitem to change
257 if (padapter->pwrctrlpriv.pwr_mode > PS_MODE_ACTIVE) {
258 padapter->pwrctrlpriv.pwr_mode = PS_MODE_ACTIVE;
259 mutex_lock(&padapter->pwrctrlpriv.mutex_lock);
260 r8712_set_rpwm(padapter, PS_STATE_S4);
261 mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
266 r871x_internal_cmd_hdl(padapter, pcmd->parmbuf);
267 r8712_free_cmd_obj(pcmd);
274 return pcmd_r; /* if returning pcmd_r == NULL, pcmd must be free. */
277 static u8 check_cmd_fifo(struct _adapter *padapter, uint sz)
282 u8 r8712_fw_cmd(struct _adapter *pAdapter, u32 cmd)
284 int pollingcnts = 50;
286 r8712_write32(pAdapter, IOCMD_CTRL_REG, cmd);
288 while ((r8712_read32(pAdapter, IOCMD_CTRL_REG != 0)) &&
293 if (pollingcnts == 0)
298 void r8712_fw_cmd_data(struct _adapter *pAdapter, u32 *value, u8 flag)
300 if (flag == 0) /* set */
301 r8712_write32(pAdapter, IOCMD_DATA_REG, *value);
303 *value = r8712_read32(pAdapter, IOCMD_DATA_REG);
306 int r8712_cmd_thread(void *context)
308 struct cmd_obj *pcmd;
309 unsigned int cmdsz, wr_sz, *pcmdbuf;
310 struct tx_desc *pdesc;
311 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
312 struct _adapter *padapter = context;
313 struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
315 allow_signal(SIGTERM);
317 if (wait_for_completion_interruptible(&pcmdpriv->cmd_queue_comp))
319 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
321 if (r8712_register_cmd_alive(padapter) != _SUCCESS)
324 pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
326 r8712_unregister_cmd_alive(padapter);
329 pcmdbuf = (unsigned int *)pcmdpriv->cmd_buf;
330 pdesc = (struct tx_desc *)pcmdbuf;
331 memset(pdesc, 0, TXDESC_SIZE);
332 pcmd = cmd_hdl_filter(padapter, pcmd);
333 if (pcmd) { /* if pcmd != NULL, cmd will be handled by f/w */
334 struct dvobj_priv *pdvobj = &padapter->dvobjpriv;
337 pcmdpriv->cmd_issued_cnt++;
338 cmdsz = round_up(pcmd->cmdsz, 8);
339 wr_sz = TXDESC_SIZE + 8 + cmdsz;
340 pdesc->txdw0 |= cpu_to_le32((wr_sz - TXDESC_SIZE) &
342 if (pdvobj->ishighspeed) {
343 if ((wr_sz % 512) == 0)
346 if ((wr_sz % 64) == 0)
349 if (blnPending) /* 32 bytes for TX Desc - 8 offset */
350 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
351 OFFSET_SZ + 8) << OFFSET_SHT) &
354 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
359 pdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
360 pdesc->txdw1 |= cpu_to_le32((0x13 << QSEL_SHT) &
362 pcmdbuf += (TXDESC_SIZE >> 2);
363 *pcmdbuf = cpu_to_le32((cmdsz & 0x0000ffff) |
364 (pcmd->cmdcode << 16) |
365 (pcmdpriv->cmd_seq << 24));
366 pcmdbuf += 2; /* 8 bytes alignment */
367 memcpy((u8 *)pcmdbuf, pcmd->parmbuf, pcmd->cmdsz);
368 while (check_cmd_fifo(padapter, wr_sz) == _FAIL) {
369 if (padapter->bDriverStopped ||
370 padapter->bSurpriseRemoved)
376 wr_sz += 8; /* Append 8 bytes */
377 r8712_write_mem(padapter, RTL8712_DMA_H2CCMD, wr_sz,
380 if (pcmd->cmdcode == GEN_CMD_CODE(_CreateBss)) {
381 pcmd->res = H2C_SUCCESS;
382 pcmd_callback = cmd_callback[pcmd->
385 pcmd_callback(padapter, pcmd);
388 if (pcmd->cmdcode == GEN_CMD_CODE(_SetPwrMode)) {
389 if (padapter->pwrctrlpriv.bSleep) {
390 mutex_lock(&padapter->
391 pwrctrlpriv.mutex_lock);
392 r8712_set_rpwm(padapter, PS_STATE_S2);
393 mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
396 r8712_free_cmd_obj(pcmd);
397 if (list_empty(&pcmdpriv->cmd_queue.queue)) {
398 r8712_unregister_cmd_alive(padapter);
406 flush_signals_thread();
408 /* free all cmd_obj resources */
410 pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
413 r8712_free_cmd_obj(pcmd);
415 complete(&pcmdpriv->terminate_cmdthread_comp);
419 void r8712_event_handle(struct _adapter *padapter, uint *peventbuf)
421 u8 evt_code, evt_seq;
423 void (*event_callback)(struct _adapter *dev, u8 *pbuf);
424 struct evt_priv *pevt_priv = &(padapter->evtpriv);
428 evt_sz = (u16)(le32_to_cpu(*peventbuf) & 0xffff);
429 evt_seq = (u8)((le32_to_cpu(*peventbuf) >> 24) & 0x7f);
430 evt_code = (u8)((le32_to_cpu(*peventbuf) >> 16) & 0xff);
431 /* checking event sequence... */
432 if ((evt_seq & 0x7f) != pevt_priv->event_seq) {
433 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
436 /* checking if event code is valid */
437 if (evt_code >= MAX_C2HEVT) {
438 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
440 } else if ((evt_code == GEN_EVT_CODE(_Survey)) &&
441 (evt_sz > sizeof(struct wlan_bssid_ex))) {
442 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
445 /* checking if event size match the event parm size */
446 if ((wlanevents[evt_code].parmsize) &&
447 (wlanevents[evt_code].parmsize != evt_sz)) {
448 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
450 } else if ((evt_sz == 0) && (evt_code != GEN_EVT_CODE(_WPS_PBC))) {
451 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
454 pevt_priv->event_seq++; /* update evt_seq */
455 if (pevt_priv->event_seq > 127)
456 pevt_priv->event_seq = 0;
457 /* move to event content, 8 bytes alignment */
458 peventbuf = peventbuf + 2;
459 event_callback = wlanevents[evt_code].event_callback;
461 event_callback(padapter, (u8 *)peventbuf);
462 pevt_priv->evt_done_cnt++;