Page 1 of 1

Keyboard driver

Posted: Thu Jan 26, 2017 11:33 am
by Ruslan
I am developing my OS and I am trying to create a driver for a keyboard.

Please tell me,why is it not working and how can I use my functions right way.
===================keyboard.h===================

Code: Select all

typedef struct {
    uint8_t scancode;
    char chr; //Character it corresponds to.
} kbd_scancode;

static kbd_scancode regular_scancodes[] = {
  /* Numerical keys */
  {0x02, '1'}, {0x03, '2'}, {0x04, '3'}, {0x05, '4'}, {0x06, '5'}, {0x07, '6'}, {0x08, '7'}, {0x09, '8'}, {0x0A, '9'}, {0x0B, '0'}, 
  /* Some characters after numerical keys */
  {0x0C, '-'}, {0x0D, '='}, {0x0E, '\b'}, {0x0F, '\t'},
  /* Alphabet! */
  {0x10, 'q'}, {0x11, 'w'}, {0x12, 'e'}, {0x13, 'r'}, {0x14, 't'}, {0x15, 'y'}, {0x16, 'u'}, {0x17, 'i'}, {0x18, 'o'}, {0x19, 'p'}, {0x1A, '['}, {0x1B, ']'}, {0x1C, '\n'},
  {0x1E, 'a'}, {0x1F, 's'}, {0x20, 'd'}, {0x21, 'f'}, {0x22, 'g'}, {0x23, 'h'}, {0x24, 'j'}, {0x25, 'k'}, {0x26, 'l'}, {0x27, ';'}, {0x28, '\''}, {0x29, '`'},
  {0x2B, '\\'}, {0x2C, 'z'}, {0x2D, 'x'}, {0x2E, 'c'}, {0x2F, 'v'}, {0x30, 'b'}, {0x31, 'n'}, {0x32, 'm'}, {0x33, ','}, {0x34, '.'}, {0x35, '/'}, {0x37, '*'},
  {0x39, ' '}, {0x47, '7'}, {0x48, '8'}, {0x49, '9'}, {0x4A, '-'},
               {0x4B, '4'}, {0x4C, '5'}, {0x4D, '6'}, {0x4E, '+'},
               {0x4F, '1'}, {0x50, '2'}, {0x51, '3'},
               {0x52, '0'}, {0x53, '.'}, {0x00, '\0'}
};
static char kbd_buffer[256]={};
static uint8_t kbd_buffer_current_pos=0;
static void kbd_buffer_push(char value)
{
	if(kbd_buffer_current_pos!=255)
		kbd_buffer[kbd_buffer_current_pos++]=value;
	else
	{
		return;//Buffer  overflow
	}
}
static char kbd_buffer_pop()
{
	if(kbd_buffer_current_pos>0)
		return kbd_buffer[kbd_buffer_current_pos--];
	else
		return 0;//Nothing  in  the  buffer
}
char convert(uint8_t c)
{
	for(int i=0;regular_scancodes[i].scancode!=0x00;i++)
	{
		if(regular_scancodes[i].scancode==c)
			return regular_scancodes[i].chr;
		return 0;
	}
}
void handler()
{
	uint8_t scancode=inb(0x60);
	kbd_buffer_push(convert(scancode));
}
char kbd_getchar()
{
	char ret=0;
	while(!ret)
	{
		ret=kbd_buffer_pop();
	}
	return ret;
}
=============kernel.c=============

Code: Select all

#include "tty.h"
#include "io.h"
#include "keyboard.h"
#include "GDT.h"
void kmain(void)
{
	terminal_init(VGA_WHITE,VGA_BLACK);
	terminal_writestring("Creating  GDT  descriptors....");
	createDescriptors();
	int i=0;
	while(i<5000)
	{
	i++;
	handler();
	}
	terminal_writestring("Cought  key..");
	terminal_writestring(kbd_getchar());
}
P.S. sorry for mess

P.P.S. Write me if you want to work with me.

Re: Keyboard driver

Posted: Thu Jan 26, 2017 11:48 am
by MollenOS
I just finished my ps2 driver with drivers for both mouse and keyboard, with interrupts, not polling, but maybe it can help you to see how I did it

https://github.com/Fadekraft/MollenOS/t ... ch/x86/ps2

Re: Keyboard driver

Posted: Fri Jan 27, 2017 8:34 am
by Ruslan
I don't know.Your code is written on C++ and mine is on C.
I think,I can't understand it well.
So,Can you write me a basic example on C?Just a simple and easy.

Re: Keyboard driver

Posted: Fri Jan 27, 2017 9:04 am
by Ruslan
Oh,I forgot.When I print keyboard scancodes on the terminal,it just prints S.
Just 'S'.Nothing else
Please,help

Re: Keyboard driver

Posted: Fri Jan 27, 2017 9:59 am
by dozniak
Ruslan wrote:I don't know.Your code is written on C++ and mine is on C.
Terribly sorry to interrupt, but where did you find C++ there?

Re: Keyboard driver

Posted: Sat Jan 28, 2017 6:23 am
by Ruslan
Sorry,If I mistaken.I thought is was C++.

So,can you give me some examples?

Re: Keyboard driver

Posted: Sat Jan 28, 2017 9:08 am
by osdever
Dude, that's my code from kbd.c! :D

And... you used it COMPLETELY wrong. It should be called in your interrupt handler, not in loop.

Re: Keyboard driver

Posted: Sat Jan 28, 2017 9:43 am
by Gigasoft
I thought this looked familiar. You copied your code from a 12 year old, no wonder it doesn't work.

http://forum.osdev.org/viewtopic.php?f=1&t=31069

If you honestly can't figure out what's wrong by yourself, I strongly suggest finding another pastime.

Hint: Suppose the one byte has been pushed onto the input stack, then kbd_buffer_pop gets called. Where does it read its result from? Also, what would be the correct data structure to use if you wanted typed characters to appear to the caller in the order they were typed?

Re: Keyboard driver

Posted: Sat Jan 28, 2017 10:40 am
by osdever
Gigasoft wrote:I thought this looked familiar. You copied your code from a 12 year old, no wonder it doesn't work.

http://forum.osdev.org/viewtopic.php?f=1&t=31069

If you honestly can't figure out what's wrong by yourself, I strongly suggest finding another pastime.

Hint: Suppose the one byte has been pushed onto the input stack, then kbd_buffer_pop gets called. Where does it read its result from? Also, what would be the correct data structure to use if you wanted typed characters to appear to the caller in the order they were typed?
Wait, he copied the code from that thread? It's the buggy version, I fixed it long time ago. It works in current version of my OS, he just uses it wrong. He should make an interrupt handler for keyboard interrupt and call it only there, otherwise he won't get some adequate values at all.

To Ruslan:
Sorry if I'm being a bit rude, but you won't be able to use my code if you won't implement interrupts. Here's some tips:
1. You should implement interrupts as soon as possible, you will need them.
2. After that you can just call handler(); in your IRQ #1 handler. Also I strongly recommend you to rename handler() to something like "keyboard_irq_handler" as you will need more than one IRQ handler in your OS, you will need at least PIT and (maybe) mouse.
3. You just copy-pasted the code as I can see and even removed some features like modifier keys. Sorry, but you need to understand it first. At this point it looks that you didn't. This definitely won't be waste of time to read the code. Sorry if this was been too rude :)

Re: Keyboard driver

Posted: Sat Jan 28, 2017 10:43 am
by osdever
And as I seen in your post you put it to .h (header) file. Separate it to .h and .c files. My code was in .c file only because these structures are only for driver internal use and shouldn't be declared somewhere outside. You can do the same.

Re: Keyboard driver

Posted: Sat Jan 28, 2017 12:39 pm
by MollenOS
OP can't tell the difference from C/C++ properly, I really don't believe OS development is a task he/she is suited for.

Re: Keyboard driver

Posted: Sat Jan 28, 2017 1:38 pm
by osdever
MollenOS wrote:OP can't tell the difference from C/C++ properly, I really don't believe OS development is a task he/she is suited for.
He. Ruslan is Russian male name.

Re: Keyboard driver

Posted: Sun Jan 29, 2017 7:35 am
by Ruslan
And... you used it COMPLETELY wrong. It should be called in your interrupt handler, not in loop.
Then,can you give me an example how to use it,please? :D

Re: Keyboard driver

Posted: Sun Jan 29, 2017 7:45 am
by osdever
Ruslan wrote:
And... you used it COMPLETELY wrong. It should be called in your interrupt handler, not in loop.
Then,can you give me an example how to use it,please? :D
I can't give you an example as it differs per OS. Call handler() in IRQ #1 handler.