mousedev.c 26 KB

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