Mouse driver works in VMWare and Bochs, but not on hardware

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
SANiK

Mouse driver works in VMWare and Bochs, but not on hardware

Post by SANiK »

I quickly wrote this mouse driver using pseudo code from here:
http://www.unixcn.net/cnix/cdb2/bbs/vie ... d=UE1INz5C
AND the osdever.net section:
http://www.computer-engineering.org/ps2mouse/

The problem is that it works in VMWare and in Bochs but not on real hardware.
Could someone please help me figure out what's wrong cause it's been driving me nuts!?

Code: Select all

//Mouse.inc
byte mouse_cycle=0;
sbyte mouse_byte[3];
sbyte mouse_x=0;
sbyte mouse_y=0;

//Mouse functions
void mouse_handler(struct regs *a_r)
{
  while (inportb(0x64) & 1)
  {
    mouse_byte[mouse_cycle]=inportb(0x60);
    mouse_cycle++;
    if(mouse_cycle==3)
    {
      mouse_x=mouse_byte[1];
      mouse_y=mouse_byte[2];
      //printf("(%d, %d)\n", mouse_byte[1], mouse_byte[2]);
      mouse_cycle=0;
    }
  }
}

inline void mouse_wait(byte a_type)
{
  if(a_type==0)
  {
    while(inportb(0x64) & 1) //Data
    {
      //Loop it out
    }
    return;
  }
  else
  {
    while(inportb(0x64) & 2) //Signal
    {
      //Loop it out
    }
    return;
  }
}

inline void mouse_write(byte a_write)
{
  //Wait to be able to send a command
  mouse_wait(1);
  //Tell the mouse we are sending a command
  outportb(0x64, 0xD4);
  //Wait for the final part
  mouse_wait(1);
  //Finally write
  outportb(0x60, a_write);
}

byte mouse_read()
{
  //Get's response from mouse
  mouse_wait(0);
  
  /*
  switch(inportb(0x60)):
    case 0xAA: //Test passed
    case 0xFC: //Test failed
    case 0xFA: //Acknowledge
    default: //Mouse ID
  */
  
  return inportb(0x60);
}

void mouse_install()
{
  //Setup the mouse handler
  irq_install_handler(12, mouse_handler);
  
  //Part 1 - Initiate the mouse
  byte _status;
  
  //Mouse aux
  mouse_wait(1);
  outportb(0x64, 0xA8);
  
  //Bounce the mouse
  mouse_wait(1);
  outportb(0x64, 0x20);
  mouse_wait(0);
  _status=(inportb(0x60) | 2);
  mouse_wait(1);
  outportb(0x64, 0x60);
  mouse_wait(1);
  outportb(0x60, _status);

  mouse_write(0xFF);  //Reset the mouse
  mouse_read();  //Acknowledge
  mouse_read();  //Acknowledge/Mouse ID
  mouse_read();  //Acknowledge/Mouse ID
  mouse_write(0xF6); //Tell the mouse to use default settings
  mouse_read();  //Acknowledge
  mouse_write(0xF4);  //Enable the mouse
  mouse_read();  //Acknowledge*/
  
  //Part 2 - setup mouse
  mouse_write(0xF3);  //Set Sample Rate 
  mouse_read();  //Acknowledge
  mouse_write(0x0A);  //decimal 10
  mouse_read(); //Acknowledge
  mouse_write(0xF2);  //Read Device Type
  mouse_read();  //Acknowledge
  mouse_read();  //Mouse ID
  mouse_write(0xE8);  //Set resolution
  mouse_read(); //Acknowledge
  mouse_write(0x03);  //8 Counts/mm
  mouse_read();  //Acknowledge
  mouse_write(0xE6);  //Set Scaling 1:1
  mouse_read();  //Acknowledge
  mouse_write(0xF3);  //Set Sample Rate
  mouse_read();  //Acknowledge
  mouse_write(0x28);  //decimal 40
  mouse_read();  //Acknowledge
  mouse_write(0xF4);  //Enable the mouse
  mouse_read();  //Acknowledge
}

mystran

Re:Mouse driver works in VMWare and Bochs, but not on hardwa

Post by mystran »

Does your inport/outport macros (functions?) include a small wait?
If not, then VMWare and Bochs are unlikely to care, but real hardware might.

It would also help if you told us something more specific than "it won't work".
Post Reply