Linux 6.13-rc4
[linux.git] / drivers / staging / nvec / nvec_ps2.c
blob575233fa1677eaab75b28448f3a15f9839527b12
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * nvec_ps2: mouse driver for a NVIDIA compliant embedded controller
5 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net>
7 * Authors: Pierre-Hugues Husson <phhusson@free.fr>
8 * Ilya Petrov <ilya.muromec@gmail.com>
9 * Marc Dietrich <marvin24@gmx.de>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/serio.h>
15 #include <linux/delay.h>
16 #include <linux/platform_device.h>
18 #include "nvec.h"
20 #define PACKET_SIZE 6
22 #define ENABLE_MOUSE 0xf4
23 #define DISABLE_MOUSE 0xf5
24 #define PSMOUSE_RST 0xff
26 #ifdef NVEC_PS2_DEBUG
27 #define NVEC_PHD(str, buf, len) \
28 print_hex_dump(KERN_DEBUG, str, DUMP_PREFIX_NONE, \
29 16, 1, buf, len, false)
30 #else
31 #define NVEC_PHD(str, buf, len) do { } while (0)
32 #endif
34 enum ps2_subcmds {
35 SEND_COMMAND = 1,
36 RECEIVE_N,
37 AUTO_RECEIVE_N,
38 CANCEL_AUTO_RECEIVE,
41 struct nvec_ps2 {
42 struct serio *ser_dev;
43 struct notifier_block notifier;
44 struct nvec_chip *nvec;
47 static struct nvec_ps2 ps2_dev;
49 static int ps2_startstreaming(struct serio *ser_dev)
51 unsigned char buf[] = { NVEC_PS2, AUTO_RECEIVE_N, PACKET_SIZE };
53 return nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
56 static void ps2_stopstreaming(struct serio *ser_dev)
58 unsigned char buf[] = { NVEC_PS2, CANCEL_AUTO_RECEIVE };
60 nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
63 static int nvec_ps2_notifier(struct notifier_block *nb,
64 unsigned long event_type, void *data)
66 int i;
67 unsigned char *msg = data;
69 switch (event_type) {
70 case NVEC_PS2_EVT:
71 for (i = 0; i < msg[1]; i++)
72 serio_interrupt(ps2_dev.ser_dev, msg[2 + i], 0);
73 NVEC_PHD("ps/2 mouse event: ", &msg[2], msg[1]);
74 return NOTIFY_STOP;
76 case NVEC_PS2:
77 if (msg[2] == 1) {
78 for (i = 0; i < (msg[1] - 2); i++)
79 serio_interrupt(ps2_dev.ser_dev, msg[i + 4], 0);
80 NVEC_PHD("ps/2 mouse reply: ", &msg[4], msg[1] - 2);
83 else if (msg[1] != 2) /* !ack */
84 NVEC_PHD("unhandled mouse event: ", msg, msg[1] + 2);
85 return NOTIFY_STOP;
88 return NOTIFY_DONE;
91 static int ps2_sendcommand(struct serio *ser_dev, unsigned char cmd)
93 unsigned char buf[] = { NVEC_PS2, SEND_COMMAND, ENABLE_MOUSE, 1 };
94 struct nvec_msg *msg;
95 int ret;
97 buf[2] = cmd & 0xff;
99 dev_dbg(&ser_dev->dev, "Sending ps2 cmd %02x\n", cmd);
101 ret = nvec_write_sync(ps2_dev.nvec, buf, sizeof(buf), &msg);
102 if (ret < 0)
103 return ret;
105 nvec_ps2_notifier(NULL, NVEC_PS2, msg->data);
107 nvec_msg_free(ps2_dev.nvec, msg);
109 return 0;
112 static int nvec_mouse_probe(struct platform_device *pdev)
114 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
115 struct serio *ser_dev;
117 ser_dev = kzalloc(sizeof(*ser_dev), GFP_KERNEL);
118 if (!ser_dev)
119 return -ENOMEM;
121 ser_dev->id.type = SERIO_8042;
122 ser_dev->write = ps2_sendcommand;
123 ser_dev->start = ps2_startstreaming;
124 ser_dev->stop = ps2_stopstreaming;
126 strscpy(ser_dev->name, "nvec mouse", sizeof(ser_dev->name));
127 strscpy(ser_dev->phys, "nvec", sizeof(ser_dev->phys));
129 ps2_dev.ser_dev = ser_dev;
130 ps2_dev.notifier.notifier_call = nvec_ps2_notifier;
131 ps2_dev.nvec = nvec;
132 nvec_register_notifier(nvec, &ps2_dev.notifier, 0);
134 serio_register_port(ser_dev);
136 return 0;
139 static void nvec_mouse_remove(struct platform_device *pdev)
141 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
143 ps2_sendcommand(ps2_dev.ser_dev, DISABLE_MOUSE);
144 ps2_stopstreaming(ps2_dev.ser_dev);
145 nvec_unregister_notifier(nvec, &ps2_dev.notifier);
146 serio_unregister_port(ps2_dev.ser_dev);
149 #ifdef CONFIG_PM_SLEEP
150 static int nvec_mouse_suspend(struct device *dev)
152 /* disable mouse */
153 ps2_sendcommand(ps2_dev.ser_dev, DISABLE_MOUSE);
155 /* send cancel autoreceive */
156 ps2_stopstreaming(ps2_dev.ser_dev);
158 return 0;
161 static int nvec_mouse_resume(struct device *dev)
163 /* start streaming */
164 ps2_startstreaming(ps2_dev.ser_dev);
166 /* enable mouse */
167 ps2_sendcommand(ps2_dev.ser_dev, ENABLE_MOUSE);
169 return 0;
171 #endif
173 static SIMPLE_DEV_PM_OPS(nvec_mouse_pm_ops, nvec_mouse_suspend,
174 nvec_mouse_resume);
176 static struct platform_driver nvec_mouse_driver = {
177 .probe = nvec_mouse_probe,
178 .remove = nvec_mouse_remove,
179 .driver = {
180 .name = "nvec-mouse",
181 .pm = &nvec_mouse_pm_ops,
185 module_platform_driver(nvec_mouse_driver);
187 MODULE_DESCRIPTION("NVEC mouse driver");
188 MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
189 MODULE_ALIAS("platform:nvec-mouse");
190 MODULE_LICENSE("GPL");