mousedev.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * Input driver to ExplorerPS/2 device driver module.
  3. *
  4. * Copyright (c) 1999-2002 Vojtech Pavlik
  5. * Copyright (c) 2004 Dmitry Torokhov
  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 version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #define MOUSEDEV_MINOR_BASE 32
  12. #define MOUSEDEV_MINORS 32
  13. #define MOUSEDEV_MIX 31
  14. #include <linux/slab.h>
  15. #include <linux/smp_lock.h>
  16. #include <linux/poll.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/input.h>
  20. #include <linux/random.h>
  21. #include <linux/major.h>
  22. #include <linux/device.h>
  23. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  24. #include <linux/miscdevice.h>
  25. #endif
  26. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  27. MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
  28. MODULE_LICENSE("GPL");
  29. #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
  30. #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
  31. #endif
  32. #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
  33. #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
  34. #endif
  35. static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
  36. module_param(xres, uint, 0644);
  37. MODULE_PARM_DESC(xres, "Horizontal screen resolution");
  38. static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
  39. module_param(yres, uint, 0644);
  40. MODULE_PARM_DESC(yres, "Vertical screen resolution");
  41. static unsigned tap_time = 200;
  42. module_param(tap_time, uint, 0644);
  43. MODULE_PARM_DESC(tap_time, "Tap time for touchpads in absolute mode (msecs)");
  44. struct mousedev_hw_data {
  45. int dx, dy, dz;
  46. int x, y;
  47. int abs_event;
  48. unsigned long buttons;
  49. };
  50. struct mousedev {
  51. int exist;
  52. int open;
  53. int minor;
  54. char name[16];
  55. struct input_handle handle;
  56. wait_queue_head_t wait;
  57. struct list_head client_list;
  58. spinlock_t client_lock; /* protects client_list */
  59. struct mutex mutex;
  60. struct device dev;
  61. struct list_head mixdev_node;
  62. int mixdev_open;
  63. struct mousedev_hw_data packet;
  64. unsigned int pkt_count;
  65. int old_x[4], old_y[4];
  66. int frac_dx, frac_dy;
  67. unsigned long touch;
  68. };
  69. enum mousedev_emul {
  70. MOUSEDEV_EMUL_PS2,
  71. MOUSEDEV_EMUL_IMPS,
  72. MOUSEDEV_EMUL_EXPS
  73. };
  74. struct mousedev_motion {
  75. int dx, dy, dz;
  76. unsigned long buttons;
  77. };
  78. #define PACKET_QUEUE_LEN 16
  79. struct mousedev_client {
  80. struct fasync_struct *fasync;
  81. struct mousedev *mousedev;
  82. struct list_head node;
  83. struct mousedev_motion packets[PACKET_QUEUE_LEN];
  84. unsigned int head, tail;
  85. spinlock_t packet_lock;
  86. int pos_x, pos_y;
  87. signed char ps2[6];
  88. unsigned char ready, buffer, bufsiz;
  89. unsigned char imexseq, impsseq;
  90. enum mousedev_emul mode;
  91. unsigned long last_buttons;
  92. };
  93. #define MOUSEDEV_SEQ_LEN 6
  94. static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
  95. static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
  96. static struct input_handler mousedev_handler;
  97. static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
  98. static DEFINE_MUTEX(mousedev_table_mutex);
  99. static struct mousedev *mousedev_mix;
  100. static LIST_HEAD(mousedev_mix_list);
  101. static void mixdev_open_devices(void);
  102. static void mixdev_close_devices(void);
  103. #define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
  104. #define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
  105. static void mousedev_touchpad_event(struct input_dev *dev,
  106. struct mousedev *mousedev,
  107. unsigned int code, int value)
  108. {
  109. int size, tmp;
  110. enum { FRACTION_DENOM = 128 };
  111. switch (code) {
  112. case ABS_X:
  113. fx(0) = value;
  114. if (mousedev->touch && mousedev->pkt_count >= 2) {
  115. size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
  116. if (size == 0)
  117. size = 256 * 2;
  118. tmp = ((value - fx(2)) * 256 * FRACTION_DENOM) / size;
  119. tmp += mousedev->frac_dx;
  120. mousedev->packet.dx = tmp / FRACTION_DENOM;
  121. mousedev->frac_dx =
  122. tmp - mousedev->packet.dx * FRACTION_DENOM;
  123. }
  124. break;
  125. case ABS_Y:
  126. fy(0) = value;
  127. if (mousedev->touch && mousedev->pkt_count >= 2) {
  128. /* use X size to keep the same scale */
  129. size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
  130. if (size == 0)
  131. size = 256 * 2;
  132. tmp = -((value - fy(2)) * 256 * FRACTION_DENOM) / size;
  133. tmp += mousedev->frac_dy;
  134. mousedev->packet.dy = tmp / FRACTION_DENOM;
  135. mousedev->frac_dy = tmp -
  136. mousedev->packet.dy * FRACTION_DENOM;
  137. }
  138. break;
  139. }
  140. }
  141. static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev,
  142. unsigned int code, int value)
  143. {
  144. int size;
  145. switch (code) {
  146. case ABS_X:
  147. size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
  148. if (size == 0)
  149. size = xres ? : 1;
  150. if (value > dev->absmax[ABS_X])
  151. value = dev->absmax[ABS_X];
  152. if (value < dev->absmin[ABS_X])
  153. value = dev->absmin[ABS_X];
  154. mousedev->packet.x =
  155. ((value - dev->absmin[ABS_X]) * xres) / size;
  156. mousedev->packet.abs_event = 1;
  157. break;
  158. case ABS_Y:
  159. size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
  160. if (size == 0)
  161. size = yres ? : 1;
  162. if (value > dev->absmax[ABS_Y])
  163. value = dev->absmax[ABS_Y];
  164. if (value < dev->absmin[ABS_Y])
  165. value = dev->absmin[ABS_Y];
  166. mousedev->packet.y = yres -
  167. ((value - dev->absmin[ABS_Y]) * yres) / size;
  168. mousedev->packet.abs_event = 1;
  169. break;
  170. }
  171. }
  172. static void mousedev_rel_event(struct mousedev *mousedev,
  173. unsigned int code, int value)
  174. {
  175. switch (code) {
  176. case REL_X:
  177. mousedev->packet.dx += value;
  178. break;
  179. case REL_Y:
  180. mousedev->packet.dy -= value;
  181. break;
  182. case REL_WHEEL:
  183. mousedev->packet.dz -= value;
  184. break;
  185. }
  186. }
  187. static void mousedev_key_event(struct mousedev *mousedev,
  188. unsigned int code, int value)
  189. {
  190. int index;
  191. switch (code) {
  192. case BTN_TOUCH:
  193. case BTN_0:
  194. case BTN_LEFT: index = 0; break;
  195. case BTN_STYLUS:
  196. case BTN_1:
  197. case BTN_RIGHT: index = 1; break;
  198. case BTN_2:
  199. case BTN_FORWARD:
  200. case BTN_STYLUS2:
  201. case BTN_MIDDLE: index = 2; break;
  202. case BTN_3:
  203. case BTN_BACK:
  204. case BTN_SIDE: index = 3; break;
  205. case BTN_4:
  206. case BTN_EXTRA: index = 4; break;
  207. default: return;
  208. }
  209. if (value) {
  210. set_bit(index, &mousedev->packet.buttons);
  211. set_bit(index, &mousedev_mix->packet.buttons);
  212. } else {
  213. clear_bit(index, &mousedev->packet.buttons);
  214. clear_bit(index, &mousedev_mix->packet.buttons);
  215. }
  216. }
  217. static void mousedev_notify_readers(struct mousedev *mousedev,
  218. struct mousedev_hw_data *packet)
  219. {
  220. struct mousedev_client *client;
  221. struct mousedev_motion *p;
  222. unsigned int new_head;
  223. int wake_readers = 0;
  224. rcu_read_lock();
  225. list_for_each_entry_rcu(client, &mousedev->client_list, node) {
  226. /* Just acquire the lock, interrupts already disabled */
  227. spin_lock(&client->packet_lock);
  228. p = &client->packets[client->head];
  229. if (client->ready && p->buttons != mousedev->packet.buttons) {
  230. new_head = (client->head + 1) % PACKET_QUEUE_LEN;
  231. if (new_head != client->tail) {
  232. p = &client->packets[client->head = new_head];
  233. memset(p, 0, sizeof(struct mousedev_motion));
  234. }
  235. }
  236. if (packet->abs_event) {
  237. p->dx += packet->x - client->pos_x;
  238. p->dy += packet->y - client->pos_y;
  239. client->pos_x = packet->x;
  240. client->pos_y = packet->y;
  241. }
  242. client->pos_x += packet->dx;
  243. client->pos_x = client->pos_x < 0 ?
  244. 0 : (client->pos_x >= xres ? xres : client->pos_x);
  245. client->pos_y += packet->dy;
  246. client->pos_y = client->pos_y < 0 ?
  247. 0 : (client->pos_y >= yres ? yres : client->pos_y);
  248. p->dx += packet->dx;
  249. p->dy += packet->dy;
  250. p->dz += packet->dz;
  251. p->buttons = mousedev->packet.buttons;
  252. if (p->dx || p->dy || p->dz ||
  253. p->buttons != client->last_buttons)
  254. client->ready = 1;
  255. spin_unlock(&client->packet_lock);
  256. if (client->ready) {
  257. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  258. wake_readers = 1;
  259. }
  260. }
  261. rcu_read_unlock();
  262. if (wake_readers)
  263. wake_up_interruptible(&mousedev->wait);
  264. }
  265. static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
  266. {
  267. if (!value) {
  268. if (mousedev->touch &&
  269. time_before(jiffies,
  270. mousedev->touch + msecs_to_jiffies(tap_time))) {
  271. /*
  272. * Toggle left button to emulate tap.
  273. * We rely on the fact that mousedev_mix always has 0
  274. * motion packet so we won't mess current position.
  275. */
  276. set_bit(0, &mousedev->packet.buttons);
  277. set_bit(0, &mousedev_mix->packet.buttons);
  278. mousedev_notify_readers(mousedev, &mousedev_mix->packet);
  279. mousedev_notify_readers(mousedev_mix,
  280. &mousedev_mix->packet);
  281. clear_bit(0, &mousedev->packet.buttons);
  282. clear_bit(0, &mousedev_mix->packet.buttons);
  283. }
  284. mousedev->touch = mousedev->pkt_count = 0;
  285. mousedev->frac_dx = 0;
  286. mousedev->frac_dy = 0;
  287. } else if (!mousedev->touch)
  288. mousedev->touch = jiffies;
  289. }
  290. static void mousedev_event(struct input_handle *handle,
  291. unsigned int type, unsigned int code, int value)
  292. {
  293. struct mousedev *mousedev = handle->private;
  294. switch (type) {
  295. case EV_ABS:
  296. /* Ignore joysticks */
  297. if (test_bit(BTN_TRIGGER, handle->dev->keybit))
  298. return;
  299. if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
  300. mousedev_touchpad_event(handle->dev,
  301. mousedev, code, value);
  302. else
  303. mousedev_abs_event(handle->dev, mousedev, code, value);
  304. break;
  305. case EV_REL:
  306. mousedev_rel_event(mousedev, code, value);
  307. break;
  308. case EV_KEY:
  309. if (value != 2) {
  310. if (code == BTN_TOUCH &&
  311. test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
  312. mousedev_touchpad_touch(mousedev, value);
  313. else
  314. mousedev_key_event(mousedev, code, value);
  315. }
  316. break;
  317. case EV_SYN:
  318. if (code == SYN_REPORT) {
  319. if (mousedev->touch) {
  320. mousedev->pkt_count++;
  321. /*
  322. * Input system eats duplicate events,
  323. * but we need all of them to do correct
  324. * averaging so apply present one forward
  325. */
  326. fx(0) = fx(1);
  327. fy(0) = fy(1);
  328. }
  329. mousedev_notify_readers(mousedev, &mousedev->packet);
  330. mousedev_notify_readers(mousedev_mix, &mousedev->packet);
  331. mousedev->packet.dx = mousedev->packet.dy =
  332. mousedev->packet.dz = 0;
  333. mousedev->packet.abs_event = 0;
  334. }
  335. break;
  336. }
  337. }
  338. static int mousedev_fasync(int fd, struct file *file, int on)
  339. {
  340. int retval;
  341. struct mousedev_client *client = file->private_data;
  342. retval = fasync_helper(fd, file, on, &client->fasync);
  343. return retval < 0 ? retval : 0;
  344. }
  345. static void mousedev_free(struct device *dev)
  346. {
  347. struct mousedev *mousedev = container_of(dev, struct mousedev, dev);
  348. input_put_device(mousedev->handle.dev);
  349. kfree(mousedev);
  350. }
  351. static int mousedev_open_device(struct mousedev *mousedev)
  352. {
  353. int retval;
  354. retval = mutex_lock_interruptible(&mousedev->mutex);
  355. if (retval)
  356. return retval;
  357. if (mousedev->minor == MOUSEDEV_MIX)
  358. mixdev_open_devices();
  359. else if (!mousedev->exist)
  360. retval = -ENODEV;
  361. else if (!mousedev->open++) {
  362. retval = input_open_device(&mousedev->handle);
  363. if (retval)
  364. mousedev->open--;
  365. }
  366. mutex_unlock(&mousedev->mutex);
  367. return retval;
  368. }
  369. static void mousedev_close_device(struct mousedev *mousedev)
  370. {
  371. mutex_lock(&mousedev->mutex);
  372. if (mousedev->minor == MOUSEDEV_MIX)
  373. mixdev_close_devices();
  374. else if (mousedev->exist && !--mousedev->open)
  375. input_close_device(&mousedev->handle);
  376. mutex_unlock(&mousedev->mutex);
  377. }
  378. /*
  379. * Open all available devices so they can all be multiplexed in one.
  380. * stream. Note that this function is called with mousedev_mix->mutex
  381. * held.
  382. */
  383. static void mixdev_open_devices(void)
  384. {
  385. struct mousedev *mousedev;
  386. if (mousedev_mix->open++)
  387. return;
  388. list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
  389. if (!mousedev->mixdev_open) {
  390. if (mousedev_open_device(mousedev))
  391. continue;
  392. mousedev->mixdev_open = 1;
  393. }
  394. }
  395. }
  396. /*
  397. * Close all devices that were opened as part of multiplexed
  398. * device. Note that this function is called with mousedev_mix->mutex
  399. * held.
  400. */
  401. static void mixdev_close_devices(void)
  402. {
  403. struct mousedev *mousedev;
  404. if (--mousedev_mix->open)
  405. return;
  406. list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
  407. if (mousedev->mixdev_open) {
  408. mousedev->mixdev_open = 0;
  409. mousedev_close_device(mousedev);
  410. }
  411. }
  412. }
  413. static void mousedev_attach_client(struct mousedev *mousedev,
  414. struct mousedev_client *client)
  415. {
  416. spin_lock(&mousedev->client_lock);
  417. list_add_tail_rcu(&client->node, &mousedev->client_list);
  418. spin_unlock(&mousedev->client_lock);
  419. synchronize_rcu();
  420. }
  421. static void mousedev_detach_client(struct mousedev *mousedev,
  422. struct mousedev_client *client)
  423. {
  424. spin_lock(&mousedev->client_lock);
  425. list_del_rcu(&client->node);
  426. spin_unlock(&mousedev->client_lock);
  427. synchronize_rcu();
  428. }
  429. static int mousedev_release(struct inode *inode, struct file *file)
  430. {
  431. struct mousedev_client *client = file->private_data;
  432. struct mousedev *mousedev = client->mousedev;
  433. mousedev_fasync(-1, file, 0);
  434. mousedev_detach_client(mousedev, client);
  435. kfree(client);
  436. mousedev_close_device(mousedev);
  437. put_device(&mousedev->dev);
  438. return 0;
  439. }
  440. static int mousedev_open(struct inode *inode, struct file *file)
  441. {
  442. struct mousedev_client *client;
  443. struct mousedev *mousedev;
  444. int error;
  445. int i;
  446. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  447. if (imajor(inode) == MISC_MAJOR)
  448. i = MOUSEDEV_MIX;
  449. else
  450. #endif
  451. i = iminor(inode) - MOUSEDEV_MINOR_BASE;
  452. if (i >= MOUSEDEV_MINORS)
  453. return -ENODEV;
  454. lock_kernel();
  455. error = mutex_lock_interruptible(&mousedev_table_mutex);
  456. if (error) {
  457. unlock_kernel();
  458. return error;
  459. }
  460. mousedev = mousedev_table[i];
  461. if (mousedev)
  462. get_device(&mousedev->dev);
  463. mutex_unlock(&mousedev_table_mutex);
  464. if (!mousedev) {
  465. unlock_kernel();
  466. return -ENODEV;
  467. }
  468. client = kzalloc(sizeof(struct mousedev_client), GFP_KERNEL);
  469. if (!client) {
  470. error = -ENOMEM;
  471. goto err_put_mousedev;
  472. }
  473. spin_lock_init(&client->packet_lock);
  474. client->pos_x = xres / 2;
  475. client->pos_y = yres / 2;
  476. client->mousedev = mousedev;
  477. mousedev_attach_client(mousedev, client);
  478. error = mousedev_open_device(mousedev);
  479. if (error)
  480. goto err_free_client;
  481. file->private_data = client;
  482. unlock_kernel();
  483. return 0;
  484. err_free_client:
  485. mousedev_detach_client(mousedev, client);
  486. kfree(client);
  487. err_put_mousedev:
  488. put_device(&mousedev->dev);
  489. unlock_kernel();
  490. return error;
  491. }
  492. static inline int mousedev_limit_delta(int delta, int limit)
  493. {
  494. return delta > limit ? limit : (delta < -limit ? -limit : delta);
  495. }
  496. static void mousedev_packet(struct mousedev_client *client,
  497. signed char *ps2_data)
  498. {
  499. struct mousedev_motion *p = &client->packets[client->tail];
  500. ps2_data[0] = 0x08 |
  501. ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
  502. ps2_data[1] = mousedev_limit_delta(p->dx, 127);
  503. ps2_data[2] = mousedev_limit_delta(p->dy, 127);
  504. p->dx -= ps2_data[1];
  505. p->dy -= ps2_data[2];
  506. switch (client->mode) {
  507. case MOUSEDEV_EMUL_EXPS:
  508. ps2_data[3] = mousedev_limit_delta(p->dz, 7);
  509. p->dz -= ps2_data[3];
  510. ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
  511. client->bufsiz = 4;
  512. break;
  513. case MOUSEDEV_EMUL_IMPS:
  514. ps2_data[0] |=
  515. ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
  516. ps2_data[3] = mousedev_limit_delta(p->dz, 127);
  517. p->dz -= ps2_data[3];
  518. client->bufsiz = 4;
  519. break;
  520. case MOUSEDEV_EMUL_PS2:
  521. default:
  522. ps2_data[0] |=
  523. ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
  524. p->dz = 0;
  525. client->bufsiz = 3;
  526. break;
  527. }
  528. if (!p->dx && !p->dy && !p->dz) {
  529. if (client->tail == client->head) {
  530. client->ready = 0;
  531. client->last_buttons = p->buttons;
  532. } else
  533. client->tail = (client->tail + 1) % PACKET_QUEUE_LEN;
  534. }
  535. }
  536. static void mousedev_generate_response(struct mousedev_client *client,
  537. int command)
  538. {
  539. client->ps2[0] = 0xfa; /* ACK */
  540. switch (command) {
  541. case 0xeb: /* Poll */
  542. mousedev_packet(client, &client->ps2[1]);
  543. client->bufsiz++; /* account for leading ACK */
  544. break;
  545. case 0xf2: /* Get ID */
  546. switch (client->mode) {
  547. case MOUSEDEV_EMUL_PS2:
  548. client->ps2[1] = 0;
  549. break;
  550. case MOUSEDEV_EMUL_IMPS:
  551. client->ps2[1] = 3;
  552. break;
  553. case MOUSEDEV_EMUL_EXPS:
  554. client->ps2[1] = 4;
  555. break;
  556. }
  557. client->bufsiz = 2;
  558. break;
  559. case 0xe9: /* Get info */
  560. client->ps2[1] = 0x60; client->ps2[2] = 3; client->ps2[3] = 200;
  561. client->bufsiz = 4;
  562. break;
  563. case 0xff: /* Reset */
  564. client->impsseq = client->imexseq = 0;
  565. client->mode = MOUSEDEV_EMUL_PS2;
  566. client->ps2[1] = 0xaa; client->ps2[2] = 0x00;
  567. client->bufsiz = 3;
  568. break;
  569. default:
  570. client->bufsiz = 1;
  571. break;
  572. }
  573. client->buffer = client->bufsiz;
  574. }
  575. static ssize_t mousedev_write(struct file *file, const char __user *buffer,
  576. size_t count, loff_t *ppos)
  577. {
  578. struct mousedev_client *client = file->private_data;
  579. unsigned char c;
  580. unsigned int i;
  581. for (i = 0; i < count; i++) {
  582. if (get_user(c, buffer + i))
  583. return -EFAULT;
  584. spin_lock_irq(&client->packet_lock);
  585. if (c == mousedev_imex_seq[client->imexseq]) {
  586. if (++client->imexseq == MOUSEDEV_SEQ_LEN) {
  587. client->imexseq = 0;
  588. client->mode = MOUSEDEV_EMUL_EXPS;
  589. }
  590. } else
  591. client->imexseq = 0;
  592. if (c == mousedev_imps_seq[client->impsseq]) {
  593. if (++client->impsseq == MOUSEDEV_SEQ_LEN) {
  594. client->impsseq = 0;
  595. client->mode = MOUSEDEV_EMUL_IMPS;
  596. }
  597. } else
  598. client->impsseq = 0;
  599. mousedev_generate_response(client, c);
  600. spin_unlock_irq(&client->packet_lock);
  601. }
  602. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  603. wake_up_interruptible(&client->mousedev->wait);
  604. return count;
  605. }
  606. static ssize_t mousedev_read(struct file *file, char __user *buffer,
  607. size_t count, loff_t *ppos)
  608. {
  609. struct mousedev_client *client = file->private_data;
  610. struct mousedev *mousedev = client->mousedev;
  611. signed char data[sizeof(client->ps2)];
  612. int retval = 0;
  613. if (!client->ready && !client->buffer && mousedev->exist &&
  614. (file->f_flags & O_NONBLOCK))
  615. return -EAGAIN;
  616. retval = wait_event_interruptible(mousedev->wait,
  617. !mousedev->exist || client->ready || client->buffer);
  618. if (retval)
  619. return retval;
  620. if (!mousedev->exist)
  621. return -ENODEV;
  622. spin_lock_irq(&client->packet_lock);
  623. if (!client->buffer && client->ready) {
  624. mousedev_packet(client, client->ps2);
  625. client->buffer = client->bufsiz;
  626. }
  627. if (count > client->buffer)
  628. count = client->buffer;
  629. memcpy(data, client->ps2 + client->bufsiz - client->buffer, count);
  630. client->buffer -= count;
  631. spin_unlock_irq(&client->packet_lock);
  632. if (copy_to_user(buffer, data, count))
  633. return -EFAULT;
  634. return count;
  635. }
  636. /* No kernel lock - fine */
  637. static unsigned int mousedev_poll(struct file *file, poll_table *wait)
  638. {
  639. struct mousedev_client *client = file->private_data;
  640. struct mousedev *mousedev = client->mousedev;
  641. poll_wait(file, &mousedev->wait, wait);
  642. return ((client->ready || client->buffer) ? (POLLIN | POLLRDNORM) : 0) |
  643. (mousedev->exist ? 0 : (POLLHUP | POLLERR));
  644. }
  645. static const struct file_operations mousedev_fops = {
  646. .owner = THIS_MODULE,
  647. .read = mousedev_read,
  648. .write = mousedev_write,
  649. .poll = mousedev_poll,
  650. .open = mousedev_open,
  651. .release = mousedev_release,
  652. .fasync = mousedev_fasync,
  653. };
  654. static int mousedev_install_chrdev(struct mousedev *mousedev)
  655. {
  656. mousedev_table[mousedev->minor] = mousedev;
  657. return 0;
  658. }
  659. static void mousedev_remove_chrdev(struct mousedev *mousedev)
  660. {
  661. mutex_lock(&mousedev_table_mutex);
  662. mousedev_table[mousedev->minor] = NULL;
  663. mutex_unlock(&mousedev_table_mutex);
  664. }
  665. /*
  666. * Mark device non-existent. This disables writes, ioctls and
  667. * prevents new users from opening the device. Already posted
  668. * blocking reads will stay, however new ones will fail.
  669. */
  670. static void mousedev_mark_dead(struct mousedev *mousedev)
  671. {
  672. mutex_lock(&mousedev->mutex);
  673. mousedev->exist = 0;
  674. mutex_unlock(&mousedev->mutex);
  675. }
  676. /*
  677. * Wake up users waiting for IO so they can disconnect from
  678. * dead device.
  679. */
  680. static void mousedev_hangup(struct mousedev *mousedev)
  681. {
  682. struct mousedev_client *client;
  683. spin_lock(&mousedev->client_lock);
  684. list_for_each_entry(client, &mousedev->client_list, node)
  685. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  686. spin_unlock(&mousedev->client_lock);
  687. wake_up_interruptible(&mousedev->wait);
  688. }
  689. static void mousedev_cleanup(struct mousedev *mousedev)
  690. {
  691. struct input_handle *handle = &mousedev->handle;
  692. mousedev_mark_dead(mousedev);
  693. mousedev_hangup(mousedev);
  694. mousedev_remove_chrdev(mousedev);
  695. /* mousedev is marked dead so no one else accesses mousedev->open */
  696. if (mousedev->open)
  697. input_close_device(handle);
  698. }
  699. static struct mousedev *mousedev_create(struct input_dev *dev,
  700. struct input_handler *handler,
  701. int minor)
  702. {
  703. struct mousedev *mousedev;
  704. int error;
  705. mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL);
  706. if (!mousedev) {
  707. error = -ENOMEM;
  708. goto err_out;
  709. }
  710. INIT_LIST_HEAD(&mousedev->client_list);
  711. INIT_LIST_HEAD(&mousedev->mixdev_node);
  712. spin_lock_init(&mousedev->client_lock);
  713. mutex_init(&mousedev->mutex);
  714. lockdep_set_subclass(&mousedev->mutex,
  715. minor == MOUSEDEV_MIX ? MOUSEDEV_MIX : 0);
  716. init_waitqueue_head(&mousedev->wait);
  717. if (minor == MOUSEDEV_MIX)
  718. strlcpy(mousedev->name, "mice", sizeof(mousedev->name));
  719. else
  720. snprintf(mousedev->name, sizeof(mousedev->name),
  721. "mouse%d", minor);
  722. mousedev->minor = minor;
  723. mousedev->exist = 1;
  724. mousedev->handle.dev = input_get_device(dev);
  725. mousedev->handle.name = mousedev->name;
  726. mousedev->handle.handler = handler;
  727. mousedev->handle.private = mousedev;
  728. strlcpy(mousedev->dev.bus_id, mousedev->name,
  729. sizeof(mousedev->dev.bus_id));
  730. mousedev->dev.class = &input_class;
  731. if (dev)
  732. mousedev->dev.parent = &dev->dev;
  733. mousedev->dev.devt = MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor);
  734. mousedev->dev.release = mousedev_free;
  735. device_initialize(&mousedev->dev);
  736. if (minor != MOUSEDEV_MIX) {
  737. error = input_register_handle(&mousedev->handle);
  738. if (error)
  739. goto err_free_mousedev;
  740. }
  741. error = mousedev_install_chrdev(mousedev);
  742. if (error)
  743. goto err_unregister_handle;
  744. error = device_add(&mousedev->dev);
  745. if (error)
  746. goto err_cleanup_mousedev;
  747. return mousedev;
  748. err_cleanup_mousedev:
  749. mousedev_cleanup(mousedev);
  750. err_unregister_handle:
  751. if (minor != MOUSEDEV_MIX)
  752. input_unregister_handle(&mousedev->handle);
  753. err_free_mousedev:
  754. put_device(&mousedev->dev);
  755. err_out:
  756. return ERR_PTR(error);
  757. }
  758. static void mousedev_destroy(struct mousedev *mousedev)
  759. {
  760. device_del(&mousedev->dev);
  761. mousedev_cleanup(mousedev);
  762. if (mousedev->minor != MOUSEDEV_MIX)
  763. input_unregister_handle(&mousedev->handle);
  764. put_device(&mousedev->dev);
  765. }
  766. static int mixdev_add_device(struct mousedev *mousedev)
  767. {
  768. int retval;
  769. retval = mutex_lock_interruptible(&mousedev_mix->mutex);
  770. if (retval)
  771. return retval;
  772. if (mousedev_mix->open) {
  773. retval = mousedev_open_device(mousedev);
  774. if (retval)
  775. goto out;
  776. mousedev->mixdev_open = 1;
  777. }
  778. get_device(&mousedev->dev);
  779. list_add_tail(&mousedev->mixdev_node, &mousedev_mix_list);
  780. out:
  781. mutex_unlock(&mousedev_mix->mutex);
  782. return retval;
  783. }
  784. static void mixdev_remove_device(struct mousedev *mousedev)
  785. {
  786. mutex_lock(&mousedev_mix->mutex);
  787. if (mousedev->mixdev_open) {
  788. mousedev->mixdev_open = 0;
  789. mousedev_close_device(mousedev);
  790. }
  791. list_del_init(&mousedev->mixdev_node);
  792. mutex_unlock(&mousedev_mix->mutex);
  793. put_device(&mousedev->dev);
  794. }
  795. static int mousedev_connect(struct input_handler *handler,
  796. struct input_dev *dev,
  797. const struct input_device_id *id)
  798. {
  799. struct mousedev *mousedev;
  800. int minor;
  801. int error;
  802. for (minor = 0; minor < MOUSEDEV_MINORS; minor++)
  803. if (!mousedev_table[minor])
  804. break;
  805. if (minor == MOUSEDEV_MINORS) {
  806. printk(KERN_ERR "mousedev: no more free mousedev devices\n");
  807. return -ENFILE;
  808. }
  809. mousedev = mousedev_create(dev, handler, minor);
  810. if (IS_ERR(mousedev))
  811. return PTR_ERR(mousedev);
  812. error = mixdev_add_device(mousedev);
  813. if (error) {
  814. mousedev_destroy(mousedev);
  815. return error;
  816. }
  817. return 0;
  818. }
  819. static void mousedev_disconnect(struct input_handle *handle)
  820. {
  821. struct mousedev *mousedev = handle->private;
  822. mixdev_remove_device(mousedev);
  823. mousedev_destroy(mousedev);
  824. }
  825. static const struct input_device_id mousedev_ids[] = {
  826. {
  827. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  828. INPUT_DEVICE_ID_MATCH_KEYBIT |
  829. INPUT_DEVICE_ID_MATCH_RELBIT,
  830. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) },
  831. .keybit = { [BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) },
  832. .relbit = { BIT_MASK(REL_X) | BIT_MASK(REL_Y) },
  833. }, /* A mouse like device, at least one button,
  834. two relative axes */
  835. {
  836. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  837. INPUT_DEVICE_ID_MATCH_RELBIT,
  838. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) },
  839. .relbit = { BIT_MASK(REL_WHEEL) },
  840. }, /* A separate scrollwheel */
  841. {
  842. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  843. INPUT_DEVICE_ID_MATCH_KEYBIT |
  844. INPUT_DEVICE_ID_MATCH_ABSBIT,
  845. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) },
  846. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  847. .absbit = { BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
  848. }, /* A tablet like device, at least touch detection,
  849. two absolute axes */
  850. {
  851. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  852. INPUT_DEVICE_ID_MATCH_KEYBIT |
  853. INPUT_DEVICE_ID_MATCH_ABSBIT,
  854. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) },
  855. .keybit = { [BIT_WORD(BTN_TOOL_FINGER)] =
  856. BIT_MASK(BTN_TOOL_FINGER) },
  857. .absbit = { BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
  858. BIT_MASK(ABS_PRESSURE) |
  859. BIT_MASK(ABS_TOOL_WIDTH) },
  860. }, /* A touchpad */
  861. {
  862. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  863. INPUT_DEVICE_ID_MATCH_KEYBIT |
  864. INPUT_DEVICE_ID_MATCH_ABSBIT,
  865. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) },
  866. .keybit = { [BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) },
  867. .absbit = { BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
  868. }, /* Mouse-like device with absolute X and Y but ordinary
  869. clicks, like hp ILO2 High Performance mouse */
  870. { }, /* Terminating entry */
  871. };
  872. MODULE_DEVICE_TABLE(input, mousedev_ids);
  873. static struct input_handler mousedev_handler = {
  874. .event = mousedev_event,
  875. .connect = mousedev_connect,
  876. .disconnect = mousedev_disconnect,
  877. .fops = &mousedev_fops,
  878. .minor = MOUSEDEV_MINOR_BASE,
  879. .name = "mousedev",
  880. .id_table = mousedev_ids,
  881. };
  882. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  883. static struct miscdevice psaux_mouse = {
  884. PSMOUSE_MINOR, "psaux", &mousedev_fops
  885. };
  886. static int psaux_registered;
  887. #endif
  888. static int __init mousedev_init(void)
  889. {
  890. int error;
  891. mousedev_mix = mousedev_create(NULL, &mousedev_handler, MOUSEDEV_MIX);
  892. if (IS_ERR(mousedev_mix))
  893. return PTR_ERR(mousedev_mix);
  894. error = input_register_handler(&mousedev_handler);
  895. if (error) {
  896. mousedev_destroy(mousedev_mix);
  897. return error;
  898. }
  899. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  900. error = misc_register(&psaux_mouse);
  901. if (error)
  902. printk(KERN_WARNING "mice: could not register psaux device, "
  903. "error: %d\n", error);
  904. else
  905. psaux_registered = 1;
  906. #endif
  907. printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
  908. return 0;
  909. }
  910. static void __exit mousedev_exit(void)
  911. {
  912. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  913. if (psaux_registered)
  914. misc_deregister(&psaux_mouse);
  915. #endif
  916. input_unregister_handler(&mousedev_handler);
  917. mousedev_destroy(mousedev_mix);
  918. }
  919. module_init(mousedev_init);
  920. module_exit(mousedev_exit);