nvec_ps2.c 4.1 KB

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