joydev.c 26 KB

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