-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.c
143 lines (131 loc) · 3.54 KB
/
write.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
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/sysfs.h>
#include <linux/mutex.h>
struct write_device {
struct mutex lock;
size_t size;
struct cdev cdev;
struct device base;
};
static struct write_driver {
dev_t devt;
struct file_operations fops;
struct device_driver base;
struct write_device devs[1000]; /* 1000 devices!? */
} write_driver = {
.base.name = "write",
.base.owner = THIS_MODULE,
};
static ssize_t write(struct file *fp, const char __user *buf, size_t count, loff_t *pos)
{
struct write_device *dev = fp->private_data;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
if (dev->size < *pos+count)
dev->size = *pos+count;
mutex_unlock(&dev->lock);
*pos += count;
return count;
}
static int open(struct inode *ip, struct file *fp)
{
struct write_device *dev = container_of(ip->i_cdev, struct write_device, cdev);
fp->private_data = dev;
if ((fp->f_flags&O_ACCMODE) == O_RDONLY)
return -EINVAL;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
if (fp->f_flags&O_TRUNC)
dev->size = 0;
mutex_unlock(&dev->lock);
return 0;
}
static ssize_t size_show(struct device *base, struct device_attribute *attr,
char *page)
{
struct write_device *dev = container_of(base, struct write_device, base);
size_t size;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
size = dev->size;
mutex_unlock(&dev->lock);
return snprintf(page, PAGE_SIZE, "%ld\n", size);
}
static DEVICE_ATTR_RO(size);
static void __init init_driver(struct write_driver *drv)
{
memset(&drv->fops, 0, sizeof(struct file_operations));
drv->fops.owner = drv->base.owner;
drv->fops.write = write;
drv->fops.open = open;
}
static int __init init(void)
{
struct write_driver *drv = &write_driver;
struct write_device *end = drv->devs+ARRAY_SIZE(drv->devs);
struct write_device *dev;
char name[9]; /* for 1000 devices */
int i, err;
err = alloc_chrdev_region(&drv->devt, 0, ARRAY_SIZE(drv->devs),
drv->base.name);
if (err)
return err;
init_driver(drv);
for (dev = drv->devs, i = 0; dev < end; dev++, i++) {
err = snprintf(name, sizeof(name), "%s%d", drv->base.name, i);
if (err < 0) {
end = dev;
goto err;
}
memset(dev, 0, sizeof(struct write_device));
cdev_init(&dev->cdev, &drv->fops);
device_initialize(&dev->base);
mutex_init(&dev->lock);
dev->cdev.owner = THIS_MODULE;
dev->base.init_name = name;
dev->base.devt = MKDEV(MAJOR(drv->devt),
MINOR(drv->devt)+i);
err = cdev_device_add(&dev->cdev, &dev->base);
if (err) {
end = dev;
goto err;
}
err = device_create_file(&dev->base, &dev_attr_size);
if (err) {
end = dev+1;
goto err;
}
}
return 0;
err:
for (dev = drv->devs; dev < end; dev++) {
device_remove_file(&dev->base, &dev_attr_size);
cdev_device_del(&dev->cdev, &dev->base);
}
unregister_chrdev_region(drv->devt, ARRAY_SIZE(drv->devs));
return err;
}
module_init(init);
static void __exit term(void)
{
struct write_driver *drv = &write_driver;
struct write_device *end = drv->devs+ARRAY_SIZE(drv->devs);
struct write_device *dev;
for (dev = drv->devs; dev < end; dev++) {
device_remove_file(&dev->base, &dev_attr_size);
cdev_device_del(&dev->cdev, &dev->base);
}
unregister_chrdev_region(drv->devt, ARRAY_SIZE(drv->devs));
}
module_exit(term);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kei Nohguchi <kei@nohguchi.com>");
MODULE_DESCRIPTION("write(2) example");