mousedev.c 26 KB

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