-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpar.c
102 lines (90 loc) · 2.73 KB
/
par.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
/* Necessary includes for drivers */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h> /* O_ACCMODE */
#include <linux/ioport.h>
#include <asm/system.h> /* cli(), *_flags */
#include <asm/uaccess.h> /* copy_from/to_user */
#include <asm/io.h> /* inb, outb */
MODULE_LICENSE("Dual BSD/GPL");
/* Function declaration of parlelport.c */
int parlelport_open(struct inode *inode, struct file *filp);
int parlelport_release(struct inode *inode, struct file *filp);
ssize_t parlelport_read(struct file *filp,const char *buf, size_t count, loff_t *f_pos);
ssize_t parlelport_write(struct file *filp,const char *buf, size_t count, loff_t *f_pos);
static void parlelport_exit(void);
static int parlelport_init(void);
/* Structure that declares the common */
/* file access fcuntions */
static struct file_operations parlelport_fops = {
read: parlelport_read,
write: parlelport_write, /* line 30 */
open: parlelport_open,
release: parlelport_release
};
/* Driver global variables */
/* Major number */
int parlelport_major = 61;
/* Control variable for memory */
/* reservation of the parallel port*/
int port;
module_init(parlelport_init);
module_exit(parlelport_exit);
int parlelport_init(void) {
int result;
/* Registering device */
result = register_chrdev(parlelport_major, "parlelport", &parlelport_fops);
if (result < 0) {
printk(
"<1>parlelport: cannot obtain major number %d\n",
parlelport_major);
return result;
}
printk("<1>Inserting parlelport module\n");
return 0;
/*fail:
parlelport_exit();
return result;*/
}
void parlelport_exit(void) {
/* Make major number free! */
unregister_chrdev(parlelport_major, "parlelport");
printk(KERN_DEBUG "Removing parlelport module\n");
}
int parlelport_open(struct inode *inode, struct file *filp) {
/* Success */
return 0;
}
int parlelport_release(struct inode *inode, struct file *filp) {
/* Success */
return 0;
}
ssize_t parlelport_read(struct file *filp,const char *buf,
size_t count, loff_t *f_pos) {
/* Buffer to read the device */
char parlelport_buffer;
/* We transfer data to user space */
copy_to_user(buf,&parlelport_buffer,1);
/* We change the reading position as best suits */
if (*f_pos == 0) {
*f_pos+=1;
return 1;
} else {
return 0;
}
}
ssize_t parlelport_write( struct file *filp,const char *buf,
size_t count, loff_t *f_pos) {
char *tmp;
/* Buffer writing to the device */
char parlelport_buffer;
tmp=buf+count-1;
copy_from_user(&parlelport_buffer,tmp,1);
return 1;
}