-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch.c
156 lines (122 loc) · 3.36 KB
/
touch.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
/*
driver1.c : driver elementaire
*/
#include <linux/module.h> // needed for modules
#include <linux/kernel.h> // needed for KERN_ALERT
#include <linux/init.h> // needed for macros
#include <linux/fs.h> // needed for driver management
#include <asm/uaccess.h> // needed for copy_to/from_user
#include <asm/system.h>
#include <linux/types.h>
#include <linux/time.h> // needed for time services
#include <linux/cdev.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/slab.h> // needed for kmalloc and kfree
#include "driver.h"
MODULE_AUTHOR("JP Babau - F Touchard");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("premier driver; kernel version : 2.6");
static int myMajor = 0; // allocation dynamique par défaut
static int myMinor = 0;
struct devInfo
{
double x;
double y;
double pressure;
};
struct devInfo *touchscreen;
static void initialisation(){
}
static ssize_t open(struct inode *inode, struct file *filp)
{
struct devInfo *dev;
dev = container_of (inode->i_cdev, struct devInfo, cdev);
filp-> private_data = dev;
return 0;
}
static ssize_t read(struct file *filp, char * buf, size_t count, loff_t *ppos)
{
int result;
struct devInfo *dev = filp->private_data;
if (copy_to_user(buf, &dev->value, sizeof(int))) {
result = -EFAULT;
goto out;
}
result = sizeof(int);
printk(KERN_ALERT "Read operation OK value %d\n", dev->value);
out:
return result;
}
static ssize_t write(struct file *filp, const char * buf, size_t count, loff_t *ppos)
{
int result;
struct devInfo *dev = filp->private_data;
if (copy_from_user(&dev->value, buf, sizeof(int))) {
result = -EFAULT;
goto out;
}
printk(KERN_ALERT "Write operation OK value = %d\n",dev->value);
result = sizeof(int);
out:
return result;
}
static ssize_t release(struct inode *inode, struct file* filp)
{
printk(KERN_ALERT "close()\n");
return 0;
}
static struct file_operations fops =
{
.open = myOpen,
.read = myRead,
.write = myWrite,
.release = myRelease
};
static void my_setup_cdev(struct devInfo *dev, int index)
{
int err, devno=MKDEV(myMajor, myMinor+index);
cdev_init(&dev->cdev, &fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &fops;
err = cdev_add(&dev->cdev, devno, 1);
if (err) printk(KERN_ALERT "error %d adding my_devices%d\n", err, index);
}
static void desinstall(void)
{
dev_t devno = MKDEV(myMajor, myMinor);
printk(KERN_ALERT "see you\n");
kfree(my_devices);
unregister_chrdev_region(devno, MAXDEVICES);
printk(KERN_ALERT "desinstallation OK\n");
}
static int __init install(void)
{
int i, result;
dev_t dev=0;
printk(KERN_ALERT "Initialisation driver touchscreen\n");
printk(KERN_ALERT "install driver : dynamic allocation of major number\n");
result = alloc_chrdev_region(&dev, myMinor, MAXDEVICES, DRIVERNAME);
myMajor = MAJOR(dev);
if (result<0) {
printk(KERN_ALERT "install driver : can't get major number\n");
return result;
}
my_devices = kmalloc(MAXDEVICES*sizeof(struct devInfo), GFP_KERNEL);
if (!my_devices) {
result = -ENOMEM;
goto fail;
}
memset(my_devices, 0, MAXDEVICES*sizeof(struct devInfo));
for (i=0; i < MAXDEVICES; i++) {
my_devices[i].value = -1;
my_setup_cdev(&my_devices[i], i);
}
printk(KERN_ALERT "installation OK, MAJOR NUMBER: %i\n",myMajor);
return 0;
fail:
desinstall();
return result;
}
module_init(install);
module_exit(desinstall);