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