mousedev.c 26 KB

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