]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/rtl8712/rtl8712_cmd.c
Merge tag 'efi-urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/efi/efi into...
[mirror_ubuntu-artful-kernel.git] / drivers / staging / rtl8712 / rtl8712_cmd.c
CommitLineData
2865d42c
LF
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
359140aa
AB
31#include <linux/compiler.h>
32#include <linux/kernel.h>
33#include <linux/errno.h>
359140aa 34#include <linux/slab.h>
3f07c014 35#include <linux/sched/signal.h>
359140aa
AB
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
2865d42c
LF
49#include "osdep_service.h"
50#include "drv_types.h"
51#include "recv_osdep.h"
52#include "mlme_osdep.h"
2865d42c
LF
53#include "rtl871x_ioctl_set.h"
54
55static 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)
77e73e8c 65 return;
4ef2de5a 66 if (tmp1byte & HAL_8192S_HW_GPIO_WPS_BIT) {
2865d42c 67 /* Here we only set bPbcPressed to true
ed9c838a
PV
68 * After trigger PBC, the variable will be set to false
69 */
2865d42c
LF
70 DBG_8712("CheckPbcGPIO - PBC is pressed !!!!\n");
71 /* 0 is the default value and it means the application monitors
ed9c838a
PV
72 * the HW PBC doesn't provide its pid to driver.
73 */
2865d42c
LF
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.
ed9c838a
PV
82 * Infrastructure mode: beacon , data.
83 */
2865d42c
LF
84static void query_fw_rx_phy_status(struct _adapter *padapter)
85{
86 u32 val32 = 0;
87 int pollingcnts = 50;
88
1ca96884 89 if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
2865d42c
LF
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;
ae52e527 102 val32 >>= 4;
2865d42c
LF
103 padapter->recvpriv.fw_rssi =
104 (u8)r8712_signal_scale_mapping(val32);
105 }
106}
107
108/* check mlme, hw, phy, or dynamic algorithm status. */
109static void StatusWatchdogCallback(struct _adapter *padapter)
110{
111 check_hw_pbc(padapter);
112 query_fw_rx_phy_status(padapter);
113}
114
115static 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
132static u8 read_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
133{
134 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
135 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
136
137 /* invoke cmd->callback function */
138 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
7fcf92c0 139 if (!pcmd_callback)
2865d42c
LF
140 r8712_free_cmd_obj(pcmd);
141 else
142 pcmd_callback(padapter, pcmd);
143 return H2C_SUCCESS;
144}
145
146static u8 write_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
147{
148 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
149 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
150
151 /* invoke cmd->callback function */
152 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
7fcf92c0 153 if (!pcmd_callback)
2865d42c
LF
154 r8712_free_cmd_obj(pcmd);
155 else
156 pcmd_callback(padapter, pcmd);
157 return H2C_SUCCESS;
158}
159
160static u8 read_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
161{
162 u32 val;
163 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
2865d42c
LF
164 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
165
2865d42c
LF
166 if (pcmd->rsp && pcmd->rspsz > 0)
167 memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
168 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
7fcf92c0 169 if (!pcmd_callback)
2865d42c
LF
170 r8712_free_cmd_obj(pcmd);
171 else
172 pcmd_callback(padapter, pcmd);
173 return H2C_SUCCESS;
174}
175
176static u8 write_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
177{
178 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
2865d42c
LF
179 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
180
2865d42c 181 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
7fcf92c0 182 if (!pcmd_callback)
2865d42c
LF
183 r8712_free_cmd_obj(pcmd);
184 else
185 pcmd_callback(padapter, pcmd);
186 return H2C_SUCCESS;
187}
188
189static u8 read_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
190{
191 u32 val;
192 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
2865d42c
LF
193 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
194
2865d42c
LF
195 if (pcmd->rsp && pcmd->rspsz > 0)
196 memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
197 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
7fcf92c0 198 if (!pcmd_callback)
2865d42c
LF
199 r8712_free_cmd_obj(pcmd);
200 else
201 pcmd_callback(padapter, pcmd);
202 return H2C_SUCCESS;
203}
204
205static u8 write_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
206{
207 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
2865d42c
LF
208 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
209
2865d42c 210 pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
7fcf92c0 211 if (!pcmd_callback)
2865d42c
LF
212 r8712_free_cmd_obj(pcmd);
213 else
214 pcmd_callback(padapter, pcmd);
215 return H2C_SUCCESS;
216}
217
218static u8 sys_suspend_hdl(struct _adapter *padapter, u8 *pbuf)
219{
220 struct cmd_obj *pcmd = (struct cmd_obj *)pbuf;
2865d42c 221
2865d42c
LF
222 r8712_free_cmd_obj(pcmd);
223 return H2C_SUCCESS;
224}
225
226static struct cmd_obj *cmd_hdl_filter(struct _adapter *padapter,
227 struct cmd_obj *pcmd)
228{
229 struct cmd_obj *pcmd_r;
230
7fcf92c0 231 if (!pcmd)
2865d42c
LF
232 return pcmd;
233 pcmd_r = NULL;
234
235 switch (pcmd->cmdcode) {
236 case GEN_CMD_CODE(_Read_MACREG):
237 read_macreg_hdl(padapter, (u8 *)pcmd);
238 pcmd_r = pcmd;
239 break;
240 case GEN_CMD_CODE(_Write_MACREG):
241 write_macreg_hdl(padapter, (u8 *)pcmd);
242 pcmd_r = pcmd;
243 break;
244 case GEN_CMD_CODE(_Read_BBREG):
245 read_bbreg_hdl(padapter, (u8 *)pcmd);
246 break;
247 case GEN_CMD_CODE(_Write_BBREG):
248 write_bbreg_hdl(padapter, (u8 *)pcmd);
249 break;
250 case GEN_CMD_CODE(_Read_RFREG):
251 read_rfreg_hdl(padapter, (u8 *)pcmd);
252 break;
253 case GEN_CMD_CODE(_Write_RFREG):
254 write_rfreg_hdl(padapter, (u8 *)pcmd);
255 break;
256 case GEN_CMD_CODE(_SetUsbSuspend):
257 sys_suspend_hdl(padapter, (u8 *)pcmd);
258 break;
259 case GEN_CMD_CODE(_JoinBss):
260 r8712_joinbss_reset(padapter);
261 /* Before set JoinBss_CMD to FW, driver must ensure FW is in
262 * PS_MODE_ACTIVE. Directly write rpwm to radio on and assign
263 * new pwr_mode to Driver, instead of use workitem to change
ed9c838a
PV
264 * state.
265 */
2865d42c
LF
266 if (padapter->pwrctrlpriv.pwr_mode > PS_MODE_ACTIVE) {
267 padapter->pwrctrlpriv.pwr_mode = PS_MODE_ACTIVE;
5c2ba8b8 268 mutex_lock(&padapter->pwrctrlpriv.mutex_lock);
2865d42c 269 r8712_set_rpwm(padapter, PS_STATE_S4);
5c2ba8b8 270 mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
2865d42c
LF
271 }
272 pcmd_r = pcmd;
273 break;
274 case _DRV_INT_CMD_:
275 r871x_internal_cmd_hdl(padapter, pcmd->parmbuf);
276 r8712_free_cmd_obj(pcmd);
277 pcmd_r = NULL;
278 break;
279 default:
280 pcmd_r = pcmd;
281 break;
282 }
283 return pcmd_r; /* if returning pcmd_r == NULL, pcmd must be free. */
284}
285
286static u8 check_cmd_fifo(struct _adapter *padapter, uint sz)
287{
8ffca9ea 288 return _SUCCESS;
2865d42c
LF
289}
290
291u8 r8712_fw_cmd(struct _adapter *pAdapter, u32 cmd)
292{
293 int pollingcnts = 50;
294
295 r8712_write32(pAdapter, IOCMD_CTRL_REG, cmd);
296 msleep(100);
97e2ba90 297 while ((r8712_read32(pAdapter, IOCMD_CTRL_REG != 0)) &&
2865d42c
LF
298 (pollingcnts > 0)) {
299 pollingcnts--;
300 msleep(20);
301 }
302 if (pollingcnts == 0)
303 return false;
304 return true;
305}
306
307void r8712_fw_cmd_data(struct _adapter *pAdapter, u32 *value, u8 flag)
308{
309 if (flag == 0) /* set */
310 r8712_write32(pAdapter, IOCMD_DATA_REG, *value);
311 else /* query */
312 *value = r8712_read32(pAdapter, IOCMD_DATA_REG);
313}
314
315int r8712_cmd_thread(void *context)
316{
317 struct cmd_obj *pcmd;
e8cd841b
JB
318 unsigned int cmdsz, wr_sz;
319 __le32 *pcmdbuf;
2865d42c
LF
320 struct tx_desc *pdesc;
321 void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
56238e45 322 struct _adapter *padapter = context;
2865d42c
LF
323 struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
324
fee51243 325 allow_signal(SIGTERM);
2865d42c 326 while (1) {
0f89054a 327 if (wait_for_completion_interruptible(&pcmdpriv->cmd_queue_comp))
2865d42c 328 break;
1ca96884 329 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
2865d42c
LF
330 break;
331 if (r8712_register_cmd_alive(padapter) != _SUCCESS)
332 continue;
333_next:
334 pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
335 if (!(pcmd)) {
336 r8712_unregister_cmd_alive(padapter);
337 continue;
338 }
e8cd841b 339 pcmdbuf = (__le32 *)pcmdpriv->cmd_buf;
2865d42c
LF
340 pdesc = (struct tx_desc *)pcmdbuf;
341 memset(pdesc, 0, TXDESC_SIZE);
342 pcmd = cmd_hdl_filter(padapter, pcmd);
343 if (pcmd) { /* if pcmd != NULL, cmd will be handled by f/w */
adc08cc4 344 struct dvobj_priv *pdvobj = &padapter->dvobjpriv;
2865d42c 345 u8 blnPending = 0;
02a29d2d 346
2865d42c 347 pcmdpriv->cmd_issued_cnt++;
d913e54e 348 cmdsz = round_up(pcmd->cmdsz, 8);
2865d42c 349 wr_sz = TXDESC_SIZE + 8 + cmdsz;
4ef2de5a 350 pdesc->txdw0 |= cpu_to_le32((wr_sz - TXDESC_SIZE) &
2865d42c
LF
351 0x0000ffff);
352 if (pdvobj->ishighspeed) {
353 if ((wr_sz % 512) == 0)
354 blnPending = 1;
355 } else {
356 if ((wr_sz % 64) == 0)
357 blnPending = 1;
358 }
359 if (blnPending) /* 32 bytes for TX Desc - 8 offset */
360 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
361 OFFSET_SZ + 8) << OFFSET_SHT) &
362 0x00ff0000);
363 else {
364 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
365 OFFSET_SZ) <<
366 OFFSET_SHT) &
367 0x00ff0000);
368 }
369 pdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
370 pdesc->txdw1 |= cpu_to_le32((0x13 << QSEL_SHT) &
371 0x00001f00);
372 pcmdbuf += (TXDESC_SIZE >> 2);
373 *pcmdbuf = cpu_to_le32((cmdsz & 0x0000ffff) |
374 (pcmd->cmdcode << 16) |
375 (pcmdpriv->cmd_seq << 24));
77e73e8c 376 pcmdbuf += 2; /* 8 bytes alignment */
2865d42c
LF
377 memcpy((u8 *)pcmdbuf, pcmd->parmbuf, pcmd->cmdsz);
378 while (check_cmd_fifo(padapter, wr_sz) == _FAIL) {
1ca96884
LB
379 if (padapter->bDriverStopped ||
380 padapter->bSurpriseRemoved)
2865d42c
LF
381 break;
382 msleep(100);
383 continue;
384 }
385 if (blnPending)
386 wr_sz += 8; /* Append 8 bytes */
387 r8712_write_mem(padapter, RTL8712_DMA_H2CCMD, wr_sz,
388 (u8 *)pdesc);
389 pcmdpriv->cmd_seq++;
390 if (pcmd->cmdcode == GEN_CMD_CODE(_CreateBss)) {
391 pcmd->res = H2C_SUCCESS;
392 pcmd_callback = cmd_callback[pcmd->
393 cmdcode].callback;
394 if (pcmd_callback)
395 pcmd_callback(padapter, pcmd);
396 continue;
397 }
398 if (pcmd->cmdcode == GEN_CMD_CODE(_SetPwrMode)) {
399 if (padapter->pwrctrlpriv.bSleep) {
5c2ba8b8
BJ
400 mutex_lock(&padapter->
401 pwrctrlpriv.mutex_lock);
2865d42c 402 r8712_set_rpwm(padapter, PS_STATE_S2);
5c2ba8b8 403 mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
2865d42c
LF
404 }
405 }
406 r8712_free_cmd_obj(pcmd);
df353f61 407 if (list_empty(&pcmdpriv->cmd_queue.queue)) {
2865d42c
LF
408 r8712_unregister_cmd_alive(padapter);
409 continue;
168a2c10 410 } else {
2865d42c 411 goto _next;
168a2c10
LB
412 }
413 } else {
2865d42c 414 goto _next;
168a2c10 415 }
2865d42c
LF
416 flush_signals_thread();
417 }
418 /* free all cmd_obj resources */
419 do {
420 pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
7fcf92c0 421 if (!pcmd)
2865d42c
LF
422 break;
423 r8712_free_cmd_obj(pcmd);
424 } while (1);
204a8ac1 425 complete(&pcmdpriv->terminate_cmdthread_comp);
2865d42c
LF
426 thread_exit();
427}
428
bcb91a5c 429void r8712_event_handle(struct _adapter *padapter, __le32 *peventbuf)
2865d42c
LF
430{
431 u8 evt_code, evt_seq;
432 u16 evt_sz;
433 void (*event_callback)(struct _adapter *dev, u8 *pbuf);
434 struct evt_priv *pevt_priv = &(padapter->evtpriv);
435
7fcf92c0 436 if (!peventbuf)
2865d42c
LF
437 goto _abort_event_;
438 evt_sz = (u16)(le32_to_cpu(*peventbuf) & 0xffff);
439 evt_seq = (u8)((le32_to_cpu(*peventbuf) >> 24) & 0x7f);
440 evt_code = (u8)((le32_to_cpu(*peventbuf) >> 16) & 0xff);
441 /* checking event sequence... */
442 if ((evt_seq & 0x7f) != pevt_priv->event_seq) {
443 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
444 goto _abort_event_;
445 }
446 /* checking if event code is valid */
447 if (evt_code >= MAX_C2HEVT) {
4ef2de5a 448 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
2865d42c
LF
449 goto _abort_event_;
450 } else if ((evt_code == GEN_EVT_CODE(_Survey)) &&
451 (evt_sz > sizeof(struct wlan_bssid_ex))) {
4ef2de5a 452 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
2865d42c
LF
453 goto _abort_event_;
454 }
455 /* checking if event size match the event parm size */
456 if ((wlanevents[evt_code].parmsize) &&
457 (wlanevents[evt_code].parmsize != evt_sz)) {
4ef2de5a 458 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
2865d42c
LF
459 goto _abort_event_;
460 } else if ((evt_sz == 0) && (evt_code != GEN_EVT_CODE(_WPS_PBC))) {
4ef2de5a 461 pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
2865d42c
LF
462 goto _abort_event_;
463 }
464 pevt_priv->event_seq++; /* update evt_seq */
465 if (pevt_priv->event_seq > 127)
466 pevt_priv->event_seq = 0;
57b6686e
TP
467 /* move to event content, 8 bytes alignment */
468 peventbuf = peventbuf + 2;
a376932a
DC
469 event_callback = wlanevents[evt_code].event_callback;
470 if (event_callback)
471 event_callback(padapter, (u8 *)peventbuf);
2865d42c
LF
472 pevt_priv->evt_done_cnt++;
473_abort_event_:
474 return;
475}