-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlunix-chrdev.c
361 lines (304 loc) · 8.43 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
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
/*
* lunix-chrdev.c
*
* Implementation of character devices
* for Lunix:TNG
*
* Andreas Evaggelatos
* Panagiota-Nikoletta Barmpa
*/
#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));
/* ? */
//0elei lock
spin_lock(&sensor->lock);
if(state->buf_timestamp == sensor->msr_data[state->type]->last_update)
{
spin_unlock(&sensor->lock);
return 0;
}
spin_unlock(&sensor->lock);
/* The following return is bogus, just for the stub to compile */
return 1; /* ? */
}
/*
* 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 temp_val;
debug("entered\n");
sensor = state->sensor;
/*
* Grab the raw data quickly, hold the
* spinlock for as little as possible.
*/
/* ? */
if(lunix_chrdev_state_needs_refresh(state) == 0)
{
debug("leaving at needs -EAGAIN\n");
return -EAGAIN;
}
// an den exei nea dedomena dwse -EAGAIN
/* Why use spinlocks? See LDD3, p. 119 */
spin_lock(&sensor->lock);
//SPLIN LOCK TO GET THE DATA FROM THE LINE DISCIPLE SENSOR_STRUCT
//kai valta ston lunix_chrdev_state_struct.
//kanoume copy to timestampt, kai ta dedomena ston buf_data
state->buf_timestamp = sensor->msr_data[state->type]->last_update;
temp_val = sensor->msr_data[state->type]->values[0];
/*
* Any new data available?
*/
/* ? */
//SPIN UNLOCK
spin_unlock(&sensor->lock);
debug("YES THE LINE WORKS---------------------------\n\n\n\n");
debug("%d\n", state->buf_timestamp);
debug("temp_val read %u\n", temp_val);
int dekadiko_meros, akeraio_meros;
long whole;
/*
* Now we can take our time to format them,
* holding only the private state semaphore
*/
switch (state->type)
{
case BATT:
whole = lookup_voltage[temp_val];
break;
case TEMP:
whole = lookup_temperature[temp_val];
break;
case LIGHT:
whole = lookup_light[temp_val];
break;
default:
whole = 0;
break;
}
debug("WHOLE = %ld\n", whole);
akeraio_meros = whole / 1000;
dekadiko_meros = whole % 1000;
state->buf_lim = snprintf(state->buf_data, LUNIX_CHRDEV_BUFSZ, "%d.%d\n", akeraio_meros, dekadiko_meros);
/* ? */
debug("buf data = %s\n", state->buf_data);
debug("leaving\n");
return 0;
}
/*************************************
* Implementation of file operations
* for the Lunix character device
*************************************/
static int lunix_chrdev_open(struct inode *inode, struct file *filp)
{
/* Declarations */
unsigned int mes_id, mes_type, sensor_id;
/* ? */
int ret;
debug("entering\n");
ret = -ENODEV;
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>]
*/
mes_id = iminor(inode);
sensor_id = mes_id >> 3;
mes_type = mes_id & 0x7;
/*
* Check that the device is of the ones we handle.
*/
if(sensor_id >= lunix_sensor_cnt || mes_type >= N_LUNIX_MSR){
ret = -ENODEV;
goto out;
}
//to lunix_sensor_struct * --> lunix_sensors[sensor_id];
/* Allocate a new Lunix character device private state structure */
struct lunix_chrdev_state_struct* new_chdev_state;
new_chdev_state = (struct lunix_chrdev_state_struct* )kmalloc(sizeof(struct lunix_chrdev_state_struct), GFP_KERNEL);
if(!new_chdev_state){
debug("failed to allocate memory for chrdev_state\n");
goto out;
}
//Set state parameters
new_chdev_state->type = mes_type;
new_chdev_state->sensor = &lunix_sensors[sensor_id];
new_chdev_state->buf_lim = 1;
new_chdev_state->buf_data[0] = '\0';
new_chdev_state->buf_timestamp = 0;
//initialize semaphore to avoid race conditions.
sema_init(&new_chdev_state->lock, 1);
//pass it to private data
filp->private_data = new_chdev_state;
debug("successfuly opened\n");
/* ? */
out:
debug("leaving, with ret = %d\n", ret);
return ret;
}
static int lunix_chrdev_release(struct inode *inode, struct file *filp)
{
/* Gets called only 0 reference count. */
kfree((filp->private_data));
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)
{
ssize_t ret = 0;
struct lunix_sensor_struct *sensor;
struct lunix_chrdev_state_struct *state;
state = filp->private_data;
WARN_ON(!state);
sensor = state->sensor;
WARN_ON(!sensor);
/* Lock? */
//DIKO MAS
if (down_interruptible(&state->lock))
return -ERESTARTSYS;
/*
* 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) {
//if we enter here it means all data are consumed
while (lunix_chrdev_state_update(state) == -EAGAIN) {
/* ? */
up(&state->lock); //release the lock.
/* The process needs to sleep */
/* See LDD3, page 153 for a hint */
//if no data.
//mpes sthn oura tou sensor kai 9a se 3upnhsei to line_disciple
if(wait_event_interruptible(sensor->wq, lunix_chrdev_state_needs_refresh(state)))
return -ERESTARTSYS;
//reacquire lock
if (down_interruptible(&state->lock))
return -ERESTARTSYS;
}
}
/* End of file */
/* ? */
/* Determine the number of cached bytes to copy to userspace */
/* ? */
if(*f_pos >= state->buf_lim){
*f_pos = 0;
goto out;
}
// if(*f_pos + cnt > state->buf_lim)
// cnt = state->buf_lim - *f_pos;
cnt = min(cnt, state->buf_lim - *f_pos);
debug("before copy_to_user (newchange) cnt = %d, f_pos = %d\n", cnt, *f_pos);
//the following checks if the pointer is faulty.
if(copy_to_user(usrbuf, state->buf_data + *f_pos, cnt)){
debug("returned from copy_to_user with fault\n");
ret = -EFAULT;
goto out;
}
debug("returned from copy_to_user without problem\n");
*f_pos += cnt;
ret = cnt;
/* Auto-rewind on EOF mode? */
if(*f_pos >= state->buf_lim)
*f_pos = 0; //so that on next read we will block till new data arrive.
/* ? */
out:
/* Unlock? */
up(&state->lock); //release the 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");
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");
}