-
Notifications
You must be signed in to change notification settings - Fork 0
/
krad.c
272 lines (210 loc) · 6.13 KB
/
krad.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
/*
* Experimental module for using a geiger counter as a hardware RNG
*
* Author:
* Stefan Wendler ([email protected])
* Brendan Whitfield ([email protected])
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/time.h>
#include <linux/hw_random.h>
#include <linux/spinlock.h>
#include <linux/gfp.h>
#include <linux/circ_buf.h>
#define DEBUG 1
/* Define a GPIO for the Geiger counter */
static int geiger_pulse_pin = 3;
/* the assigned IRQ for the geiger pulse pin */
static int geiger_irq = -1;
//circular buffer of random pulse times
#define BUFFER_SIZE (PAGE_SIZE / sizeof(struct timespec))
static struct timespec* buffer;
static int buffer_head = 0;
static int buffer_tail = 0;
DEFINE_SPINLOCK(producer_lock); //lock for the ISR, not that it should need one...
DEFINE_SPINLOCK(consumer_lock); //lock for hwrng API
static int geiger_data_present(struct hwrng* rng, int wait)
{
int head;
int tail;
int size;
spin_lock(&consumer_lock);
head = smp_load_acquire(&buffer_head);
tail = buffer_tail;
spin_unlock(&consumer_lock);
size = CIRC_CNT(head, tail, BUFFER_SIZE) * sizeof(struct timespec);
#ifdef DEBUG
printk(KERN_INFO "krad: geiger_data_present (%d bytes)", size);
#endif
return size;
}
//the old hwrng API
static int geiger_data_read(struct hwrng* rng, u32 *data)
{
int bytes = 0;
int head;
int tail;
#ifdef DEBUG
printk(KERN_INFO "krad: geiger_data_read called\n");
#endif
spin_lock(&consumer_lock);
head = smp_load_acquire(&buffer_head);
tail = buffer_tail;
if(CIRC_CNT(head, tail, BUFFER_SIZE) >= 1)
{
*data = (u32) buffer[tail].tv_nsec;
smp_store_release(&buffer_tail, (tail + 1) & (BUFFER_SIZE - 1));
bytes = 4;
}
spin_unlock(&consumer_lock);
return bytes;
}
//the new hwrng API
static int geiger_read(struct hwrng* rng, void* data, size_t max, bool wait)
{
int head;
int tail;
size_t p;
size_t pulses_given;
#ifdef DEBUG
printk(KERN_INFO "krad: geiger_read called\n");
#endif
spin_lock(&consumer_lock);
head = smp_load_acquire(&buffer_head);
tail = buffer_tail;
//figure out how much we can give them
pulses_given = min((size_t) max / sizeof(struct timespec), //pulses wanted
(size_t) CIRC_CNT(head, tail, BUFFER_SIZE)); //pulses we have
if(!pulses_given)
{
printk(KERN_INFO "krad: %s was called with max bytes (%zu) smaller than the storage type\n", __func__, max);
}
for(p = 0; p < pulses_given; p++)
{
#ifdef DEBUG
printk(KERN_INFO "krad: dispensed pulse: %ld seconds %ld nanoseconds \n", buffer[tail].tv_sec, buffer[tail].tv_nsec);
#endif
((struct timespec*) data)[p] = buffer[tail];
smp_store_release(&buffer_tail, (tail + 1) & (BUFFER_SIZE - 1));
}
spin_unlock(&consumer_lock);
return pulses_given * sizeof(struct timespec);
}
static struct hwrng geiger_rng = {
"krad",
NULL,
NULL,
geiger_data_present,
geiger_data_read,
geiger_read,
0,
32
};
/*
* The interrupt service routine called on geiger pulses
*/
static irqreturn_t geiger_isr(int irq, void *data)
{
if(irq == geiger_irq)
{
struct timespec t = CURRENT_TIME;
int head;
int tail;
#ifdef DEBUG
printk(KERN_INFO "krad: acquired pulse: %ld seconds %ld nanoseconds \n", t.tv_sec, t.tv_nsec);
#endif
spin_lock(&producer_lock);
head = buffer_head;
tail = ACCESS_ONCE(buffer_tail);
if(CIRC_SPACE(head, tail, BUFFER_SIZE) >= 1)
{
buffer[head] = t;
smp_store_release(&buffer_head, (head + 1) & (BUFFER_SIZE - 1));
}
spin_unlock(&producer_lock);
}
return IRQ_HANDLED;
}
/*
* Module init function
*/
static int __init krad_init(void)
{
int ret = 0;
//allocate a single page for our circular buffer
buffer = (struct timespec*) __get_free_page(GFP_KERNEL);
if(!buffer)
{
printk(KERN_ERR "krad: Not enough memory for buffer\n");
return ENOMEM;
}
// register Geiger pulse gpio
ret = gpio_request_one(geiger_pulse_pin, GPIOF_IN, "Geiger Pulse");
if(ret)
{
printk(KERN_ERR "krad: Unable to request GPIO for the Geiger Counter: %d\n", ret);
goto fail1;
}
ret = gpio_to_irq(geiger_pulse_pin);
if(ret < 0)
{
printk(KERN_ERR "krad: Unable to request IRQ: %d\n", ret);
goto fail2;
}
geiger_irq = ret;
ret = request_irq(geiger_irq, geiger_isr, IRQF_TRIGGER_RISING, "krad#geiger", NULL);
if(ret)
{
printk(KERN_ERR "krad: Unable to request IRQ: %d\n", ret);
goto fail2;
}
ret = hwrng_register(&geiger_rng);
if(ret)
{
printk(KERN_ERR "krad: Unable to register hardware RNG device: %d\n", ret);
goto fail3;
}
printk(KERN_INFO "krad: started (buffer size %lu pulses)\n", BUFFER_SIZE);
// finished successfully
return 0;
// failure cases
fail3:
free_irq(geiger_irq, NULL);
fail2:
gpio_free(geiger_pulse_pin);
fail1:
return ret;
}
/**
* Module exit function
*/
static void __exit krad_exit(void)
{
// unregister the hwrng
hwrng_unregister(&geiger_rng);
// free irqs
free_irq(geiger_irq, NULL);
// unregister
gpio_free(geiger_pulse_pin);
//release our buffer memory
free_page((unsigned long) buffer);
printk(KERN_INFO "krad: stopped\n");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Brendan Whitfield");
MODULE_DESCRIPTION("Module for using a geiger counter as a hardware RNG");
module_init(krad_init);
module_exit(krad_exit);