[Feature][ZXW-137][AUTOSUSPEND]add usb wakelock

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: I1710076b6b491c8fdd2c7c1f62f86bd5fb04b367
diff --git a/ap/app/zte_comm/zte_drv_ctrl/usb/zte_drv_usb_ctrl.c b/ap/app/zte_comm/zte_drv_ctrl/usb/zte_drv_usb_ctrl.c
index d266663..35154d0 100755
--- a/ap/app/zte_comm/zte_drv_ctrl/usb/zte_drv_usb_ctrl.c
+++ b/ap/app/zte_comm/zte_drv_ctrl/usb/zte_drv_usb_ctrl.c
@@ -1819,6 +1819,74 @@
     return 0;

 }

 

+/*jb.qi add for usb wakelock on 20230918 start*/

+#define RPMSG_IOC_MAGIC     'R'

+

+/*ioctl cmd usd by device*/

+#define RPMSG_CREATE_CHANNEL          _IOW(RPMSG_IOC_MAGIC, 1, char *)

+#define RPMSG_GET_DATASIZE       _IOWR(RPMSG_IOC_MAGIC, 2, char *)

+#define RPMSG_SET_INT            _IOW(RPMSG_IOC_MAGIC, 3, char *)

+#define RPMSG_SET_INT_FLAG        _IOW(RPMSG_IOC_MAGIC, 4, char *)

+#define RPMSG_CLEAR_INT_FLAG      _IOW(RPMSG_IOC_MAGIC, 5, char *)

+#define RPMSG_SET_POLL_FLAG       _IOW(RPMSG_IOC_MAGIC, 6, char *)

+#define RPMSG_CLEAR_POLL_FLAG     _IOW(RPMSG_IOC_MAGIC, 7, char *)

+

+#define USB_PLUG_WAKE_CAP_DEV    "/dev/rpmsg50"

+

+int usbplug_fd = -1;

+

+int usbplug_icp_init(void) 

+{

+    usbplug_fd = open(USB_PLUG_WAKE_CAP_DEV, O_RDWR);

+	

+    if(0 > usbplug_fd){

+        printf("%s: open the channel(%s) error!\n", __func__, USB_PLUG_WAKE_CAP_DEV);

+        return -1;

+    }

+

+    if(0 > ioctl(usbplug_fd, RPMSG_CREATE_CHANNEL, 64)){

+        printf("%s: ioctl RPMSG_CREATE_CHANNEL fail!\n", __func__);

+        close(usbplug_fd);

+        usbplug_fd = -1;

+        return -1;

+    }

+

+    if(0 > ioctl(usbplug_fd, RPMSG_SET_INT_FLAG, NULL)){ //¿¿¿

+        printf("%s: ioctl RPMSG_SET_INT_FLAG fail!\n", __func__);

+        close(usbplug_fd);

+        usbplug_fd = -1;

+        return -1;

+    }

+

+    printf("yanming %s create success!\n",__func__);

+	

+    return 0;

+}

+

+int usbplug_rpmsg_send(int value)

+{

+    int write_len = 0;

+    char usbplug_in[20]="usbplug_in";

+    char usbplug_out[20]="usbplug_out";

+	

+	printf("yanming usbplug_rpmsg_send value:%d\n",value);

+

+    if(value == 1){

+    write_len = write(usbplug_fd, usbplug_in, sizeof(usbplug_in));

+    }

+

+    if(value == 0){

+    write_len = write(usbplug_fd, usbplug_out, sizeof(usbplug_out));

+    }

+

+	if(write_len <0){

+	 	printf("yanming usbplug rpmsg write FAIL\n");

+		close(usbplug_fd);

+		}

+

+    return write_len;

+}

+/*jb.qi add for usb wakelock on 20230918 end*/

 

 /*usbÈȰβåʼþ´¦Àí*/

 int usb_event_proc(unsigned short usb_msg, int usb_event)

@@ -1850,6 +1918,7 @@
         {

             slog(USBCFGMNG_PRINT,SLOG_NORMAL, "[usbCfgMng] usb charger plugin \n");

         }

+	usbplug_rpmsg_send(1);//jb.qi add for usb wakelock on 20230918

         break;

     case MSG_CMD_DRV_USB_REMOVE:

         /*°Î³öʼþ´¦Àí*/

@@ -1867,6 +1936,7 @@
         {

             slog(USBCFGMNG_PRINT,SLOG_NORMAL, "[usbCfgMng] usb charger plugOut \n");

         }

+	usbplug_rpmsg_send(0);//jb.qi add for usb wakelock on 20230918

         break;

     default:

         break;

@@ -2020,6 +2090,15 @@
         slog(USBCFGMNG_PRINT,SLOG_ERR, "[usbCfgMng] warning: readfile %s fail \n", USB_PLUG_FILE_NAME);

         return -1;

     }

+    /*jb.qi add for usb wakelock on 20230918 start*/

+    printf("usbPlugtype:%d\n",usbPlugtype);

+    if(usbPlugtype == 1)

+	sc_cfg_set("usbplug_nv","1");

+   

+    if(usbPlugtype == 0)

+	sc_cfg_set("usbplug_nv","0");

+    usbplug_icp_init();

+    /*jb.qi add for usb wakelock on 20230918 end*/

     usb_usbCfgMngInit(usbPlugtype);

     if (usbPlugtype == 1 && cp_need_udisk == 1)

 		usb_sendUsbOperateRlt(2);

diff --git a/ap/os/linux/linux-3.4.x/drivers/usb/dwc_otg/dwc_otg_chg_identify.c b/ap/os/linux/linux-3.4.x/drivers/usb/dwc_otg/dwc_otg_chg_identify.c
index 82f129a..a0057aa 100755
--- a/ap/os/linux/linux-3.4.x/drivers/usb/dwc_otg/dwc_otg_chg_identify.c
+++ b/ap/os/linux/linux-3.4.x/drivers/usb/dwc_otg/dwc_otg_chg_identify.c
@@ -184,7 +184,7 @@
 
 //for mdl,enum usb when poweron,maybe no gpio detect	
 #ifndef _USE_VEHICLE_DC_REF
-	return 1;
+	//return 1; jb.qi add for usb wakelock on 20230918
 #else
 //others ,depends gpio detect
 	return usb_plugin;
@@ -396,7 +396,7 @@
     DWC_WRITE_REG32(core_if->pcgcctl, 0);
 
     /* Device configuration register */
-    init_chg_devspd(core_if,0);//ĬÈÏÅäÖóɸßËÙ
+    init_chg_devspd(core_if,0);//Ĭ\C8\CF\C5\E4\D6óɸ\DF\CB\D9
     dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
     dcfg.b.descdma = 0;
     dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
@@ -633,7 +633,7 @@
         case DWC_SLAVE_ONLY_ARCH:
             
     		//ahbcfg.b.nptxfemplvl_txfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
-            //  ÅäÖÃÈ«¿ÕFIFO²úÉúÖжÏ
+            //  \C5\E4\D6\C3ȫ\BF\D5FIFO\B2\FA\C9\FA\D6ж\CF
             ahbcfg.b.nptxfemplvl_txfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_EMPTY ;
     		ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
     		core_if->dma_enable = 0;
@@ -688,7 +688,7 @@
 
 
     ahbcfg.b.dmaenable = 0;
-//  ÅäÖÃÈ«¿ÕFIFO²úÉúÖжÏ
+//  \C5\E4\D6\C3ȫ\BF\D5FIFO\B2\FA\C9\FA\D6ж\CF
     ahbcfg.b.nptxfemplvl_txfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_EMPTY ;
 #endif
 
@@ -1047,8 +1047,8 @@
 extern unsigned int get_usb_gpio_detect_flag(void);
 //wangzhen
 extern int dwc_otg_disconnect(void);
-/*GPIOºÍÍⲿÖжϺŸù¾ÝÏîĿʵ¼ÊÇé¿öÐÞ¸Ä
- *´Ë´¦Îª²Î¿¼´úÂë
+/*GPIO\BA\CD\CDⲿ\D6жϺŸ\F9\BE\DD\CF\EEĿʵ\BC\CA\C7\E9\BF\F6\D0޸\C4
+ *\B4˴\A6Ϊ\B2ο\BC\B4\FA\C2\EB
  */
 //xf.li@20230909 modify for usb hot-plug start
  #ifdef _USE_VEHICLE_DC_REF
@@ -1254,4 +1254,4 @@
 
 module_init(usb_detect_init);
 
-#endif
\ No newline at end of file
+#endif
diff --git a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/zxic_mainctrl/zxic_mainctrl.c b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/zxic_mainctrl/zxic_mainctrl.c
index 816e85f..e8cdc65 100755
--- a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/zxic_mainctrl/zxic_mainctrl.c
+++ b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/platform/zxic_mainctrl/zxic_mainctrl.c
@@ -1584,6 +1584,134 @@
 	}
 }
 
+/*jb.qi add for usb wake lock on 20230918 start*/
+static void write_lockfile(char *filepath, char *setbuf)
+{
+    int f = -1;
+
+    int len = 0;
+    f = open(filepath, O_RDWR);
+
+    if (f < 0)
+    {
+        printf("usbplug wakelock open error");
+        return;
+    }
+    len = strlen(setbuf);
+
+    if (write(f, setbuf, len) != len)
+    {
+        printf("usbplug wakelock write fail\n");
+    }
+    close(f);
+}
+
+
+
+static void wake_usbplug_lock()
+{
+    write_lockfile("/sys/power/wake_lock", "usb_wakelock");
+}
+
+static void wake_usbplug_unlock()
+{
+    write_lockfile("/sys/power/wake_unlock", "usb_wakelock");
+}
+
+#define RPMSG_IOC_MAGIC     'R'
+
+/*ioctl cmd usd by device*/
+#define RPMSG_CREATE_CHANNEL          _IOW(RPMSG_IOC_MAGIC, 1, char *)
+#define RPMSG_GET_DATASIZE       _IOWR(RPMSG_IOC_MAGIC, 2, char *)
+#define RPMSG_SET_INT            _IOW(RPMSG_IOC_MAGIC, 3, char *)
+#define RPMSG_SET_INT_FLAG        _IOW(RPMSG_IOC_MAGIC, 4, char *)
+#define RPMSG_CLEAR_INT_FLAG      _IOW(RPMSG_IOC_MAGIC, 5, char *)
+#define RPMSG_SET_POLL_FLAG       _IOW(RPMSG_IOC_MAGIC, 6, char *)
+#define RPMSG_CLEAR_POLL_FLAG     _IOW(RPMSG_IOC_MAGIC, 7, char *)
+
+void *recv_usbplug_entry(void *arg)
+{
+	int charging_netlink = -1;
+	int fd_counts = -1;
+	char buf[20] = {0};
+
+	char *buf_temp;
+    int len = 0;
+	int ret = 0;
+	fd_set readfds;
+	CHAR usbplug_nv[8] = {0};
+
+	printf("recv_usbplug_entry \n");
+
+	prctl(PR_SET_NAME, "usbplug", 0, 0, 0);
+       int fd = open("/dev/armps_rpmsgch50", O_RDWR);
+	if (fd < 0) {
+		printf("usbplug open armps_rpmsgch50 wrong \n");
+		return NULL;
+	}
+	  ret = ioctl(fd, RPMSG_CREATE_CHANNEL, 64); // cov M CHECKED_RETURN
+        if(ret<0){
+	 	printf("usbplug cap add  ioctl create ch FAIL\n");
+		close(fd);
+		return ret;
+
+	}
+	ret=ioctl(fd, RPMSG_SET_INT_FLAG, NULL);
+
+	if(ret<0){
+	 	printf("usbplug cap add ioctl set int FAIL\n");
+		close(fd);
+
+		return ret;
+	}
+	sc_cfg_get("usbplug_nv",usbplug_nv,sizeof(usbplug_nv));
+
+	if(!strcmp(usbplug_nv,"1")){
+		printf("yanming usbplug in state\n");
+		wake_usbplug_lock();
+		}
+
+	while (1) {
+		FD_ZERO(&readfds);
+		FD_SET(fd, &readfds);
+
+		fd_counts = select(fd + 1, &readfds, NULL, NULL, NULL);
+		if (fd_counts < 0)
+			continue;
+
+		if (FD_ISSET(fd, &readfds)) {
+			memset(buf, 0, sizeof(buf));
+			len = read(fd, &buf, sizeof(buf));
+			if (len <= 0) {
+				printf("usbplug recv msg fail!\n");
+				continue;
+			}
+
+			buf_temp = malloc(len+1);
+
+	    if(NULL == buf_temp)
+	    {
+	        assert(0);
+	    }
+
+	    memset(buf_temp, 0x00, len+1);
+	    memcpy(buf_temp, buf, len);
+
+			printf("yanming usbplug recv buf:%s\n",buf_temp);
+			if(!strcmp(buf_temp,"usbplug_in")){
+						wake_usbplug_lock();
+						printf("yanming usbplug lock\n");
+				}
+
+			if(!strcmp(buf_temp,"usbplug_out")){
+						wake_usbplug_unlock();
+						printf("yanming usbplug unlock\n");
+				}
+		}
+	}
+}
+/*jb.qi add for usb wake lock on 20230918 end*/
+
 int main(int argc, char * argv[])
 {
 	int iMsgHandle = 0;
@@ -1601,9 +1729,16 @@
 	slog(NET_PRINT, SLOG_ERR, "zxic_mainctrl begin ======= build date: %s %s \n", __DATE__, __TIME__);
 
 	pthread_t zte_security_log_tid;
+	pthread_t zte_usbplug_log_tid;//jb.qi add for usb wake lock on 20230918 end
+
 	if (pthread_create(&zte_security_log_tid, NULL, (void *)zte_security_log_entry, NULL) != 0) //cov m
 		slog(NET_PRINT, SLOG_ERR, "security_log_entry create fail\n");
 
+	/*jb.qi add for usb wake lock on 20230918 start*/
+	if (pthread_create(&zte_usbplug_log_tid, NULL, (void *)recv_usbplug_entry, NULL) != 0)
+				printf("recv_usbplug_entry create fail\n");
+	/*jb.qi add for usb wake lock on 20230918 end*/
+
 	iMsgHandle = create_msg_queue();
 	if (-1 == iMsgHandle) {
 		slog(NET_PRINT, SLOG_ERR, "can not create msg queue for router!\n");