-
Notifications
You must be signed in to change notification settings - Fork 1
/
lunix-chrdev.c
324 lines (264 loc) · 8.28 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
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
/*
* lunix-chrdev.c
*
* Implementation of character devices
* for Lunix:TNG
*
* Konstantinos Vosinas
* Konstantinos Andriopoulos
*
*/
#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;
/* Prints registers and stack trace in case of bug */
WARN_ON (!(sensor = state->sensor));
/* If the last data that were read are not the latest data on the lunix
sensor buffer, then the state needs refresh */
if (state->buf_timestamp != sensor->msr_data[state->type]->last_update)
return 1;
return 0;
}
/*
* 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;
WARN_ON (!(sensor = state->sensor));
/* lunix_chrdev_read() checks if '-EAGAIN' is returned to put processes to sleep*/
if(!lunix_chrdev_state_needs_refresh(state)) {
return -EAGAIN;
}
uint32_t newdata, last_update;
long lookup = 0;
debug("locking sensor spinlock\n");
spin_lock(&sensor->lock);
/* Grabbing the raw data quickly, and holding the spinlock for as little as possible. */
newdata = sensor->msr_data[state->type]->values[0];
last_update = sensor->msr_data[state->type]->last_update;
spin_unlock(&sensor->lock);
debug("unlocking sensor spinlock\n");
state->buf_timestamp = last_update;
switch (state->type) {
case BATT:
lookup = lookup_voltage[newdata];
break;
case TEMP:
lookup = lookup_temperature[newdata];
break;
case LIGHT:
lookup = lookup_light[newdata];
break;
case N_LUNIX_MSR:
return -EFAULT;
}
/*
* Now we can take our time to format them,
* holding only the private state semaphore
*/
long decimal, fractional;
decimal = lookup / 1000;
fractional = lookup % 1000;
if (lookup < 0){
state->buf_lim = sprintf(state->buf_data, "-%ld.%ld\n", (-1)*decimal, (-1)*fractional);
} else {
state->buf_lim = sprintf(state->buf_data, "%ld.%ld\n", decimal, fractional);
}
debug("leaving update\n");
return 0;
}
/*************************************
* Implementation of file operations
* for the Lunix character device
*************************************/
static int lunix_chrdev_open(struct inode *inode, struct file *filp)
{
debug("entering open\n");
int ret;
if ((ret = nonseekable_open(inode, filp)) < 0) {
goto out;
}
/*
* Associate this open file with the relevant sensor based on
* the minor number of the device node [/dev/sensor<NO>-<TYPE>]
*/
unsigned int minorNum, type, sensorNum;
minorNum = iminor(inode);
/*Each minor number refers to a specific type of measurement and a specific sensor*/
type = minorNum % 8; //Type of measurement (currently, 3 different measurement types allowed, 0 to 2)
sensorNum = minorNum / 8; //Number of the sensor device
if (type >= N_LUNIX_MSR) { //N_LUNIX_MSR is the number of different measurment types (see lunix-chrdev.h)
ret = -EINVAL;
goto out;
}
/* Allocate a new Lunix chr dev state structure */
struct lunix_chrdev_state_struct *state;
state = kmalloc(sizeof(struct lunix_chrdev_state_struct), GFP_KERNEL);
/* The GFP_KERNEL flag means that the process can go to sleep while the kernel is searching
for the memory pages to allocate */
if(!state){
ret = -ENOMEM;
debug("couldn't allocate memmory\n");
goto out;
}
state->type = type;
state->sensor = &lunix_sensors[sensorNum]; //lunix_sensors have been initialized in lunix-module.c
state->buf_lim = 0; //Length of state->buf_data
state->buf_timestamp = 0;
sema_init(&state->lock, 1); //Initialize semaphore for state
/*This will be used by the other file operations of
the driver in order to read and update the data.*/
filp->private_data = state;
ret = 0;
out:
debug("leaving open, with ret = %d\n", ret);
return ret;
}
static int lunix_chrdev_release(struct inode *inode, struct file *filp)
{
kfree(filp->private_data);
debug("released private data successfully \n");
return 0;
}
static long lunix_chrdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
/* Why? */
return -EINVAL;
}
static ssize_t lunix_chrdev_read(struct file *filp, char __user *usrbuf, size_t cnt, loff_t *f_pos)
{
struct lunix_sensor_struct *sensor;
struct lunix_chrdev_state_struct *state;
state = filp->private_data;
WARN_ON(!state);
sensor = state->sensor;
WARN_ON(!sensor);
/* Check if state semaphore is down. down_interruptible() wakes up the user process not only
when the semaphore gets unlocked, but also when a SIGNAL is received (p27, lunix guide) */
if(down_interruptible(&state->lock))
return -ERESTARTSYS; // If interrupted, the read() syscall can be restarted with the same arguments,
// after the interrupt is handled.
/* 'lunix_chrdev_state_update()' must be called while the chr_dev state lock is held */
// If there are data, return them to the user
// otherwise sleep, droping the semaphore
if (*f_pos == 0) {
while (lunix_chrdev_state_update(state) == -EAGAIN) {
/* See LDD3, page 153 for a hint */
// No need for an updates yet.
// Release lock, so that other processes can access 'state'.
up(&state->lock);
// Sleep, using queue, until an update event wakes you up (new data arrived).
if(wait_event_interruptible(sensor->wq, lunix_chrdev_state_needs_refresh(state)))
return -ERESTARTSYS;
// Finally awake! Get access to 'state' again, and go for a state update.
if(down_interruptible(&state->lock))
return -ERESTARTSYS;
}
}
ssize_t ret;
/* Determine the number of cached bytes to copy to userspace */
// if *f_pos + cnt (number of bytes requested by read) is greater than buf_lim,
// then change cnt to as many bytes as possible.
if (*f_pos + cnt > (size_t) state->buf_lim) {
cnt = (size_t) state->buf_lim - *f_pos;
}
if(copy_to_user(usrbuf, state->buf_data + *f_pos, cnt)) {
ret = -EFAULT;
goto out;
}
*f_pos += cnt;
ret = cnt;
/* Auto-rewind on EOF mode */
if(state->buf_lim == *f_pos)
*f_pos = 0;
out:
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);
/* ? */
/* register_chrdev_region? */
ret = register_chrdev_region(dev_no, lunix_minor_cnt, "Lunix-Sensors");
if (ret < 0) {
debug("failed to register region, ret = %d\n", ret);
goto out;
}
/* ? */
/* cdev_add? */
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");
}