Mice and Mouse.

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
vbbrett

Mice and Mouse.

Post by vbbrett »

Hello,

How do I go about writing a driver for my mouse?

Also, how could I get the BIOS's time?

Thanks,
Brett
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Mice and Mouse.

Post by Brendan »

Hi,
vbbrett wrote: How do I go about writing a driver for my mouse?
Which mouse?

There's the PS/2 mice, which are used via channel B of the keyboard controller chip, and there's serial mice which are controlled through the serial port it's connected to.

Then there's the actual protocol the mouse uses. For PS/2 there's 3 different protocols that I know of (standard, scroll wheel and 5 button scroll wheel). For serial mice there's microsoft standard, microsoft wheel, logitech, mouse systems, etc. Also there's a "bus mouse", which uses an old plug-in ISA card (very rare now).

For PS/2 mice it's like the keyboard driver except it uses IRQ 12 and there's a different set of codes to send and receive (depends on which type of mouse). How to write a serial mouse driver will depend on if you have a device driver for serial ports & if the mouse is built into it or not. Once you know how the mouse driver talks to the serial port it mostly involves sending/receiving bytes to/from the serial port - once you've set up the serial port (how many date bits, stop bits, parity, etc), which also depends on which mouse.

In general you should be able to find details of each type of mouse using Google...
vbbrett wrote: Also, how could I get the BIOS's time?
The BIOS's current timer tick can be read at 0x0000046c. The ASCII string representing the date that the BIOS was manufactured can be read at 0xF000:0xFFF5 (or 0xFFFF5).

If you're after the current RTC time (second, minute, hour, day, month, week) then you can get this direct from the RTC/CMOS, or use BIOS functions "INT 0x1A, AH=0x03" (time) and "INT 0x1A, AH=0x04" (date). If you want to know how to get the current RTC time direct from the RTC/CMOS (using IO port reads) let me know & I'll post some code...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Mice and Mouse.

Post by Pype.Clicker »

Once again, .:QuickLinkz:. come to the rescue :)

there's a collection of collected links about implementing a mouse driver there, both for COMx and PS/2 mice. For USB mice, you'll be happy to know the motherboard can emulate them as PS2 ...
vbbrett

Re:Mice and Mouse.

Post by vbbrett »

Thanks alot Brendan! Very Informative post, if I can say so myself. As to my plight: I wish to write a driver for a PS/2 mouse. Correct me if I'm wrong, but serial mice are hardly in date except for old PCs. Would you consider it worth while to include support for and almost outdated device? Though, I suppose that I must implement a serial driver somewhere along the way..... As to the time, and such... I would prefer to get this RTC-whatever-you-call-it time. Also, would you care being more precise about this BIOS ticker? I just get the info direct from the port you mentioned? Is it in any specific format? Does it count? ect,ect. Thanks again for the info.

Thanks,
vbbrett

P.S. Pype, I don't mean to be rude, but this thing with the QuickLinkz gets on my nerves. It's a great resource, but it's kinda hard to search just that topic. Instead, if you know where it's located within the numerous posts; it would be great if you could copy and paste or give me a link, ect. Not that I don't appreciate the resource, it's the ease of searching and such. Thanks for the info. I shall continue to go through it.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Mice and Mouse.

Post by Brendan »

Hi,
vbbrett wrote: Thanks alot Brendan! Very Informative post, if I can say so myself. As to my plight: I wish to write a driver for a PS/2 mouse. Correct me if I'm wrong, but serial mice are hardly in date except for old PCs. Would you consider it worth while to include support for and almost outdated device? Though, I suppose that I must implement a serial driver somewhere along the way.....
Serial mice are still in use, but I guess it can depend on which computers your OS supports. On newer computers my OS will allow 2 seperate users per computer, with 2 PS/2 keyboards and 2 serial mice. Most PS/2 mice are designed so that they can be plugged into a serial port (they automatically detect what they are plugged into and behave accordingly).

Because the PS/2 mouse is controlled by the same chip/IO ports as the keyboard it can be difficult to write a device driver for mouse only. If your OS protects IO ports, so that a device driver needs to allocate an IO port before using it then you only one device driver will be able to access the keyboard controller chip IO ports at a time. Also because the keyboard controller chip uses the same input and output buffers for both keyboard and mouse (channel A and channel B) it would be difficult to prevent a keyboard driver from interfering with the mouse driver. To avoid all of this I'd write a single PS/2 driver that handles keyboards and mice.

Initializing the keyboard controller chip:
- allocate resources for both channels (IO ports 0x60 and 0x64, IRQ's 1 and 12)
- set keyboard controller command byte (disable IRQ's and disable both channels)
- perform controller chip self test
- set keyboard controller command byte (enable both channels)
- perform interface tests
- send reset command to channel A
- determine channel A device type from status returned from reset
- send reset command to channel B
- determine channel B device type from status returned from reset
- set keyboard controller command byte (enable IRQ's for used channels, disable unused channels)

Once all this is done you can initialize each device. For keyboards this means setting typematic delay, repeat rate and LED's. Mice will start out as a standard mouse, but by setting the sample rate several times with special values you can change the protocol it uses to enable the scroll wheel, and then (if scroll wheel worked) use another sample rate sequence to enable the 5 button with scroll wheel protocol.

The best PS/2 mouse page I've found so far is:
http://panda.cs.ndsu.nodak.edu/%7Eachap ... mouse.html

vbbrett wrote: As to the time, and such... I would prefer to get this RTC-whatever-you-call-it time. Also, would you care being more precise about this BIOS ticker? I just get the info direct from the port you mentioned? Is it in any specific format? Does it count? ect,ect. Thanks again for the info.
The BIOS tick timer uses IRQ 0 to increase a counter (at 0x0000046c in memory) 18.2065 times per second (by default). It's not much use in protected mode, but you can re-program the timer chip and write your own IRQ 0 handler.

The RTC (Real Time Chip) keeps track of the current second, minute, hour, day, month, year in hardware. There's a few problems with it though, as some OS's set it to local time (windows) and some set it to UTC (Linux). IMHO it's best to use UTC as it's the same everywhere (which avoids inconsistancies over networks). Unfortunately if a computer is set up to run your OS and windows (dual boot) you'll need to use local time, and there's no way to tell when windows has changed the RTC for daylight savings so your OS's time can be messed up easily.

I've attached code that reads the current time values from the RTC, but I'd recommend downloading this excellent document that covers all PC timing:
http://www.contactor.se/~daniel/links/Pctim003.txt


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Mice and Mouse.

Post by Pype.Clicker »

vbbrett wrote:
Instead, if you know where it's located within the numerous posts; it would be great if you could copy and paste or give me a link, ect.
here you are ...
[*]A collection of links about serial mice: UART, MS mouse protocol (recommended by Chris Griese, the author of the serial mouse demo program), more about COMx device, and some misc. info about how mice work in general.
[*] other links about PS/2 mice are available through PS2 mouse IRQ thread.
having a 'one-key-search-while-typing' feature in my webbrowser, i tend to forget how hard it can be for other to locate a specific info in a large page ...
Therx

Re:Mice and Mouse.

Post by Therx »

There's a feeble doc i wrote about the RTC here.

Pete

(i'd appeciate the more advanced people not picking holes in my work as i did do it several years ago and I was young and foolish :D )
Post Reply