joydev.c 21 KB

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