mousedev.c 22 KB

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