hwrng: core - Replace u32 in driver API with byte array
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / hw_random / core.c
blob82367262f3a8b604d8d9b11a3b37f3bf18f4fdb9
1 /*
2 Added support for the AMD Geode LX RNG
3 (c) Copyright 2004-2005 Advanced Micro Devices, Inc.
5 derived from
7 Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
8 (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
10 derived from
12 Hardware driver for the AMD 768 Random Number Generator (RNG)
13 (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
15 derived from
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>
21 Added generic RNG API
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>
38 #include <linux/fs.h>
39 #include <linux/sched.h>
40 #include <linux/smp_lock.h>
41 #include <linux/init.h>
42 #include <linux/miscdevice.h>
43 #include <linux/delay.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[SMP_CACHE_BYTES] __cacheline_aligned;
58 static inline int hwrng_init(struct hwrng *rng)
60 if (!rng->init)
61 return 0;
62 return rng->init(rng);
65 static inline void hwrng_cleanup(struct hwrng *rng)
67 if (rng && rng->cleanup)
68 rng->cleanup(rng);
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)
75 return -EINVAL;
76 if (filp->f_mode & FMODE_WRITE)
77 return -EINVAL;
78 cycle_kernel_lock();
79 return 0;
82 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
83 int wait) {
84 int present;
86 if (rng->read)
87 return rng->read(rng, (void *)buffer, size, wait);
89 if (rng->data_present)
90 present = rng->data_present(rng, wait);
91 else
92 present = 1;
94 if (present)
95 return rng->data_read(rng, (u32 *)buffer);
97 return 0;
100 static ssize_t rng_dev_read(struct file *filp, char __user *buf,
101 size_t size, loff_t *offp)
103 ssize_t ret = 0;
104 int err = 0;
105 int bytes_read, len;
107 while (size) {
108 if (mutex_lock_interruptible(&rng_mutex)) {
109 err = -ERESTARTSYS;
110 goto out;
113 if (!current_rng) {
114 err = -ENODEV;
115 goto out_unlock;
118 if (!data_avail) {
119 bytes_read = rng_get_data(current_rng, rng_buffer,
120 sizeof(rng_buffer),
121 !(filp->f_flags & O_NONBLOCK));
122 if (bytes_read < 0) {
123 err = bytes_read;
124 goto out_unlock;
126 data_avail = bytes_read;
129 if (!data_avail) {
130 if (filp->f_flags & O_NONBLOCK) {
131 err = -EAGAIN;
132 goto out_unlock;
134 } else {
135 len = data_avail;
136 if (len > size)
137 len = size;
139 data_avail -= len;
141 if (copy_to_user(buf + ret, rng_buffer + data_avail,
142 len)) {
143 err = -EFAULT;
144 goto out_unlock;
147 size -= len;
148 ret += len;
151 mutex_unlock(&rng_mutex);
153 if (need_resched())
154 schedule_timeout_interruptible(1);
156 if (signal_pending(current)) {
157 err = -ERESTARTSYS;
158 goto out;
161 out_unlock:
162 mutex_unlock(&rng_mutex);
163 out:
164 return ret ? : err;
168 static const struct file_operations rng_chrdev_ops = {
169 .owner = THIS_MODULE,
170 .open = rng_dev_open,
171 .read = rng_dev_read,
174 static struct miscdevice rng_miscdev = {
175 .minor = RNG_MISCDEV_MINOR,
176 .name = RNG_MODULE_NAME,
177 .devnode = "hwrng",
178 .fops = &rng_chrdev_ops,
182 static ssize_t hwrng_attr_current_store(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf, size_t len)
186 int err;
187 struct hwrng *rng;
189 err = mutex_lock_interruptible(&rng_mutex);
190 if (err)
191 return -ERESTARTSYS;
192 err = -ENODEV;
193 list_for_each_entry(rng, &rng_list, list) {
194 if (strcmp(rng->name, buf) == 0) {
195 if (rng == current_rng) {
196 err = 0;
197 break;
199 err = hwrng_init(rng);
200 if (err)
201 break;
202 hwrng_cleanup(current_rng);
203 current_rng = rng;
204 err = 0;
205 break;
208 mutex_unlock(&rng_mutex);
210 return err ? : len;
213 static ssize_t hwrng_attr_current_show(struct device *dev,
214 struct device_attribute *attr,
215 char *buf)
217 int err;
218 ssize_t ret;
219 const char *name = "none";
221 err = mutex_lock_interruptible(&rng_mutex);
222 if (err)
223 return -ERESTARTSYS;
224 if (current_rng)
225 name = current_rng->name;
226 ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
227 mutex_unlock(&rng_mutex);
229 return ret;
232 static ssize_t hwrng_attr_available_show(struct device *dev,
233 struct device_attribute *attr,
234 char *buf)
236 int err;
237 ssize_t ret = 0;
238 struct hwrng *rng;
240 err = mutex_lock_interruptible(&rng_mutex);
241 if (err)
242 return -ERESTARTSYS;
243 buf[0] = '\0';
244 list_for_each_entry(rng, &rng_list, list) {
245 strncat(buf, rng->name, PAGE_SIZE - ret - 1);
246 ret += strlen(rng->name);
247 strncat(buf, " ", PAGE_SIZE - ret - 1);
248 ret++;
250 strncat(buf, "\n", PAGE_SIZE - ret - 1);
251 ret++;
252 mutex_unlock(&rng_mutex);
254 return ret;
257 static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
258 hwrng_attr_current_show,
259 hwrng_attr_current_store);
260 static DEVICE_ATTR(rng_available, S_IRUGO,
261 hwrng_attr_available_show,
262 NULL);
265 static void unregister_miscdev(void)
267 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
268 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
269 misc_deregister(&rng_miscdev);
272 static int register_miscdev(void)
274 int err;
276 err = misc_register(&rng_miscdev);
277 if (err)
278 goto out;
279 err = device_create_file(rng_miscdev.this_device,
280 &dev_attr_rng_current);
281 if (err)
282 goto err_misc_dereg;
283 err = device_create_file(rng_miscdev.this_device,
284 &dev_attr_rng_available);
285 if (err)
286 goto err_remove_current;
287 out:
288 return err;
290 err_remove_current:
291 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
292 err_misc_dereg:
293 misc_deregister(&rng_miscdev);
294 goto out;
297 int hwrng_register(struct hwrng *rng)
299 int must_register_misc;
300 int err = -EINVAL;
301 struct hwrng *old_rng, *tmp;
303 if (rng->name == NULL ||
304 (rng->data_read == NULL && rng->read == NULL))
305 goto out;
307 mutex_lock(&rng_mutex);
309 /* Must not register two RNGs with the same name. */
310 err = -EEXIST;
311 list_for_each_entry(tmp, &rng_list, list) {
312 if (strcmp(tmp->name, rng->name) == 0)
313 goto out_unlock;
316 must_register_misc = (current_rng == NULL);
317 old_rng = current_rng;
318 if (!old_rng) {
319 err = hwrng_init(rng);
320 if (err)
321 goto out_unlock;
322 current_rng = rng;
324 err = 0;
325 if (must_register_misc) {
326 err = register_miscdev();
327 if (err) {
328 if (!old_rng) {
329 hwrng_cleanup(rng);
330 current_rng = NULL;
332 goto out_unlock;
335 INIT_LIST_HEAD(&rng->list);
336 list_add_tail(&rng->list, &rng_list);
337 out_unlock:
338 mutex_unlock(&rng_mutex);
339 out:
340 return err;
342 EXPORT_SYMBOL_GPL(hwrng_register);
344 void hwrng_unregister(struct hwrng *rng)
346 int err;
348 mutex_lock(&rng_mutex);
350 list_del(&rng->list);
351 if (current_rng == rng) {
352 hwrng_cleanup(rng);
353 if (list_empty(&rng_list)) {
354 current_rng = NULL;
355 } else {
356 current_rng = list_entry(rng_list.prev, struct hwrng, list);
357 err = hwrng_init(current_rng);
358 if (err)
359 current_rng = NULL;
362 if (list_empty(&rng_list))
363 unregister_miscdev();
365 mutex_unlock(&rng_mutex);
367 EXPORT_SYMBOL_GPL(hwrng_unregister);
370 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
371 MODULE_LICENSE("GPL");