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