[Feature][ZXW-214]ZXW's patch query (AP core)USB part switch (on/off) the interface returned the result of the query

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

Change-Id: I6bf15ce18fb794cb2659aeb8a4c85293c1018b59
diff --git a/ap/os/linux/linux-3.4.x/drivers/usb/gadget/android.c b/ap/os/linux/linux-3.4.x/drivers/usb/gadget/android.c
index c6601b5..cb4a4cc 100755
--- a/ap/os/linux/linux-3.4.x/drivers/usb/gadget/android.c
+++ b/ap/os/linux/linux-3.4.x/drivers/usb/gadget/android.c
@@ -3076,7 +3076,12 @@
 {
 	usb_rpmsg_cmd *t_resp = (usb_rpmsg_cmd *)resp;
 	unsigned long flags;
-    struct android_usb_function *func_t;
+	struct android_usb_function *func_t;
+	struct android_usb_function *f;	
+	char *buff = t_resp->param;
+	int n = 0;
+	int max_len = 252;
+	int data_len = 0;	
 	
 	if(!_android_dev){
 		printk("android_set_rpmsg_resp, _android_dev is NULL\n");
@@ -3174,8 +3179,20 @@
 			}			
 			
 			break;
-		//case :
-		//	break;
+		case USB_RPMSG_GET_USB_CURR_CONFIG:
+			
+			t_resp->cmd = USB_RPMSG_GET_USB_CURR_CONFIG;
+			spin_lock_irqsave(&cdev->lock, flags);
+			list_for_each_entry(f, &_android_dev->enabled_functions, enabled_list)
+			{	
+				n = snprintf(buff, (max_len - n), "%s,", f->name);
+				buff += n;
+				data_len += n;
+			}
+			t_resp->param[data_len] = '\0';
+			spin_unlock_irqrestore(&cdev->lock, flags);
+			
+			break;
 		//case :
 		//	break;
 		//case :
diff --git a/ap/os/linux/linux-3.4.x/include/linux/android_notify.h b/ap/os/linux/linux-3.4.x/include/linux/android_notify.h
index 8ac4fe9..c18981d 100755
--- a/ap/os/linux/linux-3.4.x/include/linux/android_notify.h
+++ b/ap/os/linux/linux-3.4.x/include/linux/android_notify.h
@@ -78,6 +78,7 @@
 	USB_RPMSG_GET_USB_STATE,

 	USB_RPMSG_GET_USB_LINK_STATE,

 	USB_RPMSG_GET_USB_ENUM_MODE,

+	USB_RPMSG_GET_USB_CURR_CONFIG,

 	//USB_RPMSG_GET_,

 	//USB_RPMSG_GET_,

 	

diff --git a/cap/zx297520v3/sources/meta-zxic-selinux/recipes-security/refpolicy/files/policy-mls/policy/modules/system/logging.te b/cap/zx297520v3/sources/meta-zxic-selinux/recipes-security/refpolicy/files/policy-mls/policy/modules/system/logging.te
old mode 100755
new mode 100644
index 2672b4a..fe01295
--- a/cap/zx297520v3/sources/meta-zxic-selinux/recipes-security/refpolicy/files/policy-mls/policy/modules/system/logging.te
+++ b/cap/zx297520v3/sources/meta-zxic-selinux/recipes-security/refpolicy/files/policy-mls/policy/modules/system/logging.te
@@ -526,7 +526,7 @@
 	# for systemd-journal
 	allow syslogd_t self:netlink_audit_socket connected_socket_perms;
 	allow syslogd_t self:capability2 audit_read;
-	allow syslogd_t self:capability { chown setgid setuid sys_ptrace dac_read_search };
+	allow syslogd_t self:capability { chown setgid setuid sys_ptrace dac_read_search sys_rawio };
 	allow syslogd_t self:netlink_audit_socket { getattr getopt read setopt write nlmsg_write };
 
 	# remove /run/log/journal when switching to permanent storage
@@ -671,6 +671,7 @@
 allow syslogd_t mnt_t:dir { search };
 allow syslogd_t tmpfs_t:dir { write add_name remove_name search };
 
+allow syslogd_t self:capability { sys_rawio };
 
 
 
diff --git a/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/usb/gadget/usb_netlink.c b/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/usb/gadget/usb_netlink.c
index ad17fd7..4be86bd 100755
--- a/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/usb/gadget/usb_netlink.c
+++ b/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/usb/gadget/usb_netlink.c
@@ -56,6 +56,7 @@
 #define USB_SPEED_TYPE   "usb_speed"

 

 #define USB_SWITCH_MODE   "switch_mode"

+#define USB_CURR_CONFIG   "currcfg"

 #define USB_DL_OPEN   "dl_open"

 

 #define NET_NAME           "netname"

@@ -163,6 +164,11 @@
 		.name = "dl_open",

 		.mode = S_IRUGO|S_IWUSR,

 };

+static struct attribute usb_currcfg_attr =

+{

+		.name = "currcfg",

+		.mode = S_IRUGO|S_IWUSR,

+};

 

 static struct attribute *usb_status_attrs[] =

 {

@@ -177,6 +183,7 @@
 	&usb_adb_switch_attr,

 	&usb_switch_mode_attr,

 	&usb_dl_open_attr,

+	&usb_currcfg_attr,

        NULL,

 };

 

@@ -259,7 +266,8 @@
 	int dc=0;

 	int ret = 0;

 	if(usbnetlink_ws){

-		__pm_wakeup_event(usbnetlink_ws, 3000);

+		//__pm_wakeup_event(usbnetlink_ws, 3000);

+		__pm_stay_awake(usbnetlink_ws);

 	}	

 	  if(!strcmp(attr->name, USB_STATE)){	  	

             //printk("\n[usb_netlink:%s-%d]:plug_state\n",__func__,__LINE__);

@@ -325,6 +333,20 @@
 				goto done;

 			}				

 	  		sprintf(buf, "%s\n",usb_get_resp_data());	  

+	  }else if(!strcmp(attr->name, USB_CURR_CONFIG)){

+			usb_rpmsg_config_cmd(USB_RPMSG_GET_USB_CURR_CONFIG, NULL, 0);

+			usb_schedule_work();

+			ret = wait_event_interruptible(usb_netlink_wq, atomic_read(&resp_done));

+			if (ret < 0 ) {            

+				sprintf(buf, "%s\n", "got fail \n");

+				goto done;

+			}

+			if(usb_get_resp_type() != USB_RPMSG_GET_USB_CURR_CONFIG){

+				printk("GET_USB_CURR_CONFIG invalid resp type\n");           

+				sprintf(buf, "%s\n",  "got fail \n");

+				goto done;

+			}				

+	  		sprintf(buf, "%s\n",usb_get_resp_data());	  

 	  }else if(!strcmp(attr->name, RAMDUMP_FLAG)){

 	  		sprintf(buf, "%d\n",ramdump_flag);

 	  }else if(!strcmp(attr->name, HOT_PLUG)){

@@ -345,6 +367,11 @@
  done:    

  	atomic_set(&resp_done,0);

 	usb_clean_mem();

+

+	if(usbnetlink_ws){

+		//__pm_wakeup_event(usbnetlink_ws, 3000);

+		__pm_relax(usbnetlink_ws);

+	}		

 	return strlen(buf);

 }

 

diff --git a/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/include/linux/android_notify.h b/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/include/linux/android_notify.h
index a95eb06..4e2a0ed 100755
--- a/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/include/linux/android_notify.h
+++ b/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/include/linux/android_notify.h
@@ -67,6 +67,7 @@
 	USB_RPMSG_GET_USB_STATE,
 	USB_RPMSG_GET_USB_LINK_STATE,

 	USB_RPMSG_GET_USB_ENUM_MODE,

+	USB_RPMSG_GET_USB_CURR_CONFIG,

 	USB_RPMSG_CMD_MAX,
 	USB_RPMSG_NOTIFY_ADB_TO_AP,

 	USB_RPMSG_NOTIFY_ADB_TO_CAP,

diff --git a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.c b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.c
index 6a1bd73..8970498 100644
--- a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.c
+++ b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.c
@@ -19,6 +19,7 @@
 #include "sc_usb.h"
 //#include "nv_api.h"
 
+#define BUFF_LEN   256
 
 /*¶ÁдÎļþÏà¹Ø²Ù×÷begin*/
 static int filelength(FILE *fp)
@@ -178,5 +179,43 @@
 	sc_usb_set_netcardtype(USB_NET_ECM);
 }
 
+int sc_usb_usermode_net_state(void)
+{
+	char curr_cfg[BUFF_LEN] = {0};
+	int ret = 0;
+	memset(curr_cfg, 0, BUFF_LEN);
+	ret = readfile(USB_ATTR_CURRCFG, curr_cfg, BUFF_LEN);
+	if(ret < 0){
+		printf("sc_usb_usermode_adb_state fail\n");
+		goto END;
+	}
+	ret = 0;
+	
+	if(strstr(curr_cfg, "rndis")  || strstr(curr_cfg, "ecm"))
+		return 1;
+	
+END:
+	return ret;
+}
 
 
+int  sc_usb_usermode_adb_state(void)
+{
+	
+	char curr_cfg[BUFF_LEN] = {0};
+	int ret = 0;
+	memset(curr_cfg, 0, BUFF_LEN);
+	ret = readfile(USB_ATTR_CURRCFG, curr_cfg, BUFF_LEN);
+	if(ret < 0){
+		printf("sc_usb_usermode_adb_state fail\n");
+		goto END;
+	}
+	ret = 0;
+	
+	if(strstr(curr_cfg, "adb"))
+		return 1;
+	
+END:
+	return ret;
+}
+
diff --git a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.c.orig b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.c.orig
new file mode 100644
index 0000000..6a1bd73
--- /dev/null
+++ b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.c.orig
@@ -0,0 +1,182 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <unistd.h>
+
+//add for cmux
+#include <sys/socket.h>
+#include <linux/if.h>
+#include <linux/gsmmux.h>
+#include <linux/netlink.h>
+#include <poll.h>
+#include <linux/serial.h>
+#include <termios.h>
+#include "sc_usb.h"
+//#include "nv_api.h"
+
+
+/*¶ÁдÎļþÏà¹Ø²Ù×÷begin*/
+static int filelength(FILE *fp)
+{
+    int num;
+    fseek(fp,0,SEEK_END);
+    num=ftell(fp);
+    fseek(fp,0,SEEK_SET);
+    return num;
+}
+
+int readfile(char *path, char* buf, unsigned len)
+{
+    FILE *fp;
+    unsigned int length;
+    if((fp=fopen(path,"r"))==NULL)
+    {
+        //slog(USBCFGMNG_PRINT,SLOG_ERR, "[usbCfgMng] open file %s error.\n",path);
+        return -1;
+    }
+    length=filelength(fp);
+    length = length > len? len: length;
+    //ch=(char *)malloc(length+1);
+    int read_len = fread(buf,length,1,fp);
+    if(read_len < 1)  // cov M CHECKED_RETURN
+    {
+        //slog(SDCARD_PRINT, SLOG_ERR, "fread %s error.\n",path); 
+    }
+	
+    fclose(fp);
+    *(buf+length) = '\0';
+    return (int)length;
+}
+
+
+
+int writefile(char*path, char*buf, unsigned len)
+{
+    FILE *fp;
+    int rtv = 0;
+    if((fp=fopen(path,"w"))==NULL)
+    {
+        return -1;
+    }
+    rtv = fwrite(buf,len,1, fp);
+    fclose(fp);
+    return rtv;
+}
+
+
+void sc_usb_dl_open(void)
+{
+	writefile(USB_ATTR_DL_OPEN, "1", 1);
+}
+
+void sc_usb_dl_close(void)
+{
+	writefile(USB_ATTR_DL_OPEN, "0", 1);
+}
+
+int sc_usb_get_mode(char* buf, int len)
+{
+	int ret = 0;
+	ret = readfile(USB_ATTR_SWITCH_MODE, buf, len);
+	return ret;
+}
+
+int sc_usb_switch_mode(char *type)
+{
+	int ret = 0;
+	
+	ret = writefile(USB_ATTR_SWITCH_MODE, type, strlen(type));
+	return ret;
+}
+
+void sc_usb_set_usermode(void)
+{
+	sc_usb_switch_mode("1");
+}
+
+void sc_usb_user_closedl(void)
+{
+	sc_usb_dl_close();
+	sc_usb_switch_mode("1");
+}
+
+void sc_usb_set_debugmode(void)
+{
+	sc_usb_switch_mode("0");
+
+}
+
+void sc_usb_debug_opendl(void)
+{
+	sc_usb_dl_open();
+	sc_usb_switch_mode("0");
+
+}
+void sc_usb_set_amtmode(void)
+{
+	sc_usb_switch_mode("3");
+}
+
+void sc_usb_set_factorymode(void)
+{
+	sc_usb_switch_mode("2");
+}
+
+void sc_usb_set_eyemode(void)
+{
+	sc_usb_switch_mode("4");
+}
+
+
+int sc_usb_get_usbstate(char* buf, int len)
+{
+	int ret = 0;
+	ret = readfile(USB_ATTR_USB_STATE, buf, len);
+	return ret;
+}
+
+int sc_usb_get_usb_speed(char* buf, int len)
+{
+	int ret = 0;
+	ret = readfile(USB_ATTR_SPEED, buf, len);
+	return ret;
+}
+
+
+int sc_usb_get_netname(char* buf, int len)
+{
+	int ret = 0;
+	ret = readfile(USB_ATTR_NETNAME, buf, len);
+	return ret;
+}
+
+
+int sc_usb_set_netcardtype(e_usb_net_type type)
+{
+	int ret = 0;
+	if(type == USB_NET_RNDIS)
+		ret = writefile(USB_ATTR_FORCENET, "0", 1);
+	else
+		ret = writefile(USB_ATTR_FORCENET, "1", 1);
+	return ret;
+}
+
+void sc_usb_set_net_rndis(void)
+{
+	//nv_set_item(NV_RO, STR_FORCE_NETCARD_TYPE, "force_rndis", 1);
+	sc_usb_set_netcardtype(USB_NET_RNDIS);
+}
+
+void sc_usb_set_net_ecm(void)
+{
+	//nv_set_item(NV_RO, STR_FORCE_NETCARD_TYPE, "force_ecm", 1);
+	sc_usb_set_netcardtype(USB_NET_ECM);
+}
+
+
+
diff --git a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.h b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.h
index 32820a3..d24f750 100644
--- a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.h
+++ b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/sdk/libbsp/sc_usb.h
@@ -28,7 +28,8 @@
 #define USB_ATTR_NETNAME           "/sys/gen_usb/usb_state/netname"

 #define USB_ATTR_FORCENET          "/sys/gen_usb/usb_state/forcenet"

 #define USB_ATTR_SIMULATE_PLUG     "/sys/gen_usb/usb_state/usb_simulate_plug"

-#define USB_ATTR_BOOT_MODE         "/sys/gen_usb/usb_state/boot_mode"    

+#define USB_ATTR_BOOT_MODE         "/sys/gen_usb/usb_state/boot_mode"  

+#define USB_ATTR_CURRCFG           "/sys/gen_usb/usb_state/currcfg"  

 #define STR_FORCE_NETCARD_TYPE           "forcenetcard_type" 

 

 #define USB_ATTR_FAST_POWEROFF     "/sys/gen_usb/usb_state/fast_poweroff"

@@ -128,6 +129,8 @@
 

 
 void sc_usb_set_net_ecm(void);

+int  sc_usb_usermode_net_state(void);

+int  sc_usb_usermode_adb_state(void);

 

 

 

diff --git a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/test/usbtest/usbtest.c b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/test/usbtest/usbtest.c
index 998ee5d..6955ce8 100755
--- a/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/test/usbtest/usbtest.c
+++ b/cap/zx297520v3/zxic_code/zxic_source/zxic_app_open/test/usbtest/usbtest.c
@@ -408,6 +408,39 @@
 	printf("do_set_netcardtype test fail\n");

 	

 }

+

+

+void do_get_adb_state(int nargs, char **argv)

+{

+	

+	int ret = 0;

+	

+	ret = sc_usb_usermode_adb_state();

+	if(ret < 0){

+		printf("get adb state fail,ret:%d \n", ret);

+		return;

+	}

+	

+	printf("adb state:%d, %s\n", ret, ((ret == 0) ? "off" : "on"));

+	

+}

+

+void do_get_net_state(int nargs, char **argv)

+{

+	

+	int ret = 0;

+	

+	ret = sc_usb_usermode_net_state();

+	if(ret < 0){

+		printf("get net state fail,ret:%d \n", ret);

+		return;

+	}

+	

+	printf("net state:%d, %s\n", ret, ((ret == 0) ? "off" : "on"));

+	

+}

+

+

 void do_monitor_usb_plug(int nargs, char **argv)

 {

 	int ret;
@@ -477,6 +510,16 @@
 		"monitor usb plug in/plug out",

 	  NULL, NULL, 0

 	},

+	{ do_get_adb_state, -0, 

+		"get_adb_state","\n"

+		"get adb state.",

+	  NULL, NULL, 0

+	},

+	{ do_get_net_state, -0, 

+		"get_net_state","\n"

+		"get net state.",

+	  NULL, NULL, 0

+	},

 	{ 0, 0, 0, 0, NULL, NULL, 0 }

 };