-
Notifications
You must be signed in to change notification settings - Fork 0
/
hako_kmod.c
154 lines (125 loc) · 3.52 KB
/
hako_kmod.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
/*
* A pseude devide to keep the string "hakodate"
*
* This program code was written for the IT Arch. class
* in Future University Hakodate.
*
* Copyright (C) 2017 Katsuya Matsubara.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#define CHARDEV_BUFFLEN (8)
static unsigned char chardev_buff[CHARDEV_BUFFLEN] = {
'h', 'a', 'k', 'o', 'd', 'a', 't', 'e'
};
static int chardev_open(struct inode *inode, struct file *filp)
{
int minor = iminor(inode);
printk(KERN_INFO "hako: device hako%d open.\n", minor);
filp->private_data = (void *)minor;
return 0;
}
static int chardev_release(struct inode *inode, struct file *filp)
{
int minor = iminor(inode);
printk(KERN_INFO "hako: device hako%d released.\n", minor);
return 0;
}
static ssize_t chardev_read(struct file *filp, char __user *buff,
size_t count, loff_t *offp)
{
unsigned long remain;
int minor = (int)filp->private_data;
printk(KERN_INFO "hako: read from device hako%d.\n", minor);
if (minor == 0) {
if (count < CHARDEV_BUFFLEN)
return -EINVAL;
remain = copy_to_user(buff, chardev_buff,
CHARDEV_BUFFLEN);
if (remain > 0)
printk(KERN_INFO "hako: %ld bytes remain "
"in kernel buffer.\n", remain);
return CHARDEV_BUFFLEN - remain;
} else if (minor <= CHARDEV_BUFFLEN) {
if (put_user(chardev_buff[minor - 1], buff)) {
printk(KERN_ERR "hako: put_user failed.\n");
return -EFAULT;
}
return 1;
}
printk(KERN_ERR "hako: invalid minor number %d.\n",
minor);
return -EINVAL;
}
static ssize_t chardev_write(struct file *filp, const char __user *buff,
size_t count, loff_t *offp)
{
unsigned long remain;
int minor = (int)filp->private_data;
printk(KERN_INFO "hako: write to device hako%d.\n", minor);
if (minor == 0) {
if (count < CHARDEV_BUFFLEN)
return -EINVAL;
remain = copy_from_user(chardev_buff, buff, CHARDEV_BUFFLEN);
if (remain > 0)
printk(KERN_INFO "hako: %ld bytes remain in user buffer.\n",
remain);
return CHARDEV_BUFFLEN - remain;
} else if (minor <= CHARDEV_BUFFLEN) {
if (get_user(chardev_buff[minor - 1], buff)) {
printk(KERN_ERR "hako: get_user failed.\n");
return -EFAULT;
}
return 1;
}
printk(KERN_ERR "hako: invalid minor number %d.\n",
minor);
return -EINVAL;
}
static struct file_operations chardev_fops = {
.owner = THIS_MODULE,
.open = chardev_open,
.read = chardev_read,
.write = chardev_write,
.release = chardev_release,
};
static struct cdev cdev;
static const dev_t dev = MKDEV(240, 0);
static int __init hello_init(void)
{
int err;
err = register_chrdev_region(dev, CHARDEV_BUFFLEN + 1, "hako");
if (err) {
printk(KERN_ERR "hako: register_chrdev_region failed.\n");
return -1;
}
cdev_init(&cdev, &chardev_fops);
cdev.owner = THIS_MODULE;
err = cdev_add(&cdev, dev, CHARDEV_BUFFLEN + 1);
if (err) {
printk(KERN_ERR "hako: cdev_add failed.\n");
return -1;
}
printk(KERN_INFO "hako: all %d devices are ready for I/O.\n",
CHARDEV_BUFFLEN + 1);
return err;
}
static void __exit hello_exit(void)
{
cdev_del(&cdev);
unregister_chrdev_region(dev, CHARDEV_BUFFLEN + 1);
printk(KERN_INFO "hako: all %d devices removed.\n",
CHARDEV_BUFFLEN + 1);
return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Katsuya Matsubara");