Page 1 of 1

PS/2 Mouse?

Posted: Thu Sep 11, 2003 11:00 pm
by Squrky
I must be really really lame...

Got my keyboard handler, works great (see below). Mapped my interrupts etc and I receive interrupt 33 for my keyboard, and in theory I should receive 44 (32 + 12) for the mouse. But.

It doesn't work at all. How do I tell my mouse to start sending data?

Below is how I'm setting up my keyboard, and it works fine:

    void keyboardInit() {
        idtLoadEntry(32 + PIC_KEYBOARD, (dword) isrKeyboard,
                     IDT_CS_SELECTOR,
                     IDT_INT_GATE | IDT_PRESENT | IDT_BITS_32);
picEnableIRQ(PIC_KEYBOARD);
        ...

Should I in theory be doing similar for the mouse? Because I am and it doesn't work :(

Any help would be so great because I've just got my VESA routines to work 8)

RE:PS/2 Mouse?

Posted: Fri Sep 12, 2003 11:00 pm
by gaffi
Both the mouse and the keyboard are managed by the controller (i8042). The keyboard will work right after booting - if you want to use the mouse you'll have to setup a few things first:

1. Enable the mouse int in the "command-byte"
Send command 0x60 to the controller at port 0x64
Send 00000011b to port 0x60 (mouse and kbd interrupts on)

2. Enable the host's mouse interface
Send command 0xA8 to the controller

3. Activate the mouse itself
Send command 0xD4 to the controller (The next command written to 0x60 will be send to the mouse)
Send 0xFA to port 0x60

You should also think about switching the keyboard's scancode-set from XT (1) to AT (2), since the XT set is very old and not longer supported by all keyboards.
Send 0xF0 to port 0x60
Send 0x2  to port 0x60

Every time you send data to port 0x64 or 0x60 you have to make sure that the
your last command has already been executed. Therefore you have to wait until
bit2 at port 0x64 is zero.

Have a look at this site for further information:
http://govschl.ndsu.nodak.edu/~achapwes ... index.html
(The controller commands can be found in the "AT Keyboard" section)

cheers,
Daniel Raffler

RE:PS/2 Mouse?

Posted: Fri Sep 12, 2003 11:00 pm
by Squrky
Thankyou very much :)

RE:PS/2 Mouse?

Posted: Mon Sep 15, 2003 11:00 pm
by Squrky
Ok, would you mind taking a look at this?
-------------------------------------------------------------------------------
#include "include\types.h"
#include "include\kernel.h"
#include "include\mouse.h"
#include "include\io.h"
#include "include\idt.h"
#include "include\console.h"
#include "include\pic.h"

void write_controller(byte command) {
    while(inb(0x64) & 0x02);
    outb(0x64, command);
}

void write_command(byte command) {
    while(inb(0x64) & 0x02);
    outb(0x60, command);
}

void mouse_init() {
    idt_load_entry(32 + PIC_MOUSE, (dword) mouse_handler, IDT_CS_SELECTOR, IDT_INT_GATE | IDT_PRESENT | IDT_BITS_32);
    
    // 1. Enable the mouse int in the "command-byte"
    // Send command 0x60 to the controller at port 0x64
    // Send 00000011b to port 0x60 (mouse and kbd interrupts on)
    kernel_print("1) Enable mouse interrupt\n");
    write_controller(0x60);
    write_command(0x3);

    // 2. Enable the host's mouse interface
    // Send command 0xA8 to the controller
    kernel_print("2) Enable mouse interface\n");
    write_controller(0xa8);
    
    // 3. Activate the mouse itself
    // Send command 0xD4 to the controller (The next command written to 0x60 will be send to the mouse)
    // Send 0xFA to port 0x60
    kernel_print("3) Activate mouse:\n");
    write_controller(0xd4);
    write_command(0xfa);

    pic_enable_irq(PIC_MOUSE);
}

void mouse_handler() {
    kernel_print('Mouse Interrupt!\n');
    
    outb(0xa0, 0x20);
    outb(0x20, 0x20);
}

-------------------------------------------------------------------------------
Everything goes according to plan, but the last command sent to the mouse interface causes bochs to abort. Here's a copy of bochsout.txt:

00000000000i[MEM0 ] allocated memory at 00B40020. after alignment, vector=00B41000
00000000000i[MEM0 ] 32.00MB
00000000000i[MEM0 ] rom at 0xf0000/65536 ('BIOS-bochs-latest')
00000000000i[MEM0 ] rom at 0xc0000/27392 ('vgabios-0.3b.bin')
00000000000i[CMOS ] Setting initial clock to: Tue Sep 16 17:01:38 2003 (time0=1063728098)
00000000000i[DMA  ] channel 4 used by cascade
00000000000i[DMA  ] channel 2 used by Floppy Drive
00000000000i[FDD  ] fd0: '../nxos3-9-12/nxos.ima' ro=0, h=2,t=80,spt=18
00000000000i[VGA  ] interval=30000
00000000000i[VGA  ] VBE Bochs Display Extension Enabled
00000000000i[     ] init_mem of 'harddrv' plugin device by virtual method
00000000000i[     ] init_mem of 'keyboard' plugin device by virtual method
00000000000i[     ] init_mem of 'serial' plugin device by virtual method
00000000000i[     ] init_mem of 'parallel' plugin device by virtual method
00000000000i[     ] init_dev of 'harddrv' plugin device by virtual method
00000000000i[HD   ] Boot device will be 'a'
00000000000i[HD   ] Floppy boot signature check is enabled
00000000000i[     ] init_dev of 'keyboard' plugin device by virtual method
00000000000i[KBD  ] will paste characters every 1000 keyboard ticks
00000000000i[     ] init_dev of 'serial' plugin device by virtual method
00000000000i[SER  ] com1 at 0x3f8/8 irq 4
00000000000i[     ] init_dev of 'parallel' plugin device by virtual method
00000000000i[PAR  ] parallel port 1 at 0x378
00000000000i[     ] reset of 'harddrv' plugin device by virtual method
00000000000i[     ] reset of 'keyboard' plugin device by virtual method
00000000000i[     ] reset of 'serial' plugin device by virtual method
00000000000i[     ] reset of 'parallel' plugin device by virtual method
00000004256i[BIOS ]  rombios.c,v 1.85.2.1 2003/01/16 21:58:42 cbothamy Exp $
00000330043i[KBD  ] reset-disable command received
00000353372i[VBIOS] VGABios $Id: vgabios.c,v 1.20 2002/10/20 15:12:33 vruppert Exp $
00000353603i[VGA  ] VBE known Display Interface b0c1
00000353688i[VGA  ] VBE known Display Interface b0c0
00000356633i[VBIOS] VBE Bios $Id: vbe.c,v 1.22 2002/09/19 17:03:21 cbothamy Exp $
00000356748i[VGA  ] VBE known Display Interface b0c1
00000356832i[VGA  ] VBE disabling
00000693509i[VGA  ] VBE known Display Interface b0c1
00000784311e[HD   ] device set to 0 which does not exist
00000784604e[HD   ] device set to 1 which does not exist
00000942354e[KBD  ] keyboard: (mch) scan convert turned off
00000942492p[KBD  ] >>PANIC<< MOUSE: kbd_ctrl_to_mouse(fah)
00000942492i[SYS  ] Last time is 1063728099
00000942492i[CPU  ] protected mode
00000942492i[CPU  ] CS.d_b = 32 bit
00000942492i[CPU  ] SS.d_b = 32 bit
00000942492i[CPU  ] | EAX=000000fa  EBX=01e00000  ECX=00000001  EDX=00000060
00000942492i[CPU  ] | ESP=01dfffa0  EBP=01dfffa8  ESI=00000800  EDI=00240160
00000942492i[CPU  ] | IOPL=0 NV UP EI PL NZ AC PE NC
00000942492i[CPU  ] | SEG selector     base    limit G D
00000942492i[CPU  ] | SEG sltr(index|ti|rpl)     base    limit G D
00000942492i[CPU  ] |  DS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00000942492i[CPU  ] |  ES:0010( 0002| 0|  0) 00000000 000fffff 1 1
00000942492i[CPU  ] |  FS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00000942492i[CPU  ] |  GS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00000942492i[CPU  ] |  SS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00000942492i[CPU  ] |  CS:0008( 0001| 0|  0) 00000000 000ffbff 1 1
00000942492i[CPU  ] | EIP=00100028 (00100027)
00000942492i[CPU  ] | CR0=0x60000011 CR1=0x00000000 CR2=0x00000000
00000942492i[CPU  ] | CR3=0x00000000 CR4=0x00000000
00000942492i[     ] restoring default signal behavior
00000942492i[CTRL ] quit_sim called with exit code 1
-------------------------------------------------------------------------------

It doesn't seem to like enabling the mouse. HAve you any idea what I might be doing wrong? I've taken the time to look over the link you recommended, but I've been on that site for a few days now and I can't fathom it. Thanks again in advance.

RE:PS/2 Mouse?

Posted: Tue Sep 16, 2003 11:00 pm
by gaffi
Hi Squrky,
your code looks good. Did you try to boot your os from a floppy? If this works you may have forgotten enabling the mouse in your bochsrc.txt:
mouse: enabled=1


void write_controller(byte command) {
    while(inb(0x64) & 0x02);
    outb(0x64, command);
}

I'm not sure which bit you have to wait for:
bit0: Output Buffer Full - Indicates when it's okay to write to output buffer

bit1: Input Buffer Full  - Indicates when input is available in the input buffer
bit5: Mouse Output Buffer Full - Similar to OBF, except for PS/2 mouse


Make sure that the interrupts are disabled while enabling the mouse !

Hope this will help,
Daniel Raffler

RE:PS/2 Mouse?

Posted: Tue Sep 16, 2003 11:00 pm
by gaffi
Hi,
I ran my own kernel with bochs this afternoon without any problems. I uploaded a
kernel image, my bochsrc.txt and the code (nasm) here:
http://mitglied.lycos.de/cozmo86/        ~ 6KB
(the add disappears after a few seconds)

This kernel now waits until 0x64&1 instead of 0x64&2 before writing to the
ports.
As soon as the os-window appears you can press F12 to enable the mouse (press again to disable).

Cheers,
Daniel Raffler    [email protected]

RE:PS/2 Mouse?

Posted: Wed Sep 17, 2003 11:00 pm
by Squrky
Thanks very much for lending me your code, it was very useful, but I still got problems.
I've been over and over Adam Chapweske's PS/2 Mouse Interface document and I've even followed the initialisation procedure to the letter. My mouse driver works in that I get all the correct responses from the controller. All the communication I do with the controller is fine. The problem seems to be that I cannot get the controller to assert a mouse interrupt. As I said before, the keyboard part of my code works fine.

I've studied how you wrote your driver very hard and I can't see any real differences. I notice you start up in real-mode and then jump to protected mode where you do your mouse initialisation.

1) You enable mouse/keyboard interrupts on the controller.
2) You enable the mouse interface on the controller.      
3) You enable mouse data reporting.

I've also looked at your isrs and I'm doing exactly the same for the mouse (int 44)

1) disable ints
2) push all
3) handling the mouse (calling a C extern)
4) end of int to master and slave pics
5) pop all
6) enable ints
7) iret

This works fine for my keyboard, but doesn't get called at all for the mouse. The mouse controller does seem to be doing something however, since if I move the mouse, it just fills the 8042 output buffer and I'm no longer able to read from the keyboard.

Would my gdt have anything to do with it? The actual routines work, but this is how I'm setting up:

void gdt_init() {
// Null descriptor
gdt_null_segment(0);

// GDT_CODE_SELECTOR   0x08
gdt_code_segment(1, kRead,  kRing0, k4K, k32Bit, kPresent, 0, 0xfffff);

// GDT_DATA_SELECTOR   0x10
gdt_data_segment(2, kWrite, kRing0, k4K, k32Bit, kPresent, 0, 0xfffff);

gdt_load();
asm("mov $0x10, %ax");
asm("mov %ax, %ds");
    asm("mov %ax, %ss");
    asm("mov %ax, %es");
    asm("mov %ax, %fs");
    asm("mov %ax, %gs");
}

code selector is 0x08, data selector 0x10. As above ds, ss, es, fs, gs are 0x10 and cs is 0x08 (checked with disas).

I'm setting up my isrs like so:

byte options = IDT_RING_0 | IDT_INT_GATE | IDT_PRESENT | IDT_BITS_32;
idt_load_entry(32 + PIC_KEYBOARD, (dword) isr_keyboard, IDT_CODE_SELECTOR, options);
idt_load_entry(32 + PIC_MOUSE,    (dword) isr_mouse,    IDT_CODE_SELECTOR, options);

PIC_KEYBOARD is 0x01
PIC_MOUSE is 0x0c
IDT_CODE_SELECTOR is 0x08.

After this I program the PIC to enable the corresponding ints.

I've also remapped the PIC properly as far as I can tell:

Here's my PIC code:

#include "include\type.h"
#include "include\pic.h"
#include "include\io.h"
#include "include\console.h"

void pic_init() {
asm("cli");
outb(PIC_MASTER_PORT_A, 0x11); // ICW 1
outb(PIC_SLAVE_PORT_A, 0x11);  // 7 | 6 | 5 | 4 | 3  | 2 | 1  |
           // 0 | 0 | 0 | 1 |Trig|M/S|ICW4|
           // Master Slave Configuration
           // IC4 needed                            

outb(PIC_MASTER_PORT_B, 0x20); // ICW 2
outb(PIC_SLAVE_PORT_B, 0x28);  // INT 32-39 mapped to IRQ 0 to IRQ 7
           // INT 40-47 mapped to IRQ 8 to IRQ 15

outb(PIC_MASTER_PORT_B, 0x04); // ICW 3
outb(PIC_SLAVE_PORT_B, 0x02);  // IR 2 of master is connected to slave pic
           // IRQ on master this slave is connected to

outb(PIC_MASTER_PORT_B, 0x01); // ICW 4
outb(PIC_SLAVE_PORT_B, 0x01);  // Master, Fully Nested Mode
           // Slave, Fully Nested Mode

outb(PIC_MASTER_PORT_B, 0xff); // All interrupts except IRQ 0 are disabled
outb(PIC_SLAVE_PORT_B, 0xff);
asm("sti");
}

void pic_enable_irq(byte irq) {
byte status;
if(irq < 8) {
status = inb(PIC_MASTER_PORT_B);
status &= ~(1 << irq);
outb(PIC_MASTER_PORT_B, status);
} else {
irq -= 8;
status = inb(PIC_SLAVE_PORT_B);
status &= ~(1 << irq);
outb(PIC_SLAVE_PORT_B, status);
}
}

void pic_disable_irq(byte irq) {
byte status;
if(irq < 8) {
status = inb(PIC_MASTER_PORT_B);
status |= 1 << irq;
outb(PIC_MASTER_PORT_B, status);
} else {
irq -= 8;
status = inb(PIC_SLAVE_PORT_B);
status |= 1 << irq;
outb(PIC_SLAVE_PORT_B, status);
}
}

I can't think of much else that could be wrong and its driving me nuts. If you can think of anything then do let me know :) In the mean time however I would like your permission to hack at your kernel code, and start from scratch to get my mouse and keyboard to work. You've managed to do it in a few lines so I must be missing something.

Kind regards, and thanks for your help.

Nathan Hunt
[email protected]

RE:PS/2 Mouse?

Posted: Thu Sep 25, 2003 11:00 pm
by Ramanan
THANX that was a good sample.
i shall rate 100/100 for PS/2 demo in that code.

It works fine in my kernel for normal PS/2 mouse. But the problem come when i try to enter in to 3D intelli mouse with 3 buttons.
Actually i refered to the URL
http://panda.cs.ndsu.nodak.edu/%7Eachap ... mouse.html
for the mouse protocol. it works fine on Bochs emulator. But nothing happens when i run that on my PC (P-IV 2.4GHz).
Is there any spl thing i have to follow other than the protocol ?
It wud b a greate help if any one cud help me?

Thanx,
YogaRamanan.T

Thanx in advance.

RE:PS/2 Mouse?

Posted: Tue Sep 30, 2003 11:00 pm
by Brandon S
A place to look would be www.osdever.net

They have a lot of great items!

RE:PS/2 Mouse?

Posted: Wed Oct 01, 2003 11:00 pm
by Ramanan
Hi,
   But i cudn't find out any useful item for Microsoft PS/2 mouse in www.osdever.net link. Can u pls send me the specific link?


Thanx
YogaRamanan.T

RE:PS/2 Mouse?

Posted: Wed Oct 01, 2003 11:00 pm
by Brandon S
Oh, That is a bit different.

Microsoft changes a lot. Not many want to do with it. I'd suggest going to their Chatroom and asking. A lot of useful help there.

Love to All