LCOV - code coverage report
Current view: top level - drivers/input - evdev.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 42 422 10.0 %
Date: 2014-04-16 Functions: 5 41 12.2 %
Branches: 8 378 2.1 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Event char devices, giving access to raw input device events.
       3                 :            :  *
       4                 :            :  * Copyright (c) 1999-2002 Vojtech Pavlik
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or modify it
       7                 :            :  * under the terms of the GNU General Public License version 2 as published by
       8                 :            :  * the Free Software Foundation.
       9                 :            :  */
      10                 :            : 
      11                 :            : #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
      12                 :            : 
      13                 :            : #define EVDEV_MINOR_BASE        64
      14                 :            : #define EVDEV_MINORS            32
      15                 :            : #define EVDEV_MIN_BUFFER_SIZE   64U
      16                 :            : #define EVDEV_BUF_PACKETS       8
      17                 :            : 
      18                 :            : #include <linux/poll.h>
      19                 :            : #include <linux/sched.h>
      20                 :            : #include <linux/slab.h>
      21                 :            : #include <linux/vmalloc.h>
      22                 :            : #include <linux/mm.h>
      23                 :            : #include <linux/module.h>
      24                 :            : #include <linux/init.h>
      25                 :            : #include <linux/input/mt.h>
      26                 :            : #include <linux/major.h>
      27                 :            : #include <linux/device.h>
      28                 :            : #include <linux/cdev.h>
      29                 :            : #include <linux/wakelock.h>
      30                 :            : #include "input-compat.h"
      31                 :            : 
      32                 :            : struct evdev {
      33                 :            :         int open;
      34                 :            :         struct input_handle handle;
      35                 :            :         wait_queue_head_t wait;
      36                 :            :         struct evdev_client __rcu *grab;
      37                 :            :         struct list_head client_list;
      38                 :            :         spinlock_t client_lock; /* protects client_list */
      39                 :            :         struct mutex mutex;
      40                 :            :         struct device dev;
      41                 :            :         struct cdev cdev;
      42                 :            :         bool exist;
      43                 :            : };
      44                 :            : 
      45                 :            : struct evdev_client {
      46                 :            :         unsigned int head;
      47                 :            :         unsigned int tail;
      48                 :            :         unsigned int packet_head; /* [future] position of the first element of next packet */
      49                 :            :         spinlock_t buffer_lock; /* protects access to buffer, head and tail */
      50                 :            :         struct wake_lock wake_lock;
      51                 :            :         bool use_wake_lock;
      52                 :            :         char name[28];
      53                 :            :         struct fasync_struct *fasync;
      54                 :            :         struct evdev *evdev;
      55                 :            :         struct list_head node;
      56                 :            :         int clkid;
      57                 :            :         bool revoked;
      58                 :            :         unsigned int bufsize;
      59                 :            :         struct input_event buffer[];
      60                 :            : };
      61                 :            : 
      62                 :            : /* flush queued events of type @type, caller must hold client->buffer_lock */
      63                 :          0 : static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
      64                 :            : {
      65                 :            :         unsigned int i, head, num;
      66                 :          0 :         unsigned int mask = client->bufsize - 1;
      67                 :            :         bool is_report;
      68                 :            :         struct input_event *ev;
      69                 :            : 
      70         [ #  # ]:          0 :         BUG_ON(type == EV_SYN);
      71                 :            : 
      72                 :          0 :         head = client->tail;
      73                 :          0 :         client->packet_head = client->tail;
      74                 :            : 
      75                 :            :         /* init to 1 so a leading SYN_REPORT will not be dropped */
      76                 :            :         num = 1;
      77                 :            : 
      78         [ #  # ]:          0 :         for (i = client->tail; i != client->head; i = (i + 1) & mask) {
      79                 :          0 :                 ev = &client->buffer[i];
      80                 :          0 :                 is_report = ev->type == EV_SYN && ev->code == SYN_REPORT;
      81                 :            : 
      82         [ #  # ]:          0 :                 if (ev->type == type) {
      83                 :            :                         /* drop matched entry */
      84                 :          0 :                         continue;
      85         [ #  # ]:          0 :                 } else if (is_report && !num) {
      86                 :            :                         /* drop empty SYN_REPORT groups */
      87                 :          0 :                         continue;
      88         [ #  # ]:          0 :                 } else if (head != i) {
      89                 :            :                         /* move entry to fill the gap */
      90                 :          0 :                         client->buffer[head].time = ev->time;
      91                 :          0 :                         client->buffer[head].type = ev->type;
      92                 :          0 :                         client->buffer[head].code = ev->code;
      93                 :          0 :                         client->buffer[head].value = ev->value;
      94                 :            :                 }
      95                 :            : 
      96                 :          0 :                 num++;
      97                 :          0 :                 head = (head + 1) & mask;
      98                 :            : 
      99         [ #  # ]:          0 :                 if (is_report) {
     100                 :            :                         num = 0;
     101                 :          0 :                         client->packet_head = head;
     102                 :            :                 }
     103                 :            :         }
     104                 :            : 
     105                 :          0 :         client->head = head;
     106                 :          0 : }
     107                 :            : 
     108                 :            : /* queue SYN_DROPPED event */
     109                 :          0 : static void evdev_queue_syn_dropped(struct evdev_client *client)
     110                 :            : {
     111                 :            :         unsigned long flags;
     112                 :            :         struct input_event ev;
     113                 :            :         ktime_t time;
     114                 :            : 
     115                 :          0 :         time = ktime_get();
     116         [ #  # ]:          0 :         if (client->clkid != CLOCK_MONOTONIC)
     117                 :          0 :                 time = ktime_sub(time, ktime_get_monotonic_offset());
     118                 :            : 
     119                 :          0 :         ev.time = ktime_to_timeval(time);
     120                 :            :         ev.type = EV_SYN;
     121                 :            :         ev.code = SYN_DROPPED;
     122                 :            :         ev.value = 0;
     123                 :            : 
     124                 :          0 :         spin_lock_irqsave(&client->buffer_lock, flags);
     125                 :            : 
     126                 :          0 :         client->buffer[client->head++] = ev;
     127                 :          0 :         client->head &= client->bufsize - 1;
     128                 :            : 
     129         [ #  # ]:          0 :         if (unlikely(client->head == client->tail)) {
     130                 :            :                 /* drop queue but keep our SYN_DROPPED event */
     131                 :          0 :                 client->tail = (client->head - 1) & (client->bufsize - 1);
     132                 :          0 :                 client->packet_head = client->tail;
     133                 :            :         }
     134                 :            : 
     135                 :            :         spin_unlock_irqrestore(&client->buffer_lock, flags);
     136                 :          0 : }
     137                 :            : 
     138                 :          0 : static void __pass_event(struct evdev_client *client,
     139                 :            :                          const struct input_event *event)
     140                 :            : {
     141                 :          0 :         client->buffer[client->head++] = *event;
     142                 :          0 :         client->head &= client->bufsize - 1;
     143                 :            : 
     144         [ #  # ]:          0 :         if (unlikely(client->head == client->tail)) {
     145                 :            :                 /*
     146                 :            :                  * This effectively "drops" all unconsumed events, leaving
     147                 :            :                  * EV_SYN/SYN_DROPPED plus the newest event in the queue.
     148                 :            :                  */
     149                 :          0 :                 client->tail = (client->head - 2) & (client->bufsize - 1);
     150                 :            : 
     151                 :          0 :                 client->buffer[client->tail].time = event->time;
     152                 :          0 :                 client->buffer[client->tail].type = EV_SYN;
     153                 :          0 :                 client->buffer[client->tail].code = SYN_DROPPED;
     154                 :          0 :                 client->buffer[client->tail].value = 0;
     155                 :            : 
     156                 :          0 :                 client->packet_head = client->tail;
     157         [ #  # ]:          0 :                 if (client->use_wake_lock)
     158                 :            :                         wake_unlock(&client->wake_lock);
     159                 :            :         }
     160                 :            : 
     161         [ #  # ]:          0 :         if (event->type == EV_SYN && event->code == SYN_REPORT) {
     162                 :          0 :                 client->packet_head = client->head;
     163         [ #  # ]:          0 :                 if (client->use_wake_lock)
     164                 :            :                         wake_lock(&client->wake_lock);
     165                 :          0 :                 kill_fasync(&client->fasync, SIGIO, POLL_IN);
     166                 :            :         }
     167                 :          0 : }
     168                 :            : 
     169                 :          0 : static void evdev_pass_values(struct evdev_client *client,
     170                 :            :                         const struct input_value *vals, unsigned int count,
     171                 :            :                         ktime_t mono, ktime_t real)
     172                 :            : {
     173                 :          0 :         struct evdev *evdev = client->evdev;
     174                 :            :         const struct input_value *v;
     175                 :            :         struct input_event event;
     176                 :            :         bool wakeup = false;
     177                 :            : 
     178         [ #  # ]:          0 :         if (client->revoked)
     179                 :          0 :                 return;
     180                 :            : 
     181         [ #  # ]:          0 :         event.time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ?
     182                 :            :                                       mono : real);
     183                 :            : 
     184                 :            :         /* Interrupts are disabled, just acquire the lock. */
     185                 :            :         spin_lock(&client->buffer_lock);
     186                 :            : 
     187         [ #  # ]:          0 :         for (v = vals; v != vals + count; v++) {
     188                 :          0 :                 event.type = v->type;
     189                 :          0 :                 event.code = v->code;
     190                 :          0 :                 event.value = v->value;
     191                 :          0 :                 __pass_event(client, &event);
     192         [ #  # ]:          0 :                 if (v->type == EV_SYN && v->code == SYN_REPORT)
     193                 :            :                         wakeup = true;
     194                 :            :         }
     195                 :            : 
     196                 :            :         spin_unlock(&client->buffer_lock);
     197                 :            : 
     198         [ #  # ]:          0 :         if (wakeup)
     199                 :          0 :                 wake_up_interruptible(&evdev->wait);
     200                 :            : }
     201                 :            : 
     202                 :            : /*
     203                 :            :  * Pass incoming events to all connected clients.
     204                 :            :  */
     205                 :          0 : static void evdev_events(struct input_handle *handle,
     206                 :            :                          const struct input_value *vals, unsigned int count)
     207                 :            : {
     208                 :          0 :         struct evdev *evdev = handle->private;
     209                 :            :         struct evdev_client *client;
     210                 :            :         ktime_t time_mono, time_real;
     211                 :            : 
     212                 :          0 :         time_mono = ktime_get();
     213                 :          0 :         time_real = ktime_sub(time_mono, ktime_get_monotonic_offset());
     214                 :            : 
     215                 :            :         rcu_read_lock();
     216                 :            : 
     217                 :          0 :         client = rcu_dereference(evdev->grab);
     218                 :            : 
     219         [ #  # ]:          0 :         if (client)
     220                 :          0 :                 evdev_pass_values(client, vals, count, time_mono, time_real);
     221                 :            :         else
     222         [ #  # ]:          0 :                 list_for_each_entry_rcu(client, &evdev->client_list, node)
     223                 :          0 :                         evdev_pass_values(client, vals, count,
     224                 :            :                                           time_mono, time_real);
     225                 :            : 
     226                 :            :         rcu_read_unlock();
     227                 :          0 : }
     228                 :            : 
     229                 :            : /*
     230                 :            :  * Pass incoming event to all connected clients.
     231                 :            :  */
     232                 :          0 : static void evdev_event(struct input_handle *handle,
     233                 :            :                         unsigned int type, unsigned int code, int value)
     234                 :            : {
     235                 :          0 :         struct input_value vals[] = { { type, code, value } };
     236                 :            : 
     237                 :          0 :         evdev_events(handle, vals, 1);
     238                 :          0 : }
     239                 :            : 
     240                 :          0 : static int evdev_fasync(int fd, struct file *file, int on)
     241                 :            : {
     242                 :          0 :         struct evdev_client *client = file->private_data;
     243                 :            : 
     244                 :          0 :         return fasync_helper(fd, file, on, &client->fasync);
     245                 :            : }
     246                 :            : 
     247                 :          0 : static int evdev_flush(struct file *file, fl_owner_t id)
     248                 :            : {
     249                 :          0 :         struct evdev_client *client = file->private_data;
     250                 :          0 :         struct evdev *evdev = client->evdev;
     251                 :            :         int retval;
     252                 :            : 
     253                 :          0 :         retval = mutex_lock_interruptible(&evdev->mutex);
     254         [ #  # ]:          0 :         if (retval)
     255                 :            :                 return retval;
     256                 :            : 
     257 [ #  # ][ #  # ]:          0 :         if (!evdev->exist || client->revoked)
     258                 :            :                 retval = -ENODEV;
     259                 :            :         else
     260                 :          0 :                 retval = input_flush_device(&evdev->handle, file);
     261                 :            : 
     262                 :          0 :         mutex_unlock(&evdev->mutex);
     263                 :          0 :         return retval;
     264                 :            : }
     265                 :            : 
     266                 :          0 : static void evdev_free(struct device *dev)
     267                 :            : {
     268                 :          2 :         struct evdev *evdev = container_of(dev, struct evdev, dev);
     269                 :            : 
     270                 :          2 :         input_put_device(evdev->handle.dev);
     271                 :          2 :         kfree(evdev);
     272                 :          2 : }
     273                 :            : 
     274                 :            : /*
     275                 :            :  * Grabs an event device (along with underlying input device).
     276                 :            :  * This function is called with evdev->mutex taken.
     277                 :            :  */
     278                 :            : static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
     279                 :            : {
     280                 :            :         int error;
     281                 :            : 
     282         [ #  # ]:          0 :         if (evdev->grab)
     283                 :            :                 return -EBUSY;
     284                 :            : 
     285                 :          0 :         error = input_grab_device(&evdev->handle);
     286         [ #  # ]:          0 :         if (error)
     287                 :            :                 return error;
     288                 :            : 
     289                 :          0 :         rcu_assign_pointer(evdev->grab, client);
     290                 :            : 
     291                 :            :         return 0;
     292                 :            : }
     293                 :            : 
     294                 :          0 : static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
     295                 :            : {
     296                 :          0 :         struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
     297                 :            :                                         lockdep_is_held(&evdev->mutex));
     298                 :            : 
     299 [ #  # ][ #  # ]:          0 :         if (grab != client)
     300                 :            :                 return  -EINVAL;
     301                 :            : 
     302                 :          0 :         rcu_assign_pointer(evdev->grab, NULL);
     303                 :            :         synchronize_rcu();
     304                 :          0 :         input_release_device(&evdev->handle);
     305                 :            : 
     306                 :          0 :         return 0;
     307                 :            : }
     308                 :            : 
     309                 :          0 : static void evdev_attach_client(struct evdev *evdev,
     310                 :            :                                 struct evdev_client *client)
     311                 :            : {
     312                 :            :         spin_lock(&evdev->client_lock);
     313                 :          0 :         list_add_tail_rcu(&client->node, &evdev->client_list);
     314                 :            :         spin_unlock(&evdev->client_lock);
     315                 :          0 : }
     316                 :            : 
     317                 :          0 : static void evdev_detach_client(struct evdev *evdev,
     318                 :            :                                 struct evdev_client *client)
     319                 :            : {
     320                 :            :         spin_lock(&evdev->client_lock);
     321                 :            :         list_del_rcu(&client->node);
     322                 :            :         spin_unlock(&evdev->client_lock);
     323                 :            :         synchronize_rcu();
     324                 :          0 : }
     325                 :            : 
     326                 :          0 : static int evdev_open_device(struct evdev *evdev)
     327                 :            : {
     328                 :            :         int retval;
     329                 :            : 
     330                 :          0 :         retval = mutex_lock_interruptible(&evdev->mutex);
     331         [ #  # ]:          0 :         if (retval)
     332                 :            :                 return retval;
     333                 :            : 
     334         [ #  # ]:          0 :         if (!evdev->exist)
     335                 :            :                 retval = -ENODEV;
     336         [ #  # ]:          0 :         else if (!evdev->open++) {
     337                 :          0 :                 retval = input_open_device(&evdev->handle);
     338         [ #  # ]:          0 :                 if (retval)
     339                 :          0 :                         evdev->open--;
     340                 :            :         }
     341                 :            : 
     342                 :          0 :         mutex_unlock(&evdev->mutex);
     343                 :          0 :         return retval;
     344                 :            : }
     345                 :            : 
     346                 :          0 : static void evdev_close_device(struct evdev *evdev)
     347                 :            : {
     348                 :          0 :         mutex_lock(&evdev->mutex);
     349                 :            : 
     350 [ #  # ][ #  # ]:          0 :         if (evdev->exist && !--evdev->open)
     351                 :          0 :                 input_close_device(&evdev->handle);
     352                 :            : 
     353                 :          0 :         mutex_unlock(&evdev->mutex);
     354                 :          0 : }
     355                 :            : 
     356                 :            : /*
     357                 :            :  * Wake up users waiting for IO so they can disconnect from
     358                 :            :  * dead device.
     359                 :            :  */
     360                 :          0 : static void evdev_hangup(struct evdev *evdev)
     361                 :            : {
     362                 :            :         struct evdev_client *client;
     363                 :            : 
     364                 :            :         spin_lock(&evdev->client_lock);
     365         [ -  + ]:          4 :         list_for_each_entry(client, &evdev->client_list, node)
     366                 :          0 :                 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
     367                 :            :         spin_unlock(&evdev->client_lock);
     368                 :            : 
     369                 :          2 :         wake_up_interruptible(&evdev->wait);
     370                 :          2 : }
     371                 :            : 
     372                 :          0 : static int evdev_release(struct inode *inode, struct file *file)
     373                 :            : {
     374                 :          0 :         struct evdev_client *client = file->private_data;
     375                 :          0 :         struct evdev *evdev = client->evdev;
     376                 :            : 
     377                 :          0 :         mutex_lock(&evdev->mutex);
     378                 :          0 :         evdev_ungrab(evdev, client);
     379                 :          0 :         mutex_unlock(&evdev->mutex);
     380                 :            : 
     381                 :          0 :         evdev_detach_client(evdev, client);
     382                 :            : 
     383         [ #  # ]:          0 :         if (client->use_wake_lock)
     384                 :            :                 wake_lock_destroy(&client->wake_lock);
     385                 :            : 
     386         [ #  # ]:          0 :         if (is_vmalloc_addr(client))
     387                 :          0 :                 vfree(client);
     388                 :            :         else
     389                 :          0 :                 kfree(client);
     390                 :          0 :         kfree(client);
     391                 :            : 
     392                 :          0 :         evdev_close_device(evdev);
     393                 :            : 
     394                 :          0 :         return 0;
     395                 :            : }
     396                 :            : 
     397                 :          0 : static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
     398                 :            : {
     399                 :            :         unsigned int n_events =
     400                 :          0 :                 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
     401                 :            :                     EVDEV_MIN_BUFFER_SIZE);
     402                 :            : 
     403 [ #  # ][ #  # ]:          0 :         return roundup_pow_of_two(n_events);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     404                 :            : }
     405                 :            : 
     406                 :          0 : static int evdev_open(struct inode *inode, struct file *file)
     407                 :            : {
     408                 :          0 :         struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
     409                 :          0 :         unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
     410                 :          0 :         unsigned int size = sizeof(struct evdev_client) +
     411                 :          0 :                                         bufsize * sizeof(struct input_event);
     412                 :            :         struct evdev_client *client;
     413                 :            :         int error;
     414                 :            : 
     415                 :            :         client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
     416         [ #  # ]:          0 :         if (!client)
     417                 :          0 :                 client = vzalloc(size);
     418         [ #  # ]:          0 :         if (!client)
     419                 :            :                 return -ENOMEM;
     420                 :            : 
     421                 :          0 :         client->bufsize = bufsize;
     422                 :          0 :         spin_lock_init(&client->buffer_lock);
     423                 :          0 :         snprintf(client->name, sizeof(client->name), "%s-%d",
     424                 :            :                         dev_name(&evdev->dev), task_tgid_vnr(current));
     425                 :          0 :         client->evdev = evdev;
     426                 :          0 :         evdev_attach_client(evdev, client);
     427                 :            : 
     428                 :          0 :         error = evdev_open_device(evdev);
     429         [ #  # ]:          0 :         if (error)
     430                 :            :                 goto err_free_client;
     431                 :            : 
     432                 :          0 :         file->private_data = client;
     433                 :          0 :         nonseekable_open(inode, file);
     434                 :            : 
     435                 :          0 :         return 0;
     436                 :            : 
     437                 :            :  err_free_client:
     438                 :          0 :         evdev_detach_client(evdev, client);
     439                 :          0 :         kfree(client);
     440                 :          0 :         return error;
     441                 :            : }
     442                 :            : 
     443                 :          0 : static ssize_t evdev_write(struct file *file, const char __user *buffer,
     444                 :            :                            size_t count, loff_t *ppos)
     445                 :            : {
     446                 :          0 :         struct evdev_client *client = file->private_data;
     447                 :          0 :         struct evdev *evdev = client->evdev;
     448                 :            :         struct input_event event;
     449                 :            :         int retval = 0;
     450                 :            : 
     451 [ #  # ][ #  # ]:          0 :         if (count != 0 && count < input_event_size())
     452                 :            :                 return -EINVAL;
     453                 :            : 
     454                 :          0 :         retval = mutex_lock_interruptible(&evdev->mutex);
     455         [ #  # ]:          0 :         if (retval)
     456                 :            :                 return retval;
     457                 :            : 
     458 [ #  # ][ #  # ]:          0 :         if (!evdev->exist || client->revoked) {
     459                 :            :                 retval = -ENODEV;
     460                 :            :                 goto out;
     461                 :            :         }
     462                 :            : 
     463         [ #  # ]:          0 :         while (retval + input_event_size() <= count) {
     464                 :            : 
     465         [ #  # ]:          0 :                 if (input_event_from_user(buffer + retval, &event)) {
     466                 :            :                         retval = -EFAULT;
     467                 :            :                         goto out;
     468                 :            :                 }
     469                 :          0 :                 retval += input_event_size();
     470                 :            : 
     471                 :          0 :                 input_inject_event(&evdev->handle,
     472                 :          0 :                                    event.type, event.code, event.value);
     473                 :            :         }
     474                 :            : 
     475                 :            :  out:
     476                 :          0 :         mutex_unlock(&evdev->mutex);
     477                 :          0 :         return retval;
     478                 :            : }
     479                 :            : 
     480                 :          0 : static int evdev_fetch_next_event(struct evdev_client *client,
     481                 :            :                                   struct input_event *event)
     482                 :            : {
     483                 :            :         int have_event;
     484                 :            : 
     485                 :            :         spin_lock_irq(&client->buffer_lock);
     486                 :            : 
     487                 :          0 :         have_event = client->packet_head != client->tail;
     488         [ #  # ]:          0 :         if (have_event) {
     489                 :          0 :                 *event = client->buffer[client->tail++];
     490                 :          0 :                 client->tail &= client->bufsize - 1;
     491 [ #  # ][ #  # ]:          0 :                 if (client->use_wake_lock &&
     492                 :          0 :                     client->packet_head == client->tail)
     493                 :            :                         wake_unlock(&client->wake_lock);
     494                 :            :         }
     495                 :            : 
     496                 :            :         spin_unlock_irq(&client->buffer_lock);
     497                 :            : 
     498                 :          0 :         return have_event;
     499                 :            : }
     500                 :            : 
     501                 :          0 : static ssize_t evdev_read(struct file *file, char __user *buffer,
     502                 :            :                           size_t count, loff_t *ppos)
     503                 :            : {
     504                 :          0 :         struct evdev_client *client = file->private_data;
     505                 :          0 :         struct evdev *evdev = client->evdev;
     506                 :            :         struct input_event event;
     507                 :            :         size_t read = 0;
     508                 :            :         int error;
     509                 :            : 
     510 [ #  # ][ #  # ]:          0 :         if (count != 0 && count < input_event_size())
     511                 :            :                 return -EINVAL;
     512                 :            : 
     513                 :            :         for (;;) {
     514 [ #  # ][ #  # ]:          0 :                 if (!evdev->exist || client->revoked)
     515                 :            :                         return -ENODEV;
     516                 :            : 
     517 [ #  # ][ #  # ]:          0 :                 if (client->packet_head == client->tail &&
     518                 :          0 :                     (file->f_flags & O_NONBLOCK))
     519                 :            :                         return -EAGAIN;
     520                 :            : 
     521                 :            :                 /*
     522                 :            :                  * count == 0 is special - no IO is done but we check
     523                 :            :                  * for error conditions (see above).
     524                 :            :                  */
     525         [ #  # ]:          0 :                 if (count == 0)
     526                 :            :                         break;
     527                 :            : 
     528   [ #  #  #  # ]:          0 :                 while (read + input_event_size() <= count &&
     529                 :          0 :                        evdev_fetch_next_event(client, &event)) {
     530                 :            : 
     531         [ #  # ]:          0 :                         if (input_event_to_user(buffer + read, &event))
     532                 :            :                                 return -EFAULT;
     533                 :            : 
     534                 :            :                         read += input_event_size();
     535                 :            :                 }
     536                 :            : 
     537         [ #  # ]:          0 :                 if (read)
     538                 :            :                         break;
     539                 :            : 
     540         [ #  # ]:          0 :                 if (!(file->f_flags & O_NONBLOCK)) {
     541 [ #  # ][ #  # ]:          0 :                         error = wait_event_interruptible(evdev->wait,
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     542                 :            :                                         client->packet_head != client->tail ||
     543                 :            :                                         !evdev->exist || client->revoked);
     544         [ #  # ]:          0 :                         if (error)
     545                 :            :                                 return error;
     546                 :            :                 }
     547                 :            :         }
     548                 :            : 
     549                 :          0 :         return read;
     550                 :            : }
     551                 :            : 
     552                 :            : /* No kernel lock - fine */
     553                 :          0 : static unsigned int evdev_poll(struct file *file, poll_table *wait)
     554                 :            : {
     555                 :          0 :         struct evdev_client *client = file->private_data;
     556                 :          0 :         struct evdev *evdev = client->evdev;
     557                 :            :         unsigned int mask;
     558                 :            : 
     559                 :          0 :         poll_wait(file, &evdev->wait, wait);
     560                 :            : 
     561 [ #  # ][ #  # ]:          0 :         if (evdev->exist && !client->revoked)
     562                 :            :                 mask = POLLOUT | POLLWRNORM;
     563                 :            :         else
     564                 :            :                 mask = POLLHUP | POLLERR;
     565                 :            : 
     566         [ #  # ]:          0 :         if (client->packet_head != client->tail)
     567                 :          0 :                 mask |= POLLIN | POLLRDNORM;
     568                 :            : 
     569                 :          0 :         return mask;
     570                 :            : }
     571                 :            : 
     572                 :            : #ifdef CONFIG_COMPAT
     573                 :            : 
     574                 :            : #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
     575                 :            : #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
     576                 :            : 
     577                 :            : #ifdef __BIG_ENDIAN
     578                 :            : static int bits_to_user(unsigned long *bits, unsigned int maxbit,
     579                 :            :                         unsigned int maxlen, void __user *p, int compat)
     580                 :            : {
     581                 :            :         int len, i;
     582                 :            : 
     583                 :            :         if (compat) {
     584                 :            :                 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
     585                 :            :                 if (len > maxlen)
     586                 :            :                         len = maxlen;
     587                 :            : 
     588                 :            :                 for (i = 0; i < len / sizeof(compat_long_t); i++)
     589                 :            :                         if (copy_to_user((compat_long_t __user *) p + i,
     590                 :            :                                          (compat_long_t *) bits +
     591                 :            :                                                 i + 1 - ((i % 2) << 1),
     592                 :            :                                          sizeof(compat_long_t)))
     593                 :            :                                 return -EFAULT;
     594                 :            :         } else {
     595                 :            :                 len = BITS_TO_LONGS(maxbit) * sizeof(long);
     596                 :            :                 if (len > maxlen)
     597                 :            :                         len = maxlen;
     598                 :            : 
     599                 :            :                 if (copy_to_user(p, bits, len))
     600                 :            :                         return -EFAULT;
     601                 :            :         }
     602                 :            : 
     603                 :            :         return len;
     604                 :            : }
     605                 :            : #else
     606                 :            : static int bits_to_user(unsigned long *bits, unsigned int maxbit,
     607                 :            :                         unsigned int maxlen, void __user *p, int compat)
     608                 :            : {
     609                 :            :         int len = compat ?
     610                 :            :                         BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
     611                 :            :                         BITS_TO_LONGS(maxbit) * sizeof(long);
     612                 :            : 
     613                 :            :         if (len > maxlen)
     614                 :            :                 len = maxlen;
     615                 :            : 
     616                 :            :         return copy_to_user(p, bits, len) ? -EFAULT : len;
     617                 :            : }
     618                 :            : #endif /* __BIG_ENDIAN */
     619                 :            : 
     620                 :            : #else
     621                 :            : 
     622                 :          0 : static int bits_to_user(unsigned long *bits, unsigned int maxbit,
     623                 :            :                         unsigned int maxlen, void __user *p, int compat)
     624                 :            : {
     625                 :          0 :         int len = BITS_TO_LONGS(maxbit) * sizeof(long);
     626                 :            : 
     627         [ #  # ]:          0 :         if (len > maxlen)
     628                 :          0 :                 len = maxlen;
     629                 :            : 
     630         [ #  # ]:          0 :         return copy_to_user(p, bits, len) ? -EFAULT : len;
     631                 :            : }
     632                 :            : 
     633                 :            : #endif /* CONFIG_COMPAT */
     634                 :            : 
     635                 :          0 : static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
     636                 :            : {
     637                 :            :         int len;
     638                 :            : 
     639         [ #  # ]:          0 :         if (!str)
     640                 :            :                 return -ENOENT;
     641                 :            : 
     642                 :          0 :         len = strlen(str) + 1;
     643         [ #  # ]:          0 :         if (len > maxlen)
     644                 :          0 :                 len = maxlen;
     645                 :            : 
     646         [ #  # ]:          0 :         return copy_to_user(p, str, len) ? -EFAULT : len;
     647                 :            : }
     648                 :            : 
     649                 :            : #define OLD_KEY_MAX     0x1ff
     650                 :          0 : static int handle_eviocgbit(struct input_dev *dev,
     651                 :            :                             unsigned int type, unsigned int size,
     652                 :            :                             void __user *p, int compat_mode)
     653                 :            : {
     654                 :            :         static unsigned long keymax_warn_time;
     655                 :            :         unsigned long *bits;
     656                 :            :         int len;
     657                 :            : 
     658   [ #  #  #  #  :          0 :         switch (type) {
          #  #  #  #  #  
                      # ]
     659                 :            : 
     660                 :          0 :         case      0: bits = dev->evbit;  len = EV_MAX;  break;
     661                 :          0 :         case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
     662                 :          0 :         case EV_REL: bits = dev->relbit; len = REL_MAX; break;
     663                 :          0 :         case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
     664                 :          0 :         case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
     665                 :          0 :         case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
     666                 :          0 :         case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
     667                 :          0 :         case EV_FF:  bits = dev->ffbit;  len = FF_MAX;  break;
     668                 :          0 :         case EV_SW:  bits = dev->swbit;  len = SW_MAX;  break;
     669                 :            :         default: return -EINVAL;
     670                 :            :         }
     671                 :            : 
     672                 :            :         /*
     673                 :            :          * Work around bugs in userspace programs that like to do
     674                 :            :          * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
     675                 :            :          * should be in bytes, not in bits.
     676                 :            :          */
     677         [ #  # ]:          0 :         if (type == EV_KEY && size == OLD_KEY_MAX) {
     678                 :            :                 len = OLD_KEY_MAX;
     679         [ #  # ]:          0 :                 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
     680                 :          0 :                         pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
     681                 :            :                                    "limiting output to %zu bytes. See "
     682                 :            :                                    "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
     683                 :            :                                    OLD_KEY_MAX,
     684                 :            :                                    BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
     685                 :            :         }
     686                 :            : 
     687                 :          0 :         return bits_to_user(bits, len, size, p, compat_mode);
     688                 :            : }
     689                 :            : #undef OLD_KEY_MAX
     690                 :            : 
     691                 :          0 : static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
     692                 :            : {
     693                 :          0 :         struct input_keymap_entry ke = {
     694                 :            :                 .len    = sizeof(unsigned int),
     695                 :            :                 .flags  = 0,
     696                 :            :         };
     697                 :            :         int __user *ip = (int __user *)p;
     698                 :            :         int error;
     699                 :            : 
     700                 :            :         /* legacy case */
     701         [ #  # ]:          0 :         if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
     702                 :            :                 return -EFAULT;
     703                 :            : 
     704                 :          0 :         error = input_get_keycode(dev, &ke);
     705         [ #  # ]:          0 :         if (error)
     706                 :            :                 return error;
     707                 :            : 
     708         [ #  # ]:          0 :         if (put_user(ke.keycode, ip + 1))
     709                 :            :                 return -EFAULT;
     710                 :            : 
     711                 :          0 :         return 0;
     712                 :            : }
     713                 :            : 
     714                 :          0 : static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
     715                 :            : {
     716                 :            :         struct input_keymap_entry ke;
     717                 :            :         int error;
     718                 :            : 
     719         [ #  # ]:          0 :         if (copy_from_user(&ke, p, sizeof(ke)))
     720                 :            :                 return -EFAULT;
     721                 :            : 
     722                 :          0 :         error = input_get_keycode(dev, &ke);
     723         [ #  # ]:          0 :         if (error)
     724                 :            :                 return error;
     725                 :            : 
     726         [ #  # ]:          0 :         if (copy_to_user(p, &ke, sizeof(ke)))
     727                 :            :                 return -EFAULT;
     728                 :            : 
     729                 :          0 :         return 0;
     730                 :            : }
     731                 :            : 
     732                 :          0 : static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
     733                 :            : {
     734                 :          0 :         struct input_keymap_entry ke = {
     735                 :            :                 .len    = sizeof(unsigned int),
     736                 :            :                 .flags  = 0,
     737                 :            :         };
     738                 :            :         int __user *ip = (int __user *)p;
     739                 :            : 
     740         [ #  # ]:          0 :         if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
     741                 :            :                 return -EFAULT;
     742                 :            : 
     743         [ #  # ]:          0 :         if (get_user(ke.keycode, ip + 1))
     744                 :            :                 return -EFAULT;
     745                 :            : 
     746                 :          0 :         return input_set_keycode(dev, &ke);
     747                 :            : }
     748                 :            : 
     749                 :          0 : static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
     750                 :            : {
     751                 :            :         struct input_keymap_entry ke;
     752                 :            : 
     753         [ #  # ]:          0 :         if (copy_from_user(&ke, p, sizeof(ke)))
     754                 :            :                 return -EFAULT;
     755                 :            : 
     756         [ #  # ]:          0 :         if (ke.len > sizeof(ke.scancode))
     757                 :            :                 return -EINVAL;
     758                 :            : 
     759                 :          0 :         return input_set_keycode(dev, &ke);
     760                 :            : }
     761                 :            : 
     762                 :            : /*
     763                 :            :  * If we transfer state to the user, we should flush all pending events
     764                 :            :  * of the same type from the client's queue. Otherwise, they might end up
     765                 :            :  * with duplicate events, which can screw up client's state tracking.
     766                 :            :  * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
     767                 :            :  * event so user-space will notice missing events.
     768                 :            :  *
     769                 :            :  * LOCKING:
     770                 :            :  * We need to take event_lock before buffer_lock to avoid dead-locks. But we
     771                 :            :  * need the even_lock only to guarantee consistent state. We can safely release
     772                 :            :  * it while flushing the queue. This allows input-core to handle filters while
     773                 :            :  * we flush the queue.
     774                 :            :  */
     775                 :          0 : static int evdev_handle_get_val(struct evdev_client *client,
     776                 :            :                                 struct input_dev *dev, unsigned int type,
     777                 :            :                                 unsigned long *bits, unsigned int max,
     778                 :            :                                 unsigned int size, void __user *p, int compat)
     779                 :            : {
     780                 :            :         int ret;
     781                 :            :         unsigned long *mem;
     782                 :            : 
     783                 :          0 :         mem = kmalloc(sizeof(unsigned long) * max, GFP_KERNEL);
     784         [ #  # ]:          0 :         if (!mem)
     785                 :            :                 return -ENOMEM;
     786                 :            : 
     787                 :            :         spin_lock_irq(&dev->event_lock);
     788                 :            :         spin_lock(&client->buffer_lock);
     789                 :            : 
     790                 :          0 :         memcpy(mem, bits, sizeof(unsigned long) * max);
     791                 :            : 
     792                 :            :         spin_unlock(&dev->event_lock);
     793                 :            : 
     794                 :          0 :         __evdev_flush_queue(client, type);
     795                 :            : 
     796                 :            :         spin_unlock_irq(&client->buffer_lock);
     797                 :            : 
     798                 :          0 :         ret = bits_to_user(mem, max, size, p, compat);
     799         [ #  # ]:          0 :         if (ret < 0)
     800                 :          0 :                 evdev_queue_syn_dropped(client);
     801                 :            : 
     802                 :          0 :         kfree(mem);
     803                 :            : 
     804                 :            :         return ret;
     805                 :            : }
     806                 :            : 
     807                 :          0 : static int evdev_handle_mt_request(struct input_dev *dev,
     808                 :            :                                    unsigned int size,
     809                 :            :                                    int __user *ip)
     810                 :            : {
     811                 :          0 :         const struct input_mt *mt = dev->mt;
     812                 :            :         unsigned int code;
     813                 :            :         int max_slots;
     814                 :            :         int i;
     815                 :            : 
     816         [ #  # ]:          0 :         if (get_user(code, &ip[0]))
     817                 :            :                 return -EFAULT;
     818 [ #  # ][ #  # ]:          0 :         if (!mt || !input_is_mt_value(code))
     819                 :            :                 return -EINVAL;
     820                 :            : 
     821                 :          0 :         max_slots = (size - sizeof(__u32)) / sizeof(__s32);
     822 [ #  # ][ #  # ]:          0 :         for (i = 0; i < mt->num_slots && i < max_slots; i++) {
     823                 :          0 :                 int value = input_mt_get_value(&mt->slots[i], code);
     824         [ #  # ]:          0 :                 if (put_user(value, &ip[1 + i]))
     825                 :            :                         return -EFAULT;
     826                 :            :         }
     827                 :            : 
     828                 :            :         return 0;
     829                 :            : }
     830                 :            : 
     831                 :            : /*
     832                 :            :  * HACK: disable conflicting EVIOCREVOKE until Android userspace stops using
     833                 :            :  * EVIOCSSUSPENDBLOCK
     834                 :            :  */
     835                 :            : /*
     836                 :            : static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
     837                 :            :                         struct file *file)
     838                 :            : {
     839                 :            :         client->revoked = true;
     840                 :            :         evdev_ungrab(evdev, client);
     841                 :            :         input_flush_device(&evdev->handle, file);
     842                 :            :         wake_up_interruptible(&evdev->wait);
     843                 :            : 
     844                 :            :         return 0;
     845                 :            : }
     846                 :            : */
     847                 :            : 
     848                 :          0 : static int evdev_enable_suspend_block(struct evdev *evdev,
     849                 :            :                                       struct evdev_client *client)
     850                 :            : {
     851         [ #  # ]:          0 :         if (client->use_wake_lock)
     852                 :            :                 return 0;
     853                 :            : 
     854                 :            :         spin_lock_irq(&client->buffer_lock);
     855                 :          0 :         wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
     856                 :          0 :         client->use_wake_lock = true;
     857         [ #  # ]:          0 :         if (client->packet_head != client->tail)
     858                 :            :                 wake_lock(&client->wake_lock);
     859                 :            :         spin_unlock_irq(&client->buffer_lock);
     860                 :            :         return 0;
     861                 :            : }
     862                 :            : 
     863                 :          0 : static int evdev_disable_suspend_block(struct evdev *evdev,
     864                 :            :                                        struct evdev_client *client)
     865                 :            : {
     866         [ #  # ]:          0 :         if (!client->use_wake_lock)
     867                 :            :                 return 0;
     868                 :            : 
     869                 :            :         spin_lock_irq(&client->buffer_lock);
     870                 :          0 :         client->use_wake_lock = false;
     871                 :            :         wake_lock_destroy(&client->wake_lock);
     872                 :            :         spin_unlock_irq(&client->buffer_lock);
     873                 :            : 
     874                 :            :         return 0;
     875                 :            : }
     876                 :            : 
     877                 :          0 : static long evdev_do_ioctl(struct file *file, unsigned int cmd,
     878                 :            :                            void __user *p, int compat_mode)
     879                 :            : {
     880                 :          0 :         struct evdev_client *client = file->private_data;
     881                 :          0 :         struct evdev *evdev = client->evdev;
     882                 :          0 :         struct input_dev *dev = evdev->handle.dev;
     883                 :            :         struct input_absinfo abs;
     884                 :            :         struct ff_effect effect;
     885                 :            :         int __user *ip = (int __user *)p;
     886                 :            :         unsigned int i, t, u, v;
     887                 :            :         unsigned int size;
     888                 :            :         int error;
     889                 :            : 
     890                 :            :         /* First we check for fixed-length commands */
     891   [ #  #  #  #  :          0 :         switch (cmd) {
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     892                 :            : 
     893                 :            :         case EVIOCGVERSION:
     894                 :          0 :                 return put_user(EV_VERSION, ip);
     895                 :            : 
     896                 :            :         case EVIOCGID:
     897         [ #  # ]:          0 :                 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
     898                 :            :                         return -EFAULT;
     899                 :            :                 return 0;
     900                 :            : 
     901                 :            :         case EVIOCGREP:
     902         [ #  # ]:          0 :                 if (!test_bit(EV_REP, dev->evbit))
     903                 :            :                         return -ENOSYS;
     904         [ #  # ]:          0 :                 if (put_user(dev->rep[REP_DELAY], ip))
     905                 :            :                         return -EFAULT;
     906         [ #  # ]:          0 :                 if (put_user(dev->rep[REP_PERIOD], ip + 1))
     907                 :            :                         return -EFAULT;
     908                 :            :                 return 0;
     909                 :            : 
     910                 :            :         case EVIOCSREP:
     911         [ #  # ]:          0 :                 if (!test_bit(EV_REP, dev->evbit))
     912                 :            :                         return -ENOSYS;
     913         [ #  # ]:          0 :                 if (get_user(u, ip))
     914                 :            :                         return -EFAULT;
     915         [ #  # ]:          0 :                 if (get_user(v, ip + 1))
     916                 :            :                         return -EFAULT;
     917                 :            : 
     918                 :          0 :                 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
     919                 :          0 :                 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
     920                 :            : 
     921                 :            :                 return 0;
     922                 :            : 
     923                 :            :         case EVIOCRMFF:
     924                 :          0 :                 return input_ff_erase(dev, (int)(unsigned long) p, file);
     925                 :            : 
     926                 :            :         case EVIOCGEFFECTS:
     927         [ #  # ]:          0 :                 i = test_bit(EV_FF, dev->evbit) ?
     928                 :          0 :                                 dev->ff->max_effects : 0;
     929         [ #  # ]:          0 :                 if (put_user(i, ip))
     930                 :            :                         return -EFAULT;
     931                 :            :                 return 0;
     932                 :            : 
     933                 :            :         case EVIOCGRAB:
     934         [ #  # ]:          0 :                 if (p)
     935                 :            :                         return evdev_grab(evdev, client);
     936                 :            :                 else
     937                 :            :                         return evdev_ungrab(evdev, client);
     938                 :            : 
     939                 :            :         /*
     940                 :            :          * HACK: disable conflicting EVIOCREVOKE until Android userspace stops
     941                 :            :          * using EVIOCSSUSPENDBLOCK
     942                 :            :          */
     943                 :            :         /*
     944                 :            :         case EVIOCREVOKE:
     945                 :            :                 if (p)
     946                 :            :                         return -EINVAL;
     947                 :            :                 else
     948                 :            :                         return evdev_revoke(evdev, client, file);
     949                 :            :         */
     950                 :            :         case EVIOCSCLOCKID:
     951         [ #  # ]:          0 :                 if (copy_from_user(&i, p, sizeof(unsigned int)))
     952                 :            :                         return -EFAULT;
     953         [ #  # ]:          0 :                 if (i != CLOCK_MONOTONIC && i != CLOCK_REALTIME)
     954                 :            :                         return -EINVAL;
     955                 :          0 :                 client->clkid = i;
     956                 :            :                 return 0;
     957                 :            : 
     958                 :            :         case EVIOCGKEYCODE:
     959                 :          0 :                 return evdev_handle_get_keycode(dev, p);
     960                 :            : 
     961                 :            :         case EVIOCSKEYCODE:
     962                 :          0 :                 return evdev_handle_set_keycode(dev, p);
     963                 :            : 
     964                 :            :         case EVIOCGKEYCODE_V2:
     965                 :          0 :                 return evdev_handle_get_keycode_v2(dev, p);
     966                 :            : 
     967                 :            :         case EVIOCSKEYCODE_V2:
     968                 :          0 :                 return evdev_handle_set_keycode_v2(dev, p);
     969                 :            : 
     970                 :            :         case EVIOCGSUSPENDBLOCK:
     971                 :          0 :                 return put_user(client->use_wake_lock, ip);
     972                 :            : 
     973                 :            :         case EVIOCSSUSPENDBLOCK:
     974         [ #  # ]:          0 :                 if (p)
     975                 :          0 :                         return evdev_enable_suspend_block(evdev, client);
     976                 :            :                 else
     977                 :          0 :                         return evdev_disable_suspend_block(evdev, client);
     978                 :            :         }
     979                 :            : 
     980                 :          0 :         size = _IOC_SIZE(cmd);
     981                 :            : 
     982                 :            :         /* Now check variable-length commands */
     983                 :            : #define EVIOC_MASK_SIZE(nr)     ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
     984   [ #  #  #  #  :          0 :         switch (EVIOC_MASK_SIZE(cmd)) {
          #  #  #  #  #  
                   #  # ]
     985                 :            : 
     986                 :            :         case EVIOCGPROP(0):
     987                 :          0 :                 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
     988                 :            :                                     size, p, compat_mode);
     989                 :            : 
     990                 :            :         case EVIOCGMTSLOTS(0):
     991                 :          0 :                 return evdev_handle_mt_request(dev, size, ip);
     992                 :            : 
     993                 :            :         case EVIOCGKEY(0):
     994                 :          0 :                 return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
     995                 :            :                                             KEY_MAX, size, p, compat_mode);
     996                 :            : 
     997                 :            :         case EVIOCGLED(0):
     998                 :          0 :                 return evdev_handle_get_val(client, dev, EV_LED, dev->led,
     999                 :            :                                             LED_MAX, size, p, compat_mode);
    1000                 :            : 
    1001                 :            :         case EVIOCGSND(0):
    1002                 :          0 :                 return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
    1003                 :            :                                             SND_MAX, size, p, compat_mode);
    1004                 :            : 
    1005                 :            :         case EVIOCGSW(0):
    1006                 :          0 :                 return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
    1007                 :            :                                             SW_MAX, size, p, compat_mode);
    1008                 :            : 
    1009                 :            :         case EVIOCGNAME(0):
    1010                 :          0 :                 return str_to_user(dev->name, size, p);
    1011                 :            : 
    1012                 :            :         case EVIOCGPHYS(0):
    1013                 :          0 :                 return str_to_user(dev->phys, size, p);
    1014                 :            : 
    1015                 :            :         case EVIOCGUNIQ(0):
    1016                 :          0 :                 return str_to_user(dev->uniq, size, p);
    1017                 :            : 
    1018                 :            :         case EVIOC_MASK_SIZE(EVIOCSFF):
    1019         [ #  # ]:          0 :                 if (input_ff_effect_from_user(p, size, &effect))
    1020                 :            :                         return -EFAULT;
    1021                 :            : 
    1022                 :          0 :                 error = input_ff_upload(dev, &effect, file);
    1023         [ #  # ]:          0 :                 if (error)
    1024                 :            :                         return error;
    1025                 :            : 
    1026         [ #  # ]:          0 :                 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
    1027                 :            :                         return -EFAULT;
    1028                 :            : 
    1029                 :            :                 return 0;
    1030                 :            :         }
    1031                 :            : 
    1032                 :            :         /* Multi-number variable-length handlers */
    1033         [ #  # ]:          0 :         if (_IOC_TYPE(cmd) != 'E')
    1034                 :            :                 return -EINVAL;
    1035                 :            : 
    1036         [ #  # ]:          0 :         if (_IOC_DIR(cmd) == _IOC_READ) {
    1037                 :            : 
    1038         [ #  # ]:          0 :                 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
    1039                 :          0 :                         return handle_eviocgbit(dev,
    1040                 :            :                                                 _IOC_NR(cmd) & EV_MAX, size,
    1041                 :            :                                                 p, compat_mode);
    1042                 :            : 
    1043         [ #  # ]:          0 :                 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
    1044                 :            : 
    1045         [ #  # ]:          0 :                         if (!dev->absinfo)
    1046                 :            :                                 return -EINVAL;
    1047                 :            : 
    1048                 :          0 :                         t = _IOC_NR(cmd) & ABS_MAX;
    1049                 :          0 :                         abs = dev->absinfo[t];
    1050                 :            : 
    1051         [ #  # ]:          0 :                         if (copy_to_user(p, &abs, min_t(size_t,
    1052                 :            :                                         size, sizeof(struct input_absinfo))))
    1053                 :            :                                 return -EFAULT;
    1054                 :            : 
    1055                 :            :                         return 0;
    1056                 :            :                 }
    1057                 :            :         }
    1058                 :            : 
    1059         [ #  # ]:          0 :         if (_IOC_DIR(cmd) == _IOC_WRITE) {
    1060                 :            : 
    1061         [ #  # ]:          0 :                 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
    1062                 :            : 
    1063         [ #  # ]:          0 :                         if (!dev->absinfo)
    1064                 :            :                                 return -EINVAL;
    1065                 :            : 
    1066                 :          0 :                         t = _IOC_NR(cmd) & ABS_MAX;
    1067                 :            : 
    1068         [ #  # ]:          0 :                         if (copy_from_user(&abs, p, min_t(size_t,
    1069                 :            :                                         size, sizeof(struct input_absinfo))))
    1070                 :            :                                 return -EFAULT;
    1071                 :            : 
    1072         [ #  # ]:          0 :                         if (size < sizeof(struct input_absinfo))
    1073                 :          0 :                                 abs.resolution = 0;
    1074                 :            : 
    1075                 :            :                         /* We can't change number of reserved MT slots */
    1076         [ #  # ]:          0 :                         if (t == ABS_MT_SLOT)
    1077                 :            :                                 return -EINVAL;
    1078                 :            : 
    1079                 :            :                         /*
    1080                 :            :                          * Take event lock to ensure that we are not
    1081                 :            :                          * changing device parameters in the middle
    1082                 :            :                          * of event.
    1083                 :            :                          */
    1084                 :            :                         spin_lock_irq(&dev->event_lock);
    1085                 :          0 :                         dev->absinfo[t] = abs;
    1086                 :            :                         spin_unlock_irq(&dev->event_lock);
    1087                 :            : 
    1088                 :            :                         return 0;
    1089                 :            :                 }
    1090                 :            :         }
    1091                 :            : 
    1092                 :            :         return -EINVAL;
    1093                 :            : }
    1094                 :            : 
    1095                 :          0 : static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
    1096                 :            :                                 void __user *p, int compat_mode)
    1097                 :            : {
    1098                 :          0 :         struct evdev_client *client = file->private_data;
    1099                 :          0 :         struct evdev *evdev = client->evdev;
    1100                 :            :         int retval;
    1101                 :            : 
    1102                 :          0 :         retval = mutex_lock_interruptible(&evdev->mutex);
    1103         [ #  # ]:          0 :         if (retval)
    1104                 :            :                 return retval;
    1105                 :            : 
    1106 [ #  # ][ #  # ]:          0 :         if (!evdev->exist || client->revoked) {
    1107                 :            :                 retval = -ENODEV;
    1108                 :            :                 goto out;
    1109                 :            :         }
    1110                 :            : 
    1111                 :          0 :         retval = evdev_do_ioctl(file, cmd, p, compat_mode);
    1112                 :            : 
    1113                 :            :  out:
    1114                 :          0 :         mutex_unlock(&evdev->mutex);
    1115                 :            :         return retval;
    1116                 :            : }
    1117                 :            : 
    1118                 :          0 : static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    1119                 :            : {
    1120                 :          0 :         return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
    1121                 :            : }
    1122                 :            : 
    1123                 :            : #ifdef CONFIG_COMPAT
    1124                 :            : static long evdev_ioctl_compat(struct file *file,
    1125                 :            :                                 unsigned int cmd, unsigned long arg)
    1126                 :            : {
    1127                 :            :         return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
    1128                 :            : }
    1129                 :            : #endif
    1130                 :            : 
    1131                 :            : static const struct file_operations evdev_fops = {
    1132                 :            :         .owner          = THIS_MODULE,
    1133                 :            :         .read           = evdev_read,
    1134                 :            :         .write          = evdev_write,
    1135                 :            :         .poll           = evdev_poll,
    1136                 :            :         .open           = evdev_open,
    1137                 :            :         .release        = evdev_release,
    1138                 :            :         .unlocked_ioctl = evdev_ioctl,
    1139                 :            : #ifdef CONFIG_COMPAT
    1140                 :            :         .compat_ioctl   = evdev_ioctl_compat,
    1141                 :            : #endif
    1142                 :            :         .fasync         = evdev_fasync,
    1143                 :            :         .flush          = evdev_flush,
    1144                 :            :         .llseek         = no_llseek,
    1145                 :            : };
    1146                 :            : 
    1147                 :            : /*
    1148                 :            :  * Mark device non-existent. This disables writes, ioctls and
    1149                 :            :  * prevents new users from opening the device. Already posted
    1150                 :            :  * blocking reads will stay, however new ones will fail.
    1151                 :            :  */
    1152                 :            : static void evdev_mark_dead(struct evdev *evdev)
    1153                 :            : {
    1154                 :          2 :         mutex_lock(&evdev->mutex);
    1155                 :          2 :         evdev->exist = false;
    1156                 :          2 :         mutex_unlock(&evdev->mutex);
    1157                 :            : }
    1158                 :            : 
    1159                 :          0 : static void evdev_cleanup(struct evdev *evdev)
    1160                 :            : {
    1161                 :          2 :         struct input_handle *handle = &evdev->handle;
    1162                 :            : 
    1163                 :            :         evdev_mark_dead(evdev);
    1164                 :          2 :         evdev_hangup(evdev);
    1165                 :            : 
    1166                 :          2 :         cdev_del(&evdev->cdev);
    1167                 :            : 
    1168                 :            :         /* evdev is marked dead so no one else accesses evdev->open */
    1169         [ -  + ]:          2 :         if (evdev->open) {
    1170                 :          0 :                 input_flush_device(handle, NULL);
    1171                 :          0 :                 input_close_device(handle);
    1172                 :            :         }
    1173                 :          0 : }
    1174                 :            : 
    1175                 :            : /*
    1176                 :            :  * Create new evdev device. Note that input core serializes calls
    1177                 :            :  * to connect and disconnect.
    1178                 :            :  */
    1179                 :          0 : static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
    1180                 :            :                          const struct input_device_id *id)
    1181                 :            : {
    1182                 :            :         struct evdev *evdev;
    1183                 :            :         int minor;
    1184                 :            :         int dev_no;
    1185                 :            :         int error;
    1186                 :            : 
    1187                 :          2 :         minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
    1188         [ -  + ]:          2 :         if (minor < 0) {
    1189                 :            :                 error = minor;
    1190                 :          0 :                 pr_err("failed to reserve new minor: %d\n", error);
    1191                 :          0 :                 return error;
    1192                 :            :         }
    1193                 :            : 
    1194                 :            :         evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
    1195         [ +  - ]:          2 :         if (!evdev) {
    1196                 :            :                 error = -ENOMEM;
    1197                 :            :                 goto err_free_minor;
    1198                 :            :         }
    1199                 :            : 
    1200                 :          2 :         INIT_LIST_HEAD(&evdev->client_list);
    1201                 :          2 :         spin_lock_init(&evdev->client_lock);
    1202                 :          2 :         mutex_init(&evdev->mutex);
    1203                 :          2 :         init_waitqueue_head(&evdev->wait);
    1204                 :          2 :         evdev->exist = true;
    1205                 :            : 
    1206                 :            :         dev_no = minor;
    1207                 :            :         /* Normalize device number if it falls into legacy range */
    1208         [ +  - ]:          2 :         if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
    1209                 :          2 :                 dev_no -= EVDEV_MINOR_BASE;
    1210                 :          2 :         dev_set_name(&evdev->dev, "event%d", dev_no);
    1211                 :            : 
    1212                 :          2 :         evdev->handle.dev = input_get_device(dev);
    1213                 :          0 :         evdev->handle.name = dev_name(&evdev->dev);
    1214                 :          0 :         evdev->handle.handler = handler;
    1215                 :          0 :         evdev->handle.private = evdev;
    1216                 :            : 
    1217                 :          0 :         evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
    1218                 :          0 :         evdev->dev.class = &input_class;
    1219                 :          0 :         evdev->dev.parent = &dev->dev;
    1220                 :          0 :         evdev->dev.release = evdev_free;
    1221                 :          2 :         device_initialize(&evdev->dev);
    1222                 :            : 
    1223                 :          2 :         error = input_register_handle(&evdev->handle);
    1224         [ +  - ]:          2 :         if (error)
    1225                 :            :                 goto err_free_evdev;
    1226                 :            : 
    1227                 :          2 :         cdev_init(&evdev->cdev, &evdev_fops);
    1228                 :          2 :         evdev->cdev.kobj.parent = &evdev->dev.kobj;
    1229                 :          2 :         error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);
    1230         [ +  - ]:          2 :         if (error)
    1231                 :            :                 goto err_unregister_handle;
    1232                 :            : 
    1233                 :          2 :         error = device_add(&evdev->dev);
    1234         [ -  + ]:          2 :         if (error)
    1235                 :            :                 goto err_cleanup_evdev;
    1236                 :            : 
    1237                 :            :         return 0;
    1238                 :            : 
    1239                 :            :  err_cleanup_evdev:
    1240                 :          0 :         evdev_cleanup(evdev);
    1241                 :            :  err_unregister_handle:
    1242                 :          0 :         input_unregister_handle(&evdev->handle);
    1243                 :            :  err_free_evdev:
    1244                 :          0 :         put_device(&evdev->dev);
    1245                 :            :  err_free_minor:
    1246                 :          0 :         input_free_minor(minor);
    1247                 :          0 :         return error;
    1248                 :            : }
    1249                 :            : 
    1250                 :          0 : static void evdev_disconnect(struct input_handle *handle)
    1251                 :            : {
    1252                 :          2 :         struct evdev *evdev = handle->private;
    1253                 :            : 
    1254                 :          2 :         device_del(&evdev->dev);
    1255                 :          2 :         evdev_cleanup(evdev);
    1256                 :          2 :         input_free_minor(MINOR(evdev->dev.devt));
    1257                 :          2 :         input_unregister_handle(handle);
    1258                 :          2 :         put_device(&evdev->dev);
    1259                 :          2 : }
    1260                 :            : 
    1261                 :            : static const struct input_device_id evdev_ids[] = {
    1262                 :            :         { .driver_info = 1 },   /* Matches all devices */
    1263                 :            :         { },                    /* Terminating zero entry */
    1264                 :            : };
    1265                 :            : 
    1266                 :            : MODULE_DEVICE_TABLE(input, evdev_ids);
    1267                 :            : 
    1268                 :            : static struct input_handler evdev_handler = {
    1269                 :            :         .event          = evdev_event,
    1270                 :            :         .events         = evdev_events,
    1271                 :            :         .connect        = evdev_connect,
    1272                 :            :         .disconnect     = evdev_disconnect,
    1273                 :            :         .legacy_minors  = true,
    1274                 :            :         .minor          = EVDEV_MINOR_BASE,
    1275                 :            :         .name           = "evdev",
    1276                 :            :         .id_table       = evdev_ids,
    1277                 :            : };
    1278                 :            : 
    1279                 :          0 : static int __init evdev_init(void)
    1280                 :            : {
    1281                 :          0 :         return input_register_handler(&evdev_handler);
    1282                 :            : }
    1283                 :            : 
    1284                 :          0 : static void __exit evdev_exit(void)
    1285                 :            : {
    1286                 :          0 :         input_unregister_handler(&evdev_handler);
    1287                 :          0 : }
    1288                 :            : 
    1289                 :            : module_init(evdev_init);
    1290                 :            : module_exit(evdev_exit);
    1291                 :            : 
    1292                 :            : MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
    1293                 :            : MODULE_DESCRIPTION("Input driver event char devices");
    1294                 :            : MODULE_LICENSE("GPL");

Generated by: LCOV version 1.9