Kernel添加全局变量

参照下面的patch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
diff --git a/arch/arm/configs/xxx_defconfig b/arch/arm/configs/xxx_defconfig
index de0e7bf..93d7202 100644
--- a/arch/arm/configs/xxx_defconfig
+++ b/arch/arm/configs/xxx_defconfig
@@ -111,4 +111,5 @@ CONFIG_HAS_IOPORT_MAP=y
CONFIG_HAS_DMA=m
CONFIG_CPU_RMAP=y
CONFIG_NLATTR=y
+CONFIG_ADD_GLOBAL_VAR=y
# CONFIG_AVERAGE is not set
diff --git a/drivers/usb/core/Kconfig b/drivers/usb/core/Kconfig
index cc0ced0..60d968a 100755
--- a/drivers/usb/core/Kconfig
+++ b/drivers/usb/core/Kconfig
@@ -84,3 +84,8 @@ config USB_OTG_FSM
Implements OTG Finite State Machine as specified in On-The-Go
and Embedded Host Supplement to the USB Revision 2.0 Specification.

+config ADD_GLOBAL_VAR
+ tristate "add global variable"
+ depends on USB
+ help
+ add global variable.
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 8d5b2f4..a4ade71 100755
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -1039,6 +1039,10 @@ static void usb_debugfs_cleanup(void)
debugfs_remove(usb_debug_root);
}

+#ifdef CONFIG_ADD_GLOBAL_VAR
+extern unsigned int k_g_usb_state;
+#endif
+
/*
* Init
*/
@@ -1049,6 +1053,10 @@ static int __init usb_init(void)
pr_info("%s: USB support disabled\n", usbcore_name);
return 0;
}
+
+ #ifdef CONFIG_ADD_GLOBAL_VAR
+ printk("[xxx] get global var,usb_state = %d\n", k_g_usb_state);
+ #endif
usb_init_pool_max();

retval = usb_debugfs_init();
@@ -1099,12 +1107,13 @@ out:
/*
* Cleanup
*/
+
static void __exit usb_exit(void)
{
/* This will matter if shutdown/reboot does exitcalls. */
if (usb_disabled())
return;
-
+
usb_deregister_device_driver(&usb_generic_driver);
usb_major_cleanup();
usb_deregister(&usbfs_driver);
diff --git a/init/main.c b/init/main.c
index 1ed674b..196de9a 100755
--- a/init/main.c
+++ b/init/main.c
@@ -110,6 +110,11 @@ bool early_boot_irqs_disabled __read_mostly;
enum system_states system_state __read_mostly;
EXPORT_SYMBOL(system_state);

+#ifdef CONFIG_ADD_GLOBAL_VAR
+unsigned int k_g_usb_state = 2;
+EXPORT_SYMBOL(k_g_usb_state);
+#endif
+
/*
* Boot command-line arguments
*/

Title:Kernel添加全局变量

Author:Victor Huang

Time:2019-06-09 / 17:06

Link:http://wowothink.com/2bec1a3f/

License: Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)