regmap: rbtree: Fixed node range check on sync
[firefly-linux-kernel-4.4.55.git] / drivers / staging / nvec / nvec_ps2.c
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
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/serio.h>
19 #include <linux/delay.h>
20 #include <linux/platform_device.h>
21
22 #include "nvec.h"
23
24 #define PACKET_SIZE     6
25
26 #define ENABLE_MOUSE    0xf4
27 #define DISABLE_MOUSE   0xf5
28 #define PSMOUSE_RST     0xff
29
30 #ifdef NVEC_PS2_DEBUG
31 #define NVEC_PHD(str, buf, len) \
32         print_hex_dump(KERN_DEBUG, str, DUMP_PREFIX_NONE, \
33                         16, 1, buf, len, false)
34 #else
35 #define NVEC_PHD(str, buf, len)
36 #endif
37
38 enum ps2_subcmds {
39         SEND_COMMAND = 1,
40         RECEIVE_N,
41         AUTO_RECEIVE_N,
42         CANCEL_AUTO_RECEIVE,
43 };
44
45 struct nvec_ps2 {
46         struct serio *ser_dev;
47         struct notifier_block notifier;
48         struct nvec_chip *nvec;
49 };
50
51 static struct nvec_ps2 ps2_dev;
52
53 static int ps2_startstreaming(struct serio *ser_dev)
54 {
55         unsigned char buf[] = { NVEC_PS2, AUTO_RECEIVE_N, PACKET_SIZE };
56         return nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
57 }
58
59 static void ps2_stopstreaming(struct serio *ser_dev)
60 {
61         unsigned char buf[] = { NVEC_PS2, CANCEL_AUTO_RECEIVE };
62         nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
63 }
64
65 static int ps2_sendcommand(struct serio *ser_dev, unsigned char cmd)
66 {
67         unsigned char buf[] = { NVEC_PS2, SEND_COMMAND, ENABLE_MOUSE, 1 };
68
69         buf[2] = cmd & 0xff;
70
71         dev_dbg(&ser_dev->dev, "Sending ps2 cmd %02x\n", cmd);
72         return nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
73 }
74
75 static int nvec_ps2_notifier(struct notifier_block *nb,
76                              unsigned long event_type, void *data)
77 {
78         int i;
79         unsigned char *msg = (unsigned char *)data;
80
81         switch (event_type) {
82         case NVEC_PS2_EVT:
83                 for (i = 0; i < msg[1]; i++)
84                         serio_interrupt(ps2_dev.ser_dev, msg[2 + i], 0);
85                 NVEC_PHD("ps/2 mouse event: ", &msg[2], msg[1]);
86                 return NOTIFY_STOP;
87
88         case NVEC_PS2:
89                 if (msg[2] == 1) {
90                         for (i = 0; i < (msg[1] - 2); i++)
91                                 serio_interrupt(ps2_dev.ser_dev, msg[i + 4], 0);
92                         NVEC_PHD("ps/2 mouse reply: ", &msg[4], msg[1] - 2);
93                 }
94
95                 else if (msg[1] != 2) /* !ack */
96                         NVEC_PHD("unhandled mouse event: ", msg, msg[1] + 2);
97                 return NOTIFY_STOP;
98         }
99
100         return NOTIFY_DONE;
101 }
102
103 static int nvec_mouse_probe(struct platform_device *pdev)
104 {
105         struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
106         struct serio *ser_dev;
107         char mouse_reset[] = { NVEC_PS2, SEND_COMMAND, PSMOUSE_RST, 3 };
108
109         ser_dev = devm_kzalloc(&pdev->dev, sizeof(struct serio), GFP_KERNEL);
110         if (ser_dev == NULL)
111                 return -ENOMEM;
112
113         ser_dev->id.type = SERIO_PS_PSTHRU;
114         ser_dev->write = ps2_sendcommand;
115         ser_dev->start = ps2_startstreaming;
116         ser_dev->stop = ps2_stopstreaming;
117
118         strlcpy(ser_dev->name, "nvec mouse", sizeof(ser_dev->name));
119         strlcpy(ser_dev->phys, "nvec", sizeof(ser_dev->phys));
120
121         ps2_dev.ser_dev = ser_dev;
122         ps2_dev.notifier.notifier_call = nvec_ps2_notifier;
123         ps2_dev.nvec = nvec;
124         nvec_register_notifier(nvec, &ps2_dev.notifier, 0);
125
126         serio_register_port(ser_dev);
127
128         /* mouse reset */
129         nvec_write_async(nvec, mouse_reset, sizeof(mouse_reset));
130
131         return 0;
132 }
133
134 static int nvec_mouse_remove(struct platform_device *pdev)
135 {
136         serio_unregister_port(ps2_dev.ser_dev);
137
138         return 0;
139 }
140
141 #ifdef CONFIG_PM_SLEEP
142 static int nvec_mouse_suspend(struct device *dev)
143 {
144         /* disable mouse */
145         ps2_sendcommand(ps2_dev.ser_dev, DISABLE_MOUSE);
146
147         /* send cancel autoreceive */
148         ps2_stopstreaming(ps2_dev.ser_dev);
149
150         return 0;
151 }
152
153 static int nvec_mouse_resume(struct device *dev)
154 {
155         /* start streaming */
156         ps2_startstreaming(ps2_dev.ser_dev);
157
158         /* enable mouse */
159         ps2_sendcommand(ps2_dev.ser_dev, ENABLE_MOUSE);
160
161         return 0;
162 }
163 #endif
164
165 static const SIMPLE_DEV_PM_OPS(nvec_mouse_pm_ops, nvec_mouse_suspend,
166                                 nvec_mouse_resume);
167
168 static struct platform_driver nvec_mouse_driver = {
169         .probe  = nvec_mouse_probe,
170         .remove = nvec_mouse_remove,
171         .driver = {
172                 .name = "nvec-mouse",
173                 .owner = THIS_MODULE,
174                 .pm = &nvec_mouse_pm_ops,
175         },
176 };
177
178 module_platform_driver(nvec_mouse_driver);
179
180 MODULE_DESCRIPTION("NVEC mouse driver");
181 MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
182 MODULE_LICENSE("GPL");