mousedev.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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. /* mousedev is marked dead so no one else accesses mousedev->open */
  659. if (mousedev->open)
  660. input_close_device(handle);
  661. }
  662. static int mousedev_reserve_minor(bool mixdev)
  663. {
  664. int minor;
  665. if (mixdev) {
  666. minor = input_get_new_minor(MOUSEDEV_MIX, 1, false);
  667. if (minor < 0)
  668. pr_err("failed to reserve mixdev minor: %d\n", minor);
  669. } else {
  670. minor = input_get_new_minor(MOUSEDEV_MINOR_BASE,
  671. MOUSEDEV_MINORS, true);
  672. if (minor < 0)
  673. pr_err("failed to reserve new minor: %d\n", minor);
  674. }
  675. return minor;
  676. }
  677. static struct mousedev *mousedev_create(struct input_dev *dev,
  678. struct input_handler *handler,
  679. bool mixdev)
  680. {
  681. struct mousedev *mousedev;
  682. int minor;
  683. int error;
  684. minor = mousedev_reserve_minor(mixdev);
  685. if (minor < 0) {
  686. error = minor;
  687. goto err_out;
  688. }
  689. mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL);
  690. if (!mousedev) {
  691. error = -ENOMEM;
  692. goto err_free_minor;
  693. }
  694. INIT_LIST_HEAD(&mousedev->client_list);
  695. INIT_LIST_HEAD(&mousedev->mixdev_node);
  696. spin_lock_init(&mousedev->client_lock);
  697. mutex_init(&mousedev->mutex);
  698. lockdep_set_subclass(&mousedev->mutex,
  699. mixdev ? SINGLE_DEPTH_NESTING : 0);
  700. init_waitqueue_head(&mousedev->wait);
  701. if (mixdev) {
  702. dev_set_name(&mousedev->dev, "mice");
  703. mousedev->open_device = mixdev_open_devices;
  704. mousedev->close_device = mixdev_close_devices;
  705. } else {
  706. int dev_no = minor;
  707. /* Normalize device number if it falls into legacy range */
  708. if (dev_no < MOUSEDEV_MINOR_BASE + MOUSEDEV_MINORS)
  709. dev_no -= MOUSEDEV_MINOR_BASE;
  710. dev_set_name(&mousedev->dev, "mouse%d", dev_no);
  711. mousedev->open_device = mousedev_open_device;
  712. mousedev->close_device = mousedev_close_device;
  713. }
  714. mousedev->exist = true;
  715. mousedev->handle.dev = input_get_device(dev);
  716. mousedev->handle.name = dev_name(&mousedev->dev);
  717. mousedev->handle.handler = handler;
  718. mousedev->handle.private = mousedev;
  719. mousedev->dev.class = &input_class;
  720. if (dev)
  721. mousedev->dev.parent = &dev->dev;
  722. mousedev->dev.devt = MKDEV(INPUT_MAJOR, minor);
  723. mousedev->dev.release = mousedev_free;
  724. device_initialize(&mousedev->dev);
  725. if (!mixdev) {
  726. error = input_register_handle(&mousedev->handle);
  727. if (error)
  728. goto err_free_mousedev;
  729. }
  730. cdev_init(&mousedev->cdev, &mousedev_fops);
  731. error = cdev_device_add(&mousedev->cdev, &mousedev->dev);
  732. if (error)
  733. goto err_cleanup_mousedev;
  734. return mousedev;
  735. err_cleanup_mousedev:
  736. mousedev_cleanup(mousedev);
  737. if (!mixdev)
  738. input_unregister_handle(&mousedev->handle);
  739. err_free_mousedev:
  740. put_device(&mousedev->dev);
  741. err_free_minor:
  742. input_free_minor(minor);
  743. err_out:
  744. return ERR_PTR(error);
  745. }
  746. static void mousedev_destroy(struct mousedev *mousedev)
  747. {
  748. cdev_device_del(&mousedev->cdev, &mousedev->dev);
  749. mousedev_cleanup(mousedev);
  750. input_free_minor(MINOR(mousedev->dev.devt));
  751. if (mousedev != mousedev_mix)
  752. input_unregister_handle(&mousedev->handle);
  753. put_device(&mousedev->dev);
  754. }
  755. static int mixdev_add_device(struct mousedev *mousedev)
  756. {
  757. int retval;
  758. retval = mutex_lock_interruptible(&mousedev_mix->mutex);
  759. if (retval)
  760. return retval;
  761. if (mousedev_mix->open) {
  762. retval = mousedev_open_device(mousedev);
  763. if (retval)
  764. goto out;
  765. mousedev->opened_by_mixdev = true;
  766. }
  767. get_device(&mousedev->dev);
  768. list_add_tail(&mousedev->mixdev_node, &mousedev_mix_list);
  769. out:
  770. mutex_unlock(&mousedev_mix->mutex);
  771. return retval;
  772. }
  773. static void mixdev_remove_device(struct mousedev *mousedev)
  774. {
  775. mutex_lock(&mousedev_mix->mutex);
  776. if (mousedev->opened_by_mixdev) {
  777. mousedev->opened_by_mixdev = false;
  778. mousedev_close_device(mousedev);
  779. }
  780. list_del_init(&mousedev->mixdev_node);
  781. mutex_unlock(&mousedev_mix->mutex);
  782. put_device(&mousedev->dev);
  783. }
  784. static int mousedev_connect(struct input_handler *handler,
  785. struct input_dev *dev,
  786. const struct input_device_id *id)
  787. {
  788. struct mousedev *mousedev;
  789. int error;
  790. mousedev = mousedev_create(dev, handler, false);
  791. if (IS_ERR(mousedev))
  792. return PTR_ERR(mousedev);
  793. error = mixdev_add_device(mousedev);
  794. if (error) {
  795. mousedev_destroy(mousedev);
  796. return error;
  797. }
  798. return 0;
  799. }
  800. static void mousedev_disconnect(struct input_handle *handle)
  801. {
  802. struct mousedev *mousedev = handle->private;
  803. mixdev_remove_device(mousedev);
  804. mousedev_destroy(mousedev);
  805. }
  806. static const struct input_device_id mousedev_ids[] = {
  807. {
  808. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  809. INPUT_DEVICE_ID_MATCH_KEYBIT |
  810. INPUT_DEVICE_ID_MATCH_RELBIT,
  811. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) },
  812. .keybit = { [BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) },
  813. .relbit = { BIT_MASK(REL_X) | BIT_MASK(REL_Y) },
  814. }, /* A mouse like device, at least one button,
  815. two relative axes */
  816. {
  817. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  818. INPUT_DEVICE_ID_MATCH_RELBIT,
  819. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) },
  820. .relbit = { BIT_MASK(REL_WHEEL) },
  821. }, /* A separate scrollwheel */
  822. {
  823. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  824. INPUT_DEVICE_ID_MATCH_KEYBIT |
  825. INPUT_DEVICE_ID_MATCH_ABSBIT,
  826. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) },
  827. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  828. .absbit = { BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
  829. }, /* A tablet like device, at least touch detection,
  830. two absolute axes */
  831. {
  832. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  833. INPUT_DEVICE_ID_MATCH_KEYBIT |
  834. INPUT_DEVICE_ID_MATCH_ABSBIT,
  835. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) },
  836. .keybit = { [BIT_WORD(BTN_TOOL_FINGER)] =
  837. BIT_MASK(BTN_TOOL_FINGER) },
  838. .absbit = { BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
  839. BIT_MASK(ABS_PRESSURE) |
  840. BIT_MASK(ABS_TOOL_WIDTH) },
  841. }, /* A touchpad */
  842. {
  843. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  844. INPUT_DEVICE_ID_MATCH_KEYBIT |
  845. INPUT_DEVICE_ID_MATCH_ABSBIT,
  846. .evbit = { BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) },
  847. .keybit = { [BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) },
  848. .absbit = { BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
  849. }, /* Mouse-like device with absolute X and Y but ordinary
  850. clicks, like hp ILO2 High Performance mouse */
  851. { }, /* Terminating entry */
  852. };
  853. MODULE_DEVICE_TABLE(input, mousedev_ids);
  854. static struct input_handler mousedev_handler = {
  855. .event = mousedev_event,
  856. .connect = mousedev_connect,
  857. .disconnect = mousedev_disconnect,
  858. .legacy_minors = true,
  859. .minor = MOUSEDEV_MINOR_BASE,
  860. .name = "mousedev",
  861. .id_table = mousedev_ids,
  862. };
  863. #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
  864. #include <linux/miscdevice.h>
  865. static struct miscdevice psaux_mouse = {
  866. .minor = PSMOUSE_MINOR,
  867. .name = "psaux",
  868. .fops = &mousedev_fops,
  869. };
  870. static bool psaux_registered;
  871. static void __init mousedev_psaux_register(void)
  872. {
  873. int error;
  874. error = misc_register(&psaux_mouse);
  875. if (error)
  876. pr_warn("could not register psaux device, error: %d\n",
  877. error);
  878. else
  879. psaux_registered = true;
  880. }
  881. static void __exit mousedev_psaux_unregister(void)
  882. {
  883. if (psaux_registered)
  884. misc_deregister(&psaux_mouse);
  885. }
  886. #else
  887. static inline void mousedev_psaux_register(void) { }
  888. static inline void mousedev_psaux_unregister(void) { }
  889. #endif
  890. static int __init mousedev_init(void)
  891. {
  892. int error;
  893. mousedev_mix = mousedev_create(NULL, &mousedev_handler, true);
  894. if (IS_ERR(mousedev_mix))
  895. return PTR_ERR(mousedev_mix);
  896. error = input_register_handler(&mousedev_handler);
  897. if (error) {
  898. mousedev_destroy(mousedev_mix);
  899. return error;
  900. }
  901. mousedev_psaux_register();
  902. pr_info("PS/2 mouse device common for all mice\n");
  903. return 0;
  904. }
  905. static void __exit mousedev_exit(void)
  906. {
  907. mousedev_psaux_unregister();
  908. input_unregister_handler(&mousedev_handler);
  909. mousedev_destroy(mousedev_mix);
  910. }
  911. module_init(mousedev_init);
  912. module_exit(mousedev_exit);