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 <mbuesch@freenet.de>
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/init.h>
41 #include <linux/miscdevice.h>
42 #include <linux/delay.h>
43 #include <asm/uaccess.h>
46 #define RNG_MODULE_NAME "hw_random"
47 #define PFX RNG_MODULE_NAME ": "
48 #define RNG_MISCDEV_MINOR 183 /* official */
51 static struct hwrng
*current_rng
;
52 static LIST_HEAD(rng_list
);
53 static DEFINE_MUTEX(rng_mutex
);
54 static int data_avail
;
55 static u8 rng_buffer
[SMP_CACHE_BYTES
< 32 ? 32 : SMP_CACHE_BYTES
]
58 static inline int hwrng_init(struct hwrng
*rng
)
62 return rng
->init(rng
);
65 static inline void hwrng_cleanup(struct hwrng
*rng
)
67 if (rng
&& rng
->cleanup
)
71 static int rng_dev_open(struct inode
*inode
, struct file
*filp
)
73 /* enforce read-only access to this chrdev */
74 if ((filp
->f_mode
& FMODE_READ
) == 0)
76 if (filp
->f_mode
& FMODE_WRITE
)
81 static inline int rng_get_data(struct hwrng
*rng
, u8
*buffer
, size_t size
,
86 return rng
->read(rng
, (void *)buffer
, size
, wait
);
88 if (rng
->data_present
)
89 present
= rng
->data_present(rng
, wait
);
94 return rng
->data_read(rng
, (u32
*)buffer
);
99 static ssize_t
rng_dev_read(struct file
*filp
, char __user
*buf
,
100 size_t size
, loff_t
*offp
)
107 if (mutex_lock_interruptible(&rng_mutex
)) {
118 bytes_read
= rng_get_data(current_rng
, rng_buffer
,
120 !(filp
->f_flags
& O_NONBLOCK
));
121 if (bytes_read
< 0) {
125 data_avail
= bytes_read
;
129 if (filp
->f_flags
& O_NONBLOCK
) {
140 if (copy_to_user(buf
+ ret
, rng_buffer
+ data_avail
,
150 mutex_unlock(&rng_mutex
);
153 schedule_timeout_interruptible(1);
155 if (signal_pending(current
)) {
163 mutex_unlock(&rng_mutex
);
168 static 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
,
175 static struct miscdevice rng_miscdev
= {
176 .minor
= RNG_MISCDEV_MINOR
,
177 .name
= RNG_MODULE_NAME
,
179 .fops
= &rng_chrdev_ops
,
183 static ssize_t
hwrng_attr_current_store(struct device
*dev
,
184 struct device_attribute
*attr
,
185 const char *buf
, size_t len
)
190 err
= mutex_lock_interruptible(&rng_mutex
);
194 list_for_each_entry(rng
, &rng_list
, list
) {
195 if (strcmp(rng
->name
, buf
) == 0) {
196 if (rng
== current_rng
) {
200 err
= hwrng_init(rng
);
203 hwrng_cleanup(current_rng
);
209 mutex_unlock(&rng_mutex
);
214 static ssize_t
hwrng_attr_current_show(struct device
*dev
,
215 struct device_attribute
*attr
,
220 const char *name
= "none";
222 err
= mutex_lock_interruptible(&rng_mutex
);
226 name
= current_rng
->name
;
227 ret
= snprintf(buf
, PAGE_SIZE
, "%s\n", name
);
228 mutex_unlock(&rng_mutex
);
233 static ssize_t
hwrng_attr_available_show(struct device
*dev
,
234 struct device_attribute
*attr
,
241 err
= mutex_lock_interruptible(&rng_mutex
);
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);
251 strncat(buf
, "\n", PAGE_SIZE
- ret
- 1);
253 mutex_unlock(&rng_mutex
);
258 static DEVICE_ATTR(rng_current
, S_IRUGO
| S_IWUSR
,
259 hwrng_attr_current_show
,
260 hwrng_attr_current_store
);
261 static DEVICE_ATTR(rng_available
, S_IRUGO
,
262 hwrng_attr_available_show
,
266 static void unregister_miscdev(void)
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
);
273 static int register_miscdev(void)
277 err
= misc_register(&rng_miscdev
);
280 err
= device_create_file(rng_miscdev
.this_device
,
281 &dev_attr_rng_current
);
284 err
= device_create_file(rng_miscdev
.this_device
,
285 &dev_attr_rng_available
);
287 goto err_remove_current
;
292 device_remove_file(rng_miscdev
.this_device
, &dev_attr_rng_current
);
294 misc_deregister(&rng_miscdev
);
298 int hwrng_register(struct hwrng
*rng
)
300 int must_register_misc
;
302 struct hwrng
*old_rng
, *tmp
;
304 if (rng
->name
== NULL
||
305 (rng
->data_read
== NULL
&& rng
->read
== NULL
))
308 mutex_lock(&rng_mutex
);
310 /* Must not register two RNGs with the same name. */
312 list_for_each_entry(tmp
, &rng_list
, list
) {
313 if (strcmp(tmp
->name
, rng
->name
) == 0)
317 must_register_misc
= (current_rng
== NULL
);
318 old_rng
= current_rng
;
320 err
= hwrng_init(rng
);
326 if (must_register_misc
) {
327 err
= register_miscdev();
336 INIT_LIST_HEAD(&rng
->list
);
337 list_add_tail(&rng
->list
, &rng_list
);
339 mutex_unlock(&rng_mutex
);
343 EXPORT_SYMBOL_GPL(hwrng_register
);
345 void hwrng_unregister(struct hwrng
*rng
)
349 mutex_lock(&rng_mutex
);
351 list_del(&rng
->list
);
352 if (current_rng
== rng
) {
354 if (list_empty(&rng_list
)) {
357 current_rng
= list_entry(rng_list
.prev
, struct hwrng
, list
);
358 err
= hwrng_init(current_rng
);
363 if (list_empty(&rng_list
))
364 unregister_miscdev();
366 mutex_unlock(&rng_mutex
);
368 EXPORT_SYMBOL_GPL(hwrng_unregister
);
371 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
372 MODULE_LICENSE("GPL");