Keyboard Controller Functions

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
crbenesch
Posts: 13
Joined: Wed May 28, 2008 9:23 am
Location: Maricopa, AZ USA
Contact:

Keyboard Controller Functions

Post by crbenesch »

I wrote these functions after pouring over documentation, debugging, and strange behaviour. They perform commands with the keyboard controller.

controller_cmd - For sending a comand to port 0x64, returns the result of the command or 0xff if nothing was received.
controller_wcmd - For sending commands to port 0x64 that require data as well, such as select scancode set, set leds, etc...
keyboard_cmd and keyboard_wcmd act just like the above functions, but access port 0x60 for writing.

They are written in C and pretty much self sufficient, nothing required except the inportb and outportb functions which we should all have being OSdevers anyway.

Code: Select all

/* The next four functions dont differ by much, but they save you a lot of headache trying to figure it out */
unsigned char keyboard_command(unsigned char cmd)
{
   int tries;
   unsigned char c;

   while (inportb(0x64) & 1) inportb(0x60); /* Clear out any old data */
   tries = 10;
   while ((inportb(0x64) & 2) && tries) { delay(5); tries--; }
   if (!tries) return 0xff;
   outportb(0x60,cmd);
   tries = 10;
   while ((!(inportb(0x64) & 1)) && tries) { delay(5); tries--; }
   if (!tries) return 0xff;
   c = inportb(0x60);
   return c;
}

unsigned char keyboard_commandw(unsigned char cmd,unsigned char data)
{
   int tries;
   unsigned char c;

   while (inportb(0x64) & 1) inportb(0x60); /* Clear out any old data */
   tries = 10;
   while ((inportb(0x64) & 2) && tries) { delay(5); tries--; }
   outportb(0x60,cmd);
   tries = 10;
   while ((!(inportb(0x64) & 1)) && tries) { delay(5); tries--; }
   inportb(0x60);
   tries = 10;
   while ((inportb(0x64) & 2) && tries) { delay(5); tries--; }
   outportb(0x60,data);
   tries = 10;
   while ((!(inportb(0x64) & 1)) && tries) { delay(5); tries--; }
   c = inportb(0x60);
   return c;
}

unsigned char controller_cmdw(unsigned char cmd,unsigned char data)
{
   int tries;
   unsigned char c;

   while (inportb(0x64) & 1) inportb(0x60); /* Clear out any old data */
   tries = 10;
   while ((inportb(0x64) & 2) && tries) { delay(5); tries--; }
   outportb(0x64,cmd);
   tries = 10;
   while ((!(inportb(0x64) & 1)) && tries) { delay(5); tries--; }
   inportb(0x60);
   tries = 10;
   while ((inportb(0x64) & 2) && tries) { delay(5); tries--; }
   outportb(0x60,data);
   tries = 10;
   while ((!(inportb(0x64) & 1)) && tries) { delay(5); tries--; }
   c = inportb(0x60);
   return c;
}

unsigned char controller_cmd(unsigned char cmd)
{
   int tries;
   unsigned char c;

   while (inportb(0x64) & 1) inportb(0x60); /* Clear out any old data */
   tries = 10;
   while ((inportb(0x64) & 2) && tries) { delay(5); tries--; }
   if (!tries) return 0xff;
   outportb(0x64,cmd);
   tries = 10;
   while ((!(inportb(0x64) & 1)) && tries) { delay(5); tries--; }
   if (!tries) return 0xff;
   c = inportb(0x60);
   return c;
}

~~~~~~~~~~~~~~~~~~~~~
Chris Benesch
CRIBIX
http://www.maricopacomputer.com/cribix/
Post Reply