joydev.c 23 KB

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