/
dd_hrtimer.c
239 lines (183 loc) · 6.27 KB
/
dd_hrtimer.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/workqueue.h>
#include <linux/irq.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/ktime.h>
#define WORKQUEUE_DELAY 200; // 200msec
#define HRTIMER_DELAY 2000 // 200msec unit=1/10ms (100us)
// sysfs file is created in the /sys/devices/virtual/dd_sensors/dd
// drwxr-xr-x 2 root root 0 7月 21 08:02 power/
// lrwxrwxrwx 1 root root 0 7月 21 08:02 subsystem -> ../../../../class/dd_sensors/
// -rw-r--r-- 1 root root 4096 7月 21 08:02 test
// -rw-r--r-- 1 root root 4096 7月 21 08:02 uevent
static struct dd_data* g_data_po;
struct dd_data{
struct class* test_class;
struct device* test_dev;
struct work_struct test1_work;
struct delayed_work test2_delayed_work;
int workqueue_delay;
struct hrtimer polling_timer;
ktime_t polling_delay;
};
//sysfs
static ssize_t test1_show(struct device *dev, struct device_attribute *attr, char *buf)
{
// struct dd_data *data_po = g_data_po;
printk(KERN_ALERT "test1 sysfs show \n");
schedule_delayed_work( &g_data_po->test2_delayed_work,
msecs_to_jiffies( g_data_po->workqueue_delay ) );
schedule_work(&g_data_po->test1_work);
return 0;
}
static ssize_t test1_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
struct dd_data *data_po = g_data_po;
int ret ;
unsigned long n = 0;
if(!buf){
return -EINVAL;
}
ret = strict_strtoul(buf, 10, &n);
if(ret < 0){
printk(KERN_ALERT "invalid value\n");
}
if(n==1)
hrtimer_start(&data_po->polling_timer, data_po->polling_delay, HRTIMER_MODE_REL);
else if(n==0)
{
hrtimer_cancel(&data_po->polling_timer);
flush_work(&data_po->polling_work);
cancel_work_sync(&data_po->polling_work);
}
printk(KERN_ALERT "n = %lu \n", n);
return count;
}
static ssize_t test2_show(struct device *dev, struct device_attribute *attr, char *buf)
{
// struct dd_data *data_po = g_data_po;
printk(KERN_ALERT "test2 sysfs show \n");
return 0;
}
static ssize_t test2_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
// struct dd_data *data_po = g_data_po;
int ret ;
unsigned long n = 0;
if(!buf){
return -EINVAL;
}
ret = strict_strtoul(buf, 10, &n);
if(ret < 0){
printk(KERN_ALERT "invalid value\n");
}
printk(KERN_ALERT "n = %lu \n", n);
return count;
}
static DEVICE_ATTR(test1, 0644, test1_show, test1_store);
static DEVICE_ATTR(test2, 0644, test2_show, test2_store);
static struct attribute *dd_attributes[] =
{
&dev_attr_test1.attr,
&dev_attr_test2.attr,
NULL
};
static struct attribute_group dd_attribute_group =
{
.attrs = dd_attributes
};
static void test1_work_func( struct work_struct *work)
{
// struct dd_data *data = container_of(work,struct dd_data, test1_work);
printk("%s\n",__func__);
}
static void test2_delayed_work_func( struct work_struct *work)
{
// struct dd_data *data = container_of(( struct delayed_work * )work,
// struct dd_data, test2_delayed_work);
printk("%s\n",__func__);
}
static enum hrtimer_restart polling_timer_func(struct hrtimer *timer)
{
struct dd_data *data = container_of(timer, struct dd_data, polling_timer);
if(data!=NULL)
{
schedule_work(&data->test1_work);
hrtimer_forward_now(&data->polling_timer, data->polling_delay);
}
return HRTIMER_RESTART;
}
static int dd_init(void)
{
int ret = 0;
struct dd_data *data_po;
printk(KERN_INFO "****************************** \n");
printk(KERN_INFO " sysfs test driver loaded\n");
printk(KERN_INFO "****************************** \n");
data_po = kzalloc(sizeof(struct dd_data), GFP_KERNEL);
if (!data_po){
return -ENOMEM;
}
data_po->workqueue_delay = WORKQUEUE_DELAY;
hrtimer_init(&data_po->polling_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
data_po->polling_delay = ns_to_ktime(HRTIMER_DELAY * 100 * NSEC_PER_USEC);
data_po->polling_timer.function = polling_timer_func;
g_data_po =data_po;
data_po->test_class =class_create(THIS_MODULE, "dd_sensors");
if(IS_ERR(data_po->test_class)){
ret = PTR_ERR(data_po->test_class);
data_po->test_class = NULL;
// goto err_create_class;
}
// device_create - creates a device and registers it with sysfs
data_po->test_dev = device_create(data_po->test_class, NULL, 0, "%s", "dd");
if(unlikely(IS_ERR(data_po->test_dev))){
ret = PTR_ERR(data_po->test_dev);
data_po->test_dev = NULL;
// goto err_create_test_device;
}
// device_create_file - create sysfs attribute file for device.
// ret =device_create_file(data_po->test_dev, &dev_attr_test);
ret = sysfs_create_group( &data_po->test_dev->kobj, &dd_attribute_group) ;
if(ret){
// goto err_create_test_device_file;
}
INIT_WORK( &data_po->test1_work, test1_work_func);
INIT_DELAYED_WORK( &data_po->test2_delayed_work, test2_delayed_work_func) ;
//err_create_singlethread_workqueue:
return 0;
}
static void dd_exit(void)
{
struct dd_data *data_po = g_data_po;
printk(KERN_INFO "******************************** \n");
printk(KERN_INFO " sysfs test driver unloaded\n");
printk(KERN_INFO "******************************** \n");
hrtimer_cancel(&data_po->polling_timer);
cancel_work_sync(&data_po->polling_work);
flush_scheduled_work();
cancel_delayed_work(&data_po->test2_delayed_work);
// device_remove_file(data_po->test_dev);
sysfs_remove_group( &data_po->test_dev->kobj, &dd_attribute_group) ;
device_unregister(data_po->test_dev); //do work
class_unregister(data_po->test_class);
kfree(data_po);
// class_destroy(data_po->test_class);
// device_destroy(data_po->test_class);
printk(KERN_ALERT "driver unloaded\n");
}
module_init(dd_init);
module_exit(dd_exit);
MODULE_AUTHOR( "Isamu Kawabe <ikwb136@icloud.com>" ) ;
MODULE_DESCRIPTION( "The hrtimer") ;
MODULE_LICENSE( "GPL" ) ;