i8042.hh revision 11175
114262Sodanrc@yahoo.com.br/*
214262Sodanrc@yahoo.com.br * Copyright (c) 2008 The Regents of The University of Michigan
314262Sodanrc@yahoo.com.br * All rights reserved.
414262Sodanrc@yahoo.com.br *
514262Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without
614262Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are
714262Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright
814262Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer;
914262Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright
1014262Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the
1114262Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution;
1214262Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its
1314262Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from
1414262Sodanrc@yahoo.com.br * this software without specific prior written permission.
1514262Sodanrc@yahoo.com.br *
1614262Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1714262Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1814262Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1914262Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2014262Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2114262Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2214262Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2314262Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2414262Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2514262Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2614262Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2714262Sodanrc@yahoo.com.br *
2814262Sodanrc@yahoo.com.br * Authors: Gabe Black
2914262Sodanrc@yahoo.com.br */
3014262Sodanrc@yahoo.com.br
3114262Sodanrc@yahoo.com.br#ifndef __DEV_X86_I8042_HH__
3214262Sodanrc@yahoo.com.br#define __DEV_X86_I8042_HH__
3314262Sodanrc@yahoo.com.br
3414262Sodanrc@yahoo.com.br#include <deque>
3514262Sodanrc@yahoo.com.br
3614262Sodanrc@yahoo.com.br#include "dev/x86/intdev.hh"
3714262Sodanrc@yahoo.com.br#include "dev/io_device.hh"
3814262Sodanrc@yahoo.com.br#include "params/I8042.hh"
3914262Sodanrc@yahoo.com.br
4014262Sodanrc@yahoo.com.brnamespace X86ISA
4114262Sodanrc@yahoo.com.br{
4214262Sodanrc@yahoo.com.br
4314262Sodanrc@yahoo.com.brclass IntPin;
4414262Sodanrc@yahoo.com.br
4514262Sodanrc@yahoo.com.brclass PS2Device
4614262Sodanrc@yahoo.com.br{
4714262Sodanrc@yahoo.com.br  protected:
4814262Sodanrc@yahoo.com.br    std::deque<uint8_t> outBuffer;
4914262Sodanrc@yahoo.com.br
5014262Sodanrc@yahoo.com.br    static const uint16_t NoCommand = (uint16_t)(-1);
5114262Sodanrc@yahoo.com.br
5214262Sodanrc@yahoo.com.br    uint16_t lastCommand;
5314262Sodanrc@yahoo.com.br    void bufferData(const uint8_t *data, int size);
5414262Sodanrc@yahoo.com.br    void ack();
5514262Sodanrc@yahoo.com.br    void nack();
5614262Sodanrc@yahoo.com.br
5714262Sodanrc@yahoo.com.br  public:
5814262Sodanrc@yahoo.com.br    virtual ~PS2Device()
5914262Sodanrc@yahoo.com.br    {};
6014262Sodanrc@yahoo.com.br
6114262Sodanrc@yahoo.com.br    PS2Device() : lastCommand(NoCommand)
6214262Sodanrc@yahoo.com.br    {}
6314262Sodanrc@yahoo.com.br
6414262Sodanrc@yahoo.com.br    virtual void serialize(const std::string &base, CheckpointOut &cp) const;
6514262Sodanrc@yahoo.com.br    virtual void unserialize(const std::string &base, CheckpointIn &cp);
6614262Sodanrc@yahoo.com.br
67    bool hasData()
68    {
69        return !outBuffer.empty();
70    }
71
72    uint8_t getData()
73    {
74        uint8_t data = outBuffer.front();
75        outBuffer.pop_front();
76        return data;
77    }
78
79    virtual bool processData(uint8_t data) = 0;
80};
81
82class PS2Mouse : public PS2Device
83{
84  protected:
85    static const uint8_t ID[];
86
87    enum Command
88    {
89        Scale1to1 = 0xE6,
90        Scale2to1 = 0xE7,
91        SetResolution = 0xE8,
92        GetStatus = 0xE9,
93        ReadData = 0xEB,
94        ResetWrapMode = 0xEC,
95        WrapMode = 0xEE,
96        RemoteMode = 0xF0,
97        ReadID = 0xF2,
98        SampleRate = 0xF3,
99        EnableReporting = 0xF4,
100        DisableReporting = 0xF5,
101        DefaultsAndDisable = 0xF6,
102        Resend = 0xFE,
103        Reset = 0xFF
104    };
105
106    BitUnion8(Status)
107        Bitfield<6> remote;
108        Bitfield<5> enabled;
109        Bitfield<4> twoToOne;
110        Bitfield<2> leftButton;
111        Bitfield<0> rightButton;
112    EndBitUnion(Status)
113
114    Status status;
115    uint8_t resolution;
116    uint8_t sampleRate;
117  public:
118    PS2Mouse() : PS2Device(), status(0), resolution(4), sampleRate(100)
119    {}
120
121    bool processData(uint8_t data) override;
122
123    void serialize(const std::string &base, CheckpointOut &cp) const override;
124    void unserialize(const std::string &base, CheckpointIn &cp) override;
125};
126
127class PS2Keyboard : public PS2Device
128{
129  protected:
130    static const uint8_t ID[];
131
132    enum Command
133    {
134        LEDWrite = 0xED,
135        DiagnosticEcho = 0xEE,
136        AlternateScanCodes = 0xF0,
137        ReadID = 0xF2,
138        TypematicInfo = 0xF3,
139        Enable = 0xF4,
140        Disable = 0xF5,
141        DefaultsAndDisable = 0xF6,
142        AllKeysToTypematic = 0xF7,
143        AllKeysToMakeRelease = 0xF8,
144        AllKeysToMake = 0xF9,
145        AllKeysToTypematicMakeRelease = 0xFA,
146        KeyToTypematic = 0xFB,
147        KeyToMakeRelease = 0xFC,
148        KeyToMakeOnly = 0xFD,
149        Resend = 0xFE,
150        Reset = 0xFF
151    };
152
153  public:
154    bool processData(uint8_t data) override;
155};
156
157class I8042 : public BasicPioDevice
158{
159  protected:
160    enum Command
161    {
162        GetCommandByte = 0x20,
163        ReadControllerRamBase = 0x20,
164        WriteCommandByte = 0x60,
165        WriteControllerRamBase = 0x60,
166        CheckForPassword = 0xA4,
167        LoadPassword = 0xA5,
168        CheckPassword = 0xA6,
169        DisableMouse = 0xA7,
170        EnableMouse = 0xA8,
171        TestMouse = 0xA9,
172        SelfTest = 0xAA,
173        InterfaceTest = 0xAB,
174        DiagnosticDump = 0xAC,
175        DisableKeyboard = 0xAD,
176        EnableKeyboard = 0xAE,
177        ReadInputPort = 0xC0,
178        ContinuousPollLow = 0xC1,
179        ContinuousPollHigh = 0xC2,
180        ReadOutputPort = 0xD0,
181        WriteOutputPort = 0xD1,
182        WriteKeyboardOutputBuff = 0xD2,
183        WriteMouseOutputBuff = 0xD3,
184        WriteToMouse = 0xD4,
185        DisableA20 = 0xDD,
186        EnableA20 = 0xDF,
187        ReadTestInputs = 0xE0,
188        PulseOutputBitBase = 0xF0,
189        SystemReset = 0xFE
190    };
191
192    BitUnion8(StatusReg)
193        Bitfield<7> parityError;
194        Bitfield<6> timeout;
195        Bitfield<5> mouseOutputFull;
196        Bitfield<4> keyboardUnlocked;
197        Bitfield<3> commandLast;
198        Bitfield<2> passedSelfTest;
199        Bitfield<1> inputFull;
200        Bitfield<0> outputFull;
201    EndBitUnion(StatusReg)
202
203    BitUnion8(CommandByte)
204        Bitfield<6> convertScanCodes;
205        Bitfield<5> disableMouse;
206        Bitfield<4> disableKeyboard;
207        Bitfield<2> passedSelfTest;
208        Bitfield<1> mouseFullInt;
209        Bitfield<0> keyboardFullInt;
210    EndBitUnion(CommandByte)
211
212    Tick latency;
213    Addr dataPort;
214    Addr commandPort;
215
216    StatusReg statusReg;
217    CommandByte commandByte;
218
219    uint8_t dataReg;
220
221    static const uint16_t NoCommand = (uint16_t)(-1);
222    uint16_t lastCommand;
223
224    IntSourcePin *mouseIntPin;
225    IntSourcePin *keyboardIntPin;
226
227    PS2Mouse mouse;
228    PS2Keyboard keyboard;
229
230    void writeData(uint8_t newData, bool mouse = false);
231    uint8_t readDataOut();
232
233  public:
234    typedef I8042Params Params;
235
236    const Params *
237    params() const
238    {
239        return dynamic_cast<const Params *>(_params);
240    }
241
242    I8042(Params *p);
243
244    AddrRangeList getAddrRanges() const override;
245
246    Tick read(PacketPtr pkt) override;
247
248    Tick write(PacketPtr pkt) override;
249
250    void serialize(CheckpointOut &cp) const override;
251    void unserialize(CheckpointIn &cp) override;
252};
253
254} // namespace X86ISA
255
256#endif //__DEV_X86_I8042_HH__
257