mousedev.c 26 KB

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