-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdata.c
325 lines (262 loc) · 6.71 KB
/
cdata.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/vmalloc.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/input.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/debugfs.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include "cdata_ioctl.h"
#define CDATA_MAJOR 121
#define BUF_SIZE 8
#ifdef __USE_FBMEM__
#define FRAMEBUFFER_SIZE (640*480*1)
static unsigned int framebuffer_off;
#endif
static DEFINE_MUTEX(ioctl_lock);
static struct dentry *debugfs;
void *write_framebuffer_with_timer(unsigned long);
void write_framebuffer_with_work(struct work_struct *);
struct cdata_t {
unsigned char buf[BUF_SIZE];
int idx;
wait_queue_head_t writeable;
struct timer_list timer;
struct work_struct work;
struct mutex write_lock;
spinlock_t lock;
#ifdef __USE_FBMEM__
unsigned char *iomem;
#endif
};
static int cdata_open(struct inode *inode, struct file *filp)
{
struct cdata_t *cdata;
printk(KERN_ALERT "cdata in open: filp = %p\n", filp);
cdata = kzalloc(sizeof(*cdata), GFP_KERNEL);
cdata->idx = 0;
#ifdef __USE_FBMEM__
cdata->iomem = ioremap(0xe0000000, FRAMEBUFFER_SIZE);
#endif
init_waitqueue_head(&cdata->writeable);
init_timer(&cdata->timer);
INIT_WORK(&cdata->work, write_framebuffer_with_work);
mutex_init(&cdata->write_lock);
spin_lock_init(&cdata->lock);
filp->private_data = (void *)cdata;
return 0;
}
static int cdata_close(struct inode *inode, struct file *filp)
{
struct cdata_t *cdata = (struct cdata_t *)filp->private_data;
int idx;
int i;
idx = cdata->idx;
for (i = 0; i < idx; i++) {
printk(KERN_ALERT "buf[%d]: %c\n", i, cdata->buf[i]);
}
del_timer(&cdata->timer);
kfree(cdata);
return 0;
}
static ssize_t cdata_read(struct file *filp, const char __user *user,
size_t size, loff_t *off)
{
printk(KERN_ALERT "cdata in read\n");
return 0;
}
void write_framebuffer_with_work(struct work_struct *work)
{
struct cdata_t *cdata = container_of(work, struct cdata_t, work);
cdata->idx = 0;
wake_up_interruptible(&cdata->writeable);
printk(KERN_INFO "cdata: wake up process");
}
void *write_framebuffer_with_timer(unsigned long arg)
{
struct cdata_t *cdata = (struct cdata_t *)arg;
int i;
printk(KERN_INFO "cdata: wake up process");
#ifdef __USE_FBMEM__
unsigned char *iomem;
iomem = cdata->iomem;
for (i = 0; i < BUF_SIZE - 1; i++) {
if (framebuffer_off >= FRAMEBUFFER_SIZE)
framebuffer_off = 0;
writeb(cdata->buf[i], iomem + framebuffer_off);
framebuffer_off++;
}
#endif
cdata->idx = 0;
wake_up_interruptible(&cdata->writeable);
}
static ssize_t cdata_write(struct file *filp, const char __user *user,
size_t size, loff_t *off)
{
struct cdata_t *cdata = (struct cdata_t *)filp->private_data;
DECLARE_WAITQUEUE(wait, current);
struct timer_list timer;
int i;
int idx;
mutex_lock_interruptible(&cdata->write_lock);
init_timer(&timer);
idx = cdata->idx;
for (i = 0; i < size; i++) {
if (idx > (BUF_SIZE - 1)) {
repeat:
//add_wait_queue(&cdata->writeable, &wait);
//current->state = TASK_INTERRUPTIBLE;
prepare_to_wait(&cdata->writeable, &wait, TASK_INTERRUPTIBLE);
//schedule_work(&cdata->work);
//timer->function = write_framebuffer_with_timer;
//timer->data = (unsigned long)cdata;
//timer->expires = jiffies + 10*HZ;
//mod_timer(timer, jiffies + 10*HZ);
timer.function = write_framebuffer_with_timer;
timer.data = (unsigned long)cdata;
timer.expires = jiffies + 10*HZ;
add_timer(&timer);
mutex_unlock(&cdata->write_lock);
schedule();
remove_wait_queue(&cdata->writeable, &wait);
idx = cdata->idx;
if (idx <= (BUF_SIZE - 1))
break;
printk(KERN_ALERT "race condition: idx = %d\n", idx);
mutex_lock_interruptible(&cdata->write_lock);
goto repeat;
}
copy_from_user(&cdata->buf[idx], &user[i], 1);
idx++;
}
cdata->idx = idx;
mutex_unlock(&cdata->write_lock);
return 0;
}
static long cdata_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct cdata_t *cdata = (struct cdata_t *)filp->private_data;
char *buf;
int idx;
int i;
int ret = 0;
char *user;
int size;
if (mutex_lock_interruptible(&ioctl_lock))
return -EINTR;
user = (char *)arg;
size = sizeof(*user);
idx = cdata->idx;
buf = cdata->buf;
switch (cmd) {
case IOCTL_EMPTY:
idx = 0;
break;
case IOCTL_SYNC:
printk(KERN_ALERT "in ioctl: %s\n", buf);
break;
case IOCTL_NAME:
for (i = 0; i < size; i++) {
if (idx > (BUF_SIZE - 1)) {
ret = -EFAULT;
goto exit;
}
copy_from_user(&buf[idx], &user[i], 1);
idx++;
}
break;
default:
goto exit;
}
exit:
cdata->idx = idx;
mutex_unlock(&ioctl_lock);
return ret;
}
static int cdata_mmap(struct file *filp, struct vm_area_struct *vma)
{
unsigned long start = vma->vm_start;
unsigned long end = vma->vm_end;
unsigned long size = end - start;
printk(KERN_ALERT "remap %p to 0xe0000000\n", start);
remap_pfn_range(vma, start, 0xe0000000, size, PAGE_SHARED);
return 0;
}
static struct file_operations cdata_fops = {
owner: THIS_MODULE,
open: cdata_open,
read: cdata_read,
write: cdata_write,
mmap: cdata_mmap,
unlocked_ioctl: cdata_ioctl,
release: cdata_close
};
static struct miscdevice cdata_miscdev = {
.minor = 77,
.name = "cdata-misc",
.fops = &cdata_fops,
};
static int cdata_plat_probe(struct platform_device *pdev)
{
int ret = 0;
ret = misc_register(&cdata_miscdev);
if (ret < 0) {
printk(KERN_ALERT "misc_register failed\n");
goto exit;
}
printk(KERN_ALERT "cdata module: registered!\n");
exit:
return ret;
}
static int cdata_plat_remove(struct platform_device *pdev)
{
misc_deregister(&cdata_miscdev);
printk(KERN_ALERT "cdata module: unregisterd.\n");
}
static struct platform_driver cdata_plat_driver = {
.probe = cdata_plat_probe,
.remove = cdata_plat_remove,
.driver = {
.name = "cdata",
.owner = THIS_MODULE,
},
};
int cdata_init_module(void)
{
int ret = 0;
#ifdef __USE_FBMEM__
framebuffer_off = 0;
#endif
debugfs = debugfs_create_file("cdata", S_IRUGO, NULL, NULL, &cdata_fops);
if (IS_ERR(debugfs)) {
ret = PTR_ERR(debugfs);
printk(KERN_ALERT "debugfs_create_file failed\n");
goto exit;
}
printk(KERN_ALERT "cdata: debugfs created\n");
mutex_init(&ioctl_lock);
ret = platform_driver_register(&cdata_plat_driver);
exit:
return ret;
}
void cdata_cleanup_module(void)
{
platform_driver_unregister(&cdata_plat_driver);
debugfs_remove(debugfs);
}
module_init(cdata_init_module);
module_exit(cdata_cleanup_module);
MODULE_LICENSE("GPL");