mousedev.c 26 KB

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