-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypad16_a710.c
158 lines (126 loc) · 3.65 KB
/
keypad16_a710.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
/*
* keypad16.c: Creates a char device driver to manage 4x4 keyboards
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/tty.h> /* For the tty declarations */
#include <asm/uaccess.h> /* for put_user */
#include <asm/io.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Malta de ARCOM");
MODULE_DESCRIPTION("Device driver for 4x4 keyboards"); /* What does this module do */
MODULE_VERSION("a07.1.0");
/*
* Prototypes - this would normally go in a .h file
*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
#define SUCCESS 0
#define DEVICE_NAME "keypad16" /* Dev name as it appears in /proc/devices */
#define DEVICE_MAJOR 216
#define BUF_SIZE 80 /* Max length of char buffer */
#define BASEPORT 0x378
/*
* Global variables are declared as static, so are global within the file.
*/
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?
Used to prevent multiple access to device */
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
/*
* This function is called when the module is loaded
*/
int __init init_module(void)
{
printk(KERN_INFO "Loading module keypad16\n");
Major = register_chrdev(DEVICE_MAJOR, DEVICE_NAME, &fops);
if (Major < 0) {
printk(KERN_ALERT " Registering char device failed with %d\n", Major);
return Major;
}
if (DEVICE_MAJOR > 0) Major = DEVICE_MAJOR;
printk(KERN_INFO " I was assigned major number %d.\n", Major);
return SUCCESS;
}
/*
* This function is called when the module is unloaded
*/
void __exit cleanup_module(void)
{
/*
* Unregister the device
*/
unregister_chrdev(Major, DEVICE_NAME);
printk(KERN_INFO "Module keypad16 unloaded.\n");
}
/*
* Device File Methods
*/
/*
* 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++;
try_module_get(THIS_MODULE);
return SUCCESS;
}
/*
* 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, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset)
{
if (length==0) return 0;
/*
* Actually put the data into the buffer
*/
/*
* The buffer is in the user data segment, not the kernel
* segment so "*" assignment won't work. We have to use
* put_user which copies data from the kernel data segment to
* the user data segment.
*/
put_user('A', buffer);
/*
* Most read functions return the number of bytes put into the buffer
*/
return 1;
}
/*
* Called when a process writes to dev file.
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
printk(KERN_ALERT "Sorry, write operation isn't supported.\n");
return -EINVAL;
}