The Pedigree Project 0.1
Ps2Controller.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef MACHINE_X86_PS2CONTROLLER_H
21#define MACHINE_X86_PS2CONTROLLER_H
22#include "pedigree/kernel/Atomic.h"
23#include "pedigree/kernel/compiler.h"
24#include "pedigree/kernel/machine/Controller.h"
25#include "pedigree/kernel/machine/IrqHandler.h"
26#include "pedigree/kernel/machine/Ps2CaptureState.h"
27#include "pedigree/kernel/machine/types.h"
28#include "pedigree/kernel/process/Mutex.h"
29#include "pedigree/kernel/processor/types.h"
30#include "pedigree/kernel/utilities/Buffer.h"
31#include "pedigree/kernel/utilities/String.h"
32
33#include <config.h>
34
35class IoBase;
36
37class Ps2Controller : public Controller, private IrqHandler {
38 public:
41
42 virtual ~Ps2Controller();
43
44 virtual void getName(String& str) {
45 str.assign("PS/2 Controller", 16);
46 }
47
48 virtual void dump(String& str) {
49 str.assign("PS/2 Controller", 16);
50 }
51
52 void initialise();
54 bool initialise3();
56 void uninitialise();
57
59 void sendCommand(uint8_t command);
60 void sendCommand(uint8_t command, uint8_t data);
62 uint8_t sendCommandWithResponse(uint8_t command);
63 uint8_t sendCommandWithResponse(uint8_t command, uint8_t data);
64
66 void writeFirstPort(uint8_t byte);
68 EXPORTED_PUBLIC void writeSecondPort(uint8_t byte);
69
71 EXPORTED_PUBLIC bool hasSecondPort() const;
72
74 EXPORTED_PUBLIC void setIrqEnable(bool firstEnabled, bool secondEnabled);
75
77 uint8_t readByte();
78 uint8_t readByteNonBlock();
79
81 bool readFirstPort(uint8_t& byte, bool block = true);
83 EXPORTED_PUBLIC bool readSecondPort(uint8_t& byte, bool block = true);
84
86 EXPORTED_PUBLIC bool readsStopping() const {
87 return m_ReadMode.value() == StoppingReadMode;
88 }
89
91 void setDebugState(bool debugState);
92
94 bool getDebugState() const {
95 return m_DebugState.active();
96 }
97
98 private:
99 static constexpr size_t PollingReadMode = 0;
100 static constexpr size_t BufferedReadMode = 1;
101 static constexpr size_t StoppingReadMode = 2;
102 static constexpr uint8_t OutputBufferFull = 1;
103 static constexpr uint8_t InputBufferFull = 1 << 1;
104 static constexpr uint8_t SecondPortData = 1 << 5;
105
106 IrqDisposition irq(irq_id_t number) override;
107
108 bool acquireIoForThread();
109 void releaseIo();
110
111 bool sendCommandLocked(uint8_t command);
112 bool sendCommandLocked(uint8_t command, uint8_t data);
113 bool sendCommandWithResponseLocked(uint8_t command, uint8_t& response);
114 bool sendCommandWithResponseLocked(uint8_t command, uint8_t data, uint8_t& response);
115 bool writeFirstPortLocked(uint8_t byte);
116 bool configureIrqEnable(bool firstEnabled, bool secondEnabled);
117
118 bool waitForReadingLocked();
119 bool waitForWritingLocked();
120
121 IoBase* m_pBase;
122 bool m_bHasSecondPort;
123
124 Buffer<uint8_t> m_FirstPortBuffer;
125 Buffer<uint8_t> m_SecondPortBuffer;
126
127 irq_id_t m_FirstIrqId;
128 irq_id_t m_SecondIrqId;
129
130 Ps2DebuggerPollingState m_DebugState;
131
132 uint8_t m_ConfigByte;
133
134 Ps2IoAdmissionGate m_IoGate;
135 Mutex m_DrainLock;
136 Atomic<size_t> m_ReadMode;
137 Atomic<size_t> m_RejectedThreadIo;
138 Atomic<size_t> m_FirstPortDrops;
139 Atomic<size_t> m_SecondPortDrops;
140 Atomic<size_t> m_EmptyIrqs;
141};
142
143#endif
Abstrace base class for hardware I/O capabilities.
Definition IoBase.h:32
Definition Mutex.h:56
void sendCommand(uint8_t command)
Send a command with no response or data. A timed-out command is dropped.
uint8_t readByte()
Reads a single byte from the PS/2 controller by polling.
EXPORTED_PUBLIC bool readsStopping() const
Reports that buffered readers must leave their long-lived loops.
void writeFirstPort(uint8_t byte)
Send a byte to the first port, dropping it if the controller times out.
virtual void dump(String &str)
virtual void getName(String &str)
EXPORTED_PUBLIC void writeSecondPort(uint8_t byte)
Send a byte to the second port, dropping it on timeout.
IrqDisposition irq(irq_id_t number) override
bool getDebugState() const
Gets the debug state.
bool readFirstPort(uint8_t &byte, bool block=true)
Reads a single byte from the first port.
EXPORTED_PUBLIC bool hasSecondPort() const
Reports whether this PS/2 controller has two ports.
EXPORTED_PUBLIC void setIrqEnable(bool firstEnabled, bool secondEnabled)
Enables/disables IRQs for the first or second ports.
uint8_t sendCommandWithResponse(uint8_t command)
Send a command and report its response, or zero if the operation fails.
EXPORTED_PUBLIC bool readSecondPort(uint8_t &byte, bool block=true)
Reads a single byte from the second port.
void setDebugState(bool debugState)
Selects trap-safe polling without changing controller IRQ state.
IrqDisposition
Definition IrqHandler.h:31