mousedev.c 20 KB

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