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 
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>
17 
18 #include "nvec.h"
19 
20 #define PACKET_SIZE	6
21 
22 #define ENABLE_MOUSE	0xf4
23 #define DISABLE_MOUSE	0xf5
24 #define PSMOUSE_RST	0xff
25 
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
33 
34 enum ps2_subcmds {
35 	SEND_COMMAND = 1,
36 	RECEIVE_N,
37 	AUTO_RECEIVE_N,
38 	CANCEL_AUTO_RECEIVE,
39 };
40 
41 struct nvec_ps2 {
42 	struct serio *ser_dev;
43 	struct notifier_block notifier;
44 	struct nvec_chip *nvec;
45 };
46 
47 static struct nvec_ps2 ps2_dev;
48 
ps2_startstreaming(struct serio * ser_dev)49 static int ps2_startstreaming(struct serio *ser_dev)
50 {
51 	unsigned char buf[] = { NVEC_PS2, AUTO_RECEIVE_N, PACKET_SIZE };
52 
53 	return nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
54 }
55 
ps2_stopstreaming(struct serio * ser_dev)56 static void ps2_stopstreaming(struct serio *ser_dev)
57 {
58 	unsigned char buf[] = { NVEC_PS2, CANCEL_AUTO_RECEIVE };
59 
60 	nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
61 }
62 
nvec_ps2_notifier(struct notifier_block * nb,unsigned long event_type,void * data)63 static int nvec_ps2_notifier(struct notifier_block *nb,
64 			     unsigned long event_type, void *data)
65 {
66 	int i;
67 	unsigned char *msg = data;
68 
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;
75 
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);
81 		}
82 
83 		else if (msg[1] != 2) /* !ack */
84 			NVEC_PHD("unhandled mouse event: ", msg, msg[1] + 2);
85 		return NOTIFY_STOP;
86 	}
87 
88 	return NOTIFY_DONE;
89 }
90 
ps2_sendcommand(struct serio * ser_dev,unsigned char cmd)91 static int ps2_sendcommand(struct serio *ser_dev, unsigned char cmd)
92 {
93 	unsigned char buf[] = { NVEC_PS2, SEND_COMMAND, ENABLE_MOUSE, 1 };
94 	struct nvec_msg *msg;
95 	int ret;
96 
97 	buf[2] = cmd & 0xff;
98 
99 	dev_dbg(&ser_dev->dev, "Sending ps2 cmd %02x\n", cmd);
100 
101 	ret = nvec_write_sync(ps2_dev.nvec, buf, sizeof(buf), &msg);
102 	if (ret < 0)
103 		return ret;
104 
105 	nvec_ps2_notifier(NULL, NVEC_PS2, msg->data);
106 
107 	nvec_msg_free(ps2_dev.nvec, msg);
108 
109 	return 0;
110 }
111 
nvec_mouse_probe(struct platform_device * pdev)112 static int nvec_mouse_probe(struct platform_device *pdev)
113 {
114 	struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
115 	struct serio *ser_dev;
116 
117 	ser_dev = kzalloc(sizeof(*ser_dev), GFP_KERNEL);
118 	if (!ser_dev)
119 		return -ENOMEM;
120 
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;
125 
126 	strscpy(ser_dev->name, "nvec mouse", sizeof(ser_dev->name));
127 	strscpy(ser_dev->phys, "nvec", sizeof(ser_dev->phys));
128 
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);
133 
134 	serio_register_port(ser_dev);
135 
136 	return 0;
137 }
138 
nvec_mouse_remove(struct platform_device * pdev)139 static void nvec_mouse_remove(struct platform_device *pdev)
140 {
141 	struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
142 
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);
147 }
148 
149 #ifdef CONFIG_PM_SLEEP
nvec_mouse_suspend(struct device * dev)150 static int nvec_mouse_suspend(struct device *dev)
151 {
152 	/* disable mouse */
153 	ps2_sendcommand(ps2_dev.ser_dev, DISABLE_MOUSE);
154 
155 	/* send cancel autoreceive */
156 	ps2_stopstreaming(ps2_dev.ser_dev);
157 
158 	return 0;
159 }
160 
nvec_mouse_resume(struct device * dev)161 static int nvec_mouse_resume(struct device *dev)
162 {
163 	/* start streaming */
164 	ps2_startstreaming(ps2_dev.ser_dev);
165 
166 	/* enable mouse */
167 	ps2_sendcommand(ps2_dev.ser_dev, ENABLE_MOUSE);
168 
169 	return 0;
170 }
171 #endif
172 
173 static SIMPLE_DEV_PM_OPS(nvec_mouse_pm_ops, nvec_mouse_suspend,
174 			 nvec_mouse_resume);
175 
176 static struct platform_driver nvec_mouse_driver = {
177 	.probe  = nvec_mouse_probe,
178 	.remove_new = nvec_mouse_remove,
179 	.driver = {
180 		.name = "nvec-mouse",
181 		.pm = &nvec_mouse_pm_ops,
182 	},
183 };
184 
185 module_platform_driver(nvec_mouse_driver);
186 
187 MODULE_DESCRIPTION("NVEC mouse driver");
188 MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
189 MODULE_ALIAS("platform:nvec-mouse");
190 MODULE_LICENSE("GPL");
191