Merge "[Bugfix][T106]fix always adb device offline"
diff --git a/ap/os/linux/linux-3.4.x/drivers/usb/gadget/adb_server.c b/ap/os/linux/linux-3.4.x/drivers/usb/gadget/adb_server.c
index 8bd460f..b2ed27e 100644
--- a/ap/os/linux/linux-3.4.x/drivers/usb/gadget/adb_server.c
+++ b/ap/os/linux/linux-3.4.x/drivers/usb/gadget/adb_server.c
@@ -38,6 +38,9 @@
  struct adb_dev *g_adb_agent;
 ssize_t adb_server_write2usb( const char *buf,size_t count);
 extern void usb_rpmsg_notify_ap(usb_rpmsg_cmd *notify, int has_param);
+//xf.li@20230614 add for adb offline start
+extern int usb_get_adb_agent(void);
+//xf.li@20230614 add for adb offline end
 
 void adb_recv_from_ap(void *buf, unsigned int len)
 {
@@ -440,7 +443,11 @@
 		//wakeup adb read and return	
 		//dev->rx_done = 1;
 		//wake_up(&dev->read_wq);
-			adb_agent_dequeue_rx(dev);
+		//xf.li@20230614 add for adb offline start
+			if (atomic_read(&dev->read_excl)){
+				adb_agent_dequeue_rx(dev);
+			}
+		//xf.li@20230614 add for adb offline end
 		}else{	
 			//dev->agent_start = 0;
 			//disable agent
@@ -479,8 +486,19 @@
 	return 0;
 }
 EXPORT_SYMBOL_GPL(adb_enable_rpmsg_agent);
-
-
+//xf.li@20230614 add for adb offline start
+void adb_agent_switch_work(struct work_struct *work)
+{
+	struct adb_dev	*dev = container_of(work, struct adb_dev, agent_switch_work);
+	if(usb_get_adb_agent() == 1){
+		printk("agent_switch_work, switch to CAP\n");
+		adb_enable_rpmsg_agent(1);
+		return;
+	}
+	printk("agent_switch_work, switch to AP\n");
+	adb_enable_rpmsg_agent(0);
+}
+//xf.li@20230614 add for adb offline end
 void adb_rpmsg_recv_thread(void *ptr)
 {
 
@@ -611,6 +629,9 @@
 	init_waitqueue_head(&dev->agt_read_wq);
 	init_waitqueue_head(&dev->agt_write_wq);	
 	INIT_WORK(&dev->agent_inform_work, adb_agent_inform_work);
+//xf.li@20230614 add for adb offline start
+	INIT_DELAYED_WORK(&dev->agent_switch_work, adb_agent_switch_work);
+//xf.li@20230614 add for adb offline end
 	atomic_set(&dev->agent_switch, 0);
 	atomic_set(&dev->write_busy, 0);
 	atomic_set(&dev->agt_read_flag, 0);
diff --git a/ap/os/linux/linux-3.4.x/drivers/usb/gadget/f_adb.c b/ap/os/linux/linux-3.4.x/drivers/usb/gadget/f_adb.c
index 4c263e6..75825ef 100644
--- a/ap/os/linux/linux-3.4.x/drivers/usb/gadget/f_adb.c
+++ b/ap/os/linux/linux-3.4.x/drivers/usb/gadget/f_adb.c
@@ -38,7 +38,9 @@
 extern int adb_rpmsg_write2ap(void *buf, unsigned int len);
 extern int adb_agent_close(void);
 extern void adb_server_plug_notify(e_usb_rpmsg_cmd_type plug_type);
-
+//xf.li@20230614 add for adb offline start
+extern int usb_get_adb_agent(void);
+//xf.li@20230614 add for adb offline end
 #endif
 
 static const char adb_shortname[] = "android_adb";
@@ -84,6 +86,7 @@
 	
 	int agent_start;//using for recv thread
 	struct work_struct agent_inform_work;	 /* inform USB adb state */	
+	struct delayed_work agent_switch_work;
 #endif	
 	struct work_struct online_inform;	 /* online inform USB adb  */	
 	struct work_struct offline_inform;	 /* offline inform USB adb  */	
@@ -546,6 +549,11 @@
 		printk("\n adb_open,now is agent mode ,return\n");
 		return -ENODEV;
 	}
+	if(usb_get_adb_agent() == 1){
+		//delay time is num of jiffies
+		printk("---adb_open, switch to cap\n");
+		schedule_delayed_work(&_adb_dev->agent_switch_work, 200);
+	}
 #endif	
 	USBSTACK_DBG("%s", __func__);
 	if (adb_lock(&_adb_dev->open_excl)){
@@ -562,6 +570,12 @@
 	if(atomic_read(&_adb_dev->enable_excl)){
 		_adb_dev->online = 1;
 		wake_up(&_adb_dev->read_wq);
+//xf.li@20230614 add for adb offline start
+	}else{
+
+		adb_unlock(&_adb_dev->open_excl);
+		return -ENODEV;
+//xf.li@20230614 add for adb offline end
 	}
 	usb_record_dbginfo(USB_DEV_OPEN, 0, 3);
 
diff --git a/ap/os/linux/linux-3.4.x/drivers/usb/gadget/usb_netlink.c b/ap/os/linux/linux-3.4.x/drivers/usb/gadget/usb_netlink.c
index 9c157fe..8805fa9 100755
--- a/ap/os/linux/linux-3.4.x/drivers/usb/gadget/usb_netlink.c
+++ b/ap/os/linux/linux-3.4.x/drivers/usb/gadget/usb_netlink.c
@@ -26,6 +26,9 @@
 int zDrvNand_WriteBootflag( int flag );

 #endif

 extern int detected_charger(void);

+//xf.li@20230614 add for adb offline start

+int get_usb_enum_mode(void);

+//xf.li@20230614 add for adb offline end

 #ifdef _USE_VEHICLE_DC

 extern int usb_server_init(void);

 

@@ -563,7 +566,9 @@
 	  }else if(!strcmp(attr->name, USB_ADB_AGENT)){

 #ifdef _USE_VEHICLE_DC

 			  adb_agent_state = adb_rpmsg_agent_state();

-	  		 sprintf(buf, "%u",adb_agent_state);

+//xf.li@20230614 add for adb offline start

+	  		  sprintf(buf, "%u, %s\n",adb_agent_state,( (adb_agent_state == 0) ? "AP" : "CAP"));

+//xf.li@20230614 add for adb offline end

 #endif			 

 	  } 	  

 

@@ -574,6 +579,9 @@
 ssize_t kobj_usb_store(struct kobject *kobject,struct attribute *attr, const char *buf,size_t size)

 {

 	unsigned int value = 0;

+//xf.li@20230614 add for adb offline start

+	int cur_usbmode = 0;

+//xf.li@20230614 add for adb offline end

 	

 	value = simple_strtoul(buf, NULL, 10);

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

@@ -612,8 +620,16 @@
 		usb_gpio_detect_enable =value;

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

 		adb_agent_state =value;

+//xf.li@20230614 add for adb offline start

 #ifdef _USE_VEHICLE_DC

-		adb_enable_rpmsg_agent(adb_agent_state);

+		cur_usbmode = get_usb_enum_mode();

+		adb_agent_state =value;

+		if(cur_usbmode == 0){

+			adb_enable_rpmsg_agent(adb_agent_state);

+		}else{

+			printk("---none adb, switch is forbidern\n");

+		}

+//xf.li@20230614 add for adb offline end

 #endif

 	}

 	

@@ -950,6 +966,14 @@
 EXPORT_SYMBOL_GPL(usb_dbg_showLog);

 

 #ifdef _USE_VEHICLE_DC

+//xf.li@20230614 add for adb offline start

+int usb_get_adb_agent(void)

+{

+	return adb_agent_state;

+}

+

+EXPORT_SYMBOL_GPL(usb_get_adb_agent);

+//xf.li@20230614 add for adb offline end

 void usb_set_rpmsg_resp(int type, char*resp)

 {

 

@@ -978,6 +1002,9 @@
 

 void usb_parse_cap_notify(int type)

 {

+//xf.li@20230614 add for adb offline start

+	int cur_usbmode = 0;

+//xf.li@20230614 add for adb offline end

 	if(type >= USB_RPMSG_NOTIFY_MAX){

 		printk("usb_parse_cap_notify fail, invalid type:%d\n", type);

 		return ;

@@ -1044,6 +1071,24 @@
 			

 			break;

 		

+//xf.li@20230614 add for adb offline start

+		case USB_RPMSG_FORCE_RNDIS:

+		case USB_RPMSG_FORCE_ECM:

+			cur_usbmode = get_usb_enum_mode();	

+			if(cur_usbmode != 0 && 

+				cur_usbmode != 1){

+				printk("usb_parse_cap_notify, none net mode, do nothing\n");

+				break;

+			}

+			printk("cur_usbmode: %s\n", ( (cur_usbmode == 0) ?  "debug": "user"));

+			if(cur_usbmode == 0){

+				switch_mode = USB_SWITCH_DEBUG;

+			}else if( cur_usbmode == 1){

+				switch_mode = USB_SWITCH_USER;

+			}

+			schedule_work(&switch_usbmode);

+			break;	

+//xf.li@20230614 add for adb offline end

 		case USB_RPMSG_OPEN_DL:

 			usb_record_dbginfo(USB_OPEN_DL, 0, 0);

 #ifndef CONFIG_SYSTEM_RECOVERY

diff --git a/ap/project/zx297520v3/prj_vehicle_dc/fs/normal/rootfs/etc/rc b/ap/project/zx297520v3/prj_vehicle_dc/fs/normal/rootfs/etc/rc
index ff19fa3..f9809fc 100755
--- a/ap/project/zx297520v3/prj_vehicle_dc/fs/normal/rootfs/etc/rc
+++ b/ap/project/zx297520v3/prj_vehicle_dc/fs/normal/rootfs/etc/rc
@@ -266,8 +266,13 @@
 sh /sbin/rm_dev.sh
 echo 2000 > /proc/sys/net/nf_conntrack_max
 
+usb_mode=`nv getro usb_modetype`
+#xf.li@20230614 add for adb offline start
+echo "------usb_mode:"
 adb_agent=`nv get adb_core_id`
-
-if [ "$adb_agent" == "1" ]; then
-echo 1 >/sys/dwc_usb/usbconfig/adb_agent
+if [[ $usb_mode == "debug" ]]; then
+	if [ "$adb_agent" == "1" ]; then
+		echo 1 >/sys/dwc_usb/usbconfig/adb_agent
+	fi
 fi
+#xf.li@20230614 add for adb offline end
diff --git a/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/usb/gadget/function/adb_client.c b/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/usb/gadget/function/adb_client.c
index 46b5743..8b5479a 100644
--- a/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/usb/gadget/function/adb_client.c
+++ b/cap/zx297520v3/zxic_code/zxic_source/linux-5.10/drivers/usb/gadget/function/adb_client.c
@@ -30,6 +30,9 @@
 #include <linux/android_notify.h>
 #include <linux/soc/sc/rpmsg.h>
 #include <linux/suspend.h>
+//xf.li@20230614 add for adb offline start
+#include <linux/pm_wakeup.h>
+//xf.li@20230614 add for adb offline end
 
 #define ADB_BULK_BUFFER_SIZE           4096
 #define ADB_RPMSG_BUF_SIZE           512
@@ -71,9 +74,12 @@
 
 	T_RpMsg_Msg rpmsg_tx;
 	char *tx_buf;
-    struct work_struct online_inform_work;	 /* inform USB gserial state( online ) */
+	struct work_struct online_inform_work;	 /* inform USB gserial state( online ) */
 	struct work_struct offline_inform_work;	 /* inform USB gserial state( offline ) */
-    struct device *misc_dev ; 
+	struct device *misc_dev ; 
+//xf.li@20230614 add for adb offline start
+	struct wakeup_source *adb_ws;
+//xf.li@20230614 add for adb offline end
 
 #ifdef CONFIG_PM
 	u32 suspend_state;
@@ -154,8 +160,11 @@
 	struct f_adb *dev = g_adb_dev.adb_dev;
 	if(dev == NULL)
 		return;
-	
-
+//xf.li@20230614 add for adb offline start
+	if(dev->adb_ws){
+		__pm_wakeup_event(dev->adb_ws, 1000);
+	}
+//xf.li@20230614 add for adb offline end
 	if (len==0){
 		//printk("adb_recv_from_ps,len 0 means notify\n");		
 		atomic_set(&dev->write_busy, 0);
@@ -402,7 +411,12 @@
         ret =  -ENODEV;
 		goto exit_done ;
 	}
-    atomic_inc(&g_adb_dev.write_cnt);
+//xf.li@20230614 add for adb offline start
+	if(dev->adb_ws){
+		__pm_wakeup_event(dev->adb_ws, 1000);
+	}	
+//xf.li@20230614 add for adb offline end
+    	atomic_inc(&g_adb_dev.write_cnt);
 	if (!g_adb_dev.adb_dev)
 	{
         ret =  -ENODEV;
@@ -579,7 +593,16 @@
     mutex_unlock(&g_adb_dev.exclude_lock);
 	return ret;
 }
+//xf.li@20230614 add for adb offline start
+int adb_get_state(void)
+{
+	if(g_adb_dev.adb_dev == NULL)
+		return 2;
 
+	return ((g_adb_dev.adb_state == 1)  ?  1 : 2);
+}
+EXPORT_SYMBOL_GPL(adb_get_state);
+//xf.li@20230614 add for adb offline end
 ssize_t adb_state_show(struct device *dev, struct device_attribute *attr,
 			  char *buf)
 {	
@@ -633,13 +656,16 @@
 			printk("schedule_inform_work send online\n");
 			schedule_work(&g_adb_dev.adb_dev->online_inform_work);
 		}else
-			printk("schedule_inform_work already online\n");
-		
+			printk("schedule_inform_work adb already online\n");
+//xf.li@20230614 modify for adb offline start
+		g_adb_dev.adb_state = 1;
 		return;
 	}	
 	printk("schedule_inform_work notify adb offline\n");
 	g_adb_dev.adb_dev->rx_done = 1;
 	g_adb_dev.adb_dev->online = 0;
+	g_adb_dev.adb_state = 0;
+//xf.li@20230614 modify for adb offline end
 	wake_up(&g_adb_dev.adb_dev->read_wq);
 	schedule_work(&g_adb_dev.adb_dev->offline_inform_work);	
 }
@@ -701,6 +727,13 @@
     mutex_lock(&g_adb_dev.exclude_lock);
     atomic_set(&g_adb_dev.read_cnt, 0);
     atomic_set(&g_adb_dev.write_cnt, 0);
+//xf.li@20230614 add for adb offline start
+dev->adb_ws = wakeup_source_register(NULL, "adb_client");
+	if(dev->adb_ws  == NULL){
+		printk("adb_setup wakeup_source_create fail\n");
+		goto err_cb;
+	}
+//xf.li@20230614 add for adb offline end
 	g_adb_dev.adb_dev = dev;
 	ret = misc_register(&adb_device);
     atomic_set(&g_adb_dev.inst_cnt, 1);
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 f6fbd5c..2ee6392 100644
--- 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
@@ -36,6 +36,9 @@
 unsigned int usb_gpio_detect_enable = 0;

 

 unsigned  int  multi_packet_num = 10;

+//xf.li@20230614 add for adb offline start

+extern int adb_get_state(void);

+//xf.li@20230614 add for adb offline end

 

 //1:cap use adb,2:AP use adb 

 #define ADB_SWITCH_TO_CAP 0x1

@@ -295,6 +298,9 @@
 	  }else if(!strcmp(attr->name, USB_RNDIS_PACKET_NUM)){

 	        sprintf(buf, "%u\n",multi_packet_num);

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

+//xf.li@20230614 add for adb offline start

+		adb_state =  adb_get_state();

+//xf.li@20230614 add for adb offline end	

 	        sprintf(buf, "%u,%s\n",adb_state, (adb_state==1 ? "CAP adb": "AP adb"));

 			return strlen(buf);

 	  }

@@ -639,7 +645,7 @@
 module_init(kset_usb_init);

 

 module_exit(kset_usb_exit);

-
+

 //late_initcall(adb_client_init);

 MODULE_LICENSE("GPL");