Loading...
1/*
2 Added support for the AMD Geode LX RNG
3 (c) Copyright 2004-2005 Advanced Micro Devices, Inc.
4
5 derived from
6
7 Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
8 (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
9
10 derived from
11
12 Hardware driver for the AMD 768 Random Number Generator (RNG)
13 (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
14
15 derived from
16
17 Hardware driver for Intel i810 Random Number Generator (RNG)
18 Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
19 Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
20
21 Added generic RNG API
22 Copyright 2006 Michael Buesch <m@bues.ch>
23 Copyright 2005 (c) MontaVista Software, Inc.
24
25 Please read Documentation/hw_random.txt for details on use.
26
27 ----------------------------------------------------------
28 This software may be used and distributed according to the terms
29 of the GNU General Public License, incorporated herein by reference.
30
31 */
32
33
34#include <linux/device.h>
35#include <linux/hw_random.h>
36#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/fs.h>
39#include <linux/sched.h>
40#include <linux/init.h>
41#include <linux/miscdevice.h>
42#include <linux/delay.h>
43#include <asm/uaccess.h>
44
45
46#define RNG_MODULE_NAME "hw_random"
47#define PFX RNG_MODULE_NAME ": "
48#define RNG_MISCDEV_MINOR 183 /* official */
49
50
51static struct hwrng *current_rng;
52static LIST_HEAD(rng_list);
53static DEFINE_MUTEX(rng_mutex);
54static int data_avail;
55static u8 rng_buffer[SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES]
56 __cacheline_aligned;
57
58static inline int hwrng_init(struct hwrng *rng)
59{
60 if (!rng->init)
61 return 0;
62 return rng->init(rng);
63}
64
65static inline void hwrng_cleanup(struct hwrng *rng)
66{
67 if (rng && rng->cleanup)
68 rng->cleanup(rng);
69}
70
71static int rng_dev_open(struct inode *inode, struct file *filp)
72{
73 /* enforce read-only access to this chrdev */
74 if ((filp->f_mode & FMODE_READ) == 0)
75 return -EINVAL;
76 if (filp->f_mode & FMODE_WRITE)
77 return -EINVAL;
78 return 0;
79}
80
81static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
82 int wait) {
83 int present;
84
85 if (rng->read)
86 return rng->read(rng, (void *)buffer, size, wait);
87
88 if (rng->data_present)
89 present = rng->data_present(rng, wait);
90 else
91 present = 1;
92
93 if (present)
94 return rng->data_read(rng, (u32 *)buffer);
95
96 return 0;
97}
98
99static ssize_t rng_dev_read(struct file *filp, char __user *buf,
100 size_t size, loff_t *offp)
101{
102 ssize_t ret = 0;
103 int err = 0;
104 int bytes_read, len;
105
106 while (size) {
107 if (mutex_lock_interruptible(&rng_mutex)) {
108 err = -ERESTARTSYS;
109 goto out;
110 }
111
112 if (!current_rng) {
113 err = -ENODEV;
114 goto out_unlock;
115 }
116
117 if (!data_avail) {
118 bytes_read = rng_get_data(current_rng, rng_buffer,
119 sizeof(rng_buffer),
120 !(filp->f_flags & O_NONBLOCK));
121 if (bytes_read < 0) {
122 err = bytes_read;
123 goto out_unlock;
124 }
125 data_avail = bytes_read;
126 }
127
128 if (!data_avail) {
129 if (filp->f_flags & O_NONBLOCK) {
130 err = -EAGAIN;
131 goto out_unlock;
132 }
133 } else {
134 len = data_avail;
135 if (len > size)
136 len = size;
137
138 data_avail -= len;
139
140 if (copy_to_user(buf + ret, rng_buffer + data_avail,
141 len)) {
142 err = -EFAULT;
143 goto out_unlock;
144 }
145
146 size -= len;
147 ret += len;
148 }
149
150 mutex_unlock(&rng_mutex);
151
152 if (need_resched())
153 schedule_timeout_interruptible(1);
154
155 if (signal_pending(current)) {
156 err = -ERESTARTSYS;
157 goto out;
158 }
159 }
160out:
161 return ret ? : err;
162out_unlock:
163 mutex_unlock(&rng_mutex);
164 goto out;
165}
166
167
168static const struct file_operations rng_chrdev_ops = {
169 .owner = THIS_MODULE,
170 .open = rng_dev_open,
171 .read = rng_dev_read,
172 .llseek = noop_llseek,
173};
174
175static struct miscdevice rng_miscdev = {
176 .minor = RNG_MISCDEV_MINOR,
177 .name = RNG_MODULE_NAME,
178 .nodename = "hwrng",
179 .fops = &rng_chrdev_ops,
180};
181
182
183static ssize_t hwrng_attr_current_store(struct device *dev,
184 struct device_attribute *attr,
185 const char *buf, size_t len)
186{
187 int err;
188 struct hwrng *rng;
189
190 err = mutex_lock_interruptible(&rng_mutex);
191 if (err)
192 return -ERESTARTSYS;
193 err = -ENODEV;
194 list_for_each_entry(rng, &rng_list, list) {
195 if (strcmp(rng->name, buf) == 0) {
196 if (rng == current_rng) {
197 err = 0;
198 break;
199 }
200 err = hwrng_init(rng);
201 if (err)
202 break;
203 hwrng_cleanup(current_rng);
204 current_rng = rng;
205 err = 0;
206 break;
207 }
208 }
209 mutex_unlock(&rng_mutex);
210
211 return err ? : len;
212}
213
214static ssize_t hwrng_attr_current_show(struct device *dev,
215 struct device_attribute *attr,
216 char *buf)
217{
218 int err;
219 ssize_t ret;
220 const char *name = "none";
221
222 err = mutex_lock_interruptible(&rng_mutex);
223 if (err)
224 return -ERESTARTSYS;
225 if (current_rng)
226 name = current_rng->name;
227 ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
228 mutex_unlock(&rng_mutex);
229
230 return ret;
231}
232
233static ssize_t hwrng_attr_available_show(struct device *dev,
234 struct device_attribute *attr,
235 char *buf)
236{
237 int err;
238 ssize_t ret = 0;
239 struct hwrng *rng;
240
241 err = mutex_lock_interruptible(&rng_mutex);
242 if (err)
243 return -ERESTARTSYS;
244 buf[0] = '\0';
245 list_for_each_entry(rng, &rng_list, list) {
246 strncat(buf, rng->name, PAGE_SIZE - ret - 1);
247 ret += strlen(rng->name);
248 strncat(buf, " ", PAGE_SIZE - ret - 1);
249 ret++;
250 }
251 strncat(buf, "\n", PAGE_SIZE - ret - 1);
252 ret++;
253 mutex_unlock(&rng_mutex);
254
255 return ret;
256}
257
258static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
259 hwrng_attr_current_show,
260 hwrng_attr_current_store);
261static DEVICE_ATTR(rng_available, S_IRUGO,
262 hwrng_attr_available_show,
263 NULL);
264
265
266static void unregister_miscdev(void)
267{
268 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
269 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
270 misc_deregister(&rng_miscdev);
271}
272
273static int register_miscdev(void)
274{
275 int err;
276
277 err = misc_register(&rng_miscdev);
278 if (err)
279 goto out;
280 err = device_create_file(rng_miscdev.this_device,
281 &dev_attr_rng_current);
282 if (err)
283 goto err_misc_dereg;
284 err = device_create_file(rng_miscdev.this_device,
285 &dev_attr_rng_available);
286 if (err)
287 goto err_remove_current;
288out:
289 return err;
290
291err_remove_current:
292 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
293err_misc_dereg:
294 misc_deregister(&rng_miscdev);
295 goto out;
296}
297
298int hwrng_register(struct hwrng *rng)
299{
300 int must_register_misc;
301 int err = -EINVAL;
302 struct hwrng *old_rng, *tmp;
303
304 if (rng->name == NULL ||
305 (rng->data_read == NULL && rng->read == NULL))
306 goto out;
307
308 mutex_lock(&rng_mutex);
309
310 /* Must not register two RNGs with the same name. */
311 err = -EEXIST;
312 list_for_each_entry(tmp, &rng_list, list) {
313 if (strcmp(tmp->name, rng->name) == 0)
314 goto out_unlock;
315 }
316
317 must_register_misc = (current_rng == NULL);
318 old_rng = current_rng;
319 if (!old_rng) {
320 err = hwrng_init(rng);
321 if (err)
322 goto out_unlock;
323 current_rng = rng;
324 }
325 err = 0;
326 if (must_register_misc) {
327 err = register_miscdev();
328 if (err) {
329 if (!old_rng) {
330 hwrng_cleanup(rng);
331 current_rng = NULL;
332 }
333 goto out_unlock;
334 }
335 }
336 INIT_LIST_HEAD(&rng->list);
337 list_add_tail(&rng->list, &rng_list);
338out_unlock:
339 mutex_unlock(&rng_mutex);
340out:
341 return err;
342}
343EXPORT_SYMBOL_GPL(hwrng_register);
344
345void hwrng_unregister(struct hwrng *rng)
346{
347 int err;
348
349 mutex_lock(&rng_mutex);
350
351 list_del(&rng->list);
352 if (current_rng == rng) {
353 hwrng_cleanup(rng);
354 if (list_empty(&rng_list)) {
355 current_rng = NULL;
356 } else {
357 current_rng = list_entry(rng_list.prev, struct hwrng, list);
358 err = hwrng_init(current_rng);
359 if (err)
360 current_rng = NULL;
361 }
362 }
363 if (list_empty(&rng_list))
364 unregister_miscdev();
365
366 mutex_unlock(&rng_mutex);
367}
368EXPORT_SYMBOL_GPL(hwrng_unregister);
369
370
371MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
372MODULE_LICENSE("GPL");
1/*
2 Added support for the AMD Geode LX RNG
3 (c) Copyright 2004-2005 Advanced Micro Devices, Inc.
4
5 derived from
6
7 Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
8 (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
9
10 derived from
11
12 Hardware driver for the AMD 768 Random Number Generator (RNG)
13 (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
14
15 derived from
16
17 Hardware driver for Intel i810 Random Number Generator (RNG)
18 Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
19 Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
20
21 Added generic RNG API
22 Copyright 2006 Michael Buesch <m@bues.ch>
23 Copyright 2005 (c) MontaVista Software, Inc.
24
25 Please read Documentation/hw_random.txt for details on use.
26
27 ----------------------------------------------------------
28 This software may be used and distributed according to the terms
29 of the GNU General Public License, incorporated herein by reference.
30
31 */
32
33
34#include <linux/device.h>
35#include <linux/hw_random.h>
36#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/fs.h>
39#include <linux/sched.h>
40#include <linux/miscdevice.h>
41#include <linux/delay.h>
42#include <linux/slab.h>
43#include <linux/random.h>
44#include <asm/uaccess.h>
45
46
47#define RNG_MODULE_NAME "hw_random"
48#define PFX RNG_MODULE_NAME ": "
49#define RNG_MISCDEV_MINOR 183 /* official */
50
51
52static struct hwrng *current_rng;
53static LIST_HEAD(rng_list);
54static DEFINE_MUTEX(rng_mutex);
55static int data_avail;
56static u8 *rng_buffer;
57
58static size_t rng_buffer_size(void)
59{
60 return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
61}
62
63static inline int hwrng_init(struct hwrng *rng)
64{
65 if (!rng->init)
66 return 0;
67 return rng->init(rng);
68}
69
70static inline void hwrng_cleanup(struct hwrng *rng)
71{
72 if (rng && rng->cleanup)
73 rng->cleanup(rng);
74}
75
76static int rng_dev_open(struct inode *inode, struct file *filp)
77{
78 /* enforce read-only access to this chrdev */
79 if ((filp->f_mode & FMODE_READ) == 0)
80 return -EINVAL;
81 if (filp->f_mode & FMODE_WRITE)
82 return -EINVAL;
83 return 0;
84}
85
86static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
87 int wait) {
88 int present;
89
90 if (rng->read)
91 return rng->read(rng, (void *)buffer, size, wait);
92
93 if (rng->data_present)
94 present = rng->data_present(rng, wait);
95 else
96 present = 1;
97
98 if (present)
99 return rng->data_read(rng, (u32 *)buffer);
100
101 return 0;
102}
103
104static ssize_t rng_dev_read(struct file *filp, char __user *buf,
105 size_t size, loff_t *offp)
106{
107 ssize_t ret = 0;
108 int err = 0;
109 int bytes_read, len;
110
111 while (size) {
112 if (mutex_lock_interruptible(&rng_mutex)) {
113 err = -ERESTARTSYS;
114 goto out;
115 }
116
117 if (!current_rng) {
118 err = -ENODEV;
119 goto out_unlock;
120 }
121
122 if (!data_avail) {
123 bytes_read = rng_get_data(current_rng, rng_buffer,
124 rng_buffer_size(),
125 !(filp->f_flags & O_NONBLOCK));
126 if (bytes_read < 0) {
127 err = bytes_read;
128 goto out_unlock;
129 }
130 data_avail = bytes_read;
131 }
132
133 if (!data_avail) {
134 if (filp->f_flags & O_NONBLOCK) {
135 err = -EAGAIN;
136 goto out_unlock;
137 }
138 } else {
139 len = data_avail;
140 if (len > size)
141 len = size;
142
143 data_avail -= len;
144
145 if (copy_to_user(buf + ret, rng_buffer + data_avail,
146 len)) {
147 err = -EFAULT;
148 goto out_unlock;
149 }
150
151 size -= len;
152 ret += len;
153 }
154
155 mutex_unlock(&rng_mutex);
156
157 if (need_resched())
158 schedule_timeout_interruptible(1);
159
160 if (signal_pending(current)) {
161 err = -ERESTARTSYS;
162 goto out;
163 }
164 }
165out:
166 return ret ? : err;
167out_unlock:
168 mutex_unlock(&rng_mutex);
169 goto out;
170}
171
172
173static const struct file_operations rng_chrdev_ops = {
174 .owner = THIS_MODULE,
175 .open = rng_dev_open,
176 .read = rng_dev_read,
177 .llseek = noop_llseek,
178};
179
180static struct miscdevice rng_miscdev = {
181 .minor = RNG_MISCDEV_MINOR,
182 .name = RNG_MODULE_NAME,
183 .nodename = "hwrng",
184 .fops = &rng_chrdev_ops,
185};
186
187
188static ssize_t hwrng_attr_current_store(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf, size_t len)
191{
192 int err;
193 struct hwrng *rng;
194
195 err = mutex_lock_interruptible(&rng_mutex);
196 if (err)
197 return -ERESTARTSYS;
198 err = -ENODEV;
199 list_for_each_entry(rng, &rng_list, list) {
200 if (strcmp(rng->name, buf) == 0) {
201 if (rng == current_rng) {
202 err = 0;
203 break;
204 }
205 err = hwrng_init(rng);
206 if (err)
207 break;
208 hwrng_cleanup(current_rng);
209 current_rng = rng;
210 err = 0;
211 break;
212 }
213 }
214 mutex_unlock(&rng_mutex);
215
216 return err ? : len;
217}
218
219static ssize_t hwrng_attr_current_show(struct device *dev,
220 struct device_attribute *attr,
221 char *buf)
222{
223 int err;
224 ssize_t ret;
225 const char *name = "none";
226
227 err = mutex_lock_interruptible(&rng_mutex);
228 if (err)
229 return -ERESTARTSYS;
230 if (current_rng)
231 name = current_rng->name;
232 ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
233 mutex_unlock(&rng_mutex);
234
235 return ret;
236}
237
238static ssize_t hwrng_attr_available_show(struct device *dev,
239 struct device_attribute *attr,
240 char *buf)
241{
242 int err;
243 ssize_t ret = 0;
244 struct hwrng *rng;
245
246 err = mutex_lock_interruptible(&rng_mutex);
247 if (err)
248 return -ERESTARTSYS;
249 buf[0] = '\0';
250 list_for_each_entry(rng, &rng_list, list) {
251 strncat(buf, rng->name, PAGE_SIZE - ret - 1);
252 ret += strlen(rng->name);
253 strncat(buf, " ", PAGE_SIZE - ret - 1);
254 ret++;
255 }
256 strncat(buf, "\n", PAGE_SIZE - ret - 1);
257 ret++;
258 mutex_unlock(&rng_mutex);
259
260 return ret;
261}
262
263static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
264 hwrng_attr_current_show,
265 hwrng_attr_current_store);
266static DEVICE_ATTR(rng_available, S_IRUGO,
267 hwrng_attr_available_show,
268 NULL);
269
270
271static void unregister_miscdev(void)
272{
273 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
274 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
275 misc_deregister(&rng_miscdev);
276}
277
278static int register_miscdev(void)
279{
280 int err;
281
282 err = misc_register(&rng_miscdev);
283 if (err)
284 goto out;
285 err = device_create_file(rng_miscdev.this_device,
286 &dev_attr_rng_current);
287 if (err)
288 goto err_misc_dereg;
289 err = device_create_file(rng_miscdev.this_device,
290 &dev_attr_rng_available);
291 if (err)
292 goto err_remove_current;
293out:
294 return err;
295
296err_remove_current:
297 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
298err_misc_dereg:
299 misc_deregister(&rng_miscdev);
300 goto out;
301}
302
303int hwrng_register(struct hwrng *rng)
304{
305 int err = -EINVAL;
306 struct hwrng *old_rng, *tmp;
307 unsigned char bytes[16];
308 int bytes_read;
309
310 if (rng->name == NULL ||
311 (rng->data_read == NULL && rng->read == NULL))
312 goto out;
313
314 mutex_lock(&rng_mutex);
315
316 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
317 err = -ENOMEM;
318 if (!rng_buffer) {
319 rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
320 if (!rng_buffer)
321 goto out_unlock;
322 }
323
324 /* Must not register two RNGs with the same name. */
325 err = -EEXIST;
326 list_for_each_entry(tmp, &rng_list, list) {
327 if (strcmp(tmp->name, rng->name) == 0)
328 goto out_unlock;
329 }
330
331 old_rng = current_rng;
332 if (!old_rng) {
333 err = hwrng_init(rng);
334 if (err)
335 goto out_unlock;
336 current_rng = rng;
337 }
338 err = 0;
339 if (!old_rng) {
340 err = register_miscdev();
341 if (err) {
342 hwrng_cleanup(rng);
343 current_rng = NULL;
344 goto out_unlock;
345 }
346 }
347 INIT_LIST_HEAD(&rng->list);
348 list_add_tail(&rng->list, &rng_list);
349
350 bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
351 if (bytes_read > 0)
352 add_device_randomness(bytes, bytes_read);
353out_unlock:
354 mutex_unlock(&rng_mutex);
355out:
356 return err;
357}
358EXPORT_SYMBOL_GPL(hwrng_register);
359
360void hwrng_unregister(struct hwrng *rng)
361{
362 int err;
363
364 mutex_lock(&rng_mutex);
365
366 list_del(&rng->list);
367 if (current_rng == rng) {
368 hwrng_cleanup(rng);
369 if (list_empty(&rng_list)) {
370 current_rng = NULL;
371 } else {
372 current_rng = list_entry(rng_list.prev, struct hwrng, list);
373 err = hwrng_init(current_rng);
374 if (err)
375 current_rng = NULL;
376 }
377 }
378 if (list_empty(&rng_list))
379 unregister_miscdev();
380
381 mutex_unlock(&rng_mutex);
382}
383EXPORT_SYMBOL_GPL(hwrng_unregister);
384
385static void __exit hwrng_exit(void)
386{
387 mutex_lock(&rng_mutex);
388 BUG_ON(current_rng);
389 kfree(rng_buffer);
390 mutex_unlock(&rng_mutex);
391}
392
393module_exit(hwrng_exit);
394
395MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
396MODULE_LICENSE("GPL");