joydev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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/miscdevice.h>
  24. #include <linux/module.h>
  25. #include <linux/poll.h>
  26. #include <linux/init.h>
  27. #include <linux/device.h>
  28. #include <linux/cdev.h>
  29. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  30. MODULE_DESCRIPTION("Joystick device interfaces");
  31. MODULE_SUPPORTED_DEVICE("input/js");
  32. MODULE_LICENSE("GPL");
  33. #define JOYDEV_MINOR_BASE 0
  34. #define JOYDEV_MINORS 16
  35. #define JOYDEV_BUFFER_SIZE 64
  36. struct joydev {
  37. int open;
  38. struct input_handle handle;
  39. wait_queue_head_t wait;
  40. struct list_head client_list;
  41. spinlock_t client_lock; /* protects client_list */
  42. struct mutex mutex;
  43. struct device dev;
  44. struct cdev cdev;
  45. bool exist;
  46. struct js_corr corr[ABS_CNT];
  47. struct JS_DATA_SAVE_TYPE glue;
  48. int nabs;
  49. int nkey;
  50. __u16 keymap[KEY_MAX - BTN_MISC + 1];
  51. __u16 keypam[KEY_MAX - BTN_MISC + 1];
  52. __u8 absmap[ABS_CNT];
  53. __u8 abspam[ABS_CNT];
  54. __s16 abs[ABS_CNT];
  55. };
  56. struct joydev_client {
  57. struct js_event buffer[JOYDEV_BUFFER_SIZE];
  58. int head;
  59. int tail;
  60. int startup;
  61. spinlock_t buffer_lock; /* protects access to buffer, head and tail */
  62. struct fasync_struct *fasync;
  63. struct joydev *joydev;
  64. struct list_head node;
  65. };
  66. static int joydev_correct(int value, struct js_corr *corr)
  67. {
  68. switch (corr->type) {
  69. case JS_CORR_NONE:
  70. break;
  71. case JS_CORR_BROKEN:
  72. value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
  73. ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
  74. ((corr->coef[2] * (value - corr->coef[0])) >> 14);
  75. break;
  76. default:
  77. return 0;
  78. }
  79. return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
  80. }
  81. static void joydev_pass_event(struct joydev_client *client,
  82. struct js_event *event)
  83. {
  84. struct joydev *joydev = client->joydev;
  85. /*
  86. * IRQs already disabled, just acquire the lock
  87. */
  88. spin_lock(&client->buffer_lock);
  89. client->buffer[client->head] = *event;
  90. if (client->startup == joydev->nabs + joydev->nkey) {
  91. client->head++;
  92. client->head &= JOYDEV_BUFFER_SIZE - 1;
  93. if (client->tail == client->head)
  94. client->startup = 0;
  95. }
  96. spin_unlock(&client->buffer_lock);
  97. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  98. }
  99. static void joydev_event(struct input_handle *handle,
  100. unsigned int type, unsigned int code, int value)
  101. {
  102. struct joydev *joydev = handle->private;
  103. struct joydev_client *client;
  104. struct js_event event;
  105. switch (type) {
  106. case EV_KEY:
  107. if (code < BTN_MISC || value == 2)
  108. return;
  109. event.type = JS_EVENT_BUTTON;
  110. event.number = joydev->keymap[code - BTN_MISC];
  111. event.value = value;
  112. break;
  113. case EV_ABS:
  114. event.type = JS_EVENT_AXIS;
  115. event.number = joydev->absmap[code];
  116. event.value = joydev_correct(value,
  117. &joydev->corr[event.number]);
  118. if (event.value == joydev->abs[event.number])
  119. return;
  120. joydev->abs[event.number] = event.value;
  121. break;
  122. default:
  123. return;
  124. }
  125. event.time = jiffies_to_msecs(jiffies);
  126. rcu_read_lock();
  127. list_for_each_entry_rcu(client, &joydev->client_list, node)
  128. joydev_pass_event(client, &event);
  129. rcu_read_unlock();
  130. wake_up_interruptible(&joydev->wait);
  131. }
  132. static int joydev_fasync(int fd, struct file *file, int on)
  133. {
  134. struct joydev_client *client = file->private_data;
  135. return fasync_helper(fd, file, on, &client->fasync);
  136. }
  137. static void joydev_free(struct device *dev)
  138. {
  139. struct joydev *joydev = container_of(dev, struct joydev, dev);
  140. input_put_device(joydev->handle.dev);
  141. kfree(joydev);
  142. }
  143. static void joydev_attach_client(struct joydev *joydev,
  144. struct joydev_client *client)
  145. {
  146. spin_lock(&joydev->client_lock);
  147. list_add_tail_rcu(&client->node, &joydev->client_list);
  148. spin_unlock(&joydev->client_lock);
  149. }
  150. static void joydev_detach_client(struct joydev *joydev,
  151. struct joydev_client *client)
  152. {
  153. spin_lock(&joydev->client_lock);
  154. list_del_rcu(&client->node);
  155. spin_unlock(&joydev->client_lock);
  156. synchronize_rcu();
  157. }
  158. static int joydev_open_device(struct joydev *joydev)
  159. {
  160. int retval;
  161. retval = mutex_lock_interruptible(&joydev->mutex);
  162. if (retval)
  163. return retval;
  164. if (!joydev->exist)
  165. retval = -ENODEV;
  166. else if (!joydev->open++) {
  167. retval = input_open_device(&joydev->handle);
  168. if (retval)
  169. joydev->open--;
  170. }
  171. mutex_unlock(&joydev->mutex);
  172. return retval;
  173. }
  174. static void joydev_close_device(struct joydev *joydev)
  175. {
  176. mutex_lock(&joydev->mutex);
  177. if (joydev->exist && !--joydev->open)
  178. input_close_device(&joydev->handle);
  179. mutex_unlock(&joydev->mutex);
  180. }
  181. /*
  182. * Wake up users waiting for IO so they can disconnect from
  183. * dead device.
  184. */
  185. static void joydev_hangup(struct joydev *joydev)
  186. {
  187. struct joydev_client *client;
  188. spin_lock(&joydev->client_lock);
  189. list_for_each_entry(client, &joydev->client_list, node)
  190. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  191. spin_unlock(&joydev->client_lock);
  192. wake_up_interruptible(&joydev->wait);
  193. }
  194. static int joydev_release(struct inode *inode, struct file *file)
  195. {
  196. struct joydev_client *client = file->private_data;
  197. struct joydev *joydev = client->joydev;
  198. joydev_detach_client(joydev, client);
  199. kfree(client);
  200. joydev_close_device(joydev);
  201. put_device(&joydev->dev);
  202. return 0;
  203. }
  204. static int joydev_open(struct inode *inode, struct file *file)
  205. {
  206. struct joydev *joydev =
  207. container_of(inode->i_cdev, struct joydev, cdev);
  208. struct joydev_client *client;
  209. int error;
  210. client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
  211. if (!client)
  212. return -ENOMEM;
  213. spin_lock_init(&client->buffer_lock);
  214. client->joydev = joydev;
  215. joydev_attach_client(joydev, client);
  216. error = joydev_open_device(joydev);
  217. if (error)
  218. goto err_free_client;
  219. file->private_data = client;
  220. nonseekable_open(inode, file);
  221. get_device(&joydev->dev);
  222. return 0;
  223. err_free_client:
  224. joydev_detach_client(joydev, client);
  225. kfree(client);
  226. return error;
  227. }
  228. static int joydev_generate_startup_event(struct joydev_client *client,
  229. struct input_dev *input,
  230. struct js_event *event)
  231. {
  232. struct joydev *joydev = client->joydev;
  233. int have_event;
  234. spin_lock_irq(&client->buffer_lock);
  235. have_event = client->startup < joydev->nabs + joydev->nkey;
  236. if (have_event) {
  237. event->time = jiffies_to_msecs(jiffies);
  238. if (client->startup < joydev->nkey) {
  239. event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
  240. event->number = client->startup;
  241. event->value = !!test_bit(joydev->keypam[event->number],
  242. input->key);
  243. } else {
  244. event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
  245. event->number = client->startup - joydev->nkey;
  246. event->value = joydev->abs[event->number];
  247. }
  248. client->startup++;
  249. }
  250. spin_unlock_irq(&client->buffer_lock);
  251. return have_event;
  252. }
  253. static int joydev_fetch_next_event(struct joydev_client *client,
  254. struct js_event *event)
  255. {
  256. int have_event;
  257. spin_lock_irq(&client->buffer_lock);
  258. have_event = client->head != client->tail;
  259. if (have_event) {
  260. *event = client->buffer[client->tail++];
  261. client->tail &= JOYDEV_BUFFER_SIZE - 1;
  262. }
  263. spin_unlock_irq(&client->buffer_lock);
  264. return have_event;
  265. }
  266. /*
  267. * Old joystick interface
  268. */
  269. static ssize_t joydev_0x_read(struct joydev_client *client,
  270. struct input_dev *input,
  271. char __user *buf)
  272. {
  273. struct joydev *joydev = client->joydev;
  274. struct JS_DATA_TYPE data;
  275. int i;
  276. spin_lock_irq(&input->event_lock);
  277. /*
  278. * Get device state
  279. */
  280. for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
  281. data.buttons |=
  282. test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
  283. data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
  284. data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
  285. /*
  286. * Reset reader's event queue
  287. */
  288. spin_lock(&client->buffer_lock);
  289. client->startup = 0;
  290. client->tail = client->head;
  291. spin_unlock(&client->buffer_lock);
  292. spin_unlock_irq(&input->event_lock);
  293. if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
  294. return -EFAULT;
  295. return sizeof(struct JS_DATA_TYPE);
  296. }
  297. static inline int joydev_data_pending(struct joydev_client *client)
  298. {
  299. struct joydev *joydev = client->joydev;
  300. return client->startup < joydev->nabs + joydev->nkey ||
  301. client->head != client->tail;
  302. }
  303. static ssize_t joydev_read(struct file *file, char __user *buf,
  304. size_t count, loff_t *ppos)
  305. {
  306. struct joydev_client *client = file->private_data;
  307. struct joydev *joydev = client->joydev;
  308. struct input_dev *input = joydev->handle.dev;
  309. struct js_event event;
  310. int retval;
  311. if (!joydev->exist)
  312. return -ENODEV;
  313. if (count < sizeof(struct js_event))
  314. return -EINVAL;
  315. if (count == sizeof(struct JS_DATA_TYPE))
  316. return joydev_0x_read(client, input, buf);
  317. if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
  318. return -EAGAIN;
  319. retval = wait_event_interruptible(joydev->wait,
  320. !joydev->exist || joydev_data_pending(client));
  321. if (retval)
  322. return retval;
  323. if (!joydev->exist)
  324. return -ENODEV;
  325. while (retval + sizeof(struct js_event) <= count &&
  326. joydev_generate_startup_event(client, input, &event)) {
  327. if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
  328. return -EFAULT;
  329. retval += sizeof(struct js_event);
  330. }
  331. while (retval + sizeof(struct js_event) <= count &&
  332. joydev_fetch_next_event(client, &event)) {
  333. if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
  334. return -EFAULT;
  335. retval += sizeof(struct js_event);
  336. }
  337. return retval;
  338. }
  339. /* No kernel lock - fine */
  340. static unsigned int joydev_poll(struct file *file, poll_table *wait)
  341. {
  342. struct joydev_client *client = file->private_data;
  343. struct joydev *joydev = client->joydev;
  344. poll_wait(file, &joydev->wait, wait);
  345. return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
  346. (joydev->exist ? 0 : (POLLHUP | POLLERR));
  347. }
  348. static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
  349. void __user *argp, size_t len)
  350. {
  351. __u8 *abspam;
  352. int i;
  353. int retval = 0;
  354. len = min(len, sizeof(joydev->abspam));
  355. /* Validate the map. */
  356. abspam = kmalloc(len, GFP_KERNEL);
  357. if (!abspam)
  358. return -ENOMEM;
  359. if (copy_from_user(abspam, argp, len)) {
  360. retval = -EFAULT;
  361. goto out;
  362. }
  363. for (i = 0; i < joydev->nabs; i++) {
  364. if (abspam[i] > ABS_MAX) {
  365. retval = -EINVAL;
  366. goto out;
  367. }
  368. }
  369. memcpy(joydev->abspam, abspam, len);
  370. for (i = 0; i < joydev->nabs; i++)
  371. joydev->absmap[joydev->abspam[i]] = i;
  372. out:
  373. kfree(abspam);
  374. return retval;
  375. }
  376. static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
  377. void __user *argp, size_t len)
  378. {
  379. __u16 *keypam;
  380. int i;
  381. int retval = 0;
  382. len = min(len, sizeof(joydev->keypam));
  383. /* Validate the map. */
  384. keypam = kmalloc(len, GFP_KERNEL);
  385. if (!keypam)
  386. return -ENOMEM;
  387. if (copy_from_user(keypam, argp, len)) {
  388. retval = -EFAULT;
  389. goto out;
  390. }
  391. for (i = 0; i < joydev->nkey; i++) {
  392. if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
  393. retval = -EINVAL;
  394. goto out;
  395. }
  396. }
  397. memcpy(joydev->keypam, keypam, len);
  398. for (i = 0; i < joydev->nkey; i++)
  399. joydev->keymap[keypam[i] - BTN_MISC] = i;
  400. out:
  401. kfree(keypam);
  402. return retval;
  403. }
  404. static int joydev_ioctl_common(struct joydev *joydev,
  405. unsigned int cmd, void __user *argp)
  406. {
  407. struct input_dev *dev = joydev->handle.dev;
  408. size_t len;
  409. int i;
  410. const char *name;
  411. /* Process fixed-sized commands. */
  412. switch (cmd) {
  413. case JS_SET_CAL:
  414. return copy_from_user(&joydev->glue.JS_CORR, argp,
  415. sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
  416. case JS_GET_CAL:
  417. return copy_to_user(argp, &joydev->glue.JS_CORR,
  418. sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
  419. case JS_SET_TIMEOUT:
  420. return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
  421. case JS_GET_TIMEOUT:
  422. return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
  423. case JSIOCGVERSION:
  424. return put_user(JS_VERSION, (__u32 __user *) argp);
  425. case JSIOCGAXES:
  426. return put_user(joydev->nabs, (__u8 __user *) argp);
  427. case JSIOCGBUTTONS:
  428. return put_user(joydev->nkey, (__u8 __user *) argp);
  429. case JSIOCSCORR:
  430. if (copy_from_user(joydev->corr, argp,
  431. sizeof(joydev->corr[0]) * joydev->nabs))
  432. return -EFAULT;
  433. for (i = 0; i < joydev->nabs; i++) {
  434. int val = input_abs_get_val(dev, joydev->abspam[i]);
  435. joydev->abs[i] = joydev_correct(val, &joydev->corr[i]);
  436. }
  437. return 0;
  438. case JSIOCGCORR:
  439. return copy_to_user(argp, joydev->corr,
  440. sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
  441. }
  442. /*
  443. * Process variable-sized commands (the axis and button map commands
  444. * are considered variable-sized to decouple them from the values of
  445. * ABS_MAX and KEY_MAX).
  446. */
  447. switch (cmd & ~IOCSIZE_MASK) {
  448. case (JSIOCSAXMAP & ~IOCSIZE_MASK):
  449. return joydev_handle_JSIOCSAXMAP(joydev, argp, _IOC_SIZE(cmd));
  450. case (JSIOCGAXMAP & ~IOCSIZE_MASK):
  451. len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam));
  452. return copy_to_user(argp, joydev->abspam, len) ? -EFAULT : len;
  453. case (JSIOCSBTNMAP & ~IOCSIZE_MASK):
  454. return joydev_handle_JSIOCSBTNMAP(joydev, argp, _IOC_SIZE(cmd));
  455. case (JSIOCGBTNMAP & ~IOCSIZE_MASK):
  456. len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam));
  457. return copy_to_user(argp, joydev->keypam, len) ? -EFAULT : len;
  458. case JSIOCGNAME(0):
  459. name = dev->name;
  460. if (!name)
  461. return 0;
  462. len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1);
  463. return copy_to_user(argp, name, len) ? -EFAULT : len;
  464. }
  465. return -EINVAL;
  466. }
  467. #ifdef CONFIG_COMPAT
  468. static long joydev_compat_ioctl(struct file *file,
  469. unsigned int cmd, unsigned long arg)
  470. {
  471. struct joydev_client *client = file->private_data;
  472. struct joydev *joydev = client->joydev;
  473. void __user *argp = (void __user *)arg;
  474. s32 tmp32;
  475. struct JS_DATA_SAVE_TYPE_32 ds32;
  476. int retval;
  477. retval = mutex_lock_interruptible(&joydev->mutex);
  478. if (retval)
  479. return retval;
  480. if (!joydev->exist) {
  481. retval = -ENODEV;
  482. goto out;
  483. }
  484. switch (cmd) {
  485. case JS_SET_TIMELIMIT:
  486. retval = get_user(tmp32, (s32 __user *) arg);
  487. if (retval == 0)
  488. joydev->glue.JS_TIMELIMIT = tmp32;
  489. break;
  490. case JS_GET_TIMELIMIT:
  491. tmp32 = joydev->glue.JS_TIMELIMIT;
  492. retval = put_user(tmp32, (s32 __user *) arg);
  493. break;
  494. case JS_SET_ALL:
  495. retval = copy_from_user(&ds32, argp,
  496. sizeof(ds32)) ? -EFAULT : 0;
  497. if (retval == 0) {
  498. joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
  499. joydev->glue.BUSY = ds32.BUSY;
  500. joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
  501. joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
  502. joydev->glue.JS_SAVE = ds32.JS_SAVE;
  503. joydev->glue.JS_CORR = ds32.JS_CORR;
  504. }
  505. break;
  506. case JS_GET_ALL:
  507. ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
  508. ds32.BUSY = joydev->glue.BUSY;
  509. ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
  510. ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
  511. ds32.JS_SAVE = joydev->glue.JS_SAVE;
  512. ds32.JS_CORR = joydev->glue.JS_CORR;
  513. retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
  514. break;
  515. default:
  516. retval = joydev_ioctl_common(joydev, cmd, argp);
  517. break;
  518. }
  519. out:
  520. mutex_unlock(&joydev->mutex);
  521. return retval;
  522. }
  523. #endif /* CONFIG_COMPAT */
  524. static long joydev_ioctl(struct file *file,
  525. unsigned int cmd, unsigned long arg)
  526. {
  527. struct joydev_client *client = file->private_data;
  528. struct joydev *joydev = client->joydev;
  529. void __user *argp = (void __user *)arg;
  530. int retval;
  531. retval = mutex_lock_interruptible(&joydev->mutex);
  532. if (retval)
  533. return retval;
  534. if (!joydev->exist) {
  535. retval = -ENODEV;
  536. goto out;
  537. }
  538. switch (cmd) {
  539. case JS_SET_TIMELIMIT:
  540. retval = get_user(joydev->glue.JS_TIMELIMIT,
  541. (long __user *) arg);
  542. break;
  543. case JS_GET_TIMELIMIT:
  544. retval = put_user(joydev->glue.JS_TIMELIMIT,
  545. (long __user *) arg);
  546. break;
  547. case JS_SET_ALL:
  548. retval = copy_from_user(&joydev->glue, argp,
  549. sizeof(joydev->glue)) ? -EFAULT : 0;
  550. break;
  551. case JS_GET_ALL:
  552. retval = copy_to_user(argp, &joydev->glue,
  553. sizeof(joydev->glue)) ? -EFAULT : 0;
  554. break;
  555. default:
  556. retval = joydev_ioctl_common(joydev, cmd, argp);
  557. break;
  558. }
  559. out:
  560. mutex_unlock(&joydev->mutex);
  561. return retval;
  562. }
  563. static const struct file_operations joydev_fops = {
  564. .owner = THIS_MODULE,
  565. .read = joydev_read,
  566. .poll = joydev_poll,
  567. .open = joydev_open,
  568. .release = joydev_release,
  569. .unlocked_ioctl = joydev_ioctl,
  570. #ifdef CONFIG_COMPAT
  571. .compat_ioctl = joydev_compat_ioctl,
  572. #endif
  573. .fasync = joydev_fasync,
  574. .llseek = no_llseek,
  575. };
  576. /*
  577. * Mark device non-existent. This disables writes, ioctls and
  578. * prevents new users from opening the device. Already posted
  579. * blocking reads will stay, however new ones will fail.
  580. */
  581. static void joydev_mark_dead(struct joydev *joydev)
  582. {
  583. mutex_lock(&joydev->mutex);
  584. joydev->exist = false;
  585. mutex_unlock(&joydev->mutex);
  586. }
  587. static void joydev_cleanup(struct joydev *joydev)
  588. {
  589. struct input_handle *handle = &joydev->handle;
  590. joydev_mark_dead(joydev);
  591. joydev_hangup(joydev);
  592. cdev_del(&joydev->cdev);
  593. /* joydev is marked dead so no one else accesses joydev->open */
  594. if (joydev->open)
  595. input_close_device(handle);
  596. }
  597. static bool joydev_match(struct input_handler *handler, struct input_dev *dev)
  598. {
  599. /* Avoid touchpads and touchscreens */
  600. if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_TOUCH, dev->keybit))
  601. return false;
  602. /* Avoid tablets, digitisers and similar devices */
  603. if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_DIGI, dev->keybit))
  604. return false;
  605. return true;
  606. }
  607. static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
  608. const struct input_device_id *id)
  609. {
  610. struct joydev *joydev;
  611. int i, j, t, minor, dev_no;
  612. int error;
  613. minor = input_get_new_minor(JOYDEV_MINOR_BASE, JOYDEV_MINORS, true);
  614. if (minor < 0) {
  615. error = minor;
  616. pr_err("failed to reserve new minor: %d\n", error);
  617. return error;
  618. }
  619. joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
  620. if (!joydev) {
  621. error = -ENOMEM;
  622. goto err_free_minor;
  623. }
  624. INIT_LIST_HEAD(&joydev->client_list);
  625. spin_lock_init(&joydev->client_lock);
  626. mutex_init(&joydev->mutex);
  627. init_waitqueue_head(&joydev->wait);
  628. joydev->exist = true;
  629. dev_no = minor;
  630. /* Normalize device number if it falls into legacy range */
  631. if (dev_no < JOYDEV_MINOR_BASE + JOYDEV_MINORS)
  632. dev_no -= JOYDEV_MINOR_BASE;
  633. dev_set_name(&joydev->dev, "js%d", dev_no);
  634. joydev->handle.dev = input_get_device(dev);
  635. joydev->handle.name = dev_name(&joydev->dev);
  636. joydev->handle.handler = handler;
  637. joydev->handle.private = joydev;
  638. for (i = 0; i < ABS_CNT; i++)
  639. if (test_bit(i, dev->absbit)) {
  640. joydev->absmap[i] = joydev->nabs;
  641. joydev->abspam[joydev->nabs] = i;
  642. joydev->nabs++;
  643. }
  644. for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
  645. if (test_bit(i + BTN_MISC, dev->keybit)) {
  646. joydev->keymap[i] = joydev->nkey;
  647. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  648. joydev->nkey++;
  649. }
  650. for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
  651. if (test_bit(i + BTN_MISC, dev->keybit)) {
  652. joydev->keymap[i] = joydev->nkey;
  653. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  654. joydev->nkey++;
  655. }
  656. for (i = 0; i < joydev->nabs; i++) {
  657. j = joydev->abspam[i];
  658. if (input_abs_get_max(dev, j) == input_abs_get_min(dev, j)) {
  659. joydev->corr[i].type = JS_CORR_NONE;
  660. joydev->abs[i] = input_abs_get_val(dev, j);
  661. continue;
  662. }
  663. joydev->corr[i].type = JS_CORR_BROKEN;
  664. joydev->corr[i].prec = input_abs_get_fuzz(dev, j);
  665. t = (input_abs_get_max(dev, j) + input_abs_get_min(dev, j)) / 2;
  666. joydev->corr[i].coef[0] = t - input_abs_get_flat(dev, j);
  667. joydev->corr[i].coef[1] = t + input_abs_get_flat(dev, j);
  668. t = (input_abs_get_max(dev, j) - input_abs_get_min(dev, j)) / 2
  669. - 2 * input_abs_get_flat(dev, j);
  670. if (t) {
  671. joydev->corr[i].coef[2] = (1 << 29) / t;
  672. joydev->corr[i].coef[3] = (1 << 29) / t;
  673. joydev->abs[i] =
  674. joydev_correct(input_abs_get_val(dev, j),
  675. joydev->corr + i);
  676. }
  677. }
  678. joydev->dev.devt = MKDEV(INPUT_MAJOR, minor);
  679. joydev->dev.class = &input_class;
  680. joydev->dev.parent = &dev->dev;
  681. joydev->dev.release = joydev_free;
  682. device_initialize(&joydev->dev);
  683. error = input_register_handle(&joydev->handle);
  684. if (error)
  685. goto err_free_joydev;
  686. cdev_init(&joydev->cdev, &joydev_fops);
  687. error = cdev_add(&joydev->cdev, joydev->dev.devt, 1);
  688. if (error)
  689. goto err_unregister_handle;
  690. error = device_add(&joydev->dev);
  691. if (error)
  692. goto err_cleanup_joydev;
  693. return 0;
  694. err_cleanup_joydev:
  695. joydev_cleanup(joydev);
  696. err_unregister_handle:
  697. input_unregister_handle(&joydev->handle);
  698. err_free_joydev:
  699. put_device(&joydev->dev);
  700. err_free_minor:
  701. input_free_minor(minor);
  702. return error;
  703. }
  704. static void joydev_disconnect(struct input_handle *handle)
  705. {
  706. struct joydev *joydev = handle->private;
  707. device_del(&joydev->dev);
  708. joydev_cleanup(joydev);
  709. input_free_minor(MINOR(joydev->dev.devt));
  710. input_unregister_handle(handle);
  711. put_device(&joydev->dev);
  712. }
  713. static const struct input_device_id joydev_ids[] = {
  714. {
  715. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  716. INPUT_DEVICE_ID_MATCH_ABSBIT,
  717. .evbit = { BIT_MASK(EV_ABS) },
  718. .absbit = { BIT_MASK(ABS_X) },
  719. },
  720. {
  721. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  722. INPUT_DEVICE_ID_MATCH_ABSBIT,
  723. .evbit = { BIT_MASK(EV_ABS) },
  724. .absbit = { BIT_MASK(ABS_WHEEL) },
  725. },
  726. {
  727. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  728. INPUT_DEVICE_ID_MATCH_ABSBIT,
  729. .evbit = { BIT_MASK(EV_ABS) },
  730. .absbit = { BIT_MASK(ABS_THROTTLE) },
  731. },
  732. {
  733. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  734. INPUT_DEVICE_ID_MATCH_KEYBIT,
  735. .evbit = { BIT_MASK(EV_KEY) },
  736. .keybit = {[BIT_WORD(BTN_JOYSTICK)] = BIT_MASK(BTN_JOYSTICK) },
  737. },
  738. {
  739. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  740. INPUT_DEVICE_ID_MATCH_KEYBIT,
  741. .evbit = { BIT_MASK(EV_KEY) },
  742. .keybit = { [BIT_WORD(BTN_GAMEPAD)] = BIT_MASK(BTN_GAMEPAD) },
  743. },
  744. {
  745. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  746. INPUT_DEVICE_ID_MATCH_KEYBIT,
  747. .evbit = { BIT_MASK(EV_KEY) },
  748. .keybit = { [BIT_WORD(BTN_TRIGGER_HAPPY)] = BIT_MASK(BTN_TRIGGER_HAPPY) },
  749. },
  750. { } /* Terminating entry */
  751. };
  752. MODULE_DEVICE_TABLE(input, joydev_ids);
  753. static struct input_handler joydev_handler = {
  754. .event = joydev_event,
  755. .match = joydev_match,
  756. .connect = joydev_connect,
  757. .disconnect = joydev_disconnect,
  758. .legacy_minors = true,
  759. .minor = JOYDEV_MINOR_BASE,
  760. .name = "joydev",
  761. .id_table = joydev_ids,
  762. };
  763. static int __init joydev_init(void)
  764. {
  765. return input_register_handler(&joydev_handler);
  766. }
  767. static void __exit joydev_exit(void)
  768. {
  769. input_unregister_handler(&joydev_handler);
  770. }
  771. module_init(joydev_init);
  772. module_exit(joydev_exit);