joydev.c 21 KB

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