-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunix-chrdev.c
executable file
·264 lines (223 loc) · 6.1 KB
/
lunix-chrdev.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
/*
* lunix-chrdev.c
*
* Implementation of character devices
* for Lunix:TNG as a project of the Operating Systems Lab
* course of the 7th Semester
*
* Giannis Giortzis <giortzis.giannis@gmail.com>
* Dimitris Lampros <dim.lambros@gmail.com>
* Team 7
*/
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/cdev.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/ioctl.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mmzone.h>
#include <linux/vmalloc.h>
#include <linux/spinlock.h>
#include "lunix.h"
#include "lunix-chrdev.h"
#include "lunix-lookup.h"
/*
* Global data
*/
struct cdev lunix_chrdev_cdev;
/*
* Just a quick [unlocked] check to see if the cached
* chrdev state needs to be updated from sensor measurements.
*/
static int lunix_chrdev_state_needs_refresh(struct lunix_chrdev_state_struct *state)
{
struct lunix_sensor_struct *sensor;
WARN_ON ( !(sensor = state->sensor));
return sensor->msr_data[state->type]->last_update != state->buf_timestamp;
}
/*
* Updates the cached state of a character device
* based on sensor data. Must be called with the
* character device state lock held.
*/
static int lunix_chrdev_state_update(struct lunix_chrdev_state_struct *state)
{
struct lunix_sensor_struct *sensor;
uint32_t tmp;
long val = 0;
char *measurement = "";
debug("leaving\n");
if (lunix_chrdev_state_needs_refresh(state)){
WARN_ON(!(sensor = state->sensor));
spin_lock(&sensor->lock);
/*
* Changing the buffer timestamp
* and saving the data to release
* the spin_lock fast
*/
state->buf_timestamp = sensor->msr_data[state->type]->last_update ;
tmp = sensor->msr_data[state->type]->values[0];
spin_unlock(&sensor->lock);
/*
* Now we can take our time to format them,
* holding only the private state semaphore
*/
switch (state->type){
case BATT:
val = lookup_voltage[tmp];
measurement = "V";
break;
case TEMP:
val = lookup_temperature[tmp];
measurement = "°C";
break;
case LIGHT:
val = lookup_light[tmp];
measurement = "lx";
break;
default:
return -EAGAIN;
}
state->buf_lim = sprintf(state->buf_data, "+%ld.%ld %s\n", val/1000,val%1000, measurement);
debug("leaving\n");
return 0;
}
return -EAGAIN;
}
/*************************************
* Implementation of file operations
* for the Lunix character device
*************************************/
static int lunix_chrdev_open(struct inode *inode, struct file *filp)
{
/* Declarations */
struct lunix_chrdev_state_struct *dev;
int ret, minor_num;
debug("entering\n");
ret = -ENODEV;
if ((ret = nonseekable_open(inode, filp)) < 0){
goto out;
}
minor_num = iminor(inode);
/*
* Associate this open file with the relevant sensor based on
* the minor number of the device node [/dev/sensor<NO>-<TYPE>]
*/
dev = kmalloc(sizeof(struct lunix_chrdev_state_struct),GFP_KERNEL);
//initialise semaphore of state as non-blocking
sema_init(&dev->lock, 1);
dev->buf_timestamp = 0;
dev->type = minor_num%8;
dev->sensor = &lunix_sensors[minor_num/8];
dev->buf_lim = 0;
filp->private_data = dev;
out:
debug("leaving, with ret = %d\n", ret);
return ret;
}
static int lunix_chrdev_release(struct inode *inode, struct file *filp)
{
kfree(filp->private_data);
return 0;
}
static long lunix_chrdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg){
return -EINVAL;
}
static ssize_t lunix_chrdev_read(struct file *filp, char __user *usrbuf, size_t cnt, loff_t *f_pos)
{
ssize_t ret;
struct lunix_sensor_struct *sensor;
struct lunix_chrdev_state_struct *state;
WARN_ON(!(state = filp->private_data));
WARN_ON(!(sensor = state->sensor));
down(&state->lock); // lock the semaphore to read the data
/*
* If the cached character device state needs to be
* updated by actual sensor data (i.e. we need to report
* on a "fresh" measurement, do so
*/
if (*f_pos == 0) {
while (lunix_chrdev_state_update(state) == -EAGAIN) {
up(&state->lock);
if(wait_event_interruptible(sensor->wq, lunix_chrdev_state_needs_refresh(state)))
return ERESTARTSYS;
down(&state->lock);
}
}
/* End of file */
if(cnt>state->buf_lim-*f_pos)
cnt=state->buf_lim-*f_pos;
ret=cnt;
/* Determine the number of cached bytes to copy to userspace */
if(copy_to_user(usrbuf, state->buf_data+*f_pos, cnt)){
ret=-EFAULT;
goto out;
}
*f_pos+=cnt;
/* Auto-rewind on EOF mode? */
if(*f_pos>=state->buf_lim) *f_pos=0;
out:
/* Unlock*/
up(&state->lock);
return ret;
}
static int lunix_chrdev_mmap(struct file *filp, struct vm_area_struct *vma)
{
return -EINVAL;
}
static struct file_operations lunix_chrdev_fops =
{
.owner = THIS_MODULE,
.open = lunix_chrdev_open,
.release = lunix_chrdev_release,
.read = lunix_chrdev_read,
.unlocked_ioctl = lunix_chrdev_ioctl,
.mmap = lunix_chrdev_mmap
};
int lunix_chrdev_init(void)
{
/*
* Register the character device with the kernel, asking for
* a range of minor numbers (number of sensors * 8 measurements / sensor)
* beginning with LINUX_CHRDEV_MAJOR:0
*/
int ret;
dev_t dev_no;
unsigned int lunix_minor_cnt = lunix_sensor_cnt << 3;
debug("initializing character device\n");
cdev_init(&lunix_chrdev_cdev, &lunix_chrdev_fops);
lunix_chrdev_cdev.owner = THIS_MODULE;
dev_no = MKDEV(LUNIX_CHRDEV_MAJOR, 0);
ret = register_chrdev_region(dev_no, lunix_minor_cnt, "LunixTNG");
if (ret < 0) {
debug("failed to register region, ret = %d\n", ret);
goto out;
}
ret = cdev_add(&lunix_chrdev_cdev, dev_no, lunix_minor_cnt);
if (ret < 0) {
debug("failed to add character device\n");
goto out_with_chrdev_region;
}
debug("completed successfully\n");
return 0;
out_with_chrdev_region:
unregister_chrdev_region(dev_no, lunix_minor_cnt);
out:
return ret;
}
void lunix_chrdev_destroy(void)
{
dev_t dev_no;
unsigned int lunix_minor_cnt = lunix_sensor_cnt << 3;
debug("entering\n");
dev_no = MKDEV(LUNIX_CHRDEV_MAJOR, 0);
cdev_del(&lunix_chrdev_cdev);
unregister_chrdev_region(dev_no, lunix_minor_cnt);
debug("leaving\n");
}