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