mousedev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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/smp_lock.h>
  21. #include <linux/random.h>
  22. #include <linux/major.h>
  23. #include <linux/device.h>
  24. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  25. #include <linux/miscdevice.h>
  26. #endif
  27. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  28. MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
  29. MODULE_LICENSE("GPL");
  30. #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
  31. #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
  32. #endif
  33. #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
  34. #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
  35. #endif
  36. static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
  37. module_param(xres, uint, 0644);
  38. MODULE_PARM_DESC(xres, "Horizontal screen resolution");
  39. static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
  40. module_param(yres, uint, 0644);
  41. MODULE_PARM_DESC(yres, "Vertical screen resolution");
  42. static unsigned tap_time = 200;
  43. module_param(tap_time, uint, 0644);
  44. MODULE_PARM_DESC(tap_time, "Tap time for touchpads in absolute mode (msecs)");
  45. struct mousedev_hw_data {
  46. int dx, dy, dz;
  47. int x, y;
  48. int abs_event;
  49. unsigned long buttons;
  50. };
  51. struct mousedev {
  52. int exist;
  53. int open;
  54. int minor;
  55. char name[16];
  56. wait_queue_head_t wait;
  57. struct list_head client_list;
  58. struct input_handle handle;
  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 mousedev *mousedev)
  311. {
  312. mousedev_table[mousedev->minor] = NULL;
  313. kfree(mousedev);
  314. }
  315. static int mixdev_add_device(struct mousedev *mousedev)
  316. {
  317. int error;
  318. if (mousedev_mix.open) {
  319. error = input_open_device(&mousedev->handle);
  320. if (error)
  321. return error;
  322. mousedev->open++;
  323. mousedev->mixdev_open++;
  324. }
  325. list_add_tail(&mousedev->mixdev_node, &mousedev_mix_list);
  326. return 0;
  327. }
  328. static void mixdev_remove_device(struct mousedev *mousedev)
  329. {
  330. if (mousedev->mixdev_open) {
  331. mousedev->mixdev_open = 0;
  332. if (!--mousedev->open && mousedev->exist)
  333. input_close_device(&mousedev->handle);
  334. }
  335. list_del_init(&mousedev->mixdev_node);
  336. }
  337. static void mixdev_open_devices(void)
  338. {
  339. struct mousedev *mousedev;
  340. list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
  341. if (mousedev->exist && !mousedev->open) {
  342. if (input_open_device(&mousedev->handle))
  343. continue;
  344. mousedev->open++;
  345. mousedev->mixdev_open++;
  346. }
  347. }
  348. }
  349. static void mixdev_close_devices(void)
  350. {
  351. struct mousedev *mousedev, *next;
  352. list_for_each_entry_safe(mousedev, next, &mousedev_mix_list, mixdev_node) {
  353. if (mousedev->mixdev_open) {
  354. mousedev->mixdev_open = 0;
  355. if (!--mousedev->open) {
  356. if (mousedev->exist)
  357. input_close_device(&mousedev->handle);
  358. else
  359. mousedev_free(mousedev);
  360. }
  361. }
  362. }
  363. }
  364. static int mousedev_release(struct inode *inode, struct file *file)
  365. {
  366. struct mousedev_client *client = file->private_data;
  367. struct mousedev *mousedev = client->mousedev;
  368. mousedev_fasync(-1, file, 0);
  369. list_del(&client->node);
  370. kfree(client);
  371. if (!--mousedev->open) {
  372. if (mousedev->minor == MOUSEDEV_MIX)
  373. mixdev_close_devices();
  374. else if (mousedev->exist)
  375. input_close_device(&mousedev->handle);
  376. else
  377. mousedev_free(mousedev);
  378. }
  379. return 0;
  380. }
  381. static int mousedev_open(struct inode *inode, struct file *file)
  382. {
  383. struct mousedev_client *client;
  384. struct mousedev *mousedev;
  385. int error;
  386. int i;
  387. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  388. if (imajor(inode) == MISC_MAJOR)
  389. i = MOUSEDEV_MIX;
  390. else
  391. #endif
  392. i = iminor(inode) - MOUSEDEV_MINOR_BASE;
  393. if (i >= MOUSEDEV_MINORS)
  394. return -ENODEV;
  395. mousedev = mousedev_table[i];
  396. if (!mousedev)
  397. return -ENODEV;
  398. client = kzalloc(sizeof(struct mousedev_client), GFP_KERNEL);
  399. if (!client)
  400. return -ENOMEM;
  401. spin_lock_init(&client->packet_lock);
  402. client->pos_x = xres / 2;
  403. client->pos_y = yres / 2;
  404. client->mousedev = mousedev;
  405. list_add_tail(&client->node, &mousedev->client_list);
  406. if (!mousedev->open++) {
  407. if (mousedev->minor == MOUSEDEV_MIX)
  408. mixdev_open_devices();
  409. else if (mousedev->exist) {
  410. error = input_open_device(&mousedev->handle);
  411. if (error) {
  412. list_del(&client->node);
  413. kfree(client);
  414. return error;
  415. }
  416. }
  417. }
  418. file->private_data = client;
  419. return 0;
  420. }
  421. static inline int mousedev_limit_delta(int delta, int limit)
  422. {
  423. return delta > limit ? limit : (delta < -limit ? -limit : delta);
  424. }
  425. static void mousedev_packet(struct mousedev_client *client, signed char *ps2_data)
  426. {
  427. struct mousedev_motion *p;
  428. unsigned long flags;
  429. spin_lock_irqsave(&client->packet_lock, flags);
  430. p = &client->packets[client->tail];
  431. ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
  432. ps2_data[1] = mousedev_limit_delta(p->dx, 127);
  433. ps2_data[2] = mousedev_limit_delta(p->dy, 127);
  434. p->dx -= ps2_data[1];
  435. p->dy -= ps2_data[2];
  436. switch (client->mode) {
  437. case MOUSEDEV_EMUL_EXPS:
  438. ps2_data[3] = mousedev_limit_delta(p->dz, 7);
  439. p->dz -= ps2_data[3];
  440. ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
  441. client->bufsiz = 4;
  442. break;
  443. case MOUSEDEV_EMUL_IMPS:
  444. ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
  445. ps2_data[3] = mousedev_limit_delta(p->dz, 127);
  446. p->dz -= ps2_data[3];
  447. client->bufsiz = 4;
  448. break;
  449. case MOUSEDEV_EMUL_PS2:
  450. default:
  451. ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
  452. p->dz = 0;
  453. client->bufsiz = 3;
  454. break;
  455. }
  456. if (!p->dx && !p->dy && !p->dz) {
  457. if (client->tail == client->head) {
  458. client->ready = 0;
  459. client->last_buttons = p->buttons;
  460. } else
  461. client->tail = (client->tail + 1) % PACKET_QUEUE_LEN;
  462. }
  463. spin_unlock_irqrestore(&client->packet_lock, flags);
  464. }
  465. static ssize_t mousedev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  466. {
  467. struct mousedev_client *client = file->private_data;
  468. unsigned char c;
  469. unsigned int i;
  470. for (i = 0; i < count; i++) {
  471. if (get_user(c, buffer + i))
  472. return -EFAULT;
  473. if (c == mousedev_imex_seq[client->imexseq]) {
  474. if (++client->imexseq == MOUSEDEV_SEQ_LEN) {
  475. client->imexseq = 0;
  476. client->mode = MOUSEDEV_EMUL_EXPS;
  477. }
  478. } else
  479. client->imexseq = 0;
  480. if (c == mousedev_imps_seq[client->impsseq]) {
  481. if (++client->impsseq == MOUSEDEV_SEQ_LEN) {
  482. client->impsseq = 0;
  483. client->mode = MOUSEDEV_EMUL_IMPS;
  484. }
  485. } else
  486. client->impsseq = 0;
  487. client->ps2[0] = 0xfa;
  488. switch (c) {
  489. case 0xeb: /* Poll */
  490. mousedev_packet(client, &client->ps2[1]);
  491. client->bufsiz++; /* account for leading ACK */
  492. break;
  493. case 0xf2: /* Get ID */
  494. switch (client->mode) {
  495. case MOUSEDEV_EMUL_PS2: client->ps2[1] = 0; break;
  496. case MOUSEDEV_EMUL_IMPS: client->ps2[1] = 3; break;
  497. case MOUSEDEV_EMUL_EXPS: client->ps2[1] = 4; break;
  498. }
  499. client->bufsiz = 2;
  500. break;
  501. case 0xe9: /* Get info */
  502. client->ps2[1] = 0x60; client->ps2[2] = 3; client->ps2[3] = 200;
  503. client->bufsiz = 4;
  504. break;
  505. case 0xff: /* Reset */
  506. client->impsseq = client->imexseq = 0;
  507. client->mode = MOUSEDEV_EMUL_PS2;
  508. client->ps2[1] = 0xaa; client->ps2[2] = 0x00;
  509. client->bufsiz = 3;
  510. break;
  511. default:
  512. client->bufsiz = 1;
  513. break;
  514. }
  515. client->buffer = client->bufsiz;
  516. }
  517. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  518. wake_up_interruptible(&client->mousedev->wait);
  519. return count;
  520. }
  521. static ssize_t mousedev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  522. {
  523. struct mousedev_client *client = file->private_data;
  524. int retval = 0;
  525. if (!client->ready && !client->buffer && (file->f_flags & O_NONBLOCK))
  526. return -EAGAIN;
  527. retval = wait_event_interruptible(client->mousedev->wait,
  528. !client->mousedev->exist || client->ready || client->buffer);
  529. if (retval)
  530. return retval;
  531. if (!client->mousedev->exist)
  532. return -ENODEV;
  533. if (!client->buffer && client->ready) {
  534. mousedev_packet(client, client->ps2);
  535. client->buffer = client->bufsiz;
  536. }
  537. if (count > client->buffer)
  538. count = client->buffer;
  539. client->buffer -= count;
  540. if (copy_to_user(buffer, client->ps2 + client->bufsiz - client->buffer - count, count))
  541. return -EFAULT;
  542. return count;
  543. }
  544. /* No kernel lock - fine */
  545. static unsigned int mousedev_poll(struct file *file, poll_table *wait)
  546. {
  547. struct mousedev_client *client = file->private_data;
  548. struct mousedev *mousedev = client->mousedev;
  549. poll_wait(file, &mousedev->wait, wait);
  550. return ((client->ready || client->buffer) ? (POLLIN | POLLRDNORM) : 0) |
  551. (mousedev->exist ? 0 : (POLLHUP | POLLERR));
  552. }
  553. static const struct file_operations mousedev_fops = {
  554. .owner = THIS_MODULE,
  555. .read = mousedev_read,
  556. .write = mousedev_write,
  557. .poll = mousedev_poll,
  558. .open = mousedev_open,
  559. .release = mousedev_release,
  560. .fasync = mousedev_fasync,
  561. };
  562. static int mousedev_connect(struct input_handler *handler, struct input_dev *dev,
  563. const struct input_device_id *id)
  564. {
  565. struct mousedev *mousedev;
  566. struct class_device *cdev;
  567. dev_t devt;
  568. int minor;
  569. int error;
  570. for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
  571. if (minor == MOUSEDEV_MINORS) {
  572. printk(KERN_ERR "mousedev: no more free mousedev devices\n");
  573. return -ENFILE;
  574. }
  575. mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL);
  576. if (!mousedev)
  577. return -ENOMEM;
  578. INIT_LIST_HEAD(&mousedev->client_list);
  579. INIT_LIST_HEAD(&mousedev->mixdev_node);
  580. init_waitqueue_head(&mousedev->wait);
  581. mousedev->minor = minor;
  582. mousedev->exist = 1;
  583. mousedev->handle.dev = dev;
  584. mousedev->handle.name = mousedev->name;
  585. mousedev->handle.handler = handler;
  586. mousedev->handle.private = mousedev;
  587. sprintf(mousedev->name, "mouse%d", minor);
  588. mousedev_table[minor] = mousedev;
  589. devt = MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor),
  590. cdev = class_device_create(&input_class, &dev->cdev, devt,
  591. dev->cdev.dev, mousedev->name);
  592. if (IS_ERR(cdev)) {
  593. error = PTR_ERR(cdev);
  594. goto err_free_mousedev;
  595. }
  596. /* temporary symlink to keep userspace happy */
  597. error = sysfs_create_link(&input_class.subsys.kobj,
  598. &cdev->kobj, mousedev->name);
  599. if (error)
  600. goto err_cdev_destroy;
  601. error = input_register_handle(&mousedev->handle);
  602. if (error)
  603. goto err_remove_link;
  604. error = mixdev_add_device(mousedev);
  605. if (error)
  606. goto err_unregister_handle;
  607. return 0;
  608. err_unregister_handle:
  609. input_unregister_handle(&mousedev->handle);
  610. err_remove_link:
  611. sysfs_remove_link(&input_class.subsys.kobj, mousedev->name);
  612. err_cdev_destroy:
  613. class_device_destroy(&input_class, devt);
  614. err_free_mousedev:
  615. mousedev_table[minor] = NULL;
  616. kfree(mousedev);
  617. return error;
  618. }
  619. static void mousedev_disconnect(struct input_handle *handle)
  620. {
  621. struct mousedev *mousedev = handle->private;
  622. struct mousedev_client *client;
  623. input_unregister_handle(handle);
  624. sysfs_remove_link(&input_class.subsys.kobj, mousedev->name);
  625. class_device_destroy(&input_class,
  626. MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + mousedev->minor));
  627. mousedev->exist = 0;
  628. mixdev_remove_device(mousedev);
  629. if (mousedev->open) {
  630. input_close_device(handle);
  631. list_for_each_entry(client, &mousedev->client_list, node)
  632. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  633. wake_up_interruptible(&mousedev->wait);
  634. } else
  635. mousedev_free(mousedev);
  636. }
  637. static const struct input_device_id mousedev_ids[] = {
  638. {
  639. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
  640. .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
  641. .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
  642. .relbit = { BIT(REL_X) | BIT(REL_Y) },
  643. }, /* A mouse like device, at least one button, two relative axes */
  644. {
  645. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
  646. .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
  647. .relbit = { BIT(REL_WHEEL) },
  648. }, /* A separate scrollwheel */
  649. {
  650. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
  651. .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
  652. .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
  653. .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
  654. }, /* A tablet like device, at least touch detection, two absolute axes */
  655. {
  656. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
  657. .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
  658. .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
  659. .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) },
  660. }, /* A touchpad */
  661. { }, /* Terminating entry */
  662. };
  663. MODULE_DEVICE_TABLE(input, mousedev_ids);
  664. static struct input_handler mousedev_handler = {
  665. .event = mousedev_event,
  666. .connect = mousedev_connect,
  667. .disconnect = mousedev_disconnect,
  668. .fops = &mousedev_fops,
  669. .minor = MOUSEDEV_MINOR_BASE,
  670. .name = "mousedev",
  671. .id_table = mousedev_ids,
  672. };
  673. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  674. static struct miscdevice psaux_mouse = {
  675. PSMOUSE_MINOR, "psaux", &mousedev_fops
  676. };
  677. static int psaux_registered;
  678. #endif
  679. static int __init mousedev_init(void)
  680. {
  681. struct class_device *cdev;
  682. int error;
  683. error = input_register_handler(&mousedev_handler);
  684. if (error)
  685. return error;
  686. memset(&mousedev_mix, 0, sizeof(struct mousedev));
  687. INIT_LIST_HEAD(&mousedev_mix.client_list);
  688. init_waitqueue_head(&mousedev_mix.wait);
  689. mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
  690. mousedev_mix.exist = 1;
  691. mousedev_mix.minor = MOUSEDEV_MIX;
  692. cdev = class_device_create(&input_class, NULL,
  693. MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX), NULL, "mice");
  694. if (IS_ERR(cdev)) {
  695. input_unregister_handler(&mousedev_handler);
  696. return PTR_ERR(cdev);
  697. }
  698. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  699. error = misc_register(&psaux_mouse);
  700. if (error)
  701. printk(KERN_WARNING "mice: could not register psaux device, "
  702. "error: %d\n", error);
  703. else
  704. psaux_registered = 1;
  705. #endif
  706. printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
  707. return 0;
  708. }
  709. static void __exit mousedev_exit(void)
  710. {
  711. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  712. if (psaux_registered)
  713. misc_deregister(&psaux_mouse);
  714. #endif
  715. class_device_destroy(&input_class,
  716. MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX));
  717. input_unregister_handler(&mousedev_handler);
  718. }
  719. module_init(mousedev_init);
  720. module_exit(mousedev_exit);