llseek: automatically add .llseek fop
[linux-2.6.git] / drivers / char / hw_random / core.c
index 26a860a..788da05 100644 (file)
@@ -37,6 +37,7 @@
 #include <linux/kernel.h>
 #include <linux/fs.h>
 #include <linux/sched.h>
+#include <linux/smp_lock.h>
 #include <linux/init.h>
 #include <linux/miscdevice.h>
 #include <linux/delay.h>
@@ -51,7 +52,9 @@
 static struct hwrng *current_rng;
 static LIST_HEAD(rng_list);
 static DEFINE_MUTEX(rng_mutex);
-
+static int data_avail;
+static u8 rng_buffer[SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES]
+       __cacheline_aligned;
 
 static inline int hwrng_init(struct hwrng *rng)
 {
@@ -66,19 +69,6 @@ static inline void hwrng_cleanup(struct hwrng *rng)
                rng->cleanup(rng);
 }
 
-static inline int hwrng_data_present(struct hwrng *rng)
-{
-       if (!rng->data_present)
-               return 1;
-       return rng->data_present(rng);
-}
-
-static inline int hwrng_data_read(struct hwrng *rng, u32 *data)
-{
-       return rng->data_read(rng, data);
-}
-
-
 static int rng_dev_open(struct inode *inode, struct file *filp)
 {
        /* enforce read-only access to this chrdev */
@@ -89,64 +79,90 @@ static int rng_dev_open(struct inode *inode, struct file *filp)
        return 0;
 }
 
+static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
+                       int wait) {
+       int present;
+
+       if (rng->read)
+               return rng->read(rng, (void *)buffer, size, wait);
+
+       if (rng->data_present)
+               present = rng->data_present(rng, wait);
+       else
+               present = 1;
+
+       if (present)
+               return rng->data_read(rng, (u32 *)buffer);
+
+       return 0;
+}
+
 static ssize_t rng_dev_read(struct file *filp, char __user *buf,
                            size_t size, loff_t *offp)
 {
-       u32 data;
        ssize_t ret = 0;
-       int i, err = 0;
-       int data_present;
-       int bytes_read;
+       int err = 0;
+       int bytes_read, len;
 
        while (size) {
-               err = -ERESTARTSYS;
-               if (mutex_lock_interruptible(&rng_mutex))
+               if (mutex_lock_interruptible(&rng_mutex)) {
+                       err = -ERESTARTSYS;
                        goto out;
+               }
+
                if (!current_rng) {
-                       mutex_unlock(&rng_mutex);
                        err = -ENODEV;
-                       goto out;
+                       goto out_unlock;
                }
-               if (filp->f_flags & O_NONBLOCK) {
-                       data_present = hwrng_data_present(current_rng);
-               } else {
-                       /* Some RNG require some time between data_reads to gather
-                        * new entropy. Poll it.
-                        */
-                       for (i = 0; i < 20; i++) {
-                               data_present = hwrng_data_present(current_rng);
-                               if (data_present)
-                                       break;
-                               udelay(10);
+
+               if (!data_avail) {
+                       bytes_read = rng_get_data(current_rng, rng_buffer,
+                               sizeof(rng_buffer),
+                               !(filp->f_flags & O_NONBLOCK));
+                       if (bytes_read < 0) {
+                               err = bytes_read;
+                               goto out_unlock;
                        }
+                       data_avail = bytes_read;
                }
-               bytes_read = 0;
-               if (data_present)
-                       bytes_read = hwrng_data_read(current_rng, &data);
-               mutex_unlock(&rng_mutex);
 
-               err = -EAGAIN;
-               if (!bytes_read && (filp->f_flags & O_NONBLOCK))
-                       goto out;
+               if (!data_avail) {
+                       if (filp->f_flags & O_NONBLOCK) {
+                               err = -EAGAIN;
+                               goto out_unlock;
+                       }
+               } else {
+                       len = data_avail;
+                       if (len > size)
+                               len = size;
 
-               err = -EFAULT;
-               while (bytes_read && size) {
-                       if (put_user((u8)data, buf++))
-                               goto out;
-                       size--;
-                       ret++;
-                       bytes_read--;
-                       data >>= 8;
+                       data_avail -= len;
+
+                       if (copy_to_user(buf + ret, rng_buffer + data_avail,
+                                                               len)) {
+                               err = -EFAULT;
+                               goto out_unlock;
+                       }
+
+                       size -= len;
+                       ret += len;
                }
 
+               mutex_unlock(&rng_mutex);
+
                if (need_resched())
                        schedule_timeout_interruptible(1);
-               err = -ERESTARTSYS;
-               if (signal_pending(current))
+
+               if (signal_pending(current)) {
+                       err = -ERESTARTSYS;
                        goto out;
+               }
        }
 out:
        return ret ? : err;
+out_unlock:
+       mutex_unlock(&rng_mutex);
+       goto out;
 }
 
 
@@ -154,11 +170,13 @@ static const struct file_operations rng_chrdev_ops = {
        .owner          = THIS_MODULE,
        .open           = rng_dev_open,
        .read           = rng_dev_read,
+       .llseek         = noop_llseek,
 };
 
 static struct miscdevice rng_miscdev = {
        .minor          = RNG_MISCDEV_MINOR,
        .name           = RNG_MODULE_NAME,
+       .nodename       = "hwrng",
        .fops           = &rng_chrdev_ops,
 };
 
@@ -285,7 +303,7 @@ int hwrng_register(struct hwrng *rng)
        struct hwrng *old_rng, *tmp;
 
        if (rng->name == NULL ||
-           rng->data_read == NULL)
+           (rng->data_read == NULL && rng->read == NULL))
                goto out;
 
        mutex_lock(&rng_mutex);