Page 1 of 1
How to implement a PS/2 keyboard driver?
Posted: Tue May 14, 2013 3:10 pm
by jhunterd
Hello all,
I am trying to implement a PS/2 keyboard driver in my operating system. The wiki page on PS/2 keyboards leaves out two important things for a noob like me:
1. What port should I use to interact with the keyboard?
2. After sending 0xF4, how to I listen for scan codes?
I did try searching the web and the wiki and I found nothing on this. Could anybody please help me?
Re: How to implement a PS/2 keyboard driver?
Posted: Tue May 14, 2013 3:23 pm
by Satoshi
Re: How to implement a PS/2 keyboard driver?
Posted: Tue May 14, 2013 3:28 pm
by jhunterd
Thank you, I'll update once I have this implemented in my kernel.
Re: How to implement a PS/2 keyboard driver?
Posted: Tue May 14, 2013 4:11 pm
by sortie
jhunterd: [Edit: A post has been removed, so disregard this. Please relax and remember that all forums have their trolls. You should report the post to the operators if you find it offensive rather than responding in anger. If you feel offended by a post, I can recommend taking a break and returning later when you are level-headed.]
I can see you are new here, welcome!
If you managed to write a keyboard driver, feel free to update the wiki article such that it would have helped you. I can recommend reading the CPU documentation sooner than later. Despite being heavy documents, it is crucial to learn to search them for the proper information. You can often find some basic keyboard drivers in various kernel tutorials, you can easily find some using google.
Re: How to implement a PS/2 keyboard driver?
Posted: Tue May 14, 2013 4:33 pm
by dozniak
Offended or not, spend a little bit more effort next time.
PS/2 keyboard driver is probably one of the
most overdocumented things on the internets.
Re: How to implement a PS/2 keyboard driver?
Posted: Tue May 14, 2013 4:34 pm
by dozniak
You never reset escaped to false, is that intentional?
Re: How to implement a PS/2 keyboard driver?
Posted: Tue May 14, 2013 4:36 pm
by Mikemk
The basic steps are:
1) wait for an interrupt
2) report to the driver to read the scan code
3] read scan code, and convert to something for OS use
4] return the character code
5) report to whatever program
A list of scancodes can be found at
http://wiki.osdev.org/PS/2_Keyboard#Scan_Code_Sets
The easiest method is to use a lookup table, but this probably isn't the best method.
Re: How to implement a PS/2 keyboard driver?
Posted: Tue May 14, 2013 9:05 pm
by gerryg400
jhunterd, I feel your pain a little bit. I just finished a PS/2 driver for my OS.
The wiki articles try to describe a proper PS/2 driver that actually manages the PS/2 controller and the things attached to it. If you are just starting out and doing something simple then it may not be necessary yet.
The link that dozniak provided is excellent.
Bloodman's code will likely work. It takes advantage of the fact that the the Bios probably leaves the hardware in a particular state that you might find useful. It will not work on all hardware (in fact it will lock up or crash some machines) and will present issues when you try to get a PS/2 mouse working but it is probably a useful starting point.
So in answer to your questions.
1. 0x60
2. If everything is enabled correctly you will get an interrupt when there is something to read. And you read the data on port 0x60.
Re: How to implement a PS/2 keyboard driver?
Posted: Wed May 15, 2013 3:30 pm
by jhunterd
Thanks to everybody for all the help. One other question: how exactly would I check for an interrupt?
Re: How to implement a PS/2 keyboard driver?
Posted: Wed May 15, 2013 3:45 pm
by sortie
You don't check for interrupts, they happen. You set up the IDT such that the relevant IRQ triggers your interrupt handler, which performs the keyboard access, and then returns to whatever the CPU was doing before it was interrupted. I recommend you look up some kernel development tutorials, they describe all this information. Then when you get a grasp of what is supposed to happen, I recommend you get the Intel/AMD manuals and look up the topics in the System Programming (or what it is called) volumes.
Re: How to implement a PS/2 keyboard driver?
Posted: Wed May 15, 2013 3:46 pm
by jhunterd
Okay. Thank you everybody for all of the help. I really appreciate it.