blob: 75fb859a50be4740012b9e5afb94a9f446876475 [file] [log] [blame]
Michael Buesch844dd052006-06-26 00:24:59 -07001/*
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 <mbuesch@freenet.de>
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>
Al Viro914e2632006-10-18 13:55:46 -040039#include <linux/sched.h>
Arnd Bergmanna9c4e8f2008-05-20 19:16:00 +020040#include <linux/smp_lock.h>
Michael Buesch844dd052006-06-26 00:24:59 -070041#include <linux/init.h>
42#include <linux/miscdevice.h>
43#include <linux/delay.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);
55
56
57static inline int hwrng_init(struct hwrng *rng)
58{
59 if (!rng->init)
60 return 0;
61 return rng->init(rng);
62}
63
64static inline void hwrng_cleanup(struct hwrng *rng)
65{
66 if (rng && rng->cleanup)
67 rng->cleanup(rng);
68}
69
Patrick McHardy984e9762007-11-21 12:24:45 +080070static inline int hwrng_data_present(struct hwrng *rng, int wait)
Michael Buesch844dd052006-06-26 00:24:59 -070071{
72 if (!rng->data_present)
73 return 1;
Patrick McHardy984e9762007-11-21 12:24:45 +080074 return rng->data_present(rng, wait);
Michael Buesch844dd052006-06-26 00:24:59 -070075}
76
77static inline int hwrng_data_read(struct hwrng *rng, u32 *data)
78{
79 return rng->data_read(rng, data);
80}
81
82
83static int rng_dev_open(struct inode *inode, struct file *filp)
84{
85 /* enforce read-only access to this chrdev */
86 if ((filp->f_mode & FMODE_READ) == 0)
87 return -EINVAL;
88 if (filp->f_mode & FMODE_WRITE)
89 return -EINVAL;
90 return 0;
91}
92
93static ssize_t rng_dev_read(struct file *filp, char __user *buf,
94 size_t size, loff_t *offp)
95{
96 u32 data;
97 ssize_t ret = 0;
Patrick McHardy984e9762007-11-21 12:24:45 +080098 int err = 0;
Michael Buesch844dd052006-06-26 00:24:59 -070099 int bytes_read;
100
101 while (size) {
102 err = -ERESTARTSYS;
103 if (mutex_lock_interruptible(&rng_mutex))
104 goto out;
105 if (!current_rng) {
106 mutex_unlock(&rng_mutex);
107 err = -ENODEV;
108 goto out;
109 }
Patrick McHardy984e9762007-11-21 12:24:45 +0800110
Michael Buesch844dd052006-06-26 00:24:59 -0700111 bytes_read = 0;
Patrick McHardy984e9762007-11-21 12:24:45 +0800112 if (hwrng_data_present(current_rng,
113 !(filp->f_flags & O_NONBLOCK)))
Michael Buesch844dd052006-06-26 00:24:59 -0700114 bytes_read = hwrng_data_read(current_rng, &data);
115 mutex_unlock(&rng_mutex);
116
117 err = -EAGAIN;
118 if (!bytes_read && (filp->f_flags & O_NONBLOCK))
119 goto out;
Ralph Wuerthner893f1122008-04-17 07:46:14 +0200120 if (bytes_read < 0) {
121 err = bytes_read;
122 goto out;
123 }
Michael Buesch844dd052006-06-26 00:24:59 -0700124
125 err = -EFAULT;
126 while (bytes_read && size) {
127 if (put_user((u8)data, buf++))
128 goto out;
129 size--;
130 ret++;
131 bytes_read--;
132 data >>= 8;
133 }
134
135 if (need_resched())
136 schedule_timeout_interruptible(1);
137 err = -ERESTARTSYS;
138 if (signal_pending(current))
139 goto out;
140 }
141out:
142 return ret ? : err;
143}
144
145
Arjan van de Ven62322d22006-07-03 00:24:21 -0700146static const struct file_operations rng_chrdev_ops = {
Michael Buesch844dd052006-06-26 00:24:59 -0700147 .owner = THIS_MODULE,
148 .open = rng_dev_open,
149 .read = rng_dev_read,
150};
151
152static struct miscdevice rng_miscdev = {
153 .minor = RNG_MISCDEV_MINOR,
154 .name = RNG_MODULE_NAME,
Kay Sieverse454cea2009-09-18 23:01:12 +0200155 .nodename = "hwrng",
Michael Buesch844dd052006-06-26 00:24:59 -0700156 .fops = &rng_chrdev_ops,
157};
158
159
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -0700160static ssize_t hwrng_attr_current_store(struct device *dev,
161 struct device_attribute *attr,
Michael Buesch844dd052006-06-26 00:24:59 -0700162 const char *buf, size_t len)
163{
164 int err;
165 struct hwrng *rng;
166
167 err = mutex_lock_interruptible(&rng_mutex);
168 if (err)
169 return -ERESTARTSYS;
170 err = -ENODEV;
171 list_for_each_entry(rng, &rng_list, list) {
172 if (strcmp(rng->name, buf) == 0) {
173 if (rng == current_rng) {
174 err = 0;
175 break;
176 }
177 err = hwrng_init(rng);
178 if (err)
179 break;
180 hwrng_cleanup(current_rng);
181 current_rng = rng;
182 err = 0;
183 break;
184 }
185 }
186 mutex_unlock(&rng_mutex);
187
188 return err ? : len;
189}
190
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -0700191static ssize_t hwrng_attr_current_show(struct device *dev,
192 struct device_attribute *attr,
Michael Buesch844dd052006-06-26 00:24:59 -0700193 char *buf)
194{
195 int err;
196 ssize_t ret;
197 const char *name = "none";
198
199 err = mutex_lock_interruptible(&rng_mutex);
200 if (err)
201 return -ERESTARTSYS;
202 if (current_rng)
203 name = current_rng->name;
204 ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
205 mutex_unlock(&rng_mutex);
206
207 return ret;
208}
209
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -0700210static ssize_t hwrng_attr_available_show(struct device *dev,
211 struct device_attribute *attr,
Michael Buesch844dd052006-06-26 00:24:59 -0700212 char *buf)
213{
214 int err;
215 ssize_t ret = 0;
216 struct hwrng *rng;
217
218 err = mutex_lock_interruptible(&rng_mutex);
219 if (err)
220 return -ERESTARTSYS;
221 buf[0] = '\0';
222 list_for_each_entry(rng, &rng_list, list) {
223 strncat(buf, rng->name, PAGE_SIZE - ret - 1);
224 ret += strlen(rng->name);
225 strncat(buf, " ", PAGE_SIZE - ret - 1);
226 ret++;
227 }
228 strncat(buf, "\n", PAGE_SIZE - ret - 1);
229 ret++;
230 mutex_unlock(&rng_mutex);
231
232 return ret;
233}
234
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -0700235static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
236 hwrng_attr_current_show,
237 hwrng_attr_current_store);
238static DEVICE_ATTR(rng_available, S_IRUGO,
239 hwrng_attr_available_show,
240 NULL);
Michael Buesch844dd052006-06-26 00:24:59 -0700241
242
Rafael J. Wysockib844eba2008-03-23 20:28:24 +0100243static void unregister_miscdev(void)
Michael Buesch844dd052006-06-26 00:24:59 -0700244{
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -0700245 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
246 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
Rafael J. Wysockib844eba2008-03-23 20:28:24 +0100247 misc_deregister(&rng_miscdev);
Michael Buesch844dd052006-06-26 00:24:59 -0700248}
249
250static int register_miscdev(void)
251{
252 int err;
253
254 err = misc_register(&rng_miscdev);
255 if (err)
256 goto out;
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -0700257 err = device_create_file(rng_miscdev.this_device,
258 &dev_attr_rng_current);
Michael Buesch844dd052006-06-26 00:24:59 -0700259 if (err)
260 goto err_misc_dereg;
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -0700261 err = device_create_file(rng_miscdev.this_device,
262 &dev_attr_rng_available);
Michael Buesch844dd052006-06-26 00:24:59 -0700263 if (err)
264 goto err_remove_current;
265out:
266 return err;
267
268err_remove_current:
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -0700269 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
Michael Buesch844dd052006-06-26 00:24:59 -0700270err_misc_dereg:
271 misc_deregister(&rng_miscdev);
272 goto out;
273}
274
275int hwrng_register(struct hwrng *rng)
276{
277 int must_register_misc;
278 int err = -EINVAL;
279 struct hwrng *old_rng, *tmp;
280
281 if (rng->name == NULL ||
282 rng->data_read == NULL)
283 goto out;
284
285 mutex_lock(&rng_mutex);
286
287 /* Must not register two RNGs with the same name. */
288 err = -EEXIST;
289 list_for_each_entry(tmp, &rng_list, list) {
290 if (strcmp(tmp->name, rng->name) == 0)
291 goto out_unlock;
292 }
293
294 must_register_misc = (current_rng == NULL);
295 old_rng = current_rng;
296 if (!old_rng) {
297 err = hwrng_init(rng);
298 if (err)
299 goto out_unlock;
300 current_rng = rng;
301 }
302 err = 0;
303 if (must_register_misc) {
304 err = register_miscdev();
305 if (err) {
306 if (!old_rng) {
307 hwrng_cleanup(rng);
308 current_rng = NULL;
309 }
310 goto out_unlock;
311 }
312 }
313 INIT_LIST_HEAD(&rng->list);
314 list_add_tail(&rng->list, &rng_list);
315out_unlock:
316 mutex_unlock(&rng_mutex);
317out:
318 return err;
319}
320EXPORT_SYMBOL_GPL(hwrng_register);
321
Rafael J. Wysockib844eba2008-03-23 20:28:24 +0100322void hwrng_unregister(struct hwrng *rng)
Michael Buesch844dd052006-06-26 00:24:59 -0700323{
324 int err;
325
326 mutex_lock(&rng_mutex);
327
328 list_del(&rng->list);
329 if (current_rng == rng) {
330 hwrng_cleanup(rng);
331 if (list_empty(&rng_list)) {
332 current_rng = NULL;
333 } else {
334 current_rng = list_entry(rng_list.prev, struct hwrng, list);
335 err = hwrng_init(current_rng);
336 if (err)
337 current_rng = NULL;
338 }
339 }
340 if (list_empty(&rng_list))
Rafael J. Wysockib844eba2008-03-23 20:28:24 +0100341 unregister_miscdev();
Michael Buesch844dd052006-06-26 00:24:59 -0700342
343 mutex_unlock(&rng_mutex);
344}
Rafael J. Wysockib844eba2008-03-23 20:28:24 +0100345EXPORT_SYMBOL_GPL(hwrng_unregister);
Michael Buesch844dd052006-06-26 00:24:59 -0700346
347
348MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
349MODULE_LICENSE("GPL");