forked from moko365/ProgramAssignment-LinuxDeviceDriver
/
cdata-2.6.c
323 lines (269 loc) · 7.59 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
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
#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/semaphore.h>
#include <linux/spinlock.h>
#include <linux/mutex.h>
#include <linux/input.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include "cdata_ioctl.h"
#define DEV_MAJOR 121
#define DEV_NAME "cdata"
#define VERSION 6
#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;
//DECLARE_WAIT_QUEUE(wq);
wait_queue_head_t wq;
struct semaphore sem;
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 number: %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, 1);
spink_lock_init(&cdata->lock);
filp->private_data = (void *)cdata;
return 0;
}
static ssize_t cdata_read(struct file *filp, char *buff, size_t size, loff_t *off)
{
return 0;
}
void flush_lcd(unsigned long *priv)
{
struct cdata_t *cdata = (struct cdata_t *)priv;
unsigned char *fb;
unsigned char *pixel;
int index;
int offset;
int i;
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;
}
cdata->index = 0;
cdata->offset = offset;
}
void cdata_wake_up(unsigned long priv)
{
// Wake up process (Switch process to ready)
struct cdata_t *cdata = (struct cdata_t *)priv;
struct timer_list *sched;
wait_queue_head_t *wq;
sched = &cdata->sched_timer;
wq = &cdata->wq;
// because kernel timer expire belongs to I/O interrupt,
// You CAN NOT switch process state in I/O interrupt
// Also, No context switching in I/O interrupt as well
//current->state = TASK_RUNNING;
//schedule();
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_t *)filp->private_data;
unsigned char *pixel;
unsigned int i;
unsigned int index;
struct timer_list *timer;
struct timer_list *sched;
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);
//printk(KERN_INFO "CDATA: In cdata_write()\n");
for (i = 0; i < size; i++){
if (index >= BUF_SIZE){
down_interruptible(&cdata->sem);
cdata->index = index;
up(&cdata->sem);
// Kernel scheduling
timer->expires = jiffies + 5*HZ; // 1*HZ = 1 second
timer->function = flush_lcd;
timer->data = (unsigned long)cdata;
add_timer(timer);
// You can setup one timer(timer) without sched
// Timer expires, call flush_lcd()
// When flush_lcd() finish the write I/O, then wake up the process
// Then you do notn need to maintain 2nd timer for process state change
sched->expires = jiffies + 10; // 10 == 0.1 second
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:
// Process scheduling
current->state = TASK_INTERRUPTIBLE;
schedule();
// Every time, sched timer expires, it will read the index and check if index != 0
// Meaning flush_lcd has not finished yet. So, keep changing itself to ready state for next wake up.
down_interruptible(&cdata->sem);
index = cdata->index; // IMPORTANT: Use state machine concept to maintain. Do not use index = 0; not good!
up(&cdata->sem);
if (index != 0)
goto repeat;
remove_wait_queue(wq, &wait);
del_timer(sched);
}
//fb[index] = buf[i]; // wrong!! Can NOT access user space data directly
copy_from_user(&pixel[index], &buf[i], 1);
index++;
}
down_interruptible(&cdata->sem);
cdata->index = index;
up(&cdata->sem);
//while(1) {
//current->state=TASK_UNINTERRUPTIBLE;
// current->state=TASK_INTERRUPTIBLE;
// schedule();
//}
return 0;
}
static int cdata_close(struct inode *inode, struct file *filp)
{
struct cdata_t* cdata = (struct cdata_t *)filp->private_data;
printk(KERN_INFO "CDATA: cdata_close() is invoked.\n");
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)
{
int i,n ;
unsigned long *fb;
struct cdata_t *cdata = (struct cdata_t *)filp->private_data;
switch (cmd) {
case CDATA_CLEAR:
//n = *((int *) arg); // dirty
copy_from_user(&n, (int*)arg, 4);
//get_user(n,(int*)arg);
printk(KERN_INFO "Action: CDATA_CLEAR: %d pixel.\n", n);
//fb = ioremap(0x33f00000, n*4); // FIXME: dirty
// fork & multithread needs to file locking on fb
// FIXME: Lock
fb = cdata->fb;
// FIXME: unlock
for( i = 0; i < n; i++)
writel(0x00ff00ff, fb++);
return 0;
case CDATA_RED:
break;
case CDATA_GREEN:
break;
case CDATA_BLUE:
break;
case CDATA_BLACK:
break;
case CDATA_WHITE:
break;
}
return -ENOTTY;
}
static int cdata_flush(struct file *filp)
{
printk(KERN_INFO "CDATA: cdata_flush() called back!!\n");
return 0;
}
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_INFO "CDATA: in cdata_mmap(). \n");
printk(KERN_INFO "CDATA: start = %08x\n", vma->vm_start);
printk(KERN_INFO "CDATA: end = %08x\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,
flush: cdata_flush,
mmap: cdata_mmap,
};
static 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++);
printk(KERN_INFO "CDATA: Exercise [ %d ]\n", VERSION);
if(register_chrdev(DEV_MAJOR, DEV_NAME, &cdata_fops) < 0){
printk(KERN_INFO "CDATA: Couldn't register a device.\n");
return -1;
}
printk(KERN_INFO "CDATA: In cdata_init_module.\n");
return 0;
}
static void cdata_cleanup_module(void)
{
unregister_chrdev(DEV_MAJOR, DEV_NAME);
}
module_init(cdata_init_module);
module_exit(cdata_cleanup_module);
MODULE_LICENSE("GPL");