-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypad16_a711.c
207 lines (164 loc) · 4.66 KB
/
keypad16_a711.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* 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.1");
/*
* 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
#define SCAN_DELAY HZ/20
/*
* 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
};
static struct timer_list my_timer;
static unsigned int key_status = 0;
static void keypad_scan(void)
{
unsigned int local_kstatus = 0;
unsigned char row_sel = 0x10;
int row;
unsigned char row_data;
for( row=0 ; row<4 ; row++ )
{
outb( 0x0F | ~row_sel, BASEPORT);
row_data = inb(BASEPORT+1) ^ 0x80;
local_kstatus = local_kstatus << 4;
local_kstatus |= (row_data & 0xF0) >> 4;
row_sel = row_sel << 1;
}
key_status = 0x0000ffff & ~local_kstatus;
}
static void keypad_proc(void)
{
keypad_scan();
}
static void my_timer_func(unsigned long ptr)
{
keypad_proc();
my_timer.expires = jiffies + SCAN_DELAY;
add_timer(&my_timer);
}
/*
* 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);
/*
* Set up the keypad scanner timer the first time
*/
init_timer(&my_timer);
my_timer.function = my_timer_func;
my_timer.expires = jiffies + SCAN_DELAY;
add_timer(&my_timer);
return SUCCESS;
}
/*
* This function is called when the module is unloaded
*/
void __exit cleanup_module(void)
{
/*
* Unregister the device
*/
unregister_chrdev(Major, DEVICE_NAME);
del_timer(&my_timer);
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) {
printk(KERN_ALERT "Device keypad16 is already open\n");
return -EBUSY;
}
Device_Open++;
try_module_get(THIS_MODULE);
printk(KERN_INFO "Device keypad16 open\n");
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);
printk(KERN_INFO "Device keypad16 closed\n");
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)
{
char *st_ptr = (char *)&key_status;
if (length<2) return 0;
/*
* Actually put the data into the buffer
*/
put_user(st_ptr[0], buffer++);
put_user(st_ptr[1], buffer++);
put_user('\n', buffer++);
/*
* Most read functions return the number of bytes put into the buffer
*/
return 3;
}
/*
* 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;
}