joydev.c 26 KB

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