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