i8042.hh revision 5832:38f3f3e1e442
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#ifndef __DEV_X86_I8042_HH__
32#define __DEV_X86_I8042_HH__
33
34#include "dev/io_device.hh"
35#include "dev/x86/intdev.hh"
36#include "params/I8042.hh"
37
38#include <queue>
39
40namespace X86ISA
41{
42
43class IntPin;
44
45class PS2Device
46{
47  protected:
48    std::queue<uint8_t> outBuffer;
49
50    static const uint16_t NoCommand = (uint16_t)(-1);
51
52    uint16_t lastCommand;
53    void bufferData(const uint8_t *data, int size);
54    void ack();
55    void nack();
56
57  public:
58    virtual ~PS2Device()
59    {};
60
61    PS2Device() : lastCommand(NoCommand)
62    {}
63
64    bool hasData()
65    {
66        return !outBuffer.empty();
67    }
68
69    uint8_t getData()
70    {
71        uint8_t data = outBuffer.front();
72        outBuffer.pop();
73        return data;
74    }
75
76    virtual bool processData(uint8_t data) = 0;
77};
78
79class PS2Mouse : public PS2Device
80{
81  protected:
82    static const uint8_t ID[];
83
84    enum Command
85    {
86        Scale1to1 = 0xE6,
87        Scale2to1 = 0xE7,
88        SetResolution = 0xE8,
89        GetStatus = 0xE9,
90        ReadData = 0xEB,
91        ResetWrapMode = 0xEC,
92        WrapMode = 0xEE,
93        RemoteMode = 0xF0,
94        ReadID = 0xF2,
95        SampleRate = 0xF3,
96        EnableReporting = 0xF4,
97        DisableReporting = 0xF5,
98        DefaultsAndDisable = 0xF6,
99        Resend = 0xFE,
100        Reset = 0xFF
101    };
102
103    BitUnion8(Status)
104        Bitfield<6> remote;
105        Bitfield<5> enabled;
106        Bitfield<4> twoToOne;
107        Bitfield<2> leftButton;
108        Bitfield<0> rightButton;
109    EndBitUnion(Status)
110
111    Status status;
112    uint8_t resolution;
113    uint8_t sampleRate;
114  public:
115    PS2Mouse() : PS2Device(), status(0), resolution(4), sampleRate(100)
116    {}
117
118    bool processData(uint8_t data);
119};
120
121class PS2Keyboard : public PS2Device
122{
123  protected:
124    static const uint8_t ID[];
125
126    enum Command
127    {
128        LEDWrite = 0xED,
129        DiagnosticEcho = 0xEE,
130        AlternateScanCodes = 0xF0,
131        ReadID = 0xF2,
132        TypematicInfo = 0xF3,
133        Enable = 0xF4,
134        Disable = 0xF5,
135        DefaultsAndDisable = 0xF6,
136        AllKeysToTypematic = 0xF7,
137        AllKeysToMakeRelease = 0xF8,
138        AllKeysToMake = 0xF9,
139        AllKeysToTypematicMakeRelease = 0xFA,
140        KeyToTypematic = 0xFB,
141        KeyToMakeRelease = 0xFC,
142        KeyToMakeOnly = 0xFD,
143        Resend = 0xFE,
144        Reset = 0xFF
145    };
146
147  public:
148    bool processData(uint8_t data);
149};
150
151class I8042 : public BasicPioDevice
152{
153  protected:
154    enum Command
155    {
156        GetCommandByte = 0x20,
157        ReadControllerRamBase = 0x20,
158        WriteCommandByte = 0x60,
159        WriteControllerRamBase = 0x60,
160        CheckForPassword = 0xA4,
161        LoadPassword = 0xA5,
162        CheckPassword = 0xA6,
163        DisableMouse = 0xA7,
164        EnableMouse = 0xA8,
165        TestMouse = 0xA9,
166        SelfTest = 0xAA,
167        InterfaceTest = 0xAB,
168        DiagnosticDump = 0xAC,
169        DisableKeyboard = 0xAD,
170        EnableKeyboard = 0xAE,
171        ReadInputPort = 0xC0,
172        ContinuousPollLow = 0xC1,
173        ContinuousPollHigh = 0xC2,
174        ReadOutputPort = 0xD0,
175        WriteOutputPort = 0xD1,
176        WriteKeyboardOutputBuff = 0xD2,
177        WriteMouseOutputBuff = 0xD3,
178        WriteToMouse = 0xD4,
179        DisableA20 = 0xDD,
180        EnableA20 = 0xDF,
181        ReadTestInputs = 0xE0,
182        PulseOutputBitBase = 0xF0,
183        SystemReset = 0xFE
184    };
185
186    BitUnion8(StatusReg)
187        Bitfield<7> parityError;
188        Bitfield<6> timeout;
189        Bitfield<5> mouseOutputFull;
190        Bitfield<4> keyboardUnlocked;
191        Bitfield<3> commandLast;
192        Bitfield<2> passedSelfTest;
193        Bitfield<1> inputFull;
194        Bitfield<0> outputFull;
195    EndBitUnion(StatusReg)
196
197    BitUnion8(CommandByte)
198        Bitfield<6> convertScanCodes;
199        Bitfield<5> disableMouse;
200        Bitfield<4> disableKeyboard;
201        Bitfield<2> passedSelfTest;
202        Bitfield<1> mouseFullInt;
203        Bitfield<0> keyboardFullInt;
204    EndBitUnion(CommandByte)
205
206    Tick latency;
207    Addr dataPort;
208    Addr commandPort;
209
210    StatusReg statusReg;
211    CommandByte commandByte;
212
213    uint8_t dataReg;
214
215    static const uint16_t NoCommand = (uint16_t)(-1);
216    uint16_t lastCommand;
217
218    IntSourcePin *mouseIntPin;
219    IntSourcePin *keyboardIntPin;
220
221    PS2Mouse mouse;
222    PS2Keyboard keyboard;
223
224    void writeData(uint8_t newData, bool mouse = false);
225    uint8_t readDataOut();
226
227  public:
228    typedef I8042Params Params;
229
230    const Params *
231    params() const
232    {
233        return dynamic_cast<const Params *>(_params);
234    }
235
236    I8042(Params *p) : BasicPioDevice(p), latency(p->pio_latency),
237            dataPort(p->data_port), commandPort(p->command_port),
238            statusReg(0), commandByte(0), dataReg(0), lastCommand(NoCommand),
239            mouseIntPin(p->mouse_int_pin), keyboardIntPin(p->keyboard_int_pin)
240    {
241        statusReg.passedSelfTest = 1;
242        statusReg.commandLast = 1;
243        statusReg.keyboardUnlocked = 1;
244
245        commandByte.convertScanCodes = 1;
246        commandByte.passedSelfTest = 1;
247        commandByte.keyboardFullInt = 1;
248    }
249
250    void addressRanges(AddrRangeList &range_list);
251
252    Tick read(PacketPtr pkt);
253
254    Tick write(PacketPtr pkt);
255};
256
257}; // namespace X86ISA
258
259#endif //__DEV_X86_I8042_HH__
260