joydev.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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 __poll_t 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) ? (EPOLLIN | EPOLLRDNORM) : 0) |
  354. (joydev->exist ? 0 : (EPOLLHUP | EPOLLERR));
  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. /*
  597. * These codes are copied from from hid-ids.h, unfortunately there is no common
  598. * usb_ids/bt_ids.h header.
  599. */
  600. #define USB_VENDOR_ID_SONY 0x054c
  601. #define USB_DEVICE_ID_SONY_PS3_CONTROLLER 0x0268
  602. #define USB_DEVICE_ID_SONY_PS4_CONTROLLER 0x05c4
  603. #define USB_DEVICE_ID_SONY_PS4_CONTROLLER_2 0x09cc
  604. #define USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE 0x0ba0
  605. #define USB_VENDOR_ID_THQ 0x20d6
  606. #define USB_DEVICE_ID_THQ_PS3_UDRAW 0xcb17
  607. #define ACCEL_DEV(vnd, prd) \
  608. { \
  609. .flags = INPUT_DEVICE_ID_MATCH_VENDOR | \
  610. INPUT_DEVICE_ID_MATCH_PRODUCT | \
  611. INPUT_DEVICE_ID_MATCH_PROPBIT, \
  612. .vendor = (vnd), \
  613. .product = (prd), \
  614. .propbit = { BIT_MASK(INPUT_PROP_ACCELEROMETER) }, \
  615. }
  616. static const struct input_device_id joydev_blacklist[] = {
  617. /* Avoid touchpads and touchscreens */
  618. {
  619. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  620. INPUT_DEVICE_ID_MATCH_KEYBIT,
  621. .evbit = { BIT_MASK(EV_KEY) },
  622. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  623. },
  624. /* Avoid tablets, digitisers and similar devices */
  625. {
  626. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  627. INPUT_DEVICE_ID_MATCH_KEYBIT,
  628. .evbit = { BIT_MASK(EV_KEY) },
  629. .keybit = { [BIT_WORD(BTN_DIGI)] = BIT_MASK(BTN_DIGI) },
  630. },
  631. /* Disable accelerometers on composite devices */
  632. ACCEL_DEV(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
  633. ACCEL_DEV(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
  634. ACCEL_DEV(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
  635. ACCEL_DEV(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE),
  636. ACCEL_DEV(USB_VENDOR_ID_THQ, USB_DEVICE_ID_THQ_PS3_UDRAW),
  637. { /* sentinel */ }
  638. };
  639. static bool joydev_dev_is_blacklisted(struct input_dev *dev)
  640. {
  641. const struct input_device_id *id;
  642. for (id = joydev_blacklist; id->flags; id++) {
  643. if (input_match_device_id(dev, id)) {
  644. dev_dbg(&dev->dev,
  645. "joydev: blacklisting '%s'\n", dev->name);
  646. return true;
  647. }
  648. }
  649. return false;
  650. }
  651. static bool joydev_dev_is_absolute_mouse(struct input_dev *dev)
  652. {
  653. DECLARE_BITMAP(jd_scratch, KEY_CNT);
  654. BUILD_BUG_ON(ABS_CNT > KEY_CNT || EV_CNT > KEY_CNT);
  655. /*
  656. * Virtualization (VMware, etc) and remote management (HP
  657. * ILO2) solutions use absolute coordinates for their virtual
  658. * pointing devices so that there is one-to-one relationship
  659. * between pointer position on the host screen and virtual
  660. * guest screen, and so their mice use ABS_X, ABS_Y and 3
  661. * primary button events. This clashes with what joydev
  662. * considers to be joysticks (a device with at minimum ABS_X
  663. * axis).
  664. *
  665. * Here we are trying to separate absolute mice from
  666. * joysticks. A device is, for joystick detection purposes,
  667. * considered to be an absolute mouse if the following is
  668. * true:
  669. *
  670. * 1) Event types are exactly EV_ABS, EV_KEY and EV_SYN.
  671. * 2) Absolute events are exactly ABS_X and ABS_Y.
  672. * 3) Keys are exactly BTN_LEFT, BTN_RIGHT and BTN_MIDDLE.
  673. * 4) Device is not on "Amiga" bus.
  674. */
  675. bitmap_zero(jd_scratch, EV_CNT);
  676. __set_bit(EV_ABS, jd_scratch);
  677. __set_bit(EV_KEY, jd_scratch);
  678. __set_bit(EV_SYN, jd_scratch);
  679. if (!bitmap_equal(jd_scratch, dev->evbit, EV_CNT))
  680. return false;
  681. bitmap_zero(jd_scratch, ABS_CNT);
  682. __set_bit(ABS_X, jd_scratch);
  683. __set_bit(ABS_Y, jd_scratch);
  684. if (!bitmap_equal(dev->absbit, jd_scratch, ABS_CNT))
  685. return false;
  686. bitmap_zero(jd_scratch, KEY_CNT);
  687. __set_bit(BTN_LEFT, jd_scratch);
  688. __set_bit(BTN_RIGHT, jd_scratch);
  689. __set_bit(BTN_MIDDLE, jd_scratch);
  690. if (!bitmap_equal(dev->keybit, jd_scratch, KEY_CNT))
  691. return false;
  692. /*
  693. * Amiga joystick (amijoy) historically uses left/middle/right
  694. * button events.
  695. */
  696. if (dev->id.bustype == BUS_AMIGA)
  697. return false;
  698. return true;
  699. }
  700. static bool joydev_match(struct input_handler *handler, struct input_dev *dev)
  701. {
  702. /* Disable blacklisted devices */
  703. if (joydev_dev_is_blacklisted(dev))
  704. return false;
  705. /* Avoid absolute mice */
  706. if (joydev_dev_is_absolute_mouse(dev))
  707. return false;
  708. return true;
  709. }
  710. static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
  711. const struct input_device_id *id)
  712. {
  713. struct joydev *joydev;
  714. int i, j, t, minor, dev_no;
  715. int error;
  716. minor = input_get_new_minor(JOYDEV_MINOR_BASE, JOYDEV_MINORS, true);
  717. if (minor < 0) {
  718. error = minor;
  719. pr_err("failed to reserve new minor: %d\n", error);
  720. return error;
  721. }
  722. joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
  723. if (!joydev) {
  724. error = -ENOMEM;
  725. goto err_free_minor;
  726. }
  727. INIT_LIST_HEAD(&joydev->client_list);
  728. spin_lock_init(&joydev->client_lock);
  729. mutex_init(&joydev->mutex);
  730. init_waitqueue_head(&joydev->wait);
  731. joydev->exist = true;
  732. dev_no = minor;
  733. /* Normalize device number if it falls into legacy range */
  734. if (dev_no < JOYDEV_MINOR_BASE + JOYDEV_MINORS)
  735. dev_no -= JOYDEV_MINOR_BASE;
  736. dev_set_name(&joydev->dev, "js%d", dev_no);
  737. joydev->handle.dev = input_get_device(dev);
  738. joydev->handle.name = dev_name(&joydev->dev);
  739. joydev->handle.handler = handler;
  740. joydev->handle.private = joydev;
  741. for_each_set_bit(i, dev->absbit, ABS_CNT) {
  742. joydev->absmap[i] = joydev->nabs;
  743. joydev->abspam[joydev->nabs] = i;
  744. joydev->nabs++;
  745. }
  746. for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
  747. if (test_bit(i + BTN_MISC, dev->keybit)) {
  748. joydev->keymap[i] = joydev->nkey;
  749. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  750. joydev->nkey++;
  751. }
  752. for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
  753. if (test_bit(i + BTN_MISC, dev->keybit)) {
  754. joydev->keymap[i] = joydev->nkey;
  755. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  756. joydev->nkey++;
  757. }
  758. for (i = 0; i < joydev->nabs; i++) {
  759. j = joydev->abspam[i];
  760. if (input_abs_get_max(dev, j) == input_abs_get_min(dev, j)) {
  761. joydev->corr[i].type = JS_CORR_NONE;
  762. continue;
  763. }
  764. joydev->corr[i].type = JS_CORR_BROKEN;
  765. joydev->corr[i].prec = input_abs_get_fuzz(dev, j);
  766. t = (input_abs_get_max(dev, j) + input_abs_get_min(dev, j)) / 2;
  767. joydev->corr[i].coef[0] = t - input_abs_get_flat(dev, j);
  768. joydev->corr[i].coef[1] = t + input_abs_get_flat(dev, j);
  769. t = (input_abs_get_max(dev, j) - input_abs_get_min(dev, j)) / 2
  770. - 2 * input_abs_get_flat(dev, j);
  771. if (t) {
  772. joydev->corr[i].coef[2] = (1 << 29) / t;
  773. joydev->corr[i].coef[3] = (1 << 29) / t;
  774. }
  775. }
  776. joydev->dev.devt = MKDEV(INPUT_MAJOR, minor);
  777. joydev->dev.class = &input_class;
  778. joydev->dev.parent = &dev->dev;
  779. joydev->dev.release = joydev_free;
  780. device_initialize(&joydev->dev);
  781. error = input_register_handle(&joydev->handle);
  782. if (error)
  783. goto err_free_joydev;
  784. cdev_init(&joydev->cdev, &joydev_fops);
  785. error = cdev_device_add(&joydev->cdev, &joydev->dev);
  786. if (error)
  787. goto err_cleanup_joydev;
  788. return 0;
  789. err_cleanup_joydev:
  790. joydev_cleanup(joydev);
  791. input_unregister_handle(&joydev->handle);
  792. err_free_joydev:
  793. put_device(&joydev->dev);
  794. err_free_minor:
  795. input_free_minor(minor);
  796. return error;
  797. }
  798. static void joydev_disconnect(struct input_handle *handle)
  799. {
  800. struct joydev *joydev = handle->private;
  801. cdev_device_del(&joydev->cdev, &joydev->dev);
  802. joydev_cleanup(joydev);
  803. input_free_minor(MINOR(joydev->dev.devt));
  804. input_unregister_handle(handle);
  805. put_device(&joydev->dev);
  806. }
  807. static const struct input_device_id joydev_ids[] = {
  808. {
  809. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  810. INPUT_DEVICE_ID_MATCH_ABSBIT,
  811. .evbit = { BIT_MASK(EV_ABS) },
  812. .absbit = { BIT_MASK(ABS_X) },
  813. },
  814. {
  815. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  816. INPUT_DEVICE_ID_MATCH_ABSBIT,
  817. .evbit = { BIT_MASK(EV_ABS) },
  818. .absbit = { BIT_MASK(ABS_Z) },
  819. },
  820. {
  821. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  822. INPUT_DEVICE_ID_MATCH_ABSBIT,
  823. .evbit = { BIT_MASK(EV_ABS) },
  824. .absbit = { BIT_MASK(ABS_WHEEL) },
  825. },
  826. {
  827. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  828. INPUT_DEVICE_ID_MATCH_ABSBIT,
  829. .evbit = { BIT_MASK(EV_ABS) },
  830. .absbit = { BIT_MASK(ABS_THROTTLE) },
  831. },
  832. {
  833. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  834. INPUT_DEVICE_ID_MATCH_KEYBIT,
  835. .evbit = { BIT_MASK(EV_KEY) },
  836. .keybit = {[BIT_WORD(BTN_JOYSTICK)] = BIT_MASK(BTN_JOYSTICK) },
  837. },
  838. {
  839. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  840. INPUT_DEVICE_ID_MATCH_KEYBIT,
  841. .evbit = { BIT_MASK(EV_KEY) },
  842. .keybit = { [BIT_WORD(BTN_GAMEPAD)] = BIT_MASK(BTN_GAMEPAD) },
  843. },
  844. {
  845. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  846. INPUT_DEVICE_ID_MATCH_KEYBIT,
  847. .evbit = { BIT_MASK(EV_KEY) },
  848. .keybit = { [BIT_WORD(BTN_TRIGGER_HAPPY)] = BIT_MASK(BTN_TRIGGER_HAPPY) },
  849. },
  850. { } /* Terminating entry */
  851. };
  852. MODULE_DEVICE_TABLE(input, joydev_ids);
  853. static struct input_handler joydev_handler = {
  854. .event = joydev_event,
  855. .match = joydev_match,
  856. .connect = joydev_connect,
  857. .disconnect = joydev_disconnect,
  858. .legacy_minors = true,
  859. .minor = JOYDEV_MINOR_BASE,
  860. .name = "joydev",
  861. .id_table = joydev_ids,
  862. };
  863. static int __init joydev_init(void)
  864. {
  865. return input_register_handler(&joydev_handler);
  866. }
  867. static void __exit joydev_exit(void)
  868. {
  869. input_unregister_handler(&joydev_handler);
  870. }
  871. module_init(joydev_init);
  872. module_exit(joydev_exit);