]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/hw/8042/8042.h
added a bunch of things~ and midi stuff~
[16.git] / src / lib / doslib / hw / 8042 / 8042.h
1 /* 8042.h
2  *
3  * Intel 8042 keyboard controller library.
4  * (C) 2008-2012 Jonathan Campbell.
5  * Hackipedia DOS library.
6  *
7  * This code is licensed under the LGPL.
8  * <insert LGPL legal text here>
9  *
10  */
11
12 #ifndef __HW_8042_8042_H
13 #define __HW_8042_8042_H
14
15 #include <hw/cpu/cpu.h>
16 #include <stdint.h>
17
18 #define K8042_STATUS                    0x64    /* (R) */
19 #define K8042_COMMAND                   0x64    /* (W) */
20 #define K8042_DATA                      0x60    /* (R/W) */
21
22 /* status bits */
23 #define K8042_STATUS_OUTPUT_FULL        0x01
24 #define K8042_STATUS_INPUT_FULL         0x02
25 #define K8042_STATUS_SYSTEM             0x04
26 #define K8042_STATUS_DATA_WRITE         0x08    /* 1=next write to 0x64  0=next write to 0x60 */
27 #define K8042_STATUS_INHIBIT            0x10
28 #define K8042_STATUS_XMIT_TIMEOUT       0x20
29 #define K8042_STATUS_MOUSE_OUTPUT_FULL  0x20
30 #define K8042_STATUS_RECV_TIMEOUT       0x40
31 #define K8042_STATUS_PARITY_ERR         0x80
32
33 #define K8042_IRQ                       1
34 #define K8042_MOUSE_IRQ                 12
35
36 int k8042_probe();
37 int k8042_probe_aux();
38 void k8042_drain_buffer();
39 int k8042_write_aux_synaptics_E8(unsigned char c);
40 int k8042_write_aux_synaptics_mode(unsigned char c);
41
42 extern unsigned char                    k8042_flags;
43 extern unsigned char                    k8042_last_status;
44
45 #define K8042_F_AUX                     0x01
46
47 /* NTS: Do not confuse our input with the controller's input. This reflects the controller's input
48  *      meaning: Can we, the host, OUTPUT DATA to the CONTROLLER'S INPUT? */
49 static inline unsigned char k8042_wait_for_input_buffer() {
50         unsigned int patience = 0xFFFFU;
51         unsigned char c;
52
53         do { c = inp(K8042_STATUS);
54         } while ((c&2) && --patience != 0U);
55         k8042_last_status = c;
56         return (c & 2) == 0;
57 }
58
59 /* NTS: Do not confue our output with the controller's output. This reflects the controller's output
60  *      meaning: Can we, the host, INPUT DATA from the CONTROLLER'S OUTPUT? */
61 static inline unsigned char k8042_wait_for_output() {
62         unsigned int patience = 0xFFFFU;
63         unsigned char c;
64
65         do { c = inp(K8042_STATUS);
66         } while ((c&1) == 0 && --patience != 0U);
67         k8042_last_status = c;
68         return (c & 1);
69 }
70
71 static inline unsigned char k8042_is_output_ready() {
72         return ((k8042_last_status = inp(K8042_STATUS)) & K8042_STATUS_OUTPUT_FULL);
73 }
74
75 static inline unsigned char k8042_is_mouse_output_ready() {
76         return ((k8042_last_status = inp(K8042_STATUS)) & (K8042_STATUS_OUTPUT_FULL|K8042_STATUS_MOUSE_OUTPUT_FULL)) ==
77                 (K8042_STATUS_OUTPUT_FULL|K8042_STATUS_MOUSE_OUTPUT_FULL);
78 }
79
80 static inline unsigned char k8042_output_was_aux() {
81         return (k8042_flags & K8042_F_AUX) != 0 && (k8042_last_status & K8042_STATUS_MOUSE_OUTPUT_FULL) != 0;
82 }
83
84 /* WARNING: caller is expected to use k8042_wait_for_output() prior to calling this */
85 static inline unsigned char k8042_read_output() {
86         return inp(K8042_DATA);
87 }
88
89 static inline int k8042_read_output_wait() {
90         if (k8042_wait_for_output() || k8042_wait_for_output() || k8042_wait_for_output())
91                 return k8042_read_output();
92         else
93                 return -1;
94 }
95
96 static inline unsigned char k8042_write_command(unsigned char c) {
97         unsigned char r = k8042_wait_for_input_buffer();
98         if (r) outp(K8042_COMMAND,c);
99         return r;
100 }
101
102 static inline unsigned char k8042_write_data(unsigned char c) {
103         unsigned char r = k8042_wait_for_input_buffer();
104         if (r) outp(K8042_DATA,c);
105         return r;
106 }
107
108 static inline int k8042_read_command_byte() {
109         if (k8042_write_command(0x20))
110                 return k8042_read_output_wait();
111         return -1;
112 }
113
114 static inline unsigned char k8042_write_command_byte(unsigned char c) {
115         if (k8042_write_command(0x60) && k8042_write_data(c))
116                 return 1;
117
118         return 0;
119 }
120
121 static inline void k8042_disable_keyboard() {
122         k8042_write_command(0xAD); /* disable keyboard */
123 }
124
125 static inline void k8042_enable_keyboard() {
126         k8042_write_command(0xAE); /* enable keyboard */
127 }
128
129 static inline void k8042_disable_aux() {
130         k8042_write_command(0xA7); /* disable aux */
131 }
132
133 static inline void k8042_enable_aux() {
134         k8042_write_command(0xA8); /* enable aux */
135 }
136
137 static inline unsigned char k8042_write_aux(unsigned char c) {
138         return (k8042_write_command(0xD4) && k8042_write_data(c));
139 }
140
141 #endif /* __HW_8042_8042_H */
142