How to implement a PS/2 keyboard driver?

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
jhunterd
Posts: 12
Joined: Tue May 14, 2013 3:03 pm

How to implement a PS/2 keyboard driver?

Post 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?
User avatar
Satoshi
Member
Member
Posts: 28
Joined: Thu Sep 13, 2012 2:18 pm

Re: How to implement a PS/2 keyboard driver?

Post by Satoshi »

Trinix (written in D) https://github.com/Rikarin/Trinix
Streaming OS development https://www.livecoding.tv/satoshi/
jhunterd
Posts: 12
Joined: Tue May 14, 2013 3:03 pm

Re: How to implement a PS/2 keyboard driver?

Post by jhunterd »

Thank you, I'll update once I have this implemented in my kernel.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: How to implement a PS/2 keyboard driver?

Post 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.
Last edited by sortie on Wed May 15, 2013 3:27 am, edited 1 time in total.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: How to implement a PS/2 keyboard driver?

Post 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.
Learn to read.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: How to implement a PS/2 keyboard driver?

Post by dozniak »

You never reset escaped to false, is that intentional?
Learn to read.
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: How to implement a PS/2 keyboard driver?

Post 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.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: How to implement a PS/2 keyboard driver?

Post 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.
If a trainstation is where trains stop, what is a workstation ?
jhunterd
Posts: 12
Joined: Tue May 14, 2013 3:03 pm

Re: How to implement a PS/2 keyboard driver?

Post by jhunterd »

Thanks to everybody for all the help. One other question: how exactly would I check for an interrupt?
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: How to implement a PS/2 keyboard driver?

Post 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.
jhunterd
Posts: 12
Joined: Tue May 14, 2013 3:03 pm

Re: How to implement a PS/2 keyboard driver?

Post by jhunterd »

Okay. Thank you everybody for all of the help. I really appreciate it.
Post Reply