forked from moko365/ProgramAssignment-LinuxDeviceDriver
-
Notifications
You must be signed in to change notification settings - Fork 15
/
cdata-2.6.c
277 lines (219 loc) · 5.42 KB
/
cdata-2.6.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
#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/mutex.h>
#include <linux/spinlock.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include "cdata_ioctl.h"
#define BUF_SIZE (128)
#define LCD_SIZE (320*240*4)
struct cdata_t {
unsigned long *fb;
unsigned char *buf;
unsigned int index;
unsigned int offset;
struct timer_list flush_timer;
struct timer_list sched_timer;
wait_queue_head_t wq;
DEFINE_MUTEX(mutex);
spinlock_t lock;
};
static int cdata_open(struct inode *inode, struct file *filp)
{
int minor;
struct cdata_t *cdata;
printk(KERN_INFO "CDATA: in open\n");
minor = MINOR(inode->i_rdev);
printk(KERN_INFO "CDATA: minor = %d\n", minor);
cdata= kmalloc(sizeof(struct cdata_t), GFP_KERNEL);
cdata->buf = kmalloc(BUF_SIZE, GFP_KERNEL);
cdata->fb = ioremap(0x33f00000, 320*240*4);
cdata->index = 0;
cdata->offset = 0;
init_timer(&cdata->flush_timer);
init_timer(&cdata->sched_timer);
init_waitqueue_head(&cdata->wq);
mutex_init(&cdata->mutex);
spin_lock_init(&cdata->lock);
filp->private_data = (void *)cdata;
return 0;
}
static ssize_t cdata_read(struct file *filp, char *buf, size_t size, loff_t *off)
{
return 0;
}
void flush_lcd(unsigned long priv)
{
struct cdata_t *cdata = (struct cdata *)priv;
unsigned char *fb;
unsigned char *pixel;
int index;
int offset;
int i;
int j;
spin_lock_irqsave(&cdata->lock);
fb = (unsigned char *)cdata->fb;
pixel = cdata->buf;
index = cdata->index;
offset = cdata->offset;
spin_unlock_irqsave(&cdata->lock);
for (i = 0; i < index; i++) {
writeb(pixel[i], fb+offset);
offset++;
if (offset >= LCD_SIZE)
offset = 0;
// Lab
for (j = 0; j < 100000; j++);
}
cdata->index = 0;
cdata->offset = offset;
}
void cdata_wake_up(unsigned long priv)
{
struct cdata_t *cdata = (struct cdata *)priv;
struct timer_list *sched;
wait_queue_head_t *wq;
sched = &cdata->sched_timer;
wq = &cdata->wq;
wake_up(wq);
sched->expires = jiffies + 10;
add_timer(sched);
}
static ssize_t cdata_write(struct file *filp, const char *buf, size_t size,
loff_t *off)
{
struct cdata_t *cdata = (struct cdata *)filp->private_data;
struct timer_list *timer;
struct timer_list *sched;
unsigned char *pixel;
unsigned int index;
unsigned int i;
wait_queue_head_t *wq;
wait_queue_t wait;
mutex_lock(&cdata->mutex);
spin_lock_irqsave(&cdata->lock);
pixel = cdata->buf;
index = cdata->index;
spin_unlock_irqsave(&cdata->lock);
timer = &cdata->flush_timer;
sched = &cdata->sched_timer;
wq = &cdata->wq;
mutex_unlock(&cdata->mutex);
for (i = 0; i < size; i++) {
if (index >= BUF_SIZE) {
down_interruptible(&cdata->sem);
cdata->index = index;
up(&cdata->sem);
timer->expires = jiffies + 5*HZ;
timer->function = flush_lcd;
timer->data = (unsigned long)cdata;
add_timer(timer);
sched->expires = jiffies + 10;
sched->function = cdata_wake_up;
sched->data = (unsigned long)cdata;
add_timer(sched);
wait.flags = 0;
wait.task = current;
add_wait_queue(wq, &wait);
repeat:
current->state = TASK_INTERRUPTIBLE;
schedule();
down_interruptible(&cdata->sem);
index = cdata->index;
up(&cdata->sem);
if (index != 0)
goto repeat;
remove_wait_queue(wq, &wait);
del_timer(sched);
}
copy_from_user(&pixel[index], &buf[i], 1);
index++;
}
down_interruptible(&cdata->sem);
cdata->index = index;
up(&cdata->sem);
return 0;
}
static int cdata_close(struct inode *inode, struct file *filp)
{
struct cdata_t *cdata = (struct cdata *)filp->private_data;
flush_lcd((unsigned long)cdata);
del_timer(&cdata->flush_timer);
kfree(cdata->buf);
kfree(cdata);
return 0;
}
static int cdata_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
struct cdata_t *cdata = (struct cdata *)filp->private_data;
int n;
unsigned long *fb;
int i;
switch (cmd) {
case CDATA_CLEAR:
printk(KERN_INFO "CDATA_CLEAR: %d pixel\n", n);
return 0;
}
return -ENOTTY;
}
int cdata_mmap(struct file *filp, struct vm_area_struct *vma)
{
unsigned long from;
unsigned long to;
unsigned long size;
from = vma->vm_start;
to = 0x33f00000;
size = vma->vm_end-vma->vm_start;
while (size) {
remap_page_range(from, to, PAGE_SIZE, PAGE_SHARED);
from += PAGE_SIZE;
to += PAGE_SIZE;
size -= PAGE_SIZE;
}
printk(KERN_ALERT "vma start: %p\n", vma->vm_start);
printk(KERN_ALERT "vma end: %p\n", vma->vm_end);
return 0;
}
static struct file_operations cdata_fops = {
owner: THIS_MODULE,
open: cdata_open,
release: cdata_close,
read: cdata_read,
write: cdata_write,
ioctl: cdata_ioctl,
mmap: cdata_mmap,
};
int cdata_init_module(void)
{
unsigned long *fb;
int i;
fb = ioremap(0x33f00000, 320*240*4);
for (i = 0; i < 320*240; i++)
writel(0x00ff0000, fb++);
if (register_chrdev(121, "cdata", &cdata_fops) < 0) {
printk(KERN_INFO "CDATA: can't register driver\n");
return -1;
}
printk(KERN_INFO "CDATA: cdata_init_module\n");
return 0;
}
void cdata_cleanup_module(void)
{
unregister_chrdev(121, "cdata");
}
module_init(cdata_init_module);
module_exit(cdata_cleanup_module);
MODULE_LICENSE("GPL");