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