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