"8042" PS/2 Controller

From OSDev.wiki
Revision as of 05:57, 25 March 2012 by Brendan (talk | contribs) (Hot-plug info)
Jump to navigation Jump to search
This page is a stub.
You can help the wiki by accurately adding more contents to it.

Overview

The PS/2 Controller (often called a "Keyboard controller") is located on the mainboard. In the early days the controller was a single chip (8042). As of today it is part of the Advanced Integrated Peripheral.

The name is misleading because the controller does more than controlling communication with PS/2 devices.

Overview of the PS/2-Controller


History

The PS/2 controller supersedes an older (uni-directional, single channel) XT controller. The XT controller is only partly compatible (but is 100% obsolete).

Overview of the XT-Controller

To reduce costs, IBM originally used some of the general purpose input/output capabilities of the XT controller to control various unrelated parts of the system. This includes:

When IBM upgraded to the (bi-directional) PS/2 controller, they intended it to be used to control a keyboard and a mouse, and therefore added support for controlling a second PS/2 channel. Unfortunately, at the time most people were using mice connected to Serial Ports (the PS/2 mouse didn't catch on until later) and several clone manufacturers created PS/2 controllers that only support a single PS/2 channel. Eventually (around the late 80486 and early Pentium time frame) PS/2 mice became more popular, and PS/2 controllers have supported two PS/2 channels since then.

Translation

The original keyboards (using the old XT interface) used "scan code set 1". When the original interface was superseded, IBM wanted to use a newer scan code, "scan code set 2". This change would have created compatibility problems for older software that was expecting different scan codes from the keyboard. To avoid the compatibility problem, for the first PS/2 channel, the PS/2 controller supports a translation mode. If translation is enabled the PS/2 controller will translate "scan code set 2" into "scan code set 1". This translation is enabled by default. To actually use "scan code set 2" (or "scan code set 3"), or to allow different types of PS/2 devices to be used in the first PS/2 port, you need to disable this translation.


USB Legacy Support

By modern standards you will find many PCs bundled with USB input devices. Some PCs may not even have PS/2 connectors at all. To remain compatible with old software, the mainboard emulates USB Keyboards and Mice as PS/2 devices. This is called USB Legacy Support.

Because the implementation differ by manufacturer and mainboard there are flaws and sometimes even bugs:

  • Some emulation layers also handle the communication with the real PS/2 connectors regardless of any connected USB device. So maybe not all capabilities of the PS/2 connectors and devices can be used. For example extended mouse modes needed to use the scroll wheel won't work or the keyboard only works on the first PS/2 connector and the mouse only on the second connector.
  • The SMM BIOS that's providing the PS/2 USB Legacy Support may not support extended memory techniques or Long Mode and may cause system crashes.

This USB Legacy Support should be disabled by the OS as soon as the OS initialises the USB Controller, and this should be done before the OS attempts to initialise the real PS/2 controller. Otherwise the OS would only be initialising the emulated PS/2 controller and there's a large risk of problems caused by deficiencies in the firmware's emulation.


PS/2 Controller Commands

TODO Note: this is Controller commands and NOT Device commands


Initialising the PS/2 Controller

TODO Note: this is Controller initialisation and NOT Device initialisation


Detecting PS/2 Devices

TODO


Hot Plug PS/2 Devices

WARNING: PS/2 was never intentionally designed to support hot-plug. Usually it is fine as most PS/2 controllers have reasonably robust IO lines, however some PS/2 controllers (mostly those in old chipsets) may potentially be damaged.

Despite the warning, most OSs (Windows, Linux, etc) do support hot-plug PS/2. It is also relied on by old "mechanical switch" KVMs (which allow the same PS/2 devices to be shared by multiple computers by effectively disconnecting the device from one computer and connecting it to the next).

When a PS/2 device is removed the PS/2 controller won't know. To work around this, when no data has been received from the device for some length of time (e.g. 2 seconds), an OS can periodically test for the presence of the device by sending an "echo" command to the device and checking for a reply. If the device doesn't respond, then assume the device has been unplugged.

When a PS/2 device is first powered up (e.g. when it is plugged in to a PS/2 port), the device should perform its Basic Assurance Test and then attempt to send a "BAT completion code". This means that software (e.g. an OS) can automatically detect when a PS/2 device has been inserted. Note: If a device is removed and then another device (or the same device) is plugged in quickly enough, the software may not have had time to detect the removal.

When software detects that a device was plugged in it can determine the type of device (see above). If the device was the same type as before software can re-configure it so that the device is in the same state as it was before removal. This means that (for example) someone using an old "mechanical switch" KVMs doesn't lose state (things like keyboard LEDs, typematic rate, etc) when switching between computers. If the device is not the same as before or there was no previously connected device, then software may need to start a new device driver (and terminate the old device driver, if any).


Sending Bytes To Device/s

TODO


Recieving Bytes From Device/s

TODO

CPU Reset

To trigger a CPU Reset (No matter what state the CPU currently has) you have to write the value 0xFE to the Output port.

 ;Wait for a empty Input Buffer
 wait1:
 in   al, 0x64
 test al, 00000010b
 jne  wait1
 
 ;Send 0xFE to the keyboard controller.
 mov  al, 0xFE
 out  0x64, al


See Also

Threads

External Links