-
Notifications
You must be signed in to change notification settings - Fork 0
/
parportsnif.c
561 lines (503 loc) · 14.5 KB
/
parportsnif.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* Parallel port sniffer kernel module
*
* Copyright (c) 2012, Fernando Gabriel Vicente (www.alfersoft.com.ar - [email protected])
* All rights reserved.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* Thanks to Rodrigo Zechin Rosauro!!!
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#include <linux/smp_lock.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/proc_fs.h> /* Necessary because we use proc fs */
#include <linux/seq_file.h> /* for seq_file */
#include <linux/file.h>
#include <asm/uaccess.h> /* for get_user and put_user */
#define SUCCESS 0
#define BUF_LEN 80
#define CHRDEV "parportsnif"
#define VPP_MAJOR 367
#define MAX_DEVICES 10
#define MAX_LINES 10000
#define PORT_NUM 0
#define SAMPLE_RATE 1000000
#define SAMPLE_RATE_STR "1000000"
DECLARE_WAIT_QUEUE_HEAD(readwait);
struct vpp_st {
struct file *pfd;
unsigned int port;
int bufsz;
struct timespec start;
unsigned char chanvalues[3];
unsigned char lastvalues[3];
};
struct vlogline_st {
struct vlogline_st *next;
char *data;
int size;
int offset;
};
struct vlog_st {
struct vlogline_st *lines;
int totlines;
} vlog;
static struct class *vpp_class;
static ssize_t log_write(const char *buffer, size_t length)
{
struct vlogline_st *line, *newline;
lock_kernel();
line = vlog.lines;
while(line && line->next)
line = line->next;
newline = (struct vlogline_st *)kzalloc(sizeof(struct vlogline_st), GFP_KERNEL);
if (!newline) {
unlock_kernel();
return -ENOMEM;
}
newline->data = (char *)kzalloc(length+1, GFP_KERNEL);
if (!newline->data) {
unlock_kernel();
kfree(newline);
return -ENOMEM;
}
newline->size = length;
memcpy(newline->data, buffer, length);
if(line)
line->next = newline;
else
vlog.lines = newline;
vlog.totlines++;
while (vlog.totlines > MAX_LINES && vlog.lines) {
line = vlog.lines;
vlog.lines = line->next;
kfree(line->data);
kfree(line);
vlog.totlines--;
}
unlock_kernel();
wake_up_interruptible(&readwait);
return length;
}
static void vpp_log_ols(struct vpp_st *vpp)
{
char buf[900];
int sz;
struct timespec ts;
long long udelta;
uint32_t samplenum;
if (memcmp(vpp->lastvalues, vpp->chanvalues, sizeof(vpp->lastvalues)) == 0) {
// values did not change - nothing to do
return;
}
memset(&ts, 0, sizeof(ts));
getnstimeofday(&ts);
// microseconds since "start"
udelta = ((ts.tv_sec - vpp->start.tv_sec) * (long long)1000000);
udelta += ((ts.tv_nsec - vpp->start.tv_nsec) / 1000);
// number of the sample
// /* todo - find a better way */ samplenum = do_div(udelta * 1000000, SAMPLE_RATE);
samplenum = udelta;
//0000000f@0
sz = snprintf(buf, sizeof(buf), "0000%.2X%.2X%.2X@%u\n", vpp->chanvalues[2], vpp->chanvalues[1], vpp->chanvalues[0], samplenum);
log_write(buf, sz);
memcpy(vpp->lastvalues, vpp->chanvalues, sizeof(vpp->lastvalues));
}
static void vpp_log(struct vpp_st *vpp, const char *fmt, ...)
{
char buf[900];
va_list args;
int sz;
struct timespec ts;
memset(&ts, 0, sizeof(ts));
getnstimeofday(&ts);
// current time
sz = snprintf(buf, sizeof(buf), "# [%u.%u] ", (unsigned int)ts.tv_sec, (unsigned int)ts.tv_nsec);
// log message
va_start(args, fmt);
sz += vsnprintf(buf+sz, sizeof(buf)-sz, fmt, args);
va_end(args);
buf[sz++] = '\n';
buf[sz] = '\0';
log_write(buf, sz);
}
/*
* This is called whenever a process attempts to open the device file
*/
static int device_open(struct inode *inode, struct file *file)
{
struct vpp_st *vpp = NULL;
char port[64];
int err = 0;
vpp = kzalloc (sizeof(struct vpp_st), GFP_KERNEL);
if (!vpp) {
return -ENOMEM;
}
vpp->bufsz = 512;
vpp->port = iminor(inode);
snprintf(port, sizeof(port)-1, "/dev/parport%d", vpp->port);
/* open the real parallel port */
vpp->pfd = filp_open(port, O_RDWR, 0);
if (IS_ERR(vpp->pfd)) {
err = PTR_ERR(vpp->pfd);
printk(KERN_ALERT "parportsnif: failed to open %s", port);
return err;
}
file->private_data = vpp;
/* write the OLS log header */
log_write(";Rate: "SAMPLE_RATE_STR"\n", strlen(";Rate: "SAMPLE_RATE_STR"\n"));
log_write(";Channels: 32\n", strlen(";Channels: 32\n"));
/* get the starting timestamp */
memset(&vpp->start, 0, sizeof(vpp->start));
getnstimeofday(&vpp->start);
/* log the initial reading */
vpp_log_ols(vpp);
try_module_get(THIS_MODULE);
printk(KERN_INFO "parportsnif: successfully opened %s\n", port);
//vpp_log(vpp, "%d %s", vpp->port, "OPEN");
return SUCCESS;
}
static int device_release(struct inode *inode, struct file *file)
{
struct vpp_st *vpp = (struct vpp_st *) file->private_data;
if (vpp->pfd) {
filp_close(vpp->pfd, NULL);
}
vpp->pfd = NULL;
module_put(THIS_MODULE);
printk(KERN_INFO "parportsnif: successfully closed /dev/parport%d\n", vpp->port);
vpp_log(vpp, "%d %s", vpp->port, "CLOSE");
kfree(vpp);
return SUCCESS;
}
loff_t device_llseek(struct file *file, loff_t offset, int p)
{
struct vpp_st *vpp = (struct vpp_st *) file->private_data;
vpp_log(vpp, "%d %s", vpp->port, "SEEK");
return(vpp->pfd->f_op->llseek(vpp->pfd, offset, p));
}
unsigned int device_poll(struct file *file, struct poll_table_struct *ps)
{
struct vpp_st *vpp = (struct vpp_st *) file->private_data;
vpp_log(vpp, "%d %s", vpp->port, "POLL");
return(vpp->pfd->f_op->poll(vpp->pfd, ps));
}
/*
* This function is called whenever a process which has already opened the
* device file attempts to read from it.
*/
static ssize_t device_read(struct file *file, char __user * buffer, size_t length, loff_t * offset)
{
struct vpp_st *vpp = (struct vpp_st *) file->private_data;
int i;
char *buf;
unsigned char ch;
ssize_t bytes_read;
printk(KERN_ALERT "%s\n", "parportsnif: starting read");
buf = (char *)kmalloc((length*3)+1, GFP_KERNEL);
if (!buf) {
printk(KERN_ALERT "%s\n", "parportsnif: Failed to allocate read buffer");
return 0;
}
*buf = '\0';
bytes_read = vpp->pfd->f_op->read(vpp->pfd, buffer, length, offset);
for (i = 0; i < bytes_read; i++, buffer++) {
get_user(ch, buffer);
snprintf(&buf[i*3], ((length-i)*3)+1, " %.2X", (unsigned int)ch);
}
vpp_log(vpp, "%d READ %d %s", vpp->port, bytes_read, buf);
kfree(buf);
return(bytes_read);
}
/*
* This function is called when somebody tries to
* write into our device file.
*/
static ssize_t
device_write(struct file *file,
const char __user * buffer, size_t length, loff_t * offset)
{
struct vpp_st *vpp = (struct vpp_st *) file->private_data;
int i;
char *buf;
unsigned char ch;
ssize_t bytes_written;
printk(KERN_ALERT "%s\n", "parportsnif: starting write");
buf = (char *)kmalloc((length*3)+1, GFP_KERNEL);
if (!buf) {
printk(KERN_ALERT "%s\n", "parportsnif: Failed to allocate write buffer");
return 0;
}
*buf = '\0';
bytes_written = vpp->pfd->f_op->write(vpp->pfd, buffer, length, offset);
for (i = 0; i < bytes_written; i++, buffer++) {
get_user(ch, buffer);
snprintf(&buf[i*3], ((length-i)*3)+1, " %.2X", (unsigned int)ch);
}
vpp_log(vpp, "%d WRITE %d %s", vpp->port, bytes_written, buf);
kfree(buf);
return(bytes_written);
}
#define LOGKFOP(el) printk(KERN_INFO "vpp->pfd->f_op->%s = %p\n", #el, vpp->pfd->f_op->el)
#define CASE(a) case a: snprintf(buf, sizeof(buf)-1, "%s", #a)
/*
* This function is called whenever a process tries to do an ioctl on our
* device file. We get two extra parameters (additional to the inode and file
* structures, which all device functions get): the number of the ioctl called
* and the parameter given to the ioctl function.
*
* If the ioctl is write or read/write (meaning output is returned to the
* calling process), the ioctl call returns the output of this function.
*
*/
long device_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct vpp_st *vpp = (struct vpp_st *) file->private_data;
int has_data=0;
unsigned char ch;
char buf[64];
long ret;
/*
* Switch according to the ioctl called
*/
switch(cmd) {
CASE(PPSETMODE);
break;
CASE(PPRSTATUS);
break;
CASE(PPRCONTROL);
break;
CASE(PPWCONTROL);
get_user(ch, (char *)arg);
has_data = 1;
vpp->chanvalues[2] = ch;
break;
CASE(PPFCONTROL);
break;
CASE(PPRDATA);
break;
CASE(PPWDATA);
get_user(ch, (char *)arg);
has_data = 1;
vpp->chanvalues[0] = ch;
break;
CASE(PPCLAIM);
break;
CASE(PPRELEASE);
break;
CASE(PPYIELD);
break;
CASE(PPEXCL);
break;
CASE(PPDATADIR);
break;
CASE(PPNEGOT);
break;
CASE(PPWCTLONIRQ);
break;
CASE(PPCLRIRQ);
break;
CASE(PPSETPHASE);
break;
CASE(PPGETTIME);
break;
CASE(PPSETTIME);
break;
CASE(PPGETMODES);
break;
CASE(PPGETMODE);
break;
CASE(PPGETPHASE);
break;
CASE(PPGETFLAGS);
break;
CASE(PPSETFLAGS);
break;
CASE(PP_FASTWRITE);
break;
CASE(PP_FASTREAD);
break;
CASE(PP_W91284PIC);
break;
default:
snprintf(buf, sizeof(buf)-1, "UNKNOWN %u", cmd);
break;
}
ret = vpp->pfd->f_op->unlocked_ioctl(vpp->pfd, cmd, arg);
if(cmd == PPRSTATUS) {
get_user(ch, (char *)arg);
has_data = 1;
vpp->chanvalues[1] = ch;
}
if(has_data) {
vpp_log_ols(vpp);
}
//if(has_data) {
// vpp_log(vpp, "%d IOCTL %s %.2X", vpp->port, buf, (unsigned int)ch);
//} else {
// vpp_log(vpp, "%d IOCTL %s", vpp->port, buf);
//}
return(ret);
}
/* log file operations */
/**
* This function is called when the /proc file is open.
*
*/
static int log_open(struct inode *inode, struct file *file)
{
return SUCCESS;
}
static int log_release(struct inode *inode, struct file *file)
{
return SUCCESS;
}
static ssize_t log_read(struct file *file,
char __user * buffer, size_t length, loff_t * offset)
{
struct vlogline_st *line = NULL;
size_t sz;
if (!buffer || length <= 0)
return -EINVAL;
if (!vlog.lines) {
if (file->f_flags & O_NONBLOCK) {
return -EAGAIN;
}
wait_event_interruptible(readwait, vlog.lines != NULL);
}
lock_kernel();
line = vlog.lines;
if (!line) {
unlock_kernel();
return 0;
}
sz = ((line->size-line->offset) > length) ? length : (line->size-line->offset);
copy_to_user(buffer, line->data+line->offset, sz);
line->offset += sz;
if (line->offset >= line->size) {
vlog.lines = line->next;
kfree(line->data);
kfree(line);
vlog.totlines--;
}
unlock_kernel();
return sz;
}
static unsigned int log_poll(struct file *file, poll_table * wait)
{
struct vlog_st *vlog = (struct vlog_st *) file->private_data;
unsigned int mask;
poll_wait(file, &readwait, wait);
mask = 0;
if (vlog->lines != NULL)
mask |= POLLIN | POLLRDNORM;
return mask;
}
/* Module Declarations */
/*
* This structure will hold the functions to be called
* when a process does something to the device we
* created. Since a pointer to this structure is kept in
* the devices table, it can't be local to
* init_module. NULL is for unimplemented functions.
*/
struct file_operations vpp_fops = {
.owner = THIS_MODULE,
.llseek = device_llseek,
.read = device_read,
.write = device_write,
.poll = device_poll,
.unlocked_ioctl = device_unlocked_ioctl,
.open = device_open,
.release = device_release, /* a.k.a. close */
};
struct file_operations vlog_fops = {
.owner = THIS_MODULE,
.open = log_open,
.read = log_read,
.poll = log_poll,
.release = log_release
};
static void vpp_attach(struct parport *port)
{
struct device *device;
int err = 0;
device = device_create(vpp_class, port->dev, MKDEV(VPP_MAJOR, port->number),
NULL, "parportsnif%d", port->number);
if (IS_ERR(device)) {
err = PTR_ERR(device);
printk(KERN_WARNING CHRDEV "Error %d while trying to create %s%d", err, CHRDEV, port->number);
}
}
static void vpp_detach(struct parport *port)
{
device_destroy(vpp_class, MKDEV(VPP_MAJOR, port->number));
}
static struct parport_driver vpp_driver = {
.name = CHRDEV,
.attach = vpp_attach,
.detach = vpp_detach,
};
static int __init vpp_init (void)
{
int err = 0;
struct proc_dir_entry *entry;
memset(&vlog, 0, sizeof(vlog));
if (register_chrdev (VPP_MAJOR, CHRDEV, &vpp_fops)) {
printk (KERN_WARNING CHRDEV ": unable to get major %d\n",
VPP_MAJOR);
return -EIO;
}
vpp_class = class_create(THIS_MODULE, CHRDEV);
if (IS_ERR(vpp_class)) {
printk (KERN_WARNING CHRDEV ": error creating class parportsnif\n");
err = PTR_ERR(vpp_class);
goto out_chrdev;
}
if (parport_register_driver(&vpp_driver)) {
printk (KERN_WARNING CHRDEV ": unable to register with parport\n");
goto out_class;
}
/* register log file in /proc */
entry = create_proc_entry("parportlog", 0, NULL);
if (entry) {
entry->proc_fops = &vlog_fops;
}
goto out;
out_class:
class_destroy(vpp_class);
out_chrdev:
unregister_chrdev(VPP_MAJOR, CHRDEV);
out:
return err;
}
static void __exit vpp_cleanup (void)
{
/* Clean up all parport stuff */
remove_proc_entry("parportlog", NULL);
parport_unregister_driver(&vpp_driver);
class_destroy(vpp_class);
unregister_chrdev (VPP_MAJOR, CHRDEV);
}
module_init(vpp_init);
module_exit(vpp_cleanup);
MODULE_LICENSE("GPL");
MODULE_ALIAS_CHARDEV_MAJOR(VPP_MAJOR);