joydev.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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 = memdup_user(argp, len);
  355. if (IS_ERR(abspam))
  356. return PTR_ERR(abspam);
  357. for (i = 0; i < joydev->nabs; i++) {
  358. if (abspam[i] > ABS_MAX) {
  359. retval = -EINVAL;
  360. goto out;
  361. }
  362. }
  363. memcpy(joydev->abspam, abspam, len);
  364. for (i = 0; i < joydev->nabs; i++)
  365. joydev->absmap[joydev->abspam[i]] = i;
  366. out:
  367. kfree(abspam);
  368. return retval;
  369. }
  370. static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
  371. void __user *argp, size_t len)
  372. {
  373. __u16 *keypam;
  374. int i;
  375. int retval = 0;
  376. len = min(len, sizeof(joydev->keypam));
  377. /* Validate the map. */
  378. keypam = memdup_user(argp, len);
  379. if (IS_ERR(keypam))
  380. return PTR_ERR(keypam);
  381. for (i = 0; i < joydev->nkey; i++) {
  382. if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
  383. retval = -EINVAL;
  384. goto out;
  385. }
  386. }
  387. memcpy(joydev->keypam, keypam, len);
  388. for (i = 0; i < joydev->nkey; i++)
  389. joydev->keymap[keypam[i] - BTN_MISC] = i;
  390. out:
  391. kfree(keypam);
  392. return retval;
  393. }
  394. static int joydev_ioctl_common(struct joydev *joydev,
  395. unsigned int cmd, void __user *argp)
  396. {
  397. struct input_dev *dev = joydev->handle.dev;
  398. size_t len;
  399. int i;
  400. const char *name;
  401. /* Process fixed-sized commands. */
  402. switch (cmd) {
  403. case JS_SET_CAL:
  404. return copy_from_user(&joydev->glue.JS_CORR, argp,
  405. sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
  406. case JS_GET_CAL:
  407. return copy_to_user(argp, &joydev->glue.JS_CORR,
  408. sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
  409. case JS_SET_TIMEOUT:
  410. return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
  411. case JS_GET_TIMEOUT:
  412. return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
  413. case JSIOCGVERSION:
  414. return put_user(JS_VERSION, (__u32 __user *) argp);
  415. case JSIOCGAXES:
  416. return put_user(joydev->nabs, (__u8 __user *) argp);
  417. case JSIOCGBUTTONS:
  418. return put_user(joydev->nkey, (__u8 __user *) argp);
  419. case JSIOCSCORR:
  420. if (copy_from_user(joydev->corr, argp,
  421. sizeof(joydev->corr[0]) * joydev->nabs))
  422. return -EFAULT;
  423. for (i = 0; i < joydev->nabs; i++) {
  424. int val = input_abs_get_val(dev, joydev->abspam[i]);
  425. joydev->abs[i] = joydev_correct(val, &joydev->corr[i]);
  426. }
  427. return 0;
  428. case JSIOCGCORR:
  429. return copy_to_user(argp, joydev->corr,
  430. sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
  431. }
  432. /*
  433. * Process variable-sized commands (the axis and button map commands
  434. * are considered variable-sized to decouple them from the values of
  435. * ABS_MAX and KEY_MAX).
  436. */
  437. switch (cmd & ~IOCSIZE_MASK) {
  438. case (JSIOCSAXMAP & ~IOCSIZE_MASK):
  439. return joydev_handle_JSIOCSAXMAP(joydev, argp, _IOC_SIZE(cmd));
  440. case (JSIOCGAXMAP & ~IOCSIZE_MASK):
  441. len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam));
  442. return copy_to_user(argp, joydev->abspam, len) ? -EFAULT : len;
  443. case (JSIOCSBTNMAP & ~IOCSIZE_MASK):
  444. return joydev_handle_JSIOCSBTNMAP(joydev, argp, _IOC_SIZE(cmd));
  445. case (JSIOCGBTNMAP & ~IOCSIZE_MASK):
  446. len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam));
  447. return copy_to_user(argp, joydev->keypam, len) ? -EFAULT : len;
  448. case JSIOCGNAME(0):
  449. name = dev->name;
  450. if (!name)
  451. return 0;
  452. len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1);
  453. return copy_to_user(argp, name, len) ? -EFAULT : len;
  454. }
  455. return -EINVAL;
  456. }
  457. #ifdef CONFIG_COMPAT
  458. static long joydev_compat_ioctl(struct file *file,
  459. unsigned int cmd, unsigned long arg)
  460. {
  461. struct joydev_client *client = file->private_data;
  462. struct joydev *joydev = client->joydev;
  463. void __user *argp = (void __user *)arg;
  464. s32 tmp32;
  465. struct JS_DATA_SAVE_TYPE_32 ds32;
  466. int retval;
  467. retval = mutex_lock_interruptible(&joydev->mutex);
  468. if (retval)
  469. return retval;
  470. if (!joydev->exist) {
  471. retval = -ENODEV;
  472. goto out;
  473. }
  474. switch (cmd) {
  475. case JS_SET_TIMELIMIT:
  476. retval = get_user(tmp32, (s32 __user *) arg);
  477. if (retval == 0)
  478. joydev->glue.JS_TIMELIMIT = tmp32;
  479. break;
  480. case JS_GET_TIMELIMIT:
  481. tmp32 = joydev->glue.JS_TIMELIMIT;
  482. retval = put_user(tmp32, (s32 __user *) arg);
  483. break;
  484. case JS_SET_ALL:
  485. retval = copy_from_user(&ds32, argp,
  486. sizeof(ds32)) ? -EFAULT : 0;
  487. if (retval == 0) {
  488. joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
  489. joydev->glue.BUSY = ds32.BUSY;
  490. joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
  491. joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
  492. joydev->glue.JS_SAVE = ds32.JS_SAVE;
  493. joydev->glue.JS_CORR = ds32.JS_CORR;
  494. }
  495. break;
  496. case JS_GET_ALL:
  497. ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
  498. ds32.BUSY = joydev->glue.BUSY;
  499. ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
  500. ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
  501. ds32.JS_SAVE = joydev->glue.JS_SAVE;
  502. ds32.JS_CORR = joydev->glue.JS_CORR;
  503. retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
  504. break;
  505. default:
  506. retval = joydev_ioctl_common(joydev, cmd, argp);
  507. break;
  508. }
  509. out:
  510. mutex_unlock(&joydev->mutex);
  511. return retval;
  512. }
  513. #endif /* CONFIG_COMPAT */
  514. static long joydev_ioctl(struct file *file,
  515. unsigned int cmd, unsigned long arg)
  516. {
  517. struct joydev_client *client = file->private_data;
  518. struct joydev *joydev = client->joydev;
  519. void __user *argp = (void __user *)arg;
  520. int retval;
  521. retval = mutex_lock_interruptible(&joydev->mutex);
  522. if (retval)
  523. return retval;
  524. if (!joydev->exist) {
  525. retval = -ENODEV;
  526. goto out;
  527. }
  528. switch (cmd) {
  529. case JS_SET_TIMELIMIT:
  530. retval = get_user(joydev->glue.JS_TIMELIMIT,
  531. (long __user *) arg);
  532. break;
  533. case JS_GET_TIMELIMIT:
  534. retval = put_user(joydev->glue.JS_TIMELIMIT,
  535. (long __user *) arg);
  536. break;
  537. case JS_SET_ALL:
  538. retval = copy_from_user(&joydev->glue, argp,
  539. sizeof(joydev->glue)) ? -EFAULT : 0;
  540. break;
  541. case JS_GET_ALL:
  542. retval = copy_to_user(argp, &joydev->glue,
  543. sizeof(joydev->glue)) ? -EFAULT : 0;
  544. break;
  545. default:
  546. retval = joydev_ioctl_common(joydev, cmd, argp);
  547. break;
  548. }
  549. out:
  550. mutex_unlock(&joydev->mutex);
  551. return retval;
  552. }
  553. static const struct file_operations joydev_fops = {
  554. .owner = THIS_MODULE,
  555. .read = joydev_read,
  556. .poll = joydev_poll,
  557. .open = joydev_open,
  558. .release = joydev_release,
  559. .unlocked_ioctl = joydev_ioctl,
  560. #ifdef CONFIG_COMPAT
  561. .compat_ioctl = joydev_compat_ioctl,
  562. #endif
  563. .fasync = joydev_fasync,
  564. .llseek = no_llseek,
  565. };
  566. /*
  567. * Mark device non-existent. This disables writes, ioctls and
  568. * prevents new users from opening the device. Already posted
  569. * blocking reads will stay, however new ones will fail.
  570. */
  571. static void joydev_mark_dead(struct joydev *joydev)
  572. {
  573. mutex_lock(&joydev->mutex);
  574. joydev->exist = false;
  575. mutex_unlock(&joydev->mutex);
  576. }
  577. static void joydev_cleanup(struct joydev *joydev)
  578. {
  579. struct input_handle *handle = &joydev->handle;
  580. joydev_mark_dead(joydev);
  581. joydev_hangup(joydev);
  582. cdev_del(&joydev->cdev);
  583. /* joydev is marked dead so no one else accesses joydev->open */
  584. if (joydev->open)
  585. input_close_device(handle);
  586. }
  587. static bool joydev_dev_is_absolute_mouse(struct input_dev *dev)
  588. {
  589. DECLARE_BITMAP(jd_scratch, KEY_CNT);
  590. BUILD_BUG_ON(ABS_CNT > KEY_CNT || EV_CNT > KEY_CNT);
  591. /*
  592. * Virtualization (VMware, etc) and remote management (HP
  593. * ILO2) solutions use absolute coordinates for their virtual
  594. * pointing devices so that there is one-to-one relationship
  595. * between pointer position on the host screen and virtual
  596. * guest screen, and so their mice use ABS_X, ABS_Y and 3
  597. * primary button events. This clashes with what joydev
  598. * considers to be joysticks (a device with at minimum ABS_X
  599. * axis).
  600. *
  601. * Here we are trying to separate absolute mice from
  602. * joysticks. A device is, for joystick detection purposes,
  603. * considered to be an absolute mouse if the following is
  604. * true:
  605. *
  606. * 1) Event types are exactly EV_ABS, EV_KEY and EV_SYN.
  607. * 2) Absolute events are exactly ABS_X and ABS_Y.
  608. * 3) Keys are exactly BTN_LEFT, BTN_RIGHT and BTN_MIDDLE.
  609. * 4) Device is not on "Amiga" bus.
  610. */
  611. bitmap_zero(jd_scratch, EV_CNT);
  612. __set_bit(EV_ABS, jd_scratch);
  613. __set_bit(EV_KEY, jd_scratch);
  614. __set_bit(EV_SYN, jd_scratch);
  615. if (!bitmap_equal(jd_scratch, dev->evbit, EV_CNT))
  616. return false;
  617. bitmap_zero(jd_scratch, ABS_CNT);
  618. __set_bit(ABS_X, jd_scratch);
  619. __set_bit(ABS_Y, jd_scratch);
  620. if (!bitmap_equal(dev->absbit, jd_scratch, ABS_CNT))
  621. return false;
  622. bitmap_zero(jd_scratch, KEY_CNT);
  623. __set_bit(BTN_LEFT, jd_scratch);
  624. __set_bit(BTN_RIGHT, jd_scratch);
  625. __set_bit(BTN_MIDDLE, jd_scratch);
  626. if (!bitmap_equal(dev->keybit, jd_scratch, KEY_CNT))
  627. return false;
  628. /*
  629. * Amiga joystick (amijoy) historically uses left/middle/right
  630. * button events.
  631. */
  632. if (dev->id.bustype == BUS_AMIGA)
  633. return false;
  634. return true;
  635. }
  636. static bool joydev_match(struct input_handler *handler, struct input_dev *dev)
  637. {
  638. /* Avoid touchpads and touchscreens */
  639. if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_TOUCH, dev->keybit))
  640. return false;
  641. /* Avoid tablets, digitisers and similar devices */
  642. if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_DIGI, dev->keybit))
  643. return false;
  644. /* Avoid absolute mice */
  645. if (joydev_dev_is_absolute_mouse(dev))
  646. return false;
  647. return true;
  648. }
  649. static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
  650. const struct input_device_id *id)
  651. {
  652. struct joydev *joydev;
  653. int i, j, t, minor, dev_no;
  654. int error;
  655. minor = input_get_new_minor(JOYDEV_MINOR_BASE, JOYDEV_MINORS, true);
  656. if (minor < 0) {
  657. error = minor;
  658. pr_err("failed to reserve new minor: %d\n", error);
  659. return error;
  660. }
  661. joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
  662. if (!joydev) {
  663. error = -ENOMEM;
  664. goto err_free_minor;
  665. }
  666. INIT_LIST_HEAD(&joydev->client_list);
  667. spin_lock_init(&joydev->client_lock);
  668. mutex_init(&joydev->mutex);
  669. init_waitqueue_head(&joydev->wait);
  670. joydev->exist = true;
  671. dev_no = minor;
  672. /* Normalize device number if it falls into legacy range */
  673. if (dev_no < JOYDEV_MINOR_BASE + JOYDEV_MINORS)
  674. dev_no -= JOYDEV_MINOR_BASE;
  675. dev_set_name(&joydev->dev, "js%d", dev_no);
  676. joydev->handle.dev = input_get_device(dev);
  677. joydev->handle.name = dev_name(&joydev->dev);
  678. joydev->handle.handler = handler;
  679. joydev->handle.private = joydev;
  680. for_each_set_bit(i, dev->absbit, ABS_CNT) {
  681. joydev->absmap[i] = joydev->nabs;
  682. joydev->abspam[joydev->nabs] = i;
  683. joydev->nabs++;
  684. }
  685. for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
  686. if (test_bit(i + BTN_MISC, dev->keybit)) {
  687. joydev->keymap[i] = joydev->nkey;
  688. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  689. joydev->nkey++;
  690. }
  691. for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
  692. if (test_bit(i + BTN_MISC, dev->keybit)) {
  693. joydev->keymap[i] = joydev->nkey;
  694. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  695. joydev->nkey++;
  696. }
  697. for (i = 0; i < joydev->nabs; i++) {
  698. j = joydev->abspam[i];
  699. if (input_abs_get_max(dev, j) == input_abs_get_min(dev, j)) {
  700. joydev->corr[i].type = JS_CORR_NONE;
  701. joydev->abs[i] = input_abs_get_val(dev, j);
  702. continue;
  703. }
  704. joydev->corr[i].type = JS_CORR_BROKEN;
  705. joydev->corr[i].prec = input_abs_get_fuzz(dev, j);
  706. t = (input_abs_get_max(dev, j) + input_abs_get_min(dev, j)) / 2;
  707. joydev->corr[i].coef[0] = t - input_abs_get_flat(dev, j);
  708. joydev->corr[i].coef[1] = t + input_abs_get_flat(dev, j);
  709. t = (input_abs_get_max(dev, j) - input_abs_get_min(dev, j)) / 2
  710. - 2 * input_abs_get_flat(dev, j);
  711. if (t) {
  712. joydev->corr[i].coef[2] = (1 << 29) / t;
  713. joydev->corr[i].coef[3] = (1 << 29) / t;
  714. joydev->abs[i] =
  715. joydev_correct(input_abs_get_val(dev, j),
  716. joydev->corr + i);
  717. }
  718. }
  719. joydev->dev.devt = MKDEV(INPUT_MAJOR, minor);
  720. joydev->dev.class = &input_class;
  721. joydev->dev.parent = &dev->dev;
  722. joydev->dev.release = joydev_free;
  723. device_initialize(&joydev->dev);
  724. error = input_register_handle(&joydev->handle);
  725. if (error)
  726. goto err_free_joydev;
  727. cdev_init(&joydev->cdev, &joydev_fops);
  728. joydev->cdev.kobj.parent = &joydev->dev.kobj;
  729. error = cdev_add(&joydev->cdev, joydev->dev.devt, 1);
  730. if (error)
  731. goto err_unregister_handle;
  732. error = device_add(&joydev->dev);
  733. if (error)
  734. goto err_cleanup_joydev;
  735. return 0;
  736. err_cleanup_joydev:
  737. joydev_cleanup(joydev);
  738. err_unregister_handle:
  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. device_del(&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_WHEEL) },
  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_THROTTLE) },
  773. },
  774. {
  775. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  776. INPUT_DEVICE_ID_MATCH_KEYBIT,
  777. .evbit = { BIT_MASK(EV_KEY) },
  778. .keybit = {[BIT_WORD(BTN_JOYSTICK)] = BIT_MASK(BTN_JOYSTICK) },
  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_GAMEPAD)] = BIT_MASK(BTN_GAMEPAD) },
  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_TRIGGER_HAPPY)] = BIT_MASK(BTN_TRIGGER_HAPPY) },
  791. },
  792. { } /* Terminating entry */
  793. };
  794. MODULE_DEVICE_TABLE(input, joydev_ids);
  795. static struct input_handler joydev_handler = {
  796. .event = joydev_event,
  797. .match = joydev_match,
  798. .connect = joydev_connect,
  799. .disconnect = joydev_disconnect,
  800. .legacy_minors = true,
  801. .minor = JOYDEV_MINOR_BASE,
  802. .name = "joydev",
  803. .id_table = joydev_ids,
  804. };
  805. static int __init joydev_init(void)
  806. {
  807. return input_register_handler(&joydev_handler);
  808. }
  809. static void __exit joydev_exit(void)
  810. {
  811. input_unregister_handler(&joydev_handler);
  812. }
  813. module_init(joydev_init);
  814. module_exit(joydev_exit);