mousedev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * Input driver to ExplorerPS/2 device driver module.
  3. *
  4. * Copyright (c) 1999-2002 Vojtech Pavlik
  5. * Copyright (c) 2004 Dmitry Torokhov
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #define MOUSEDEV_MINOR_BASE 32
  12. #define MOUSEDEV_MINORS 32
  13. #define MOUSEDEV_MIX 31
  14. #include <linux/slab.h>
  15. #include <linux/poll.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.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. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  24. #include <linux/miscdevice.h>
  25. #endif
  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 exist;
  52. int open;
  53. int minor;
  54. char name[16];
  55. wait_queue_head_t wait;
  56. struct list_head client_list;
  57. struct input_handle handle;
  58. struct device dev;
  59. struct list_head mixdev_node;
  60. int mixdev_open;
  61. struct mousedev_hw_data packet;
  62. unsigned int pkt_count;
  63. int old_x[4], old_y[4];
  64. int frac_dx, frac_dy;
  65. unsigned long touch;
  66. };
  67. enum mousedev_emul {
  68. MOUSEDEV_EMUL_PS2,
  69. MOUSEDEV_EMUL_IMPS,
  70. MOUSEDEV_EMUL_EXPS
  71. };
  72. struct mousedev_motion {
  73. int dx, dy, dz;
  74. unsigned long buttons;
  75. };
  76. #define PACKET_QUEUE_LEN 16
  77. struct mousedev_client {
  78. struct fasync_struct *fasync;
  79. struct mousedev *mousedev;
  80. struct list_head node;
  81. struct mousedev_motion packets[PACKET_QUEUE_LEN];
  82. unsigned int head, tail;
  83. spinlock_t packet_lock;
  84. int pos_x, pos_y;
  85. signed char ps2[6];
  86. unsigned char ready, buffer, bufsiz;
  87. unsigned char imexseq, impsseq;
  88. enum mousedev_emul mode;
  89. unsigned long last_buttons;
  90. };
  91. #define MOUSEDEV_SEQ_LEN 6
  92. static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
  93. static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
  94. static struct input_handler mousedev_handler;
  95. static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
  96. static struct mousedev *mousedev_mix;
  97. static LIST_HEAD(mousedev_mix_list);
  98. #define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
  99. #define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
  100. static void mousedev_touchpad_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
  101. {
  102. int size, tmp;
  103. enum { FRACTION_DENOM = 128 };
  104. switch (code) {
  105. case ABS_X:
  106. fx(0) = value;
  107. if (mousedev->touch && mousedev->pkt_count >= 2) {
  108. size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
  109. if (size == 0)
  110. size = 256 * 2;
  111. tmp = ((value - fx(2)) * (256 * FRACTION_DENOM)) / size;
  112. tmp += mousedev->frac_dx;
  113. mousedev->packet.dx = tmp / FRACTION_DENOM;
  114. mousedev->frac_dx = tmp - mousedev->packet.dx * FRACTION_DENOM;
  115. }
  116. break;
  117. case ABS_Y:
  118. fy(0) = value;
  119. if (mousedev->touch && mousedev->pkt_count >= 2) {
  120. /* use X size to keep the same scale */
  121. size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
  122. if (size == 0)
  123. size = 256 * 2;
  124. tmp = -((value - fy(2)) * (256 * FRACTION_DENOM)) / size;
  125. tmp += mousedev->frac_dy;
  126. mousedev->packet.dy = tmp / FRACTION_DENOM;
  127. mousedev->frac_dy = tmp - mousedev->packet.dy * FRACTION_DENOM;
  128. }
  129. break;
  130. }
  131. }
  132. static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
  133. {
  134. int size;
  135. switch (code) {
  136. case ABS_X:
  137. size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
  138. if (size == 0)
  139. size = xres ? : 1;
  140. if (value > dev->absmax[ABS_X])
  141. value = dev->absmax[ABS_X];
  142. if (value < dev->absmin[ABS_X])
  143. value = dev->absmin[ABS_X];
  144. mousedev->packet.x = ((value - dev->absmin[ABS_X]) * xres) / size;
  145. mousedev->packet.abs_event = 1;
  146. break;
  147. case ABS_Y:
  148. size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
  149. if (size == 0)
  150. size = yres ? : 1;
  151. if (value > dev->absmax[ABS_Y])
  152. value = dev->absmax[ABS_Y];
  153. if (value < dev->absmin[ABS_Y])
  154. value = dev->absmin[ABS_Y];
  155. mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size;
  156. mousedev->packet.abs_event = 1;
  157. break;
  158. }
  159. }
  160. static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value)
  161. {
  162. switch (code) {
  163. case REL_X: mousedev->packet.dx += value; break;
  164. case REL_Y: mousedev->packet.dy -= value; break;
  165. case REL_WHEEL: mousedev->packet.dz -= value; break;
  166. }
  167. }
  168. static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value)
  169. {
  170. int index;
  171. switch (code) {
  172. case BTN_TOUCH:
  173. case BTN_0:
  174. case BTN_LEFT: index = 0; break;
  175. case BTN_STYLUS:
  176. case BTN_1:
  177. case BTN_RIGHT: index = 1; break;
  178. case BTN_2:
  179. case BTN_FORWARD:
  180. case BTN_STYLUS2:
  181. case BTN_MIDDLE: index = 2; break;
  182. case BTN_3:
  183. case BTN_BACK:
  184. case BTN_SIDE: index = 3; break;
  185. case BTN_4:
  186. case BTN_EXTRA: index = 4; break;
  187. default: return;
  188. }
  189. if (value) {
  190. set_bit(index, &mousedev->packet.buttons);
  191. set_bit(index, &mousedev_mix->packet.buttons);
  192. } else {
  193. clear_bit(index, &mousedev->packet.buttons);
  194. clear_bit(index, &mousedev_mix->packet.buttons);
  195. }
  196. }
  197. static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet)
  198. {
  199. struct mousedev_client *client;
  200. struct mousedev_motion *p;
  201. unsigned long flags;
  202. int wake_readers = 0;
  203. list_for_each_entry(client, &mousedev->client_list, node) {
  204. spin_lock_irqsave(&client->packet_lock, flags);
  205. p = &client->packets[client->head];
  206. if (client->ready && p->buttons != mousedev->packet.buttons) {
  207. unsigned int new_head = (client->head + 1) % PACKET_QUEUE_LEN;
  208. if (new_head != client->tail) {
  209. p = &client->packets[client->head = new_head];
  210. memset(p, 0, sizeof(struct mousedev_motion));
  211. }
  212. }
  213. if (packet->abs_event) {
  214. p->dx += packet->x - client->pos_x;
  215. p->dy += packet->y - client->pos_y;
  216. client->pos_x = packet->x;
  217. client->pos_y = packet->y;
  218. }
  219. client->pos_x += packet->dx;
  220. client->pos_x = client->pos_x < 0 ? 0 : (client->pos_x >= xres ? xres : client->pos_x);
  221. client->pos_y += packet->dy;
  222. client->pos_y = client->pos_y < 0 ? 0 : (client->pos_y >= yres ? yres : client->pos_y);
  223. p->dx += packet->dx;
  224. p->dy += packet->dy;
  225. p->dz += packet->dz;
  226. p->buttons = mousedev->packet.buttons;
  227. if (p->dx || p->dy || p->dz || p->buttons != client->last_buttons)
  228. client->ready = 1;
  229. spin_unlock_irqrestore(&client->packet_lock, flags);
  230. if (client->ready) {
  231. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  232. wake_readers = 1;
  233. }
  234. }
  235. if (wake_readers)
  236. wake_up_interruptible(&mousedev->wait);
  237. }
  238. static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
  239. {
  240. if (!value) {
  241. if (mousedev->touch &&
  242. time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) {
  243. /*
  244. * Toggle left button to emulate tap.
  245. * We rely on the fact that mousedev_mix always has 0
  246. * motion packet so we won't mess current position.
  247. */
  248. set_bit(0, &mousedev->packet.buttons);
  249. set_bit(0, &mousedev_mix->packet.buttons);
  250. mousedev_notify_readers(mousedev, &mousedev_mix->packet);
  251. mousedev_notify_readers(mousedev_mix, &mousedev_mix->packet);
  252. clear_bit(0, &mousedev->packet.buttons);
  253. clear_bit(0, &mousedev_mix->packet.buttons);
  254. }
  255. mousedev->touch = mousedev->pkt_count = 0;
  256. mousedev->frac_dx = 0;
  257. mousedev->frac_dy = 0;
  258. } else if (!mousedev->touch)
  259. mousedev->touch = jiffies;
  260. }
  261. static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
  262. {
  263. struct mousedev *mousedev = handle->private;
  264. switch (type) {
  265. case EV_ABS:
  266. /* Ignore joysticks */
  267. if (test_bit(BTN_TRIGGER, handle->dev->keybit))
  268. return;
  269. if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
  270. mousedev_touchpad_event(handle->dev, mousedev, code, value);
  271. else
  272. mousedev_abs_event(handle->dev, mousedev, code, value);
  273. break;
  274. case EV_REL:
  275. mousedev_rel_event(mousedev, code, value);
  276. break;
  277. case EV_KEY:
  278. if (value != 2) {
  279. if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
  280. mousedev_touchpad_touch(mousedev, value);
  281. else
  282. mousedev_key_event(mousedev, code, value);
  283. }
  284. break;
  285. case EV_SYN:
  286. if (code == SYN_REPORT) {
  287. if (mousedev->touch) {
  288. mousedev->pkt_count++;
  289. /* Input system eats duplicate events, but we need all of them
  290. * to do correct averaging so apply present one forward
  291. */
  292. fx(0) = fx(1);
  293. fy(0) = fy(1);
  294. }
  295. mousedev_notify_readers(mousedev, &mousedev->packet);
  296. mousedev_notify_readers(mousedev_mix, &mousedev->packet);
  297. mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0;
  298. mousedev->packet.abs_event = 0;
  299. }
  300. break;
  301. }
  302. }
  303. static int mousedev_fasync(int fd, struct file *file, int on)
  304. {
  305. int retval;
  306. struct mousedev_client *client = file->private_data;
  307. retval = fasync_helper(fd, file, on, &client->fasync);
  308. return retval < 0 ? retval : 0;
  309. }
  310. static void mousedev_free(struct device *dev)
  311. {
  312. struct mousedev *mousedev = container_of(dev, struct mousedev, dev);
  313. mousedev_table[mousedev->minor] = NULL;
  314. kfree(mousedev);
  315. }
  316. static int mixdev_add_device(struct mousedev *mousedev)
  317. {
  318. int error;
  319. if (mousedev_mix->open) {
  320. error = input_open_device(&mousedev->handle);
  321. if (error)
  322. return error;
  323. mousedev->open++;
  324. mousedev->mixdev_open = 1;
  325. }
  326. get_device(&mousedev->dev);
  327. list_add_tail(&mousedev->mixdev_node, &mousedev_mix_list);
  328. return 0;
  329. }
  330. static void mixdev_remove_device(struct mousedev *mousedev)
  331. {
  332. if (mousedev->mixdev_open) {
  333. mousedev->mixdev_open = 0;
  334. if (!--mousedev->open && mousedev->exist)
  335. input_close_device(&mousedev->handle);
  336. }
  337. list_del_init(&mousedev->mixdev_node);
  338. put_device(&mousedev->dev);
  339. }
  340. static void mixdev_open_devices(void)
  341. {
  342. struct mousedev *mousedev;
  343. if (mousedev_mix->open++)
  344. return;
  345. list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
  346. if (!mousedev->mixdev_open) {
  347. if (!mousedev->open && mousedev->exist)
  348. if (input_open_device(&mousedev->handle))
  349. continue;
  350. mousedev->open++;
  351. mousedev->mixdev_open = 1;
  352. }
  353. }
  354. }
  355. static void mixdev_close_devices(void)
  356. {
  357. struct mousedev *mousedev;
  358. if (--mousedev_mix->open)
  359. return;
  360. list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
  361. if (mousedev->mixdev_open) {
  362. mousedev->mixdev_open = 0;
  363. if (!--mousedev->open && mousedev->exist)
  364. input_close_device(&mousedev->handle);
  365. }
  366. }
  367. }
  368. static int mousedev_release(struct inode *inode, struct file *file)
  369. {
  370. struct mousedev_client *client = file->private_data;
  371. struct mousedev *mousedev = client->mousedev;
  372. mousedev_fasync(-1, file, 0);
  373. list_del(&client->node);
  374. kfree(client);
  375. if (mousedev->minor == MOUSEDEV_MIX)
  376. mixdev_close_devices();
  377. else if (!--mousedev->open && mousedev->exist)
  378. input_close_device(&mousedev->handle);
  379. put_device(&mousedev->dev);
  380. return 0;
  381. }
  382. static int mousedev_open(struct inode *inode, struct file *file)
  383. {
  384. struct mousedev_client *client;
  385. struct mousedev *mousedev;
  386. int error;
  387. int i;
  388. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  389. if (imajor(inode) == MISC_MAJOR)
  390. i = MOUSEDEV_MIX;
  391. else
  392. #endif
  393. i = iminor(inode) - MOUSEDEV_MINOR_BASE;
  394. if (i >= MOUSEDEV_MINORS)
  395. return -ENODEV;
  396. mousedev = mousedev_table[i];
  397. if (!mousedev)
  398. return -ENODEV;
  399. get_device(&mousedev->dev);
  400. client = kzalloc(sizeof(struct mousedev_client), GFP_KERNEL);
  401. if (!client) {
  402. error = -ENOMEM;
  403. goto err_put_mousedev;
  404. }
  405. spin_lock_init(&client->packet_lock);
  406. client->pos_x = xres / 2;
  407. client->pos_y = yres / 2;
  408. client->mousedev = mousedev;
  409. list_add_tail(&client->node, &mousedev->client_list);
  410. if (mousedev->minor == MOUSEDEV_MIX)
  411. mixdev_open_devices();
  412. else if (!mousedev->open++ && mousedev->exist) {
  413. error = input_open_device(&mousedev->handle);
  414. if (error)
  415. goto err_free_client;
  416. }
  417. file->private_data = client;
  418. return 0;
  419. err_free_client:
  420. list_del(&client->node);
  421. kfree(client);
  422. err_put_mousedev:
  423. put_device(&mousedev->dev);
  424. return error;
  425. }
  426. static inline int mousedev_limit_delta(int delta, int limit)
  427. {
  428. return delta > limit ? limit : (delta < -limit ? -limit : delta);
  429. }
  430. static void mousedev_packet(struct mousedev_client *client, signed char *ps2_data)
  431. {
  432. struct mousedev_motion *p;
  433. unsigned long flags;
  434. spin_lock_irqsave(&client->packet_lock, flags);
  435. p = &client->packets[client->tail];
  436. ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
  437. ps2_data[1] = mousedev_limit_delta(p->dx, 127);
  438. ps2_data[2] = mousedev_limit_delta(p->dy, 127);
  439. p->dx -= ps2_data[1];
  440. p->dy -= ps2_data[2];
  441. switch (client->mode) {
  442. case MOUSEDEV_EMUL_EXPS:
  443. ps2_data[3] = mousedev_limit_delta(p->dz, 7);
  444. p->dz -= ps2_data[3];
  445. ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
  446. client->bufsiz = 4;
  447. break;
  448. case MOUSEDEV_EMUL_IMPS:
  449. ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
  450. ps2_data[3] = mousedev_limit_delta(p->dz, 127);
  451. p->dz -= ps2_data[3];
  452. client->bufsiz = 4;
  453. break;
  454. case MOUSEDEV_EMUL_PS2:
  455. default:
  456. ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
  457. p->dz = 0;
  458. client->bufsiz = 3;
  459. break;
  460. }
  461. if (!p->dx && !p->dy && !p->dz) {
  462. if (client->tail == client->head) {
  463. client->ready = 0;
  464. client->last_buttons = p->buttons;
  465. } else
  466. client->tail = (client->tail + 1) % PACKET_QUEUE_LEN;
  467. }
  468. spin_unlock_irqrestore(&client->packet_lock, flags);
  469. }
  470. static ssize_t mousedev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  471. {
  472. struct mousedev_client *client = file->private_data;
  473. unsigned char c;
  474. unsigned int i;
  475. for (i = 0; i < count; i++) {
  476. if (get_user(c, buffer + i))
  477. return -EFAULT;
  478. if (c == mousedev_imex_seq[client->imexseq]) {
  479. if (++client->imexseq == MOUSEDEV_SEQ_LEN) {
  480. client->imexseq = 0;
  481. client->mode = MOUSEDEV_EMUL_EXPS;
  482. }
  483. } else
  484. client->imexseq = 0;
  485. if (c == mousedev_imps_seq[client->impsseq]) {
  486. if (++client->impsseq == MOUSEDEV_SEQ_LEN) {
  487. client->impsseq = 0;
  488. client->mode = MOUSEDEV_EMUL_IMPS;
  489. }
  490. } else
  491. client->impsseq = 0;
  492. client->ps2[0] = 0xfa;
  493. switch (c) {
  494. case 0xeb: /* Poll */
  495. mousedev_packet(client, &client->ps2[1]);
  496. client->bufsiz++; /* account for leading ACK */
  497. break;
  498. case 0xf2: /* Get ID */
  499. switch (client->mode) {
  500. case MOUSEDEV_EMUL_PS2: client->ps2[1] = 0; break;
  501. case MOUSEDEV_EMUL_IMPS: client->ps2[1] = 3; break;
  502. case MOUSEDEV_EMUL_EXPS: client->ps2[1] = 4; break;
  503. }
  504. client->bufsiz = 2;
  505. break;
  506. case 0xe9: /* Get info */
  507. client->ps2[1] = 0x60; client->ps2[2] = 3; client->ps2[3] = 200;
  508. client->bufsiz = 4;
  509. break;
  510. case 0xff: /* Reset */
  511. client->impsseq = client->imexseq = 0;
  512. client->mode = MOUSEDEV_EMUL_PS2;
  513. client->ps2[1] = 0xaa; client->ps2[2] = 0x00;
  514. client->bufsiz = 3;
  515. break;
  516. default:
  517. client->bufsiz = 1;
  518. break;
  519. }
  520. client->buffer = client->bufsiz;
  521. }
  522. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  523. wake_up_interruptible(&client->mousedev->wait);
  524. return count;
  525. }
  526. static ssize_t mousedev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  527. {
  528. struct mousedev_client *client = file->private_data;
  529. int retval = 0;
  530. if (!client->ready && !client->buffer && (file->f_flags & O_NONBLOCK))
  531. return -EAGAIN;
  532. retval = wait_event_interruptible(client->mousedev->wait,
  533. !client->mousedev->exist || client->ready || client->buffer);
  534. if (retval)
  535. return retval;
  536. if (!client->mousedev->exist)
  537. return -ENODEV;
  538. if (!client->buffer && client->ready) {
  539. mousedev_packet(client, client->ps2);
  540. client->buffer = client->bufsiz;
  541. }
  542. if (count > client->buffer)
  543. count = client->buffer;
  544. client->buffer -= count;
  545. if (copy_to_user(buffer, client->ps2 + client->bufsiz - client->buffer - count, count))
  546. return -EFAULT;
  547. return count;
  548. }
  549. /* No kernel lock - fine */
  550. static unsigned int mousedev_poll(struct file *file, poll_table *wait)
  551. {
  552. struct mousedev_client *client = file->private_data;
  553. struct mousedev *mousedev = client->mousedev;
  554. poll_wait(file, &mousedev->wait, wait);
  555. return ((client->ready || client->buffer) ? (POLLIN | POLLRDNORM) : 0) |
  556. (mousedev->exist ? 0 : (POLLHUP | POLLERR));
  557. }
  558. static const struct file_operations mousedev_fops = {
  559. .owner = THIS_MODULE,
  560. .read = mousedev_read,
  561. .write = mousedev_write,
  562. .poll = mousedev_poll,
  563. .open = mousedev_open,
  564. .release = mousedev_release,
  565. .fasync = mousedev_fasync,
  566. };
  567. static struct mousedev *mousedev_create(struct input_dev *dev,
  568. struct input_handler *handler,
  569. int minor)
  570. {
  571. struct mousedev *mousedev;
  572. int error;
  573. mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL);
  574. if (!mousedev) {
  575. error = -ENOMEM;
  576. goto err_out;
  577. }
  578. INIT_LIST_HEAD(&mousedev->client_list);
  579. INIT_LIST_HEAD(&mousedev->mixdev_node);
  580. init_waitqueue_head(&mousedev->wait);
  581. if (minor == MOUSEDEV_MIX)
  582. strlcpy(mousedev->name, "mice", sizeof(mousedev->name));
  583. else
  584. snprintf(mousedev->name, sizeof(mousedev->name),
  585. "mouse%d", minor);
  586. mousedev->minor = minor;
  587. mousedev->exist = 1;
  588. mousedev->handle.dev = dev;
  589. mousedev->handle.name = mousedev->name;
  590. mousedev->handle.handler = handler;
  591. mousedev->handle.private = mousedev;
  592. strlcpy(mousedev->dev.bus_id, mousedev->name,
  593. sizeof(mousedev->dev.bus_id));
  594. mousedev->dev.class = &input_class;
  595. if (dev)
  596. mousedev->dev.parent = &dev->dev;
  597. mousedev->dev.devt = MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor);
  598. mousedev->dev.release = mousedev_free;
  599. device_initialize(&mousedev->dev);
  600. mousedev_table[minor] = mousedev;
  601. error = device_add(&mousedev->dev);
  602. if (error)
  603. goto err_free_mousedev;
  604. return mousedev;
  605. err_free_mousedev:
  606. put_device(&mousedev->dev);
  607. err_out:
  608. return ERR_PTR(error);
  609. }
  610. static void mousedev_destroy(struct mousedev *mousedev)
  611. {
  612. struct mousedev_client *client;
  613. device_del(&mousedev->dev);
  614. mousedev->exist = 0;
  615. if (mousedev->open) {
  616. input_close_device(&mousedev->handle);
  617. list_for_each_entry(client, &mousedev->client_list, node)
  618. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  619. wake_up_interruptible(&mousedev->wait);
  620. }
  621. put_device(&mousedev->dev);
  622. }
  623. static int mousedev_connect(struct input_handler *handler, struct input_dev *dev,
  624. const struct input_device_id *id)
  625. {
  626. struct mousedev *mousedev;
  627. int minor;
  628. int error;
  629. for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
  630. if (minor == MOUSEDEV_MINORS) {
  631. printk(KERN_ERR "mousedev: no more free mousedev devices\n");
  632. return -ENFILE;
  633. }
  634. mousedev = mousedev_create(dev, handler, minor);
  635. if (IS_ERR(mousedev))
  636. return PTR_ERR(mousedev);
  637. error = input_register_handle(&mousedev->handle);
  638. if (error)
  639. goto err_delete_mousedev;
  640. error = mixdev_add_device(mousedev);
  641. if (error)
  642. goto err_unregister_handle;
  643. return 0;
  644. err_unregister_handle:
  645. input_unregister_handle(&mousedev->handle);
  646. err_delete_mousedev:
  647. device_unregister(&mousedev->dev);
  648. return error;
  649. }
  650. static void mousedev_disconnect(struct input_handle *handle)
  651. {
  652. struct mousedev *mousedev = handle->private;
  653. mixdev_remove_device(mousedev);
  654. input_unregister_handle(handle);
  655. mousedev_destroy(mousedev);
  656. }
  657. static const struct input_device_id mousedev_ids[] = {
  658. {
  659. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
  660. .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
  661. .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
  662. .relbit = { BIT(REL_X) | BIT(REL_Y) },
  663. }, /* A mouse like device, at least one button, two relative axes */
  664. {
  665. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
  666. .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
  667. .relbit = { BIT(REL_WHEEL) },
  668. }, /* A separate scrollwheel */
  669. {
  670. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
  671. .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
  672. .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
  673. .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
  674. }, /* A tablet like device, at least touch detection, two absolute axes */
  675. {
  676. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
  677. .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
  678. .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
  679. .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) },
  680. }, /* A touchpad */
  681. { }, /* Terminating entry */
  682. };
  683. MODULE_DEVICE_TABLE(input, mousedev_ids);
  684. static struct input_handler mousedev_handler = {
  685. .event = mousedev_event,
  686. .connect = mousedev_connect,
  687. .disconnect = mousedev_disconnect,
  688. .fops = &mousedev_fops,
  689. .minor = MOUSEDEV_MINOR_BASE,
  690. .name = "mousedev",
  691. .id_table = mousedev_ids,
  692. };
  693. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  694. static struct miscdevice psaux_mouse = {
  695. PSMOUSE_MINOR, "psaux", &mousedev_fops
  696. };
  697. static int psaux_registered;
  698. #endif
  699. static int __init mousedev_init(void)
  700. {
  701. int error;
  702. mousedev_mix = mousedev_create(NULL, &mousedev_handler, MOUSEDEV_MIX);
  703. if (IS_ERR(mousedev_mix))
  704. return PTR_ERR(mousedev_mix);
  705. error = input_register_handler(&mousedev_handler);
  706. if (error) {
  707. mousedev_destroy(mousedev_mix);
  708. return error;
  709. }
  710. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  711. error = misc_register(&psaux_mouse);
  712. if (error)
  713. printk(KERN_WARNING "mice: could not register psaux device, "
  714. "error: %d\n", error);
  715. else
  716. psaux_registered = 1;
  717. #endif
  718. printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
  719. return 0;
  720. }
  721. static void __exit mousedev_exit(void)
  722. {
  723. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  724. if (psaux_registered)
  725. misc_deregister(&psaux_mouse);
  726. #endif
  727. input_unregister_handler(&mousedev_handler);
  728. mousedev_destroy(mousedev_mix);
  729. }
  730. module_init(mousedev_init);
  731. module_exit(mousedev_exit);