1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/kernel/kb/ps2.cpp

271 lines
6.3 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2011, 2012, 2014, 2015 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* kb/ps2.cpp
* PS2 Keyboard.
*/
#include <stddef.h>
#include <stdint.h>
2013-10-26 20:42:10 -04:00
#include <string.h>
2013-01-09 17:30:36 -05:00
#include <sortix/keycodes.h>
2013-01-09 17:30:36 -05:00
2013-10-26 20:42:10 -04:00
#include <sortix/kernel/keyboard.h>
2015-04-30 18:07:06 -04:00
#include <sortix/kernel/ps2.h>
#include <sortix/kernel/kthread.h>
#include "ps2.h"
2015-04-30 18:07:06 -04:00
// TODO: This driver doesn't deal with keyboard scancode sets yet.
namespace Sortix {
2015-04-30 18:07:06 -04:00
static const uint8_t DEVICE_RESET_OK = 0xAA;
static const uint8_t DEVICE_SCANCODE_ESCAPE = 0xE0;
static const uint8_t DEVICE_ECHO = 0xEE;
static const uint8_t DEVICE_ACK = 0xFA;
static const uint8_t DEVICE_RESEND = 0xFE;
static const uint8_t DEVICE_ERROR = 0xFF;
2015-04-30 18:07:06 -04:00
static const uint8_t DEVICE_CMD_SET_LED = 0xED;
static const uint8_t DEVICE_CMD_SET_TYPEMATIC = 0xF3;
static const uint8_t DEVICE_CMD_ENABLE_SCAN = 0xF4;
static const uint8_t DEVICE_CMD_DISABLE_SCAN = 0xF5;
static const uint8_t DEVICE_CMD_IDENTIFY = 0xF2;
static const uint8_t DEVICE_CMD_RESET = 0xFF;
static const uint8_t DEVICE_LED_SCRLCK = 1 << 0;
static const uint8_t DEVICE_LED_NUMLCK = 1 << 1;
static const uint8_t DEVICE_LED_CAPSLCK = 1 << 2;
2015-04-30 18:07:06 -04:00
static const size_t DEVICE_RETRIES = 5;
PS2Keyboard::PS2Keyboard()
{
this->queue = NULL;
this->queuelength = 0;
this->queueoffset = 0;
this->queueused = 0;
this->owner = NULL;
this->ownerptr = NULL;
2015-04-30 18:07:06 -04:00
// TODO: Initial LED status can be read from the BIOS data area. If so, we
// need to emulate fake presses of the modifier keys to keep the
// keyboard layout in sync.
this->leds = 0;
this->kblock = KTHREAD_MUTEX_INITIALIZER;
}
PS2Keyboard::~PS2Keyboard()
{
delete[] queue;
}
2015-04-30 18:07:06 -04:00
void PS2Keyboard::PS2DeviceInitialize(void* send_ctx, bool (*send)(void*, uint8_t),
uint8_t* id, size_t id_size)
{
2015-04-30 18:07:06 -04:00
if ( sizeof(this->id) < id_size )
id_size = sizeof(this->id);
this->send_ctx = send_ctx;
this->send = send;
this->state = STATE_INIT;
this->tries = 0;
memcpy(this->id, id, id_size);
this->id_size = id_size;
PS2DeviceOnByte(DEVICE_RESEND);
}
2012-08-01 14:53:51 -04:00
2015-04-30 18:07:06 -04:00
void PS2Keyboard::PS2DeviceOnByte(uint8_t byte)
{
2015-04-30 18:07:06 -04:00
ScopedLock lock(&kblock);
if ( state == STATE_INIT )
2012-08-01 14:53:51 -04:00
{
2015-04-30 18:07:06 -04:00
state = STATE_RESET_LED;
tries = DEVICE_RETRIES;
byte = DEVICE_RESEND;
2012-08-01 14:53:51 -04:00
}
2015-04-30 18:07:06 -04:00
if ( state == STATE_RESET_LED )
{
if ( byte == DEVICE_RESEND && tries-- )
{
if ( send(send_ctx, DEVICE_CMD_SET_LED) &&
send(send_ctx, leds & 0x07) )
return;
}
state = STATE_RESET_TYPEMATIC;
tries = DEVICE_RETRIES;
byte = DEVICE_RESEND;
}
if ( state == STATE_RESET_TYPEMATIC )
{
if ( byte == DEVICE_RESEND && tries-- )
{
uint8_t rate = 0b00000; // 33.36 ms/repeat.
uint8_t delay = 0b01; // 500 ms.
uint8_t typematic = delay << 3 | rate << 0;
if ( send(send_ctx, DEVICE_CMD_SET_TYPEMATIC) &&
send(send_ctx, typematic) )
return;
}
state = STATE_ENABLE_SCAN;
tries = DEVICE_RETRIES;
byte = DEVICE_RESEND;
}
if ( state == STATE_ENABLE_SCAN )
{
if ( byte == DEVICE_RESEND && tries-- )
{
if ( send(send_ctx, DEVICE_CMD_ENABLE_SCAN) )
return;
}
state = STATE_NORMAL;
tries = DEVICE_RETRIES;
byte = DEVICE_RESEND;
}
if ( byte == DEVICE_RESEND || byte == DEVICE_ACK )
return;
if ( byte == DEVICE_SCANCODE_ESCAPE )
{
2015-04-30 18:07:06 -04:00
state = STATE_NORMAL_ESCAPED;
return;
2012-08-01 14:53:51 -04:00
}
2015-04-30 18:07:06 -04:00
if ( state == STATE_NORMAL )
{
int kbkey = byte & 0x7F;
OnKeyboardKey(byte & 0x80 ? -kbkey : kbkey);
lock.Reset();
NotifyOwner();
return;
}
if ( state == STATE_NORMAL_ESCAPED )
2012-08-01 14:53:51 -04:00
{
2015-04-30 18:07:06 -04:00
state = STATE_NORMAL;
int kbkey = (byte & 0x7F) + 0x80;
OnKeyboardKey(byte & 0x80 ? -kbkey : kbkey);
lock.Reset();
NotifyOwner();
return;
}
2015-04-30 18:07:06 -04:00
}
void PS2Keyboard::OnKeyboardKey(int kbkey)
{
if ( !PushKey(kbkey) )
return;
uint8_t newleds = leds;
if ( kbkey == KBKEY_CAPSLOCK )
2015-04-30 18:07:06 -04:00
newleds ^= DEVICE_LED_CAPSLCK;
if ( kbkey == KBKEY_SCROLLLOCK )
2015-04-30 18:07:06 -04:00
newleds ^= DEVICE_LED_SCRLCK;
if ( kbkey == KBKEY_NUMLOCK )
2015-04-30 18:07:06 -04:00
newleds ^= DEVICE_LED_NUMLCK;
if ( newleds != leds )
UpdateLEDs(leds = newleds);
}
2012-08-01 14:53:51 -04:00
void PS2Keyboard::NotifyOwner()
{
2015-04-30 18:07:06 -04:00
if ( !owner )
return;
owner->OnKeystroke(this, ownerptr);
}
void PS2Keyboard::UpdateLEDs(int ledval)
{
2015-04-30 18:07:06 -04:00
send(send_ctx, DEVICE_CMD_SET_LED) &&
send(send_ctx, ledval);
}
void PS2Keyboard::SetOwner(KeyboardOwner* owner, void* user)
{
kthread_mutex_lock(&kblock);
this->owner = owner;
this->ownerptr = user;
kthread_mutex_unlock(&kblock);
if ( queueused )
NotifyOwner();
}
bool PS2Keyboard::PushKey(int key)
{
// Check if we need to allocate or resize the circular queue.
if ( queueused == queuelength )
{
2015-04-30 18:07:06 -04:00
size_t newqueuelength = queuelength ? 2 * queuelength : 32UL;
if ( 16 * 1024 < newqueuelength )
return false;
int* newqueue = new int[newqueuelength];
if ( !newqueue )
return false;
if ( queue )
{
size_t elemsize = sizeof(*queue);
2015-04-30 18:07:06 -04:00
size_t leadingavai = queuelength - queueoffset;
size_t leading = leadingavai < queueused ? leadingavai : queueused;
size_t trailing = queueused - leading;
memcpy(newqueue, queue + queueoffset, leading * elemsize);
memcpy(newqueue + leading, queue, trailing * elemsize);
delete[] queue;
}
queue = newqueue;
queuelength = newqueuelength;
queueoffset = 0;
}
queue[(queueoffset + queueused++) % queuelength] = key;
return true;
}
int PS2Keyboard::PopKey()
{
if ( !queueused )
return 0;
int kbkey = queue[queueoffset];
queueoffset = (queueoffset + 1) % queuelength;
queueused--;
return kbkey;
}
int PS2Keyboard::Read()
{
ScopedLock lock(&kblock);
return PopKey();
}
size_t PS2Keyboard::GetPending() const
{
ScopedLock lock(&kblock);
return queueused;
}
bool PS2Keyboard::HasPending() const
{
ScopedLock lock(&kblock);
return queueused;
}
} // namespace Sortix