From: John Hodge Date: Sun, 25 Sep 2011 09:17:09 +0000 (+0800) Subject: Modules/PS2KbMouse - Broke 8042 specific code out X-Git-Tag: rel0.11~61 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=f31fce99ba695db4d5b4509bfcf5d0c568d02af9;p=tpg%2Facess2.git Modules/PS2KbMouse - Broke 8042 specific code out - Incomplete PL050 support --- diff --git a/Modules/Input/PS2KbMouse/8042.c b/Modules/Input/PS2KbMouse/8042.c new file mode 100644 index 00000000..7d032c56 --- /dev/null +++ b/Modules/Input/PS2KbMouse/8042.c @@ -0,0 +1,102 @@ +/* + * Acess2 + * - By thePowersGang (John Hodge) + * + * 8042 (or comaptible) Driver + */ +#include +#include "common.h" + +// === PROTOTYPES === +void KBC8042_Init(void); +void KBC8042_KeyboardHandler(int IRQ); +void KBC8042_MouseHandler(int IRQ); +void KBC8042_EnableMouse(void); +static inline void KBC8042_SendDataAlt(Uint8 data); +static inline void KBC8042_SendData(Uint8 data); +static inline Uint8 KBC8042_ReadData(void); +static void KBC8042_SendMouseCommand(Uint8 cmd); + +// === CODE === +void KBC8042_Init(void) +{ + IRQ_AddHandler(1, KBC8042_KeyboardHandler); + IRQ_AddHandler(12, KBC8042_MouseHandler); // Set IRQ + + { + Uint8 temp; + // Attempt to get around a strange bug in Bochs/Qemu by toggling + // the controller on and off + temp = inb(0x61); + outb(0x61, temp | 0x80); + outb(0x61, temp & 0x7F); + inb(0x60); // Clear keyboard buffer + } +} + +void KBC8042_KeyboardHandler(int IRQ) +{ + Uint8 scancode; + + scancode = inb(0x60); + KB_HandleScancode( scancode ); +} + +void KBC8042_MouseHandler(int IRQ) +{ + PS2Mouse_HandleInterrupt( inb(0x60) ); +} + +void KBC8042_SetLEDs(Uint8 leds) +{ + while( inb(0x64) & 2 ); // Wait for bit 2 to unset + outb(0x60, 0xED); // Send update command + + while( inb(0x64) & 2 ); // Wait for bit 2 to unset + outb(0x60, leds); +} + +void KBC8042_EnableMouse(void) +{ + Uint8 status; + Log_Log("8042", "Enabling Mouse..."); + + // Enable AUX PS/2 + KBC8042_SendDataAlt(0xA8); + + // Enable AUX PS/2 (Compaq Status Byte) + KBC8042_SendDataAlt(0x20); // Send Command + status = KBC8042_ReadData(); // Get Status + status &= ~0x20; // Clear "Disable Mouse Clock" + status |= 0x02; // Set IRQ12 Enable + KBC8042_SendDataAlt(0x60); // Send Command + KBC8042_SendData(status); // Set Status + + //mouseSendCommand(0xF6); // Set Default Settings + KBC8042_SendMouseCommand(0xF4); // Enable Packets +} + +static inline void KBC8042_SendDataAlt(Uint8 data) +{ + int timeout=100000; + while( timeout-- && inb(0x64) & 2 ); // Wait for Flag to clear + outb(0x64, data); // Send Command +} +static inline void KBC8042_SendData(Uint8 data) +{ + int timeout=100000; + while( timeout-- && inb(0x64) & 2 ); // Wait for Flag to clear + outb(0x60, data); // Send Command +} +static inline Uint8 KBC8042_ReadData(void) +{ + int timeout=100000; + while( timeout-- && (inb(0x64) & 1) == 0); // Wait for Flag to set + return inb(0x60); +} +static inline void KBC8042_SendMouseCommand(Uint8 cmd) +{ + KBC8042_SendDataAlt(0xD4); + KBC8042_SendData(cmd); +} + diff --git a/Modules/Input/PS2KbMouse/Makefile b/Modules/Input/PS2KbMouse/Makefile index e30e472c..2500c02c 100644 --- a/Modules/Input/PS2KbMouse/Makefile +++ b/Modules/Input/PS2KbMouse/Makefile @@ -2,6 +2,7 @@ # OBJ = main.o kb.o ps2mouse.o +OBJ += 8042.o NAME = PS2KbMouse -include ../Makefile.tpl diff --git a/Modules/Input/PS2KbMouse/common.h b/Modules/Input/PS2KbMouse/common.h new file mode 100644 index 00000000..f7fa36b6 --- /dev/null +++ b/Modules/Input/PS2KbMouse/common.h @@ -0,0 +1,21 @@ +/* + * Acess2 + * + * PS2 Keyboard/Mouse Driver + * + * common.h + * - Shared definitions + */ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +extern int KB_Install(char **Arguments); +extern int PS2Mouse_Install(char **Arguments); + +extern void KBC8042_Init(void); +extern void KBC8042_EnableMouse(void); + +extern void KB_HandleScancode(Uint8 scancode); +extern void PS2Mouse_HandleInterrupt(Uint8 InputByte); + +#endif diff --git a/Modules/Input/PS2KbMouse/kb.c b/Modules/Input/PS2KbMouse/kb.c index db0ee220..e53f0930 100644 --- a/Modules/Input/PS2KbMouse/kb.c +++ b/Modules/Input/PS2KbMouse/kb.c @@ -20,9 +20,7 @@ extern void Heap_Stats(void); // === PROTOTYPES === int KB_Install(char **Arguments); -void KB_IRQHandler(int IRQNum); -void KB_AddBuffer(char ch); -Uint64 KB_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Dest); +void KB_HandleScancode(Uint8 scancode); void KB_UpdateLEDs(void); int KB_IOCtl(tVFS_Node *Node, int Id, void *Data); @@ -32,7 +30,6 @@ tDevFS_Driver gKB_DevInfo = { { .NumACLs = 0, .Size = 0, - //.Read = KB_Read, .IOCtl = KB_IOCtl } }; @@ -55,19 +52,6 @@ Uint8 gbaKB_States[3][256]; */ int KB_Install(char **Arguments) { - - IRQ_AddHandler(1, KB_IRQHandler); - - { - Uint8 temp; - // Attempt to get around a strange bug in Bochs/Qemu by toggling - // the controller on and off - temp = inb(0x61); - outb(0x61, temp | 0x80); - outb(0x61, temp & 0x7F); - inb(0x60); // Clear keyboard buffer - } - DevFS_AddDevice( &gKB_DevInfo ); //Log("KB_Install: Installed"); return MODULE_ERR_OK; @@ -77,16 +61,10 @@ int KB_Install(char **Arguments) * \brief Called on a keyboard IRQ * \param IRQNum IRQ number (unused) */ -void KB_IRQHandler(int IRQNum) +void KB_HandleScancode(Uint8 scancode) { - Uint8 scancode; Uint32 ch; - // int keyNum; - - // Check port 0x64 to tell if this is from the aux port - //if( inb(0x64) & 0x20 ) return; - scancode = inb(0x60); // Read from the keyboard's data buffer //Log_Debug("Keyboard", "scancode = %02x", scancode); // Ignore ACKs @@ -260,11 +238,8 @@ void KB_UpdateLEDs(void) leds = (gbKB_CapsState ? 4 : 0); - while( inb(0x64) & 2 ); // Wait for bit 2 to unset - outb(0x60, 0xED); // Send update command - - while( inb(0x64) & 2 ); // Wait for bit 2 to unset - outb(0x60, leds); + // TODO: Update LEDS + Log_Warning("Keyboard", "TODO: Update LEDs"); } static const char *csaIOCTL_NAMES[] = {DRV_IOCTLNAMES, DRV_KEYBAORD_IOCTLNAMES, NULL}; diff --git a/Modules/Input/PS2KbMouse/main.c b/Modules/Input/PS2KbMouse/main.c index 344c59ea..e3ea35cf 100644 --- a/Modules/Input/PS2KbMouse/main.c +++ b/Modules/Input/PS2KbMouse/main.c @@ -5,10 +5,9 @@ */ #include #include +#include "common.h" // === IMPORTS === -extern int KB_Install(char **Arguments); -extern int PS2Mouse_Install(char **Arguments); // === PROTOTYPES === int PS2_Install(char **Arguments); @@ -21,7 +20,6 @@ MODULE_DEFINE(0, 0x0100, PS2Mouse, PS2Mouse_Install, NULL, NULL); // === CODE === int PS2_Install(char **Arguments) { -// KB_Install(Arguments); -// Mouse_Install(Arguments); + KBC8042_Init(); return MODULE_ERR_OK; } diff --git a/Modules/Input/PS2KbMouse/pl050.c b/Modules/Input/PS2KbMouse/pl050.c new file mode 100644 index 00000000..f4b31986 --- /dev/null +++ b/Modules/Input/PS2KbMouse/pl050.c @@ -0,0 +1,104 @@ +/* + * Acess2 + * - By thePowersGang (John Hodge) + * + * PL050 (or comaptible) Driver + */ +#include +#include "common.h" + +// TODO: Allow runtime/compile-time switching +// Maybe PCI will have it? +// Integrator-CP +#if 0 +#define KEYBOARD_IRQ 3 +#define KEYBOARD_BASE 0x18000000 +#define MOUSE_IRQ 4 +#define MOUSE_BASE 0x19000000 +#endif +// Realview +#if 1 +#define KEYBOARD_IRQ 20 +#define KEYBOARD_BASE 0x10006000 +#define MOUSE_IRQ 21 +#define MOUSE_BASE 0x10007000 +#endif + +#define PL050_TXBUSY 0x20 + +// === PROTOTYPES === +void PL050_Init(void); +void PL050_KeyboardHandler(int IRQ); +void PL050_MouseHandler(int IRQ); +void PL050_EnableMouse(void); +static inline void PL050_WriteMouseData(Uint8 data); +static inline void PL050_WriteKeyboardData(Uint8 data); +static inline Uint8 PL050_ReadMouseData(void); +static inline Uint8 PL050_ReadKeyboardData(void); + +// === CODE === +void PL050_Init(void) +{ + IRQ_AddHandler(KEYBOARD_IRQ, PL050_KeyboardHandler); + IRQ_AddHandler(MOUSE_IRQ, PL050_MouseHandler); // Set IRQ +} + +void PL050_KeyboardHandler(int IRQ) +{ + Uint8 scancode; + + scancode = PL050_ReadKeyboardData(0x60); + KB_HandleScancode( scancode ); +} + +void PL050_MouseHandler(int IRQ) +{ + PS2Mouse_HandleInterrupt( PL050_ReadMouseData(0x60) ); +} + +void PL050_SetLEDs(Uint8 leds) +{ + PL050_WriteKeyboardData(0xED); + PL050_WriteKeyboardData(leds); +} + +void PL050_EnableMouse(void) +{ + Uint8 status; + Log_Log("8042", "Enabling Mouse..."); + + + //PL050_WriteMouseData(0xD4); + //PL050_WriteMouseData(0xF6); // Set Default Settings + PL050_WriteMouseData(0xD4); + PL050_WriteMouseData(0xF4); // Enable Packets +} + +static inline void PL050_WriteMouseData(Uint8 Data) +{ + int timeout = 10000; + while( --timeout && *(Uint32*)(MOUSE_BASE+1) & PL050_TXBUSY ); + if(timeout) + *(Uint32*)(MOUSE_BASE+2) = Data; + else + Log_Error("PL050", "Write to mouse timed out"); +} + +static inline Uint8 PL050_ReadMouseData(void) +{ + return *(Uint32*)(MOUSE_BASE+2); +} +static inline void PL050_WriteKeyboardData(Uint8 data) +{ + int timeout = 10000; + while( --timeout && *(Uint32*)(KEYBOARD_BASE+1) & PL050_TXBUSY ); + if(timeout) + *(Uint32*)(KEYBOARD_BASE+2) = Data; + else + Log_Error("PL050", "Write to keyboard timed out"); +} +static inline Uint8 PL050_ReadKeyboardData(void) +{ + return *(Uint32*)(MOUSE_BASE+2); +} + diff --git a/Modules/Input/PS2KbMouse/ps2mouse.c b/Modules/Input/PS2KbMouse/ps2mouse.c index cb26efe5..7c831f76 100644 --- a/Modules/Input/PS2KbMouse/ps2mouse.c +++ b/Modules/Input/PS2KbMouse/ps2mouse.c @@ -8,6 +8,7 @@ #include #include #include +#include "common.h" static inline int MIN(int a, int b) { return (a < b) ? a : b; } static inline int MAX(int a, int b) { return (a > b) ? a : b; } @@ -15,14 +16,11 @@ static inline int MAX(int a, int b) { return (a > b) ? a : b; } // == CONSTANTS == #define NUM_AXIES 2 // X+Y #define NUM_BUTTONS 5 // Left, Right, Scroll Click, Scroll Up, Scroll Down -#define PS2_IO_PORT 0x60 // == PROTOTYPES == // - Internal - int PS2Mouse_Install(char **Arguments); -void PS2Mouse_IRQ(int Num); -static void mouseSendCommand(Uint8 cmd); -void PS2Mouse_Enable(); +void PS2Mouse_HandleInterrupt(Uint8 InputByte); // - Filesystem - Uint64 PS2Mouse_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer); int PS2Mouse_IOCtl(tVFS_Node *Node, int ID, void *Data); @@ -59,9 +57,8 @@ int PS2Mouse_Install(char **Arguments) gMouse_Buttons = (void*)&gMouse_Axies[NUM_AXIES]; // Initialise Mouse Controller - IRQ_AddHandler(12, PS2Mouse_IRQ); // Set IRQ giMouse_Cycle = 0; // Set Current Cycle position - PS2Mouse_Enable(); // Enable the mouse + KBC8042_EnableMouse(); DevFS_AddDevice(&gMouse_DriverStruct); @@ -70,14 +67,14 @@ int PS2Mouse_Install(char **Arguments) /* Handle Mouse Interrupt */ -void PS2Mouse_IRQ(int Num) +void PS2Mouse_HandleInterrupt(Uint8 InputByte) { Uint8 flags; int d[2], d_accel[2]; int i; // Gather mouse data - gaMouse_Bytes[giMouse_Cycle] = inb(0x60); + gaMouse_Bytes[giMouse_Cycle] = InputByte; LOG("gaMouse_Bytes[%i] = 0x%02x", gMouse_Axies[0].MaxValue); // - If bit 3 of the first byte is not set, it's not a valid packet? if(giMouse_Cycle == 0 && !(gaMouse_Bytes[0] & 0x08) ) @@ -137,11 +134,8 @@ void PS2Mouse_IRQ(int Num) gMouse_Axies[i].CurValue = d_accel[i]; gMouse_Axies[i].CursorPos = newCursor; } - -// Log_Debug("PS2Mouse", "gMouse_Buttons = {0x%x,0x%x,0x%x}, gMouse_Axies={%i,%i}", -// gMouse_Buttons[0], gMouse_Buttons[1], gMouse_Buttons[2], -// gMouse_Axies[0].CursorPos, gMouse_Axies[1].CursorPos); - + + VFS_MarkAvaliable(&gMouse_DriverStruct.RootNode, 1); } @@ -201,47 +195,3 @@ int PS2Mouse_IOCtl(tVFS_Node *Node, int ID, void *Data) } } -//== Internal Functions == -static inline void mouseOut64(Uint8 data) -{ - int timeout=100000; - while( timeout-- && inb(0x64) & 2 ); // Wait for Flag to clear - outb(0x64, data); // Send Command -} -static inline void mouseOut60(Uint8 data) -{ - int timeout=100000; - while( timeout-- && inb(0x64) & 2 ); // Wait for Flag to clear - outb(0x60, data); // Send Command -} -static inline Uint8 mouseIn60(void) -{ - int timeout=100000; - while( timeout-- && (inb(0x64) & 1) == 0); // Wait for Flag to set - return inb(0x60); -} -static inline void mouseSendCommand(Uint8 cmd) -{ - mouseOut64(0xD4); - mouseOut60(cmd); -} - -void PS2Mouse_Enable(void) -{ - Uint8 status; - Log_Log("PS2Mouse", "Enabling Mouse..."); - - // Enable AUX PS/2 - mouseOut64(0xA8); - - // Enable AUX PS/2 (Compaq Status Byte) - mouseOut64(0x20); // Send Command - status = mouseIn60(); // Get Status - status &= ~0x20; // Clear "Disable Mouse Clock" - status |= 0x02; // Set IRQ12 Enable - mouseOut64(0x60); // Send Command - mouseOut60(status); // Set Status - - //mouseSendCommand(0xF6); // Set Default Settings - mouseSendCommand(0xF4); // Enable Packets -}