mousedev.c 26 KB

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