joydev.c 21 KB

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