mousedev.c 26 KB

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