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