-
Notifications
You must be signed in to change notification settings - Fork 1
/
wom.c
127 lines (104 loc) · 3.73 KB
/
wom.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
/*
* wom.c
*
* an implementation of signetics' famous write-only-memory as a kernel module
*
* supported chips:
* * signetics 25120 - PDF attached to source
*
* * LAST CHANGE: 23. nov 2017, Nils Stec
*
* Authors: Nils Stec, <[email protected]>, (c) 2013, 2017
* some module parts are by LKMPG - taken from version "2007-05-18 ver 2.6.4"
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <asm/uaccess.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <asm/io.h>
#include "wom.h"
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open? */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static char *msg_Ptr;
static uint64_t discarded_bytes;
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
/* called when a process tries to open the device file */
static int device_open(struct inode *inode, struct file *file) {
if(Device_Open) return -EBUSY;
Device_Open++;
sprintf(msg, "write only memory does not support read operations. Already wrote %llubytes to nowhere.\n", discarded_bytes);
msg_Ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off) {
char message_from_user[BUF_LEN];
if(copy_from_user(message_from_user, buff, (len < BUF_LEN) ? len : BUF_LEN)) return -EINVAL;
message_from_user[4] = '\0';
printk(KERN_INFO "[wom] discarded %dbytes of data\n", (int)len);
discarded_bytes += len;
return len;
}
/* called when module loaded */
int init_module_wom(void) {
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0) {
printk(KERN_ALERT "[wom] registering char device failed with %d\n", Major);
return Major;
}
discarded_bytes = 0;
printk(KERN_INFO "[wom] driver loaded with major %d\n", Major);
printk(KERN_INFO "[wom] >> $ mknod /dev/%s c %d 0\n", DEVICE_NAME, Major);
return SUCCESS;
}
/* called when module unloaded */
void cleanup_module_wom(void) {
unregister_chrdev(Major, DEVICE_NAME);
printk(KERN_INFO "[wom] DRIVER UNLOADED\n");
}
/* Called when a process closes the device file. */
static int device_release(struct inode *inode, struct file *file) {
Device_Open--; /* We're now ready for our next caller */
/* Decrement the usage count, or else once you opened the file, you'll never get get rid of the module. */
module_put(THIS_MODULE);
return 0;
}
/* Called when a process, which already opened the dev file, attempts to read from it. */
static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset) {
int bytes_read = 0;
if(*msg_Ptr == 0) return 0;
while(length && *msg_Ptr) {
put_user(*(msg_Ptr++), buffer++);
length--;
bytes_read++;
}
return bytes_read;
}
module_init(init_module_wom);
module_exit(cleanup_module_wom);
MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_SUPPORTED_DEVICE("signetics wom-25120");