nvec_ps2.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * nvec_ps2: mouse driver for a NVIDIA compliant embedded controller
  3. *
  4. * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net>
  5. *
  6. * Authors: Pierre-Hugues Husson <phhusson@free.fr>
  7. * Ilya Petrov <ilya.muromec@gmail.com>
  8. * Marc Dietrich <marvin24@gmx.de>
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/serio.h>
  18. #include <linux/delay.h>
  19. #include <linux/platform_device.h>
  20. #include "nvec.h"
  21. #define PACKET_SIZE 6
  22. #define ENABLE_MOUSE 0xf4
  23. #define DISABLE_MOUSE 0xf5
  24. #define PSMOUSE_RST 0xff
  25. #ifdef NVEC_PS2_DEBUG
  26. #define NVEC_PHD(str, buf, len) \
  27. print_hex_dump(KERN_DEBUG, str, DUMP_PREFIX_NONE, \
  28. 16, 1, buf, len, false)
  29. #else
  30. #define NVEC_PHD(str, buf, len)
  31. #endif
  32. enum ps2_subcmds {
  33. SEND_COMMAND = 1,
  34. RECEIVE_N,
  35. AUTO_RECEIVE_N,
  36. CANCEL_AUTO_RECEIVE,
  37. };
  38. struct nvec_ps2 {
  39. struct serio *ser_dev;
  40. struct notifier_block notifier;
  41. struct nvec_chip *nvec;
  42. };
  43. static struct nvec_ps2 ps2_dev;
  44. static int ps2_startstreaming(struct serio *ser_dev)
  45. {
  46. unsigned char buf[] = { NVEC_PS2, AUTO_RECEIVE_N, PACKET_SIZE };
  47. return nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
  48. }
  49. static void ps2_stopstreaming(struct serio *ser_dev)
  50. {
  51. unsigned char buf[] = { NVEC_PS2, CANCEL_AUTO_RECEIVE };
  52. nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
  53. }
  54. static int ps2_sendcommand(struct serio *ser_dev, unsigned char cmd)
  55. {
  56. unsigned char buf[] = { NVEC_PS2, SEND_COMMAND, ENABLE_MOUSE, 1 };
  57. buf[2] = cmd & 0xff;
  58. dev_dbg(&ser_dev->dev, "Sending ps2 cmd %02x\n", cmd);
  59. return nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
  60. }
  61. static int nvec_ps2_notifier(struct notifier_block *nb,
  62. unsigned long event_type, void *data)
  63. {
  64. int i;
  65. unsigned char *msg = (unsigned char *)data;
  66. switch (event_type) {
  67. case NVEC_PS2_EVT:
  68. for (i = 0; i < msg[1]; i++)
  69. serio_interrupt(ps2_dev.ser_dev, msg[2 + i], 0);
  70. NVEC_PHD("ps/2 mouse event: ", &msg[2], msg[1]);
  71. return NOTIFY_STOP;
  72. case NVEC_PS2:
  73. if (msg[2] == 1) {
  74. for (i = 0; i < (msg[1] - 2); i++)
  75. serio_interrupt(ps2_dev.ser_dev, msg[i + 4], 0);
  76. NVEC_PHD("ps/2 mouse reply: ", &msg[4], msg[1] - 2);
  77. }
  78. else if (msg[1] != 2) /* !ack */
  79. NVEC_PHD("unhandled mouse event: ", msg, msg[1] + 2);
  80. return NOTIFY_STOP;
  81. }
  82. return NOTIFY_DONE;
  83. }
  84. static int nvec_mouse_probe(struct platform_device *pdev)
  85. {
  86. struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
  87. struct serio *ser_dev;
  88. ser_dev = kzalloc(sizeof(struct serio), GFP_KERNEL);
  89. if (!ser_dev)
  90. return -ENOMEM;
  91. ser_dev->id.type = SERIO_8042;
  92. ser_dev->write = ps2_sendcommand;
  93. ser_dev->start = ps2_startstreaming;
  94. ser_dev->stop = ps2_stopstreaming;
  95. strlcpy(ser_dev->name, "nvec mouse", sizeof(ser_dev->name));
  96. strlcpy(ser_dev->phys, "nvec", sizeof(ser_dev->phys));
  97. ps2_dev.ser_dev = ser_dev;
  98. ps2_dev.notifier.notifier_call = nvec_ps2_notifier;
  99. ps2_dev.nvec = nvec;
  100. nvec_register_notifier(nvec, &ps2_dev.notifier, 0);
  101. serio_register_port(ser_dev);
  102. return 0;
  103. }
  104. static int nvec_mouse_remove(struct platform_device *pdev)
  105. {
  106. struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
  107. ps2_sendcommand(ps2_dev.ser_dev, DISABLE_MOUSE);
  108. ps2_stopstreaming(ps2_dev.ser_dev);
  109. nvec_unregister_notifier(nvec, &ps2_dev.notifier);
  110. serio_unregister_port(ps2_dev.ser_dev);
  111. return 0;
  112. }
  113. #ifdef CONFIG_PM_SLEEP
  114. static int nvec_mouse_suspend(struct device *dev)
  115. {
  116. /* disable mouse */
  117. ps2_sendcommand(ps2_dev.ser_dev, DISABLE_MOUSE);
  118. /* send cancel autoreceive */
  119. ps2_stopstreaming(ps2_dev.ser_dev);
  120. return 0;
  121. }
  122. static int nvec_mouse_resume(struct device *dev)
  123. {
  124. /* start streaming */
  125. ps2_startstreaming(ps2_dev.ser_dev);
  126. /* enable mouse */
  127. ps2_sendcommand(ps2_dev.ser_dev, ENABLE_MOUSE);
  128. return 0;
  129. }
  130. #endif
  131. static SIMPLE_DEV_PM_OPS(nvec_mouse_pm_ops, nvec_mouse_suspend,
  132. nvec_mouse_resume);
  133. static struct platform_driver nvec_mouse_driver = {
  134. .probe = nvec_mouse_probe,
  135. .remove = nvec_mouse_remove,
  136. .driver = {
  137. .name = "nvec-mouse",
  138. .pm = &nvec_mouse_pm_ops,
  139. },
  140. };
  141. module_platform_driver(nvec_mouse_driver);
  142. MODULE_DESCRIPTION("NVEC mouse driver");
  143. MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
  144. MODULE_ALIAS("platform:nvec-mouse");
  145. MODULE_LICENSE("GPL");