-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_chardev.c
More file actions
94 lines (73 loc) · 2.87 KB
/
mod_chardev.c
File metadata and controls
94 lines (73 loc) · 2.87 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
/*
* File: mod_readaddr.c
* Autor: Felix Arnold
* Aim: read data from address
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/io.h>
#include <linux/litex.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/cdev.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Felix Arnold <felix.arnold@gmail.com>");
MODULE_DESCRIPTION("Simple linux kernel module");
#define MY_MODULE_NAME "mod_chardev"
static int my_open(struct inode *inode, struct file *filp);
static int my_release(struct inode *inode, struct file *filp);
static ssize_t my_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
static ssize_t my_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos);
static struct file_operations my_fops = {
.owner = THIS_MODULE,
.read = my_read,
.write = my_write,
.open = my_open,
.release = my_release,
};
int major;
static char mydata[100];
typedef struct {
volatile unsigned int reg0;
volatile unsigned int reg1;
} mask_regs_t;
mask_regs_t *regs;
static int __init module_start(void)
{
printk(MY_MODULE_NAME ": Loading hello module...\n");
printk(MY_MODULE_NAME ": Hello universe!\n");
regs = (mask_regs_t *) ioremap_nocache(0xf0005000, 8);
printk(MY_MODULE_NAME ": read data from reg0 is %d\n", regs->reg0);
printk(MY_MODULE_NAME ": read data from reg1 is %d\n", regs->reg1);
printk(MY_MODULE_NAME ": read data from reg0_l is %d\n", litex_get_reg((void *) &(regs->reg0), 4));
major = register_chrdev(0, MY_MODULE_NAME, &my_fops);
if (major < 0) {
printk(MY_MODULE_NAME " error, cannot register the character device\n");
return major;
}
return 0;
}
static void __exit module_end(void) {
unregister_chrdev(major, "mod_hrtimer_dev");
printk(MY_MODULE_NAME ": see you around.\n");
}
static int my_open(struct inode *inode, struct file *filp) {
return 0; // SUCCESS zurueckmelden
}
static int my_release(struct inode *inode, struct file *filp) {
return 0; // SUCCESS zurückmelden
}
static ssize_t my_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) {
if (strnlen(mydata,sizeof(mydata))<count) // mehr Daten angefordert als in mydata[] vorhanden?
count = strnlen(mydata,sizeof(mydata)); // wenn ja, count entsprechend dezimieren
__copy_to_user (buf, mydata, count); // Daten aus Kernel- in Userspace kopieren
mydata[0]=0; // und lokale Daten "loeschen" womit naechstes read() EOF
return count; // Zurueckmelden wieviele Bytes effektiv geliefert werden
}
static ssize_t my_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos) {
if (sizeof(mydata)<count) // mydata[] Platz fuer gelieferte Daten?
count = sizeof(mydata); // wenn nicht entsprehchend begrenzen!
__copy_from_user(mydata, buf, count); // Daten aus User- in Kernel-space kopieren
return count; // Zurückmelden wieviele Bytes effektiv konsumiert wurden
}
module_init(module_start);
module_exit(module_end);