mousedev.c 26 KB

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