keyboard.cc revision 12653
1/*
2 * Copyright (c) 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) 2008 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 * Authors: Gabe Black
41 *          Andreas Sandberg
42 */
43
44#include "dev/ps2/keyboard.hh"
45
46#include "base/logging.hh"
47#include "debug/PS2.hh"
48#include "params/PS2Keyboard.hh"
49
50const uint8_t PS2Keyboard::ID[] = {0xab, 0x83};
51
52PS2Keyboard::PS2Keyboard(const PS2KeyboardParams *p)
53    : PS2Device(p),
54      lastCommand(NoCommand)
55{
56}
57
58void
59PS2Keyboard::serialize(CheckpointOut &cp) const
60{
61    PS2Device::serialize(cp);
62    SERIALIZE_SCALAR(lastCommand);
63}
64
65void
66PS2Keyboard::unserialize(CheckpointIn &cp)
67{
68    PS2Device::unserialize(cp);
69    UNSERIALIZE_SCALAR(lastCommand);
70}
71
72void
73PS2Keyboard::recv(uint8_t data)
74{
75    if (lastCommand != NoCommand) {
76        switch (lastCommand) {
77          case LEDWrite:
78            DPRINTF(PS2, "Setting LEDs: "
79                    "caps lock %s, num lock %s, scroll lock %s\n",
80                    bits(data, 2) ? "on" : "off",
81                    bits(data, 1) ? "on" : "off",
82                    bits(data, 0) ? "on" : "off");
83            sendAck();
84            lastCommand = NoCommand;
85            break;
86          case TypematicInfo:
87            DPRINTF(PS2, "Setting typematic info to %#02x.\n", data);
88            sendAck();
89            lastCommand = NoCommand;
90            break;
91        }
92        return;
93    }
94
95    switch (data) {
96      case LEDWrite:
97        DPRINTF(PS2, "Got LED write command.\n");
98        sendAck();
99        lastCommand = LEDWrite;
100        break;
101      case DiagnosticEcho:
102        panic("Keyboard diagnostic echo unimplemented.\n");
103      case AlternateScanCodes:
104        panic("Accessing alternate scan codes unimplemented.\n");
105      case ReadID:
106        DPRINTF(PS2, "Got keyboard read ID command.\n");
107        sendAck();
108        send((uint8_t *)&ID, sizeof(ID));
109        break;
110      case TypematicInfo:
111        DPRINTF(PS2, "Setting typematic info.\n");
112        sendAck();
113        lastCommand = TypematicInfo;
114        break;
115      case Enable:
116        DPRINTF(PS2, "Enabling the keyboard.\n");
117        sendAck();
118        break;
119      case Disable:
120        DPRINTF(PS2, "Disabling the keyboard.\n");
121        sendAck();
122        break;
123      case DefaultsAndDisable:
124        DPRINTF(PS2, "Disabling and resetting the keyboard.\n");
125        sendAck();
126        break;
127      case AllKeysToTypematic:
128        panic("Setting all keys to typemantic unimplemented.\n");
129      case AllKeysToMakeRelease:
130        panic("Setting all keys to make/release unimplemented.\n");
131      case AllKeysToMake:
132        panic("Setting all keys to make unimplemented.\n");
133      case AllKeysToTypematicMakeRelease:
134        panic("Setting all keys to "
135                "typematic/make/release unimplemented.\n");
136      case KeyToTypematic:
137        panic("Setting a key to typematic unimplemented.\n");
138      case KeyToMakeRelease:
139        panic("Setting a key to make/release unimplemented.\n");
140      case KeyToMakeOnly:
141        panic("Setting key to make only unimplemented.\n");
142      case Resend:
143        panic("Keyboard resend unimplemented.\n");
144      case Reset:
145        panic("Keyboard reset unimplemented.\n");
146      default:
147        panic("Unknown keyboard command %#02x.\n", data);
148    }
149}
150
151PS2Keyboard *
152PS2KeyboardParams::create()
153{
154    return new PS2Keyboard(this);
155}
156