#include "dev/ps2/device.hh"
#include "base/logging.hh"
+#include "debug/PS2.hh"
#include "dev/ps2.hh"
#include "params/PS2Device.hh"
PS2Device::PS2Device(const PS2DeviceParams *p)
: SimObject(p)
{
+ inBuffer.reserve(16);
}
void
std::vector<uint8_t> buffer(outBuffer.size());
std::copy(outBuffer.begin(), outBuffer.end(), buffer.begin());
arrayParamOut(cp, "outBuffer", buffer);
+
+ SERIALIZE_CONTAINER(inBuffer);
}
void
arrayParamIn(cp, "outBuffer", buffer);
for (auto c : buffer)
outBuffer.push_back(c);
+
+ UNSERIALIZE_CONTAINER(inBuffer);
}
void
void
PS2Device::hostWrite(uint8_t c)
{
- recv(c);
+ DPRINTF(PS2, "PS2: Host -> device: %#x\n", c);
+ inBuffer.push_back(c);
+ if (recv(inBuffer))
+ inBuffer.clear();
}
void
{
assert(data || size == 0);
while (size) {
+ DPRINTF(PS2, "PS2: Device -> host: %#x\n", *data);
outBuffer.push_back(*(data++));
size--;
}
#define __DEV_PS2_DEVICE_HH__
#include <deque>
+#include <vector>
#include "sim/sim_object.hh"
protected: /* Device interface */
/**
* Data received from host.
+ *
+ * Data sent to the device is buffered one byte at a time. Each
+ * time a byte is added, this function is called and passed the
+ * current buffer. It should return true if it has consumed the
+ * data and the buffer can be cleared, or false if more data is
+ * needed to process the current command.
+ *
+ * @param data Pending input data (at least one byte)
+ * @return false if more data is needed to process the current
+ * command, true otherwise.
*/
- virtual void recv(uint8_t data) = 0;
+ virtual bool recv(const std::vector<uint8_t> &data) = 0;
/**
* Send data from a PS/2 device to a host
/** Device -> host FIFO */
std::deque<uint8_t> outBuffer;
+ /** Host -> device buffer */
+ std::vector<uint8_t> inBuffer;
+
std::function<void()> dataAvailableCallback;
};
PS2Keyboard::PS2Keyboard(const PS2KeyboardParams *p)
: PS2Device(p),
- lastCommand(NoCommand),
shiftDown(false),
enabled(false)
{
PS2Keyboard::serialize(CheckpointOut &cp) const
{
PS2Device::serialize(cp);
- SERIALIZE_SCALAR(lastCommand);
SERIALIZE_SCALAR(shiftDown);
SERIALIZE_SCALAR(enabled);
}
PS2Keyboard::unserialize(CheckpointIn &cp)
{
PS2Device::unserialize(cp);
- UNSERIALIZE_SCALAR(lastCommand);
UNSERIALIZE_SCALAR(shiftDown);
UNSERIALIZE_SCALAR(enabled);
}
-void
-PS2Keyboard::recv(uint8_t data)
+bool
+PS2Keyboard::recv(const std::vector<uint8_t> &data)
{
- if (lastCommand != NoCommand) {
- switch (lastCommand) {
- case LEDWrite:
+ switch (data[0]) {
+ case LEDWrite:
+ if (data.size() == 1) {
+ DPRINTF(PS2, "Got LED write command.\n");
+ sendAck();
+ return false;
+ } else {
DPRINTF(PS2, "Setting LEDs: "
"caps lock %s, num lock %s, scroll lock %s\n",
- bits(data, 2) ? "on" : "off",
- bits(data, 1) ? "on" : "off",
- bits(data, 0) ? "on" : "off");
- sendAck();
- lastCommand = NoCommand;
- break;
- case TypematicInfo:
- DPRINTF(PS2, "Setting typematic info to %#02x.\n", data);
+ bits(data[1], 2) ? "on" : "off",
+ bits(data[1], 1) ? "on" : "off",
+ bits(data[1], 0) ? "on" : "off");
sendAck();
- lastCommand = NoCommand;
- break;
+ return true;
}
- return;
- }
-
- switch (data) {
- case LEDWrite:
- DPRINTF(PS2, "Got LED write command.\n");
- sendAck();
- lastCommand = LEDWrite;
- break;
case DiagnosticEcho:
panic("Keyboard diagnostic echo unimplemented.\n");
case AlternateScanCodes:
DPRINTF(PS2, "Got keyboard read ID command.\n");
sendAck();
send((uint8_t *)&ID, sizeof(ID));
- break;
+ return true;
case TypematicInfo:
- DPRINTF(PS2, "Setting typematic info.\n");
- sendAck();
- lastCommand = TypematicInfo;
- break;
+ if (data.size() == 1) {
+ DPRINTF(PS2, "Setting typematic info.\n");
+ sendAck();
+ return false;
+ } else {
+ DPRINTF(PS2, "Setting typematic info to %#02x.\n", data[1]);
+ sendAck();
+ return true;
+ }
case Enable:
DPRINTF(PS2, "Enabling the keyboard.\n");
enabled = true;
sendAck();
- break;
+ return true;
case Disable:
DPRINTF(PS2, "Disabling the keyboard.\n");
enabled = false;
sendAck();
- break;
+ return true;
case DefaultsAndDisable:
DPRINTF(PS2, "Disabling and resetting the keyboard.\n");
enabled = false;
sendAck();
- break;
+ return true;
case AllKeysToTypematic:
panic("Setting all keys to typemantic unimplemented.\n");
case AllKeysToMakeRelease:
case Reset:
panic("Keyboard reset unimplemented.\n");
default:
- panic("Unknown keyboard command %#02x.\n", data);
+ panic("Unknown keyboard command %#02x.\n", data[0]);
}
}
Resend = 0xFE,
Reset = 0xFF
};
- static const uint16_t NoCommand = (uint16_t)(-1);
- uint16_t lastCommand;
-
/** is the shift key currently down */
bool shiftDown;
void unserialize(CheckpointIn &cp) override;
protected: // PS2Device
- void recv(uint8_t data) override;
+ bool recv(const std::vector<uint8_t> &data) override;
public: // VncKeyboard
void keyPress(uint32_t key, bool down) override;
PS2Mouse::PS2Mouse(const PS2MouseParams *p)
: PS2Device(p),
- lastCommand(NoCommand),
status(0), resolution(4), sampleRate(100)
{
}
-void
-PS2Mouse::recv(uint8_t data)
+bool
+PS2Mouse::recv(const std::vector<uint8_t> &data)
{
- if (lastCommand != NoCommand) {
- switch(lastCommand) {
- case SetResolution:
- DPRINTF(PS2, "Mouse resolution set to %d.\n", data);
- resolution = data;
- sendAck();
- lastCommand = NoCommand;
- break;
- case SampleRate:
- DPRINTF(PS2, "Mouse sample rate %d samples "
- "per second.\n", data);
- sampleRate = data;
- sendAck();
- lastCommand = NoCommand;
- break;
- default:
- panic("Not expecting data for a mouse command.\n");
- }
- return;
- }
- switch (data) {
+ switch (data[0]) {
case Scale1to1:
DPRINTF(PS2, "Setting mouse scale to 1:1.\n");
status.twoToOne = 0;
sendAck();
- break;
+ return true;
case Scale2to1:
DPRINTF(PS2, "Setting mouse scale to 2:1.\n");
status.twoToOne = 1;
sendAck();
- break;
+ return true;
case SetResolution:
- DPRINTF(PS2, "Setting mouse resolution.\n");
- lastCommand = SetResolution;
- sendAck();
- break;
+ if (data.size() == 1) {
+ DPRINTF(PS2, "Setting mouse resolution.\n");
+ sendAck();
+ return false;
+ } else {
+ DPRINTF(PS2, "Mouse resolution set to %d.\n", data[1]);
+ resolution = data[1];
+ sendAck();
+ return true;
+ }
case GetStatus:
DPRINTF(PS2, "Getting mouse status.\n");
sendAck();
send((uint8_t *)&(status), 1);
send(&resolution, sizeof(resolution));
send(&sampleRate, sizeof(sampleRate));
- break;
+ return true;
case ReadData:
panic("Reading mouse data unimplemented.\n");
case ResetWrapMode:
DPRINTF(PS2, "Mouse ID requested.\n");
sendAck();
send(ID, sizeof(ID));
- break;
+ return true;
case SampleRate:
- DPRINTF(PS2, "Setting mouse sample rate.\n");
- lastCommand = SampleRate;
- sendAck();
- break;
+ if (data.size() == 1) {
+ DPRINTF(PS2, "Setting mouse sample rate.\n");
+ sendAck();
+ return false;
+ } else {
+ DPRINTF(PS2, "Mouse sample rate %d samples "
+ "per second.\n", data[1]);
+ sampleRate = data[1];
+ sendAck();
+ return true;
+ }
case DisableReporting:
DPRINTF(PS2, "Disabling data reporting.\n");
status.enabled = 0;
sendAck();
- break;
+ return true;
case EnableReporting:
DPRINTF(PS2, "Enabling data reporting.\n");
status.enabled = 1;
sendAck();
- break;
+ return true;
case DefaultsAndDisable:
DPRINTF(PS2, "Disabling and resetting mouse.\n");
sampleRate = 100;
status.twoToOne = 0;
status.enabled = 0;
sendAck();
- break;
+ return true;
case Resend:
panic("Mouse resend unimplemented.\n");
case Reset:
sendAck();
send(&BatSuccessful, sizeof(BatSuccessful));
send(ID, sizeof(ID));
- break;
+ return true;
default:
- warn("Unknown mouse command %#02x.\n", data);
+ warn("Unknown mouse command %#02x.\n", data[0]);
send(Resend);
- break;
+ return true;
}
}
{
PS2Device::serialize(cp);
- SERIALIZE_SCALAR(lastCommand);
-
SERIALIZE_SCALAR(status);
SERIALIZE_SCALAR(resolution);
SERIALIZE_SCALAR(sampleRate);
{
PS2Device::unserialize(cp);
- UNSERIALIZE_SCALAR(lastCommand);
-
UNSERIALIZE_SCALAR(status);
UNSERIALIZE_SCALAR(resolution);
UNSERIALIZE_SCALAR(sampleRate);
Resend = 0xFE,
Reset = 0xFF
};
- static const uint16_t NoCommand = (uint16_t)(-1);
BitUnion8(Status)
Bitfield<6> remote;
Bitfield<0> rightButton;
EndBitUnion(Status)
- uint16_t lastCommand;
-
Status status;
uint8_t resolution;
uint8_t sampleRate;
void unserialize(CheckpointIn &cp) override;
protected: // PS2Device
- void recv(uint8_t data) override;
+ bool recv(const std::vector<uint8_t> &data) override;
};
#endif // __DEV_PS2_MOUSE_hH__
PS2TouchKit::PS2TouchKit(const PS2TouchKitParams *p)
: PS2Device(p),
vnc(p->vnc),
- ackNext(false),
driverInitialized(false)
{
if (vnc)
{
PS2Device::serialize(cp);
- SERIALIZE_SCALAR(ackNext);
SERIALIZE_SCALAR(driverInitialized);
}
{
PS2Device::unserialize(cp);
- UNSERIALIZE_SCALAR(ackNext);
UNSERIALIZE_SCALAR(driverInitialized);
}
-void
-PS2TouchKit::recv(uint8_t data)
+bool
+PS2TouchKit::recv(const std::vector<uint8_t> &data)
{
- if (ackNext) {
- ackNext--;
- sendAck();
- return;
- }
-
- switch (data) {
+ switch (data[0]) {
case Ps2::Ps2Reset:
sendAck();
send(Ps2::SelfTestPass);
- break;
+ return true;
case Ps2::SetResolution:
case Ps2::SetRate:
case Ps2::SetStatusLed:
sendAck();
- ackNext = 1;
- break;
+ return data.size() == 2;
case Ps2::ReadId:
sendAck();
send((const uint8_t *)&ID, sizeof(ID));
- break;
+ return true;
case Ps2::TpReadId:
// We're not a trackpoint device, this should make the probe
send(0);
send(0);
sendAck();
- break;
+ return true;
case Ps2::SetScaling1_1:
case Ps2::SetScaling1_2:
case Ps2::Enable:
case Ps2::SetDefaults:
sendAck();
- break;
+ return true;
case Ps2::StatusRequest:
sendAck();
send(0);
send(2); // default resolution
send(100); // default sample rate
- break;
+ return true;
case Ps2::TouchKitId:
- ackNext = 2;
sendAck();
- send(Ps2::TouchKitId);
- send(1);
- send('A');
-
- driverInitialized = true;
- break;
+ if (data.size() == 1) {
+ send(Ps2::TouchKitId);
+ send(1);
+ send('A');
+
+ return false;
+ } else if (data.size() == 3) {
+ driverInitialized = true;
+ return true;
+ } else {
+ return false;
+ }
default:
- panic("Unknown byte received: %d\n", data);
+ panic("Unknown byte received: %d\n", data[0]);
}
}
void unserialize(CheckpointIn &cp) override;
protected: // PS2Device
- void recv(uint8_t data) override;
+ bool recv(const std::vector<uint8_t> &data) override;
public: // VncMouse
void mouseAt(uint16_t x, uint16_t y, uint8_t buttons) override;
/** The vnc server we're connected to (if any) */
VncInput *const vnc;
- /**
- * If the controller should ignore the next N data bytes and
- * acknowledge them. This occurs when the driver is attempting to
- * setup some feature we don't care about.
- */
- int ackNext;
-
/**
* Has the driver been initialized in TouchKit mode? The model
* suppresses touch event generation until this is true.