I may be getting into something I shouldn't here, but I am going to try.
The PS/2 controller uses the exact same I/O port(s) for both mouse and keyboard. If a key is pressed on the keyboard, it sends the scan code to the exact same I/O port as it does when sending data from the mouse. i.e.: If the mouse sends a packet to the PS/2 controller, the controller sends it to the same I/O port as the one it sent for the keyboard. As for the PS/2 controller, this is called the Output Buffer and holds one byte at a time. It will hold this byte there until you read it, where it will then send another byte to that Output Buffer until all bytes are read.
At first you would think that this is confusing and how the heck do I determine which byte is for what.
For example, let's say that I type a key on the keyboard and that key produces a three byte code. At the exact same time, I move the mouse and that motion produces a 4 byte packet. The Output Buffer may look something like this:
Code:
key[0] -> XX
XX <- Mouse[0]
key[1] -> XX
XX <- Mouse[1]
key[2] -> XX
XX <- Mouse[2]
XX <- Mouse[3]
So, how do I tell which bytes are the key scan code and which bytes are the mouse packet?
Simple!
The PS/2 controller will fire an interrupt each time it places a byte into the Output Buffer. The trick is, and here is the funny thing, it fires a different IRQ for the mouse than it does for the Keyboard.
Therefore, when the IRQ for the Mouse fires, have that Handler read from the Output Buffer, set a flag, send the EOI, and return. Nothing more.
When the IRQ for the Keyboard fires, have that Handler read from the Output Buffer, set a flag, send the EOI, and return. Again, nothing more.
Then your keyboard and mouse drivers watch for their respected flag. When set, it adds this byte to its internal buffer. When a full packet has been received from either the mouse or the keyboard, the corresponding driver does something with it.
It is really that simple. Really.