GNU Linux-libre 4.9.333-gnu1
[releases.git] / drivers / staging / rtl8712 / rtl8712_cmd.c
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/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>
47
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"
53
54 static void check_hw_pbc(struct _adapter *padapter)
55 {
56         u8      tmp1byte;
57
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);
63         if (tmp1byte == 0xff)
64                 return;
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                  */
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.
72                  */
73                 if (padapter->pid == 0)
74                         return;
75                 kill_pid(find_vpid(padapter->pid), SIGUSR1, 1);
76         }
77 }
78
79 /* query rx phy status from fw.
80  * Adhoc mode: beacon.
81  * Infrastructure mode: beacon , data.
82  */
83 static void query_fw_rx_phy_status(struct _adapter *padapter)
84 {
85         u32 val32 = 0;
86         int pollingcnts = 50;
87
88         if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
89                 r8712_write32(padapter, IOCMD_CTRL_REG, 0xf4000001);
90                 msleep(100);
91                 /* Wait FW complete IO Cmd */
92                 while ((r8712_read32(padapter, IOCMD_CTRL_REG)) &&
93                        (pollingcnts > 0)) {
94                         pollingcnts--;
95                         msleep(20);
96                 }
97                 if (pollingcnts != 0)
98                         val32 = r8712_read32(padapter, IOCMD_DATA_REG);
99                 else /* time out */
100                         val32 = 0;
101                 val32 >>= 4;
102                 padapter->recvpriv.fw_rssi =
103                          (u8)r8712_signal_scale_mapping(val32);
104         }
105 }
106
107 /* check mlme, hw, phy, or dynamic algorithm status. */
108 static void StatusWatchdogCallback(struct _adapter *padapter)
109 {
110         check_hw_pbc(padapter);
111         query_fw_rx_phy_status(padapter);
112 }
113
114 static void r871x_internal_cmd_hdl(struct _adapter *padapter, u8 *pbuf)
115 {
116         struct drvint_cmd_parm *pdrvcmd;
117
118         if (!pbuf)
119                 return;
120         pdrvcmd = (struct drvint_cmd_parm *)pbuf;
121         switch (pdrvcmd->i_cid) {
122         case WDG_WK_CID:
123                 StatusWatchdogCallback(padapter);
124                 break;
125         default:
126                 break;
127         }
128         kfree(pdrvcmd->pbuf);
129 }
130
131 static u8 read_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
132 {
133         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
134
135         r8712_free_cmd_obj(pcmd);
136         return H2C_SUCCESS;
137 }
138
139 static u8 write_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
140 {
141         void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
142         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
143
144         pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
145         if (!pcmd_callback)
146                 r8712_free_cmd_obj(pcmd);
147         else
148                 pcmd_callback(padapter, pcmd);
149         return H2C_SUCCESS;
150 }
151
152 static u8 read_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
153 {
154         u32 val;
155         void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
156         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
157
158         if (pcmd->rsp && pcmd->rspsz > 0)
159                 memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
160         pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
161         if (!pcmd_callback)
162                 r8712_free_cmd_obj(pcmd);
163         else
164                 pcmd_callback(padapter, pcmd);
165         return H2C_SUCCESS;
166 }
167
168 static u8 write_rfreg_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
181 static u8 sys_suspend_hdl(struct _adapter *padapter, u8 *pbuf)
182 {
183         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
184
185         r8712_free_cmd_obj(pcmd);
186         return H2C_SUCCESS;
187 }
188
189 static struct cmd_obj *cmd_hdl_filter(struct _adapter *padapter,
190                                       struct cmd_obj *pcmd)
191 {
192         struct cmd_obj *pcmd_r;
193
194         if (!pcmd)
195                 return pcmd;
196         pcmd_r = NULL;
197
198         switch (pcmd->cmdcode) {
199         case GEN_CMD_CODE(_Read_BBREG):
200                 read_bbreg_hdl(padapter, (u8 *)pcmd);
201                 break;
202         case GEN_CMD_CODE(_Write_BBREG):
203                 write_bbreg_hdl(padapter, (u8 *)pcmd);
204                 break;
205         case GEN_CMD_CODE(_Read_RFREG):
206                 read_rfreg_hdl(padapter, (u8 *)pcmd);
207                 break;
208         case GEN_CMD_CODE(_Write_RFREG):
209                 write_rfreg_hdl(padapter, (u8 *)pcmd);
210                 break;
211         case GEN_CMD_CODE(_SetUsbSuspend):
212                 sys_suspend_hdl(padapter, (u8 *)pcmd);
213                 break;
214         case GEN_CMD_CODE(_JoinBss):
215                 r8712_joinbss_reset(padapter);
216                 /* Before set JoinBss_CMD to FW, driver must ensure FW is in
217                  * PS_MODE_ACTIVE. Directly write rpwm to radio on and assign
218                  * new pwr_mode to Driver, instead of use workitem to change
219                  * state.
220                  */
221                 if (padapter->pwrctrlpriv.pwr_mode > PS_MODE_ACTIVE) {
222                         padapter->pwrctrlpriv.pwr_mode = PS_MODE_ACTIVE;
223                         mutex_lock(&padapter->pwrctrlpriv.mutex_lock);
224                         r8712_set_rpwm(padapter, PS_STATE_S4);
225                         mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
226                 }
227                 pcmd_r = pcmd;
228                 break;
229         case _DRV_INT_CMD_:
230                 r871x_internal_cmd_hdl(padapter, pcmd->parmbuf);
231                 r8712_free_cmd_obj(pcmd);
232                 pcmd_r = NULL;
233                 break;
234         default:
235                 pcmd_r = pcmd;
236                 break;
237         }
238         return pcmd_r; /* if returning pcmd_r == NULL, pcmd must be free. */
239 }
240
241 static u8 check_cmd_fifo(struct _adapter *padapter, uint sz)
242 {
243         return _SUCCESS;
244 }
245
246 u8 r8712_fw_cmd(struct _adapter *pAdapter, u32 cmd)
247 {
248         int pollingcnts = 50;
249
250         r8712_write32(pAdapter, IOCMD_CTRL_REG, cmd);
251         msleep(100);
252         while ((r8712_read32(pAdapter, IOCMD_CTRL_REG != 0)) &&
253                (pollingcnts > 0)) {
254                 pollingcnts--;
255                 msleep(20);
256         }
257         if (pollingcnts == 0)
258                 return false;
259         return true;
260 }
261
262 void r8712_fw_cmd_data(struct _adapter *pAdapter, u32 *value, u8 flag)
263 {
264         if (flag == 0)  /* set */
265                 r8712_write32(pAdapter, IOCMD_DATA_REG, *value);
266         else            /* query */
267                 *value = r8712_read32(pAdapter, IOCMD_DATA_REG);
268 }
269
270 int r8712_cmd_thread(void *context)
271 {
272         struct cmd_obj *pcmd;
273         unsigned int cmdsz, wr_sz, *pcmdbuf;
274         struct tx_desc *pdesc;
275         void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
276         struct _adapter *padapter = context;
277         struct  cmd_priv        *pcmdpriv = &(padapter->cmdpriv);
278
279         allow_signal(SIGTERM);
280         while (1) {
281                 if (wait_for_completion_interruptible(&pcmdpriv->cmd_queue_comp))
282                         break;
283                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
284                         break;
285                 if (r8712_register_cmd_alive(padapter) != _SUCCESS)
286                         continue;
287 _next:
288                 pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
289                 if (!(pcmd)) {
290                         r8712_unregister_cmd_alive(padapter);
291                         continue;
292                 }
293                 pcmdbuf = (unsigned int *)pcmdpriv->cmd_buf;
294                 pdesc = (struct tx_desc *)pcmdbuf;
295                 memset(pdesc, 0, TXDESC_SIZE);
296                 pcmd = cmd_hdl_filter(padapter, pcmd);
297                 if (pcmd) { /* if pcmd != NULL, cmd will be handled by f/w */
298                         struct dvobj_priv *pdvobj = &padapter->dvobjpriv;
299                         u8 blnPending = 0;
300
301                         pcmdpriv->cmd_issued_cnt++;
302                         cmdsz = round_up(pcmd->cmdsz, 8);
303                         wr_sz = TXDESC_SIZE + 8 + cmdsz;
304                         pdesc->txdw0 |= cpu_to_le32((wr_sz - TXDESC_SIZE) &
305                                                      0x0000ffff);
306                         if (pdvobj->ishighspeed) {
307                                 if ((wr_sz % 512) == 0)
308                                         blnPending = 1;
309                         } else {
310                                 if ((wr_sz % 64) == 0)
311                                         blnPending = 1;
312                         }
313                         if (blnPending) /* 32 bytes for TX Desc - 8 offset */
314                                 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
315                                                 OFFSET_SZ + 8) << OFFSET_SHT) &
316                                                 0x00ff0000);
317                         else {
318                                 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
319                                                               OFFSET_SZ) <<
320                                                               OFFSET_SHT) &
321                                                               0x00ff0000);
322                         }
323                         pdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
324                         pdesc->txdw1 |= cpu_to_le32((0x13 << QSEL_SHT) &
325                                                     0x00001f00);
326                         pcmdbuf += (TXDESC_SIZE >> 2);
327                         *pcmdbuf = cpu_to_le32((cmdsz & 0x0000ffff) |
328                                                (pcmd->cmdcode << 16) |
329                                                (pcmdpriv->cmd_seq << 24));
330                         pcmdbuf += 2; /* 8 bytes alignment */
331                         memcpy((u8 *)pcmdbuf, pcmd->parmbuf, pcmd->cmdsz);
332                         while (check_cmd_fifo(padapter, wr_sz) == _FAIL) {
333                                 if (padapter->bDriverStopped ||
334                                     padapter->bSurpriseRemoved)
335                                         break;
336                                 msleep(100);
337                                 continue;
338                         }
339                         if (blnPending)
340                                 wr_sz += 8;   /* Append 8 bytes */
341                         r8712_write_mem(padapter, RTL8712_DMA_H2CCMD, wr_sz,
342                                        (u8 *)pdesc);
343                         pcmdpriv->cmd_seq++;
344                         if (pcmd->cmdcode == GEN_CMD_CODE(_CreateBss)) {
345                                 pcmd->res = H2C_SUCCESS;
346                                 pcmd_callback = cmd_callback[pcmd->
347                                                 cmdcode].callback;
348                                 if (pcmd_callback)
349                                         pcmd_callback(padapter, pcmd);
350                                 continue;
351                         }
352                         if (pcmd->cmdcode == GEN_CMD_CODE(_SetPwrMode)) {
353                                 if (padapter->pwrctrlpriv.bSleep) {
354                                         mutex_lock(&padapter->
355                                                        pwrctrlpriv.mutex_lock);
356                                         r8712_set_rpwm(padapter, PS_STATE_S2);
357                                         mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
358                                 }
359                         }
360                         r8712_free_cmd_obj(pcmd);
361                         if (list_empty(&pcmdpriv->cmd_queue.queue)) {
362                                 r8712_unregister_cmd_alive(padapter);
363                                 continue;
364                         } else {
365                                 goto _next;
366                         }
367                 } else {
368                         goto _next;
369                 }
370                 flush_signals_thread();
371         }
372         /* free all cmd_obj resources */
373         do {
374                 pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
375                 if (!pcmd)
376                         break;
377                 r8712_free_cmd_obj(pcmd);
378         } while (1);
379         complete(&pcmdpriv->terminate_cmdthread_comp);
380         thread_exit();
381 }
382
383 void r8712_event_handle(struct _adapter *padapter, uint *peventbuf)
384 {
385         u8 evt_code, evt_seq;
386         u16 evt_sz;
387         void (*event_callback)(struct _adapter *dev, u8 *pbuf);
388         struct  evt_priv *pevt_priv = &(padapter->evtpriv);
389
390         if (!peventbuf)
391                 goto _abort_event_;
392         evt_sz = (u16)(le32_to_cpu(*peventbuf) & 0xffff);
393         evt_seq = (u8)((le32_to_cpu(*peventbuf) >> 24) & 0x7f);
394         evt_code = (u8)((le32_to_cpu(*peventbuf) >> 16) & 0xff);
395         /* checking event sequence... */
396         if ((evt_seq & 0x7f) != pevt_priv->event_seq) {
397                 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
398                 goto _abort_event_;
399         }
400         /* checking if event code is valid */
401         if (evt_code >= MAX_C2HEVT) {
402                 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
403                 goto _abort_event_;
404         } else if ((evt_code == GEN_EVT_CODE(_Survey)) &&
405                    (evt_sz > sizeof(struct wlan_bssid_ex))) {
406                 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
407                 goto _abort_event_;
408         }
409         /* checking if event size match the event parm size */
410         if ((wlanevents[evt_code].parmsize) &&
411             (wlanevents[evt_code].parmsize != evt_sz)) {
412                 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
413                 goto _abort_event_;
414         } else if ((evt_sz == 0) && (evt_code != GEN_EVT_CODE(_WPS_PBC))) {
415                 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
416                 goto _abort_event_;
417         }
418         pevt_priv->event_seq++; /* update evt_seq */
419         if (pevt_priv->event_seq > 127)
420                 pevt_priv->event_seq = 0;
421         /* move to event content, 8 bytes alignment */
422         peventbuf = peventbuf + 2;
423         event_callback = wlanevents[evt_code].event_callback;
424         if (event_callback)
425                 event_callback(padapter, (u8 *)peventbuf);
426         pevt_priv->evt_done_cnt++;
427 _abort_event_:
428         return;
429 }