Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Joystick device driver for the input driver suite. |
| 3 | * |
| 4 | * Copyright (c) 1999-2002 Vojtech Pavlik |
| 5 | * Copyright (c) 1999 Colin Van Dyke |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <asm/io.h> |
| 14 | #include <asm/system.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/joystick.h> |
| 18 | #include <linux/input.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/major.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/miscdevice.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/poll.h> |
| 26 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 30 | MODULE_DESCRIPTION("Joystick device interfaces"); |
| 31 | MODULE_SUPPORTED_DEVICE("input/js"); |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | |
| 34 | #define JOYDEV_MINOR_BASE 0 |
| 35 | #define JOYDEV_MINORS 16 |
| 36 | #define JOYDEV_BUFFER_SIZE 64 |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct joydev { |
| 39 | int exist; |
| 40 | int open; |
| 41 | int minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | struct input_handle handle; |
| 43 | wait_queue_head_t wait; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 44 | struct list_head client_list; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 45 | spinlock_t client_lock; /* protects client_list */ |
| 46 | struct mutex mutex; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 47 | struct device dev; |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | struct js_corr corr[ABS_MAX + 1]; |
| 50 | struct JS_DATA_SAVE_TYPE glue; |
| 51 | int nabs; |
| 52 | int nkey; |
| 53 | __u16 keymap[KEY_MAX - BTN_MISC + 1]; |
| 54 | __u16 keypam[KEY_MAX - BTN_MISC + 1]; |
| 55 | __u8 absmap[ABS_MAX + 1]; |
| 56 | __u8 abspam[ABS_MAX + 1]; |
| 57 | __s16 abs[ABS_MAX + 1]; |
| 58 | }; |
| 59 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 60 | struct joydev_client { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | struct js_event buffer[JOYDEV_BUFFER_SIZE]; |
| 62 | int head; |
| 63 | int tail; |
| 64 | int startup; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 65 | spinlock_t buffer_lock; /* protects access to buffer, head and tail */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | struct fasync_struct *fasync; |
| 67 | struct joydev *joydev; |
| 68 | struct list_head node; |
| 69 | }; |
| 70 | |
| 71 | static struct joydev *joydev_table[JOYDEV_MINORS]; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 72 | static DEFINE_MUTEX(joydev_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | static int joydev_correct(int value, struct js_corr *corr) |
| 75 | { |
| 76 | switch (corr->type) { |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 77 | |
| 78 | case JS_CORR_NONE: |
| 79 | break; |
| 80 | |
| 81 | case JS_CORR_BROKEN: |
| 82 | value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 : |
| 83 | ((corr->coef[3] * (value - corr->coef[1])) >> 14)) : |
| 84 | ((corr->coef[2] * (value - corr->coef[0])) >> 14); |
| 85 | break; |
| 86 | |
| 87 | default: |
| 88 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 91 | return value < -32767 ? -32767 : (value > 32767 ? 32767 : value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 94 | static void joydev_pass_event(struct joydev_client *client, |
| 95 | struct js_event *event) |
| 96 | { |
| 97 | struct joydev *joydev = client->joydev; |
| 98 | |
| 99 | /* |
| 100 | * IRQs already disabled, just acquire the lock |
| 101 | */ |
| 102 | spin_lock(&client->buffer_lock); |
| 103 | |
| 104 | client->buffer[client->head] = *event; |
| 105 | |
| 106 | if (client->startup == joydev->nabs + joydev->nkey) { |
| 107 | client->head++; |
| 108 | client->head &= JOYDEV_BUFFER_SIZE - 1; |
| 109 | if (client->tail == client->head) |
| 110 | client->startup = 0; |
| 111 | } |
| 112 | |
| 113 | spin_unlock(&client->buffer_lock); |
| 114 | |
| 115 | kill_fasync(&client->fasync, SIGIO, POLL_IN); |
| 116 | } |
| 117 | |
| 118 | static void joydev_event(struct input_handle *handle, |
| 119 | unsigned int type, unsigned int code, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
| 121 | struct joydev *joydev = handle->private; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 122 | struct joydev_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | struct js_event event; |
| 124 | |
| 125 | switch (type) { |
| 126 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 127 | case EV_KEY: |
| 128 | if (code < BTN_MISC || value == 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 130 | event.type = JS_EVENT_BUTTON; |
| 131 | event.number = joydev->keymap[code - BTN_MISC]; |
| 132 | event.value = value; |
| 133 | break; |
| 134 | |
| 135 | case EV_ABS: |
| 136 | event.type = JS_EVENT_AXIS; |
| 137 | event.number = joydev->absmap[code]; |
| 138 | event.value = joydev_correct(value, |
| 139 | &joydev->corr[event.number]); |
| 140 | if (event.value == joydev->abs[event.number]) |
| 141 | return; |
| 142 | joydev->abs[event.number] = event.value; |
| 143 | break; |
| 144 | |
| 145 | default: |
| 146 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Tobias Klauser | 6f5eacf | 2005-07-11 01:08:56 -0500 | [diff] [blame] | 149 | event.time = jiffies_to_msecs(jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Dmitry Torokhov | 82ba56c | 2007-10-13 15:46:55 -0400 | [diff] [blame] | 151 | rcu_read_lock(); |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 152 | list_for_each_entry_rcu(client, &joydev->client_list, node) |
| 153 | joydev_pass_event(client, &event); |
Dmitry Torokhov | 82ba56c | 2007-10-13 15:46:55 -0400 | [diff] [blame] | 154 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | wake_up_interruptible(&joydev->wait); |
| 157 | } |
| 158 | |
| 159 | static int joydev_fasync(int fd, struct file *file, int on) |
| 160 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 161 | struct joydev_client *client = file->private_data; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 162 | |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 163 | return fasync_helper(fd, file, on, &client->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 166 | static void joydev_free(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 168 | struct joydev *joydev = container_of(dev, struct joydev, dev); |
| 169 | |
Dmitry Torokhov | a7097ff | 2008-04-01 00:22:53 -0400 | [diff] [blame] | 170 | input_put_device(joydev->handle.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | kfree(joydev); |
| 172 | } |
| 173 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 174 | static void joydev_attach_client(struct joydev *joydev, |
| 175 | struct joydev_client *client) |
| 176 | { |
| 177 | spin_lock(&joydev->client_lock); |
| 178 | list_add_tail_rcu(&client->node, &joydev->client_list); |
| 179 | spin_unlock(&joydev->client_lock); |
Dmitry Torokhov | 82ba56c | 2007-10-13 15:46:55 -0400 | [diff] [blame] | 180 | synchronize_rcu(); |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void joydev_detach_client(struct joydev *joydev, |
| 184 | struct joydev_client *client) |
| 185 | { |
| 186 | spin_lock(&joydev->client_lock); |
| 187 | list_del_rcu(&client->node); |
| 188 | spin_unlock(&joydev->client_lock); |
Dmitry Torokhov | 82ba56c | 2007-10-13 15:46:55 -0400 | [diff] [blame] | 189 | synchronize_rcu(); |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static int joydev_open_device(struct joydev *joydev) |
| 193 | { |
| 194 | int retval; |
| 195 | |
| 196 | retval = mutex_lock_interruptible(&joydev->mutex); |
| 197 | if (retval) |
| 198 | return retval; |
| 199 | |
| 200 | if (!joydev->exist) |
| 201 | retval = -ENODEV; |
Oliver Neukum | 0644501 | 2007-10-12 14:18:40 -0400 | [diff] [blame] | 202 | else if (!joydev->open++) { |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 203 | retval = input_open_device(&joydev->handle); |
Oliver Neukum | 0644501 | 2007-10-12 14:18:40 -0400 | [diff] [blame] | 204 | if (retval) |
| 205 | joydev->open--; |
| 206 | } |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 207 | |
| 208 | mutex_unlock(&joydev->mutex); |
| 209 | return retval; |
| 210 | } |
| 211 | |
| 212 | static void joydev_close_device(struct joydev *joydev) |
| 213 | { |
| 214 | mutex_lock(&joydev->mutex); |
| 215 | |
| 216 | if (joydev->exist && !--joydev->open) |
| 217 | input_close_device(&joydev->handle); |
| 218 | |
| 219 | mutex_unlock(&joydev->mutex); |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Wake up users waiting for IO so they can disconnect from |
| 224 | * dead device. |
| 225 | */ |
| 226 | static void joydev_hangup(struct joydev *joydev) |
| 227 | { |
| 228 | struct joydev_client *client; |
| 229 | |
| 230 | spin_lock(&joydev->client_lock); |
| 231 | list_for_each_entry(client, &joydev->client_list, node) |
| 232 | kill_fasync(&client->fasync, SIGIO, POLL_HUP); |
| 233 | spin_unlock(&joydev->client_lock); |
| 234 | |
| 235 | wake_up_interruptible(&joydev->wait); |
| 236 | } |
| 237 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 238 | static int joydev_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 240 | struct joydev_client *client = file->private_data; |
| 241 | struct joydev *joydev = client->joydev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 243 | joydev_detach_client(joydev, client); |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 244 | kfree(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 246 | joydev_close_device(joydev); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 247 | put_device(&joydev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static int joydev_open(struct inode *inode, struct file *file) |
| 253 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 254 | struct joydev_client *client; |
| 255 | struct joydev *joydev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | int i = iminor(inode) - JOYDEV_MINOR_BASE; |
Dmitry Torokhov | d542ed8 | 2007-04-12 01:30:15 -0400 | [diff] [blame] | 257 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 259 | if (i >= JOYDEV_MINORS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return -ENODEV; |
| 261 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 262 | error = mutex_lock_interruptible(&joydev_table_mutex); |
| 263 | if (error) |
| 264 | return error; |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 265 | joydev = joydev_table[i]; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 266 | if (joydev) |
| 267 | get_device(&joydev->dev); |
| 268 | mutex_unlock(&joydev_table_mutex); |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 269 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 270 | if (!joydev) |
| 271 | return -ENODEV; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 272 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 273 | client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 274 | if (!client) { |
| 275 | error = -ENOMEM; |
| 276 | goto err_put_joydev; |
| 277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 279 | spin_lock_init(&client->buffer_lock); |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 280 | client->joydev = joydev; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 281 | joydev_attach_client(joydev, client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 283 | error = joydev_open_device(joydev); |
| 284 | if (error) |
| 285 | goto err_free_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 287 | file->private_data = client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return 0; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 289 | |
| 290 | err_free_client: |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 291 | joydev_detach_client(joydev, client); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 292 | kfree(client); |
| 293 | err_put_joydev: |
| 294 | put_device(&joydev->dev); |
| 295 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 298 | static int joydev_generate_startup_event(struct joydev_client *client, |
| 299 | struct input_dev *input, |
| 300 | struct js_event *event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 302 | struct joydev *joydev = client->joydev; |
| 303 | int have_event; |
| 304 | |
| 305 | spin_lock_irq(&client->buffer_lock); |
| 306 | |
| 307 | have_event = client->startup < joydev->nabs + joydev->nkey; |
| 308 | |
| 309 | if (have_event) { |
| 310 | |
| 311 | event->time = jiffies_to_msecs(jiffies); |
| 312 | if (client->startup < joydev->nkey) { |
| 313 | event->type = JS_EVENT_BUTTON | JS_EVENT_INIT; |
| 314 | event->number = client->startup; |
| 315 | event->value = !!test_bit(joydev->keypam[event->number], |
| 316 | input->key); |
| 317 | } else { |
| 318 | event->type = JS_EVENT_AXIS | JS_EVENT_INIT; |
| 319 | event->number = client->startup - joydev->nkey; |
| 320 | event->value = joydev->abs[event->number]; |
| 321 | } |
| 322 | client->startup++; |
| 323 | } |
| 324 | |
| 325 | spin_unlock_irq(&client->buffer_lock); |
| 326 | |
| 327 | return have_event; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 330 | static int joydev_fetch_next_event(struct joydev_client *client, |
| 331 | struct js_event *event) |
| 332 | { |
| 333 | int have_event; |
| 334 | |
| 335 | spin_lock_irq(&client->buffer_lock); |
| 336 | |
| 337 | have_event = client->head != client->tail; |
| 338 | if (have_event) { |
| 339 | *event = client->buffer[client->tail++]; |
| 340 | client->tail &= JOYDEV_BUFFER_SIZE - 1; |
| 341 | } |
| 342 | |
| 343 | spin_unlock_irq(&client->buffer_lock); |
| 344 | |
| 345 | return have_event; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Old joystick interface |
| 350 | */ |
| 351 | static ssize_t joydev_0x_read(struct joydev_client *client, |
| 352 | struct input_dev *input, |
| 353 | char __user *buf) |
| 354 | { |
| 355 | struct joydev *joydev = client->joydev; |
| 356 | struct JS_DATA_TYPE data; |
| 357 | int i; |
| 358 | |
| 359 | spin_lock_irq(&input->event_lock); |
| 360 | |
| 361 | /* |
| 362 | * Get device state |
| 363 | */ |
| 364 | for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++) |
| 365 | data.buttons |= |
| 366 | test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0; |
| 367 | data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x; |
| 368 | data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y; |
| 369 | |
| 370 | /* |
| 371 | * Reset reader's event queue |
| 372 | */ |
| 373 | spin_lock(&client->buffer_lock); |
| 374 | client->startup = 0; |
| 375 | client->tail = client->head; |
| 376 | spin_unlock(&client->buffer_lock); |
| 377 | |
| 378 | spin_unlock_irq(&input->event_lock); |
| 379 | |
| 380 | if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE))) |
| 381 | return -EFAULT; |
| 382 | |
| 383 | return sizeof(struct JS_DATA_TYPE); |
| 384 | } |
| 385 | |
| 386 | static inline int joydev_data_pending(struct joydev_client *client) |
| 387 | { |
| 388 | struct joydev *joydev = client->joydev; |
| 389 | |
| 390 | return client->startup < joydev->nabs + joydev->nkey || |
| 391 | client->head != client->tail; |
| 392 | } |
| 393 | |
| 394 | static ssize_t joydev_read(struct file *file, char __user *buf, |
| 395 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 397 | struct joydev_client *client = file->private_data; |
| 398 | struct joydev *joydev = client->joydev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | struct input_dev *input = joydev->handle.dev; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 400 | struct js_event event; |
| 401 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 403 | if (!joydev->exist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return -ENODEV; |
| 405 | |
| 406 | if (count < sizeof(struct js_event)) |
| 407 | return -EINVAL; |
| 408 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 409 | if (count == sizeof(struct JS_DATA_TYPE)) |
| 410 | return joydev_0x_read(client, input, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 412 | if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK)) |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 413 | return -EAGAIN; |
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 | retval = wait_event_interruptible(joydev->wait, |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 416 | !joydev->exist || joydev_data_pending(client)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | if (retval) |
| 418 | return retval; |
| 419 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 420 | if (!joydev->exist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | return -ENODEV; |
| 422 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 423 | while (retval + sizeof(struct js_event) <= count && |
| 424 | joydev_generate_startup_event(client, input, &event)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | if (copy_to_user(buf + retval, &event, sizeof(struct js_event))) |
| 427 | return -EFAULT; |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | retval += sizeof(struct js_event); |
| 430 | } |
| 431 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 432 | while (retval + sizeof(struct js_event) <= count && |
| 433 | joydev_fetch_next_event(client, &event)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 435 | if (copy_to_user(buf + retval, &event, sizeof(struct js_event))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | return -EFAULT; |
| 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | retval += sizeof(struct js_event); |
| 439 | } |
| 440 | |
| 441 | return retval; |
| 442 | } |
| 443 | |
| 444 | /* No kernel lock - fine */ |
| 445 | static unsigned int joydev_poll(struct file *file, poll_table *wait) |
| 446 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 447 | struct joydev_client *client = file->private_data; |
| 448 | struct joydev *joydev = client->joydev; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 449 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 450 | poll_wait(file, &joydev->wait, wait); |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 451 | return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) | |
| 452 | (joydev->exist ? 0 : (POLLHUP | POLLERR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 455 | static int joydev_ioctl_common(struct joydev *joydev, |
| 456 | unsigned int cmd, void __user *argp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | struct input_dev *dev = joydev->handle.dev; |
Stephen Kitt | ec8b4b7 | 2009-08-12 01:12:08 -0700 | [diff] [blame^] | 459 | size_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | int i, j; |
Stephen Kitt | ec8b4b7 | 2009-08-12 01:12:08 -0700 | [diff] [blame^] | 461 | const char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Stephen Kitt | ec8b4b7 | 2009-08-12 01:12:08 -0700 | [diff] [blame^] | 463 | /* Process fixed-sized commands. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | switch (cmd) { |
| 465 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 466 | case JS_SET_CAL: |
| 467 | return copy_from_user(&joydev->glue.JS_CORR, argp, |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 468 | sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 469 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 470 | case JS_GET_CAL: |
| 471 | return copy_to_user(argp, &joydev->glue.JS_CORR, |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 472 | sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 473 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 474 | case JS_SET_TIMEOUT: |
| 475 | return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp); |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 476 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 477 | case JS_GET_TIMEOUT: |
| 478 | return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 480 | case JSIOCGVERSION: |
| 481 | return put_user(JS_VERSION, (__u32 __user *) argp); |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 482 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 483 | case JSIOCGAXES: |
| 484 | return put_user(joydev->nabs, (__u8 __user *) argp); |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 485 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 486 | case JSIOCGBUTTONS: |
| 487 | return put_user(joydev->nkey, (__u8 __user *) argp); |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 488 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 489 | case JSIOCSCORR: |
| 490 | if (copy_from_user(joydev->corr, argp, |
| 491 | sizeof(joydev->corr[0]) * joydev->nabs)) |
| 492 | return -EFAULT; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 493 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 494 | for (i = 0; i < joydev->nabs; i++) { |
| 495 | j = joydev->abspam[i]; |
| 496 | joydev->abs[i] = joydev_correct(dev->abs[j], |
| 497 | &joydev->corr[i]); |
| 498 | } |
| 499 | return 0; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 500 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 501 | case JSIOCGCORR: |
| 502 | return copy_to_user(argp, joydev->corr, |
| 503 | sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0; |
| 504 | |
Stephen Kitt | ec8b4b7 | 2009-08-12 01:12:08 -0700 | [diff] [blame^] | 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Process variable-sized commands (the axis and button map commands |
| 509 | * are considered variable-sized to decouple them from the values of |
| 510 | * ABS_MAX and KEY_MAX). |
| 511 | */ |
| 512 | switch (cmd & ~IOCSIZE_MASK) { |
| 513 | |
| 514 | case (JSIOCSAXMAP & ~IOCSIZE_MASK): |
| 515 | len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam)); |
| 516 | /* |
| 517 | * FIXME: we should not copy into our axis map before |
| 518 | * validating the data. |
| 519 | */ |
| 520 | if (copy_from_user(joydev->abspam, argp, len)) |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 521 | return -EFAULT; |
| 522 | |
| 523 | for (i = 0; i < joydev->nabs; i++) { |
| 524 | if (joydev->abspam[i] > ABS_MAX) |
| 525 | return -EINVAL; |
| 526 | joydev->absmap[joydev->abspam[i]] = i; |
| 527 | } |
| 528 | return 0; |
| 529 | |
Stephen Kitt | ec8b4b7 | 2009-08-12 01:12:08 -0700 | [diff] [blame^] | 530 | case (JSIOCGAXMAP & ~IOCSIZE_MASK): |
| 531 | len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam)); |
| 532 | return copy_to_user(argp, joydev->abspam, len) ? -EFAULT : 0; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 533 | |
Stephen Kitt | ec8b4b7 | 2009-08-12 01:12:08 -0700 | [diff] [blame^] | 534 | case (JSIOCSBTNMAP & ~IOCSIZE_MASK): |
| 535 | len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam)); |
| 536 | /* |
| 537 | * FIXME: we should not copy into our keymap before |
| 538 | * validating the data. |
| 539 | */ |
| 540 | if (copy_from_user(joydev->keypam, argp, len)) |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 541 | return -EFAULT; |
| 542 | |
| 543 | for (i = 0; i < joydev->nkey; i++) { |
| 544 | if (joydev->keypam[i] > KEY_MAX || |
| 545 | joydev->keypam[i] < BTN_MISC) |
| 546 | return -EINVAL; |
| 547 | joydev->keymap[joydev->keypam[i] - BTN_MISC] = i; |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | |
Stephen Kitt | ec8b4b7 | 2009-08-12 01:12:08 -0700 | [diff] [blame^] | 552 | case (JSIOCGBTNMAP & ~IOCSIZE_MASK): |
| 553 | len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam)); |
| 554 | return copy_to_user(argp, joydev->keypam, len) ? -EFAULT : 0; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 555 | |
Stephen Kitt | ec8b4b7 | 2009-08-12 01:12:08 -0700 | [diff] [blame^] | 556 | case JSIOCGNAME(0): |
| 557 | name = dev->name; |
| 558 | if (!name) |
| 559 | return 0; |
Thadeu Lima de Souza Cascardo | 3d5cb60 | 2009-05-09 16:08:04 -0700 | [diff] [blame] | 560 | |
Stephen Kitt | ec8b4b7 | 2009-08-12 01:12:08 -0700 | [diff] [blame^] | 561 | len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1); |
| 562 | return copy_to_user(argp, name, len) ? -EFAULT : len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
Stephen Kitt | ec8b4b7 | 2009-08-12 01:12:08 -0700 | [diff] [blame^] | 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | return -EINVAL; |
| 566 | } |
| 567 | |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 568 | #ifdef CONFIG_COMPAT |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 569 | static long joydev_compat_ioctl(struct file *file, |
| 570 | unsigned int cmd, unsigned long arg) |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 571 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 572 | struct joydev_client *client = file->private_data; |
| 573 | struct joydev *joydev = client->joydev; |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 574 | void __user *argp = (void __user *)arg; |
| 575 | s32 tmp32; |
| 576 | struct JS_DATA_SAVE_TYPE_32 ds32; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 577 | int retval; |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 578 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 579 | retval = mutex_lock_interruptible(&joydev->mutex); |
| 580 | if (retval) |
| 581 | return retval; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 582 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 583 | if (!joydev->exist) { |
| 584 | retval = -ENODEV; |
| 585 | goto out; |
| 586 | } |
| 587 | |
| 588 | switch (cmd) { |
| 589 | |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 590 | case JS_SET_TIMELIMIT: |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 591 | retval = get_user(tmp32, (s32 __user *) arg); |
| 592 | if (retval == 0) |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 593 | joydev->glue.JS_TIMELIMIT = tmp32; |
| 594 | break; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 595 | |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 596 | case JS_GET_TIMELIMIT: |
| 597 | tmp32 = joydev->glue.JS_TIMELIMIT; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 598 | retval = put_user(tmp32, (s32 __user *) arg); |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 599 | break; |
| 600 | |
| 601 | case JS_SET_ALL: |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 602 | retval = copy_from_user(&ds32, argp, |
| 603 | sizeof(ds32)) ? -EFAULT : 0; |
| 604 | if (retval == 0) { |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 605 | joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT; |
| 606 | joydev->glue.BUSY = ds32.BUSY; |
| 607 | joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME; |
| 608 | joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT; |
| 609 | joydev->glue.JS_SAVE = ds32.JS_SAVE; |
| 610 | joydev->glue.JS_CORR = ds32.JS_CORR; |
| 611 | } |
| 612 | break; |
| 613 | |
| 614 | case JS_GET_ALL: |
| 615 | ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT; |
| 616 | ds32.BUSY = joydev->glue.BUSY; |
| 617 | ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME; |
| 618 | ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT; |
| 619 | ds32.JS_SAVE = joydev->glue.JS_SAVE; |
| 620 | ds32.JS_CORR = joydev->glue.JS_CORR; |
| 621 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 622 | retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0; |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 623 | break; |
| 624 | |
| 625 | default: |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 626 | retval = joydev_ioctl_common(joydev, cmd, argp); |
| 627 | break; |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 628 | } |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 629 | |
| 630 | out: |
| 631 | mutex_unlock(&joydev->mutex); |
| 632 | return retval; |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 633 | } |
| 634 | #endif /* CONFIG_COMPAT */ |
| 635 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 636 | static long joydev_ioctl(struct file *file, |
| 637 | unsigned int cmd, unsigned long arg) |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 638 | { |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 639 | struct joydev_client *client = file->private_data; |
| 640 | struct joydev *joydev = client->joydev; |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 641 | void __user *argp = (void __user *)arg; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 642 | int retval; |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 643 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 644 | retval = mutex_lock_interruptible(&joydev->mutex); |
| 645 | if (retval) |
| 646 | return retval; |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 647 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 648 | if (!joydev->exist) { |
| 649 | retval = -ENODEV; |
| 650 | goto out; |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 651 | } |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 652 | |
| 653 | switch (cmd) { |
| 654 | |
| 655 | case JS_SET_TIMELIMIT: |
| 656 | retval = get_user(joydev->glue.JS_TIMELIMIT, |
| 657 | (long __user *) arg); |
| 658 | break; |
| 659 | |
| 660 | case JS_GET_TIMELIMIT: |
| 661 | retval = put_user(joydev->glue.JS_TIMELIMIT, |
| 662 | (long __user *) arg); |
| 663 | break; |
| 664 | |
| 665 | case JS_SET_ALL: |
| 666 | retval = copy_from_user(&joydev->glue, argp, |
| 667 | sizeof(joydev->glue)) ? -EFAULT: 0; |
| 668 | break; |
| 669 | |
| 670 | case JS_GET_ALL: |
| 671 | retval = copy_to_user(argp, &joydev->glue, |
| 672 | sizeof(joydev->glue)) ? -EFAULT : 0; |
| 673 | break; |
| 674 | |
| 675 | default: |
| 676 | retval = joydev_ioctl_common(joydev, cmd, argp); |
| 677 | break; |
| 678 | } |
| 679 | out: |
| 680 | mutex_unlock(&joydev->mutex); |
| 681 | return retval; |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 682 | } |
| 683 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 684 | static const struct file_operations joydev_fops = { |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 685 | .owner = THIS_MODULE, |
| 686 | .read = joydev_read, |
| 687 | .poll = joydev_poll, |
| 688 | .open = joydev_open, |
| 689 | .release = joydev_release, |
| 690 | .unlocked_ioctl = joydev_ioctl, |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 691 | #ifdef CONFIG_COMPAT |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 692 | .compat_ioctl = joydev_compat_ioctl, |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 693 | #endif |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 694 | .fasync = joydev_fasync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | }; |
| 696 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 697 | static int joydev_install_chrdev(struct joydev *joydev) |
| 698 | { |
| 699 | joydev_table[joydev->minor] = joydev; |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static void joydev_remove_chrdev(struct joydev *joydev) |
| 704 | { |
| 705 | mutex_lock(&joydev_table_mutex); |
| 706 | joydev_table[joydev->minor] = NULL; |
| 707 | mutex_unlock(&joydev_table_mutex); |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Mark device non-existant. This disables writes, ioctls and |
| 712 | * prevents new users from opening the device. Already posted |
| 713 | * blocking reads will stay, however new ones will fail. |
| 714 | */ |
| 715 | static void joydev_mark_dead(struct joydev *joydev) |
| 716 | { |
| 717 | mutex_lock(&joydev->mutex); |
| 718 | joydev->exist = 0; |
| 719 | mutex_unlock(&joydev->mutex); |
| 720 | } |
| 721 | |
| 722 | static void joydev_cleanup(struct joydev *joydev) |
| 723 | { |
| 724 | struct input_handle *handle = &joydev->handle; |
| 725 | |
| 726 | joydev_mark_dead(joydev); |
| 727 | joydev_hangup(joydev); |
| 728 | joydev_remove_chrdev(joydev); |
| 729 | |
| 730 | /* joydev is marked dead so noone else accesses joydev->open */ |
| 731 | if (joydev->open) |
| 732 | input_close_device(handle); |
| 733 | } |
| 734 | |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 735 | static int joydev_connect(struct input_handler *handler, struct input_dev *dev, |
| 736 | const struct input_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
| 738 | struct joydev *joydev; |
| 739 | int i, j, t, minor; |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 740 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 742 | for (minor = 0; minor < JOYDEV_MINORS; minor++) |
| 743 | if (!joydev_table[minor]) |
| 744 | break; |
| 745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | if (minor == JOYDEV_MINORS) { |
| 747 | printk(KERN_ERR "joydev: no more free joydev devices\n"); |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 748 | return -ENFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 751 | joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL); |
| 752 | if (!joydev) |
| 753 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
Dmitry Torokhov | d0ffb9b | 2007-04-12 01:30:00 -0400 | [diff] [blame] | 755 | INIT_LIST_HEAD(&joydev->client_list); |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 756 | spin_lock_init(&joydev->client_lock); |
| 757 | mutex_init(&joydev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | init_waitqueue_head(&joydev->wait); |
| 759 | |
Thadeu Lima de Souza Cascardo | 3d5cb60 | 2009-05-09 16:08:04 -0700 | [diff] [blame] | 760 | dev_set_name(&joydev->dev, "js%d", minor); |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 761 | joydev->exist = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | joydev->minor = minor; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | joydev->exist = 1; |
Dmitry Torokhov | a7097ff | 2008-04-01 00:22:53 -0400 | [diff] [blame] | 765 | joydev->handle.dev = input_get_device(dev); |
Thadeu Lima de Souza Cascardo | 3d5cb60 | 2009-05-09 16:08:04 -0700 | [diff] [blame] | 766 | joydev->handle.name = dev_name(&joydev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | joydev->handle.handler = handler; |
| 768 | joydev->handle.private = joydev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | for (i = 0; i < ABS_MAX + 1; i++) |
| 771 | if (test_bit(i, dev->absbit)) { |
| 772 | joydev->absmap[i] = joydev->nabs; |
| 773 | joydev->abspam[joydev->nabs] = i; |
| 774 | joydev->nabs++; |
| 775 | } |
| 776 | |
| 777 | for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++) |
| 778 | if (test_bit(i + BTN_MISC, dev->keybit)) { |
| 779 | joydev->keymap[i] = joydev->nkey; |
| 780 | joydev->keypam[joydev->nkey] = i + BTN_MISC; |
| 781 | joydev->nkey++; |
| 782 | } |
| 783 | |
Vojtech Pavlik | f24949e | 2005-05-28 02:11:49 -0500 | [diff] [blame] | 784 | for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | if (test_bit(i + BTN_MISC, dev->keybit)) { |
| 786 | joydev->keymap[i] = joydev->nkey; |
| 787 | joydev->keypam[joydev->nkey] = i + BTN_MISC; |
| 788 | joydev->nkey++; |
| 789 | } |
| 790 | |
| 791 | for (i = 0; i < joydev->nabs; i++) { |
| 792 | j = joydev->abspam[i]; |
| 793 | if (dev->absmax[j] == dev->absmin[j]) { |
| 794 | joydev->corr[i].type = JS_CORR_NONE; |
| 795 | joydev->abs[i] = dev->abs[j]; |
| 796 | continue; |
| 797 | } |
| 798 | joydev->corr[i].type = JS_CORR_BROKEN; |
| 799 | joydev->corr[i].prec = dev->absfuzz[j]; |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 800 | joydev->corr[i].coef[0] = |
| 801 | (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j]; |
| 802 | joydev->corr[i].coef[1] = |
| 803 | (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 805 | t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j]; |
| 806 | if (t) { |
| 807 | joydev->corr[i].coef[2] = (1 << 29) / t; |
| 808 | joydev->corr[i].coef[3] = (1 << 29) / t; |
| 809 | |
| 810 | joydev->abs[i] = joydev_correct(dev->abs[j], |
| 811 | joydev->corr + i); |
| 812 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 815 | joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 816 | joydev->dev.class = &input_class; |
| 817 | joydev->dev.parent = &dev->dev; |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 818 | joydev->dev.release = joydev_free; |
| 819 | device_initialize(&joydev->dev); |
| 820 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 821 | error = input_register_handle(&joydev->handle); |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 822 | if (error) |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 823 | goto err_free_joydev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 825 | error = joydev_install_chrdev(joydev); |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 826 | if (error) |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 827 | goto err_unregister_handle; |
| 828 | |
| 829 | error = device_add(&joydev->dev); |
| 830 | if (error) |
| 831 | goto err_cleanup_joydev; |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 832 | |
| 833 | return 0; |
| 834 | |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 835 | err_cleanup_joydev: |
| 836 | joydev_cleanup(joydev); |
| 837 | err_unregister_handle: |
| 838 | input_unregister_handle(&joydev->handle); |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 839 | err_free_joydev: |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 840 | put_device(&joydev->dev); |
Dmitry Torokhov | 5b2a082 | 2007-04-12 01:29:46 -0400 | [diff] [blame] | 841 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | static void joydev_disconnect(struct input_handle *handle) |
| 845 | { |
| 846 | struct joydev *joydev = handle->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 848 | device_del(&joydev->dev); |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 849 | joydev_cleanup(joydev); |
| 850 | input_unregister_handle(handle); |
Dmitry Torokhov | 9657d75 | 2007-06-14 23:32:24 -0400 | [diff] [blame] | 851 | put_device(&joydev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 854 | static const struct input_device_id joydev_blacklist[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | { |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 856 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | |
| 857 | INPUT_DEVICE_ID_MATCH_KEYBIT, |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 858 | .evbit = { BIT_MASK(EV_KEY) }, |
| 859 | .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) }, |
Tim Cole | d07a9cb | 2009-05-07 17:08:42 -0700 | [diff] [blame] | 860 | }, /* Avoid itouchpads and touchscreens */ |
| 861 | { |
| 862 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | |
| 863 | INPUT_DEVICE_ID_MATCH_KEYBIT, |
| 864 | .evbit = { BIT_MASK(EV_KEY) }, |
| 865 | .keybit = { [BIT_WORD(BTN_DIGI)] = BIT_MASK(BTN_DIGI) }, |
| 866 | }, /* Avoid tablets, digitisers and similar devices */ |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 867 | { } /* Terminating entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | }; |
| 869 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 870 | static const struct input_device_id joydev_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | { |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 872 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | |
| 873 | INPUT_DEVICE_ID_MATCH_ABSBIT, |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 874 | .evbit = { BIT_MASK(EV_ABS) }, |
| 875 | .absbit = { BIT_MASK(ABS_X) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | }, |
| 877 | { |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 878 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | |
| 879 | INPUT_DEVICE_ID_MATCH_ABSBIT, |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 880 | .evbit = { BIT_MASK(EV_ABS) }, |
| 881 | .absbit = { BIT_MASK(ABS_WHEEL) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | }, |
| 883 | { |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 884 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | |
| 885 | INPUT_DEVICE_ID_MATCH_ABSBIT, |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 886 | .evbit = { BIT_MASK(EV_ABS) }, |
| 887 | .absbit = { BIT_MASK(ABS_THROTTLE) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | }, |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 889 | { } /* Terminating entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | }; |
| 891 | |
| 892 | MODULE_DEVICE_TABLE(input, joydev_ids); |
| 893 | |
| 894 | static struct input_handler joydev_handler = { |
Dmitry Torokhov | b126207 | 2007-08-30 00:22:32 -0400 | [diff] [blame] | 895 | .event = joydev_event, |
| 896 | .connect = joydev_connect, |
| 897 | .disconnect = joydev_disconnect, |
| 898 | .fops = &joydev_fops, |
| 899 | .minor = JOYDEV_MINOR_BASE, |
| 900 | .name = "joydev", |
| 901 | .id_table = joydev_ids, |
| 902 | .blacklist = joydev_blacklist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | }; |
| 904 | |
| 905 | static int __init joydev_init(void) |
| 906 | { |
Dmitry Torokhov | 4263cf0 | 2006-09-14 01:32:39 -0400 | [diff] [blame] | 907 | return input_register_handler(&joydev_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | static void __exit joydev_exit(void) |
| 911 | { |
| 912 | input_unregister_handler(&joydev_handler); |
| 913 | } |
| 914 | |
| 915 | module_init(joydev_init); |
| 916 | module_exit(joydev_exit); |