Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 EVDEV_MINOR_BASE 64 |
| 12 | #define EVDEV_MINORS 32 |
| 13 | #define EVDEV_BUFFER_SIZE 64 |
| 14 | |
| 15 | #include <linux/poll.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/input.h> |
| 20 | #include <linux/major.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/device.h> |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 22 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | struct evdev { |
| 25 | int exist; |
| 26 | int open; |
| 27 | int minor; |
| 28 | char name[16]; |
| 29 | struct input_handle handle; |
| 30 | wait_queue_head_t wait; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 31 | struct evdev_client *grab; |
| 32 | struct list_head client_list; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 33 | struct device dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 36 | struct evdev_client { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | struct input_event buffer[EVDEV_BUFFER_SIZE]; |
| 38 | int head; |
| 39 | int tail; |
| 40 | struct fasync_struct *fasync; |
| 41 | struct evdev *evdev; |
| 42 | struct list_head node; |
| 43 | }; |
| 44 | |
| 45 | static struct evdev *evdev_table[EVDEV_MINORS]; |
| 46 | |
| 47 | static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) |
| 48 | { |
| 49 | struct evdev *evdev = handle->private; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 50 | struct evdev_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | if (evdev->grab) { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 53 | client = evdev->grab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 55 | do_gettimeofday(&client->buffer[client->head].time); |
| 56 | client->buffer[client->head].type = type; |
| 57 | client->buffer[client->head].code = code; |
| 58 | client->buffer[client->head].value = value; |
| 59 | client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 61 | kill_fasync(&client->fasync, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } else |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 63 | list_for_each_entry(client, &evdev->client_list, node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 65 | do_gettimeofday(&client->buffer[client->head].time); |
| 66 | client->buffer[client->head].type = type; |
| 67 | client->buffer[client->head].code = code; |
| 68 | client->buffer[client->head].value = value; |
| 69 | client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 71 | kill_fasync(&client->fasync, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | wake_up_interruptible(&evdev->wait); |
| 75 | } |
| 76 | |
| 77 | static int evdev_fasync(int fd, struct file *file, int on) |
| 78 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 79 | struct evdev_client *client = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | int retval; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 81 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 82 | retval = fasync_helper(fd, file, on, &client->fasync); |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return retval < 0 ? retval : 0; |
| 85 | } |
| 86 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 87 | static int evdev_flush(struct file *file, fl_owner_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 89 | struct evdev_client *client = file->private_data; |
| 90 | struct evdev *evdev = client->evdev; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 91 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 92 | if (!evdev->exist) |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 93 | return -ENODEV; |
| 94 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 95 | return input_flush_device(&evdev->handle, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 98 | static void evdev_free(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 100 | struct evdev *evdev = container_of(dev, struct evdev, dev); |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | evdev_table[evdev->minor] = NULL; |
| 103 | kfree(evdev); |
| 104 | } |
| 105 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 106 | static int evdev_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 108 | struct evdev_client *client = file->private_data; |
| 109 | struct evdev *evdev = client->evdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 111 | if (evdev->grab == client) { |
| 112 | input_release_device(&evdev->handle); |
| 113 | evdev->grab = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | evdev_fasync(-1, file, 0); |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 117 | list_del(&client->node); |
| 118 | kfree(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 120 | if (!--evdev->open && evdev->exist) |
| 121 | input_close_device(&evdev->handle); |
| 122 | |
| 123 | put_device(&evdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 128 | static int evdev_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 130 | struct evdev_client *client; |
| 131 | struct evdev *evdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | int i = iminor(inode) - EVDEV_MINOR_BASE; |
Dmitry Torokhov | d542ed8 | 2007-04-12 01:30:15 -0400 | [diff] [blame] | 133 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 135 | if (i >= EVDEV_MINORS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return -ENODEV; |
| 137 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 138 | evdev = evdev_table[i]; |
| 139 | |
| 140 | if (!evdev || !evdev->exist) |
| 141 | return -ENODEV; |
| 142 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 143 | get_device(&evdev->dev); |
| 144 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 145 | client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 146 | if (!client) { |
| 147 | error = -ENOMEM; |
| 148 | goto err_put_evdev; |
| 149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 151 | client->evdev = evdev; |
| 152 | list_add_tail(&client->node, &evdev->client_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Dmitry Torokhov | d542ed8 | 2007-04-12 01:30:15 -0400 | [diff] [blame] | 154 | if (!evdev->open++ && evdev->exist) { |
| 155 | error = input_open_device(&evdev->handle); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 156 | if (error) |
| 157 | goto err_free_client; |
Dmitry Torokhov | d542ed8 | 2007-04-12 01:30:15 -0400 | [diff] [blame] | 158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 160 | file->private_data = client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return 0; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 162 | |
| 163 | err_free_client: |
| 164 | list_del(&client->node); |
| 165 | kfree(client); |
| 166 | err_put_evdev: |
| 167 | put_device(&evdev->dev); |
| 168 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 171 | #ifdef CONFIG_COMPAT |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 172 | |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 173 | struct input_event_compat { |
| 174 | struct compat_timeval time; |
| 175 | __u16 type; |
| 176 | __u16 code; |
| 177 | __s32 value; |
| 178 | }; |
| 179 | |
Andi Kleen | bf2fcc6 | 2006-01-11 22:44:06 +0100 | [diff] [blame] | 180 | /* Note to the author of this code: did it ever occur to |
| 181 | you why the ifdefs are needed? Think about it again. -AK */ |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 182 | #ifdef CONFIG_X86_64 |
Andi Kleen | bf2fcc6 | 2006-01-11 22:44:06 +0100 | [diff] [blame] | 183 | # define COMPAT_TEST is_compat_task() |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 184 | #elif defined(CONFIG_IA64) |
Al Viro | 6450578 | 2006-01-12 01:06:06 -0800 | [diff] [blame] | 185 | # define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current)) |
Martin Schwidefsky | 347a8dc | 2006-01-06 00:19:28 -0800 | [diff] [blame] | 186 | #elif defined(CONFIG_S390) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 187 | # define COMPAT_TEST test_thread_flag(TIF_31BIT) |
Ralf Baechle | 59df6bb | 2005-09-03 15:56:23 -0700 | [diff] [blame] | 188 | #elif defined(CONFIG_MIPS) |
Ralf Baechle | 293c5bd | 2007-07-25 16:19:33 +0100 | [diff] [blame] | 189 | # define COMPAT_TEST test_thread_flag(TIF_32BIT_ADDR) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 190 | #else |
| 191 | # define COMPAT_TEST test_thread_flag(TIF_32BIT) |
| 192 | #endif |
| 193 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 194 | static inline size_t evdev_event_size(void) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 195 | { |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 196 | return COMPAT_TEST ? |
| 197 | sizeof(struct input_event_compat) : sizeof(struct input_event); |
| 198 | } |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 199 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 200 | static int evdev_event_from_user(const char __user *buffer, struct input_event *event) |
| 201 | { |
| 202 | if (COMPAT_TEST) { |
| 203 | struct input_event_compat compat_event; |
| 204 | |
| 205 | if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat))) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 206 | return -EFAULT; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 207 | |
| 208 | event->time.tv_sec = compat_event.time.tv_sec; |
| 209 | event->time.tv_usec = compat_event.time.tv_usec; |
| 210 | event->type = compat_event.type; |
| 211 | event->code = compat_event.code; |
| 212 | event->value = compat_event.value; |
| 213 | |
| 214 | } else { |
| 215 | if (copy_from_user(event, buffer, sizeof(struct input_event))) |
| 216 | return -EFAULT; |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 219 | return 0; |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 220 | } |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 221 | |
| 222 | static int evdev_event_to_user(char __user *buffer, const struct input_event *event) |
| 223 | { |
| 224 | if (COMPAT_TEST) { |
| 225 | struct input_event_compat compat_event; |
| 226 | |
| 227 | compat_event.time.tv_sec = event->time.tv_sec; |
| 228 | compat_event.time.tv_usec = event->time.tv_usec; |
| 229 | compat_event.type = event->type; |
| 230 | compat_event.code = event->code; |
| 231 | compat_event.value = event->value; |
| 232 | |
| 233 | if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat))) |
| 234 | return -EFAULT; |
| 235 | |
| 236 | } else { |
| 237 | if (copy_to_user(buffer, event, sizeof(struct input_event))) |
| 238 | return -EFAULT; |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | #else |
| 245 | |
| 246 | static inline size_t evdev_event_size(void) |
| 247 | { |
| 248 | return sizeof(struct input_event); |
| 249 | } |
| 250 | |
| 251 | static int evdev_event_from_user(const char __user *buffer, struct input_event *event) |
| 252 | { |
| 253 | if (copy_from_user(event, buffer, sizeof(struct input_event))) |
| 254 | return -EFAULT; |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int evdev_event_to_user(char __user *buffer, const struct input_event *event) |
| 260 | { |
| 261 | if (copy_to_user(buffer, event, sizeof(struct input_event))) |
| 262 | return -EFAULT; |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | #endif /* CONFIG_COMPAT */ |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 268 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 269 | static ssize_t evdev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 271 | struct evdev_client *client = file->private_data; |
| 272 | struct evdev *evdev = client->evdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | struct input_event event; |
| 274 | int retval = 0; |
| 275 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 276 | if (!evdev->exist) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 277 | return -ENODEV; |
| 278 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 279 | while (retval < count) { |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 280 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 281 | if (evdev_event_from_user(buffer + retval, &event)) |
| 282 | return -EFAULT; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 283 | input_inject_event(&evdev->handle, event.type, event.code, event.value); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 284 | retval += evdev_event_size(); |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | return retval; |
| 288 | } |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 289 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 290 | static ssize_t evdev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 292 | struct evdev_client *client = file->private_data; |
| 293 | struct evdev *evdev = client->evdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | int retval; |
| 295 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 296 | if (count < evdev_event_size()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return -EINVAL; |
| 298 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 299 | if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | return -EAGAIN; |
| 301 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 302 | retval = wait_event_interruptible(evdev->wait, |
| 303 | client->head != client->tail || !evdev->exist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | if (retval) |
| 305 | return retval; |
| 306 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 307 | if (!evdev->exist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | return -ENODEV; |
| 309 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 310 | while (client->head != client->tail && retval + evdev_event_size() <= count) { |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 311 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 312 | struct input_event *event = (struct input_event *) client->buffer + client->tail; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 313 | |
| 314 | if (evdev_event_to_user(buffer + retval, event)) |
| 315 | return -EFAULT; |
| 316 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 317 | client->tail = (client->tail + 1) & (EVDEV_BUFFER_SIZE - 1); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 318 | retval += evdev_event_size(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | return retval; |
| 322 | } |
| 323 | |
| 324 | /* No kernel lock - fine */ |
| 325 | static unsigned int evdev_poll(struct file *file, poll_table *wait) |
| 326 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 327 | struct evdev_client *client = file->private_data; |
| 328 | struct evdev *evdev = client->evdev; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 329 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 330 | poll_wait(file, &evdev->wait, wait); |
| 331 | return ((client->head == client->tail) ? 0 : (POLLIN | POLLRDNORM)) | |
| 332 | (evdev->exist ? 0 : (POLLHUP | POLLERR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 335 | #ifdef CONFIG_COMPAT |
| 336 | |
| 337 | #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8) |
| 338 | #define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1) |
| 339 | |
| 340 | #ifdef __BIG_ENDIAN |
| 341 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 342 | unsigned int maxlen, void __user *p, int compat) |
| 343 | { |
| 344 | int len, i; |
| 345 | |
| 346 | if (compat) { |
| 347 | len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t); |
Kenichi Nagai | bf61f8d | 2007-05-11 01:12:15 -0400 | [diff] [blame] | 348 | if (len > maxlen) |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 349 | len = maxlen; |
| 350 | |
| 351 | for (i = 0; i < len / sizeof(compat_long_t); i++) |
| 352 | if (copy_to_user((compat_long_t __user *) p + i, |
| 353 | (compat_long_t *) bits + |
| 354 | i + 1 - ((i % 2) << 1), |
| 355 | sizeof(compat_long_t))) |
| 356 | return -EFAULT; |
| 357 | } else { |
| 358 | len = NBITS(maxbit) * sizeof(long); |
| 359 | if (len > maxlen) |
| 360 | len = maxlen; |
| 361 | |
| 362 | if (copy_to_user(p, bits, len)) |
| 363 | return -EFAULT; |
| 364 | } |
| 365 | |
| 366 | return len; |
| 367 | } |
| 368 | #else |
| 369 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 370 | unsigned int maxlen, void __user *p, int compat) |
| 371 | { |
| 372 | int len = compat ? |
| 373 | NBITS_COMPAT(maxbit) * sizeof(compat_long_t) : |
| 374 | NBITS(maxbit) * sizeof(long); |
| 375 | |
| 376 | if (len > maxlen) |
| 377 | len = maxlen; |
| 378 | |
| 379 | return copy_to_user(p, bits, len) ? -EFAULT : len; |
| 380 | } |
| 381 | #endif /* __BIG_ENDIAN */ |
| 382 | |
| 383 | #else |
| 384 | |
| 385 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 386 | unsigned int maxlen, void __user *p, int compat) |
| 387 | { |
| 388 | int len = NBITS(maxbit) * sizeof(long); |
| 389 | |
| 390 | if (len > maxlen) |
| 391 | len = maxlen; |
| 392 | |
| 393 | return copy_to_user(p, bits, len) ? -EFAULT : len; |
| 394 | } |
| 395 | |
| 396 | #endif /* CONFIG_COMPAT */ |
| 397 | |
| 398 | static int str_to_user(const char *str, unsigned int maxlen, void __user *p) |
| 399 | { |
| 400 | int len; |
| 401 | |
| 402 | if (!str) |
| 403 | return -ENOENT; |
| 404 | |
| 405 | len = strlen(str) + 1; |
| 406 | if (len > maxlen) |
| 407 | len = maxlen; |
| 408 | |
| 409 | return copy_to_user(p, str, len) ? -EFAULT : len; |
| 410 | } |
| 411 | |
| 412 | static long evdev_ioctl_handler(struct file *file, unsigned int cmd, |
| 413 | void __user *p, int compat_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 415 | struct evdev_client *client = file->private_data; |
| 416 | struct evdev *evdev = client->evdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | struct input_dev *dev = evdev->handle.dev; |
| 418 | struct input_absinfo abs; |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 419 | struct ff_effect effect; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 420 | int __user *ip = (int __user *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | int i, t, u, v; |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 422 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 424 | if (!evdev->exist) |
| 425 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | switch (cmd) { |
| 428 | |
| 429 | case EVIOCGVERSION: |
| 430 | return put_user(EV_VERSION, ip); |
| 431 | |
| 432 | case EVIOCGID: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 433 | if (copy_to_user(p, &dev->id, sizeof(struct input_id))) |
| 434 | return -EFAULT; |
Dmitry Torokhov | 08791e5 | 2006-04-29 01:13:21 -0400 | [diff] [blame] | 435 | return 0; |
| 436 | |
| 437 | case EVIOCGREP: |
| 438 | if (!test_bit(EV_REP, dev->evbit)) |
| 439 | return -ENOSYS; |
| 440 | if (put_user(dev->rep[REP_DELAY], ip)) |
| 441 | return -EFAULT; |
| 442 | if (put_user(dev->rep[REP_PERIOD], ip + 1)) |
| 443 | return -EFAULT; |
| 444 | return 0; |
| 445 | |
| 446 | case EVIOCSREP: |
| 447 | if (!test_bit(EV_REP, dev->evbit)) |
| 448 | return -ENOSYS; |
| 449 | if (get_user(u, ip)) |
| 450 | return -EFAULT; |
| 451 | if (get_user(v, ip + 1)) |
| 452 | return -EFAULT; |
| 453 | |
Dmitry Torokhov | 0e739d2 | 2006-07-06 00:22:43 -0400 | [diff] [blame] | 454 | input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u); |
| 455 | input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 456 | |
| 457 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | case EVIOCGKEYCODE: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 460 | if (get_user(t, ip)) |
| 461 | return -EFAULT; |
Marvin Raaijmakers | c8e4c77 | 2007-03-14 22:50:42 -0400 | [diff] [blame] | 462 | |
| 463 | error = dev->getkeycode(dev, t, &v); |
| 464 | if (error) |
| 465 | return error; |
| 466 | |
| 467 | if (put_user(v, ip + 1)) |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 468 | return -EFAULT; |
Marvin Raaijmakers | c8e4c77 | 2007-03-14 22:50:42 -0400 | [diff] [blame] | 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | return 0; |
| 471 | |
| 472 | case EVIOCSKEYCODE: |
Marvin Raaijmakers | c8e4c77 | 2007-03-14 22:50:42 -0400 | [diff] [blame] | 473 | if (get_user(t, ip) || get_user(v, ip + 1)) |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 474 | return -EFAULT; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 475 | |
Marvin Raaijmakers | c8e4c77 | 2007-03-14 22:50:42 -0400 | [diff] [blame] | 476 | return dev->setkeycode(dev, t, v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | case EVIOCSFF: |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 479 | if (copy_from_user(&effect, p, sizeof(effect))) |
| 480 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 482 | error = input_ff_upload(dev, &effect, file); |
| 483 | |
| 484 | if (put_user(effect.id, &(((struct ff_effect __user *)p)->id))) |
| 485 | return -EFAULT; |
| 486 | |
| 487 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | case EVIOCRMFF: |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 490 | return input_ff_erase(dev, (int)(unsigned long) p, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | case EVIOCGEFFECTS: |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 493 | i = test_bit(EV_FF, dev->evbit) ? dev->ff->max_effects : 0; |
| 494 | if (put_user(i, ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | return -EFAULT; |
| 496 | return 0; |
| 497 | |
| 498 | case EVIOCGRAB: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 499 | if (p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | if (evdev->grab) |
| 501 | return -EBUSY; |
| 502 | if (input_grab_device(&evdev->handle)) |
| 503 | return -EBUSY; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 504 | evdev->grab = client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return 0; |
| 506 | } else { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 507 | if (evdev->grab != client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | return -EINVAL; |
| 509 | input_release_device(&evdev->handle); |
| 510 | evdev->grab = NULL; |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | default: |
| 515 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 516 | if (_IOC_TYPE(cmd) != 'E') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | return -EINVAL; |
| 518 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 519 | if (_IOC_DIR(cmd) == _IOC_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 521 | if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Dmitry Torokhov | ce305b6 | 2007-05-03 00:53:18 -0400 | [diff] [blame] | 523 | unsigned long *bits; |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 524 | int len; |
| 525 | |
| 526 | switch (_IOC_NR(cmd) & EV_MAX) { |
| 527 | case 0: bits = dev->evbit; len = EV_MAX; break; |
| 528 | case EV_KEY: bits = dev->keybit; len = KEY_MAX; break; |
| 529 | case EV_REL: bits = dev->relbit; len = REL_MAX; break; |
| 530 | case EV_ABS: bits = dev->absbit; len = ABS_MAX; break; |
| 531 | case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break; |
| 532 | case EV_LED: bits = dev->ledbit; len = LED_MAX; break; |
| 533 | case EV_SND: bits = dev->sndbit; len = SND_MAX; break; |
| 534 | case EV_FF: bits = dev->ffbit; len = FF_MAX; break; |
Richard Purdie | 3158106 | 2005-09-06 15:19:06 -0700 | [diff] [blame] | 535 | case EV_SW: bits = dev->swbit; len = SW_MAX; break; |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 536 | default: return -EINVAL; |
| 537 | } |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 538 | return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 540 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 541 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) |
| 542 | return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd), |
| 543 | p, compat_mode); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 544 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 545 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) |
| 546 | return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd), |
| 547 | p, compat_mode); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 548 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 549 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) |
| 550 | return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd), |
| 551 | p, compat_mode); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 552 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 553 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) |
| 554 | return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd), |
| 555 | p, compat_mode); |
Richard Purdie | 3158106 | 2005-09-06 15:19:06 -0700 | [diff] [blame] | 556 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 557 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) |
| 558 | return str_to_user(dev->name, _IOC_SIZE(cmd), p); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 559 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 560 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) |
| 561 | return str_to_user(dev->phys, _IOC_SIZE(cmd), p); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 562 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 563 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) |
| 564 | return str_to_user(dev->uniq, _IOC_SIZE(cmd), p); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 565 | |
| 566 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { |
| 567 | |
Dmitry Torokhov | ce305b6 | 2007-05-03 00:53:18 -0400 | [diff] [blame] | 568 | t = _IOC_NR(cmd) & ABS_MAX; |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 569 | |
| 570 | abs.value = dev->abs[t]; |
| 571 | abs.minimum = dev->absmin[t]; |
| 572 | abs.maximum = dev->absmax[t]; |
| 573 | abs.fuzz = dev->absfuzz[t]; |
| 574 | abs.flat = dev->absflat[t]; |
| 575 | |
| 576 | if (copy_to_user(p, &abs, sizeof(struct input_absinfo))) |
| 577 | return -EFAULT; |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 584 | if (_IOC_DIR(cmd) == _IOC_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 586 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Dmitry Torokhov | ce305b6 | 2007-05-03 00:53:18 -0400 | [diff] [blame] | 588 | t = _IOC_NR(cmd) & ABS_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 590 | if (copy_from_user(&abs, p, sizeof(struct input_absinfo))) |
| 591 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 593 | dev->abs[t] = abs.value; |
| 594 | dev->absmin[t] = abs.minimum; |
| 595 | dev->absmax[t] = abs.maximum; |
| 596 | dev->absfuzz[t] = abs.fuzz; |
| 597 | dev->absflat[t] = abs.flat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 599 | return 0; |
| 600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 606 | static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 607 | { |
| 608 | return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0); |
| 609 | } |
| 610 | |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 611 | #ifdef CONFIG_COMPAT |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 612 | static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) |
| 613 | { |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 614 | return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1); |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 615 | } |
| 616 | #endif |
| 617 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 618 | static const struct file_operations evdev_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | .owner = THIS_MODULE, |
| 620 | .read = evdev_read, |
| 621 | .write = evdev_write, |
| 622 | .poll = evdev_poll, |
| 623 | .open = evdev_open, |
| 624 | .release = evdev_release, |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 625 | .unlocked_ioctl = evdev_ioctl, |
| 626 | #ifdef CONFIG_COMPAT |
| 627 | .compat_ioctl = evdev_ioctl_compat, |
| 628 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | .fasync = evdev_fasync, |
| 630 | .flush = evdev_flush |
| 631 | }; |
| 632 | |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 633 | static int evdev_connect(struct input_handler *handler, struct input_dev *dev, |
| 634 | const struct input_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
| 636 | struct evdev *evdev; |
| 637 | int minor; |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 638 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++); |
| 641 | if (minor == EVDEV_MINORS) { |
| 642 | printk(KERN_ERR "evdev: no more free evdev devices\n"); |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 643 | return -ENFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 646 | evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL); |
| 647 | if (!evdev) |
| 648 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 650 | INIT_LIST_HEAD(&evdev->client_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | init_waitqueue_head(&evdev->wait); |
| 652 | |
| 653 | evdev->exist = 1; |
| 654 | evdev->minor = minor; |
| 655 | evdev->handle.dev = dev; |
| 656 | evdev->handle.name = evdev->name; |
| 657 | evdev->handle.handler = handler; |
| 658 | evdev->handle.private = evdev; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 659 | snprintf(evdev->name, sizeof(evdev->name), "event%d", minor); |
| 660 | |
| 661 | snprintf(evdev->dev.bus_id, sizeof(evdev->dev.bus_id), |
| 662 | "event%d", minor); |
| 663 | evdev->dev.class = &input_class; |
| 664 | evdev->dev.parent = &dev->dev; |
| 665 | evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor); |
| 666 | evdev->dev.release = evdev_free; |
| 667 | device_initialize(&evdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
| 669 | evdev_table[minor] = evdev; |
| 670 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 671 | error = device_add(&evdev->dev); |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 672 | if (error) |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 673 | goto err_free_evdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 675 | error = input_register_handle(&evdev->handle); |
| 676 | if (error) |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 677 | goto err_delete_evdev; |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 678 | |
| 679 | return 0; |
| 680 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 681 | err_delete_evdev: |
| 682 | device_del(&evdev->dev); |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 683 | err_free_evdev: |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 684 | put_device(&evdev->dev); |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 685 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | static void evdev_disconnect(struct input_handle *handle) |
| 689 | { |
| 690 | struct evdev *evdev = handle->private; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 691 | struct evdev_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 693 | input_unregister_handle(handle); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 694 | device_del(&evdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | evdev->exist = 0; |
| 697 | |
| 698 | if (evdev->open) { |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 699 | input_flush_device(handle, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | input_close_device(handle); |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 701 | list_for_each_entry(client, &evdev->client_list, node) |
| 702 | kill_fasync(&client->fasync, SIGIO, POLL_HUP); |
Dmitry Torokhov | 1dfa281 | 2007-06-03 23:29:36 -0400 | [diff] [blame] | 703 | wake_up_interruptible(&evdev->wait); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | put_device(&evdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 709 | static const struct input_device_id evdev_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { .driver_info = 1 }, /* Matches all devices */ |
| 711 | { }, /* Terminating zero entry */ |
| 712 | }; |
| 713 | |
| 714 | MODULE_DEVICE_TABLE(input, evdev_ids); |
| 715 | |
| 716 | static struct input_handler evdev_handler = { |
| 717 | .event = evdev_event, |
| 718 | .connect = evdev_connect, |
| 719 | .disconnect = evdev_disconnect, |
| 720 | .fops = &evdev_fops, |
| 721 | .minor = EVDEV_MINOR_BASE, |
| 722 | .name = "evdev", |
| 723 | .id_table = evdev_ids, |
| 724 | }; |
| 725 | |
| 726 | static int __init evdev_init(void) |
| 727 | { |
Dmitry Torokhov | 4263cf0 | 2006-09-14 01:32:39 -0400 | [diff] [blame] | 728 | return input_register_handler(&evdev_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | static void __exit evdev_exit(void) |
| 732 | { |
| 733 | input_unregister_handler(&evdev_handler); |
| 734 | } |
| 735 | |
| 736 | module_init(evdev_init); |
| 737 | module_exit(evdev_exit); |
| 738 | |
| 739 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 740 | MODULE_DESCRIPTION("Input driver event char devices"); |
| 741 | MODULE_LICENSE("GPL"); |