misc: merge branch 'release-staging-v19.0.0.0' into develop
[gem5.git] / src / dev / ps2 / touchkit.cc
1 /*
2 * Copyright (c) 2010, 2017-2018 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include "dev/ps2/touchkit.hh"
42
43 #include "base/logging.hh"
44 #include "debug/PS2.hh"
45 #include "dev/ps2/types.hh"
46 #include "params/PS2TouchKit.hh"
47
48 PS2TouchKit::PS2TouchKit(const PS2TouchKitParams *p)
49 : PS2Device(p),
50 vnc(p->vnc),
51 enabled(false), touchKitEnabled(false)
52 {
53 if (vnc)
54 vnc->setMouse(this);
55 }
56
57 void
58 PS2TouchKit::serialize(CheckpointOut &cp) const
59 {
60 PS2Device::serialize(cp);
61
62 SERIALIZE_SCALAR(enabled);
63 SERIALIZE_SCALAR(touchKitEnabled);
64 }
65
66 void
67 PS2TouchKit::unserialize(CheckpointIn &cp)
68 {
69 PS2Device::unserialize(cp);
70
71 UNSERIALIZE_SCALAR(enabled);
72 UNSERIALIZE_SCALAR(touchKitEnabled);
73 }
74
75 bool
76 PS2TouchKit::recv(const std::vector<uint8_t> &data)
77 {
78 switch (data[0]) {
79 case Ps2::Reset:
80 DPRINTF(PS2, "Resetting device.\n");
81 enabled = false;
82 touchKitEnabled = false;
83 sendAck();
84 send(Ps2::SelfTestPass);
85 return true;
86
87 case Ps2::ReadID:
88 sendAck();
89 send(Ps2::Mouse::ID);
90 return true;
91
92 case Ps2::Disable:
93 DPRINTF(PS2, "Disabling device.\n");
94 enabled = false;
95 sendAck();
96 return true;
97
98 case Ps2::Enable:
99 DPRINTF(PS2, "Enabling device.\n");
100 enabled = true;
101 sendAck();
102 return true;
103
104 case Ps2::DefaultsAndDisable:
105 DPRINTF(PS2, "Setting defaults and disabling device.\n");
106 enabled = false;
107 sendAck();
108 return true;
109
110 case Ps2::Mouse::Scale1to1:
111 case Ps2::Mouse::Scale2to1:
112 sendAck();
113 return true;
114
115 case Ps2::Mouse::SetResolution:
116 case Ps2::Mouse::SampleRate:
117 sendAck();
118 return data.size() == 2;
119
120 case Ps2::Mouse::GetStatus:
121 sendAck();
122 send(0);
123 send(2); // default resolution
124 send(100); // default sample rate
125 return true;
126
127 case TpReadId:
128 // We're not a trackpoint device, this should make the probe
129 // go away
130 sendAck();
131 send(0);
132 send(0);
133 sendAck();
134 return true;
135
136 case TouchKitDiag:
137 return recvTouchKit(data);
138
139 default:
140 panic("Unknown byte received: %#x\n", data[0]);
141 }
142 }
143
144 bool
145 PS2TouchKit::recvTouchKit(const std::vector<uint8_t> &data)
146 {
147 // Ack all incoming bytes
148 sendAck();
149
150 // Packet format is: 0x0A SIZE CMD DATA
151 assert(data[0] == TouchKitDiag);
152 if (data.size() < 3 || data.size() - 2 < data[1])
153 return false;
154
155 const uint8_t len = data[1];
156 const uint8_t cmd = data[2];
157
158 // We have received at least one TouchKit diagnostic
159 // command. Enabled TouchKit reports.
160 touchKitEnabled = true;
161
162
163 switch (cmd) {
164 case TouchKitActive:
165 warn_if(len != 1, "Unexpected activate packet length: %u\n", len);
166 sendTouchKit('A');
167 return true;
168
169 default:
170 panic("Unimplemented touchscreen command: %#x\n", cmd);
171 }
172 }
173
174 void
175 PS2TouchKit::sendTouchKit(const uint8_t *data, size_t size)
176 {
177 send(TouchKitDiag);
178 send(size);
179 for (int i = 0; i < size; ++i)
180 send(data[i]);
181 }
182
183
184 void
185 PS2TouchKit::mouseAt(uint16_t x, uint16_t y, uint8_t buttons)
186 {
187 // If the driver hasn't initialized the device yet, no need to try and send
188 // it anything. Similarly we can get vnc mouse events orders of magnitude
189 // faster than m5 can process them. Only queue up two sets mouse movements
190 // and don't add more until those are processed.
191 if (!enabled || !touchKitEnabled || sendPending() > 10)
192 return;
193
194 // Convert screen coordinates to touchpad coordinates
195 const uint16_t _x = (2047.0 / vnc->videoWidth()) * x;
196 const uint16_t _y = (2047.0 / vnc->videoHeight()) * y;
197
198 const uint8_t resp[] = {
199 buttons,
200 (uint8_t)(_x >> 7), (uint8_t)(_x & 0x7f),
201 (uint8_t)(_y >> 7), (uint8_t)(_y & 0x7f),
202 };
203
204 send(resp, sizeof(resp));
205 }
206
207 PS2TouchKit *
208 PS2TouchKitParams::create()
209 {
210 return new PS2TouchKit(this);
211 }