Modules/PS2KbMouse - (minor) Cleanup 8042 driver a little
[tpg/acess2.git] / KernelLand / Modules / Input / PS2KbMouse / 8042.c
1 /*
2  * Acess2
3  * - By thePowersGang (John Hodge)
4  *
5  * 8042 (or comaptible) Driver
6  */
7 #include <acess.h>
8 #include "common.h"
9
10 // === PROTOTYPES ===
11 void    KBC8042_Init(void);
12 void    KBC8042_KeyboardHandler(int IRQ, void *Ptr);
13 void    KBC8042_MouseHandler(int IRQ, void *Ptr);
14 void    KBC8042_EnableMouse(void);
15 static inline void      KBC8042_SendDataAlt(Uint8 data);
16 static inline void      KBC8042_SendData(Uint8 data);
17 static inline Uint8     KBC8042_ReadData(void);
18 static void     KBC8042_SendMouseCommand(Uint8 cmd);
19
20 // === CODE ===
21 void KBC8042_Init(void)
22 {
23         IRQ_AddHandler(1, KBC8042_KeyboardHandler, NULL);
24         IRQ_AddHandler(12, KBC8042_MouseHandler, NULL); // Set IRQ
25         
26         {
27                 // Attempt to get around a strange bug in Bochs/Qemu by toggling
28                 // the controller on and off
29                 Uint8 temp = inb(0x61);
30                 outb(0x61, temp | 0x80);
31                 outb(0x61, temp & 0x7F);
32                 inb(0x60);      // Clear keyboard buffer
33         }
34 }
35
36 void KBC8042_KeyboardHandler(int IRQ, void *Ptr)
37 {
38         Uint8   scancode = inb(0x60);
39         KB_HandleScancode( scancode );
40 }
41
42 void KBC8042_MouseHandler(int IRQ, void *Ptr)
43 {
44         PS2Mouse_HandleInterrupt( inb(0x60) );
45 }
46
47 void KBC8042_SetLEDs(Uint8 leds)
48 {
49         while( inb(0x64) & 2 ); // Wait for bit 2 to unset
50         outb(0x60, 0xED);       // Send update command
51
52         while( inb(0x64) & 2 ); // Wait for bit 2 to unset
53         outb(0x60, leds);
54 }
55
56 void KBC8042_EnableMouse(void)
57 {
58         Uint8   status;
59         Log_Log("8042", "Enabling Mouse...");
60         
61         // Enable AUX PS/2
62         KBC8042_SendDataAlt(0xA8);
63         
64         // Enable AUX PS/2 (Compaq Status Byte)
65         KBC8042_SendDataAlt(0x20);      // Send Command
66         status = KBC8042_ReadData();    // Get Status
67         status &= ~0x20;        // Clear "Disable Mouse Clock"
68         status |= 0x02;         // Set IRQ12 Enable
69         KBC8042_SendDataAlt(0x60);      // Send Command
70         KBC8042_SendData(status);       // Set Status
71         
72         //mouseSendCommand(0xF6);       // Set Default Settings
73         KBC8042_SendMouseCommand(0xF4); // Enable Packets
74 }
75
76 static inline void KBC8042_SendDataAlt(Uint8 data)
77 {
78         int timeout=100000;
79         while( timeout-- && inb(0x64) & 2 );    // Wait for Flag to clear
80         outb(0x64, data);       // Send Command
81 }
82 static inline void KBC8042_SendData(Uint8 data)
83 {
84         int timeout=100000;
85         while( timeout-- && inb(0x64) & 2 );    // Wait for Flag to clear
86         outb(0x60, data);       // Send Command
87 }
88 static inline Uint8 KBC8042_ReadData(void)
89 {
90         int timeout=100000;
91         while( timeout-- && (inb(0x64) & 1) == 0);      // Wait for Flag to set
92         return inb(0x60);
93 }
94 static inline void KBC8042_SendMouseCommand(Uint8 cmd)
95 {
96         KBC8042_SendDataAlt(0xD4);
97         KBC8042_SendData(cmd);
98 }
99

UCC git Repository :: git.ucc.asn.au