From OSDev Wiki
Jump to: navigation, search


ps2 mouse

This page is under construction , and you will might see it changes much. ( this will be removed when im finish; reason: to insure a backup of my work.)

There already exists a page on this subject. This page will be deleted when all the information is present in the mentioned page. Do not add anything here.

Programming the ps2 mouse

activation the mouse (aux port)

This is done using the keyboard controller *, using the command controller byte. Inside this byte, there are a few bits telling if the aux is activated or not.
The controller command byte looks like this (when we only are talking about the mouse):

AUX controller IRQ 12

The aux controller controls if the aux is enabled or not, so setting this bit is needed. The IRQ 12 bit is recommended, but you can use the mouse without, but turn this on too. So to make the aux active you will have to do the following sequence:

  • wait until keyboard controller is ready to get commands (*)
  • write 0x20 to the keyboard controller (port 0x64)
  • wait until data is ready
  • read the data
  • set bit 5 and 1
  • wait until keyboard controller is ready to get commands
  • write 0x60 to the keyboard controller (port 0x64)
  • wait until it can accept data
  • write the new controller command byte to data port (0x60)

That is it. Remember to check for ack (0xFA, this is not documented to happen, but might happen on some commands ...)

(*) Keyboard controller: As reference, use my other document “ps2 controller and keyboard” found at:

Detection mouse

Detecting a mouse is not so hard after all, the only thing you need to, is reading the ID byte. The ID should be between 0 and 4. However 0 should be the one you will get. If you get something like 0xAB, then it is mostly a keyboard, and not a mouse ID.

Sending commands to the mouse

When you want to “talk” to the mouse, you will have to send one command to the keyboard controller, and then the keyboard controller knows that you will talk to the mouse. This command is “0xD4”, and after sending it, you will have to wait until you can send data to the data port (port 0x60), and the data is the command to the mouse. The mouse might answer with ACK on most commands, but maybe not. There are some few mouse commands, that request data after the command, the data need to be sent if it was a command (first 0xD4 to controller, then the data to data port (0x60)

A good advice, look at the PS2 controller before programming the mouse.


To use the mouse, you will have to do some few things, first activating the AUX port, and the IRQ 12 (that is also described under the activation subject). After that you can get data, be polling the information from the mouse, however if you want to be able to use IRQ 12, then you will have to enable packet reporting.

Initialization features

When a mouse is normally started up (not already init), it is only limited to the standard features, which is: movement and 3 buttons. But nowadays mice (PS2) almost all have a scroll wheel, so in order to make that one work, you will have to send a special sequence of instructions. But that is not the end of the extended features, since some mice have 2 scroll wheels, so there is also a special sequence for that one, so if you want to enable the “extended extended features” then you will have to send the other sequence after the other one.

The first sequence goes like this:

  • set sample to 200
  • set sample to 100
  • set sample to 80
  • get the id

If the id has changed, then it is scroll mouse, and it will now report 1 more packet than before. The second sequence goes like this:

  • set sample to 200
  • set sample to 200
  • set sample to 80
  • get the id

If the id has changed, then it is an extended scroll mouse, but it is the same amount of packets, but the meaning of the last packet has changed.

Getting packets

If you move the mouse, it will generate packets, if you click the mouse, it generates packets too, but if data reporting is disabled, the packets will not be sent to IRQ 12 ( which also must be activated) But there is a command to read the data, without using IRQ 12.(see the commands)

If you want to use the IRQ 12 solution, then this is a part you will need to read. First, be sure to map the IRQ 12, and also enable it inside the keyboard controller, but also inside the PIC(programmable interrupt controller) or what else is controlling IRQ’s. second is that you will need to keep track of how many packets the mouse is ought to send, as for a mouse without any extended features add, it is 3 packets, meaning 3 IRQ 12 ! So when a IRQ Fires, you will also need to save the packet, and wait for the other packets to come.

The packets

The meaning of the packets is like this:

byte 1:

Y overflow X overflow Y sign bit X sign bit Always 1 Middle Btn Left Btn Right Btn

byte 2:

X movement

byte 3:

Y movement

If one scroll wheel is enabled:
byte 4:

Z movement

If both scroll wheels is enabled:
byte 4:

Always 0 Always 0 5th btn 4th btn Z3 Z2 Z1 Z0

Here is Z3 though Z0 is a signed value, of the Z movement (scroll) if it is increased/ decreased with 2, then it is the vertical scroll wheel, else it is just the horizontal scroll wheel.
If a button is pressed, then the value for the button will be “1”, and when not pressed “0”.


Since some BIOS allow USB to be emulated to PS2, then you might have to think about this too. However the Emulated USB device, will (Mostly) be another IRQ (the USB IRQ) than IRQ 12.

Mouse commands

Hex value Meaning Describing
0xFF reset The mouse sends ACK (0xFA) and resets.
0xFE resend this command makes the mouse sends its second last packet to the host again.
0xF6 Set Defaults The mouse sends ACK (0xFA) and applies default values
0xF5 Disable Data Reporting The mouse sends ACK (0xFA) and disable data reporting.
0xF4 enable Data Reporting The mouse sends ACK (0xFA) and enables data reporting
0xF3 Set Sample Rate The mouse sends ACK (0xFA) and waits for the next data, written to the mouse, to be the sample rate
0xF2 Get Device ID The mouse sends ACK (0xFA) and sends its ID, though the DATA port
0xF0 Set Remote Mode The mouse sends ACK (0xFA) and then reset its movement counters, and enters remote mode
0xEE Set Wrap Mode The mouse sends ACK (0xFA) and then reset its movement counters, and enters warp mode
0xEC Reset Wrap Mode The mouse sends ACK, and then enters the last mode, before entering wrap mode, it also resets its movement counters
0xEB Read Data The mouse sends ACK, followed by the first packet, and so on, until It have sent all.
0xEA Set Stream Mode The mouse sends ACK (0xFA) and then reset its movement counters, and enters reporting mode
0xE9 Status Request The mouse sends ACK, and sends afterwards the status bytes ( 3 bytes in a row, remember to wait each time for new data)
0xE8 Set Resolution The mouse sends ACK and waits for the next data, written to the mouse, to be the resolution
0xE7 Set Scaling 2:1 The mouse sends ACK and set scaling 2:1
0xE6 Set Scaling 1:1 The mouse sends ACK and set scaling 1:1

The status byte looks like this:
Byte 1:

Always 0 mode enable scaling Always 0 left btn middle right btn

Byte 2:


Byte 3:

sample rate

Mode: if it is 1, the current mode is remote mode; if 0 then it is stream mode
Enable: if it is 1, then data reporting is enabled; if 0 then data reporting is disabled
Scaling: if it is 1, scaling 2:1 is enabled; if 0 then scaling 1:1 is enabled.


There are 3 modes:
Warp mode, reporting mode, remote mode.

  • Warp mode is a funny one, since it just sends the same back, every time something happens. It sends 0xEE back.
  • Reporting mode is the normal used one, it is here the mouse sends data each time you move / click the mouse.
  • Remote mode is a mode, where you keep polling the data from the mouse ( about movement and click)

Resolution, Scaling and Sampling rate

Those is some options about the mouse, when you move it, and about how many packets it can make pr second. Resolution: this is how many packets pr millimeter movement. Scaling: is a change to the packet reported for each millimeter. Sampling rate: this is how many packets the mouse can send pr second. Those “options” can (of cause) have different value, so let’s sum them up: Resolution:

value resolution
0x00 1 count /mm
0x01 2 count /mm
0x02 4 count /mm
0x03 8 count /mm

For scaling, there is not many possibilities, since it can either be “1:1” or “2:1”, but looking at the scheme (below) we can see what happens if it is “2:1”

Movement Counter Reported Movement
0 0
1 1
2 1
3 3
4 6
5 9
more than 5 2 * Movement Counter

Sampling rate can have the following values: (all values is Decimal, NOT hex)

value Samples pr second
10 10
20 20
40 40
60 60
80 80
100 100
200 200

Resetting and hot plugging

When a mouse is powered up, (can either be done from software reset, or by hardware, hot plugging too) the mouse will enters a default state (the same as the command “default”), which is those following values:

  • sampling rate: 100 samples pr second
  • scaling: 1:1
  • resolution: 4 counts pr mm
  • mode: data reporting
  • data reporting: disabled

But before that, the mouse sends its self test, which either can be 0xAA (successful) or 0xFC (error), after receiving 0xaa, the host will get the ID. This also applies to hot plugging, so in the IRQ 12 handler, be sure to test for 0xaa (if this comes as the first packet), and re-init it after getting the ID.

This except the “self test” and ID is applied when normally starting the computer.

A note on hot plugging: since it all depends on the BIOS, there may be computers, where hot plugging is not supported at all. There is nothing to do about it.

Personal tools