aboutsummaryrefslogtreecommitdiff
path: root/ps2.h
blob: 0ff154b64174a8658032a8de20f11f1fed4e0df1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * VBMouse - Routines to access the PS2 BIOS services
 * Copyright (C) 2022 Javier S. Pedro
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef PS2_H
#define PS2_H

#include <stdbool.h>
#include <stdint.h>

enum {
	PS2M_STATUS_BUTTON_1 = 1 << 0,
	PS2M_STATUS_BUTTON_2 = 1 << 1,
	PS2M_STATUS_BUTTON_3 = 1 << 2,
	PS2M_STATUS_X_NEG    = 1 << 4,
	PS2M_STATUS_Y_NEG    = 1 << 5,
	PS2M_STATUS_X_OVF    = 1 << 6,
	PS2M_STATUS_Y_OVF    = 1 << 7,
};

#pragma aux PS2_CB far loadds parm reverse caller []
// TODO: ax and es look not be preserved with this. VBox BIOS already preserves them though.

/** Invoked by the BIOS when there is a mouse event.
 *  @param status combination of PS2M_STATUS_* flags */
typedef void (__far * LPFN_PS2CALLBACK)(uint8_t status, uint8_t x, uint8_t y, uint8_t z);

static inline void cli(void);
#pragma aux cli = "cli"

static inline void sti(void);
#pragma aux sti = "sti"

static uint8_t ps2_init(void);
#pragma aux ps2_init = \
	"stc"              /* If nothing happens, assume failure */ \
	"mov ax, 0xC205"   /* Pointing device: initialization */ \
	"mov bh, 3"        /* Use 3 byte packets */ \
	"int 0x15" \
	"jc fail" \
	\
	"mov ax, 0xC201"   /* Pointing device: reset */ \
	"int 0x15" \
	"jc fail" \
	\
	"mov ax, 0xC203"   /* Pointing device: set resolution */ \
	"mov bh, 3"        /* 3 count per mm = ~ 200 ppi */ \
	"int 0x15" \
	"jc fail" \
	\
	"mov ax, 0xC202"   /* Pointing device: set sample rate*/ \
	"mov bh, 5"        /* 2: 40 samples per second */ \
	"int 0x15" \
	"jc fail" \
	\
	"mov ax, 0xC206"   /* Pointing device: extended commands */ \
	"mov bh, 1"        /* Set 1:1 scaling */ \
	"int 0x15" \
	"jc fail" \
	\
	"mov ah, 0"        /* Success */ \
	"jmp end" \
	\
	"fail: test ah, ah" /* Ensure we have some error code back, set ah to ff if we don't */ \
	"jnz end" \
	"mov ah, 0xFF" \
	"end:" \
	__value [ah] \
	__modify [ax]

static uint8_t ps2_set_callback(LPFN_PS2CALLBACK callback);
#pragma aux ps2_set_callback = \
	"stc"              /* If nothing happens, assume failure */ \
	"mov ax, 0xC207"   /* Pointing device: set interrupt callback (in es:bx) */ \
	"int 0x15" \
	"jc fail" \
	\
	"mov ah, 0"        /* Success */ \
	"jmp end" \
	\
	"fail: test ah, ah" /* Ensure we have some error code back */ \
	"jnz end" \
	"mov ah, 0xFF" \
	"end:" \
	__parm [es bx] \
	__value [ah] \
	__modify [ax]

static uint8_t ps2_enable(bool enable);
#pragma aux ps2_enable = \
	"test bh, bh"      /* Ensure enable is either 1 or 0 */ \
	"setnz bh" \
	"stc"              /* If nothing happens, assume failure */ \
	"mov ax, 0xC200"   /* Pointing device enable/disable (in bh) */ \
	"int 0x15" \
	"jc fail" \
	\
	"mov ah, 0"        /* Success */ \
	"jmp end" \
	\
	"fail: test ah, ah" /* Ensure we have some error code back */ \
	"jnz end" \
	"mov ah, 0xFF" \
	"end:" \
	__parm [bh] \
	__value [ah] \
	__modify [ax]

#endif