joydev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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 exist;
  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. struct js_corr corr[ABS_MAX + 1];
  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_MAX + 1];
  52. __u8 abspam[ABS_MAX + 1];
  53. __s16 abs[ABS_MAX + 1];
  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. 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. out:
  386. kfree(abspam);
  387. return retval;
  388. }
  389. static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
  390. void __user *argp, size_t len)
  391. {
  392. __u16 *keypam;
  393. int i;
  394. int retval = 0;
  395. len = min(len, sizeof(joydev->keypam));
  396. /* Validate the map. */
  397. keypam = kmalloc(len, GFP_KERNEL);
  398. if (!keypam)
  399. return -ENOMEM;
  400. if (copy_from_user(keypam, argp, len)) {
  401. retval = -EFAULT;
  402. goto out;
  403. }
  404. for (i = 0; i < joydev->nkey; i++) {
  405. if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
  406. retval = -EINVAL;
  407. goto out;
  408. }
  409. }
  410. memcpy(joydev->keypam, keypam, len);
  411. for (i = 0; i < joydev->nkey; i++)
  412. joydev->keymap[keypam[i] - BTN_MISC] = i;
  413. out:
  414. kfree(keypam);
  415. return retval;
  416. }
  417. static int joydev_ioctl_common(struct joydev *joydev,
  418. unsigned int cmd, void __user *argp)
  419. {
  420. struct input_dev *dev = joydev->handle.dev;
  421. size_t len;
  422. int i, j;
  423. const char *name;
  424. /* Process fixed-sized commands. */
  425. switch (cmd) {
  426. case JS_SET_CAL:
  427. return copy_from_user(&joydev->glue.JS_CORR, argp,
  428. sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
  429. case JS_GET_CAL:
  430. return copy_to_user(argp, &joydev->glue.JS_CORR,
  431. sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
  432. case JS_SET_TIMEOUT:
  433. return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
  434. case JS_GET_TIMEOUT:
  435. return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
  436. case JSIOCGVERSION:
  437. return put_user(JS_VERSION, (__u32 __user *) argp);
  438. case JSIOCGAXES:
  439. return put_user(joydev->nabs, (__u8 __user *) argp);
  440. case JSIOCGBUTTONS:
  441. return put_user(joydev->nkey, (__u8 __user *) argp);
  442. case JSIOCSCORR:
  443. if (copy_from_user(joydev->corr, argp,
  444. sizeof(joydev->corr[0]) * joydev->nabs))
  445. return -EFAULT;
  446. for (i = 0; i < joydev->nabs; i++) {
  447. j = joydev->abspam[i];
  448. joydev->abs[i] = joydev_correct(dev->abs[j],
  449. &joydev->corr[i]);
  450. }
  451. return 0;
  452. case JSIOCGCORR:
  453. return copy_to_user(argp, joydev->corr,
  454. sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
  455. }
  456. /*
  457. * Process variable-sized commands (the axis and button map commands
  458. * are considered variable-sized to decouple them from the values of
  459. * ABS_MAX and KEY_MAX).
  460. */
  461. switch (cmd & ~IOCSIZE_MASK) {
  462. case (JSIOCSAXMAP & ~IOCSIZE_MASK):
  463. return joydev_handle_JSIOCSAXMAP(joydev, argp, _IOC_SIZE(cmd));
  464. case (JSIOCGAXMAP & ~IOCSIZE_MASK):
  465. len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam));
  466. return copy_to_user(argp, joydev->abspam, len) ? -EFAULT : len;
  467. case (JSIOCSBTNMAP & ~IOCSIZE_MASK):
  468. return joydev_handle_JSIOCSBTNMAP(joydev, argp, _IOC_SIZE(cmd));
  469. case (JSIOCGBTNMAP & ~IOCSIZE_MASK):
  470. len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam));
  471. return copy_to_user(argp, joydev->keypam, len) ? -EFAULT : len;
  472. case JSIOCGNAME(0):
  473. name = dev->name;
  474. if (!name)
  475. return 0;
  476. len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1);
  477. return copy_to_user(argp, name, len) ? -EFAULT : len;
  478. }
  479. return -EINVAL;
  480. }
  481. #ifdef CONFIG_COMPAT
  482. static long joydev_compat_ioctl(struct file *file,
  483. unsigned int cmd, unsigned long arg)
  484. {
  485. struct joydev_client *client = file->private_data;
  486. struct joydev *joydev = client->joydev;
  487. void __user *argp = (void __user *)arg;
  488. s32 tmp32;
  489. struct JS_DATA_SAVE_TYPE_32 ds32;
  490. int retval;
  491. retval = mutex_lock_interruptible(&joydev->mutex);
  492. if (retval)
  493. return retval;
  494. if (!joydev->exist) {
  495. retval = -ENODEV;
  496. goto out;
  497. }
  498. switch (cmd) {
  499. case JS_SET_TIMELIMIT:
  500. retval = get_user(tmp32, (s32 __user *) arg);
  501. if (retval == 0)
  502. joydev->glue.JS_TIMELIMIT = tmp32;
  503. break;
  504. case JS_GET_TIMELIMIT:
  505. tmp32 = joydev->glue.JS_TIMELIMIT;
  506. retval = put_user(tmp32, (s32 __user *) arg);
  507. break;
  508. case JS_SET_ALL:
  509. retval = copy_from_user(&ds32, argp,
  510. sizeof(ds32)) ? -EFAULT : 0;
  511. if (retval == 0) {
  512. joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
  513. joydev->glue.BUSY = ds32.BUSY;
  514. joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
  515. joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
  516. joydev->glue.JS_SAVE = ds32.JS_SAVE;
  517. joydev->glue.JS_CORR = ds32.JS_CORR;
  518. }
  519. break;
  520. case JS_GET_ALL:
  521. ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
  522. ds32.BUSY = joydev->glue.BUSY;
  523. ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
  524. ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
  525. ds32.JS_SAVE = joydev->glue.JS_SAVE;
  526. ds32.JS_CORR = joydev->glue.JS_CORR;
  527. retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
  528. break;
  529. default:
  530. retval = joydev_ioctl_common(joydev, cmd, argp);
  531. break;
  532. }
  533. out:
  534. mutex_unlock(&joydev->mutex);
  535. return retval;
  536. }
  537. #endif /* CONFIG_COMPAT */
  538. static long joydev_ioctl(struct file *file,
  539. unsigned int cmd, unsigned long arg)
  540. {
  541. struct joydev_client *client = file->private_data;
  542. struct joydev *joydev = client->joydev;
  543. void __user *argp = (void __user *)arg;
  544. int retval;
  545. retval = mutex_lock_interruptible(&joydev->mutex);
  546. if (retval)
  547. return retval;
  548. if (!joydev->exist) {
  549. retval = -ENODEV;
  550. goto out;
  551. }
  552. switch (cmd) {
  553. case JS_SET_TIMELIMIT:
  554. retval = get_user(joydev->glue.JS_TIMELIMIT,
  555. (long __user *) arg);
  556. break;
  557. case JS_GET_TIMELIMIT:
  558. retval = put_user(joydev->glue.JS_TIMELIMIT,
  559. (long __user *) arg);
  560. break;
  561. case JS_SET_ALL:
  562. retval = copy_from_user(&joydev->glue, argp,
  563. sizeof(joydev->glue)) ? -EFAULT: 0;
  564. break;
  565. case JS_GET_ALL:
  566. retval = copy_to_user(argp, &joydev->glue,
  567. sizeof(joydev->glue)) ? -EFAULT : 0;
  568. break;
  569. default:
  570. retval = joydev_ioctl_common(joydev, cmd, argp);
  571. break;
  572. }
  573. out:
  574. mutex_unlock(&joydev->mutex);
  575. return retval;
  576. }
  577. static const struct file_operations joydev_fops = {
  578. .owner = THIS_MODULE,
  579. .read = joydev_read,
  580. .poll = joydev_poll,
  581. .open = joydev_open,
  582. .release = joydev_release,
  583. .unlocked_ioctl = joydev_ioctl,
  584. #ifdef CONFIG_COMPAT
  585. .compat_ioctl = joydev_compat_ioctl,
  586. #endif
  587. .fasync = joydev_fasync,
  588. };
  589. static int joydev_install_chrdev(struct joydev *joydev)
  590. {
  591. joydev_table[joydev->minor] = joydev;
  592. return 0;
  593. }
  594. static void joydev_remove_chrdev(struct joydev *joydev)
  595. {
  596. mutex_lock(&joydev_table_mutex);
  597. joydev_table[joydev->minor] = NULL;
  598. mutex_unlock(&joydev_table_mutex);
  599. }
  600. /*
  601. * Mark device non-existant. This disables writes, ioctls and
  602. * prevents new users from opening the device. Already posted
  603. * blocking reads will stay, however new ones will fail.
  604. */
  605. static void joydev_mark_dead(struct joydev *joydev)
  606. {
  607. mutex_lock(&joydev->mutex);
  608. joydev->exist = 0;
  609. mutex_unlock(&joydev->mutex);
  610. }
  611. static void joydev_cleanup(struct joydev *joydev)
  612. {
  613. struct input_handle *handle = &joydev->handle;
  614. joydev_mark_dead(joydev);
  615. joydev_hangup(joydev);
  616. joydev_remove_chrdev(joydev);
  617. /* joydev is marked dead so noone else accesses joydev->open */
  618. if (joydev->open)
  619. input_close_device(handle);
  620. }
  621. static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
  622. const struct input_device_id *id)
  623. {
  624. struct joydev *joydev;
  625. int i, j, t, minor;
  626. int error;
  627. for (minor = 0; minor < JOYDEV_MINORS; minor++)
  628. if (!joydev_table[minor])
  629. break;
  630. if (minor == JOYDEV_MINORS) {
  631. printk(KERN_ERR "joydev: no more free joydev devices\n");
  632. return -ENFILE;
  633. }
  634. joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
  635. if (!joydev)
  636. return -ENOMEM;
  637. INIT_LIST_HEAD(&joydev->client_list);
  638. spin_lock_init(&joydev->client_lock);
  639. mutex_init(&joydev->mutex);
  640. init_waitqueue_head(&joydev->wait);
  641. dev_set_name(&joydev->dev, "js%d", minor);
  642. joydev->exist = 1;
  643. joydev->minor = minor;
  644. joydev->exist = 1;
  645. joydev->handle.dev = input_get_device(dev);
  646. joydev->handle.name = dev_name(&joydev->dev);
  647. joydev->handle.handler = handler;
  648. joydev->handle.private = joydev;
  649. for (i = 0; i < ABS_MAX + 1; i++)
  650. if (test_bit(i, dev->absbit)) {
  651. joydev->absmap[i] = joydev->nabs;
  652. joydev->abspam[joydev->nabs] = i;
  653. joydev->nabs++;
  654. }
  655. for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
  656. if (test_bit(i + BTN_MISC, dev->keybit)) {
  657. joydev->keymap[i] = joydev->nkey;
  658. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  659. joydev->nkey++;
  660. }
  661. for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
  662. if (test_bit(i + BTN_MISC, dev->keybit)) {
  663. joydev->keymap[i] = joydev->nkey;
  664. joydev->keypam[joydev->nkey] = i + BTN_MISC;
  665. joydev->nkey++;
  666. }
  667. for (i = 0; i < joydev->nabs; i++) {
  668. j = joydev->abspam[i];
  669. if (dev->absmax[j] == dev->absmin[j]) {
  670. joydev->corr[i].type = JS_CORR_NONE;
  671. joydev->abs[i] = dev->abs[j];
  672. continue;
  673. }
  674. joydev->corr[i].type = JS_CORR_BROKEN;
  675. joydev->corr[i].prec = dev->absfuzz[j];
  676. joydev->corr[i].coef[0] =
  677. (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
  678. joydev->corr[i].coef[1] =
  679. (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
  680. t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j];
  681. if (t) {
  682. joydev->corr[i].coef[2] = (1 << 29) / t;
  683. joydev->corr[i].coef[3] = (1 << 29) / t;
  684. joydev->abs[i] = joydev_correct(dev->abs[j],
  685. joydev->corr + i);
  686. }
  687. }
  688. joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
  689. joydev->dev.class = &input_class;
  690. joydev->dev.parent = &dev->dev;
  691. joydev->dev.release = joydev_free;
  692. device_initialize(&joydev->dev);
  693. error = input_register_handle(&joydev->handle);
  694. if (error)
  695. goto err_free_joydev;
  696. error = joydev_install_chrdev(joydev);
  697. if (error)
  698. goto err_unregister_handle;
  699. error = device_add(&joydev->dev);
  700. if (error)
  701. goto err_cleanup_joydev;
  702. return 0;
  703. err_cleanup_joydev:
  704. joydev_cleanup(joydev);
  705. err_unregister_handle:
  706. input_unregister_handle(&joydev->handle);
  707. err_free_joydev:
  708. put_device(&joydev->dev);
  709. return error;
  710. }
  711. static void joydev_disconnect(struct input_handle *handle)
  712. {
  713. struct joydev *joydev = handle->private;
  714. device_del(&joydev->dev);
  715. joydev_cleanup(joydev);
  716. input_unregister_handle(handle);
  717. put_device(&joydev->dev);
  718. }
  719. static const struct input_device_id joydev_blacklist[] = {
  720. {
  721. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  722. INPUT_DEVICE_ID_MATCH_KEYBIT,
  723. .evbit = { BIT_MASK(EV_KEY) },
  724. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  725. }, /* Avoid itouchpads and touchscreens */
  726. {
  727. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  728. INPUT_DEVICE_ID_MATCH_KEYBIT,
  729. .evbit = { BIT_MASK(EV_KEY) },
  730. .keybit = { [BIT_WORD(BTN_DIGI)] = BIT_MASK(BTN_DIGI) },
  731. }, /* Avoid tablets, digitisers and similar devices */
  732. { } /* Terminating entry */
  733. };
  734. static const struct input_device_id joydev_ids[] = {
  735. {
  736. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  737. INPUT_DEVICE_ID_MATCH_ABSBIT,
  738. .evbit = { BIT_MASK(EV_ABS) },
  739. .absbit = { BIT_MASK(ABS_X) },
  740. },
  741. {
  742. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  743. INPUT_DEVICE_ID_MATCH_ABSBIT,
  744. .evbit = { BIT_MASK(EV_ABS) },
  745. .absbit = { BIT_MASK(ABS_WHEEL) },
  746. },
  747. {
  748. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  749. INPUT_DEVICE_ID_MATCH_ABSBIT,
  750. .evbit = { BIT_MASK(EV_ABS) },
  751. .absbit = { BIT_MASK(ABS_THROTTLE) },
  752. },
  753. { } /* Terminating entry */
  754. };
  755. MODULE_DEVICE_TABLE(input, joydev_ids);
  756. static struct input_handler joydev_handler = {
  757. .event = joydev_event,
  758. .connect = joydev_connect,
  759. .disconnect = joydev_disconnect,
  760. .fops = &joydev_fops,
  761. .minor = JOYDEV_MINOR_BASE,
  762. .name = "joydev",
  763. .id_table = joydev_ids,
  764. .blacklist = joydev_blacklist,
  765. };
  766. static int __init joydev_init(void)
  767. {
  768. return input_register_handler(&joydev_handler);
  769. }
  770. static void __exit joydev_exit(void)
  771. {
  772. input_unregister_handler(&joydev_handler);
  773. }
  774. module_init(joydev_init);
  775. module_exit(joydev_exit);