joydev.c 26 KB

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