How do i write a 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.
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

How do i write a keyboard driver?

Post by Karlosoft »

Hi, i've a question (perhaps no only one). How do i can write a driver for my keyboard? I've heard about the PCI and the IRQ but i haven't understand how do i use them...

P.S As you may have noticed, i'm not english and i cant tell it well...
If I will write some mistakes please forgive me :)
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Post by gzaloprgm »

Hello Karlosoft, it's very easy to write it.

First you'll have to register a dummy handler in IRQ1 (keyboard irq), in my kernel I do the following:

Code: Select all

register_interrupt_handler(IRQ1, &keyboard_handler);
But in yours it may different.

Then I have a keyboard_handler function which is like the following

Code: Select all

void keyboard_handler(registers_t regs){
	unsigned char key = inportb(0x60);
	if(key < 0x80){
		putch(keyboard_map_us[key]);

	}
}
Keyboard_map_us is an array of unsigned char, US key map is here, for making italian map you'll need to search for it or write yourself the table manually :P
http://www.osdever.net/bkerndev/Sources/kb.c

Cheers,
Gonzalo

ps. you'll need pci for loading list of devices in pci bus, not for this.
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Post by Karlosoft »

Thanks you, but i don't write in c/c++, i need to write the keyboard driver in assembly.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

well then, you should be able to convert that code,no?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Post by Karlosoft »

I think so...
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Post by gzaloprgm »

I assume you have Idt, isrs and irq correctly loaded.

Code: Select all

irq1:
    cli
    pusha                    ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax

    mov ax, ds               ; Lower 16-bits of eax = ds.
    push eax                 ; save the data segment descriptor

    mov ax, 0x10  ; load the kernel data segment descriptor
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    outb 0x20, 0x20 ;send signal to pic
    
    inb 0x60, ax ;read code to ax (?)

    ;do what you want here with ax 
   
    pop ebx        ; reload the original data segment descriptor
    mov ds, bx
    mov es, bx
    mov fs, bx
    mov gs, bx

    popa                     ; Pops edi,esi,ebp...
    sti
    iret           ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
I don't think that works but I hope you understand the idea.

Cheers,
Gonzalo
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Post by 01000101 »

same principles to follow.

read the scancode from the keyboard port, then run the scancode value through an array of keyboard characters (unique to each region).

then you can print the returned array value to the screen.
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Post by Karlosoft »

I assume you have Idt, isrs and irq correctly loaded.
I must write something to use this code? I mean, i don't have understand if i must set the irq lines or no

P.S
Excuse my ignorance, but it's difficult to find italian books about assembly, so I must search them in english web-sites, and i don't always understand all...
Last edited by Karlosoft on Thu Feb 14, 2008 11:52 am, edited 1 time in total.
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Post by Karlosoft »

same principles to follow.

read the scancode from the keyboard port, then run the scancode value through an array of keyboard characters (unique to each region).

then you can print the returned array value to the screen.
And all is ok, but the problem ist that an italian tutorial about "How to write a simle OS" sayd that I musst set the irq lines, the pci with many commands. Now i don't understand what do...
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Post by Karlosoft »

ps. you'll need pci for loading list of devices in pci bus, not for this.

Ok, so i don't have to write any settings for the pci

P.S Excuse me for three consecutive post ...
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Post by gzaloprgm »

I think you or the author of the text are confusing two different terms.

Programmable Interrupt Controller (PIC) vs.
Peripheral Component Interconnect (PCI)

And yes, you'll need to reprogram the pic and give it the interrupt vector.

Gonzalo

ps. can you post the link of the tutorial?
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Post by Karlosoft »

Ops I wrote PCI but it was PIC... :oops: :oops: :oops:
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Post by Karlosoft »

And yes, you'll need to reprogram the pic and give it the interrupt vector.
I've read that i must use some commands (ocw1,icw1....) to programm the PIC, but i don't have find any tutorial that say how do...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

but i don't have find any tutorial that say how do...
look harder
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
exkor
Member
Member
Posts: 111
Joined: Wed May 23, 2007 9:38 pm

Post by exkor »

take a look at the file I posted here
it's in Fasm, and has protected mode, idt & keybord hadndler for ps2 kbd.
Could be good as a template or starting point. Has config file for bochs.

PIC
interrupts
Post Reply