joydev.c 23 KB

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