Merge tag 'sound-3.16-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[cascardo/linux.git] / drivers / char / hw_random / core.c
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 <m@bues.ch>
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>
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>
45
46
47 #define RNG_MODULE_NAME         "hw_random"
48 #define PFX                     RNG_MODULE_NAME ": "
49 #define RNG_MISCDEV_MINOR       183 /* official */
50
51
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;
57
58 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
59                                int wait);
60
61 static size_t rng_buffer_size(void)
62 {
63         return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
64 }
65
66 static void add_early_randomness(struct hwrng *rng)
67 {
68         unsigned char bytes[16];
69         int bytes_read;
70
71         /*
72          * Currently only virtio-rng cannot return data during device
73          * probe, and that's handled in virtio-rng.c itself.  If there
74          * are more such devices, this call to rng_get_data can be
75          * made conditional here instead of doing it per-device.
76          */
77         bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
78         if (bytes_read > 0)
79                 add_device_randomness(bytes, bytes_read);
80 }
81
82 static inline int hwrng_init(struct hwrng *rng)
83 {
84         if (rng->init) {
85                 int ret;
86
87                 ret =  rng->init(rng);
88                 if (ret)
89                         return ret;
90         }
91         add_early_randomness(rng);
92         return 0;
93 }
94
95 static inline void hwrng_cleanup(struct hwrng *rng)
96 {
97         if (rng && rng->cleanup)
98                 rng->cleanup(rng);
99 }
100
101 static int rng_dev_open(struct inode *inode, struct file *filp)
102 {
103         /* enforce read-only access to this chrdev */
104         if ((filp->f_mode & FMODE_READ) == 0)
105                 return -EINVAL;
106         if (filp->f_mode & FMODE_WRITE)
107                 return -EINVAL;
108         return 0;
109 }
110
111 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
112                         int wait) {
113         int present;
114
115         if (rng->read)
116                 return rng->read(rng, (void *)buffer, size, wait);
117
118         if (rng->data_present)
119                 present = rng->data_present(rng, wait);
120         else
121                 present = 1;
122
123         if (present)
124                 return rng->data_read(rng, (u32 *)buffer);
125
126         return 0;
127 }
128
129 static ssize_t rng_dev_read(struct file *filp, char __user *buf,
130                             size_t size, loff_t *offp)
131 {
132         ssize_t ret = 0;
133         int err = 0;
134         int bytes_read, len;
135
136         while (size) {
137                 if (mutex_lock_interruptible(&rng_mutex)) {
138                         err = -ERESTARTSYS;
139                         goto out;
140                 }
141
142                 if (!current_rng) {
143                         err = -ENODEV;
144                         goto out_unlock;
145                 }
146
147                 if (!data_avail) {
148                         bytes_read = rng_get_data(current_rng, rng_buffer,
149                                 rng_buffer_size(),
150                                 !(filp->f_flags & O_NONBLOCK));
151                         if (bytes_read < 0) {
152                                 err = bytes_read;
153                                 goto out_unlock;
154                         }
155                         data_avail = bytes_read;
156                 }
157
158                 if (!data_avail) {
159                         if (filp->f_flags & O_NONBLOCK) {
160                                 err = -EAGAIN;
161                                 goto out_unlock;
162                         }
163                 } else {
164                         len = data_avail;
165                         if (len > size)
166                                 len = size;
167
168                         data_avail -= len;
169
170                         if (copy_to_user(buf + ret, rng_buffer + data_avail,
171                                                                 len)) {
172                                 err = -EFAULT;
173                                 goto out_unlock;
174                         }
175
176                         size -= len;
177                         ret += len;
178                 }
179
180                 mutex_unlock(&rng_mutex);
181
182                 if (need_resched())
183                         schedule_timeout_interruptible(1);
184
185                 if (signal_pending(current)) {
186                         err = -ERESTARTSYS;
187                         goto out;
188                 }
189         }
190 out:
191         return ret ? : err;
192 out_unlock:
193         mutex_unlock(&rng_mutex);
194         goto out;
195 }
196
197
198 static const struct file_operations rng_chrdev_ops = {
199         .owner          = THIS_MODULE,
200         .open           = rng_dev_open,
201         .read           = rng_dev_read,
202         .llseek         = noop_llseek,
203 };
204
205 static struct miscdevice rng_miscdev = {
206         .minor          = RNG_MISCDEV_MINOR,
207         .name           = RNG_MODULE_NAME,
208         .nodename       = "hwrng",
209         .fops           = &rng_chrdev_ops,
210 };
211
212
213 static ssize_t hwrng_attr_current_store(struct device *dev,
214                                         struct device_attribute *attr,
215                                         const char *buf, size_t len)
216 {
217         int err;
218         struct hwrng *rng;
219
220         err = mutex_lock_interruptible(&rng_mutex);
221         if (err)
222                 return -ERESTARTSYS;
223         err = -ENODEV;
224         list_for_each_entry(rng, &rng_list, list) {
225                 if (strcmp(rng->name, buf) == 0) {
226                         if (rng == current_rng) {
227                                 err = 0;
228                                 break;
229                         }
230                         err = hwrng_init(rng);
231                         if (err)
232                                 break;
233                         hwrng_cleanup(current_rng);
234                         current_rng = rng;
235                         err = 0;
236                         break;
237                 }
238         }
239         mutex_unlock(&rng_mutex);
240
241         return err ? : len;
242 }
243
244 static ssize_t hwrng_attr_current_show(struct device *dev,
245                                        struct device_attribute *attr,
246                                        char *buf)
247 {
248         int err;
249         ssize_t ret;
250         const char *name = "none";
251
252         err = mutex_lock_interruptible(&rng_mutex);
253         if (err)
254                 return -ERESTARTSYS;
255         if (current_rng)
256                 name = current_rng->name;
257         ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
258         mutex_unlock(&rng_mutex);
259
260         return ret;
261 }
262
263 static ssize_t hwrng_attr_available_show(struct device *dev,
264                                          struct device_attribute *attr,
265                                          char *buf)
266 {
267         int err;
268         ssize_t ret = 0;
269         struct hwrng *rng;
270
271         err = mutex_lock_interruptible(&rng_mutex);
272         if (err)
273                 return -ERESTARTSYS;
274         buf[0] = '\0';
275         list_for_each_entry(rng, &rng_list, list) {
276                 strncat(buf, rng->name, PAGE_SIZE - ret - 1);
277                 ret += strlen(rng->name);
278                 strncat(buf, " ", PAGE_SIZE - ret - 1);
279                 ret++;
280         }
281         strncat(buf, "\n", PAGE_SIZE - ret - 1);
282         ret++;
283         mutex_unlock(&rng_mutex);
284
285         return ret;
286 }
287
288 static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
289                    hwrng_attr_current_show,
290                    hwrng_attr_current_store);
291 static DEVICE_ATTR(rng_available, S_IRUGO,
292                    hwrng_attr_available_show,
293                    NULL);
294
295
296 static void unregister_miscdev(void)
297 {
298         device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
299         device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
300         misc_deregister(&rng_miscdev);
301 }
302
303 static int register_miscdev(void)
304 {
305         int err;
306
307         err = misc_register(&rng_miscdev);
308         if (err)
309                 goto out;
310         err = device_create_file(rng_miscdev.this_device,
311                                  &dev_attr_rng_current);
312         if (err)
313                 goto err_misc_dereg;
314         err = device_create_file(rng_miscdev.this_device,
315                                  &dev_attr_rng_available);
316         if (err)
317                 goto err_remove_current;
318 out:
319         return err;
320
321 err_remove_current:
322         device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
323 err_misc_dereg:
324         misc_deregister(&rng_miscdev);
325         goto out;
326 }
327
328 int hwrng_register(struct hwrng *rng)
329 {
330         int err = -EINVAL;
331         struct hwrng *old_rng, *tmp;
332
333         if (rng->name == NULL ||
334             (rng->data_read == NULL && rng->read == NULL))
335                 goto out;
336
337         mutex_lock(&rng_mutex);
338
339         /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
340         err = -ENOMEM;
341         if (!rng_buffer) {
342                 rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
343                 if (!rng_buffer)
344                         goto out_unlock;
345         }
346
347         /* Must not register two RNGs with the same name. */
348         err = -EEXIST;
349         list_for_each_entry(tmp, &rng_list, list) {
350                 if (strcmp(tmp->name, rng->name) == 0)
351                         goto out_unlock;
352         }
353
354         old_rng = current_rng;
355         if (!old_rng) {
356                 err = hwrng_init(rng);
357                 if (err)
358                         goto out_unlock;
359                 current_rng = rng;
360         }
361         err = 0;
362         if (!old_rng) {
363                 err = register_miscdev();
364                 if (err) {
365                         hwrng_cleanup(rng);
366                         current_rng = NULL;
367                         goto out_unlock;
368                 }
369         }
370         INIT_LIST_HEAD(&rng->list);
371         list_add_tail(&rng->list, &rng_list);
372
373         if (old_rng && !rng->init) {
374                 /*
375                  * Use a new device's input to add some randomness to
376                  * the system.  If this rng device isn't going to be
377                  * used right away, its init function hasn't been
378                  * called yet; so only use the randomness from devices
379                  * that don't need an init callback.
380                  */
381                 add_early_randomness(rng);
382         }
383
384 out_unlock:
385         mutex_unlock(&rng_mutex);
386 out:
387         return err;
388 }
389 EXPORT_SYMBOL_GPL(hwrng_register);
390
391 void hwrng_unregister(struct hwrng *rng)
392 {
393         int err;
394
395         mutex_lock(&rng_mutex);
396
397         list_del(&rng->list);
398         if (current_rng == rng) {
399                 hwrng_cleanup(rng);
400                 if (list_empty(&rng_list)) {
401                         current_rng = NULL;
402                 } else {
403                         current_rng = list_entry(rng_list.prev, struct hwrng, list);
404                         err = hwrng_init(current_rng);
405                         if (err)
406                                 current_rng = NULL;
407                 }
408         }
409         if (list_empty(&rng_list))
410                 unregister_miscdev();
411
412         mutex_unlock(&rng_mutex);
413 }
414 EXPORT_SYMBOL_GPL(hwrng_unregister);
415
416 static void __exit hwrng_exit(void)
417 {
418         mutex_lock(&rng_mutex);
419         BUG_ON(current_rng);
420         kfree(rng_buffer);
421         mutex_unlock(&rng_mutex);
422 }
423
424 module_exit(hwrng_exit);
425
426 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
427 MODULE_LICENSE("GPL");