-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_node.c
More file actions
executable file
·151 lines (126 loc) · 3.56 KB
/
sys_node.c
File metadata and controls
executable file
·151 lines (126 loc) · 3.56 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/vmalloc.h>
#include <linux/device.h>
#include <linux/jiffies.h>// for jiffies
#include <linux/kernel.h>
#define MAX_COOKIE_SIZE PAGE_SIZE
/* class start */
static struct class sys_class={
.name = "sys_class",
};
static ssize_t sys_class_show(struct class *class, struct class_attribute *attr,char *buf)
{
printk("sys_class_show!\n");
snprintf(buf,PAGE_SIZE,"sys_class_show!\n");
return strlen(buf);
}
static ssize_t sys_class_store(struct class *class, struct class_attribute *attr,const char *buf, size_t count)
{
printk("%s\n",buf);
return count;
}
static CLASS_ATTR(sys_class,S_IRUGO|S_IWUSR,sys_class_show,sys_class_store);
/* class end */
/* bus type start */
int ldd_match(struct device *dev, struct device_driver *drv)
{
return !strcmp(dev->init_name,drv->name);
}
int ldd_uevent(struct device *dev, struct kobj_uevent_env *env)
{
if(add_uevent_var(env,"LDD-VERSION:%s\n","V1.0"))
return -ENOMEM;
return 0;
}
struct bus_type ldd_bus_type={
.name = "ldd",
.match = ldd_match,
.uevent = ldd_uevent,
};
static ssize_t ldd_bus_show(struct bus_type *bus,char *buf)
{
printk("ldd_bus_type show!\n");
return sprintf(buf,"ldd_bus_type show!\n");
}
static BUS_ATTR(ldd_bus,S_IRUGO,ldd_bus_show,NULL);
/* bus type end */
/* device start */
static void ldd_bus_release(struct device *dev)
{
printk("ldd_bus_release!\n");
}
struct device ldd_bus={
.init_name="ldd0",
.release = ldd_bus_release,
.class = &sys_class,
};
static ssize_t sys_show(struct device *dev, struct device_attribute *attr,char *buf)
{
printk("sys_show!\n");
snprintf(buf,PAGE_SIZE,"sys_show!\n");
return strlen(buf);
}
static ssize_t sys_store(struct device *dev, struct device_attribute *attr,const char *buf, size_t count)
{
printk("%s\n",buf);
return count;
}
static DEVICE_ATTR(sys_node, S_IRUGO | S_IWUSR, sys_show, sys_store);
/* device end */
/* driver start */
static struct device_driver sys_driver={
.name = "sys_driver",
.bus = &ldd_bus_type,
};
static ssize_t sys_driver_show(struct device_driver *driver, char *buf)
{
printk("sys_driver_show!\n");
snprintf(buf,PAGE_SIZE,"sys_driver_show!\n");
return strlen(buf);
}
static ssize_t sys_driver_store(struct device_driver *driver, const char *buf,size_t count)
{
printk("%s\n",buf);
return count;
}
static DRIVER_ATTR(sys_driver,S_IRUGO|S_IWUSR,sys_driver_show,sys_driver_store);
/* driver end */
static int __init sys_init(void)
{
int ret=0;
ret = class_register(&sys_class);
if(ret)
printk("Unable to register sys_class!\n");
ret = class_create_file(&sys_class,&class_attr_sys_class);
ret = bus_register(&ldd_bus_type);
if(ret)
printk("Unable to register ldd_bus_type!\n");
ret = bus_create_file(&ldd_bus_type,&bus_attr_ldd_bus);
ret = device_register(&ldd_bus);
if(ret)
printk("Unable to register ldd_bus!\n");
ret = device_create_file(&ldd_bus,&dev_attr_sys_node);
ret = driver_register(&sys_driver);
if(ret)
printk("Unable to register sys_driver!\n");
ret = driver_create_file(&sys_driver,&driver_attr_sys_driver);
printk(KERN_ERR "HELLO INIT!\n");
return ret;
}
static void __exit sys_exit(void)
{
printk(KERN_ERR "HELLO EXIT!111111111111111111\n");
//device_remove_file(&ldd_bus,&dev_attr_sys_node);
driver_unregister(&sys_driver);
printk(KERN_ERR "HELLO EXIT!222222222222222222\n");
device_unregister(&ldd_bus);
printk(KERN_ERR "HELLO EXIT!333333333333333333\n");
bus_unregister(&ldd_bus_type);
class_unregister(&sys_class);
}
module_init(sys_init);
module_exit(sys_exit);
MODULE_LICENSE("Dual BSD/GPL");