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/delay.h>
42 #include <linux/slab.h>
43 #include <linux/random.h>
44 #include <asm/uaccess.h>
47 #define RNG_MODULE_NAME "hw_random"
48 #define PFX RNG_MODULE_NAME ": "
49 #define RNG_MISCDEV_MINOR 183 /* official */
52 static struct hwrng
*current_rng
;
53 static LIST_HEAD(rng_list
);
54 static DEFINE_MUTEX(rng_mutex
);
55 static int data_avail
;
56 static u8
*rng_buffer
;
58 static inline int rng_get_data(struct hwrng
*rng
, u8
*buffer
, size_t size
,
61 static size_t rng_buffer_size(void)
63 return SMP_CACHE_BYTES
< 32 ? 32 : SMP_CACHE_BYTES
;
66 static void add_early_randomness(struct hwrng
*rng
)
68 unsigned char bytes
[16];
71 bytes_read
= rng_get_data(rng
, bytes
, sizeof(bytes
), 1);
73 add_device_randomness(bytes
, bytes_read
);
76 static inline int hwrng_init(struct hwrng
*rng
)
85 add_early_randomness(rng
);
89 static inline void hwrng_cleanup(struct hwrng
*rng
)
91 if (rng
&& rng
->cleanup
)
95 static int rng_dev_open(struct inode
*inode
, struct file
*filp
)
97 /* enforce read-only access to this chrdev */
98 if ((filp
->f_mode
& FMODE_READ
) == 0)
100 if (filp
->f_mode
& FMODE_WRITE
)
105 static inline int rng_get_data(struct hwrng
*rng
, u8
*buffer
, size_t size
,
110 return rng
->read(rng
, (void *)buffer
, size
, wait
);
112 if (rng
->data_present
)
113 present
= rng
->data_present(rng
, wait
);
118 return rng
->data_read(rng
, (u32
*)buffer
);
123 static ssize_t
rng_dev_read(struct file
*filp
, char __user
*buf
,
124 size_t size
, loff_t
*offp
)
131 if (mutex_lock_interruptible(&rng_mutex
)) {
142 bytes_read
= rng_get_data(current_rng
, rng_buffer
,
144 !(filp
->f_flags
& O_NONBLOCK
));
145 if (bytes_read
< 0) {
149 data_avail
= bytes_read
;
153 if (filp
->f_flags
& O_NONBLOCK
) {
164 if (copy_to_user(buf
+ ret
, rng_buffer
+ data_avail
,
174 mutex_unlock(&rng_mutex
);
177 schedule_timeout_interruptible(1);
179 if (signal_pending(current
)) {
187 mutex_unlock(&rng_mutex
);
192 static const struct file_operations rng_chrdev_ops
= {
193 .owner
= THIS_MODULE
,
194 .open
= rng_dev_open
,
195 .read
= rng_dev_read
,
196 .llseek
= noop_llseek
,
199 static struct miscdevice rng_miscdev
= {
200 .minor
= RNG_MISCDEV_MINOR
,
201 .name
= RNG_MODULE_NAME
,
203 .fops
= &rng_chrdev_ops
,
207 static ssize_t
hwrng_attr_current_store(struct device
*dev
,
208 struct device_attribute
*attr
,
209 const char *buf
, size_t len
)
214 err
= mutex_lock_interruptible(&rng_mutex
);
218 list_for_each_entry(rng
, &rng_list
, list
) {
219 if (strcmp(rng
->name
, buf
) == 0) {
220 if (rng
== current_rng
) {
224 err
= hwrng_init(rng
);
227 hwrng_cleanup(current_rng
);
233 mutex_unlock(&rng_mutex
);
238 static ssize_t
hwrng_attr_current_show(struct device
*dev
,
239 struct device_attribute
*attr
,
244 const char *name
= "none";
246 err
= mutex_lock_interruptible(&rng_mutex
);
250 name
= current_rng
->name
;
251 ret
= snprintf(buf
, PAGE_SIZE
, "%s\n", name
);
252 mutex_unlock(&rng_mutex
);
257 static ssize_t
hwrng_attr_available_show(struct device
*dev
,
258 struct device_attribute
*attr
,
265 err
= mutex_lock_interruptible(&rng_mutex
);
269 list_for_each_entry(rng
, &rng_list
, list
) {
270 strncat(buf
, rng
->name
, PAGE_SIZE
- ret
- 1);
271 ret
+= strlen(rng
->name
);
272 strncat(buf
, " ", PAGE_SIZE
- ret
- 1);
275 strncat(buf
, "\n", PAGE_SIZE
- ret
- 1);
277 mutex_unlock(&rng_mutex
);
282 static DEVICE_ATTR(rng_current
, S_IRUGO
| S_IWUSR
,
283 hwrng_attr_current_show
,
284 hwrng_attr_current_store
);
285 static DEVICE_ATTR(rng_available
, S_IRUGO
,
286 hwrng_attr_available_show
,
290 static void unregister_miscdev(void)
292 device_remove_file(rng_miscdev
.this_device
, &dev_attr_rng_available
);
293 device_remove_file(rng_miscdev
.this_device
, &dev_attr_rng_current
);
294 misc_deregister(&rng_miscdev
);
297 static int register_miscdev(void)
301 err
= misc_register(&rng_miscdev
);
304 err
= device_create_file(rng_miscdev
.this_device
,
305 &dev_attr_rng_current
);
308 err
= device_create_file(rng_miscdev
.this_device
,
309 &dev_attr_rng_available
);
311 goto err_remove_current
;
316 device_remove_file(rng_miscdev
.this_device
, &dev_attr_rng_current
);
318 misc_deregister(&rng_miscdev
);
322 int hwrng_register(struct hwrng
*rng
)
325 struct hwrng
*old_rng
, *tmp
;
327 if (rng
->name
== NULL
||
328 (rng
->data_read
== NULL
&& rng
->read
== NULL
))
331 mutex_lock(&rng_mutex
);
333 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
336 rng_buffer
= kmalloc(rng_buffer_size(), GFP_KERNEL
);
341 /* Must not register two RNGs with the same name. */
343 list_for_each_entry(tmp
, &rng_list
, list
) {
344 if (strcmp(tmp
->name
, rng
->name
) == 0)
348 old_rng
= current_rng
;
350 err
= hwrng_init(rng
);
357 err
= register_miscdev();
364 INIT_LIST_HEAD(&rng
->list
);
365 list_add_tail(&rng
->list
, &rng_list
);
367 if (old_rng
&& !rng
->init
) {
369 * Use a new device's input to add some randomness to
370 * the system. If this rng device isn't going to be
371 * used right away, its init function hasn't been
372 * called yet; so only use the randomness from devices
373 * that don't need an init callback.
375 add_early_randomness(rng
);
379 mutex_unlock(&rng_mutex
);
383 EXPORT_SYMBOL_GPL(hwrng_register
);
385 void hwrng_unregister(struct hwrng
*rng
)
389 mutex_lock(&rng_mutex
);
391 list_del(&rng
->list
);
392 if (current_rng
== rng
) {
394 if (list_empty(&rng_list
)) {
397 current_rng
= list_entry(rng_list
.prev
, struct hwrng
, list
);
398 err
= hwrng_init(current_rng
);
403 if (list_empty(&rng_list
))
404 unregister_miscdev();
406 mutex_unlock(&rng_mutex
);
408 EXPORT_SYMBOL_GPL(hwrng_unregister
);
410 static void __exit
hwrng_exit(void)
412 mutex_lock(&rng_mutex
);
415 mutex_unlock(&rng_mutex
);
418 module_exit(hwrng_exit
);
420 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
421 MODULE_LICENSE("GPL");