joydev.c 24 KB

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