Page 1 of 1

ps/2 trouble

Posted: Fri Feb 27, 2004 12:00 am
by gaf
Hi guys,
I'm currently writing a ps/2 mouse driver for the trion project. It uses some low-level procedures that are used by all the high-level ones to interface the hardware:
uchar SendController(uchar command);
uchar ReceiveController();
uchar SendMouse(uchar command);
uchar ReceiveMouse();

These procedures work as expected when I run the kernel from bochs. On real hardware, however, they return (more or less) random rubbish. I suppose that there's some synchronisation problem between the CPU and the ps/2 controller (i.e. I'm not waiting correctly for the mouse/controller to be ready) which doesn't exist when running on an emulator like bochs since the ps/2 controller there answers immediatly.

<C++>
// output_buffer =  1
// input_buffer  =  2
// mouse_buffer  = 32

uchar Mouse::SendController(uchar command)
{
    // Send a command or a command's parameter to the controller

    // Wait for the controller to be ready, then send the command
    while(Port::ReadByte(0x64) &input_buffer);
    Port::WriteByte(0x64, command);
    
    // Wait for the response and return
    while(!Port::ReadByte(0x64) &output_buffer);
    return Port::ReadByte(0x60);
}

uchar Mouse::ReceiveController()
{
    // Used when the controller returns more than 1byte

    // Wait for the response and return
    while(!Port::ReadByte(0x64) &output_buffer);
    return Port::ReadByte(0x60);
}
          
uchar Mouse::SendMouse(uchar command)
{
    // Directly send a command/parameter to the mouse (more comfortable)

    // Make sure that neither mouse nor controller are busy
    while(Port::ReadByte(0x64) &mouse_buffer);
    while(Port::ReadByte(0x64) &input_buffer);
    
    // Send the command
    Port::WriteByte(0x64, 0xD4);
    Port::WriteByte(0x60, command);
    
    // Wait for the response and return
    while(!Port::ReadByte(0x64) &output_buffer);
    return Port::ReadByte(0x60);
}

uchar Mouse::ReceiveMouse()
{
    // Used when the mouse returns more than 1byte

    // Wait for the response and return
    while(!Port::ReadByte(0x64) &mouse_buffer);
    return Port::ReadByte(0x60);
}


I realy hope that anybody knows where the problem is since there's only very basic information available on how to interface the ps/2 mouse. If somebody knows a good ressource on ps/2 apart from http://govschl.ndsu.nodak.edu/~achapwes ... index.html please let me know.

regards,
gaf  (trion: cosmo86)

RE:ps/2 trouble

Posted: Fri Feb 27, 2004 12:00 am
by CodeSlasher
Scroll down in the forum and you'll find my FULL explaination of my PS/2 mouse system with full code.
Also http://www.mega-tokyo.com/forum/index.php?board=1 has info

RE:ps/2 trouble

Posted: Sat Feb 28, 2004 12:00 am
by gaf
Hi CodeSlasher
You refer to this post, right ?
http://www.osdev.org/board.jsp?message=5736

Could you please post the code of test_keyboard_data() and wait_keyboard() aswell ? I added some of your ini-code to my classe's constructor but it didn't help so it looks like there's something wrong with the way I wait for controller and kbd.

regards,
gaf

RE:ps/2 trouble

Posted: Sun Feb 29, 2004 12:00 am
by CodeSlasher
unsigned char test_keyboard_data()
{
  return ((inportb(0x64) & 0x01));      
}

_wait_keyboard:
               push eax
.wait:
               in al,0x64       ;wait for keyboard to be ready for next command
               test al,0x02
               jnz .wait        ;bit is set so keyboard is NOT READY

               pop eax
               ret

RE:ps/2 trouble

Posted: Mon Mar 01, 2004 12:00 am
by gaf
Hi CodeSlasher,
thanks for posting the code. I compared your code and mine line by line, but though I wasn't able to locate my error. Therefore I decided to 'port' your driver to trion - all I had to do was changing some kernel-calls and rewritting the procedures you posted.

uchar test_keyboard_data()
{ return (Port::ReadByte(0x64) &output_buffer); }
            
void wait_keyboard_data()
{ while((!Port::ReadByte(0x64)) &output_buffer); }

void wait_keyboard()
{ while((!Port::ReadByte(0x64)) &input_buffer); }

The port of your driver works with bochs but hangs on a real machine, just like mine. So there are only 2 possibilities left:
- I made the same error in the code I posted above as in my driver
- There's sth wrong with some other part of the trion-code

thanks for your support,
gaf