joydev.c 26 KB

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