]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/char/hw_random/core.c
Merge branch 'sbp2-spindown' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee139...
[mirror_ubuntu-artful-kernel.git] / drivers / char / hw_random / core.c
CommitLineData
844dd05f
MB
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 <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>
914e2637 39#include <linux/sched.h>
a9c4e8f7 40#include <linux/smp_lock.h>
844dd05f
MB
41#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
984e976f 70static inline int hwrng_data_present(struct hwrng *rng, int wait)
844dd05f
MB
71{
72 if (!rng->data_present)
73 return 1;
984e976f 74 return rng->data_present(rng, wait);
844dd05f
MB
75}
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;
a9c4e8f7 90 cycle_kernel_lock();
844dd05f
MB
91 return 0;
92}
93
94static ssize_t rng_dev_read(struct file *filp, char __user *buf,
95 size_t size, loff_t *offp)
96{
97 u32 data;
98 ssize_t ret = 0;
984e976f 99 int err = 0;
844dd05f
MB
100 int bytes_read;
101
102 while (size) {
103 err = -ERESTARTSYS;
104 if (mutex_lock_interruptible(&rng_mutex))
105 goto out;
106 if (!current_rng) {
107 mutex_unlock(&rng_mutex);
108 err = -ENODEV;
109 goto out;
110 }
984e976f 111
844dd05f 112 bytes_read = 0;
984e976f
PM
113 if (hwrng_data_present(current_rng,
114 !(filp->f_flags & O_NONBLOCK)))
844dd05f
MB
115 bytes_read = hwrng_data_read(current_rng, &data);
116 mutex_unlock(&rng_mutex);
117
118 err = -EAGAIN;
119 if (!bytes_read && (filp->f_flags & O_NONBLOCK))
120 goto out;
893f1128
RW
121 if (bytes_read < 0) {
122 err = bytes_read;
123 goto out;
124 }
844dd05f
MB
125
126 err = -EFAULT;
127 while (bytes_read && size) {
128 if (put_user((u8)data, buf++))
129 goto out;
130 size--;
131 ret++;
132 bytes_read--;
133 data >>= 8;
134 }
135
136 if (need_resched())
137 schedule_timeout_interruptible(1);
138 err = -ERESTARTSYS;
139 if (signal_pending(current))
140 goto out;
141 }
142out:
143 return ret ? : err;
144}
145
146
62322d25 147static const struct file_operations rng_chrdev_ops = {
844dd05f
MB
148 .owner = THIS_MODULE,
149 .open = rng_dev_open,
150 .read = rng_dev_read,
151};
152
153static struct miscdevice rng_miscdev = {
154 .minor = RNG_MISCDEV_MINOR,
155 .name = RNG_MODULE_NAME,
156 .fops = &rng_chrdev_ops,
157};
158
159
94fbcded
GKH
160static ssize_t hwrng_attr_current_store(struct device *dev,
161 struct device_attribute *attr,
844dd05f
MB
162 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
94fbcded
GKH
191static ssize_t hwrng_attr_current_show(struct device *dev,
192 struct device_attribute *attr,
844dd05f
MB
193 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
94fbcded
GKH
210static ssize_t hwrng_attr_available_show(struct device *dev,
211 struct device_attribute *attr,
844dd05f
MB
212 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
94fbcded
GKH
235static 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);
844dd05f
MB
241
242
b844eba2 243static void unregister_miscdev(void)
844dd05f 244{
94fbcded
GKH
245 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
246 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
b844eba2 247 misc_deregister(&rng_miscdev);
844dd05f
MB
248}
249
250static int register_miscdev(void)
251{
252 int err;
253
254 err = misc_register(&rng_miscdev);
255 if (err)
256 goto out;
94fbcded
GKH
257 err = device_create_file(rng_miscdev.this_device,
258 &dev_attr_rng_current);
844dd05f
MB
259 if (err)
260 goto err_misc_dereg;
94fbcded
GKH
261 err = device_create_file(rng_miscdev.this_device,
262 &dev_attr_rng_available);
844dd05f
MB
263 if (err)
264 goto err_remove_current;
265out:
266 return err;
267
268err_remove_current:
94fbcded 269 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
844dd05f
MB
270err_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
b844eba2 322void hwrng_unregister(struct hwrng *rng)
844dd05f
MB
323{
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))
b844eba2 341 unregister_miscdev();
844dd05f
MB
342
343 mutex_unlock(&rng_mutex);
344}
b844eba2 345EXPORT_SYMBOL_GPL(hwrng_unregister);
844dd05f
MB
346
347
348MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
349MODULE_LICENSE("GPL");