-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypad16_ups.c
491 lines (388 loc) · 12.9 KB
/
keypad16_ups.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#define SCAN_DELAY HZ/20
static unsigned int key_status = 0;
static struct timer_list my_timer;
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);
}
no init
/*
* 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);
em device_read:
char *st_ptr = (char *)&key_status;
if (length<2) return 0;
put_user(st_ptr[0], buffer++);
put_user(st_ptr[1], buffer++);
em cleanup_module:
del_timer(&my_timer);
/// do 1 para o 2
#define MAX_TABLE_KEYS 32
typedef struct
{
unsigned int scan_code;
char char_code;
} keypad_table_element;
static keypad_table_element keypad_table[MAX_TABLE_KEYS];
static keypad_table_element default_num1_table[] =
{
{ 0x0001, 'd' },
{ 0x0002, 'c' },
{ 0x0004, 'a' },
{ 0x0008, 'b' },
{ 0x0010, '\n' },
{ 0x0020, '9' },
{ 0x0040, '3' },
{ 0x0080, '6' },
{ 0x0100, '0' },
{ 0x0200, '8' },
{ 0x0400, '2' },
{ 0x0800, '5' },
{ 0x1000, '*' },
{ 0x2000, '7' },
{ 0x4000, '1' },
{ 0x8000, '4' },
};
static keypad_table_element default_alpha2_table[] =
{
{ 0x0001, 'd' },
{ 0x0002, 'c' },
{ 0x0004, 'a' },
{ 0x0008, 'b' },
{ 0x0010, '\n' },
{ 0x0020, '9' },
{ 0x0040, '3' },
{ 0x0080, '6' },
{ 0x0100, '0' },
{ 0x0200, '8' },
{ 0x0400, '2' },
{ 0x0800, '5' },
{ 0x1000, '*' },
{ 0x2000, '7' },
{ 0x4000, '1' },
{ 0x8000, '4' },
};
static int keypad_table_elements = 0;
/*
* Translates key_status into char, using keypad_table
*/
static char keypad_key(unsigned int key_status)
{
int i;
for (i=0 ; i<keypad_table_elements ; i++ )
{
if ( keypad_table[i].key_status==keypad_table[i].scan_c )
return char_c;
}
return (char)0;
}
em keypad_proc
newkey = keypad_key(key_status);
no init:
memcpy(keypad_table, default_num1_table, sizeof(default_num1_table) );
keypad_table_elements = sizeof(default_num1_table) / sizeof(keypad_table_element);
/////////////////////////////////////////////////////////////////////////////////////
/// do 2 para 3
static int KeypadModel = NUM1; /* Keyboard model identifier */
static char *model = "num1 "; /* Model name parameter */
module_param(model, charp, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(model, "Keyboard model name");
no init:
if (strlen(model)>0) {
switch (model[0]) {
case 'A': // model: alpha2
case 'a':
KeypadModel = ALPHA2;
memcpy(keypad_table, default_alpha2_table, sizeof(default_alpha2_table) );
keypad_table_elements = sizeof(default_alpha2_table) / sizeof(keypad_table_element);
printk(KERN_INFO " Model switched to 'Alpha2'\n");
break;
case 'N': // model: num1
case 'n':
default:
KeypadModel = NUM1;
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////
do 3 para 4
para autorepeat:
static long AutoRepeatFirstDelay = HZ;
static long AutoRepeatDelay = HZ/10;
static char keypad_char(void)
{
static unsigned int last_key_status = 0;
static long last_key_jiffies;
char keypad_newchar = '\0';
if (key_status != 0)
{
if ( key_status != last_key_status ) // new key
{
last_key_jiffies = jiffies + AutoRepeatFirstDelay;
keypad_newchar = keypad_key(key_status);
}
else
if ( jiffies>=last_key_jiffies )
{
last_key_jiffies = jiffies + AutoRepeatDelay;
keypad_newchar = keypad_key(key_status);
}
}
last_key_status = key_status;
return keypad_newchar;
}
em keypad_proc
newkey = keypad_char();
/// Do 4 +para 5
#include <linux/proc_fs.h> /* Necessary because we use the proc fs */
#define PROCFS_MAX_SIZE 1024
#define PROCFS_DIR "keypad16"
#define PROCFS_TABLE "table"
#define PROCFS_BUFFER "buffer"
#define PROCFS_REPEAT "rep_time"
#define PROCFS_FIRST "rep_first"
/*
* This structures hold information about the /proc files
*/
static struct proc_dir_entry *Our_Proc_Dir;
static struct proc_dir_entry *Our_Proc_Table;
static struct proc_dir_entry *Our_Proc_Buffer;
static struct proc_dir_entry *Our_Proc_Repeat;
static struct proc_dir_entry *Our_Proc_RepFirst;
/*
* The buffer used to store character for this module
*/
static char procfs_buffer[PROCFS_MAX_SIZE];
/*
* The size of the buffer
*/
static unsigned long procfs_buffer_size = 0;
/*
* This function is called then the /proc file is read
*/
int
procfile_read_table(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int i;
char *buf;
buf = buffer;
for (i=0 ; i<keypad_table_elements && i*10<buffer_length ; i++ )
{
char lineT[20];
int li;
down_interruptible(&TableMtx);
sprintf(lineT, "%04X : %02X\n", keypad_table[i].scan_code, keypad_table[i].char_code );
up(&TableMtx);
for( li=0 ; li<strlen(lineT) ; li++ )
*(buf++) = lineT[li];
}
*buf = (char)0;
return i*10;
}
/**
* This function is called when the /proc file is written
*
*/
int procfile_write_table(struct file *file, const char *buffer, unsigned long count,
void *data)
{
char *tabp;
int tabi = 0;
/* get buffer size */
unsigned long procfs_buffer_size = count;
if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
procfs_buffer_size = PROCFS_MAX_SIZE;
}
/* write data to the buffer */
if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
return -EFAULT;
}
procfs_buffer[procfs_buffer_size] = (char)0;
printk(KERN_INFO "Leu %ld bytes: '%s'\n",procfs_buffer_size,buffer);
/* parse received data and generate a new table */
tabp = procfs_buffer;
while ( strlen(tabp)>6 && tabi<MAX_TABLE_KEYS ) {
down_interruptible(&TableMtx);
sscanf(tabp, "%4X : %2hhX\n", &(keypad_table[tabi].scan_code), &(keypad_table[tabi].char_code) );
up(&TableMtx);
tabi++;
tabp = strchr(tabp, 0x0a);
if ( tabp==NULL ) break;
tabp++;
}
keypad_table_elements = tabi;
return procfs_buffer_size;
}
/**
* This function is called then the /proc file is read
*
*/
int
procfile_read_buffer(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int ret;
printk(KERN_INFO "procfile_read_buffer (/proc/%s/%s) called\n", PROCFS_DIR, PROCFS_TABLE);
if (offset > 0) {
/* we have finished to read, return 0 */
ret = 0;
} else {
/* fill the buffer, return the buffer size */
char *mptr, *buf;
mptr = msg;
buf = buffer;
while( buffer_length && mptr!=end_Ptr )
{
*buf = *mptr;
buf++;
INC_MSG_PTR(mptr);
}
ret = msg_len;
}
return ret;
}
/*
* This function is called when the /proc file is written
*/
int procfile_write_buffer(struct file *file, const char *buffer, unsigned long count,
void *data)
{
/* get buffer size */
unsigned long procfs_buffer_size = count;
if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
procfs_buffer_size = PROCFS_MAX_SIZE;
}
/* write data to the buffer */
if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
return -EFAULT;
}
printk(KERN_INFO "Leu %ld bytes: '%s'\n",procfs_buffer_size,buffer);
return procfs_buffer_size;
}
/*
* This function is called then the /proc file is read
*/
int
procfile_read_first(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int i;
char *buf;
char mymsg[20];
buf = buffer;
sprintf( mymsg, "%ld\n", AutoRepeatFirstDelay );
for( i=0 ; i<strlen(mymsg) && i<buffer_length ; i++ )
*(buf++) = mymsg[i];
return i;
}
/*
* This function is called then the /proc file is read
*/
int
procfile_read_repeat(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int i;
char *buf;
char mymsg[20];
buf = buffer;
sprintf( mymsg, "%ld\n", AutoRepeatDelay );
for( i=0 ; i<strlen(mymsg) && i<buffer_length ; i++ )
*(buf++) = mymsg[i];
return i;
}
/*
* This function is called when the /proc file is written
*/
int procfile_write_first(struct file *file, const char *buffer, unsigned long count,
void *data)
{
/* get buffer size */
unsigned long procfs_buffer_size = count;
if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
procfs_buffer_size = PROCFS_MAX_SIZE;
}
/* write data to the buffer */
if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
return -EFAULT;
}
printk(KERN_INFO "procfile_write_first (not implemented) got %ld bytes: '%s'\n",procfs_buffer_size,buffer);
return procfs_buffer_size;
}
/*
* This function is called when the /proc file is written
*/
int procfile_write_repeat(struct file *file, const char *buffer, unsigned long count,
void *data)
{
/* get buffer size */
unsigned long procfs_buffer_size = count;
if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
procfs_buffer_size = PROCFS_MAX_SIZE;
}
/* write data to the buffer */
if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
return -EFAULT;
}
printk(KERN_INFO "procfile_write_repeat (not implemented) got %ld bytes: '%s'\n",procfs_buffer_size,buffer);
return procfs_buffer_size;
}:
static int __init create_proc_file(struct proc_dir_entry **entry, const char* name,
read_proc_t *read_func, write_proc_t *write_func )
{
(*entry) = create_proc_entry(name, 0644, Our_Proc_Dir);
if ( (*entry) == NULL ) {
remove_proc_entry(PROCFS_TABLE, Our_Proc_Table);
printk(KERN_ALERT "Error: Could not initialize /proc/%s/%s\n",
PROCFS_DIR, PROCFS_TABLE);
return -ENOMEM;
}
(*entry)->read_proc = read_func;
(*entry)->write_proc = write_func;
(*entry)->owner = THIS_MODULE;
(*entry)->mode = S_IFREG | S_IRUGO;
(*entry)->uid = 0;
(*entry)->gid = 0;
(*entry)->size = 180;
printk(KERN_INFO " /proc/%s/%s created\n", PROCFS_DIR, name);
return 0;
}
no init_module()
...
na função cleanup_module()
remove_proc_entry(PROCFS_TABLE, Our_Proc_Dir);
remove_proc_entry(PROCFS_BUFFER, Our_Proc_Dir);
remove_proc_entry(PROCFS_FIRST, Our_Proc_Dir);
remove_proc_entry(PROCFS_REPEAT, Our_Proc_Dir);
remove_proc_entry(PROCFS_DIR, &proc_root);