1/* 2 * Copyright (c) 2008 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Gabe Black 29 */ 30 31#include "dev/x86/i8042.hh" 32 33#include "base/bitunion.hh" 34#include "debug/I8042.hh" 35#include "mem/packet.hh" 36#include "mem/packet_access.hh" 37 38/** 39 * Note: For details on the implementation see 40 * https://wiki.osdev.org/%228042%22_PS/2_Controller 41 */ 42 43// The 8042 has a whopping 32 bytes of internal RAM. 44const uint8_t RamSize = 32; 45const uint8_t NumOutputBits = 14;
|
46const uint8_t X86ISA::PS2Keyboard::ID[] = {0xab, 0x83};
47const uint8_t X86ISA::PS2Mouse::ID[] = {0x00};
48const uint8_t CommandAck = 0xfa;
49const uint8_t CommandNack = 0xfe;
50const uint8_t BatSuccessful = 0xaa;
|
46 47 48X86ISA::I8042::I8042(Params *p) 49 : BasicPioDevice(p, 0), // pioSize arg is dummy value... not used 50 latency(p->pio_latency), 51 dataPort(p->data_port), commandPort(p->command_port), 52 statusReg(0), commandByte(0), dataReg(0), lastCommand(NoCommand),
|
58 mouseIntPin(p->mouse_int_pin), keyboardIntPin(p->keyboard_int_pin)
|
53 mouseIntPin(p->mouse_int_pin), keyboardIntPin(p->keyboard_int_pin), 54 mouse(p->mouse), keyboard(p->keyboard) |
55{
|
56 fatal_if(!mouse, "The i8042 model requires a mouse instance"); 57 fatal_if(!keyboard, "The i8042 model requires a keyboard instance"); 58 |
59 statusReg.passedSelfTest = 1; 60 statusReg.commandLast = 1; 61 statusReg.keyboardUnlocked = 1; 62 63 commandByte.convertScanCodes = 1; 64 commandByte.passedSelfTest = 1; 65 commandByte.keyboardFullInt = 1; 66} 67 68 69AddrRangeList 70X86ISA::I8042::getAddrRanges() const 71{ 72 AddrRangeList ranges; 73 // TODO: Are these really supposed to be a single byte and not 4? 74 ranges.push_back(RangeSize(dataPort, 1)); 75 ranges.push_back(RangeSize(commandPort, 1)); 76 return ranges; 77} 78 79void 80X86ISA::I8042::writeData(uint8_t newData, bool mouse) 81{ 82 DPRINTF(I8042, "Set data %#02x.\n", newData); 83 dataReg = newData; 84 statusReg.outputFull = 1; 85 statusReg.mouseOutputFull = (mouse ? 1 : 0); 86 if (!mouse && commandByte.keyboardFullInt) { 87 DPRINTF(I8042, "Sending keyboard interrupt.\n"); 88 keyboardIntPin->raise(); 89 //This is a hack 90 keyboardIntPin->lower(); 91 } else if (mouse && commandByte.mouseFullInt) { 92 DPRINTF(I8042, "Sending mouse interrupt.\n"); 93 mouseIntPin->raise(); 94 //This is a hack 95 mouseIntPin->lower(); 96 } 97} 98
|
100void
101X86ISA::PS2Device::serialize(const std::string &base, CheckpointOut &cp) const
102{
103 paramOut(cp, base + ".lastCommand", lastCommand);
104
105 std::vector<uint8_t> buffer(outBuffer.size());
106 std::copy(outBuffer.begin(), outBuffer.end(), buffer.begin());
107 arrayParamOut(cp, base + ".outBuffer.elts", buffer);
108}
109
110void
111X86ISA::PS2Device::unserialize(const std::string &base, CheckpointIn &cp)
112{
113 paramIn(cp, base + ".lastCommand", lastCommand);
114
115 std::vector<uint8_t> buffer;
116 arrayParamIn(cp, base + ".outBuffer.elts", buffer);
117 assert(outBuffer.empty());
118 for (auto c : buffer)
119 outBuffer.push_back(c);
120}
121
122
123void
124X86ISA::PS2Device::ack()
125{
126 bufferData(&CommandAck, sizeof(CommandAck));
127}
128
129void
130X86ISA::PS2Device::nack()
131{
132 bufferData(&CommandNack, sizeof(CommandNack));
133}
134
135void
136X86ISA::PS2Device::bufferData(const uint8_t *data, int size)
137{
138 assert(data || size == 0);
139 while (size) {
140 outBuffer.push_back(*(data++));
141 size--;
142 }
143}
144
|
99uint8_t 100X86ISA::I8042::readDataOut() 101{ 102 uint8_t data = dataReg; 103 statusReg.outputFull = 0; 104 statusReg.mouseOutputFull = 0;
|
151 if (keyboard.hasData()) {
152 writeData(keyboard.getData(), false);
153 } else if (mouse.hasData()) {
154 writeData(mouse.getData(), true);
|
105 if (keyboard->hostDataAvailable()) { 106 writeData(keyboard->hostRead(), false); 107 } else if (mouse->hostDataAvailable()) { 108 writeData(mouse->hostRead(), true); |
109 } 110 return data; 111} 112
|
159bool
160X86ISA::PS2Keyboard::processData(uint8_t data)
161{
162 if (lastCommand != NoCommand) {
163 switch (lastCommand) {
164 case LEDWrite:
165 DPRINTF(I8042, "Setting LEDs: "
166 "caps lock %s, num lock %s, scroll lock %s\n",
167 bits(data, 2) ? "on" : "off",
168 bits(data, 1) ? "on" : "off",
169 bits(data, 0) ? "on" : "off");
170 ack();
171 lastCommand = NoCommand;
172 break;
173 case TypematicInfo:
174 DPRINTF(I8042, "Setting typematic info to %#02x.\n", data);
175 ack();
176 lastCommand = NoCommand;
177 break;
178 }
179 return hasData();
180 }
181 switch (data) {
182 case LEDWrite:
183 DPRINTF(I8042, "Got LED write command.\n");
184 ack();
185 lastCommand = LEDWrite;
186 break;
187 case DiagnosticEcho:
188 panic("Keyboard diagnostic echo unimplemented.\n");
189 case AlternateScanCodes:
190 panic("Accessing alternate scan codes unimplemented.\n");
191 case ReadID:
192 DPRINTF(I8042, "Got keyboard read ID command.\n");
193 ack();
194 bufferData((uint8_t *)&ID, sizeof(ID));
195 break;
196 case TypematicInfo:
197 DPRINTF(I8042, "Setting typematic info.\n");
198 ack();
199 lastCommand = TypematicInfo;
200 break;
201 case Enable:
202 DPRINTF(I8042, "Enabling the keyboard.\n");
203 ack();
204 break;
205 case Disable:
206 DPRINTF(I8042, "Disabling the keyboard.\n");
207 ack();
208 break;
209 case DefaultsAndDisable:
210 DPRINTF(I8042, "Disabling and resetting the keyboard.\n");
211 ack();
212 break;
213 case AllKeysToTypematic:
214 panic("Setting all keys to typemantic unimplemented.\n");
215 case AllKeysToMakeRelease:
216 panic("Setting all keys to make/release unimplemented.\n");
217 case AllKeysToMake:
218 panic("Setting all keys to make unimplemented.\n");
219 case AllKeysToTypematicMakeRelease:
220 panic("Setting all keys to "
221 "typematic/make/release unimplemented.\n");
222 case KeyToTypematic:
223 panic("Setting a key to typematic unimplemented.\n");
224 case KeyToMakeRelease:
225 panic("Setting a key to make/release unimplemented.\n");
226 case KeyToMakeOnly:
227 panic("Setting key to make only unimplemented.\n");
228 case Resend:
229 panic("Keyboard resend unimplemented.\n");
230 case Reset:
231 panic("Keyboard reset unimplemented.\n");
232 default:
233 panic("Unknown keyboard command %#02x.\n", data);
234 }
235 return hasData();
236}
237
238bool
239X86ISA::PS2Mouse::processData(uint8_t data)
240{
241 if (lastCommand != NoCommand) {
242 switch(lastCommand) {
243 case SetResolution:
244 DPRINTF(I8042, "Mouse resolution set to %d.\n", data);
245 resolution = data;
246 ack();
247 lastCommand = NoCommand;
248 break;
249 case SampleRate:
250 DPRINTF(I8042, "Mouse sample rate %d samples "
251 "per second.\n", data);
252 sampleRate = data;
253 ack();
254 lastCommand = NoCommand;
255 break;
256 default:
257 panic("Not expecting data for a mouse command.\n");
258 }
259 return hasData();
260 }
261 switch (data) {
262 case Scale1to1:
263 DPRINTF(I8042, "Setting mouse scale to 1:1.\n");
264 status.twoToOne = 0;
265 ack();
266 break;
267 case Scale2to1:
268 DPRINTF(I8042, "Setting mouse scale to 2:1.\n");
269 status.twoToOne = 1;
270 ack();
271 break;
272 case SetResolution:
273 DPRINTF(I8042, "Setting mouse resolution.\n");
274 lastCommand = SetResolution;
275 ack();
276 break;
277 case GetStatus:
278 DPRINTF(I8042, "Getting mouse status.\n");
279 ack();
280 bufferData((uint8_t *)&(status), 1);
281 bufferData(&resolution, sizeof(resolution));
282 bufferData(&sampleRate, sizeof(sampleRate));
283 break;
284 case ReadData:
285 panic("Reading mouse data unimplemented.\n");
286 case ResetWrapMode:
287 panic("Resetting mouse wrap mode unimplemented.\n");
288 case WrapMode:
289 panic("Setting mouse wrap mode unimplemented.\n");
290 case RemoteMode:
291 panic("Setting mouse remote mode unimplemented.\n");
292 case ReadID:
293 DPRINTF(I8042, "Mouse ID requested.\n");
294 ack();
295 bufferData(ID, sizeof(ID));
296 break;
297 case SampleRate:
298 DPRINTF(I8042, "Setting mouse sample rate.\n");
299 lastCommand = SampleRate;
300 ack();
301 break;
302 case DisableReporting:
303 DPRINTF(I8042, "Disabling data reporting.\n");
304 status.enabled = 0;
305 ack();
306 break;
307 case EnableReporting:
308 DPRINTF(I8042, "Enabling data reporting.\n");
309 status.enabled = 1;
310 ack();
311 break;
312 case DefaultsAndDisable:
313 DPRINTF(I8042, "Disabling and resetting mouse.\n");
314 sampleRate = 100;
315 resolution = 4;
316 status.twoToOne = 0;
317 status.enabled = 0;
318 ack();
319 break;
320 case Resend:
321 panic("Mouse resend unimplemented.\n");
322 case Reset:
323 DPRINTF(I8042, "Resetting the mouse.\n");
324 sampleRate = 100;
325 resolution = 4;
326 status.twoToOne = 0;
327 status.enabled = 0;
328 ack();
329 bufferData(&BatSuccessful, sizeof(BatSuccessful));
330 bufferData(ID, sizeof(ID));
331 break;
332 default:
333 warn("Unknown mouse command %#02x.\n", data);
334 nack();
335 break;
336 }
337 return hasData();
338}
339
340
|
113Tick 114X86ISA::I8042::read(PacketPtr pkt) 115{ 116 assert(pkt->getSize() == 1); 117 Addr addr = pkt->getAddr(); 118 if (addr == dataPort) { 119 uint8_t data = readDataOut(); 120 //DPRINTF(I8042, "Read from data port got %#02x.\n", data); 121 pkt->set<uint8_t>(data); 122 } else if (addr == commandPort) { 123 //DPRINTF(I8042, "Read status as %#02x.\n", (uint8_t)statusReg); 124 pkt->set<uint8_t>((uint8_t)statusReg); 125 } else { 126 panic("Read from unrecognized port %#x.\n", addr); 127 } 128 pkt->makeAtomicResponse(); 129 return latency; 130} 131 132Tick 133X86ISA::I8042::write(PacketPtr pkt) 134{ 135 assert(pkt->getSize() == 1); 136 Addr addr = pkt->getAddr(); 137 uint8_t data = pkt->get<uint8_t>(); 138 if (addr == dataPort) { 139 statusReg.commandLast = 0; 140 switch (lastCommand) { 141 case NoCommand:
|
370 if (keyboard.processData(data)) {
371 writeData(keyboard.getData(), false);
372 }
|
142 keyboard->hostWrite(data); 143 if (keyboard->hostDataAvailable()) 144 writeData(keyboard->hostRead(), false); |
145 break; 146 case WriteToMouse:
|
375 if (mouse.processData(data)) {
376 writeData(mouse.getData(), true);
377 }
|
147 mouse->hostWrite(data); 148 if (mouse->hostDataAvailable()) 149 writeData(mouse->hostRead(), true); |
150 break; 151 case WriteCommandByte: 152 commandByte = data; 153 DPRINTF(I8042, "Got data %#02x for \"Write " 154 "command byte\" command.\n", data); 155 statusReg.passedSelfTest = (uint8_t)commandByte.passedSelfTest; 156 break; 157 case WriteMouseOutputBuff: 158 DPRINTF(I8042, "Got data %#02x for \"Write " 159 "mouse output buffer\" command.\n", data); 160 writeData(data, true); 161 break; 162 case WriteKeyboardOutputBuff: 163 DPRINTF(I8042, "Got data %#02x for \"Write " 164 "keyboad output buffer\" command.\n", data); 165 writeData(data, false); 166 break; 167 case WriteOutputPort: 168 DPRINTF(I8042, "Got data %#02x for \"Write " 169 "output port\" command.\n", data); 170 panic_if(bits(data, 0) != 1, "Reset bit should be 1"); 171 // Safe to ignore otherwise 172 break; 173 default: 174 panic("Data written for unrecognized " 175 "command %#02x\n", lastCommand); 176 } 177 lastCommand = NoCommand; 178 } else if (addr == commandPort) { 179 DPRINTF(I8042, "Got command %#02x.\n", data); 180 statusReg.commandLast = 1; 181 // These purposefully leave off the first byte of the controller RAM 182 // so it can be handled specially. 183 if (data > ReadControllerRamBase && 184 data < ReadControllerRamBase + RamSize) { 185 panic("Attempted to use i8042 read controller RAM command to " 186 "get byte %d.\n", data - ReadControllerRamBase); 187 } else if (data > WriteControllerRamBase && 188 data < WriteControllerRamBase + RamSize) { 189 panic("Attempted to use i8042 read controller RAM command to " 190 "get byte %d.\n", data - ReadControllerRamBase); 191 } else if (data >= PulseOutputBitBase && 192 data < PulseOutputBitBase + NumOutputBits) { 193 panic("Attempted to use i8042 pulse output bit command to " 194 "to pulse bit %d.\n", data - PulseOutputBitBase); 195 } 196 switch (data) { 197 case GetCommandByte: 198 DPRINTF(I8042, "Getting command byte.\n"); 199 writeData(commandByte); 200 break; 201 case WriteCommandByte: 202 DPRINTF(I8042, "Setting command byte.\n"); 203 lastCommand = WriteCommandByte; 204 break; 205 case CheckForPassword: 206 panic("i8042 \"Check for password\" command not implemented.\n"); 207 case LoadPassword: 208 panic("i8042 \"Load password\" command not implemented.\n"); 209 case CheckPassword: 210 panic("i8042 \"Check password\" command not implemented.\n"); 211 case DisableMouse: 212 DPRINTF(I8042, "Disabling mouse at controller.\n"); 213 commandByte.disableMouse = 1; 214 break; 215 case EnableMouse: 216 DPRINTF(I8042, "Enabling mouse at controller.\n"); 217 commandByte.disableMouse = 0; 218 break; 219 case TestMouse: 220 panic("i8042 \"Test mouse\" command not implemented.\n"); 221 case SelfTest: 222 panic("i8042 \"Self test\" command not implemented.\n"); 223 case InterfaceTest: 224 panic("i8042 \"Interface test\" command not implemented.\n"); 225 case DiagnosticDump: 226 panic("i8042 \"Diagnostic dump\" command not implemented.\n"); 227 case DisableKeyboard: 228 DPRINTF(I8042, "Disabling keyboard at controller.\n"); 229 commandByte.disableKeyboard = 1; 230 break; 231 case EnableKeyboard: 232 DPRINTF(I8042, "Enabling keyboard at controller.\n"); 233 commandByte.disableKeyboard = 0; 234 break; 235 case ReadInputPort: 236 panic("i8042 \"Read input port\" command not implemented.\n"); 237 case ContinuousPollLow: 238 panic("i8042 \"Continuous poll low\" command not implemented.\n"); 239 case ContinuousPollHigh: 240 panic("i8042 \"Continuous poll high\" command not implemented.\n"); 241 case ReadOutputPort: 242 panic("i8042 \"Read output port\" command not implemented.\n"); 243 case WriteOutputPort: 244 lastCommand = WriteOutputPort; 245 break; 246 case WriteKeyboardOutputBuff: 247 lastCommand = WriteKeyboardOutputBuff; 248 break; 249 case WriteMouseOutputBuff: 250 DPRINTF(I8042, "Got command to write to mouse output buffer.\n"); 251 lastCommand = WriteMouseOutputBuff; 252 break; 253 case WriteToMouse: 254 DPRINTF(I8042, "Expecting mouse command.\n"); 255 lastCommand = WriteToMouse; 256 break; 257 case DisableA20: 258 panic("i8042 \"Disable A20\" command not implemented.\n"); 259 case EnableA20: 260 panic("i8042 \"Enable A20\" command not implemented.\n"); 261 case ReadTestInputs: 262 panic("i8042 \"Read test inputs\" command not implemented.\n"); 263 case SystemReset: 264 panic("i8042 \"System reset\" command not implemented.\n"); 265 default: 266 warn("Write to unknown i8042 " 267 "(keyboard controller) command port.\n"); 268 } 269 } else { 270 panic("Write to unrecognized port %#x.\n", addr); 271 } 272 pkt->makeAtomicResponse(); 273 return latency; 274} 275 276void 277X86ISA::I8042::serialize(CheckpointOut &cp) const 278{ 279 SERIALIZE_SCALAR(dataPort); 280 SERIALIZE_SCALAR(commandPort); 281 SERIALIZE_SCALAR(statusReg); 282 SERIALIZE_SCALAR(commandByte); 283 SERIALIZE_SCALAR(dataReg); 284 SERIALIZE_SCALAR(lastCommand);
|
513 mouse.serialize("mouse", cp);
514 keyboard.serialize("keyboard", cp);
|
285} 286 287void 288X86ISA::I8042::unserialize(CheckpointIn &cp) 289{ 290 UNSERIALIZE_SCALAR(dataPort); 291 UNSERIALIZE_SCALAR(commandPort); 292 UNSERIALIZE_SCALAR(statusReg); 293 UNSERIALIZE_SCALAR(commandByte); 294 UNSERIALIZE_SCALAR(dataReg); 295 UNSERIALIZE_SCALAR(lastCommand);
|
526 mouse.unserialize("mouse", cp);
527 keyboard.unserialize("keyboard", cp);
|
296} 297
|
530void
531X86ISA::PS2Mouse::serialize(const std::string &base, CheckpointOut &cp) const
532{
533 PS2Device::serialize(base, cp);
534
535 paramOut(cp, base + ".status", status);
536 paramOut(cp, base + ".resolution", resolution);
537 paramOut(cp, base + ".sampleRate", sampleRate);
538}
539
540void
541X86ISA::PS2Mouse::unserialize(const std::string &base, CheckpointIn &cp)
542{
543 PS2Device::unserialize(base, cp);
544
545 paramIn(cp, base + ".status", status);
546 paramIn(cp, base + ".resolution", resolution);
547 paramIn(cp, base + ".sampleRate", sampleRate);
548}
549
|
298X86ISA::I8042 * 299I8042Params::create() 300{ 301 return new X86ISA::I8042(this); 302}
|