keyboard.cc revision 12654
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 "dev/ps2.hh"
49#include "params/PS2Keyboard.hh"
50
51const uint8_t PS2Keyboard::ID[] = {0xab, 0x83};
52
53PS2Keyboard::PS2Keyboard(const PS2KeyboardParams *p)
54    : PS2Device(p),
55      lastCommand(NoCommand),
56      shiftDown(false),
57      enabled(false)
58{
59    if (p->vnc)
60        p->vnc->setKeyboard(this);
61}
62
63void
64PS2Keyboard::serialize(CheckpointOut &cp) const
65{
66    PS2Device::serialize(cp);
67    SERIALIZE_SCALAR(lastCommand);
68    SERIALIZE_SCALAR(shiftDown);
69    SERIALIZE_SCALAR(enabled);
70}
71
72void
73PS2Keyboard::unserialize(CheckpointIn &cp)
74{
75    PS2Device::unserialize(cp);
76    UNSERIALIZE_SCALAR(lastCommand);
77    UNSERIALIZE_SCALAR(shiftDown);
78    UNSERIALIZE_SCALAR(enabled);
79}
80
81void
82PS2Keyboard::recv(uint8_t data)
83{
84    if (lastCommand != NoCommand) {
85        switch (lastCommand) {
86          case LEDWrite:
87            DPRINTF(PS2, "Setting LEDs: "
88                    "caps lock %s, num lock %s, scroll lock %s\n",
89                    bits(data, 2) ? "on" : "off",
90                    bits(data, 1) ? "on" : "off",
91                    bits(data, 0) ? "on" : "off");
92            sendAck();
93            lastCommand = NoCommand;
94            break;
95          case TypematicInfo:
96            DPRINTF(PS2, "Setting typematic info to %#02x.\n", data);
97            sendAck();
98            lastCommand = NoCommand;
99            break;
100        }
101        return;
102    }
103
104    switch (data) {
105      case LEDWrite:
106        DPRINTF(PS2, "Got LED write command.\n");
107        sendAck();
108        lastCommand = LEDWrite;
109        break;
110      case DiagnosticEcho:
111        panic("Keyboard diagnostic echo unimplemented.\n");
112      case AlternateScanCodes:
113        panic("Accessing alternate scan codes unimplemented.\n");
114      case ReadID:
115        DPRINTF(PS2, "Got keyboard read ID command.\n");
116        sendAck();
117        send((uint8_t *)&ID, sizeof(ID));
118        break;
119      case TypematicInfo:
120        DPRINTF(PS2, "Setting typematic info.\n");
121        sendAck();
122        lastCommand = TypematicInfo;
123        break;
124      case Enable:
125        DPRINTF(PS2, "Enabling the keyboard.\n");
126        enabled = true;
127        sendAck();
128        break;
129      case Disable:
130        DPRINTF(PS2, "Disabling the keyboard.\n");
131        enabled = false;
132        sendAck();
133        break;
134      case DefaultsAndDisable:
135        DPRINTF(PS2, "Disabling and resetting the keyboard.\n");
136        enabled = false;
137        sendAck();
138        break;
139      case AllKeysToTypematic:
140        panic("Setting all keys to typemantic unimplemented.\n");
141      case AllKeysToMakeRelease:
142        panic("Setting all keys to make/release unimplemented.\n");
143      case AllKeysToMake:
144        panic("Setting all keys to make unimplemented.\n");
145      case AllKeysToTypematicMakeRelease:
146        panic("Setting all keys to "
147                "typematic/make/release unimplemented.\n");
148      case KeyToTypematic:
149        panic("Setting a key to typematic unimplemented.\n");
150      case KeyToMakeRelease:
151        panic("Setting a key to make/release unimplemented.\n");
152      case KeyToMakeOnly:
153        panic("Setting key to make only unimplemented.\n");
154      case Resend:
155        panic("Keyboard resend unimplemented.\n");
156      case Reset:
157        panic("Keyboard reset unimplemented.\n");
158      default:
159        panic("Unknown keyboard command %#02x.\n", data);
160    }
161}
162
163void
164PS2Keyboard::keyPress(uint32_t key, bool down)
165{
166    std::list<uint8_t> keys;
167
168    // convert the X11 keysym into ps2 codes and update the shift
169    // state (shiftDown)
170    Ps2::keySymToPs2(key, down, shiftDown, keys);
171
172    // Drop key presses if the keyboard hasn't been enabled by the
173    // host. We do that after translating the key code to ensure that
174    // we keep track of the shift state.
175    if (!enabled)
176        return;
177
178    // Insert into our queue of characters
179    for (uint8_t c : keys)
180        send(c);
181}
182
183
184PS2Keyboard *
185PS2KeyboardParams::create()
186{
187    return new PS2Keyboard(this);
188}
189