Page 1 of 2
How do i write a keyboard driver?
Posted: Thu Feb 14, 2008 11:03 am
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
Posted: Thu Feb 14, 2008 11:11 am
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
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.
Posted: Thu Feb 14, 2008 11:21 am
by Karlosoft
Thanks you, but i don't write in c/c++, i need to write the keyboard driver in assembly.
Posted: Thu Feb 14, 2008 11:28 am
by Combuster
well then, you should be able to convert that code,
no?
Posted: Thu Feb 14, 2008 11:30 am
by Karlosoft
I think so...
Posted: Thu Feb 14, 2008 11:31 am
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
Posted: Thu Feb 14, 2008 11:43 am
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.
Posted: Thu Feb 14, 2008 11:43 am
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...
Posted: Thu Feb 14, 2008 11:46 am
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...
Posted: Thu Feb 14, 2008 11:57 am
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 ...
Posted: Thu Feb 14, 2008 11:59 am
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?
Posted: Thu Feb 14, 2008 12:09 pm
by Karlosoft
Posted: Thu Feb 14, 2008 1:40 pm
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...
Posted: Thu Feb 14, 2008 2:47 pm
by Combuster
but i don't have find any tutorial that say how do...
look harder
Posted: Thu Feb 14, 2008 4:05 pm
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