Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / staging / nvec / nvec_ps2.c
blob06dbb02085a936c52ee97bec1787da0ec92179d4
1 /*
2 * nvec_ps2: mouse driver for a NVIDIA compliant embedded controller
4 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net>
6 * Authors: Pierre-Hugues Husson <phhusson@free.fr>
7 * Ilya Petrov <ilya.muromec@gmail.com>
8 * Marc Dietrich <marvin24@gmx.de>
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.
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>
22 #include "nvec.h"
24 #define PACKET_SIZE 6
26 #define ENABLE_MOUSE 0xf4
27 #define DISABLE_MOUSE 0xf5
28 #define PSMOUSE_RST 0xff
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
38 enum ps2_subcmds {
39 SEND_COMMAND = 1,
40 RECEIVE_N,
41 AUTO_RECEIVE_N,
42 CANCEL_AUTO_RECEIVE,
45 struct nvec_ps2 {
46 struct serio *ser_dev;
47 struct notifier_block notifier;
48 struct nvec_chip *nvec;
51 static struct nvec_ps2 ps2_dev;
53 static int ps2_startstreaming(struct serio *ser_dev)
55 unsigned char buf[] = { NVEC_PS2, AUTO_RECEIVE_N, PACKET_SIZE };
56 return nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
59 static void ps2_stopstreaming(struct serio *ser_dev)
61 unsigned char buf[] = { NVEC_PS2, CANCEL_AUTO_RECEIVE };
62 nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
65 static int ps2_sendcommand(struct serio *ser_dev, unsigned char cmd)
67 unsigned char buf[] = { NVEC_PS2, SEND_COMMAND, ENABLE_MOUSE, 1 };
69 buf[2] = cmd & 0xff;
71 dev_dbg(&ser_dev->dev, "Sending ps2 cmd %02x\n", cmd);
72 return nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
75 static int nvec_ps2_notifier(struct notifier_block *nb,
76 unsigned long event_type, void *data)
78 int i;
79 unsigned char *msg = (unsigned char *)data;
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;
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);
95 else if (msg[1] != 2) /* !ack */
96 NVEC_PHD("unhandled mouse event: ", msg, msg[1] + 2);
97 return NOTIFY_STOP;
100 return NOTIFY_DONE;
103 static int nvec_mouse_probe(struct platform_device *pdev)
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 };
109 ser_dev = kzalloc(sizeof(struct serio), GFP_KERNEL);
110 if (ser_dev == NULL)
111 return -ENOMEM;
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;
118 strlcpy(ser_dev->name, "nvec mouse", sizeof(ser_dev->name));
119 strlcpy(ser_dev->phys, "nvec", sizeof(ser_dev->phys));
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);
126 serio_register_port(ser_dev);
128 /* mouse reset */
129 nvec_write_async(nvec, mouse_reset, sizeof(mouse_reset));
131 return 0;
134 static int nvec_mouse_remove(struct platform_device *pdev)
136 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
138 ps2_sendcommand(ps2_dev.ser_dev, DISABLE_MOUSE);
139 ps2_stopstreaming(ps2_dev.ser_dev);
140 nvec_unregister_notifier(nvec, &ps2_dev.notifier);
141 serio_unregister_port(ps2_dev.ser_dev);
143 return 0;
146 #ifdef CONFIG_PM_SLEEP
147 static int nvec_mouse_suspend(struct device *dev)
149 /* disable mouse */
150 ps2_sendcommand(ps2_dev.ser_dev, DISABLE_MOUSE);
152 /* send cancel autoreceive */
153 ps2_stopstreaming(ps2_dev.ser_dev);
155 return 0;
158 static int nvec_mouse_resume(struct device *dev)
160 /* start streaming */
161 ps2_startstreaming(ps2_dev.ser_dev);
163 /* enable mouse */
164 ps2_sendcommand(ps2_dev.ser_dev, ENABLE_MOUSE);
166 return 0;
168 #endif
170 static const SIMPLE_DEV_PM_OPS(nvec_mouse_pm_ops, nvec_mouse_suspend,
171 nvec_mouse_resume);
173 static struct platform_driver nvec_mouse_driver = {
174 .probe = nvec_mouse_probe,
175 .remove = nvec_mouse_remove,
176 .driver = {
177 .name = "nvec-mouse",
178 .owner = THIS_MODULE,
179 .pm = &nvec_mouse_pm_ops,
183 module_platform_driver(nvec_mouse_driver);
185 MODULE_DESCRIPTION("NVEC mouse driver");
186 MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
187 MODULE_ALIAS("platform:nvec-mouse");
188 MODULE_LICENSE("GPL");