joydev.c 21 KB

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