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