Merge branches 'misc', 'sa1100-for-next' and 'spectre' into for-linus
[linux-2.6-microblaze.git] / drivers / staging / rtl8712 / rtl8712_cmd.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  * rtl8712_cmd.c
4  *
5  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
6  * Linux device driver for RTL8192SU
7  *
8  * Modifications for inclusion into the Linux staging tree are
9  * Copyright(c) 2010 Larry Finger. All rights reserved.
10  *
11  * Contact information:
12  * WLAN FAE <wlanfae@realtek.com>.
13  * Larry Finger <Larry.Finger@lwfinger.net>
14  *
15  ******************************************************************************/
16
17 #define _RTL8712_CMD_C_
18
19 #include <linux/compiler.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/sched/signal.h>
24 #include <linux/module.h>
25 #include <linux/kref.h>
26 #include <linux/netdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/usb.h>
29 #include <linux/usb/ch9.h>
30 #include <linux/circ_buf.h>
31 #include <linux/uaccess.h>
32 #include <asm/byteorder.h>
33 #include <linux/atomic.h>
34 #include <linux/semaphore.h>
35 #include <linux/rtnetlink.h>
36
37 #include "osdep_service.h"
38 #include "drv_types.h"
39 #include "recv_osdep.h"
40 #include "mlme_osdep.h"
41 #include "rtl871x_ioctl_set.h"
42
43 static void check_hw_pbc(struct _adapter *padapter)
44 {
45         u8      tmp1byte;
46
47         r8712_write8(padapter, MAC_PINMUX_CTRL, (GPIOMUX_EN | GPIOSEL_GPIO));
48         tmp1byte = r8712_read8(padapter, GPIO_IO_SEL);
49         tmp1byte &= ~(HAL_8192S_HW_GPIO_WPS_BIT);
50         r8712_write8(padapter, GPIO_IO_SEL, tmp1byte);
51         tmp1byte = r8712_read8(padapter, GPIO_CTRL);
52         if (tmp1byte == 0xff)
53                 return;
54         if (tmp1byte & HAL_8192S_HW_GPIO_WPS_BIT) {
55                 /* Here we only set bPbcPressed to true
56                  * After trigger PBC, the variable will be set to false
57                  */
58                 DBG_8712("CheckPbcGPIO - PBC is pressed !!!!\n");
59                 /* 0 is the default value and it means the application monitors
60                  * the HW PBC doesn't provide its pid to driver.
61                  */
62                 if (padapter->pid == 0)
63                         return;
64                 kill_pid(find_vpid(padapter->pid), SIGUSR1, 1);
65         }
66 }
67
68 /* query rx phy status from fw.
69  * Adhoc mode: beacon.
70  * Infrastructure mode: beacon , data.
71  */
72 static void query_fw_rx_phy_status(struct _adapter *padapter)
73 {
74         u32 val32 = 0;
75         int pollingcnts = 50;
76
77         if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
78                 r8712_write32(padapter, IOCMD_CTRL_REG, 0xf4000001);
79                 msleep(100);
80                 /* Wait FW complete IO Cmd */
81                 while ((r8712_read32(padapter, IOCMD_CTRL_REG)) &&
82                        (pollingcnts > 0)) {
83                         pollingcnts--;
84                         msleep(20);
85                 }
86                 if (pollingcnts != 0)
87                         val32 = r8712_read32(padapter, IOCMD_DATA_REG);
88                 else /* time out */
89                         val32 = 0;
90                 val32 >>= 4;
91                 padapter->recvpriv.fw_rssi =
92                          (u8)r8712_signal_scale_mapping(val32);
93         }
94 }
95
96 /* check mlme, hw, phy, or dynamic algorithm status. */
97 static void StatusWatchdogCallback(struct _adapter *padapter)
98 {
99         check_hw_pbc(padapter);
100         query_fw_rx_phy_status(padapter);
101 }
102
103 static void r871x_internal_cmd_hdl(struct _adapter *padapter, u8 *pbuf)
104 {
105         struct drvint_cmd_parm *pdrvcmd;
106
107         if (!pbuf)
108                 return;
109         pdrvcmd = (struct drvint_cmd_parm *)pbuf;
110         switch (pdrvcmd->i_cid) {
111         case WDG_WK_CID:
112                 StatusWatchdogCallback(padapter);
113                 break;
114         default:
115                 break;
116         }
117         kfree(pdrvcmd->pbuf);
118 }
119
120 static u8 read_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
121 {
122         void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj      *pcmd);
123         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
124
125         /*  invoke cmd->callback function */
126         pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
127         if (!pcmd_callback)
128                 r8712_free_cmd_obj(pcmd);
129         else
130                 pcmd_callback(padapter, pcmd);
131         return H2C_SUCCESS;
132 }
133
134 static u8 write_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
135 {
136         void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj      *pcmd);
137         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
138
139         /*  invoke cmd->callback function */
140         pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
141         if (!pcmd_callback)
142                 r8712_free_cmd_obj(pcmd);
143         else
144                 pcmd_callback(padapter, pcmd);
145         return H2C_SUCCESS;
146 }
147
148 static u8 read_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
149 {
150         u32 val;
151         void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj      *pcmd);
152         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
153
154         if (pcmd->rsp && pcmd->rspsz > 0)
155                 memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
156         pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
157         if (!pcmd_callback)
158                 r8712_free_cmd_obj(pcmd);
159         else
160                 pcmd_callback(padapter, pcmd);
161         return H2C_SUCCESS;
162 }
163
164 static u8 write_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
165 {
166         void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
167         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
168
169         pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
170         if (!pcmd_callback)
171                 r8712_free_cmd_obj(pcmd);
172         else
173                 pcmd_callback(padapter, pcmd);
174         return H2C_SUCCESS;
175 }
176
177 static u8 read_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
178 {
179         u32 val;
180         void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
181         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
182
183         if (pcmd->rsp && pcmd->rspsz > 0)
184                 memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
185         pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
186         if (!pcmd_callback)
187                 r8712_free_cmd_obj(pcmd);
188         else
189                 pcmd_callback(padapter, pcmd);
190         return H2C_SUCCESS;
191 }
192
193 static u8 write_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
194 {
195         void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
196         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
197
198         pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
199         if (!pcmd_callback)
200                 r8712_free_cmd_obj(pcmd);
201         else
202                 pcmd_callback(padapter, pcmd);
203         return H2C_SUCCESS;
204 }
205
206 static u8 sys_suspend_hdl(struct _adapter *padapter, u8 *pbuf)
207 {
208         struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
209
210         r8712_free_cmd_obj(pcmd);
211         return H2C_SUCCESS;
212 }
213
214 static struct cmd_obj *cmd_hdl_filter(struct _adapter *padapter,
215                                       struct cmd_obj *pcmd)
216 {
217         struct cmd_obj *pcmd_r;
218
219         if (!pcmd)
220                 return pcmd;
221         pcmd_r = NULL;
222
223         switch (pcmd->cmdcode) {
224         case GEN_CMD_CODE(_Read_MACREG):
225                 read_macreg_hdl(padapter, (u8 *)pcmd);
226                 pcmd_r = pcmd;
227                 break;
228         case GEN_CMD_CODE(_Write_MACREG):
229                 write_macreg_hdl(padapter, (u8 *)pcmd);
230                 pcmd_r = pcmd;
231                 break;
232         case GEN_CMD_CODE(_Read_BBREG):
233                 read_bbreg_hdl(padapter, (u8 *)pcmd);
234                 break;
235         case GEN_CMD_CODE(_Write_BBREG):
236                 write_bbreg_hdl(padapter, (u8 *)pcmd);
237                 break;
238         case GEN_CMD_CODE(_Read_RFREG):
239                 read_rfreg_hdl(padapter, (u8 *)pcmd);
240                 break;
241         case GEN_CMD_CODE(_Write_RFREG):
242                 write_rfreg_hdl(padapter, (u8 *)pcmd);
243                 break;
244         case GEN_CMD_CODE(_SetUsbSuspend):
245                 sys_suspend_hdl(padapter, (u8 *)pcmd);
246                 break;
247         case GEN_CMD_CODE(_JoinBss):
248                 r8712_joinbss_reset(padapter);
249                 /* Before set JoinBss_CMD to FW, driver must ensure FW is in
250                  * PS_MODE_ACTIVE. Directly write rpwm to radio on and assign
251                  * new pwr_mode to Driver, instead of use workitem to change
252                  * state.
253                  */
254                 if (padapter->pwrctrlpriv.pwr_mode > PS_MODE_ACTIVE) {
255                         padapter->pwrctrlpriv.pwr_mode = PS_MODE_ACTIVE;
256                         mutex_lock(&padapter->pwrctrlpriv.mutex_lock);
257                         r8712_set_rpwm(padapter, PS_STATE_S4);
258                         mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
259                 }
260                 pcmd_r = pcmd;
261                 break;
262         case _DRV_INT_CMD_:
263                 r871x_internal_cmd_hdl(padapter, pcmd->parmbuf);
264                 r8712_free_cmd_obj(pcmd);
265                 pcmd_r = NULL;
266                 break;
267         default:
268                 pcmd_r = pcmd;
269                 break;
270         }
271         return pcmd_r; /* if returning pcmd_r == NULL, pcmd must be free. */
272 }
273
274 static u8 check_cmd_fifo(struct _adapter *padapter, uint sz)
275 {
276         return _SUCCESS;
277 }
278
279 u8 r8712_fw_cmd(struct _adapter *pAdapter, u32 cmd)
280 {
281         int pollingcnts = 50;
282
283         r8712_write32(pAdapter, IOCMD_CTRL_REG, cmd);
284         msleep(100);
285         while ((r8712_read32(pAdapter, IOCMD_CTRL_REG != 0)) &&
286                (pollingcnts > 0)) {
287                 pollingcnts--;
288                 msleep(20);
289         }
290         if (pollingcnts == 0)
291                 return false;
292         return true;
293 }
294
295 void r8712_fw_cmd_data(struct _adapter *pAdapter, u32 *value, u8 flag)
296 {
297         if (flag == 0)  /* set */
298                 r8712_write32(pAdapter, IOCMD_DATA_REG, *value);
299         else            /* query */
300                 *value = r8712_read32(pAdapter, IOCMD_DATA_REG);
301 }
302
303 int r8712_cmd_thread(void *context)
304 {
305         struct cmd_obj *pcmd;
306         unsigned int cmdsz, wr_sz;
307         __le32 *pcmdbuf;
308         struct tx_desc *pdesc;
309         void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
310         struct _adapter *padapter = context;
311         struct  cmd_priv *pcmdpriv = &padapter->cmdpriv;
312         struct completion *cmd_queue_comp =
313                 &pcmdpriv->cmd_queue_comp;
314         struct mutex *pwctrl_lock = &padapter->pwrctrlpriv.mutex_lock;
315
316         allow_signal(SIGTERM);
317         while (1) {
318                 if (wait_for_completion_interruptible(cmd_queue_comp))
319                         break;
320                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
321                         break;
322                 if (r8712_register_cmd_alive(padapter) != _SUCCESS)
323                         continue;
324 _next:
325                 pcmd = r8712_dequeue_cmd(&pcmdpriv->cmd_queue);
326                 if (!(pcmd)) {
327                         r8712_unregister_cmd_alive(padapter);
328                         continue;
329                 }
330                 pcmdbuf = (__le32 *)pcmdpriv->cmd_buf;
331                 pdesc = (struct tx_desc *)pcmdbuf;
332                 memset(pdesc, 0, TXDESC_SIZE);
333                 pcmd = cmd_hdl_filter(padapter, pcmd);
334                 if (pcmd) { /* if pcmd != NULL, cmd will be handled by f/w */
335                         struct dvobj_priv *pdvobj = &padapter->dvobjpriv;
336                         u8 blnPending = 0;
337                         u16 cmdcode = pcmd->cmdcode;
338
339                         pcmdpriv->cmd_issued_cnt++;
340                         cmdsz = round_up(pcmd->cmdsz, 8);
341                         wr_sz = TXDESC_SIZE + 8 + cmdsz;
342                         pdesc->txdw0 |= cpu_to_le32((wr_sz - TXDESC_SIZE) &
343                                                      0x0000ffff);
344                         if (pdvobj->ishighspeed) {
345                                 if ((wr_sz % 512) == 0)
346                                         blnPending = 1;
347                         } else {
348                                 if ((wr_sz % 64) == 0)
349                                         blnPending = 1;
350                         }
351                         if (blnPending) { /* 32 bytes for TX Desc - 8 offset */
352                                 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
353                                                 OFFSET_SZ + 8) << OFFSET_SHT) &
354                                                 0x00ff0000);
355                         } else {
356                                 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
357                                                               OFFSET_SZ) <<
358                                                               OFFSET_SHT) &
359                                                               0x00ff0000);
360                         }
361                         pdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
362                         pdesc->txdw1 |= cpu_to_le32((0x13 << QSEL_SHT) &
363                                                     0x00001f00);
364                         pcmdbuf += (TXDESC_SIZE >> 2);
365                         *pcmdbuf = cpu_to_le32((cmdsz & 0x0000ffff) |
366                                                (pcmd->cmdcode << 16) |
367                                                (pcmdpriv->cmd_seq << 24));
368                         pcmdbuf += 2; /* 8 bytes alignment */
369                         memcpy((u8 *)pcmdbuf, pcmd->parmbuf, pcmd->cmdsz);
370                         while (check_cmd_fifo(padapter, wr_sz) == _FAIL) {
371                                 if (padapter->bDriverStopped ||
372                                     padapter->bSurpriseRemoved)
373                                         break;
374                                 msleep(100);
375                                 continue;
376                         }
377                         if (blnPending)
378                                 wr_sz += 8;   /* Append 8 bytes */
379                         r8712_write_mem(padapter, RTL8712_DMA_H2CCMD, wr_sz,
380                                         (u8 *)pdesc);
381                         pcmdpriv->cmd_seq++;
382                         if (cmdcode == GEN_CMD_CODE(_CreateBss)) {
383                                 pcmd->res = H2C_SUCCESS;
384                                 pcmd_callback = cmd_callback[cmdcode].callback;
385                                 if (pcmd_callback)
386                                         pcmd_callback(padapter, pcmd);
387                                 continue;
388                         }
389                         if (cmdcode == GEN_CMD_CODE(_SetPwrMode)) {
390                                 if (padapter->pwrctrlpriv.bSleep) {
391                                         mutex_lock(pwctrl_lock);
392                                         r8712_set_rpwm(padapter, PS_STATE_S2);
393                                         mutex_unlock(pwctrl_lock);
394                                 }
395                         }
396                         r8712_free_cmd_obj(pcmd);
397                         if (list_empty(&pcmdpriv->cmd_queue.queue)) {
398                                 r8712_unregister_cmd_alive(padapter);
399                                 continue;
400                         } else {
401                                 goto _next;
402                         }
403                 } else {
404                         goto _next;
405                 }
406                 flush_signals_thread();
407         }
408         /* free all cmd_obj resources */
409         do {
410                 pcmd = r8712_dequeue_cmd(&pcmdpriv->cmd_queue);
411                 if (!pcmd)
412                         break;
413                 r8712_free_cmd_obj(pcmd);
414         } while (1);
415         complete(&pcmdpriv->terminate_cmdthread_comp);
416         thread_exit();
417 }
418
419 void r8712_event_handle(struct _adapter *padapter, __le32 *peventbuf)
420 {
421         u8 evt_code, evt_seq;
422         u16 evt_sz;
423         void (*event_callback)(struct _adapter *dev, u8 *pbuf);
424         struct  evt_priv *pevt_priv = &padapter->evtpriv;
425
426         if (!peventbuf)
427                 goto _abort_event_;
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);
434                 goto _abort_event_;
435         }
436         /* checking if event code is valid */
437         if (evt_code >= MAX_C2HEVT) {
438                 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
439                 goto _abort_event_;
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);
443                 goto _abort_event_;
444         }
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);
449                 goto _abort_event_;
450         } else if ((evt_sz == 0) && (evt_code != GEN_EVT_CODE(_WPS_PBC))) {
451                 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
452                 goto _abort_event_;
453         }
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;
460         if (event_callback)
461                 event_callback(padapter, (u8 *)peventbuf);
462         pevt_priv->evt_done_cnt++;
463 _abort_event_:
464         return;
465 }