mousedev.c 20 KB

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