mousedev.c 26 KB

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