summaryrefslogtreecommitdiffhomepage
path: root/dev/ZKA/Modules/PS2/PS2MouseInterface.hxx
blob: 1cba256ea67ccef9084bbc2c5053f07fd6473f61 (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
/* -------------------------------------------

	Copyright ZKA Technologies.

	File: PS2MouseInterface.hxx
	Purpose: PS/2 mouse.

	Revision History:

	03/02/24: Added file (amlel)

------------------------------------------- */

#pragma once

#include <ArchKit/ArchKit.hxx>
#include <CompilerKit/CompilerKit.hxx>
#include <NewKit/Defines.hxx>

namespace Kernel
{
	/// @brief PS/2 Mouse driver interface
	class PS2MouseInterface final
	{
	public:
		explicit PS2MouseInterface() = default;
		~PS2MouseInterface()		 = default;

		ZKA_COPY_DEFAULT(PS2MouseInterface);

	public:
		/// @brief Enables PS2 mouse for kernel.
		/// @return
		Void Init() noexcept
		{
			HAL::rt_cli();

			HAL::Out8(0x64, 0xA8); // enabling the auxiliary device - mouse

			this->Wait();
			HAL::Out8(0x64, 0x20); // tells the keyboard controller that we want to send a command to the mouse
			this->WaitInput();

			UInt8 status = HAL::In8(0x60);
			status |= 0b10;

			this->Wait();
			HAL::Out8(0x64, 0x60);
			this->Wait();
			HAL::Out8(0x60, status); // setting the correct bit is the "compaq" status byte

			this->Write(0xF6);
			this->Read();

			this->Write(0xF4);
			this->Read();

			HAL::rt_sti();
		}

	public:
		Bool WaitInput() noexcept
		{
			UInt64 timeout = 100000;

			while (timeout)
			{
				if ((HAL::In8(0x64) & 0x1))
				{
					return true;
				}

				--timeout;
			} // wait until we can read

			// return the ack bit.
			return false;
		}

		Bool Wait() noexcept
		{
			UInt64 timeout = 100000;

			while (timeout)
			{
				if ((HAL::In8(0x64) & 0b10) == 0)
				{
					return true;
				}

				--timeout;
			} // wait until we can read

			// return the ack bit.
			return false;
		}

		Void Write(UInt8 val)
		{
			HAL::Out8(0x64, 0xD4);
			this->Wait();
			HAL::Out8(0x60, val);
			this->Wait();
		}

		UInt8 Read()
		{
			this->WaitInput();
			return HAL::In8(0x60);
		}
	};
} // namespace Kernel