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