mousedev.c 26 KB

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