Input: serio_raw - signal EFAULT even if read/write partially succeeds
[firefly-linux-kernel-4.4.55.git] / drivers / input / serio / serio_raw.c
index 4494233d331ac4689eef8ca58366c7138904627d..59df2e7317a3c8eec1c52e2a89f53a551a0e0224 100644 (file)
@@ -165,31 +165,38 @@ static ssize_t serio_raw_read(struct file *file, char __user *buffer,
        struct serio_raw *serio_raw = client->serio_raw;
        char uninitialized_var(c);
        ssize_t read = 0;
-       int retval;
+       int error;
 
-       if (serio_raw->dead)
-               return -ENODEV;
+       for (;;) {
+               if (serio_raw->dead)
+                       return -ENODEV;
 
-       if (serio_raw->head == serio_raw->tail && (file->f_flags & O_NONBLOCK))
-               return -EAGAIN;
+               if (serio_raw->head == serio_raw->tail &&
+                   (file->f_flags & O_NONBLOCK))
+                       return -EAGAIN;
 
-       retval = wait_event_interruptible(serio_raw->wait,
-                       serio_raw->head != serio_raw->tail || serio_raw->dead);
-       if (retval)
-               return retval;
+               if (count == 0)
+                       break;
 
-       if (serio_raw->dead)
-               return -ENODEV;
+               while (read < count && serio_raw_fetch_byte(serio_raw, &c)) {
+                       if (put_user(c, buffer++))
+                               return -EFAULT;
+                       read++;
+               }
 
-       while (read < count && serio_raw_fetch_byte(serio_raw, &c)) {
-               if (put_user(c, buffer++)) {
-                       retval = -EFAULT;
+               if (read)
                        break;
+
+               if (!(file->f_flags & O_NONBLOCK)) {
+                       error = wait_event_interruptible(serio_raw->wait,
+                                       serio_raw->head != serio_raw->tail ||
+                                       serio_raw->dead);
+                       if (error)
+                               return error;
                }
-               read++;
        }
 
-       return read ?: retval;
+       return read;
 }
 
 static ssize_t serio_raw_write(struct file *file, const char __user *buffer,
@@ -197,8 +204,7 @@ static ssize_t serio_raw_write(struct file *file, const char __user *buffer,
 {
        struct serio_raw_client *client = file->private_data;
        struct serio_raw *serio_raw = client->serio_raw;
-       ssize_t written = 0;
-       int retval;
+       int retval = 0;
        unsigned char c;
 
        retval = mutex_lock_interruptible(&serio_raw_mutex);
@@ -218,16 +224,20 @@ static ssize_t serio_raw_write(struct file *file, const char __user *buffer,
                        retval = -EFAULT;
                        goto out;
                }
+
                if (serio_write(serio_raw->serio, c)) {
-                       retval = -EIO;
+                       /* Either signal error or partial write */
+                       if (retval == 0)
+                               retval = -EIO;
                        goto out;
                }
-               written++;
+
+               retval++;
        }
 
 out:
        mutex_unlock(&serio_raw_mutex);
-       return written ?: retval;
+       return retval;
 }
 
 static unsigned int serio_raw_poll(struct file *file, poll_table *wait)
@@ -432,15 +442,4 @@ static struct serio_driver serio_raw_drv = {
        .manual_bind    = true,
 };
 
-static int __init serio_raw_init(void)
-{
-       return serio_register_driver(&serio_raw_drv);
-}
-
-static void __exit serio_raw_exit(void)
-{
-       serio_unregister_driver(&serio_raw_drv);
-}
-
-module_init(serio_raw_init);
-module_exit(serio_raw_exit);
+module_serio_driver(serio_raw_drv);