2004-01-16 02:52:50

by Brett Gmoser

[permalink] [raw]
Subject: Raw I/O Problems with inb()

Hello everybody.

I apologize if this is off topic, but I have seen relevant information posted to this list, even though this isn't actually a kernel programming problem. And believe me, I have searched everywhere else for a solution to this problem.

To give some short background information, I am programming the Linux client for a project that basically counts a users keystrokes, and sends the amount to a server which displays the count for all the world to see. Teams and individuals compete from around the world, and we are approaching 5000 members.

At any rate, the application is written in C/C++. I am using iopl() for permissions management, and inb() for reading raw data directly from the PS/2 keyboard to test if a key has been pressed. All of this works great, and the program is complete, except for one problem.

It seems that on Athlon XP systems using PS/2 mice, inb(0x64) always says there is data waiting from the keyboard, when it is in fact mouse data. I assume that different architecture on AMD systems is the cause of the problem, but is there a solution? There must be. Here is a snippet of the code I am using currently. I apologize for the sloppiness of it - it's been modified about 30 times with 30 different solutions to the problem, all of which did not seem to fix it. Commented out are solutions that I have tried and have not worked.

#define KEYBOARD_PORT 0x60
#define KEYBOARD_STATUS_PORT 0x64

bool keycounter::count(/*wplog* l*/) { // some very simple code to see if a key is being pressed or not.
const unsigned char kbd_read_mask = 0x01;
unsigned char status = inb_p(KEYBOARD_STATUS_PORT);
/* unsigned char status = inb(KEYBOARD_STATUS_PORT);

/* mouse data? */
// if (status == 20 && ((status & kbd_read_mask) != 0x01) || (status & 0x21)) { fflush(0); return false; }
// if(status & kbd_read_mask & 0x20) { fflush(0); return false; }
// if(!(status & kbd_read_mask & 0x01)) { fflush(0); return false; }
// if(!((inb(0x379) & 0x20) == 0x20)) { fflush(0); return false; }

if(status!=20) { fflush(0); return false; }
unsigned char c = inb(KEYBOARD_PORT);
if(status==20 && c < TABLE_SIZE) {
if(c==lastc) {
fflush(0);
return false;
}
lastc=c;
fflush(0);
return true;
}
else { // key has been lifted
lastc=0;
fflush(0);
return false;
}
}

Thanks in advance for any insight you all may be able to give me


--
Brett Gmoser
[email protected]


2004-01-16 11:28:43

by Matthias Urlichs

[permalink] [raw]
Subject: Re: Raw I/O Problems with inb()

Hi, Brett Gmoser wrote:

> Thanks in advance for any insight you all may be able to give me

Reading port registers directly doesn't make sense. No USB keyboards, no
remote use, ... you need to drop your DOS programming mentaility. Fast.

You can use a PTY approach like script(1), or you can write a small kernel
module like evbug (see the kernel source, drivers/input/evbug.c) which
monitors everything the user is doing.

--
Matthias Urlichs | {M:U} IT Design @ m-u-it.de | [email protected]
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
- -
There are two distinct sorts of what we call bashfulness; this, the
awkwardness of a booby, which a few steps into the world will convert into the
pertness of a cox comb; that, a consciousness, which the most delicate
feelings produce, and the most extensive knowledge cannot always remove.
-- Mackenzie

2004-01-16 14:51:50

by Brett Gmoser

[permalink] [raw]
Subject: Re: Raw I/O Problems with inb()


>Reading port registers directly doesn't make sense. No USB keyboards, no
>remote use, ... you need to drop your DOS programming mentaility. Fast.

Thanks for your input. However, I do not want the application to work
remotely. I have examined all other options, and this is the best way to
do it under the circumstances which I need this program to work.

The question is not one of "how" to accomplish all of this, it is how to
get inb(0x64) to state that it is indeed mouse movement, and not keyboard
data, on an AMD Athlon XP system with PS/2 mouse.

Thanks again!

Brett


2004-01-16 17:41:45

by Matthias Urlichs

[permalink] [raw]
Subject: Re: Raw I/O Problems with inb()

Hi, Brett Gmoser wrote:

> The question is not one of "how" to accomplish all of this, it is how to
> get inb(0x64) to state that it is indeed mouse movement, and not keyboard
> data, on an AMD Athlon XP system with PS/2 mouse.

Well, as I said, I would solve the "how" problem with a kernel module
which monitors mouse movement or keyboard events. This happens to be a
whole lot more reliable, and it will also eat no CPU time when nothing's
happening.

Plus, it will work on any mouse or keyboard. People actually _use_ USB
critters these days, you know ...

--
Matthias Urlichs | {M:U} IT Design @ m-u-it.de | [email protected]
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
- -
Sign my PETITION.
-- Zippy the Pinhead

2004-01-17 02:12:55

by Brett Gmoser

[permalink] [raw]
Subject: Re: [linuxkernel] Re: Raw I/O Problems with inb()

Thanks very much for your insight. I do appreciate it.

I do agree that the next step will be to write a kernel module. However, I would like to see if it is feasible to do this with inb(), since I see no very easy way around the fact that I do not want the program to work remotely.

Really, the question boils down to this: Is the keyboard status port on AMD systems 0x64, or is it something else? If so, what is it?

Thank you!

Brett

On Fri, 16 Jan 2004 18:41:25 +0100
Matthias Urlichs <[email protected]> wrote:

> Well, as I said, I would solve the "how" problem with a kernel module
> which monitors mouse movement or keyboard events. This happens to be a
> whole lot more reliable, and it will also eat no CPU time when nothing's
> happening.
>
> Plus, it will work on any mouse or keyboard. People actually _use_ USB
> critters these days, you know ...
>