2 Added support for the AMD Geode LX RNG
3 (c) Copyright 2004-2005 Advanced Micro Devices, Inc.
7 Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
8 (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
12 Hardware driver for the AMD 768 Random Number Generator (RNG)
13 (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
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>
22 Copyright 2006 Michael Buesch <m@bues.ch>
23 Copyright 2005 (c) MontaVista Software, Inc.
25 Please read Documentation/hw_random.txt for details on use.
27 ----------------------------------------------------------
28 This software may be used and distributed according to the terms
29 of the GNU General Public License, incorporated herein by reference.
34 #include <linux/device.h>
35 #include <linux/hw_random.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/miscdevice.h>
41 #include <linux/kthread.h>
42 #include <linux/delay.h>
43 #include <linux/slab.h>
44 #include <linux/random.h>
45 #include <asm/uaccess.h>
48 #define RNG_MODULE_NAME "hw_random"
49 #define PFX RNG_MODULE_NAME ": "
50 #define RNG_MISCDEV_MINOR 183 /* official */
53 static struct hwrng
*current_rng
;
54 static struct task_struct
*hwrng_fill
;
55 static LIST_HEAD(rng_list
);
56 static DEFINE_MUTEX(rng_mutex
);
57 static int data_avail
;
58 static u8
*rng_buffer
, *rng_fillbuf
;
59 static unsigned short current_quality
;
60 static unsigned short default_quality
; /* = 0; default to "off" */
62 module_param(current_quality
, ushort
, 0644);
63 MODULE_PARM_DESC(current_quality
,
64 "current hwrng entropy estimation per mill");
65 module_param(default_quality
, ushort
, 0644);
66 MODULE_PARM_DESC(default_quality
,
67 "default entropy content of hwrng per mill");
69 static void start_khwrngd(void);
71 static inline int rng_get_data(struct hwrng
*rng
, u8
*buffer
, size_t size
,
74 static size_t rng_buffer_size(void)
76 return SMP_CACHE_BYTES
< 32 ? 32 : SMP_CACHE_BYTES
;
79 static void add_early_randomness(struct hwrng
*rng
)
81 unsigned char bytes
[16];
84 bytes_read
= rng_get_data(rng
, bytes
, sizeof(bytes
), 1);
86 add_device_randomness(bytes
, bytes_read
);
89 static inline int hwrng_init(struct hwrng
*rng
)
98 add_early_randomness(rng
);
100 current_quality
= rng
->quality
? : default_quality
;
101 current_quality
&= 1023;
103 if (current_quality
== 0 && hwrng_fill
)
104 kthread_stop(hwrng_fill
);
105 if (current_quality
> 0 && !hwrng_fill
)
111 static inline void hwrng_cleanup(struct hwrng
*rng
)
113 if (rng
&& rng
->cleanup
)
117 static int rng_dev_open(struct inode
*inode
, struct file
*filp
)
119 /* enforce read-only access to this chrdev */
120 if ((filp
->f_mode
& FMODE_READ
) == 0)
122 if (filp
->f_mode
& FMODE_WRITE
)
127 static inline int rng_get_data(struct hwrng
*rng
, u8
*buffer
, size_t size
,
132 return rng
->read(rng
, (void *)buffer
, size
, wait
);
134 if (rng
->data_present
)
135 present
= rng
->data_present(rng
, wait
);
140 return rng
->data_read(rng
, (u32
*)buffer
);
145 static ssize_t
rng_dev_read(struct file
*filp
, char __user
*buf
,
146 size_t size
, loff_t
*offp
)
153 if (mutex_lock_interruptible(&rng_mutex
)) {
164 bytes_read
= rng_get_data(current_rng
, rng_buffer
,
166 !(filp
->f_flags
& O_NONBLOCK
));
167 if (bytes_read
< 0) {
171 data_avail
= bytes_read
;
175 if (filp
->f_flags
& O_NONBLOCK
) {
186 if (copy_to_user(buf
+ ret
, rng_buffer
+ data_avail
,
196 mutex_unlock(&rng_mutex
);
199 schedule_timeout_interruptible(1);
201 if (signal_pending(current
)) {
209 mutex_unlock(&rng_mutex
);
214 static const struct file_operations rng_chrdev_ops
= {
215 .owner
= THIS_MODULE
,
216 .open
= rng_dev_open
,
217 .read
= rng_dev_read
,
218 .llseek
= noop_llseek
,
221 static struct miscdevice rng_miscdev
= {
222 .minor
= RNG_MISCDEV_MINOR
,
223 .name
= RNG_MODULE_NAME
,
225 .fops
= &rng_chrdev_ops
,
229 static ssize_t
hwrng_attr_current_store(struct device
*dev
,
230 struct device_attribute
*attr
,
231 const char *buf
, size_t len
)
236 err
= mutex_lock_interruptible(&rng_mutex
);
240 list_for_each_entry(rng
, &rng_list
, list
) {
241 if (strcmp(rng
->name
, buf
) == 0) {
242 if (rng
== current_rng
) {
246 err
= hwrng_init(rng
);
249 hwrng_cleanup(current_rng
);
255 mutex_unlock(&rng_mutex
);
260 static ssize_t
hwrng_attr_current_show(struct device
*dev
,
261 struct device_attribute
*attr
,
266 const char *name
= "none";
268 err
= mutex_lock_interruptible(&rng_mutex
);
272 name
= current_rng
->name
;
273 ret
= snprintf(buf
, PAGE_SIZE
, "%s\n", name
);
274 mutex_unlock(&rng_mutex
);
279 static ssize_t
hwrng_attr_available_show(struct device
*dev
,
280 struct device_attribute
*attr
,
286 err
= mutex_lock_interruptible(&rng_mutex
);
290 list_for_each_entry(rng
, &rng_list
, list
) {
291 strlcat(buf
, rng
->name
, PAGE_SIZE
);
292 strlcat(buf
, " ", PAGE_SIZE
);
294 strlcat(buf
, "\n", PAGE_SIZE
);
295 mutex_unlock(&rng_mutex
);
300 static DEVICE_ATTR(rng_current
, S_IRUGO
| S_IWUSR
,
301 hwrng_attr_current_show
,
302 hwrng_attr_current_store
);
303 static DEVICE_ATTR(rng_available
, S_IRUGO
,
304 hwrng_attr_available_show
,
308 static void unregister_miscdev(void)
310 device_remove_file(rng_miscdev
.this_device
, &dev_attr_rng_available
);
311 device_remove_file(rng_miscdev
.this_device
, &dev_attr_rng_current
);
312 misc_deregister(&rng_miscdev
);
315 static int register_miscdev(void)
319 err
= misc_register(&rng_miscdev
);
322 err
= device_create_file(rng_miscdev
.this_device
,
323 &dev_attr_rng_current
);
326 err
= device_create_file(rng_miscdev
.this_device
,
327 &dev_attr_rng_available
);
329 goto err_remove_current
;
334 device_remove_file(rng_miscdev
.this_device
, &dev_attr_rng_current
);
336 misc_deregister(&rng_miscdev
);
340 static int hwrng_fillfn(void *unused
)
344 while (!kthread_should_stop()) {
347 rc
= rng_get_data(current_rng
, rng_fillbuf
,
348 rng_buffer_size(), 1);
350 pr_warn("hwrng: no data available\n");
351 msleep_interruptible(10000);
354 add_hwgenerator_randomness((void *)rng_fillbuf
, rc
,
355 rc
* current_quality
* 8 >> 10);
361 static void start_khwrngd(void)
363 hwrng_fill
= kthread_run(hwrng_fillfn
, NULL
, "hwrng");
364 if (hwrng_fill
== ERR_PTR(-ENOMEM
)) {
365 pr_err("hwrng_fill thread creation failed");
370 int hwrng_register(struct hwrng
*rng
)
373 struct hwrng
*old_rng
, *tmp
;
375 if (rng
->name
== NULL
||
376 (rng
->data_read
== NULL
&& rng
->read
== NULL
))
379 mutex_lock(&rng_mutex
);
381 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
384 rng_buffer
= kmalloc(rng_buffer_size(), GFP_KERNEL
);
389 rng_fillbuf
= kmalloc(rng_buffer_size(), GFP_KERNEL
);
396 /* Must not register two RNGs with the same name. */
398 list_for_each_entry(tmp
, &rng_list
, list
) {
399 if (strcmp(tmp
->name
, rng
->name
) == 0)
403 old_rng
= current_rng
;
405 err
= hwrng_init(rng
);
412 err
= register_miscdev();
419 INIT_LIST_HEAD(&rng
->list
);
420 list_add_tail(&rng
->list
, &rng_list
);
422 if (old_rng
&& !rng
->init
) {
424 * Use a new device's input to add some randomness to
425 * the system. If this rng device isn't going to be
426 * used right away, its init function hasn't been
427 * called yet; so only use the randomness from devices
428 * that don't need an init callback.
430 add_early_randomness(rng
);
434 mutex_unlock(&rng_mutex
);
438 EXPORT_SYMBOL_GPL(hwrng_register
);
440 void hwrng_unregister(struct hwrng
*rng
)
444 mutex_lock(&rng_mutex
);
446 list_del(&rng
->list
);
447 if (current_rng
== rng
) {
449 if (list_empty(&rng_list
)) {
452 current_rng
= list_entry(rng_list
.prev
, struct hwrng
, list
);
453 err
= hwrng_init(current_rng
);
458 if (list_empty(&rng_list
)) {
459 unregister_miscdev();
461 kthread_stop(hwrng_fill
);
464 mutex_unlock(&rng_mutex
);
466 EXPORT_SYMBOL_GPL(hwrng_unregister
);
468 static void __exit
hwrng_exit(void)
470 mutex_lock(&rng_mutex
);
474 mutex_unlock(&rng_mutex
);
477 module_exit(hwrng_exit
);
479 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
480 MODULE_LICENSE("GPL");