2012-08-01 14:53:51 -04:00
|
|
|
/*******************************************************************************
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014.
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
kb/ps2.cpp
|
|
|
|
A driver for the PS2 Keyboard.
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2012-08-01 14:53:51 -04:00
|
|
|
*******************************************************************************/
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <assert.h>
|
2014-01-03 19:28:59 -05:00
|
|
|
#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
|
|
|
|
2013-09-14 12:59:15 -04:00
|
|
|
#if defined(__x86_64__)
|
|
|
|
#include <msr.h>
|
|
|
|
#endif
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/keycodes.h>
|
2013-01-09 17:30:36 -05:00
|
|
|
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/cpu.h>
|
|
|
|
#include <sortix/kernel/debugger.h>
|
|
|
|
#include <sortix/kernel/interrupt.h>
|
2014-03-03 12:22:30 -05:00
|
|
|
#include <sortix/kernel/ioport.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/kernel.h>
|
|
|
|
#include <sortix/kernel/keyboard.h>
|
2014-03-03 18:11:13 -05:00
|
|
|
#include <sortix/kernel/scheduler.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/thread.h>
|
2013-01-09 17:30:36 -05:00
|
|
|
|
2013-09-14 12:59:15 -04:00
|
|
|
#if defined(__i386__)
|
|
|
|
#include "../x86-family/gdt.h"
|
|
|
|
#endif
|
|
|
|
|
2012-01-07 15:09:01 -05:00
|
|
|
#include "ps2.h"
|
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
namespace Sortix {
|
|
|
|
|
|
|
|
const uint16_t DATA = 0x0;
|
|
|
|
const uint16_t COMMAND = 0x0;
|
|
|
|
const uint16_t STATUS = 0x4;
|
|
|
|
const uint8_t CMD_SETLED = 0xED;
|
|
|
|
const uint8_t LED_SCRLCK = 1 << 0;
|
|
|
|
const uint8_t LED_NUMLCK = 1 << 1;
|
|
|
|
const uint8_t LED_CAPSLCK = 1 << 2;
|
|
|
|
|
2014-03-03 18:11:13 -05:00
|
|
|
void PS2Keyboard__OnInterrupt(struct interrupt_context* intctx, void* user)
|
2012-01-07 15:09:01 -05:00
|
|
|
{
|
2014-03-03 18:11:13 -05:00
|
|
|
((PS2Keyboard*) user)->OnInterrupt(intctx);
|
2014-01-03 19:28:59 -05:00
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
PS2Keyboard::PS2Keyboard(uint16_t iobase, uint8_t interrupt)
|
|
|
|
{
|
|
|
|
this->queue = NULL;
|
|
|
|
this->queuelength = 0;
|
|
|
|
this->queueoffset = 0;
|
|
|
|
this->queueused = 0;
|
|
|
|
this->owner = NULL;
|
|
|
|
this->ownerptr = NULL;
|
|
|
|
this->iobase = iobase;
|
|
|
|
this->interrupt = interrupt;
|
|
|
|
this->leds = 0;
|
|
|
|
this->scancodeescaped = false;
|
|
|
|
this->kblock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
Interrupt::RegisterHandler(interrupt, PS2Keyboard__OnInterrupt, this);
|
|
|
|
|
|
|
|
// If any scancodes were already pending, our interrupt handler will
|
|
|
|
// never be called. Let's just discard anything pending.
|
|
|
|
PopScancode();
|
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
PS2Keyboard::~PS2Keyboard()
|
|
|
|
{
|
|
|
|
Interrupt::RegisterHandler(interrupt, NULL, NULL);
|
|
|
|
delete[] queue;
|
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
struct PS2KeyboardWork
|
|
|
|
{
|
|
|
|
uint8_t scancode;
|
|
|
|
};
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-09-14 14:05:34 -04:00
|
|
|
static void PS2Keyboard__InterruptWork(void* kb_ptr, void* payload, size_t size)
|
2014-01-03 19:28:59 -05:00
|
|
|
{
|
|
|
|
assert(size == sizeof(PS2KeyboardWork));
|
|
|
|
PS2KeyboardWork* work = (PS2KeyboardWork*) payload;
|
2014-09-14 14:05:34 -04:00
|
|
|
((PS2Keyboard*) kb_ptr)->InterruptWork(work->scancode);
|
2014-01-03 19:28:59 -05:00
|
|
|
}
|
2012-08-01 14:53:51 -04:00
|
|
|
|
2014-03-03 18:11:13 -05:00
|
|
|
void PS2Keyboard::OnInterrupt(struct interrupt_context* intctx)
|
2014-01-03 19:28:59 -05:00
|
|
|
{
|
|
|
|
uint8_t scancode = PopScancode();
|
|
|
|
if ( scancode == KBKEY_F10 )
|
2012-08-01 14:53:51 -04:00
|
|
|
{
|
2014-03-03 18:11:13 -05:00
|
|
|
Scheduler::SaveInterruptedContext(intctx, &CurrentThread()->registers);
|
2014-04-12 17:31:21 -04:00
|
|
|
Debugger::Run(true);
|
2012-08-01 14:53:51 -04:00
|
|
|
}
|
2014-01-03 19:28:59 -05:00
|
|
|
PS2KeyboardWork work;
|
|
|
|
work.scancode = scancode;
|
2014-09-14 14:05:34 -04:00
|
|
|
Interrupt::ScheduleWork(PS2Keyboard__InterruptWork, this, &work, sizeof(work));
|
2014-01-03 19:28:59 -05:00
|
|
|
}
|
2012-08-01 14:53:51 -04:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
void PS2Keyboard::InterruptWork(uint8_t scancode)
|
|
|
|
{
|
|
|
|
kthread_mutex_lock(&kblock);
|
|
|
|
int kbkey = DecodeScancode(scancode);
|
|
|
|
if ( !kbkey )
|
2012-01-07 15:09:01 -05:00
|
|
|
{
|
2014-01-03 19:28:59 -05:00
|
|
|
kthread_mutex_unlock(&kblock);
|
|
|
|
return;
|
2012-08-01 14:53:51 -04:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
if ( !PushKey(kbkey) )
|
2012-08-01 14:53:51 -04:00
|
|
|
{
|
2014-01-03 19:28:59 -05:00
|
|
|
Log::PrintF("Warning: dropping keystroke due to insufficient "
|
|
|
|
"storage space in PS2 keyboard driver.\n");
|
|
|
|
kthread_mutex_unlock(&kblock);
|
|
|
|
return;
|
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
uint8_t newleds = leds;
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
if ( kbkey == KBKEY_CAPSLOCK )
|
|
|
|
newleds ^= LED_CAPSLCK;
|
|
|
|
if ( kbkey == KBKEY_SCROLLLOCK )
|
|
|
|
newleds ^= LED_SCRLCK;
|
|
|
|
if ( kbkey == KBKEY_NUMLOCK )
|
|
|
|
newleds ^= LED_NUMLCK;
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
if ( newleds != leds )
|
|
|
|
UpdateLEDs(leds = newleds);
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
kthread_mutex_unlock(&kblock);
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
NotifyOwner();
|
|
|
|
}
|
2012-08-01 14:53:51 -04:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
void PS2Keyboard::NotifyOwner()
|
|
|
|
{
|
|
|
|
if ( !owner)
|
|
|
|
return;
|
|
|
|
owner->OnKeystroke(this, ownerptr);
|
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
int PS2Keyboard::DecodeScancode(uint8_t scancode)
|
|
|
|
{
|
|
|
|
const uint8_t SCANCODE_ESCAPE = 0xE0;
|
|
|
|
if ( scancode == SCANCODE_ESCAPE )
|
2012-01-07 15:09:01 -05:00
|
|
|
{
|
2014-01-03 19:28:59 -05:00
|
|
|
scancodeescaped = true;
|
|
|
|
return 0;
|
2012-01-07 15:09:01 -05:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
int offset = scancodeescaped ? 0x80 : 0;
|
|
|
|
int kbkey = scancode & 0x7F;
|
|
|
|
kbkey = scancode & 0x80 ? -kbkey - offset : kbkey + offset;
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
scancodeescaped = false;
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
// kbkey is now in the format specified in <sortix/keycodes.h>.
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
return kbkey;
|
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
uint8_t PS2Keyboard::PopScancode()
|
|
|
|
{
|
2014-03-03 12:22:30 -05:00
|
|
|
return inport8(iobase + DATA);
|
2014-01-03 19:28:59 -05:00
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
void PS2Keyboard::UpdateLEDs(int ledval)
|
|
|
|
{
|
2014-03-03 12:22:30 -05:00
|
|
|
while ( (inport8(iobase + STATUS) & (1<<1)) );
|
|
|
|
outport8(iobase + COMMAND, CMD_SETLED);
|
|
|
|
while ( (inport8(iobase + STATUS) & (1<<1)) );
|
|
|
|
outport8(iobase + COMMAND, ledval);
|
2014-01-03 19:28:59 -05:00
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
void PS2Keyboard::SetOwner(KeyboardOwner* owner, void* user)
|
|
|
|
{
|
|
|
|
kthread_mutex_lock(&kblock);
|
|
|
|
this->owner = owner;
|
|
|
|
this->ownerptr = user;
|
|
|
|
kthread_mutex_unlock(&kblock);
|
|
|
|
if ( queueused )
|
|
|
|
NotifyOwner();
|
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
bool PS2Keyboard::PushKey(int key)
|
|
|
|
{
|
|
|
|
// Check if we need to allocate or resize the circular queue.
|
|
|
|
if ( queueused == queuelength )
|
|
|
|
{
|
|
|
|
size_t newqueuelength = (queuelength) ? queuelength * 2 : 32UL;
|
|
|
|
int* newqueue = new int[newqueuelength];
|
|
|
|
if ( !newqueue )
|
|
|
|
return false;
|
|
|
|
size_t elemsize = sizeof(*queue);
|
|
|
|
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;
|
2012-01-07 15:09:01 -05:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
queue[(queueoffset + queueused++) % queuelength] = key;
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
int PS2Keyboard::PopKey()
|
|
|
|
{
|
|
|
|
if ( !queueused )
|
|
|
|
return 0;
|
|
|
|
int kbkey = queue[queueoffset];
|
|
|
|
queueoffset = (queueoffset + 1) % queuelength;
|
|
|
|
queueused--;
|
|
|
|
return kbkey;
|
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
int PS2Keyboard::Read()
|
|
|
|
{
|
|
|
|
ScopedLock lock(&kblock);
|
|
|
|
return PopKey();
|
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
size_t PS2Keyboard::GetPending() const
|
|
|
|
{
|
|
|
|
ScopedLock lock(&kblock);
|
|
|
|
return queueused;
|
|
|
|
}
|
2012-01-07 15:09:01 -05:00
|
|
|
|
2014-01-03 19:28:59 -05:00
|
|
|
bool PS2Keyboard::HasPending() const
|
|
|
|
{
|
|
|
|
ScopedLock lock(&kblock);
|
|
|
|
return queueused;
|
2012-01-07 15:09:01 -05:00
|
|
|
}
|
2014-01-03 19:28:59 -05:00
|
|
|
|
|
|
|
} // namespace Sortix
|