[Feature][ZXW-202][autosuspend]add init and deinit

Only Configure: No
Affected branch: master
Affected module: autosuspend
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: Yes

Change-Id: I67c92bc675575436bac058d7f8c409c948926b01
diff --git a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-autosuspend-demo/files/lynq-qser-autosuspend-demo.cpp b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-autosuspend-demo/files/lynq-qser-autosuspend-demo.cpp
index fdeb548..9fa09c2 100755
--- a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-autosuspend-demo/files/lynq-qser-autosuspend-demo.cpp
+++ b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-autosuspend-demo/files/lynq-qser-autosuspend-demo.cpp
@@ -13,6 +13,31 @@
 
 #define MAX_LOCK_NUM 128
 
+static void qser_lpm_handler(qser_lpm_edge_t edge_state)
+{
+    printf("this is qser_lpm_handler\n");
+
+}
+
+static void printUsage(void)
+{
+    printf("-i                  --qser_lpm_init\n");
+    printf("-d                  --qser_lpm_deinit\n");
+    printf("-q                  --quit\n");
+    printf("-e                  --qser_autosuspend_enable\n");
+    printf("-cl [wakelock name] --qser_wakelock_create \n");
+    printf("-al [wakelock num]  --qser_wakelock_lock \n");
+    printf("-rl [wakelock num]  --qser_wakelock_unlock \n");
+    printf("-dl [wakelock num]  --qser_wakelock_destroy\n");
+    printf("-ccl                --check created locks\n");
+    printf("-cll                --check lockup locks\n");
+    printf("-wla [id]           --qser_whitelist_add\n");
+    printf("-wld [id]           --qser_whitelist_del\n");
+    printf("-wlg                --qser_whitelist_get\n");
+    printf("-wlc                --qser_whitelist_clear\n");
+    printf("Please input an cmd:");
+}
+
 
 int check_lock(void)
 {
@@ -50,200 +75,212 @@
 
 }
 
+
+void delete_enter(char *data)
+{
+    char *find = strchr(data, '\n');
+    if(find)
+        *find = '\0';
+    return ;
+}
+
 int main(int argc,char** argv)
 {
 
     int num;
     int ret;
     int len;
+    char cmd[16];
+    char *cmd2;
+    char *cmd1;
+    qser_pm_cfg_t *qser_lpm_cfg = NULL;
+    qser_lpm_cfg = (qser_pm_cfg_t *)malloc(sizeof(qser_pm_cfg_t));
+    qser_lpm_cfg->wakeupin.wakeupin_pin = 50;
+    qser_lpm_cfg->wakeupin.wakeupin_edge = E_QL_LPM_FALLING;
+    while(1)
+    {
+        printUsage();
+        memset(cmd,0,sizeof(cmd));
+        fgets(cmd, sizeof(cmd), stdin);
+        cmd1 = strtok(cmd, " ");
+        cmd2 = strtok(NULL, " ");
+        delete_enter(cmd1);
+        printf("cmd1:%s\n", cmd1);
+        if(strcmp(cmd1,"-i") == 0)
+        {
+            ret = qser_lpm_init( qser_lpm_handler, qser_lpm_cfg);
+            if(ret != 0)
+            {
+                printf("lpm init fail\n");
+                break;
+            }
+            else
+            {
+                printf("lpm init success\n");
+            }
+        }
+        else if(strcmp(cmd1,"-e") == 0)
+        {
+            char num='1';
+            ret = qser_autosuspend_enable(num);
+            if(ret != 0)
+            {
+                printf("qser_autosuspend_enable fail\n");
+            }
+            else
+            {
+                printf("qser_autosuspend_enable success\n");
+            }
+        }
+        else if(strcmp(cmd1,"-cl") == 0)
+        {
+            if(cmd2 == NULL)
+            {
+                printf("name is null\n");
+            }
+            len = strlen(cmd2);
+            printf("len =%d\n", len);
+            ret = qser_wakelock_create(cmd2, len);
+            if(ret < 0)
+            {
+                printf("wakelock create fail\n");
+                printf("ret=%d\n", ret);
+            }
+            else
+            {
+                printf("wakelock create success\n");
+                printf("fd=%d\n", ret);
+            }
+        }
+        else if (strcmp(cmd1,"-al") == 0)
+        {
+             if(cmd2 == NULL)
+            {
+                printf("fd is null\n");
+            }
+            num = atoi(cmd2);
+            ret = qser_wakelock_lock(num);
+            if(ret != 0)
+            {
+                printf("wakelock lock fail\n");
+                printf("ret=%d\n", ret);
+            }
+            else
+            {
+                printf("wakelock lock success\n");
+            }
+        }
 
-    if(argc < 2)
-    {
-        printf("wrong input format, please -h \n");
-        return -1;
-    }
-    if(strcmp(argv[1],"-h") == 0)
-    {
-        printf("        -h                  --help\n");
-        printf("        -i                  --qseq_lpm_init\n");
-        printf("        -d                  --qser_lpm_deinit\n");
-        printf("        -e                  --qser_autosuspend_enable\n");
-        printf("        -cl [wakelock name] --qser_wakelock_create \n");
-        printf("        -al [wakelock num]  --qser_wakelock_lock \n");
-        printf("        -rl [wakelock num]  --qser_wakelock_unlock \n");
-        printf("        -dl [wakelock num]  --qser_wakelock_destroy\n");
-        printf("        -ccl                --check created locks\n");
-        printf("        -cll                --check lockup locks\n");
-        printf("        -wla [id]           --qser_whitelist_add\n");
-        printf("        -wld [id]           --qser_whitelist_del\n");
-        printf("        -wlg                --qser_whitelist_get\n");
-        printf("        -wlc                --qser_whitelist_clear\n");
-    }
-    else if(strcmp(argv[1],"-d") == 0)
-    {
-        ret = qser_lpm_deinit();
-        if(ret != 0)
+        else if (strcmp(cmd1,"-rl") == 0)
         {
-            printf("lpm deinit fail\n");
+            if(cmd2 == NULL)
+            {
+                printf("fd is null\n");
+            }
+            num = atoi(cmd2);
+            ret = qser_wakelock_unlock(num);
+            if(ret != 0)
+            {
+                printf("wakelock unlock fail\n");
+                printf("ret=%d\n", ret);
+            }
+            else
+            {
+                printf("wakelock unlock success\n");
+            }
         }
-        else
+        else if(strcmp(cmd1,"-dl") == 0)
         {
-            printf("lpm deinit success\n");
+            if(cmd2 == NULL)
+            {
+                printf("fd is null\n");
+            }
+            num = atoi(cmd2);
+            ret = qser_wakelock_destroy(num);
+            if(ret != 0)
+            {
+                printf("wakelock destroy fail\n");
+                printf("ret=%d\n", ret);
+            }
+            else
+            {
+                printf("wakelock destroy success\n");
+            }
         }
-    }
-    else if(strcmp(argv[1],"-e") == 0)
-    {
-        char num='1';
-        ret = qser_autosuspend_enable(num);
-        if(ret != 0)
+        else if(strcmp(cmd1,"-ccl") == 0)
         {
-            printf("qser_autosuspend_enable fail\n");
+            int ret;
+            ret = check_lock();
+            if(ret != 0)
+            {
+                printf("check lock fail\n");
+                printf("ret=%d\n", ret);
+            }
+            else
+            {
+                printf("check lock success\n");
+            }
         }
-        else
+        else if(strcmp(cmd1,"-cll") == 0)
         {
-            printf("qser_autosuspend_enable success\n");
+            int ret;
+            ret = system("cat /sys/power/wake_lock");
+            if(ret != 0)
+            {
+                printf("check created lock fail\n");
+            }
         }
-    }
-    else if(strcmp(argv[1],"-cl") == 0)
-    {
-        if(argv[2] == NULL)
+        else if(strcmp(cmd1,"-wla") == 0)
         {
-            printf("name is null\n");
-            return -1;
+            num = atoi(cmd2);
+            ret = qser_whitelist_add(num);
+            if(ret != 0)
+            {
+                printf("mnet_whitelist_add ret %d.\n", ret);
+            }
+
         }
-        len = strlen(argv[2]);
-        printf("len =%d\n", len);
-        ret = qser_wakelock_create(argv[2], len);
-        if(ret < 0)
+        else if(strcmp(cmd1,"-wld") == 0)
         {
-            printf("wakelock create fail\n");
-            printf("ret=%d\n", ret);
+            num = atoi(cmd2);
+            ret = qser_whitelist_del(num);
+            if(ret != 0)
+            {
+                printf("mnet_whitelist_del ret %d.\n", ret);
+            }
         }
-        else
+        else if(strcmp(cmd1,"-wlg") == 0)
         {
-            printf("wakelock create success\n");
-            printf("fd=%d\n", ret);
+            char* whitelist;
+            qser_whitelist_get(whitelist);
+            printf("mnet_whitelist_get :%s\n",whitelist);
         }
-    }
-    else if (strcmp(argv[1],"-al") == 0)
-    {
-        if(argv[2] == NULL)
+        else if(strcmp(cmd1,"-wlc") == 0)
         {
-            printf("fd is null\n");
-            return -1;
+            ret = qser_whitelist_clear();
+            if(ret != 0)
+            {
+                printf("mnet_whitelist_clear ret %d.\n", ret);
+            }
         }
-        num = atoi(argv[2]);
-        ret = qser_wakelock_lock(num);
-        if(ret != 0)
+        else if(strcmp(cmd1, "-d") == 0)
         {
-            printf("wakelock lock fail\n");
-            printf("ret=%d\n", ret);
+            ret = qser_lpm_deinit();
+            if(ret != 0)
+            {
+                printf("lpm deinit fail\n");
+            }
+            else
+            {
+                printf("lpm deinit success\n");
+            }
         }
-        else
+        else if(strcmp(cmd1,"-q") == 0)
         {
-            printf("wakelock lock success\n");
+            break;
         }
     }
 
-    else if (strcmp(argv[1],"-rl") == 0)
-    {
-        if(argv[2] == NULL)
-        {
-            printf("fd is null\n");
-            return -1;
-        }
-        num = atoi(argv[2]);
-        ret = qser_wakelock_unlock(num);
-        if(ret != 0)
-        {
-            printf("wakelock unlock fail\n");
-            printf("ret=%d\n", ret);
-        }
-        else
-        {
-            printf("wakelock unlock success\n");
-        }
-    }
-    else if(strcmp(argv[1],"-dl") == 0)
-    {
-        if(argv[2] == NULL)
-        {
-            printf("fd is null\n");
-            return -1;
-        }
-        num = atoi(argv[2]);
-        ret = qser_wakelock_destroy(num);
-        if(ret != 0)
-        {
-            printf("wakelock destroy fail\n");
-            printf("ret=%d\n", ret);
-        }
-        else
-        {
-            printf("wakelock destroy success\n");
-        }
-    }
-    else if(strcmp(argv[1],"-ccl") == 0)
-    {
-        int ret;
-        ret = check_lock();
-        if(ret != 0)
-        {
-            printf("check lock fail\n");
-            printf("ret=%d\n", ret);
-        }
-        else
-        {
-            printf("check lock success\n");
-        }
-    }
-    else if(strcmp(argv[1],"-cll") == 0)
-    {
-        int ret;
-        ret = system("cat /sys/power/wake_lock");
-        if(ret != 0)
-        {
-            printf("check created lock fail\n");
-        }
-    }
-    else if(strcmp(argv[1],"-wla") == 0)
-    {
-        num = atoi(argv[2]);
-        ret = qser_whitelist_add(num);
-        if(ret != 0)
-        {
-            printf("mnet_whitelist_add ret %d.\n", ret);
-        }
-
-    }
-    else if(strcmp(argv[1],"-wld") == 0)
-    {
-        num = atoi(argv[2]);
-        ret = qser_whitelist_del(num);
-        if(ret != 0)
-        {
-            printf("mnet_whitelist_del ret %d.\n", ret);
-        }
-    }
-    else if(strcmp(argv[1],"-wlg") == 0)
-    {
-        char* whitelist;
-        qser_whitelist_get(whitelist);
-        printf("mnet_whitelist_get :%s\n",whitelist);
-    }
-    else if(strcmp(argv[1],"-wlc") == 0)
-    {
-        ret = qser_whitelist_clear();
-        if(ret != 0)
-        {
-            printf("mnet_whitelist_clear ret %d.\n", ret);
-        }
-    }
-    else
-    {
-        printf("wrong input format, please -h \n");
-        return -1;
-    }
     return 0;
 
 }
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-autosuspend/lynq-qser-autosuspend.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-autosuspend/lynq-qser-autosuspend.cpp
index e82e2c0..5a88ef7 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-autosuspend/lynq-qser-autosuspend.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-autosuspend/lynq-qser-autosuspend.cpp
@@ -7,6 +7,7 @@
 #include <errno.h>
 #include <pthread.h>
 #include<unistd.h>
+#include <cutils/uevent.h>
 
 #include <lynq_autosuspend.h>
 #include "lynq-qser-autosuspend.h"
@@ -25,6 +26,20 @@
 
 #define USER_LOG_TAG "LYNQ_QSER_AUTOSUSPEND"
 #define FILE_LOCK_TABLE "/tmp/.lock_table"
+#define UEVENT_MSG_LEN 128
+
+
+struct dtr_uevent {
+    const char *action;
+    const char *path;
+    const char *subsystem;
+    const char *usbevent;
+};
+
+static void parse_event(const char *msg, struct dtr_uevent *dtr_uevent);
+
+
+qser_lpm_Handler_t g_lpm_handler = NULL;
 
 
 LOCK_TABLE lock_status;
@@ -32,6 +47,7 @@
 
 int file_fd;
 int first_run = 0;
+pthread_t thid;
 
 
 int lock_table_init(void)
@@ -218,19 +234,84 @@
     return ret;
 }
 
+static void parse_event(const char *msg, struct dtr_uevent *dtr_uevent)
+{
+    dtr_uevent->action = "";
+    dtr_uevent->path = "";
+    dtr_uevent->subsystem = "";
+    dtr_uevent->usbevent = "";
+
+    while (*msg) {
+        if (!strncmp(msg, "ACTION=", 7))
+        {
+            msg += 7;
+            dtr_uevent->action = msg;
+        }
+        else if (!strncmp(msg, "DEVPATH=", 8))
+        {
+            msg += 8;
+            dtr_uevent->path = msg;
+        }
+        else if (!strncmp(msg, "SUBSYSTEM=", 10))
+        {
+            msg += 10;
+            dtr_uevent->subsystem = msg;
+        }
+        else if (!strncmp(msg, "USBEVENT=", 9)) {
+            msg += 9;
+            dtr_uevent->usbevent = msg;
+        }
+
+        while(*msg++);
+    }
+
+    LYINFLOG("event { '%s', '%s', '%s', '%s'}\n",
+                    dtr_uevent->action, dtr_uevent->path, dtr_uevent->subsystem,dtr_uevent->usbevent);
+}
+
+
+
+void *check_dtr(void * arg)
+{
+    int device_fd;
+    qser_lpm_edge_t lpm_edge;
+    char msg[UEVENT_MSG_LEN+2];
+    struct dtr_uevent dtr_uevent;
+    int n;
+    int i;
+ 
+    device_fd = uevent_open_socket(4*UEVENT_MSG_LEN, true);
+    if(device_fd < 0)
+    {
+        LYERRLOG("uevent_open_socket fail\n");
+        return -1;
+    }
+    while(1) 
+    {
+        n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN);
+        msg[n] = '\0';
+        msg[n+1] = '\0';
+        parse_event(msg, &dtr_uevent);
+        if((!strcmp(dtr_uevent.subsystem, "xp2xp")) && (!strcmp(dtr_uevent.action, "online")))
+        {
+            g_lpm_handler(lpm_edge);
+        }
+    }
+ 
+    return 0;
+}
+
+
 int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg)
 {
     int ret;
-    ret = system("uci set lynq_uci.lynq_autosuspend.init='1'");
-    system("uci commit");
+    int num;
+
+    g_lpm_handler = qser_lpm_handler;
+    ret = pthread_create(&thid,NULL,check_dtr,NULL);
     if(ret != 0)
     {
-        LYINFLOG("uci set fail");
-    }
-    ret = system("./etc/init.d/lynq-autosuspend.sh restart");
-    if(ret != 0)
-    {
-        LYINFLOG("restart service fail");
+        LYERRLOG("restart service fail");
     }
     return ret;
 }
@@ -239,18 +320,17 @@
 int qser_lpm_deinit(void)
 {
     int ret;
-    system("uci set lynq_uci.lynq_autosuspend.init='0'");
-    system("uci commit");
-    if(ret != 0)
+    ret = pthread_cancel(thid);
+    if(!ret)
     {
-        LYINFLOG("uci set fail");
+        LYERRLOG("pthread cancel success, lpm deinit success\n");
     }
-    system("./etc/init.d/lynq-autosuspend.sh restarts");
-    if(ret != 0)
+    else
     {
-        LYINFLOG("restart service fail");
+        LYERRLOG("pthread cancel fail, lpm deinit fail\n");
     }
-    return 0;
+    return ret;
+
 }
 
 
diff --git a/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/soc/sc/power_4line/hal_xp2xp.c b/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/soc/sc/power_4line/hal_xp2xp.c
index 5e14b7d..7d75efc 100755
--- a/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/soc/sc/power_4line/hal_xp2xp.c
+++ b/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/soc/sc/power_4line/hal_xp2xp.c
@@ -333,6 +333,8 @@
 *

 * Others:

 ********************************************************************************/

+

+/*jb.qi add for dtr uevent on 20231218 start*/

 static int xp2xp_wkcp_thread(void *ptr)

 {

 	int call_state = 0;

@@ -340,11 +342,7 @@
 	int ret = 0;

 	unsigned long flags;

 	struct hal_xp2xp_dev *xp2xp_dev = (struct hal_xp2xp_dev *)ptr;

-#if 0

-	struct sched_param sch_param = { .sched_priority = 1 };

-	sch_param.sched_priority = 40;

-	sched_setscheduler(current, SCHED_FIFO, &sch_param);

-#endif

+

 	while(!kthread_should_stop()){

 		ret = down_interruptible(&xp2xp_dev->wk_sem);

 		

@@ -358,43 +356,32 @@
 			}

 			

 		}

-		spin_lock_irqsave(&xp2xp_dev->wk_lock, flags);	

-			if(xp2xp_dev->wake_state){

+		spin_lock_irqsave(&xp2xp_dev->wk_lock, flags);

+		if(xp2xp_dev->wake_state){

 			spin_unlock_irqrestore(&xp2xp_dev->wk_lock, flags);

-				//now wakeup 

 			xp2xp_notify_up(WAKEUP_NOTIFY);

-			

-			xp2xp_dev->wake_cnt++;	

-#ifdef CONFIG_KERNEL_CONTROL_WAKEUP		

-		 	xp2xp_wakeup_notifier_call_chain(WAKEUP_NOTIFY, &call_state);

-		 	if(call_state == 0){//check device wakeup result

-				xp2xp_SetSleepState(TRUE);

-		 	}

-#endif			

+			xp2xp_dev->wake_cnt++;

+#ifdef CONFIG_KERNEL_CONTROL_WAKEUP

+			xp2xp_wakeup_notifier_call_chain(WAKEUP_NOTIFY, &call_state);

+#endif

 		}else{

-			//now sleep 

 			spin_unlock_irqrestore(&xp2xp_dev->wk_lock, flags);

 			xp2xp_notify_up(SLEEP_NOTIFY);

 			xp2xp_dev->sleep_cnt++;

-#ifdef CONFIG_KERNEL_CONTROL_WAKEUP		

-			xp2xp_SetSleepState(FALSE);

+#ifdef CONFIG_KERNEL_CONTROL_WAKEUP

 		 	xp2xp_wakeup_notifier_call_chain(SLEEP_NOTIFY, &call_state);

-		 	if(call_state){//check device sleep result

-		 	//to do

-		 	}

-#endif		

+#endif

 		}

-			

 

 		enable_irq(xp2xp_dev->eint_irq);

 		xp2xp_dev->xp2xp_irq_state = 1;

-		//pm_relax(xp2xp_dev->dev);				

-		

+

 	}

 	

 	return 0;

 }

-	

+/*jb.qi add for dtr uevent on 20231218 end*/

+

 /*******************************************************************************

 * Function:	ApWakeCp int

 * Description:	ap2cp wake up int isr

@@ -407,7 +394,7 @@
 *

 * Others:

 ********************************************************************************/

-/*jb.qi add for dtr on 20231304 start*/

+/*jb.qi add for dtr uevent on 20231218 start*/

 irqreturn_t xp2xp_wkcp_handler(int irq, void *priv)

 {

 	unsigned long flags;

@@ -436,10 +423,12 @@
 	if(1 == gpio_value){

         irq_type = IRQF_TRIGGER_LOW;

         printk("gpio_value is 1 , release wakelock\n");

+        xp2xp_dev->wake_state = 0;

         pm_relax(g_xp2xp.dev);

 	}else{

         irq_type = IRQF_TRIGGER_HIGH;

         printk("gpio_value is 0 , lock wakelock\n");

+        xp2xp_dev->wake_state = 1;

         pm_stay_awake(g_xp2xp.dev);

 	}

 

@@ -450,7 +439,7 @@
 	up(&xp2xp_dev->wk_sem);

 	return IRQ_HANDLED;

 }

-/*jb.qi add for dtr on 20231304 end*/

+/*jb.qi add for dtr uevent on 20231218 end*/

 

 /*******************************************************************************

 * Function: