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 */
68 DBG_8712("CheckPbcGPIO - PBC is pressed !!!!\n");
69 /* 0 is the default value and it means the application monitors
70 * the HW PBC doesn't provide its pid to driver. */
71 if (padapter->pid == 0)
73 kill_pid(find_vpid(padapter->pid), SIGUSR1, 1);
77 /* query rx phy status from fw.
79 * Infrastructure mode: beacon , data. */
80 static void query_fw_rx_phy_status(struct _adapter *padapter)
85 if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) == true) {
86 r8712_write32(padapter, IOCMD_CTRL_REG, 0xf4000001);
88 /* Wait FW complete IO Cmd */
89 while ((r8712_read32(padapter, IOCMD_CTRL_REG)) &&
95 val32 = r8712_read32(padapter, IOCMD_DATA_REG);
99 padapter->recvpriv.fw_rssi =
100 (u8)r8712_signal_scale_mapping(val32);
104 /* check mlme, hw, phy, or dynamic algorithm status. */
105 static void StatusWatchdogCallback(struct _adapter *padapter)
107 check_hw_pbc(padapter);
108 query_fw_rx_phy_status(padapter);
111 static void r871x_internal_cmd_hdl(struct _adapter *padapter, u8 *pbuf)
113 struct drvint_cmd_parm *pdrvcmd;
117 pdrvcmd = (struct drvint_cmd_parm *)pbuf;
118 switch (pdrvcmd->i_cid) {
120 StatusWatchdogCallback(padapter);
125 kfree(pdrvcmd->pbuf);
128 static u8 read_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
130 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
131 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
133 /* invoke cmd->callback function */
134 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
135 if (pcmd_callback == NULL)
136 r8712_free_cmd_obj(pcmd);
138 pcmd_callback(padapter, pcmd);
142 static u8 write_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
144 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
145 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
147 /* invoke cmd->callback function */
148 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
149 if (pcmd_callback == NULL)
150 r8712_free_cmd_obj(pcmd);
152 pcmd_callback(padapter, pcmd);
156 static u8 read_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
159 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
160 struct readBB_parm *prdbbparm;
161 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
163 prdbbparm = (struct readBB_parm *)pcmd->parmbuf;
164 if (pcmd->rsp && pcmd->rspsz > 0)
165 memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
166 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
167 if (pcmd_callback == NULL)
168 r8712_free_cmd_obj(pcmd);
170 pcmd_callback(padapter, pcmd);
174 static u8 write_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
176 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
177 struct writeBB_parm *pwritebbparm;
178 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
180 pwritebbparm = (struct writeBB_parm *)pcmd->parmbuf;
181 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
182 if (pcmd_callback == NULL)
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 readRF_parm *prdrfparm;
194 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
196 prdrfparm = (struct readRF_parm *)pcmd->parmbuf;
197 if (pcmd->rsp && pcmd->rspsz > 0)
198 memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
199 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
200 if (pcmd_callback == NULL)
201 r8712_free_cmd_obj(pcmd);
203 pcmd_callback(padapter, pcmd);
207 static u8 write_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
209 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
210 struct writeRF_parm *pwriterfparm;
211 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
213 pwriterfparm = (struct writeRF_parm *)pcmd->parmbuf;
214 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
215 if (pcmd_callback == NULL)
216 r8712_free_cmd_obj(pcmd);
218 pcmd_callback(padapter, pcmd);
222 static u8 sys_suspend_hdl(struct _adapter *padapter, u8 *pbuf)
224 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
225 struct usb_suspend_parm *psetusbsuspend;
227 psetusbsuspend = (struct usb_suspend_parm *)pcmd->parmbuf;
228 r8712_free_cmd_obj(pcmd);
232 static struct cmd_obj *cmd_hdl_filter(struct _adapter *padapter,
233 struct cmd_obj *pcmd)
235 struct cmd_obj *pcmd_r;
241 switch (pcmd->cmdcode) {
242 case GEN_CMD_CODE(_Read_MACREG):
243 read_macreg_hdl(padapter, (u8 *)pcmd);
246 case GEN_CMD_CODE(_Write_MACREG):
247 write_macreg_hdl(padapter, (u8 *)pcmd);
250 case GEN_CMD_CODE(_Read_BBREG):
251 read_bbreg_hdl(padapter, (u8 *)pcmd);
253 case GEN_CMD_CODE(_Write_BBREG):
254 write_bbreg_hdl(padapter, (u8 *)pcmd);
256 case GEN_CMD_CODE(_Read_RFREG):
257 read_rfreg_hdl(padapter, (u8 *)pcmd);
259 case GEN_CMD_CODE(_Write_RFREG):
260 write_rfreg_hdl(padapter, (u8 *)pcmd);
262 case GEN_CMD_CODE(_SetUsbSuspend):
263 sys_suspend_hdl(padapter, (u8 *)pcmd);
265 case GEN_CMD_CODE(_JoinBss):
266 r8712_joinbss_reset(padapter);
267 /* Before set JoinBss_CMD to FW, driver must ensure FW is in
268 * PS_MODE_ACTIVE. Directly write rpwm to radio on and assign
269 * new pwr_mode to Driver, instead of use workitem to change
271 if (padapter->pwrctrlpriv.pwr_mode > PS_MODE_ACTIVE) {
272 padapter->pwrctrlpriv.pwr_mode = PS_MODE_ACTIVE;
273 _enter_pwrlock(&(padapter->pwrctrlpriv.lock));
274 r8712_set_rpwm(padapter, PS_STATE_S4);
275 up(&(padapter->pwrctrlpriv.lock));
280 r871x_internal_cmd_hdl(padapter, pcmd->parmbuf);
281 r8712_free_cmd_obj(pcmd);
288 return pcmd_r; /* if returning pcmd_r == NULL, pcmd must be free. */
291 static u8 check_cmd_fifo(struct _adapter *padapter, uint sz)
296 u8 r8712_fw_cmd(struct _adapter *pAdapter, u32 cmd)
298 int pollingcnts = 50;
300 r8712_write32(pAdapter, IOCMD_CTRL_REG, cmd);
302 while ((0 != r8712_read32(pAdapter, IOCMD_CTRL_REG)) &&
307 if (pollingcnts == 0)
312 void r8712_fw_cmd_data(struct _adapter *pAdapter, u32 *value, u8 flag)
314 if (flag == 0) /* set */
315 r8712_write32(pAdapter, IOCMD_DATA_REG, *value);
317 *value = r8712_read32(pAdapter, IOCMD_DATA_REG);
320 int r8712_cmd_thread(void *context)
322 struct cmd_obj *pcmd;
323 unsigned int cmdsz, wr_sz, *pcmdbuf, *prspbuf;
324 struct tx_desc *pdesc;
325 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
326 struct _adapter *padapter = (struct _adapter *)context;
327 struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
329 thread_enter(padapter);
331 if ((_down_sema(&(pcmdpriv->cmd_queue_sema))) == _FAIL)
333 if ((padapter->bDriverStopped == true) ||
334 (padapter->bSurpriseRemoved == true))
336 if (r8712_register_cmd_alive(padapter) != _SUCCESS)
339 pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
341 r8712_unregister_cmd_alive(padapter);
344 pcmdbuf = (unsigned int *)pcmdpriv->cmd_buf;
345 prspbuf = (unsigned int *)pcmdpriv->rsp_buf;
346 pdesc = (struct tx_desc *)pcmdbuf;
347 memset(pdesc, 0, TXDESC_SIZE);
348 pcmd = cmd_hdl_filter(padapter, pcmd);
349 if (pcmd) { /* if pcmd != NULL, cmd will be handled by f/w */
350 struct dvobj_priv *pdvobj = (struct dvobj_priv *)
351 &padapter->dvobjpriv;
353 pcmdpriv->cmd_issued_cnt++;
354 cmdsz = _RND8((pcmd->cmdsz)); /* _RND8 */
355 wr_sz = TXDESC_SIZE + 8 + cmdsz;
356 pdesc->txdw0 |= cpu_to_le32((wr_sz-TXDESC_SIZE) &
358 if (pdvobj->ishighspeed) {
359 if ((wr_sz % 512) == 0)
362 if ((wr_sz % 64) == 0)
365 if (blnPending) /* 32 bytes for TX Desc - 8 offset */
366 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
367 OFFSET_SZ + 8) << OFFSET_SHT) &
370 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
375 pdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
376 pdesc->txdw1 |= cpu_to_le32((0x13 << QSEL_SHT) &
378 pcmdbuf += (TXDESC_SIZE >> 2);
379 *pcmdbuf = cpu_to_le32((cmdsz & 0x0000ffff) |
380 (pcmd->cmdcode << 16) |
381 (pcmdpriv->cmd_seq << 24));
382 pcmdbuf += 2; /* 8 bytes alignment */
383 memcpy((u8 *)pcmdbuf, pcmd->parmbuf, pcmd->cmdsz);
384 while (check_cmd_fifo(padapter, wr_sz) == _FAIL) {
385 if ((padapter->bDriverStopped == true) ||
386 (padapter->bSurpriseRemoved == true))
392 wr_sz += 8; /* Append 8 bytes */
393 r8712_write_mem(padapter, RTL8712_DMA_H2CCMD, wr_sz,
396 if (pcmd->cmdcode == GEN_CMD_CODE(_CreateBss)) {
397 pcmd->res = H2C_SUCCESS;
398 pcmd_callback = cmd_callback[pcmd->
401 pcmd_callback(padapter, pcmd);
404 if (pcmd->cmdcode == GEN_CMD_CODE(_SetPwrMode)) {
405 if (padapter->pwrctrlpriv.bSleep) {
406 _enter_pwrlock(&(padapter->
408 r8712_set_rpwm(padapter, PS_STATE_S2);
409 up(&padapter->pwrctrlpriv.lock);
412 r8712_free_cmd_obj(pcmd);
413 if (_queue_empty(&(pcmdpriv->cmd_queue))) {
414 r8712_unregister_cmd_alive(padapter);
420 flush_signals_thread();
422 /* free all cmd_obj resources */
424 pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
427 r8712_free_cmd_obj(pcmd);
429 up(&pcmdpriv->terminate_cmdthread_sema);
433 void r8712_event_handle(struct _adapter *padapter, uint *peventbuf)
435 u8 evt_code, evt_seq;
437 void (*event_callback)(struct _adapter *dev, u8 *pbuf);
438 struct evt_priv *pevt_priv = &(padapter->evtpriv);
440 if (peventbuf == NULL)
442 evt_sz = (u16)(le32_to_cpu(*peventbuf) & 0xffff);
443 evt_seq = (u8)((le32_to_cpu(*peventbuf) >> 24) & 0x7f);
444 evt_code = (u8)((le32_to_cpu(*peventbuf) >> 16) & 0xff);
445 /* checking event sequence... */
446 if ((evt_seq & 0x7f) != pevt_priv->event_seq) {
447 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
450 /* checking if event code is valid */
451 if (evt_code >= MAX_C2HEVT) {
452 pevt_priv->event_seq = ((evt_seq+1) & 0x7f);
454 } else if ((evt_code == GEN_EVT_CODE(_Survey)) &&
455 (evt_sz > sizeof(struct wlan_bssid_ex))) {
456 pevt_priv->event_seq = ((evt_seq+1)&0x7f);
459 /* checking if event size match the event parm size */
460 if ((wlanevents[evt_code].parmsize) &&
461 (wlanevents[evt_code].parmsize != evt_sz)) {
462 pevt_priv->event_seq = ((evt_seq+1)&0x7f);
464 } else if ((evt_sz == 0) && (evt_code != GEN_EVT_CODE(_WPS_PBC))) {
465 pevt_priv->event_seq = ((evt_seq+1)&0x7f);
468 pevt_priv->event_seq++; /* update evt_seq */
469 if (pevt_priv->event_seq > 127)
470 pevt_priv->event_seq = 0;
471 peventbuf = peventbuf + 2; /* move to event content, 8 bytes alignment */
472 event_callback = wlanevents[evt_code].event_callback;
474 event_callback(padapter, (u8 *)peventbuf);
475 pevt_priv->evt_done_cnt++;