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> |
| 27 | #include <linux/smp_lock.h> |
| 28 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 31 | MODULE_DESCRIPTION("Joystick device interfaces"); |
| 32 | MODULE_SUPPORTED_DEVICE("input/js"); |
| 33 | MODULE_LICENSE("GPL"); |
| 34 | |
| 35 | #define JOYDEV_MINOR_BASE 0 |
| 36 | #define JOYDEV_MINORS 16 |
| 37 | #define JOYDEV_BUFFER_SIZE 64 |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | struct joydev { |
| 40 | int exist; |
| 41 | int open; |
| 42 | int minor; |
| 43 | char name[16]; |
| 44 | struct input_handle handle; |
| 45 | wait_queue_head_t wait; |
| 46 | struct list_head list; |
| 47 | struct js_corr corr[ABS_MAX + 1]; |
| 48 | struct JS_DATA_SAVE_TYPE glue; |
| 49 | int nabs; |
| 50 | int nkey; |
| 51 | __u16 keymap[KEY_MAX - BTN_MISC + 1]; |
| 52 | __u16 keypam[KEY_MAX - BTN_MISC + 1]; |
| 53 | __u8 absmap[ABS_MAX + 1]; |
| 54 | __u8 abspam[ABS_MAX + 1]; |
| 55 | __s16 abs[ABS_MAX + 1]; |
| 56 | }; |
| 57 | |
| 58 | struct joydev_list { |
| 59 | struct js_event buffer[JOYDEV_BUFFER_SIZE]; |
| 60 | int head; |
| 61 | int tail; |
| 62 | int startup; |
| 63 | struct fasync_struct *fasync; |
| 64 | struct joydev *joydev; |
| 65 | struct list_head node; |
| 66 | }; |
| 67 | |
| 68 | static struct joydev *joydev_table[JOYDEV_MINORS]; |
| 69 | |
| 70 | static int joydev_correct(int value, struct js_corr *corr) |
| 71 | { |
| 72 | switch (corr->type) { |
| 73 | case JS_CORR_NONE: |
| 74 | break; |
| 75 | case JS_CORR_BROKEN: |
| 76 | value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 : |
| 77 | ((corr->coef[3] * (value - corr->coef[1])) >> 14)) : |
| 78 | ((corr->coef[2] * (value - corr->coef[0])) >> 14); |
| 79 | break; |
| 80 | default: |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | if (value < -32767) return -32767; |
| 85 | if (value > 32767) return 32767; |
| 86 | |
| 87 | return value; |
| 88 | } |
| 89 | |
| 90 | static void joydev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) |
| 91 | { |
| 92 | struct joydev *joydev = handle->private; |
| 93 | struct joydev_list *list; |
| 94 | struct js_event event; |
| 95 | |
| 96 | switch (type) { |
| 97 | |
| 98 | case EV_KEY: |
| 99 | if (code < BTN_MISC || value == 2) return; |
| 100 | event.type = JS_EVENT_BUTTON; |
| 101 | event.number = joydev->keymap[code - BTN_MISC]; |
| 102 | event.value = value; |
| 103 | break; |
| 104 | |
| 105 | case EV_ABS: |
| 106 | event.type = JS_EVENT_AXIS; |
| 107 | event.number = joydev->absmap[code]; |
| 108 | event.value = joydev_correct(value, joydev->corr + event.number); |
| 109 | if (event.value == joydev->abs[event.number]) return; |
| 110 | joydev->abs[event.number] = event.value; |
| 111 | break; |
| 112 | |
| 113 | default: |
| 114 | return; |
| 115 | } |
| 116 | |
Tobias Klauser | 6f5eacf | 2005-07-11 01:08:56 -0500 | [diff] [blame] | 117 | event.time = jiffies_to_msecs(jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | list_for_each_entry(list, &joydev->list, node) { |
| 120 | |
| 121 | memcpy(list->buffer + list->head, &event, sizeof(struct js_event)); |
| 122 | |
| 123 | if (list->startup == joydev->nabs + joydev->nkey) |
| 124 | if (list->tail == (list->head = (list->head + 1) & (JOYDEV_BUFFER_SIZE - 1))) |
| 125 | list->startup = 0; |
| 126 | |
| 127 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
| 128 | } |
| 129 | |
| 130 | wake_up_interruptible(&joydev->wait); |
| 131 | } |
| 132 | |
| 133 | static int joydev_fasync(int fd, struct file *file, int on) |
| 134 | { |
| 135 | int retval; |
| 136 | struct joydev_list *list = file->private_data; |
| 137 | retval = fasync_helper(fd, file, on, &list->fasync); |
| 138 | return retval < 0 ? retval : 0; |
| 139 | } |
| 140 | |
| 141 | static void joydev_free(struct joydev *joydev) |
| 142 | { |
| 143 | joydev_table[joydev->minor] = NULL; |
| 144 | kfree(joydev); |
| 145 | } |
| 146 | |
| 147 | static int joydev_release(struct inode * inode, struct file * file) |
| 148 | { |
| 149 | struct joydev_list *list = file->private_data; |
| 150 | |
| 151 | joydev_fasync(-1, file, 0); |
| 152 | |
| 153 | list_del(&list->node); |
| 154 | |
| 155 | if (!--list->joydev->open) { |
| 156 | if (list->joydev->exist) |
| 157 | input_close_device(&list->joydev->handle); |
| 158 | else |
| 159 | joydev_free(list->joydev); |
| 160 | } |
| 161 | |
| 162 | kfree(list); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int joydev_open(struct inode *inode, struct file *file) |
| 167 | { |
| 168 | struct joydev_list *list; |
| 169 | int i = iminor(inode) - JOYDEV_MINOR_BASE; |
| 170 | |
| 171 | if (i >= JOYDEV_MINORS || !joydev_table[i]) |
| 172 | return -ENODEV; |
| 173 | |
Eric Sesterhenn | b39787a | 2006-03-14 00:09:16 -0500 | [diff] [blame^] | 174 | if (!(list = kzalloc(sizeof(struct joydev_list), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | list->joydev = joydev_table[i]; |
| 178 | list_add_tail(&list->node, &joydev_table[i]->list); |
| 179 | file->private_data = list; |
| 180 | |
| 181 | if (!list->joydev->open++) |
| 182 | if (list->joydev->exist) |
| 183 | input_open_device(&list->joydev->handle); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static ssize_t joydev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos) |
| 189 | { |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | |
| 193 | static ssize_t joydev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 194 | { |
| 195 | struct joydev_list *list = file->private_data; |
| 196 | struct joydev *joydev = list->joydev; |
| 197 | struct input_dev *input = joydev->handle.dev; |
| 198 | int retval = 0; |
| 199 | |
| 200 | if (!list->joydev->exist) |
| 201 | return -ENODEV; |
| 202 | |
| 203 | if (count < sizeof(struct js_event)) |
| 204 | return -EINVAL; |
| 205 | |
| 206 | if (count == sizeof(struct JS_DATA_TYPE)) { |
| 207 | |
| 208 | struct JS_DATA_TYPE data; |
| 209 | int i; |
| 210 | |
| 211 | for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++) |
| 212 | data.buttons |= test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0; |
| 213 | data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x; |
| 214 | data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y; |
| 215 | |
| 216 | if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE))) |
| 217 | return -EFAULT; |
| 218 | |
| 219 | list->startup = 0; |
| 220 | list->tail = list->head; |
| 221 | |
| 222 | return sizeof(struct JS_DATA_TYPE); |
| 223 | } |
| 224 | |
| 225 | if (list->startup == joydev->nabs + joydev->nkey |
| 226 | && list->head == list->tail && (file->f_flags & O_NONBLOCK)) |
| 227 | return -EAGAIN; |
| 228 | |
| 229 | retval = wait_event_interruptible(list->joydev->wait, |
| 230 | !list->joydev->exist || |
| 231 | list->startup < joydev->nabs + joydev->nkey || |
| 232 | list->head != list->tail); |
| 233 | |
| 234 | if (retval) |
| 235 | return retval; |
| 236 | |
| 237 | if (!list->joydev->exist) |
| 238 | return -ENODEV; |
| 239 | |
| 240 | while (list->startup < joydev->nabs + joydev->nkey && retval + sizeof(struct js_event) <= count) { |
| 241 | |
| 242 | struct js_event event; |
| 243 | |
Tobias Klauser | 6f5eacf | 2005-07-11 01:08:56 -0500 | [diff] [blame] | 244 | event.time = jiffies_to_msecs(jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | if (list->startup < joydev->nkey) { |
| 247 | event.type = JS_EVENT_BUTTON | JS_EVENT_INIT; |
| 248 | event.number = list->startup; |
| 249 | event.value = !!test_bit(joydev->keypam[event.number], input->key); |
| 250 | } else { |
| 251 | event.type = JS_EVENT_AXIS | JS_EVENT_INIT; |
| 252 | event.number = list->startup - joydev->nkey; |
| 253 | event.value = joydev->abs[event.number]; |
| 254 | } |
| 255 | |
| 256 | if (copy_to_user(buf + retval, &event, sizeof(struct js_event))) |
| 257 | return -EFAULT; |
| 258 | |
| 259 | list->startup++; |
| 260 | retval += sizeof(struct js_event); |
| 261 | } |
| 262 | |
| 263 | while (list->head != list->tail && retval + sizeof(struct js_event) <= count) { |
| 264 | |
| 265 | if (copy_to_user(buf + retval, list->buffer + list->tail, sizeof(struct js_event))) |
| 266 | return -EFAULT; |
| 267 | |
| 268 | list->tail = (list->tail + 1) & (JOYDEV_BUFFER_SIZE - 1); |
| 269 | retval += sizeof(struct js_event); |
| 270 | } |
| 271 | |
| 272 | return retval; |
| 273 | } |
| 274 | |
| 275 | /* No kernel lock - fine */ |
| 276 | static unsigned int joydev_poll(struct file *file, poll_table *wait) |
| 277 | { |
| 278 | struct joydev_list *list = file->private_data; |
| 279 | poll_wait(file, &list->joydev->wait, wait); |
| 280 | return ((list->head != list->tail || list->startup < list->joydev->nabs + list->joydev->nkey) ? |
| 281 | (POLLIN | POLLRDNORM) : 0) | (list->joydev->exist ? 0 : (POLLHUP | POLLERR)); |
| 282 | } |
| 283 | |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 284 | static int joydev_ioctl_common(struct joydev *joydev, unsigned int cmd, void __user *argp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | struct input_dev *dev = joydev->handle.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | int i, j; |
| 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | switch (cmd) { |
| 290 | |
| 291 | case JS_SET_CAL: |
| 292 | return copy_from_user(&joydev->glue.JS_CORR, argp, |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 293 | sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | case JS_GET_CAL: |
| 295 | return copy_to_user(argp, &joydev->glue.JS_CORR, |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 296 | sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | case JS_SET_TIMEOUT: |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 298 | return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | case JS_GET_TIMEOUT: |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 300 | return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | case JSIOCGVERSION: |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 303 | return put_user(JS_VERSION, (__u32 __user *) argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | case JSIOCGAXES: |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 305 | return put_user(joydev->nabs, (__u8 __user *) argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | case JSIOCGBUTTONS: |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 307 | return put_user(joydev->nkey, (__u8 __user *) argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | case JSIOCSCORR: |
| 309 | if (copy_from_user(joydev->corr, argp, |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 310 | sizeof(joydev->corr[0]) * joydev->nabs)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | return -EFAULT; |
| 312 | for (i = 0; i < joydev->nabs; i++) { |
| 313 | j = joydev->abspam[i]; |
| 314 | joydev->abs[i] = joydev_correct(dev->abs[j], joydev->corr + i); |
| 315 | } |
| 316 | return 0; |
| 317 | case JSIOCGCORR: |
| 318 | return copy_to_user(argp, joydev->corr, |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 319 | sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | case JSIOCSAXMAP: |
| 321 | if (copy_from_user(joydev->abspam, argp, sizeof(__u8) * (ABS_MAX + 1))) |
| 322 | return -EFAULT; |
| 323 | for (i = 0; i < joydev->nabs; i++) { |
| 324 | if (joydev->abspam[i] > ABS_MAX) return -EINVAL; |
| 325 | joydev->absmap[joydev->abspam[i]] = i; |
| 326 | } |
| 327 | return 0; |
| 328 | case JSIOCGAXMAP: |
| 329 | return copy_to_user(argp, joydev->abspam, |
| 330 | sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0; |
| 331 | case JSIOCSBTNMAP: |
| 332 | if (copy_from_user(joydev->keypam, argp, sizeof(__u16) * (KEY_MAX - BTN_MISC + 1))) |
| 333 | return -EFAULT; |
| 334 | for (i = 0; i < joydev->nkey; i++) { |
| 335 | if (joydev->keypam[i] > KEY_MAX || joydev->keypam[i] < BTN_MISC) return -EINVAL; |
| 336 | joydev->keymap[joydev->keypam[i] - BTN_MISC] = i; |
| 337 | } |
| 338 | return 0; |
| 339 | case JSIOCGBTNMAP: |
| 340 | return copy_to_user(argp, joydev->keypam, |
| 341 | sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0; |
| 342 | default: |
| 343 | if ((cmd & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) == JSIOCGNAME(0)) { |
| 344 | int len; |
| 345 | if (!dev->name) return 0; |
| 346 | len = strlen(dev->name) + 1; |
| 347 | if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); |
| 348 | if (copy_to_user(argp, dev->name, len)) return -EFAULT; |
| 349 | return len; |
| 350 | } |
| 351 | } |
| 352 | return -EINVAL; |
| 353 | } |
| 354 | |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 355 | #ifdef CONFIG_COMPAT |
| 356 | static long joydev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 357 | { |
| 358 | struct joydev_list *list = file->private_data; |
| 359 | struct joydev *joydev = list->joydev; |
| 360 | void __user *argp = (void __user *)arg; |
| 361 | s32 tmp32; |
| 362 | struct JS_DATA_SAVE_TYPE_32 ds32; |
| 363 | int err; |
| 364 | |
| 365 | if (!joydev->exist) return -ENODEV; |
| 366 | switch(cmd) { |
| 367 | case JS_SET_TIMELIMIT: |
| 368 | err = get_user(tmp32, (s32 __user *) arg); |
| 369 | if (err == 0) |
| 370 | joydev->glue.JS_TIMELIMIT = tmp32; |
| 371 | break; |
| 372 | case JS_GET_TIMELIMIT: |
| 373 | tmp32 = joydev->glue.JS_TIMELIMIT; |
| 374 | err = put_user(tmp32, (s32 __user *) arg); |
| 375 | break; |
| 376 | |
| 377 | case JS_SET_ALL: |
| 378 | err = copy_from_user(&ds32, argp, |
| 379 | sizeof(ds32)) ? -EFAULT : 0; |
| 380 | if (err == 0) { |
| 381 | joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT; |
| 382 | joydev->glue.BUSY = ds32.BUSY; |
| 383 | joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME; |
| 384 | joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT; |
| 385 | joydev->glue.JS_SAVE = ds32.JS_SAVE; |
| 386 | joydev->glue.JS_CORR = ds32.JS_CORR; |
| 387 | } |
| 388 | break; |
| 389 | |
| 390 | case JS_GET_ALL: |
| 391 | ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT; |
| 392 | ds32.BUSY = joydev->glue.BUSY; |
| 393 | ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME; |
| 394 | ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT; |
| 395 | ds32.JS_SAVE = joydev->glue.JS_SAVE; |
| 396 | ds32.JS_CORR = joydev->glue.JS_CORR; |
| 397 | |
| 398 | err = copy_to_user(argp, &ds32, |
| 399 | sizeof(ds32)) ? -EFAULT : 0; |
| 400 | break; |
| 401 | |
| 402 | default: |
| 403 | err = joydev_ioctl_common(joydev, cmd, argp); |
| 404 | } |
| 405 | return err; |
| 406 | } |
| 407 | #endif /* CONFIG_COMPAT */ |
| 408 | |
| 409 | static int joydev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) |
| 410 | { |
| 411 | struct joydev_list *list = file->private_data; |
| 412 | struct joydev *joydev = list->joydev; |
| 413 | void __user *argp = (void __user *)arg; |
| 414 | |
| 415 | if (!joydev->exist) return -ENODEV; |
| 416 | |
| 417 | switch(cmd) { |
| 418 | case JS_SET_TIMELIMIT: |
| 419 | return get_user(joydev->glue.JS_TIMELIMIT, (long __user *) arg); |
| 420 | case JS_GET_TIMELIMIT: |
| 421 | return put_user(joydev->glue.JS_TIMELIMIT, (long __user *) arg); |
| 422 | case JS_SET_ALL: |
| 423 | return copy_from_user(&joydev->glue, argp, |
| 424 | sizeof(joydev->glue)) ? -EFAULT : 0; |
| 425 | case JS_GET_ALL: |
| 426 | return copy_to_user(argp, &joydev->glue, |
| 427 | sizeof(joydev->glue)) ? -EFAULT : 0; |
| 428 | default: |
| 429 | return joydev_ioctl_common(joydev, cmd, argp); |
| 430 | } |
| 431 | } |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | static struct file_operations joydev_fops = { |
| 434 | .owner = THIS_MODULE, |
| 435 | .read = joydev_read, |
| 436 | .write = joydev_write, |
| 437 | .poll = joydev_poll, |
| 438 | .open = joydev_open, |
| 439 | .release = joydev_release, |
| 440 | .ioctl = joydev_ioctl, |
Jeremy Fitzhardinge | 024ac44 | 2005-05-29 02:26:31 -0500 | [diff] [blame] | 441 | #ifdef CONFIG_COMPAT |
| 442 | .compat_ioctl = joydev_compat_ioctl, |
| 443 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | .fasync = joydev_fasync, |
| 445 | }; |
| 446 | |
| 447 | static struct input_handle *joydev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id) |
| 448 | { |
| 449 | struct joydev *joydev; |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 450 | struct class_device *cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | int i, j, t, minor; |
| 452 | |
| 453 | for (minor = 0; minor < JOYDEV_MINORS && joydev_table[minor]; minor++); |
| 454 | if (minor == JOYDEV_MINORS) { |
| 455 | printk(KERN_ERR "joydev: no more free joydev devices\n"); |
| 456 | return NULL; |
| 457 | } |
| 458 | |
Eric Sesterhenn | b39787a | 2006-03-14 00:09:16 -0500 | [diff] [blame^] | 459 | if (!(joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 462 | INIT_LIST_HEAD(&joydev->list); |
| 463 | init_waitqueue_head(&joydev->wait); |
| 464 | |
| 465 | joydev->minor = minor; |
| 466 | joydev->exist = 1; |
| 467 | joydev->handle.dev = dev; |
| 468 | joydev->handle.name = joydev->name; |
| 469 | joydev->handle.handler = handler; |
| 470 | joydev->handle.private = joydev; |
| 471 | sprintf(joydev->name, "js%d", minor); |
| 472 | |
| 473 | for (i = 0; i < ABS_MAX + 1; i++) |
| 474 | if (test_bit(i, dev->absbit)) { |
| 475 | joydev->absmap[i] = joydev->nabs; |
| 476 | joydev->abspam[joydev->nabs] = i; |
| 477 | joydev->nabs++; |
| 478 | } |
| 479 | |
| 480 | for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++) |
| 481 | if (test_bit(i + BTN_MISC, dev->keybit)) { |
| 482 | joydev->keymap[i] = joydev->nkey; |
| 483 | joydev->keypam[joydev->nkey] = i + BTN_MISC; |
| 484 | joydev->nkey++; |
| 485 | } |
| 486 | |
Vojtech Pavlik | f24949e | 2005-05-28 02:11:49 -0500 | [diff] [blame] | 487 | for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | if (test_bit(i + BTN_MISC, dev->keybit)) { |
| 489 | joydev->keymap[i] = joydev->nkey; |
| 490 | joydev->keypam[joydev->nkey] = i + BTN_MISC; |
| 491 | joydev->nkey++; |
| 492 | } |
| 493 | |
| 494 | for (i = 0; i < joydev->nabs; i++) { |
| 495 | j = joydev->abspam[i]; |
| 496 | if (dev->absmax[j] == dev->absmin[j]) { |
| 497 | joydev->corr[i].type = JS_CORR_NONE; |
| 498 | joydev->abs[i] = dev->abs[j]; |
| 499 | continue; |
| 500 | } |
| 501 | joydev->corr[i].type = JS_CORR_BROKEN; |
| 502 | joydev->corr[i].prec = dev->absfuzz[j]; |
| 503 | joydev->corr[i].coef[0] = (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j]; |
| 504 | joydev->corr[i].coef[1] = (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j]; |
| 505 | if (!(t = ((dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j]))) |
| 506 | continue; |
| 507 | joydev->corr[i].coef[2] = (1 << 29) / t; |
| 508 | joydev->corr[i].coef[3] = (1 << 29) / t; |
| 509 | |
| 510 | joydev->abs[i] = joydev_correct(dev->abs[j], joydev->corr + i); |
| 511 | } |
| 512 | |
| 513 | joydev_table[minor] = joydev; |
| 514 | |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 515 | cdev = class_device_create(&input_class, &dev->cdev, |
gregkh@suse.de | 1235686 | 2005-03-15 14:26:30 -0800 | [diff] [blame] | 516 | MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor), |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 517 | dev->cdev.dev, joydev->name); |
| 518 | |
| 519 | /* temporary symlink to keep userspace happy */ |
| 520 | sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj, |
| 521 | joydev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | return &joydev->handle; |
| 524 | } |
| 525 | |
| 526 | static void joydev_disconnect(struct input_handle *handle) |
| 527 | { |
| 528 | struct joydev *joydev = handle->private; |
| 529 | struct joydev_list *list; |
| 530 | |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 531 | sysfs_remove_link(&input_class.subsys.kset.kobj, joydev->name); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 532 | class_device_destroy(&input_class, MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + joydev->minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | joydev->exist = 0; |
| 534 | |
| 535 | if (joydev->open) { |
| 536 | input_close_device(handle); |
| 537 | wake_up_interruptible(&joydev->wait); |
| 538 | list_for_each_entry(list, &joydev->list, node) |
| 539 | kill_fasync(&list->fasync, SIGIO, POLL_HUP); |
| 540 | } else |
| 541 | joydev_free(joydev); |
| 542 | } |
| 543 | |
| 544 | static struct input_device_id joydev_blacklist[] = { |
| 545 | { |
| 546 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, |
| 547 | .evbit = { BIT(EV_KEY) }, |
| 548 | .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) }, |
| 549 | }, /* Avoid itouchpads, touchscreens and tablets */ |
| 550 | { }, /* Terminating entry */ |
| 551 | }; |
| 552 | |
| 553 | static struct input_device_id joydev_ids[] = { |
| 554 | { |
| 555 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, |
| 556 | .evbit = { BIT(EV_ABS) }, |
| 557 | .absbit = { BIT(ABS_X) }, |
| 558 | }, |
| 559 | { |
| 560 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, |
| 561 | .evbit = { BIT(EV_ABS) }, |
| 562 | .absbit = { BIT(ABS_WHEEL) }, |
| 563 | }, |
| 564 | { |
| 565 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, |
| 566 | .evbit = { BIT(EV_ABS) }, |
| 567 | .absbit = { BIT(ABS_THROTTLE) }, |
| 568 | }, |
| 569 | { }, /* Terminating entry */ |
| 570 | }; |
| 571 | |
| 572 | MODULE_DEVICE_TABLE(input, joydev_ids); |
| 573 | |
| 574 | static struct input_handler joydev_handler = { |
| 575 | .event = joydev_event, |
| 576 | .connect = joydev_connect, |
| 577 | .disconnect = joydev_disconnect, |
| 578 | .fops = &joydev_fops, |
| 579 | .minor = JOYDEV_MINOR_BASE, |
| 580 | .name = "joydev", |
| 581 | .id_table = joydev_ids, |
| 582 | .blacklist = joydev_blacklist, |
| 583 | }; |
| 584 | |
| 585 | static int __init joydev_init(void) |
| 586 | { |
| 587 | input_register_handler(&joydev_handler); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static void __exit joydev_exit(void) |
| 592 | { |
| 593 | input_unregister_handler(&joydev_handler); |
| 594 | } |
| 595 | |
| 596 | module_init(joydev_init); |
| 597 | module_exit(joydev_exit); |