MT6620: add the new driver JB2 V1.0
[firefly-linux-kernel-4.4.55.git] / drivers / mtk_wcn_combo / drv_wlan / mt6620 / wlan / os / linux / gl_rst.c
1 /*
2 ** $Id: @(#) gl_rst.c@@
3 */
4
5 /*! \file   gl_rst.c
6     \brief  Main routines for supporintg MT6620 whole-chip reset mechanism
7
8     This file contains the support routines of Linux driver for MediaTek Inc. 802.11
9     Wireless LAN Adapters.
10 */
11
12
13
14 /*
15 ** $Log: gl_rst.c $
16  *
17  * 11 10 2011 cp.wu
18  * [WCXRP00001098] [MT6620 Wi-Fi][Driver] Replace printk by DBG LOG macros in linux porting layer
19  * 1. eliminaite direct calls to printk in porting layer.
20  * 2. replaced by DBGLOG, which would be XLOG on ALPS platforms.
21  *
22  * 04 22 2011 cp.wu
23  * [WCXRP00000598] [MT6620 Wi-Fi][Driver] Implementation of interface for communicating with user space process for RESET_START and RESET_END events
24  * skip power-off handshaking when RESET indication is received.
25  *
26  * 04 14 2011 cp.wu
27  * [WCXRP00000598] [MT6620 Wi-Fi][Driver] Implementation of interface for communicating with user space process for RESET_START and RESET_END events
28  * 1. add code to put whole-chip resetting trigger when abnormal firmware assertion is detected
29  * 2. add dummy function for both Win32 and Linux part.
30  *
31  * 03 30 2011 cp.wu
32  * [WCXRP00000598] [MT6620 Wi-Fi][Driver] Implementation of interface for communicating with user space process for RESET_START and RESET_END events
33  * use netlink unicast instead of broadcast
34  *
35 **
36 */
37
38 /*******************************************************************************
39 *                         C O M P I L E R   F L A G S
40 ********************************************************************************
41 */
42
43 /*******************************************************************************
44 *                    E X T E R N A L   R E F E R E N C E S
45 ********************************************************************************
46 */
47 #include "gl_os.h"
48 #include "debug.h"
49 #include "wlan_lib.h"
50 #include "gl_wext.h"
51 #include "precomp.h"
52 #include <linux/poll.h>
53 #include <net/netlink.h>
54 #include <net/genetlink.h>
55
56 #if CFG_CHIP_RESET_SUPPORT
57
58 /*******************************************************************************
59 *                              C O N S T A N T S
60 ********************************************************************************
61 */
62 #define MAX_BIND_PROCESS    (4)
63
64 #define MTK_WIFI_FAMILY_NAME        "MTK_WIFI"
65 #define MTK_WIFI_RESET_START_NAME   "RESET_START"
66 #define MTK_WIFI_RESET_END_NAME     "RESET_END"
67 #define MTK_WIFI_RESET_TEST_NAME    "GENETLINK_START"
68
69
70 /*******************************************************************************
71 *                             D A T A   T Y P E S
72 ********************************************************************************
73 */
74 enum {
75     __MTK_WIFI_ATTR_INVALID,
76     MTK_WIFI_ATTR_MSG,
77     __MTK_WIFI_ATTR_MAX,
78 };
79 #define MTK_WIFI_ATTR_MAX       (__MTK_WIFI_ATTR_MAX - 1)
80
81
82 enum {
83     __MTK_WIFI_COMMAND_INVALID,
84     MTK_WIFI_COMMAND_BIND,
85     MTK_WIFI_COMMAND_RESET,
86     __MTK_WIFI_COMMAND_MAX,
87 };
88 #define MTK_WIFI_COMMAND_MAX    (__MTK_WIFI_COMMAND_MAX - 1)
89
90 /*******************************************************************************
91 *                            P U B L I C   D A T A
92 ********************************************************************************
93 */BOOLEAN fgIsResetting = FALSE;
94
95 /*******************************************************************************
96 *                           P R I V A T E   D A T A
97 ********************************************************************************
98 */
99 static UINT_32 mtk_wifi_seqnum = 0;
100 static int num_bind_process = 0;
101 static pid_t bind_pid[MAX_BIND_PROCESS];
102
103
104 /* attribute policy */
105 static struct nla_policy mtk_wifi_genl_policy[MTK_WIFI_ATTR_MAX + 1] = {
106     [MTK_WIFI_ATTR_MSG] = { .type = NLA_NUL_STRING },
107 };
108
109 /* family definition */
110 static struct genl_family mtk_wifi_gnl_family = {
111     .id         = GENL_ID_GENERATE,
112     .hdrsize    = 0,
113     .name       = MTK_WIFI_FAMILY_NAME,
114     .version    = 1,
115     .maxattr    = MTK_WIFI_ATTR_MAX,
116 };
117
118 /* forward declaration */
119 static int mtk_wifi_bind(
120     struct sk_buff *skb,
121     struct genl_info *info
122     );
123
124 static int mtk_wifi_reset(
125     struct sk_buff *skb,
126     struct genl_info *info
127     );
128
129 /* operation definition */
130 static struct genl_ops mtk_wifi_gnl_ops_bind = {
131     .cmd = MTK_WIFI_COMMAND_BIND,
132     .flags  = 0,
133     .policy = mtk_wifi_genl_policy,
134     .doit   = mtk_wifi_bind,
135     .dumpit = NULL,
136 };
137
138 static struct genl_ops mtk_wifi_gnl_ops_reset = {
139     .cmd = MTK_WIFI_COMMAND_RESET,
140     .flags  = 0,
141     .policy = mtk_wifi_genl_policy,
142     .doit   = mtk_wifi_reset,
143     .dumpit = NULL,
144 };
145
146
147 /*******************************************************************************
148 *                                 M A C R O S
149 ********************************************************************************
150 */
151
152 /*******************************************************************************
153 *                   F U N C T I O N   D E C L A R A T I O N S
154 ********************************************************************************
155 */
156 extern int
157 mtk_wcn_wmt_msgcb_reg(
158     ENUM_WMTDRV_TYPE_T eType,
159     PF_WMT_CB pCb);
160
161 extern int
162 mtk_wcn_wmt_msgcb_unreg(
163     ENUM_WMTDRV_TYPE_T eType
164     );
165
166 static void *
167 glResetCallback (
168     ENUM_WMTDRV_TYPE_T  eSrcType,
169     ENUM_WMTDRV_TYPE_T  eDstType,
170     ENUM_WMTMSG_TYPE_T  eMsgType,
171     void *              prMsgBody,
172     unsigned int        u4MsgLength
173     );
174
175 static BOOLEAN
176 glResetSendMessage (
177     char    *aucMsg,
178     u8      cmd
179     );
180
181
182 /*******************************************************************************
183 *                              F U N C T I O N S
184 ********************************************************************************
185 */
186 /*----------------------------------------------------------------------------*/
187 /*!
188  * @brief This routine is responsible for 
189  *        1. registering for reset callbacks
190  *        2. initialize netlink socket 
191  *
192  * @param none
193  *
194  * @retval none
195  */
196 /*----------------------------------------------------------------------------*/
197 VOID
198 glResetInit(
199     VOID
200     )
201 {
202     /* 1. register for reset callback */
203     mtk_wcn_wmt_msgcb_reg(WMTDRV_TYPE_WIFI, (PF_WMT_CB)glResetCallback);
204
205     /* 2.1 registration for NETLINK_GENERIC family */
206     if(genl_register_family(&mtk_wifi_gnl_family) != 0) {
207         DBGLOG(INIT, WARN, ("%s(): GE_NELINK family registration fail\n", __func__));
208     }
209     else {
210         /* 2.2 operation registration */
211         if(genl_register_ops(&mtk_wifi_gnl_family, &mtk_wifi_gnl_ops_bind) != 0) {
212             DBGLOG(INIT, WARN, ("%s(): BIND operation registration fail\n", __func__));
213         }
214
215         if(genl_register_ops(&mtk_wifi_gnl_family, &mtk_wifi_gnl_ops_reset) != 0) {
216             DBGLOG(INIT, WARN, ("%s(): RESET operation registration fail\n", __func__));
217         }
218     }
219
220     return;
221 }
222
223
224 /*----------------------------------------------------------------------------*/
225 /*!
226  * @brief This routine is responsible for 
227  *        1. uninitialize netlink socket 
228  *        2. deregistering for reset callbacks
229  *
230  * @param none
231  *
232  * @retval none
233  */
234 /*----------------------------------------------------------------------------*/
235 VOID
236 glResetUninit(
237     VOID
238     )
239 {
240     /* 1. release NETLINK_GENERIC family */
241     genl_unregister_family(&mtk_wifi_gnl_family);
242
243     /* 2. deregister for reset callback */
244     mtk_wcn_wmt_msgcb_unreg(WMTDRV_TYPE_WIFI);
245
246     return;
247 }
248
249
250 /*----------------------------------------------------------------------------*/
251 /*!
252  * @brief This routine is invoked when there is reset messages indicated
253  *
254  * @param   eSrcType
255  *          eDstType
256  *          eMsgType
257  *          prMsgBody
258  *          u4MsgLength
259  *
260  * @retval 
261  */
262 /*----------------------------------------------------------------------------*/
263 static void *
264 glResetCallback (
265     ENUM_WMTDRV_TYPE_T  eSrcType,
266     ENUM_WMTDRV_TYPE_T  eDstType,
267     ENUM_WMTMSG_TYPE_T  eMsgType,
268     void *              prMsgBody,
269     unsigned int        u4MsgLength
270     )
271 {
272     switch(eMsgType) {
273     case WMTMSG_TYPE_RESET:
274         if(u4MsgLength == sizeof(ENUM_WMTRSTMSG_TYPE_T)) {
275             P_ENUM_WMTRSTMSG_TYPE_T prRstMsg = (P_ENUM_WMTRSTMSG_TYPE_T) prMsgBody;
276
277             switch(*prRstMsg) {
278             case WMTRSTMSG_RESET_START:
279                 fgIsResetting = TRUE;
280                 glResetSendMessage(MTK_WIFI_RESET_START_NAME, MTK_WIFI_COMMAND_RESET);
281                 break;
282
283             case WMTRSTMSG_RESET_END:
284                 glResetSendMessage(MTK_WIFI_RESET_END_NAME, MTK_WIFI_COMMAND_RESET);
285                 fgIsResetting = FALSE;
286                 break;
287
288             default:
289                 break;
290             }
291         }
292
293         break;
294
295     default:
296         break;
297     }
298
299     return NULL;
300 }
301
302
303 /*----------------------------------------------------------------------------*/
304 /*!
305  * @brief This routine send out message via netlink socket
306  *
307  * @param   aucMsg
308  *          u4MsgLength
309  *
310  * @retval  TRUE
311  *          FALSE
312  */
313 /*----------------------------------------------------------------------------*/
314 static BOOLEAN
315 glResetSendMessage(
316     char *  aucMsg,
317     u8      cmd
318     )
319 {
320     struct sk_buff *skb = NULL;
321     void *msg_head = NULL;
322     int rc = -1;
323     int i;
324
325     if(num_bind_process == 0) {
326         /* no listening process */
327         return FALSE;
328     }
329
330     for(i = 0 ; i < num_bind_process ; i++) {
331         skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
332
333         if(skb) {
334             msg_head = genlmsg_put(skb, 0, mtk_wifi_seqnum++, &mtk_wifi_gnl_family, 0, cmd);
335
336             if(msg_head == NULL) {
337                 nlmsg_free(skb);
338                 return FALSE;
339             }
340
341             rc = nla_put_string(skb, MTK_WIFI_ATTR_MSG, aucMsg);
342             if(rc != 0) {
343                 nlmsg_free(skb);
344                 return FALSE;
345             }
346         
347             /* finalize the message */
348             genlmsg_end(skb, msg_head);
349         
350             /* sending message */
351             rc = genlmsg_unicast(&init_net, skb, bind_pid[i]);
352             if(rc != 0) {
353                 return FALSE;
354             }
355         }
356         else {
357             return FALSE;
358         }
359     }
360
361     return TRUE;
362 }
363
364
365 /*----------------------------------------------------------------------------*/
366 /*!
367  * @brief This routine is called to identify PID for process binding
368  *
369  * @param   skb
370  *          info
371  *
372  * @retval  0
373  *          nonzero
374  */
375 /*----------------------------------------------------------------------------*/
376 int mtk_wifi_bind(
377     struct sk_buff *skb,
378     struct genl_info *info
379     )
380 {
381     struct nlattr *na;
382     char * mydata;
383
384     if (info == NULL) {
385         goto out;
386     }
387
388     /*for each attribute there is an index in info->attrs which points to a nlattr structure
389      *in this structure the data is given
390      */
391     
392     na = info->attrs[MTK_WIFI_ATTR_MSG];
393     if (na) {
394         mydata = (char *)nla_data(na);
395
396         /* no need to parse mydata */
397     }
398
399     /* collect PID */
400     if(num_bind_process < MAX_BIND_PROCESS) {
401         bind_pid[num_bind_process] = info->snd_pid;
402         num_bind_process++;
403         }
404     else {
405         DBGLOG(INIT, WARN, ("%s(): exceeding binding limit %d\n", __func__, MAX_BIND_PROCESS));
406     }
407
408 out:
409     return 0;
410 }
411
412
413 /*----------------------------------------------------------------------------*/
414 /*!
415  * @brief This routine is called for reset, shout not happen
416  *
417  * @param   skb
418  *          info
419  *
420  * @retval  0
421  *          nonzero
422  */
423 /*----------------------------------------------------------------------------*/
424 int mtk_wifi_reset(
425     struct sk_buff *skb,
426     struct genl_info *info
427     )
428 {
429     DBGLOG(INIT, WARN, ("%s(): should not be invoked\n", __func__));
430
431     return 0;
432 }
433
434
435 /*----------------------------------------------------------------------------*/
436 /*!
437  * @brief This routine is called for generating reset request to WMT
438  *
439  * @param   None
440  *
441  * @retval  None
442  */
443 /*----------------------------------------------------------------------------*/
444 VOID
445 glSendResetRequest(
446     VOID
447     )
448 {
449     // WMT thread would trigger whole chip resetting itself
450     return;
451 }
452
453
454 /*----------------------------------------------------------------------------*/
455 /*!
456  * @brief This routine is called for checking if MT6620 is resetting
457  *
458  * @param   None
459  *
460  * @retval  TRUE
461  *          FALSE
462  */
463 /*----------------------------------------------------------------------------*/
464 BOOLEAN
465 kalIsResetting(
466     VOID
467     )
468 {
469     return fgIsResetting;
470 }
471
472
473 #endif // CFG_CHIP_RESET_SUPPORT