Update kernel/kb/ps2.{cpp,h} to current coding conventions.

This commit is contained in:
Jonas 'Sortie' Termansen 2014-01-04 01:28:59 +01:00
parent 6e6df64fa8
commit bb3b6b0260
2 changed files with 247 additions and 229 deletions

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014.
This file is part of Sortix. This file is part of Sortix.
@ -23,6 +23,8 @@
*******************************************************************************/ *******************************************************************************/
#include <assert.h> #include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#include <sortix/keycodes.h> #include <sortix/keycodes.h>
@ -36,23 +38,23 @@
#include "ps2.h" #include "ps2.h"
namespace Sortix 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;
void PS2Keyboard__OnInterrupt(CPU::InterruptRegisters* regs, void* user)
{ {
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);
void PS2Keyboard__OnInterrupt(CPU::InterruptRegisters* regs, void* user)
{
((PS2Keyboard*) user)->OnInterrupt(regs); ((PS2Keyboard*) user)->OnInterrupt(regs);
} }
PS2Keyboard::PS2Keyboard(uint16_t iobase, uint8_t interrupt) PS2Keyboard::PS2Keyboard(uint16_t iobase, uint8_t interrupt)
{ {
this->queue = NULL; this->queue = NULL;
this->queuelength = 0; this->queuelength = 0;
this->queueoffset = 0; this->queueoffset = 0;
@ -69,29 +71,29 @@ namespace Sortix
// If any scancodes were already pending, our interrupt handler will // If any scancodes were already pending, our interrupt handler will
// never be called. Let's just discard anything pending. // never be called. Let's just discard anything pending.
PopScancode(); PopScancode();
} }
PS2Keyboard::~PS2Keyboard() PS2Keyboard::~PS2Keyboard()
{ {
Interrupt::RegisterHandler(interrupt, NULL, NULL); Interrupt::RegisterHandler(interrupt, NULL, NULL);
delete[] queue; delete[] queue;
} }
struct PS2KeyboardWork struct PS2KeyboardWork
{ {
PS2Keyboard* kb; PS2Keyboard* kb;
uint8_t scancode; uint8_t scancode;
}; };
static void PS2Keyboard__InterruptWork(void* payload, size_t size) static void PS2Keyboard__InterruptWork(void* payload, size_t size)
{ {
assert(size == sizeof(PS2KeyboardWork)); assert(size == sizeof(PS2KeyboardWork));
PS2KeyboardWork* work = (PS2KeyboardWork*) payload; PS2KeyboardWork* work = (PS2KeyboardWork*) payload;
work->kb->InterruptWork(work->scancode); work->kb->InterruptWork(work->scancode);
} }
void PS2Keyboard::OnInterrupt(CPU::InterruptRegisters* regs) void PS2Keyboard::OnInterrupt(CPU::InterruptRegisters* regs)
{ {
uint8_t scancode = PopScancode(); uint8_t scancode = PopScancode();
if ( scancode == KBKEY_F10 ) if ( scancode == KBKEY_F10 )
{ {
@ -102,13 +104,17 @@ namespace Sortix
work.kb = this; work.kb = this;
work.scancode = scancode; work.scancode = scancode;
Interrupt::ScheduleWork(PS2Keyboard__InterruptWork, &work, sizeof(work)); Interrupt::ScheduleWork(PS2Keyboard__InterruptWork, &work, sizeof(work));
} }
void PS2Keyboard::InterruptWork(uint8_t scancode) void PS2Keyboard::InterruptWork(uint8_t scancode)
{ {
kthread_mutex_lock(&kblock); kthread_mutex_lock(&kblock);
int kbkey = DecodeScancode(scancode); int kbkey = DecodeScancode(scancode);
if ( !kbkey ) { kthread_mutex_unlock(&kblock); return; } if ( !kbkey )
{
kthread_mutex_unlock(&kblock);
return;
}
if ( !PushKey(kbkey) ) if ( !PushKey(kbkey) )
{ {
@ -120,25 +126,30 @@ namespace Sortix
uint8_t newleds = leds; uint8_t newleds = leds;
if ( kbkey == KBKEY_CAPSLOCK ) { newleds ^= LED_CAPSLCK; } if ( kbkey == KBKEY_CAPSLOCK )
if ( kbkey == KBKEY_SCROLLLOCK ) { newleds ^= LED_SCRLCK; } newleds ^= LED_CAPSLCK;
if ( kbkey == KBKEY_NUMLOCK ) { newleds ^= LED_NUMLCK; } if ( kbkey == KBKEY_SCROLLLOCK )
newleds ^= LED_SCRLCK;
if ( kbkey == KBKEY_NUMLOCK )
newleds ^= LED_NUMLCK;
if ( newleds != leds ) { UpdateLEDs(leds = newleds); } if ( newleds != leds )
UpdateLEDs(leds = newleds);
kthread_mutex_unlock(&kblock); kthread_mutex_unlock(&kblock);
NotifyOwner(); NotifyOwner();
} }
void PS2Keyboard::NotifyOwner() void PS2Keyboard::NotifyOwner()
{ {
if ( !owner) { return; } if ( !owner)
return;
owner->OnKeystroke(this, ownerptr); owner->OnKeystroke(this, ownerptr);
} }
int PS2Keyboard::DecodeScancode(uint8_t scancode) int PS2Keyboard::DecodeScancode(uint8_t scancode)
{ {
const uint8_t SCANCODE_ESCAPE = 0xE0; const uint8_t SCANCODE_ESCAPE = 0xE0;
if ( scancode == SCANCODE_ESCAPE ) if ( scancode == SCANCODE_ESCAPE )
{ {
@ -146,48 +157,49 @@ namespace Sortix
return 0; return 0;
} }
int offset = (scancodeescaped) ? 0x80 : 0; int offset = scancodeescaped ? 0x80 : 0;
int kbkey = scancode & 0x7F; int kbkey = scancode & 0x7F;
if ( scancode & 0x80 ) { kbkey = -kbkey - offset; } kbkey = scancode & 0x80 ? -kbkey - offset : kbkey + offset;
else { kbkey = kbkey + offset; }
scancodeescaped = false; scancodeescaped = false;
// kbkey is now in the format specified in <sortix/keycodes.h>. // kbkey is now in the format specified in <sortix/keycodes.h>.
return kbkey; return kbkey;
} }
uint8_t PS2Keyboard::PopScancode() uint8_t PS2Keyboard::PopScancode()
{ {
return CPU::InPortB(iobase + DATA); return CPU::InPortB(iobase + DATA);
} }
void PS2Keyboard::UpdateLEDs(int ledval) void PS2Keyboard::UpdateLEDs(int ledval)
{ {
while ( (CPU::InPortB(iobase + STATUS) & (1<<1)) != 0 ) { } while ( (CPU::InPortB(iobase + STATUS) & (1<<1)) );
CPU::OutPortB(iobase + COMMAND, CMD_SETLED); CPU::OutPortB(iobase + COMMAND, CMD_SETLED);
while ( (CPU::InPortB(iobase + STATUS) & (1<<1)) != 0 ) { } while ( (CPU::InPortB(iobase + STATUS) & (1<<1)) );
CPU::OutPortB(iobase + COMMAND, ledval); CPU::OutPortB(iobase + COMMAND, ledval);
} }
void PS2Keyboard::SetOwner(KeyboardOwner* owner, void* user) void PS2Keyboard::SetOwner(KeyboardOwner* owner, void* user)
{ {
kthread_mutex_lock(&kblock); kthread_mutex_lock(&kblock);
this->owner = owner; this->owner = owner;
this->ownerptr = user; this->ownerptr = user;
kthread_mutex_unlock(&kblock); kthread_mutex_unlock(&kblock);
if ( queueused ) { NotifyOwner(); } if ( queueused )
} NotifyOwner();
}
bool PS2Keyboard::PushKey(int key) bool PS2Keyboard::PushKey(int key)
{ {
// Check if we need to allocate or resize the circular queue. // Check if we need to allocate or resize the circular queue.
if ( queueused == queuelength ) if ( queueused == queuelength )
{ {
size_t newqueuelength = (queuelength) ? queuelength * 2 : 32UL; size_t newqueuelength = (queuelength) ? queuelength * 2 : 32UL;
int* newqueue = new int[newqueuelength]; int* newqueue = new int[newqueuelength];
if ( !newqueue ) { return false; } if ( !newqueue )
return false;
size_t elemsize = sizeof(*queue); size_t elemsize = sizeof(*queue);
size_t leadingavai = queuelength-queueoffset; size_t leadingavai = queuelength-queueoffset;
size_t leading = (leadingavai < queueused) ? leadingavai : queueused; size_t leading = (leadingavai < queueused) ? leadingavai : queueused;
@ -202,32 +214,34 @@ namespace Sortix
queue[(queueoffset + queueused++) % queuelength] = key; queue[(queueoffset + queueused++) % queuelength] = key;
return true; return true;
} }
int PS2Keyboard::PopKey() int PS2Keyboard::PopKey()
{ {
if ( !queueused ) { return 0; } if ( !queueused )
return 0;
int kbkey = queue[queueoffset]; int kbkey = queue[queueoffset];
queueoffset = (queueoffset + 1) % queuelength; queueoffset = (queueoffset + 1) % queuelength;
queueused--; queueused--;
return kbkey; return kbkey;
} }
int PS2Keyboard::Read() int PS2Keyboard::Read()
{ {
ScopedLock lock(&kblock); ScopedLock lock(&kblock);
return PopKey(); return PopKey();
}
size_t PS2Keyboard::GetPending() const
{
ScopedLock lock(&kblock);
return queueused;
}
bool PS2Keyboard::HasPending() const
{
ScopedLock lock(&kblock);
return queueused;
}
} }
size_t PS2Keyboard::GetPending() const
{
ScopedLock lock(&kblock);
return queueused;
}
bool PS2Keyboard::HasPending() const
{
ScopedLock lock(&kblock);
return queueused;
}
} // namespace Sortix

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014.
This file is part of Sortix. This file is part of Sortix.
@ -25,14 +25,17 @@
#ifndef SORTIX_KB_PS2_H #ifndef SORTIX_KB_PS2_H
#define SORTIX_KB_PS2_H #define SORTIX_KB_PS2_H
#include <stddef.h>
#include <stdint.h>
#include <sortix/kernel/kthread.h> #include <sortix/kernel/kthread.h>
#include <sortix/kernel/keyboard.h> #include <sortix/kernel/keyboard.h>
namespace Sortix namespace Sortix {
class PS2Keyboard : public Keyboard
{ {
class PS2Keyboard : public Keyboard public:
{
public:
PS2Keyboard(uint16_t iobase, uint8_t interrupt); PS2Keyboard(uint16_t iobase, uint8_t interrupt);
virtual ~PS2Keyboard(); virtual ~PS2Keyboard();
virtual int Read(); virtual int Read();
@ -40,11 +43,11 @@ namespace Sortix
virtual bool HasPending() const; virtual bool HasPending() const;
virtual void SetOwner(KeyboardOwner* owner, void* user); virtual void SetOwner(KeyboardOwner* owner, void* user);
public: public:
void OnInterrupt(CPU::InterruptRegisters* regs); void OnInterrupt(CPU::InterruptRegisters* regs);
void InterruptWork(uint8_t scancode); void InterruptWork(uint8_t scancode);
private: private:
uint8_t PopScancode(); uint8_t PopScancode();
int DecodeScancode(uint8_t scancode); int DecodeScancode(uint8_t scancode);
void UpdateLEDs(int ledval); void UpdateLEDs(int ledval);
@ -52,7 +55,7 @@ namespace Sortix
int PopKey(); int PopKey();
void NotifyOwner(); void NotifyOwner();
private: private:
int* queue; int* queue;
size_t queuelength; size_t queuelength;
size_t queueoffset; size_t queueoffset;
@ -65,7 +68,8 @@ namespace Sortix
uint8_t leds; uint8_t leds;
mutable kthread_mutex_t kblock; mutable kthread_mutex_t kblock;
}; };
}
} // namespace Sortix
#endif #endif