mousedev.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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/smp_lock.h>
  16. #include <linux/poll.h>
  17. #include <linux/module.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. struct input_handle handle;
  56. wait_queue_head_t wait;
  57. struct list_head client_list;
  58. spinlock_t client_lock; /* protects client_list */
  59. struct mutex mutex;
  60. struct device dev;
  61. struct list_head mixdev_node;
  62. int mixdev_open;
  63. struct mousedev_hw_data packet;
  64. unsigned int pkt_count;
  65. int old_x[4], old_y[4];
  66. int frac_dx, frac_dy;
  67. unsigned long touch;
  68. };
  69. enum mousedev_emul {
  70. MOUSEDEV_EMUL_PS2,
  71. MOUSEDEV_EMUL_IMPS,
  72. MOUSEDEV_EMUL_EXPS
  73. };
  74. struct mousedev_motion {
  75. int dx, dy, dz;
  76. unsigned long buttons;
  77. };
  78. #define PACKET_QUEUE_LEN 16
  79. struct mousedev_client {
  80. struct fasync_struct *fasync;
  81. struct mousedev *mousedev;
  82. struct list_head node;
  83. struct mousedev_motion packets[PACKET_QUEUE_LEN];
  84. unsigned int head, tail;
  85. spinlock_t packet_lock;
  86. int pos_x, pos_y;
  87. signed char ps2[6];
  88. unsigned char ready, buffer, bufsiz;
  89. unsigned char imexseq, impsseq;
  90. enum mousedev_emul mode;
  91. unsigned long last_buttons;
  92. };
  93. #define MOUSEDEV_SEQ_LEN 6
  94. static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
  95. static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
  96. static struct input_handler mousedev_handler;
  97. static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
  98. static DEFINE_MUTEX(mousedev_table_mutex);
  99. static struct mousedev *mousedev_mix;
  100. static LIST_HEAD(mousedev_mix_list);
  101. static void mixdev_open_devices(void);
  102. static void mixdev_close_devices(void);
  103. #define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
  104. #define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
  105. static void mousedev_touchpad_event(struct input_dev *dev,
  106. struct mousedev *mousedev,
  107. unsigned int code, int value)
  108. {
  109. int size, tmp;
  110. enum { FRACTION_DENOM = 128 };
  111. switch (code) {
  112. case ABS_X:
  113. fx(0) = value;
  114. if (mousedev->touch && mousedev->pkt_count >= 2) {
  115. size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
  116. if (size == 0)
  117. size = 256 * 2;
  118. tmp = ((value - fx(2)) * 256 * FRACTION_DENOM) / size;
  119. tmp += mousedev->frac_dx;
  120. mousedev->packet.dx = tmp / FRACTION_DENOM;
  121. mousedev->frac_dx =
  122. tmp - mousedev->packet.dx * FRACTION_DENOM;
  123. }
  124. break;
  125. case ABS_Y:
  126. fy(0) = value;
  127. if (mousedev->touch && mousedev->pkt_count >= 2) {
  128. /* use X size to keep the same scale */
  129. size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
  130. if (size == 0)
  131. size = 256 * 2;
  132. tmp = -((value - fy(2)) * 256 * FRACTION_DENOM) / size;
  133. tmp += mousedev->frac_dy;
  134. mousedev->packet.dy = tmp / FRACTION_DENOM;
  135. mousedev->frac_dy = tmp -
  136. mousedev->packet.dy * FRACTION_DENOM;
  137. }
  138. break;
  139. }
  140. }
  141. static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev,
  142. unsigned int code, int value)
  143. {
  144. int size;
  145. switch (code) {
  146. case ABS_X:
  147. size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
  148. if (size == 0)
  149. size = xres ? : 1;
  150. if (value > dev->absmax[ABS_X])
  151. value = dev->absmax[ABS_X];
  152. if (value < dev->absmin[ABS_X])
  153. value = dev->absmin[ABS_X];
  154. mousedev->packet.x =
  155. ((value - dev->absmin[ABS_X]) * xres) / size;
  156. mousedev->packet.abs_event = 1;
  157. break;
  158. case ABS_Y:
  159. size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
  160. if (size == 0)
  161. size = yres ? : 1;
  162. if (value > dev->absmax[ABS_Y])
  163. value = dev->absmax[ABS_Y];
  164. if (value < dev->absmin[ABS_Y])
  165. value = dev->absmin[ABS_Y];
  166. mousedev->packet.y = yres -
  167. ((value - dev->absmin[ABS_Y]) * yres) / size;
  168. mousedev->packet.abs_event = 1;
  169. break;
  170. }
  171. }
  172. static void mousedev_rel_event(struct mousedev *mousedev,
  173. unsigned int code, int value)
  174. {
  175. switch (code) {
  176. case REL_X:
  177. mousedev->packet.dx += value;
  178. break;
  179. case REL_Y:
  180. mousedev->packet.dy -= value;
  181. break;
  182. case REL_WHEEL:
  183. mousedev->packet.dz -= value;
  184. break;
  185. }
  186. }
  187. static void mousedev_key_event(struct mousedev *mousedev,
  188. unsigned int code, int value)
  189. {
  190. int index;
  191. switch (code) {
  192. case BTN_TOUCH:
  193. case BTN_0:
  194. case BTN_LEFT: index = 0; break;
  195. case BTN_STYLUS:
  196. case BTN_1:
  197. case BTN_RIGHT: index = 1; break;
  198. case BTN_2:
  199. case BTN_FORWARD:
  200. case BTN_STYLUS2:
  201. case BTN_MIDDLE: index = 2; break;
  202. case BTN_3:
  203. case BTN_BACK:
  204. case BTN_SIDE: index = 3; break;
  205. case BTN_4:
  206. case BTN_EXTRA: index = 4; break;
  207. default: return;
  208. }
  209. if (value) {
  210. set_bit(index, &mousedev->packet.buttons);
  211. set_bit(index, &mousedev_mix->packet.buttons);
  212. } else {
  213. clear_bit(index, &mousedev->packet.buttons);
  214. clear_bit(index, &mousedev_mix->packet.buttons);
  215. }
  216. }
  217. static void mousedev_notify_readers(struct mousedev *mousedev,
  218. struct mousedev_hw_data *packet)
  219. {
  220. struct mousedev_client *client;
  221. struct mousedev_motion *p;
  222. unsigned int new_head;
  223. int wake_readers = 0;
  224. rcu_read_lock();
  225. list_for_each_entry_rcu(client, &mousedev->client_list, node) {
  226. /* Just acquire the lock, interrupts already disabled */
  227. spin_lock(&client->packet_lock);
  228. p = &client->packets[client->head];
  229. if (client->ready && p->buttons != mousedev->packet.buttons) {
  230. new_head = (client->head + 1) % PACKET_QUEUE_LEN;
  231. if (new_head != client->tail) {
  232. p = &client->packets[client->head = new_head];
  233. memset(p, 0, sizeof(struct mousedev_motion));
  234. }
  235. }
  236. if (packet->abs_event) {
  237. p->dx += packet->x - client->pos_x;
  238. p->dy += packet->y - client->pos_y;
  239. client->pos_x = packet->x;
  240. client->pos_y = packet->y;
  241. }
  242. client->pos_x += packet->dx;
  243. client->pos_x = client->pos_x < 0 ?
  244. 0 : (client->pos_x >= xres ? xres : client->pos_x);
  245. client->pos_y += packet->dy;
  246. client->pos_y = client->pos_y < 0 ?
  247. 0 : (client->pos_y >= yres ? yres : client->pos_y);
  248. p->dx += packet->dx;
  249. p->dy += packet->dy;
  250. p->dz += packet->dz;
  251. p->buttons = mousedev->packet.buttons;
  252. if (p->dx || p->dy || p->dz ||
  253. p->buttons != client->last_buttons)
  254. client->ready = 1;
  255. spin_unlock(&client->packet_lock);
  256. if (client->ready) {
  257. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  258. wake_readers = 1;
  259. }
  260. }
  261. rcu_read_unlock();
  262. if (wake_readers)
  263. wake_up_interruptible(&mousedev->wait);
  264. }
  265. static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
  266. {
  267. if (!value) {
  268. if (mousedev->touch &&
  269. time_before(jiffies,
  270. mousedev->touch + msecs_to_jiffies(tap_time))) {
  271. /*
  272. * Toggle left button to emulate tap.
  273. * We rely on the fact that mousedev_mix always has 0
  274. * motion packet so we won't mess current position.
  275. */
  276. set_bit(0, &mousedev->packet.buttons);
  277. set_bit(0, &mousedev_mix->packet.buttons);
  278. mousedev_notify_readers(mousedev, &mousedev_mix->packet);
  279. mousedev_notify_readers(mousedev_mix,
  280. &mousedev_mix->packet);
  281. clear_bit(0, &mousedev->packet.buttons);
  282. clear_bit(0, &mousedev_mix->packet.buttons);
  283. }
  284. mousedev->touch = mousedev->pkt_count = 0;
  285. mousedev->frac_dx = 0;
  286. mousedev->frac_dy = 0;
  287. } else if (!mousedev->touch)
  288. mousedev->touch = jiffies;
  289. }
  290. static void mousedev_event(struct input_handle *handle,
  291. unsigned int type, unsigned int code, int value)
  292. {
  293. struct mousedev *mousedev = handle->private;
  294. switch (type) {
  295. case EV_ABS:
  296. /* Ignore joysticks */
  297. if (test_bit(BTN_TRIGGER, handle->dev->keybit))
  298. return;
  299. if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
  300. mousedev_touchpad_event(handle->dev,
  301. mousedev, code, value);
  302. else
  303. mousedev_abs_event(handle->dev, mousedev, code, value);
  304. break;
  305. case EV_REL:
  306. mousedev_rel_event(mousedev, code, value);
  307. break;
  308. case EV_KEY:
  309. if (value != 2) {
  310. if (code == BTN_TOUCH &&
  311. test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
  312. mousedev_touchpad_touch(mousedev, value);
  313. else
  314. mousedev_key_event(mousedev, code, value);
  315. }
  316. break;
  317. case EV_SYN:
  318. if (code == SYN_REPORT) {
  319. if (mousedev->touch) {
  320. mousedev->pkt_count++;
  321. /*
  322. * Input system eats duplicate events,
  323. * but we need all of them to do correct
  324. * averaging so apply present one forward
  325. */
  326. fx(0) = fx(1);
  327. fy(0) = fy(1);
  328. }
  329. mousedev_notify_readers(mousedev, &mousedev->packet);
  330. mousedev_notify_readers(mousedev_mix, &mousedev->packet);
  331. mousedev->packet.dx = mousedev->packet.dy =
  332. mousedev->packet.dz = 0;
  333. mousedev->packet.abs_event = 0;
  334. }
  335. break;
  336. }
  337. }
  338. static int mousedev_fasync(int fd, struct file *file, int on)
  339. {
  340. int retval;
  341. struct mousedev_client *client = file->private_data;
  342. retval = fasync_helper(fd, file, on, &client->fasync);
  343. return retval < 0 ? retval : 0;
  344. }
  345. static void mousedev_free(struct device *dev)
  346. {
  347. struct mousedev *mousedev = container_of(dev, struct mousedev, dev);
  348. input_put_device(mousedev->handle.dev);
  349. kfree(mousedev);
  350. }
  351. static int mousedev_open_device(struct mousedev *mousedev)
  352. {
  353. int retval;
  354. retval = mutex_lock_interruptible(&mousedev->mutex);
  355. if (retval)
  356. return retval;
  357. if (mousedev->minor == MOUSEDEV_MIX)
  358. mixdev_open_devices();
  359. else if (!mousedev->exist)
  360. retval = -ENODEV;
  361. else if (!mousedev->open++) {
  362. retval = input_open_device(&mousedev->handle);
  363. if (retval)
  364. mousedev->open--;
  365. }
  366. mutex_unlock(&mousedev->mutex);
  367. return retval;
  368. }
  369. static void mousedev_close_device(struct mousedev *mousedev)
  370. {
  371. mutex_lock(&mousedev->mutex);
  372. if (mousedev->minor == MOUSEDEV_MIX)
  373. mixdev_close_devices();
  374. else if (mousedev->exist && !--mousedev->open)
  375. input_close_device(&mousedev->handle);
  376. mutex_unlock(&mousedev->mutex);
  377. }
  378. /*
  379. * Open all available devices so they can all be multiplexed in one.
  380. * stream. Note that this function is called with mousedev_mix->mutex
  381. * held.
  382. */
  383. static void mixdev_open_devices(void)
  384. {
  385. struct mousedev *mousedev;
  386. if (mousedev_mix->open++)
  387. return;
  388. list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
  389. if (!mousedev->mixdev_open) {
  390. if (mousedev_open_device(mousedev))
  391. continue;
  392. mousedev->mixdev_open = 1;
  393. }
  394. }
  395. }
  396. /*
  397. * Close all devices that were opened as part of multiplexed
  398. * device. Note that this function is called with mousedev_mix->mutex
  399. * held.
  400. */
  401. static void mixdev_close_devices(void)
  402. {
  403. struct mousedev *mousedev;
  404. if (--mousedev_mix->open)
  405. return;
  406. list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
  407. if (mousedev->mixdev_open) {
  408. mousedev->mixdev_open = 0;
  409. mousedev_close_device(mousedev);
  410. }
  411. }
  412. }
  413. static void mousedev_attach_client(struct mousedev *mousedev,
  414. struct mousedev_client *client)
  415. {
  416. spin_lock(&mousedev->client_lock);
  417. list_add_tail_rcu(&client->node, &mousedev->client_list);
  418. spin_unlock(&mousedev->client_lock);
  419. synchronize_rcu();
  420. }
  421. static void mousedev_detach_client(struct mousedev *mousedev,
  422. struct mousedev_client *client)
  423. {
  424. spin_lock(&mousedev->client_lock);
  425. list_del_rcu(&client->node);
  426. spin_unlock(&mousedev->client_lock);
  427. synchronize_rcu();
  428. }
  429. static int mousedev_release(struct inode *inode, struct file *file)
  430. {
  431. struct mousedev_client *client = file->private_data;
  432. struct mousedev *mousedev = client->mousedev;
  433. mousedev_detach_client(mousedev, client);
  434. kfree(client);
  435. mousedev_close_device(mousedev);
  436. put_device(&mousedev->dev);
  437. return 0;
  438. }
  439. static int mousedev_open(struct inode *inode, struct file *file)
  440. {
  441. struct mousedev_client *client;
  442. struct mousedev *mousedev;
  443. int error;
  444. int i;
  445. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  446. if (imajor(inode) == MISC_MAJOR)
  447. i = MOUSEDEV_MIX;
  448. else
  449. #endif
  450. i = iminor(inode) - MOUSEDEV_MINOR_BASE;
  451. if (i >= MOUSEDEV_MINORS)
  452. return -ENODEV;
  453. lock_kernel();
  454. error = mutex_lock_interruptible(&mousedev_table_mutex);
  455. if (error) {
  456. unlock_kernel();
  457. return error;
  458. }
  459. mousedev = mousedev_table[i];
  460. if (mousedev)
  461. get_device(&mousedev->dev);
  462. mutex_unlock(&mousedev_table_mutex);
  463. if (!mousedev) {
  464. unlock_kernel();
  465. return -ENODEV;
  466. }
  467. client = kzalloc(sizeof(struct mousedev_client), GFP_KERNEL);
  468. if (!client) {
  469. error = -ENOMEM;
  470. goto err_put_mousedev;
  471. }
  472. spin_lock_init(&client->packet_lock);
  473. client->pos_x = xres / 2;
  474. client->pos_y = yres / 2;
  475. client->mousedev = mousedev;
  476. mousedev_attach_client(mousedev, client);
  477. error = mousedev_open_device(mousedev);
  478. if (error)
  479. goto err_free_client;
  480. file->private_data = client;
  481. unlock_kernel();
  482. return 0;
  483. err_free_client:
  484. mousedev_detach_client(mousedev, client);
  485. kfree(client);
  486. err_put_mousedev:
  487. put_device(&mousedev->dev);
  488. unlock_kernel();
  489. return error;
  490. }
  491. static inline int mousedev_limit_delta(int delta, int limit)
  492. {
  493. return delta > limit ? limit : (delta < -limit ? -limit : delta);
  494. }
  495. static void mousedev_packet(struct mousedev_client *client,
  496. signed char *ps2_data)
  497. {
  498. struct mousedev_motion *p = &client->packets[client->tail];
  499. ps2_data[0] = 0x08 |
  500. ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
  501. ps2_data[1] = mousedev_limit_delta(p->dx, 127);
  502. ps2_data[2] = mousedev_limit_delta(p->dy, 127);
  503. p->dx -= ps2_data[1];
  504. p->dy -= ps2_data[2];
  505. switch (client->mode) {
  506. case MOUSEDEV_EMUL_EXPS:
  507. ps2_data[3] = mousedev_limit_delta(p->dz, 7);
  508. p->dz -= ps2_data[3];
  509. ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
  510. client->bufsiz = 4;
  511. break;
  512. case MOUSEDEV_EMUL_IMPS:
  513. ps2_data[0] |=
  514. ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
  515. ps2_data[3] = mousedev_limit_delta(p->dz, 127);
  516. p->dz -= ps2_data[3];
  517. client->bufsiz = 4;
  518. break;
  519. case MOUSEDEV_EMUL_PS2:
  520. default:
  521. ps2_data[0] |=
  522. ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
  523. p->dz = 0;
  524. client->bufsiz = 3;
  525. break;
  526. }
  527. if (!p->dx && !p->dy && !p->dz) {
  528. if (client->tail == client->head) {
  529. client->ready = 0;
  530. client->last_buttons = p->buttons;
  531. } else
  532. client->tail = (client->tail + 1) % PACKET_QUEUE_LEN;
  533. }
  534. }
  535. static void mousedev_generate_response(struct mousedev_client *client,
  536. int command)
  537. {
  538. client->ps2[0] = 0xfa; /* ACK */
  539. switch (command) {
  540. case 0xeb: /* Poll */
  541. mousedev_packet(client, &client->ps2[1]);
  542. client->bufsiz++; /* account for leading ACK */
  543. break;
  544. case 0xf2: /* Get ID */
  545. switch (client->mode) {
  546. case MOUSEDEV_EMUL_PS2:
  547. client->ps2[1] = 0;
  548. break;
  549. case MOUSEDEV_EMUL_IMPS:
  550. client->ps2[1] = 3;
  551. break;
  552. case MOUSEDEV_EMUL_EXPS:
  553. client->ps2[1] = 4;
  554. break;
  555. }
  556. client->bufsiz = 2;
  557. break;
  558. case 0xe9: /* Get info */
  559. client->ps2[1] = 0x60; client->ps2[2] = 3; client->ps2[3] = 200;
  560. client->bufsiz = 4;
  561. break;
  562. case 0xff: /* Reset */
  563. client->impsseq = client->imexseq = 0;
  564. client->mode = MOUSEDEV_EMUL_PS2;
  565. client->ps2[1] = 0xaa; client->ps2[2] = 0x00;
  566. client->bufsiz = 3;
  567. break;
  568. default:
  569. client->bufsiz = 1;
  570. break;
  571. }
  572. client->buffer = client->bufsiz;
  573. }
  574. static ssize_t mousedev_write(struct file *file, const char __user *buffer,
  575. size_t count, loff_t *ppos)
  576. {
  577. struct mousedev_client *client = file->private_data;
  578. unsigned char c;
  579. unsigned int i;
  580. for (i = 0; i < count; i++) {
  581. if (get_user(c, buffer + i))
  582. return -EFAULT;
  583. spin_lock_irq(&client->packet_lock);
  584. if (c == mousedev_imex_seq[client->imexseq]) {
  585. if (++client->imexseq == MOUSEDEV_SEQ_LEN) {
  586. client->imexseq = 0;
  587. client->mode = MOUSEDEV_EMUL_EXPS;
  588. }
  589. } else
  590. client->imexseq = 0;
  591. if (c == mousedev_imps_seq[client->impsseq]) {
  592. if (++client->impsseq == MOUSEDEV_SEQ_LEN) {
  593. client->impsseq = 0;
  594. client->mode = MOUSEDEV_EMUL_IMPS;
  595. }
  596. } else
  597. client->impsseq = 0;
  598. mousedev_generate_response(client, c);
  599. spin_unlock_irq(&client->packet_lock);
  600. }
  601. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  602. wake_up_interruptible(&client->mousedev->wait);
  603. return count;
  604. }
  605. static ssize_t mousedev_read(struct file *file, char __user *buffer,
  606. size_t count, loff_t *ppos)
  607. {
  608. struct mousedev_client *client = file->private_data;
  609. struct mousedev *mousedev = client->mousedev;
  610. signed char data[sizeof(client->ps2)];
  611. int retval = 0;
  612. if (!client->ready && !client->buffer && mousedev->exist &&
  613. (file->f_flags & O_NONBLOCK))
  614. return -EAGAIN;
  615. retval = wait_event_interruptible(mousedev->wait,
  616. !mousedev->exist || client->ready || client->buffer);
  617. if (retval)
  618. return retval;
  619. if (!mousedev->exist)
  620. return -ENODEV;
  621. spin_lock_irq(&client->packet_lock);
  622. if (!client->buffer && client->ready) {
  623. mousedev_packet(client, client->ps2);
  624. client->buffer = client->bufsiz;
  625. }
  626. if (count > client->buffer)
  627. count = client->buffer;
  628. memcpy(data, client->ps2 + client->bufsiz - client->buffer, count);
  629. client->buffer -= count;
  630. spin_unlock_irq(&client->packet_lock);
  631. if (copy_to_user(buffer, data, count))
  632. return -EFAULT;
  633. return count;
  634. }
  635. /* No kernel lock - fine */
  636. static unsigned int mousedev_poll(struct file *file, poll_table *wait)
  637. {
  638. struct mousedev_client *client = file->private_data;
  639. struct mousedev *mousedev = client->mousedev;
  640. poll_wait(file, &mousedev->wait, wait);
  641. return ((client->ready || client->buffer) ? (POLLIN | POLLRDNORM) : 0) |
  642. (mousedev->exist ? 0 : (POLLHUP | POLLERR));
  643. }
  644. static const struct file_operations mousedev_fops = {
  645. .owner = THIS_MODULE,
  646. .read = mousedev_read,
  647. .write = mousedev_write,
  648. .poll = mousedev_poll,
  649. .open = mousedev_open,
  650. .release = mousedev_release,
  651. .fasync = mousedev_fasync,
  652. };
  653. static int mousedev_install_chrdev(struct mousedev *mousedev)
  654. {
  655. mousedev_table[mousedev->minor] = mousedev;
  656. return 0;
  657. }
  658. static void mousedev_remove_chrdev(struct mousedev *mousedev)
  659. {
  660. mutex_lock(&mousedev_table_mutex);
  661. mousedev_table[mousedev->minor] = NULL;
  662. mutex_unlock(&mousedev_table_mutex);
  663. }
  664. /*
  665. * Mark device non-existent. This disables writes, ioctls and
  666. * prevents new users from opening the device. Already posted
  667. * blocking reads will stay, however new ones will fail.
  668. */
  669. static void mousedev_mark_dead(struct mousedev *mousedev)
  670. {
  671. mutex_lock(&mousedev->mutex);
  672. mousedev->exist = 0;
  673. mutex_unlock(&mousedev->mutex);
  674. }
  675. /*
  676. * Wake up users waiting for IO so they can disconnect from
  677. * dead device.
  678. */
  679. static void mousedev_hangup(struct mousedev *mousedev)
  680. {
  681. struct mousedev_client *client;
  682. spin_lock(&mousedev->client_lock);
  683. list_for_each_entry(client, &mousedev->client_list, node)
  684. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  685. spin_unlock(&mousedev->client_lock);
  686. wake_up_interruptible(&mousedev->wait);
  687. }
  688. static void mousedev_cleanup(struct mousedev *mousedev)
  689. {
  690. struct input_handle *handle = &mousedev->handle;
  691. mousedev_mark_dead(mousedev);
  692. mousedev_hangup(mousedev);
  693. mousedev_remove_chrdev(mousedev);
  694. /* mousedev is marked dead so no one else accesses mousedev->open */
  695. if (mousedev->open)
  696. input_close_device(handle);
  697. }
  698. static struct mousedev *mousedev_create(struct input_dev *dev,
  699. struct input_handler *handler,
  700. int minor)
  701. {
  702. struct mousedev *mousedev;
  703. int error;
  704. mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL);
  705. if (!mousedev) {
  706. error = -ENOMEM;
  707. goto err_out;
  708. }
  709. INIT_LIST_HEAD(&mousedev->client_list);
  710. INIT_LIST_HEAD(&mousedev->mixdev_node);
  711. spin_lock_init(&mousedev->client_lock);
  712. mutex_init(&mousedev->mutex);
  713. lockdep_set_subclass(&mousedev->mutex,
  714. minor == MOUSEDEV_MIX ? MOUSEDEV_MIX : 0);
  715. init_waitqueue_head(&mousedev->wait);
  716. if (minor == MOUSEDEV_MIX)
  717. strlcpy(mousedev->name, "mice", sizeof(mousedev->name));
  718. else
  719. snprintf(mousedev->name, sizeof(mousedev->name),
  720. "mouse%d", minor);
  721. mousedev->minor = minor;
  722. mousedev->exist = 1;
  723. mousedev->handle.dev = input_get_device(dev);
  724. mousedev->handle.name = mousedev->name;
  725. mousedev->handle.handler = handler;
  726. mousedev->handle.private = mousedev;
  727. strlcpy(mousedev->dev.bus_id, mousedev->name,
  728. sizeof(mousedev->dev.bus_id));
  729. mousedev->dev.class = &input_class;
  730. if (dev)
  731. mousedev->dev.parent = &dev->dev;
  732. mousedev->dev.devt = MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor);
  733. mousedev->dev.release = mousedev_free;
  734. device_initialize(&mousedev->dev);
  735. if (minor != MOUSEDEV_MIX) {
  736. error = input_register_handle(&mousedev->handle);
  737. if (error)
  738. goto err_free_mousedev;
  739. }
  740. error = mousedev_install_chrdev(mousedev);
  741. if (error)
  742. goto err_unregister_handle;
  743. error = device_add(&mousedev->dev);
  744. if (error)
  745. goto err_cleanup_mousedev;
  746. return mousedev;
  747. err_cleanup_mousedev:
  748. mousedev_cleanup(mousedev);
  749. err_unregister_handle:
  750. if (minor != MOUSEDEV_MIX)
  751. input_unregister_handle(&mousedev->handle);
  752. err_free_mousedev:
  753. put_device(&mousedev->dev);
  754. err_out:
  755. return ERR_PTR(error);
  756. }
  757. static void mousedev_destroy(struct mousedev *mousedev)
  758. {
  759. device_del(&mousedev->dev);
  760. mousedev_cleanup(mousedev);
  761. if (mousedev->minor != MOUSEDEV_MIX)
  762. input_unregister_handle(&mousedev->handle);
  763. put_device(&mousedev->dev);
  764. }
  765. static int mixdev_add_device(struct mousedev *mousedev)
  766. {
  767. int retval;
  768. retval = mutex_lock_interruptible(&mousedev_mix->mutex);
  769. if (retval)
  770. return retval;
  771. if (mousedev_mix->open) {
  772. retval = mousedev_open_device(mousedev);
  773. if (retval)
  774. goto out;
  775. mousedev->mixdev_open = 1;
  776. }
  777. get_device(&mousedev->dev);
  778. list_add_tail(&mousedev->mixdev_node, &mousedev_mix_list);
  779. out:
  780. mutex_unlock(&mousedev_mix->mutex);
  781. return retval;
  782. }
  783. static void mixdev_remove_device(struct mousedev *mousedev)
  784. {
  785. mutex_lock(&mousedev_mix->mutex);
  786. if (mousedev->mixdev_open) {
  787. mousedev->mixdev_open = 0;
  788. mousedev_close_device(mousedev);
  789. }
  790. list_del_init(&mousedev->mixdev_node);
  791. mutex_unlock(&mousedev_mix->mutex);
  792. put_device(&mousedev->dev);
  793. }
  794. static int mousedev_connect(struct input_handler *handler,
  795. struct input_dev *dev,
  796. const struct input_device_id *id)
  797. {
  798. struct mousedev *mousedev;
  799. int minor;
  800. int error;
  801. for (minor = 0; minor < MOUSEDEV_MINORS; minor++)
  802. if (!mousedev_table[minor])
  803. break;
  804. if (minor == MOUSEDEV_MINORS) {
  805. printk(KERN_ERR "mousedev: no more free mousedev devices\n");
  806. return -ENFILE;
  807. }
  808. mousedev = mousedev_create(dev, handler, minor);
  809. if (IS_ERR(mousedev))
  810. return PTR_ERR(mousedev);
  811. error = mixdev_add_device(mousedev);
  812. if (error) {
  813. mousedev_destroy(mousedev);
  814. return error;
  815. }
  816. return 0;
  817. }
  818. static void mousedev_disconnect(struct input_handle *handle)
  819. {
  820. struct mousedev *mousedev = handle->private;
  821. mixdev_remove_device(mousedev);
  822. mousedev_destroy(mousedev);
  823. }
  824. static const struct input_device_id mousedev_ids[] = {
  825. {
  826. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  827. INPUT_DEVICE_ID_MATCH_KEYBIT |
  828. INPUT_DEVICE_ID_MATCH_RELBIT,
  829. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) },
  830. .keybit = { [BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) },
  831. .relbit = { BIT_MASK(REL_X) | BIT_MASK(REL_Y) },
  832. }, /* A mouse like device, at least one button,
  833. two relative axes */
  834. {
  835. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  836. INPUT_DEVICE_ID_MATCH_RELBIT,
  837. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) },
  838. .relbit = { BIT_MASK(REL_WHEEL) },
  839. }, /* A separate scrollwheel */
  840. {
  841. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  842. INPUT_DEVICE_ID_MATCH_KEYBIT |
  843. INPUT_DEVICE_ID_MATCH_ABSBIT,
  844. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) },
  845. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  846. .absbit = { BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
  847. }, /* A tablet like device, at least touch detection,
  848. two absolute axes */
  849. {
  850. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  851. INPUT_DEVICE_ID_MATCH_KEYBIT |
  852. INPUT_DEVICE_ID_MATCH_ABSBIT,
  853. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) },
  854. .keybit = { [BIT_WORD(BTN_TOOL_FINGER)] =
  855. BIT_MASK(BTN_TOOL_FINGER) },
  856. .absbit = { BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
  857. BIT_MASK(ABS_PRESSURE) |
  858. BIT_MASK(ABS_TOOL_WIDTH) },
  859. }, /* A touchpad */
  860. {
  861. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  862. INPUT_DEVICE_ID_MATCH_KEYBIT |
  863. INPUT_DEVICE_ID_MATCH_ABSBIT,
  864. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) },
  865. .keybit = { [BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) },
  866. .absbit = { BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
  867. }, /* Mouse-like device with absolute X and Y but ordinary
  868. clicks, like hp ILO2 High Performance mouse */
  869. { }, /* Terminating entry */
  870. };
  871. MODULE_DEVICE_TABLE(input, mousedev_ids);
  872. static struct input_handler mousedev_handler = {
  873. .event = mousedev_event,
  874. .connect = mousedev_connect,
  875. .disconnect = mousedev_disconnect,
  876. .fops = &mousedev_fops,
  877. .minor = MOUSEDEV_MINOR_BASE,
  878. .name = "mousedev",
  879. .id_table = mousedev_ids,
  880. };
  881. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  882. static struct miscdevice psaux_mouse = {
  883. PSMOUSE_MINOR, "psaux", &mousedev_fops
  884. };
  885. static int psaux_registered;
  886. #endif
  887. static int __init mousedev_init(void)
  888. {
  889. int error;
  890. mousedev_mix = mousedev_create(NULL, &mousedev_handler, MOUSEDEV_MIX);
  891. if (IS_ERR(mousedev_mix))
  892. return PTR_ERR(mousedev_mix);
  893. error = input_register_handler(&mousedev_handler);
  894. if (error) {
  895. mousedev_destroy(mousedev_mix);
  896. return error;
  897. }
  898. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  899. error = misc_register(&psaux_mouse);
  900. if (error)
  901. printk(KERN_WARNING "mice: could not register psaux device, "
  902. "error: %d\n", error);
  903. else
  904. psaux_registered = 1;
  905. #endif
  906. printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
  907. return 0;
  908. }
  909. static void __exit mousedev_exit(void)
  910. {
  911. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  912. if (psaux_registered)
  913. misc_deregister(&psaux_mouse);
  914. #endif
  915. input_unregister_handler(&mousedev_handler);
  916. mousedev_destroy(mousedev_mix);
  917. }
  918. module_init(mousedev_init);
  919. module_exit(mousedev_exit);