joydev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. #include <asm/io.h>
  13. #include <asm/system.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. synchronize_rcu();
  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. out:
  387. kfree(abspam);
  388. return retval;
  389. }
  390. static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
  391. void __user *argp, size_t len)
  392. {
  393. __u16 *keypam;
  394. int i;
  395. int retval = 0;
  396. len = min(len, sizeof(joydev->keypam));
  397. /* Validate the map. */
  398. keypam = kmalloc(len, GFP_KERNEL);
  399. if (!keypam)
  400. return -ENOMEM;
  401. if (copy_from_user(keypam, argp, len)) {
  402. retval = -EFAULT;
  403. goto out;
  404. }
  405. for (i = 0; i < joydev->nkey; i++) {
  406. if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
  407. retval = -EINVAL;
  408. goto out;
  409. }
  410. }
  411. memcpy(joydev->keypam, keypam, len);
  412. for (i = 0; i < joydev->nkey; i++)
  413. joydev->keymap[keypam[i] - BTN_MISC] = i;
  414. out:
  415. kfree(keypam);
  416. return retval;
  417. }
  418. static int joydev_ioctl_common(struct joydev *joydev,
  419. unsigned int cmd, void __user *argp)
  420. {
  421. struct input_dev *dev = joydev->handle.dev;
  422. size_t len;
  423. int i, j;
  424. const char *name;
  425. /* Process fixed-sized commands. */
  426. switch (cmd) {
  427. case JS_SET_CAL:
  428. return copy_from_user(&joydev->glue.JS_CORR, argp,
  429. sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
  430. case JS_GET_CAL:
  431. return copy_to_user(argp, &joydev->glue.JS_CORR,
  432. sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
  433. case JS_SET_TIMEOUT:
  434. return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
  435. case JS_GET_TIMEOUT:
  436. return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
  437. case JSIOCGVERSION:
  438. return put_user(JS_VERSION, (__u32 __user *) argp);
  439. case JSIOCGAXES:
  440. return put_user(joydev->nabs, (__u8 __user *) argp);
  441. case JSIOCGBUTTONS:
  442. return put_user(joydev->nkey, (__u8 __user *) argp);
  443. case JSIOCSCORR:
  444. if (copy_from_user(joydev->corr, argp,
  445. sizeof(joydev->corr[0]) * joydev->nabs))
  446. return -EFAULT;
  447. for (i = 0; i < joydev->nabs; i++) {
  448. j = joydev->abspam[i];
  449. joydev->abs[i] = joydev_correct(dev->abs[j],
  450. &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. };
  590. static int joydev_install_chrdev(struct joydev *joydev)
  591. {
  592. joydev_table[joydev->minor] = joydev;
  593. return 0;
  594. }
  595. static void joydev_remove_chrdev(struct joydev *joydev)
  596. {
  597. mutex_lock(&joydev_table_mutex);
  598. joydev_table[joydev->minor] = NULL;
  599. mutex_unlock(&joydev_table_mutex);
  600. }
  601. /*
  602. * Mark device non-existant. This disables writes, ioctls and
  603. * prevents new users from opening the device. Already posted
  604. * blocking reads will stay, however new ones will fail.
  605. */
  606. static void joydev_mark_dead(struct joydev *joydev)
  607. {
  608. mutex_lock(&joydev->mutex);
  609. joydev->exist = false;
  610. mutex_unlock(&joydev->mutex);
  611. }
  612. static void joydev_cleanup(struct joydev *joydev)
  613. {
  614. struct input_handle *handle = &joydev->handle;
  615. joydev_mark_dead(joydev);
  616. joydev_hangup(joydev);
  617. joydev_remove_chrdev(joydev);
  618. /* joydev is marked dead so noone else accesses joydev->open */
  619. if (joydev->open)
  620. input_close_device(handle);
  621. }
  622. static bool joydev_match(struct input_handler *handler, struct input_dev *dev)
  623. {
  624. /* Avoid touchpads and touchscreens */
  625. if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_TOUCH, dev->keybit))
  626. return false;
  627. /* Avoid tablets, digitisers and similar devices */
  628. if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_DIGI, dev->keybit))
  629. return false;
  630. return true;
  631. }
  632. static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
  633. const struct input_device_id *id)
  634. {
  635. struct joydev *joydev;
  636. int i, j, t, minor;
  637. int error;
  638. for (minor = 0; minor < JOYDEV_MINORS; minor++)
  639. if (!joydev_table[minor])
  640. break;
  641. if (minor == JOYDEV_MINORS) {
  642. printk(KERN_ERR "joydev: no more free joydev devices\n");
  643. return -ENFILE;
  644. }
  645. joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
  646. if (!joydev)
  647. return -ENOMEM;
  648. INIT_LIST_HEAD(&joydev->client_list);
  649. spin_lock_init(&joydev->client_lock);
  650. mutex_init(&joydev->mutex);
  651. init_waitqueue_head(&joydev->wait);
  652. dev_set_name(&joydev->dev, "js%d", minor);
  653. joydev->exist = true;
  654. joydev->minor = minor;
  655. joydev->handle.dev = input_get_device(dev);
  656. joydev->handle.name = dev_name(&joydev->dev);
  657. joydev->handle.handler = handler;
  658. joydev->handle.private = joydev;
  659. for (i = 0; i < ABS_CNT; i++)
  660. if (test_bit(i, dev->absbit)) {
  661. joydev->absmap[i] = joydev->nabs;
  662. joydev->abspam[joydev->nabs] = i;
  663. joydev->nabs++;
  664. }
  665. for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
  666. if (test_bit(i + BTN_MISC, dev->keybit)) {
  667. joydev->keymap[i] = joydev->nkey;
  668. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  669. joydev->nkey++;
  670. }
  671. for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
  672. if (test_bit(i + BTN_MISC, dev->keybit)) {
  673. joydev->keymap[i] = joydev->nkey;
  674. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  675. joydev->nkey++;
  676. }
  677. for (i = 0; i < joydev->nabs; i++) {
  678. j = joydev->abspam[i];
  679. if (dev->absmax[j] == dev->absmin[j]) {
  680. joydev->corr[i].type = JS_CORR_NONE;
  681. joydev->abs[i] = dev->abs[j];
  682. continue;
  683. }
  684. joydev->corr[i].type = JS_CORR_BROKEN;
  685. joydev->corr[i].prec = dev->absfuzz[j];
  686. joydev->corr[i].coef[0] =
  687. (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
  688. joydev->corr[i].coef[1] =
  689. (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
  690. t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j];
  691. if (t) {
  692. joydev->corr[i].coef[2] = (1 << 29) / t;
  693. joydev->corr[i].coef[3] = (1 << 29) / t;
  694. joydev->abs[i] = joydev_correct(dev->abs[j],
  695. joydev->corr + i);
  696. }
  697. }
  698. joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
  699. joydev->dev.class = &input_class;
  700. joydev->dev.parent = &dev->dev;
  701. joydev->dev.release = joydev_free;
  702. device_initialize(&joydev->dev);
  703. error = input_register_handle(&joydev->handle);
  704. if (error)
  705. goto err_free_joydev;
  706. error = joydev_install_chrdev(joydev);
  707. if (error)
  708. goto err_unregister_handle;
  709. error = device_add(&joydev->dev);
  710. if (error)
  711. goto err_cleanup_joydev;
  712. return 0;
  713. err_cleanup_joydev:
  714. joydev_cleanup(joydev);
  715. err_unregister_handle:
  716. input_unregister_handle(&joydev->handle);
  717. err_free_joydev:
  718. put_device(&joydev->dev);
  719. return error;
  720. }
  721. static void joydev_disconnect(struct input_handle *handle)
  722. {
  723. struct joydev *joydev = handle->private;
  724. device_del(&joydev->dev);
  725. joydev_cleanup(joydev);
  726. input_unregister_handle(handle);
  727. put_device(&joydev->dev);
  728. }
  729. static const struct input_device_id joydev_ids[] = {
  730. {
  731. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  732. INPUT_DEVICE_ID_MATCH_ABSBIT,
  733. .evbit = { BIT_MASK(EV_ABS) },
  734. .absbit = { BIT_MASK(ABS_X) },
  735. },
  736. {
  737. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  738. INPUT_DEVICE_ID_MATCH_ABSBIT,
  739. .evbit = { BIT_MASK(EV_ABS) },
  740. .absbit = { BIT_MASK(ABS_WHEEL) },
  741. },
  742. {
  743. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  744. INPUT_DEVICE_ID_MATCH_ABSBIT,
  745. .evbit = { BIT_MASK(EV_ABS) },
  746. .absbit = { BIT_MASK(ABS_THROTTLE) },
  747. },
  748. {
  749. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  750. INPUT_DEVICE_ID_MATCH_KEYBIT,
  751. .evbit = { BIT_MASK(EV_KEY) },
  752. .keybit = {[BIT_WORD(BTN_JOYSTICK)] = BIT_MASK(BTN_JOYSTICK) },
  753. },
  754. {
  755. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  756. INPUT_DEVICE_ID_MATCH_KEYBIT,
  757. .evbit = { BIT_MASK(EV_KEY) },
  758. .keybit = { [BIT_WORD(BTN_GAMEPAD)] = BIT_MASK(BTN_GAMEPAD) },
  759. },
  760. {
  761. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  762. INPUT_DEVICE_ID_MATCH_KEYBIT,
  763. .evbit = { BIT_MASK(EV_KEY) },
  764. .keybit = { [BIT_WORD(BTN_TRIGGER_HAPPY)] = BIT_MASK(BTN_TRIGGER_HAPPY) },
  765. },
  766. { } /* Terminating entry */
  767. };
  768. MODULE_DEVICE_TABLE(input, joydev_ids);
  769. static struct input_handler joydev_handler = {
  770. .event = joydev_event,
  771. .match = joydev_match,
  772. .connect = joydev_connect,
  773. .disconnect = joydev_disconnect,
  774. .fops = &joydev_fops,
  775. .minor = JOYDEV_MINOR_BASE,
  776. .name = "joydev",
  777. .id_table = joydev_ids,
  778. };
  779. static int __init joydev_init(void)
  780. {
  781. return input_register_handler(&joydev_handler);
  782. }
  783. static void __exit joydev_exit(void)
  784. {
  785. input_unregister_handler(&joydev_handler);
  786. }
  787. module_init(joydev_init);
  788. module_exit(joydev_exit);