joydev.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <asm/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/errno.h>
  16. #include <linux/joystick.h>
  17. #include <linux/input.h>
  18. #include <linux/kernel.h>
  19. #include <linux/major.h>
  20. #include <linux/sched.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/device.h>
  28. #include <linux/cdev.h>
  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. #define JOYDEV_MINOR_BASE 0
  34. #define JOYDEV_MINORS 16
  35. #define JOYDEV_BUFFER_SIZE 64
  36. struct joydev {
  37. int open;
  38. struct input_handle handle;
  39. wait_queue_head_t wait;
  40. struct list_head client_list;
  41. spinlock_t client_lock; /* protects client_list */
  42. struct mutex mutex;
  43. struct device dev;
  44. struct cdev cdev;
  45. bool exist;
  46. struct js_corr corr[ABS_CNT];
  47. struct JS_DATA_SAVE_TYPE glue;
  48. int nabs;
  49. int nkey;
  50. __u16 keymap[KEY_MAX - BTN_MISC + 1];
  51. __u16 keypam[KEY_MAX - BTN_MISC + 1];
  52. __u8 absmap[ABS_CNT];
  53. __u8 abspam[ABS_CNT];
  54. __s16 abs[ABS_CNT];
  55. };
  56. struct joydev_client {
  57. struct js_event buffer[JOYDEV_BUFFER_SIZE];
  58. int head;
  59. int tail;
  60. int startup;
  61. spinlock_t buffer_lock; /* protects access to buffer, head and tail */
  62. struct fasync_struct *fasync;
  63. struct joydev *joydev;
  64. struct list_head node;
  65. };
  66. static int joydev_correct(int value, struct js_corr *corr)
  67. {
  68. switch (corr->type) {
  69. case JS_CORR_NONE:
  70. break;
  71. case JS_CORR_BROKEN:
  72. value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
  73. ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
  74. ((corr->coef[2] * (value - corr->coef[0])) >> 14);
  75. break;
  76. default:
  77. return 0;
  78. }
  79. return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
  80. }
  81. static void joydev_pass_event(struct joydev_client *client,
  82. struct js_event *event)
  83. {
  84. struct joydev *joydev = client->joydev;
  85. /*
  86. * IRQs already disabled, just acquire the lock
  87. */
  88. spin_lock(&client->buffer_lock);
  89. client->buffer[client->head] = *event;
  90. if (client->startup == joydev->nabs + joydev->nkey) {
  91. client->head++;
  92. client->head &= JOYDEV_BUFFER_SIZE - 1;
  93. if (client->tail == client->head)
  94. client->startup = 0;
  95. }
  96. spin_unlock(&client->buffer_lock);
  97. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  98. }
  99. static void joydev_event(struct input_handle *handle,
  100. unsigned int type, unsigned int code, int value)
  101. {
  102. struct joydev *joydev = handle->private;
  103. struct joydev_client *client;
  104. struct js_event event;
  105. switch (type) {
  106. case EV_KEY:
  107. if (code < BTN_MISC || value == 2)
  108. return;
  109. event.type = JS_EVENT_BUTTON;
  110. event.number = joydev->keymap[code - BTN_MISC];
  111. event.value = value;
  112. break;
  113. case EV_ABS:
  114. event.type = JS_EVENT_AXIS;
  115. event.number = joydev->absmap[code];
  116. event.value = joydev_correct(value,
  117. &joydev->corr[event.number]);
  118. if (event.value == joydev->abs[event.number])
  119. return;
  120. joydev->abs[event.number] = event.value;
  121. break;
  122. default:
  123. return;
  124. }
  125. event.time = jiffies_to_msecs(jiffies);
  126. rcu_read_lock();
  127. list_for_each_entry_rcu(client, &joydev->client_list, node)
  128. joydev_pass_event(client, &event);
  129. rcu_read_unlock();
  130. wake_up_interruptible(&joydev->wait);
  131. }
  132. static int joydev_fasync(int fd, struct file *file, int on)
  133. {
  134. struct joydev_client *client = file->private_data;
  135. return fasync_helper(fd, file, on, &client->fasync);
  136. }
  137. static void joydev_free(struct device *dev)
  138. {
  139. struct joydev *joydev = container_of(dev, struct joydev, dev);
  140. input_put_device(joydev->handle.dev);
  141. kfree(joydev);
  142. }
  143. static void joydev_attach_client(struct joydev *joydev,
  144. struct joydev_client *client)
  145. {
  146. spin_lock(&joydev->client_lock);
  147. list_add_tail_rcu(&client->node, &joydev->client_list);
  148. spin_unlock(&joydev->client_lock);
  149. }
  150. static void joydev_detach_client(struct joydev *joydev,
  151. struct joydev_client *client)
  152. {
  153. spin_lock(&joydev->client_lock);
  154. list_del_rcu(&client->node);
  155. spin_unlock(&joydev->client_lock);
  156. synchronize_rcu();
  157. }
  158. static int joydev_open_device(struct joydev *joydev)
  159. {
  160. int retval;
  161. retval = mutex_lock_interruptible(&joydev->mutex);
  162. if (retval)
  163. return retval;
  164. if (!joydev->exist)
  165. retval = -ENODEV;
  166. else if (!joydev->open++) {
  167. retval = input_open_device(&joydev->handle);
  168. if (retval)
  169. joydev->open--;
  170. }
  171. mutex_unlock(&joydev->mutex);
  172. return retval;
  173. }
  174. static void joydev_close_device(struct joydev *joydev)
  175. {
  176. mutex_lock(&joydev->mutex);
  177. if (joydev->exist && !--joydev->open)
  178. input_close_device(&joydev->handle);
  179. mutex_unlock(&joydev->mutex);
  180. }
  181. /*
  182. * Wake up users waiting for IO so they can disconnect from
  183. * dead device.
  184. */
  185. static void joydev_hangup(struct joydev *joydev)
  186. {
  187. struct joydev_client *client;
  188. spin_lock(&joydev->client_lock);
  189. list_for_each_entry(client, &joydev->client_list, node)
  190. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  191. spin_unlock(&joydev->client_lock);
  192. wake_up_interruptible(&joydev->wait);
  193. }
  194. static int joydev_release(struct inode *inode, struct file *file)
  195. {
  196. struct joydev_client *client = file->private_data;
  197. struct joydev *joydev = client->joydev;
  198. joydev_detach_client(joydev, client);
  199. kfree(client);
  200. joydev_close_device(joydev);
  201. return 0;
  202. }
  203. static int joydev_open(struct inode *inode, struct file *file)
  204. {
  205. struct joydev *joydev =
  206. container_of(inode->i_cdev, struct joydev, cdev);
  207. struct joydev_client *client;
  208. int error;
  209. client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
  210. if (!client)
  211. return -ENOMEM;
  212. spin_lock_init(&client->buffer_lock);
  213. client->joydev = joydev;
  214. joydev_attach_client(joydev, client);
  215. error = joydev_open_device(joydev);
  216. if (error)
  217. goto err_free_client;
  218. file->private_data = client;
  219. nonseekable_open(inode, file);
  220. return 0;
  221. err_free_client:
  222. joydev_detach_client(joydev, client);
  223. kfree(client);
  224. return error;
  225. }
  226. static int joydev_generate_startup_event(struct joydev_client *client,
  227. struct input_dev *input,
  228. struct js_event *event)
  229. {
  230. struct joydev *joydev = client->joydev;
  231. int have_event;
  232. spin_lock_irq(&client->buffer_lock);
  233. have_event = client->startup < joydev->nabs + joydev->nkey;
  234. if (have_event) {
  235. event->time = jiffies_to_msecs(jiffies);
  236. if (client->startup < joydev->nkey) {
  237. event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
  238. event->number = client->startup;
  239. event->value = !!test_bit(joydev->keypam[event->number],
  240. input->key);
  241. } else {
  242. event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
  243. event->number = client->startup - joydev->nkey;
  244. event->value = joydev->abs[event->number];
  245. }
  246. client->startup++;
  247. }
  248. spin_unlock_irq(&client->buffer_lock);
  249. return have_event;
  250. }
  251. static int joydev_fetch_next_event(struct joydev_client *client,
  252. struct js_event *event)
  253. {
  254. int have_event;
  255. spin_lock_irq(&client->buffer_lock);
  256. have_event = client->head != client->tail;
  257. if (have_event) {
  258. *event = client->buffer[client->tail++];
  259. client->tail &= JOYDEV_BUFFER_SIZE - 1;
  260. }
  261. spin_unlock_irq(&client->buffer_lock);
  262. return have_event;
  263. }
  264. /*
  265. * Old joystick interface
  266. */
  267. static ssize_t joydev_0x_read(struct joydev_client *client,
  268. struct input_dev *input,
  269. char __user *buf)
  270. {
  271. struct joydev *joydev = client->joydev;
  272. struct JS_DATA_TYPE data;
  273. int i;
  274. spin_lock_irq(&input->event_lock);
  275. /*
  276. * Get device state
  277. */
  278. for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
  279. data.buttons |=
  280. test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
  281. data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
  282. data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
  283. /*
  284. * Reset reader's event queue
  285. */
  286. spin_lock(&client->buffer_lock);
  287. client->startup = 0;
  288. client->tail = client->head;
  289. spin_unlock(&client->buffer_lock);
  290. spin_unlock_irq(&input->event_lock);
  291. if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
  292. return -EFAULT;
  293. return sizeof(struct JS_DATA_TYPE);
  294. }
  295. static inline int joydev_data_pending(struct joydev_client *client)
  296. {
  297. struct joydev *joydev = client->joydev;
  298. return client->startup < joydev->nabs + joydev->nkey ||
  299. client->head != client->tail;
  300. }
  301. static ssize_t joydev_read(struct file *file, char __user *buf,
  302. size_t count, loff_t *ppos)
  303. {
  304. struct joydev_client *client = file->private_data;
  305. struct joydev *joydev = client->joydev;
  306. struct input_dev *input = joydev->handle.dev;
  307. struct js_event event;
  308. int retval;
  309. if (!joydev->exist)
  310. return -ENODEV;
  311. if (count < sizeof(struct js_event))
  312. return -EINVAL;
  313. if (count == sizeof(struct JS_DATA_TYPE))
  314. return joydev_0x_read(client, input, buf);
  315. if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
  316. return -EAGAIN;
  317. retval = wait_event_interruptible(joydev->wait,
  318. !joydev->exist || joydev_data_pending(client));
  319. if (retval)
  320. return retval;
  321. if (!joydev->exist)
  322. return -ENODEV;
  323. while (retval + sizeof(struct js_event) <= count &&
  324. joydev_generate_startup_event(client, input, &event)) {
  325. if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
  326. return -EFAULT;
  327. retval += sizeof(struct js_event);
  328. }
  329. while (retval + sizeof(struct js_event) <= count &&
  330. joydev_fetch_next_event(client, &event)) {
  331. if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
  332. return -EFAULT;
  333. retval += sizeof(struct js_event);
  334. }
  335. return retval;
  336. }
  337. /* No kernel lock - fine */
  338. static unsigned int joydev_poll(struct file *file, poll_table *wait)
  339. {
  340. struct joydev_client *client = file->private_data;
  341. struct joydev *joydev = client->joydev;
  342. poll_wait(file, &joydev->wait, wait);
  343. return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
  344. (joydev->exist ? 0 : (POLLHUP | POLLERR));
  345. }
  346. static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
  347. void __user *argp, size_t len)
  348. {
  349. __u8 *abspam;
  350. int i;
  351. int retval = 0;
  352. len = min(len, sizeof(joydev->abspam));
  353. /* Validate the map. */
  354. abspam = kmalloc(len, GFP_KERNEL);
  355. if (!abspam)
  356. return -ENOMEM;
  357. if (copy_from_user(abspam, argp, len)) {
  358. retval = -EFAULT;
  359. goto out;
  360. }
  361. for (i = 0; i < joydev->nabs; i++) {
  362. if (abspam[i] > ABS_MAX) {
  363. retval = -EINVAL;
  364. goto out;
  365. }
  366. }
  367. memcpy(joydev->abspam, abspam, len);
  368. for (i = 0; i < joydev->nabs; i++)
  369. joydev->absmap[joydev->abspam[i]] = i;
  370. out:
  371. kfree(abspam);
  372. return retval;
  373. }
  374. static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
  375. void __user *argp, size_t len)
  376. {
  377. __u16 *keypam;
  378. int i;
  379. int retval = 0;
  380. len = min(len, sizeof(joydev->keypam));
  381. /* Validate the map. */
  382. keypam = kmalloc(len, GFP_KERNEL);
  383. if (!keypam)
  384. return -ENOMEM;
  385. if (copy_from_user(keypam, argp, len)) {
  386. retval = -EFAULT;
  387. goto out;
  388. }
  389. for (i = 0; i < joydev->nkey; i++) {
  390. if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
  391. retval = -EINVAL;
  392. goto out;
  393. }
  394. }
  395. memcpy(joydev->keypam, keypam, len);
  396. for (i = 0; i < joydev->nkey; i++)
  397. joydev->keymap[keypam[i] - BTN_MISC] = i;
  398. out:
  399. kfree(keypam);
  400. return retval;
  401. }
  402. static int joydev_ioctl_common(struct joydev *joydev,
  403. unsigned int cmd, void __user *argp)
  404. {
  405. struct input_dev *dev = joydev->handle.dev;
  406. size_t len;
  407. int i;
  408. const char *name;
  409. /* Process fixed-sized commands. */
  410. switch (cmd) {
  411. case JS_SET_CAL:
  412. return copy_from_user(&joydev->glue.JS_CORR, argp,
  413. sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
  414. case JS_GET_CAL:
  415. return copy_to_user(argp, &joydev->glue.JS_CORR,
  416. sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
  417. case JS_SET_TIMEOUT:
  418. return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
  419. case JS_GET_TIMEOUT:
  420. return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
  421. case JSIOCGVERSION:
  422. return put_user(JS_VERSION, (__u32 __user *) argp);
  423. case JSIOCGAXES:
  424. return put_user(joydev->nabs, (__u8 __user *) argp);
  425. case JSIOCGBUTTONS:
  426. return put_user(joydev->nkey, (__u8 __user *) argp);
  427. case JSIOCSCORR:
  428. if (copy_from_user(joydev->corr, argp,
  429. sizeof(joydev->corr[0]) * joydev->nabs))
  430. return -EFAULT;
  431. for (i = 0; i < joydev->nabs; i++) {
  432. int val = input_abs_get_val(dev, joydev->abspam[i]);
  433. joydev->abs[i] = joydev_correct(val, &joydev->corr[i]);
  434. }
  435. return 0;
  436. case JSIOCGCORR:
  437. return copy_to_user(argp, joydev->corr,
  438. sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
  439. }
  440. /*
  441. * Process variable-sized commands (the axis and button map commands
  442. * are considered variable-sized to decouple them from the values of
  443. * ABS_MAX and KEY_MAX).
  444. */
  445. switch (cmd & ~IOCSIZE_MASK) {
  446. case (JSIOCSAXMAP & ~IOCSIZE_MASK):
  447. return joydev_handle_JSIOCSAXMAP(joydev, argp, _IOC_SIZE(cmd));
  448. case (JSIOCGAXMAP & ~IOCSIZE_MASK):
  449. len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam));
  450. return copy_to_user(argp, joydev->abspam, len) ? -EFAULT : len;
  451. case (JSIOCSBTNMAP & ~IOCSIZE_MASK):
  452. return joydev_handle_JSIOCSBTNMAP(joydev, argp, _IOC_SIZE(cmd));
  453. case (JSIOCGBTNMAP & ~IOCSIZE_MASK):
  454. len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam));
  455. return copy_to_user(argp, joydev->keypam, len) ? -EFAULT : len;
  456. case JSIOCGNAME(0):
  457. name = dev->name;
  458. if (!name)
  459. return 0;
  460. len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1);
  461. return copy_to_user(argp, name, len) ? -EFAULT : len;
  462. }
  463. return -EINVAL;
  464. }
  465. #ifdef CONFIG_COMPAT
  466. static long joydev_compat_ioctl(struct file *file,
  467. unsigned int cmd, unsigned long arg)
  468. {
  469. struct joydev_client *client = file->private_data;
  470. struct joydev *joydev = client->joydev;
  471. void __user *argp = (void __user *)arg;
  472. s32 tmp32;
  473. struct JS_DATA_SAVE_TYPE_32 ds32;
  474. int retval;
  475. retval = mutex_lock_interruptible(&joydev->mutex);
  476. if (retval)
  477. return retval;
  478. if (!joydev->exist) {
  479. retval = -ENODEV;
  480. goto out;
  481. }
  482. switch (cmd) {
  483. case JS_SET_TIMELIMIT:
  484. retval = get_user(tmp32, (s32 __user *) arg);
  485. if (retval == 0)
  486. joydev->glue.JS_TIMELIMIT = tmp32;
  487. break;
  488. case JS_GET_TIMELIMIT:
  489. tmp32 = joydev->glue.JS_TIMELIMIT;
  490. retval = put_user(tmp32, (s32 __user *) arg);
  491. break;
  492. case JS_SET_ALL:
  493. retval = copy_from_user(&ds32, argp,
  494. sizeof(ds32)) ? -EFAULT : 0;
  495. if (retval == 0) {
  496. joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
  497. joydev->glue.BUSY = ds32.BUSY;
  498. joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
  499. joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
  500. joydev->glue.JS_SAVE = ds32.JS_SAVE;
  501. joydev->glue.JS_CORR = ds32.JS_CORR;
  502. }
  503. break;
  504. case JS_GET_ALL:
  505. ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
  506. ds32.BUSY = joydev->glue.BUSY;
  507. ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
  508. ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
  509. ds32.JS_SAVE = joydev->glue.JS_SAVE;
  510. ds32.JS_CORR = joydev->glue.JS_CORR;
  511. retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
  512. break;
  513. default:
  514. retval = joydev_ioctl_common(joydev, cmd, argp);
  515. break;
  516. }
  517. out:
  518. mutex_unlock(&joydev->mutex);
  519. return retval;
  520. }
  521. #endif /* CONFIG_COMPAT */
  522. static long joydev_ioctl(struct file *file,
  523. unsigned int cmd, unsigned long arg)
  524. {
  525. struct joydev_client *client = file->private_data;
  526. struct joydev *joydev = client->joydev;
  527. void __user *argp = (void __user *)arg;
  528. int retval;
  529. retval = mutex_lock_interruptible(&joydev->mutex);
  530. if (retval)
  531. return retval;
  532. if (!joydev->exist) {
  533. retval = -ENODEV;
  534. goto out;
  535. }
  536. switch (cmd) {
  537. case JS_SET_TIMELIMIT:
  538. retval = get_user(joydev->glue.JS_TIMELIMIT,
  539. (long __user *) arg);
  540. break;
  541. case JS_GET_TIMELIMIT:
  542. retval = put_user(joydev->glue.JS_TIMELIMIT,
  543. (long __user *) arg);
  544. break;
  545. case JS_SET_ALL:
  546. retval = copy_from_user(&joydev->glue, argp,
  547. sizeof(joydev->glue)) ? -EFAULT : 0;
  548. break;
  549. case JS_GET_ALL:
  550. retval = copy_to_user(argp, &joydev->glue,
  551. sizeof(joydev->glue)) ? -EFAULT : 0;
  552. break;
  553. default:
  554. retval = joydev_ioctl_common(joydev, cmd, argp);
  555. break;
  556. }
  557. out:
  558. mutex_unlock(&joydev->mutex);
  559. return retval;
  560. }
  561. static const struct file_operations joydev_fops = {
  562. .owner = THIS_MODULE,
  563. .read = joydev_read,
  564. .poll = joydev_poll,
  565. .open = joydev_open,
  566. .release = joydev_release,
  567. .unlocked_ioctl = joydev_ioctl,
  568. #ifdef CONFIG_COMPAT
  569. .compat_ioctl = joydev_compat_ioctl,
  570. #endif
  571. .fasync = joydev_fasync,
  572. .llseek = no_llseek,
  573. };
  574. /*
  575. * Mark device non-existent. This disables writes, ioctls and
  576. * prevents new users from opening the device. Already posted
  577. * blocking reads will stay, however new ones will fail.
  578. */
  579. static void joydev_mark_dead(struct joydev *joydev)
  580. {
  581. mutex_lock(&joydev->mutex);
  582. joydev->exist = false;
  583. mutex_unlock(&joydev->mutex);
  584. }
  585. static void joydev_cleanup(struct joydev *joydev)
  586. {
  587. struct input_handle *handle = &joydev->handle;
  588. joydev_mark_dead(joydev);
  589. joydev_hangup(joydev);
  590. cdev_del(&joydev->cdev);
  591. /* joydev is marked dead so no one else accesses joydev->open */
  592. if (joydev->open)
  593. input_close_device(handle);
  594. }
  595. static bool joydev_dev_is_absolute_mouse(struct input_dev *dev)
  596. {
  597. DECLARE_BITMAP(jd_scratch, KEY_CNT);
  598. BUILD_BUG_ON(ABS_CNT > KEY_CNT || EV_CNT > KEY_CNT);
  599. /*
  600. * Virtualization (VMware, etc) and remote management (HP
  601. * ILO2) solutions use absolute coordinates for their virtual
  602. * pointing devices so that there is one-to-one relationship
  603. * between pointer position on the host screen and virtual
  604. * guest screen, and so their mice use ABS_X, ABS_Y and 3
  605. * primary button events. This clashes with what joydev
  606. * considers to be joysticks (a device with at minimum ABS_X
  607. * axis).
  608. *
  609. * Here we are trying to separate absolute mice from
  610. * joysticks. A device is, for joystick detection purposes,
  611. * considered to be an absolute mouse if the following is
  612. * true:
  613. *
  614. * 1) Event types are exactly EV_ABS, EV_KEY and EV_SYN.
  615. * 2) Absolute events are exactly ABS_X and ABS_Y.
  616. * 3) Keys are exactly BTN_LEFT, BTN_RIGHT and BTN_MIDDLE.
  617. * 4) Device is not on "Amiga" bus.
  618. */
  619. bitmap_zero(jd_scratch, EV_CNT);
  620. __set_bit(EV_ABS, jd_scratch);
  621. __set_bit(EV_KEY, jd_scratch);
  622. __set_bit(EV_SYN, jd_scratch);
  623. if (!bitmap_equal(jd_scratch, dev->evbit, EV_CNT))
  624. return false;
  625. bitmap_zero(jd_scratch, ABS_CNT);
  626. __set_bit(ABS_X, jd_scratch);
  627. __set_bit(ABS_Y, jd_scratch);
  628. if (!bitmap_equal(dev->absbit, jd_scratch, ABS_CNT))
  629. return false;
  630. bitmap_zero(jd_scratch, KEY_CNT);
  631. __set_bit(BTN_LEFT, jd_scratch);
  632. __set_bit(BTN_RIGHT, jd_scratch);
  633. __set_bit(BTN_MIDDLE, jd_scratch);
  634. if (!bitmap_equal(dev->keybit, jd_scratch, KEY_CNT))
  635. return false;
  636. /*
  637. * Amiga joystick (amijoy) historically uses left/middle/right
  638. * button events.
  639. */
  640. if (dev->id.bustype == BUS_AMIGA)
  641. return false;
  642. return true;
  643. }
  644. static bool joydev_match(struct input_handler *handler, struct input_dev *dev)
  645. {
  646. /* Avoid touchpads and touchscreens */
  647. if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_TOUCH, dev->keybit))
  648. return false;
  649. /* Avoid tablets, digitisers and similar devices */
  650. if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_DIGI, dev->keybit))
  651. return false;
  652. /* Avoid absolute mice */
  653. if (joydev_dev_is_absolute_mouse(dev))
  654. return false;
  655. return true;
  656. }
  657. static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
  658. const struct input_device_id *id)
  659. {
  660. struct joydev *joydev;
  661. int i, j, t, minor, dev_no;
  662. int error;
  663. minor = input_get_new_minor(JOYDEV_MINOR_BASE, JOYDEV_MINORS, true);
  664. if (minor < 0) {
  665. error = minor;
  666. pr_err("failed to reserve new minor: %d\n", error);
  667. return error;
  668. }
  669. joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
  670. if (!joydev) {
  671. error = -ENOMEM;
  672. goto err_free_minor;
  673. }
  674. INIT_LIST_HEAD(&joydev->client_list);
  675. spin_lock_init(&joydev->client_lock);
  676. mutex_init(&joydev->mutex);
  677. init_waitqueue_head(&joydev->wait);
  678. joydev->exist = true;
  679. dev_no = minor;
  680. /* Normalize device number if it falls into legacy range */
  681. if (dev_no < JOYDEV_MINOR_BASE + JOYDEV_MINORS)
  682. dev_no -= JOYDEV_MINOR_BASE;
  683. dev_set_name(&joydev->dev, "js%d", dev_no);
  684. joydev->handle.dev = input_get_device(dev);
  685. joydev->handle.name = dev_name(&joydev->dev);
  686. joydev->handle.handler = handler;
  687. joydev->handle.private = joydev;
  688. for (i = 0; i < ABS_CNT; i++)
  689. if (test_bit(i, dev->absbit)) {
  690. joydev->absmap[i] = joydev->nabs;
  691. joydev->abspam[joydev->nabs] = i;
  692. joydev->nabs++;
  693. }
  694. for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
  695. if (test_bit(i + BTN_MISC, dev->keybit)) {
  696. joydev->keymap[i] = joydev->nkey;
  697. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  698. joydev->nkey++;
  699. }
  700. for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
  701. if (test_bit(i + BTN_MISC, dev->keybit)) {
  702. joydev->keymap[i] = joydev->nkey;
  703. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  704. joydev->nkey++;
  705. }
  706. for (i = 0; i < joydev->nabs; i++) {
  707. j = joydev->abspam[i];
  708. if (input_abs_get_max(dev, j) == input_abs_get_min(dev, j)) {
  709. joydev->corr[i].type = JS_CORR_NONE;
  710. joydev->abs[i] = input_abs_get_val(dev, j);
  711. continue;
  712. }
  713. joydev->corr[i].type = JS_CORR_BROKEN;
  714. joydev->corr[i].prec = input_abs_get_fuzz(dev, j);
  715. t = (input_abs_get_max(dev, j) + input_abs_get_min(dev, j)) / 2;
  716. joydev->corr[i].coef[0] = t - input_abs_get_flat(dev, j);
  717. joydev->corr[i].coef[1] = t + input_abs_get_flat(dev, j);
  718. t = (input_abs_get_max(dev, j) - input_abs_get_min(dev, j)) / 2
  719. - 2 * input_abs_get_flat(dev, j);
  720. if (t) {
  721. joydev->corr[i].coef[2] = (1 << 29) / t;
  722. joydev->corr[i].coef[3] = (1 << 29) / t;
  723. joydev->abs[i] =
  724. joydev_correct(input_abs_get_val(dev, j),
  725. joydev->corr + i);
  726. }
  727. }
  728. joydev->dev.devt = MKDEV(INPUT_MAJOR, minor);
  729. joydev->dev.class = &input_class;
  730. joydev->dev.parent = &dev->dev;
  731. joydev->dev.release = joydev_free;
  732. device_initialize(&joydev->dev);
  733. error = input_register_handle(&joydev->handle);
  734. if (error)
  735. goto err_free_joydev;
  736. cdev_init(&joydev->cdev, &joydev_fops);
  737. joydev->cdev.kobj.parent = &joydev->dev.kobj;
  738. error = cdev_add(&joydev->cdev, joydev->dev.devt, 1);
  739. if (error)
  740. goto err_unregister_handle;
  741. error = device_add(&joydev->dev);
  742. if (error)
  743. goto err_cleanup_joydev;
  744. return 0;
  745. err_cleanup_joydev:
  746. joydev_cleanup(joydev);
  747. err_unregister_handle:
  748. input_unregister_handle(&joydev->handle);
  749. err_free_joydev:
  750. put_device(&joydev->dev);
  751. err_free_minor:
  752. input_free_minor(minor);
  753. return error;
  754. }
  755. static void joydev_disconnect(struct input_handle *handle)
  756. {
  757. struct joydev *joydev = handle->private;
  758. device_del(&joydev->dev);
  759. joydev_cleanup(joydev);
  760. input_free_minor(MINOR(joydev->dev.devt));
  761. input_unregister_handle(handle);
  762. put_device(&joydev->dev);
  763. }
  764. static const struct input_device_id joydev_ids[] = {
  765. {
  766. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  767. INPUT_DEVICE_ID_MATCH_ABSBIT,
  768. .evbit = { BIT_MASK(EV_ABS) },
  769. .absbit = { BIT_MASK(ABS_X) },
  770. },
  771. {
  772. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  773. INPUT_DEVICE_ID_MATCH_ABSBIT,
  774. .evbit = { BIT_MASK(EV_ABS) },
  775. .absbit = { BIT_MASK(ABS_WHEEL) },
  776. },
  777. {
  778. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  779. INPUT_DEVICE_ID_MATCH_ABSBIT,
  780. .evbit = { BIT_MASK(EV_ABS) },
  781. .absbit = { BIT_MASK(ABS_THROTTLE) },
  782. },
  783. {
  784. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  785. INPUT_DEVICE_ID_MATCH_KEYBIT,
  786. .evbit = { BIT_MASK(EV_KEY) },
  787. .keybit = {[BIT_WORD(BTN_JOYSTICK)] = BIT_MASK(BTN_JOYSTICK) },
  788. },
  789. {
  790. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  791. INPUT_DEVICE_ID_MATCH_KEYBIT,
  792. .evbit = { BIT_MASK(EV_KEY) },
  793. .keybit = { [BIT_WORD(BTN_GAMEPAD)] = BIT_MASK(BTN_GAMEPAD) },
  794. },
  795. {
  796. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  797. INPUT_DEVICE_ID_MATCH_KEYBIT,
  798. .evbit = { BIT_MASK(EV_KEY) },
  799. .keybit = { [BIT_WORD(BTN_TRIGGER_HAPPY)] = BIT_MASK(BTN_TRIGGER_HAPPY) },
  800. },
  801. { } /* Terminating entry */
  802. };
  803. MODULE_DEVICE_TABLE(input, joydev_ids);
  804. static struct input_handler joydev_handler = {
  805. .event = joydev_event,
  806. .match = joydev_match,
  807. .connect = joydev_connect,
  808. .disconnect = joydev_disconnect,
  809. .legacy_minors = true,
  810. .minor = JOYDEV_MINOR_BASE,
  811. .name = "joydev",
  812. .id_table = joydev_ids,
  813. };
  814. static int __init joydev_init(void)
  815. {
  816. return input_register_handler(&joydev_handler);
  817. }
  818. static void __exit joydev_exit(void)
  819. {
  820. input_unregister_handler(&joydev_handler);
  821. }
  822. module_init(joydev_init);
  823. module_exit(joydev_exit);