]> Joshua Wise's Git repositories - netwatch.git/blob - aseg/counter.c
d6208d6545f753bfe6079f5ae031c1b150cdc25b
[netwatch.git] / aseg / counter.c
1 #include <io.h>
2 #include <smram.h>
3 #include <video_defines.h>
4 #include <minilib.h>
5 #include <smi.h>
6 #include <pci-bother.h>
7 #include "../net/net.h"
8 #include "vga-overlay.h"
9 #include "packet.h"
10 #include "keyboard.h"
11
12 unsigned int counter = 0;
13 unsigned int lastctr = 0;
14 unsigned long pcisave;
15 unsigned char vgasave;
16 static int curdev = 0;  /* 0 if kbd, 1 if mouse */
17
18 static void cause_kbd_irq()
19 {
20         outl(0x844, 0x0);
21         outl(0x848, 0x0);
22         while (inb(0x64) & 0x1)
23                 inb(0x60);
24         outb(0x60, 0xee);       /* Cause an IRQ. */
25         while (!(inb(0x64) & 0x1))
26                 ;
27         while (inb(0x64) & 0x1)
28                 inb(0x60);
29 }
30
31 void pci_dump() {
32         unsigned long cts;
33                 
34         cts = inl(0x84C);
35         
36         outl(0x840, 0x0);
37         outl(0x848, 0x0);
38         switch(cts&0xF0000)
39         {
40         case 0x20000:
41         {
42                 unsigned char b;
43                 
44                 switch (cts & 0xFFFF)
45                 {
46                 case 0x64:
47                         /* Read the real hardware and mask in our OBF if need be. */
48                         b = inb(0x64);
49                         if (kbd_has_injected_scancode())
50                         {
51                                 dologf("OS wants to know; we have data");
52                                 lastctr = counter;
53                                 b |= 0x01;
54                                 b &= ~0x20;     /* no mouse for you! */
55                                 curdev = 0;
56                         } else 
57                                 curdev = (b & 0x20) ? 1 : 0;
58                         *(unsigned char*)0xAFFD0 /* EAX */ = b;
59                         break;
60                 case 0x60:
61                         if (kbd_has_injected_scancode())
62                         {
63                                 b = kbd_get_injected_scancode();
64                                 lastctr = counter;
65                                 inb(0x60);
66                         } else
67                                 b = inb(0x60);
68                         if ((curdev == 0) && (b == 0x01)) {     /* Escape */
69                                 outb(0xCF9, 0x4);       /* Reboot */
70                                 return;
71                         }
72                         
73                         /* If there is more nus to come, generate another IRQ. */
74                         if (kbd_has_injected_scancode())
75                                 cause_kbd_irq();
76                         
77                         *(unsigned char*)0xAFFD0 /* EAX */ = b;
78                         break;
79                 }
80
81                 *(unsigned char*)0xAFFD0 /* EAX */ = b;
82                 break;
83         }
84         case 0x30000:
85         {
86                 unsigned char b;
87                 
88                 b = *(unsigned char*)0xAFFD0 /* EAX */;
89                 dologf("WRITE: %08x (%02x)", cts, b);
90                 outb(cts & 0xFFFF, b);
91                 break;
92         }
93         default:
94                 dolog("Unhandled PCI cycle");
95         }
96         
97         outl(0x844, 0x1000);
98         outl(0x848, 0x1000);
99 }
100
101 void timer_handler(smi_event_t ev)
102 {
103         static unsigned int ticks = 0;
104         
105         smi_disable_event(SMI_EVENT_FAST_TIMER);
106         smi_enable_event(SMI_EVENT_FAST_TIMER);
107         
108         if (kbd_has_injected_scancode() && (counter > (lastctr + 2)))
109         {
110                 smi_disable_event(SMI_EVENT_DEVTRAP_KBC);
111                 dolog("Kicking timer");
112                 cause_kbd_irq();
113                 smi_enable_event(SMI_EVENT_DEVTRAP_KBC);
114         }
115         
116         outb(0x80, (ticks++) & 0xFF);
117         
118         outlog();
119 }
120
121 void kbc_handler(smi_event_t ev)
122 {
123         pci_dump();
124 }
125
126 void gbl_rls_handler(smi_event_t ev)
127 {
128         unsigned long ecx;
129         
130         ecx = *(unsigned long*)0xAFFD4;
131
132         packet_t * packet = check_packet(ecx);
133         if (!packet)
134         {
135                 dologf("WARN: bad packet at %08x", ecx);
136                 return;
137         }
138
139         dologf("Got packet: type %08x", packet->type);
140
141         if (packet->type == 42) {
142                 dump_log((char *)packet->data);
143                 *(unsigned long*)0xAFFD4 = 42;
144         } else if (packet->type == 0xAA) {
145                 kbd_inject_key('A');
146         } else {
147                 *(unsigned long*)0xAFFD4 = 0x2BADD00D;
148         }
149 }
150
151 void smi_entry(void)
152 {
153         char statstr[512];
154         
155         pcisave = inl(0xCF8);
156         vgasave = inb(0x3D4);
157         pci_unbother_all();
158         
159         counter++;
160         sprintf(statstr, "15-412! %08x %08x", smi_status(), counter);
161         strblit(statstr, 0, 0);
162         
163         eth_poll();
164         
165         if (inl(0x840) & 0x1000)
166         {
167                 pci_dump();
168                 outl(0x840, 0x1100);
169                 outl(0x840, 0x0100);
170         }
171
172         smi_poll();
173         
174         pci_bother_all();
175         outl(0xCF8, pcisave);
176         outb(0x3D4, vgasave);
177 }
178
This page took 0.028021 seconds and 2 git commands to generate.