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
Ruslan
Posts: 5
Joined: Wed Jan 25, 2017 9:54 am

Keyboard driver

Post 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.
MollenOS
Member
Member
Posts: 202
Joined: Wed Oct 26, 2011 12:00 pm

Re: Keyboard driver

Post 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
Ruslan
Posts: 5
Joined: Wed Jan 25, 2017 9:54 am

Re: Keyboard driver

Post 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.
Ruslan
Posts: 5
Joined: Wed Jan 25, 2017 9:54 am

Re: Keyboard driver

Post by Ruslan »

Oh,I forgot.When I print keyboard scancodes on the terminal,it just prints S.
Just 'S'.Nothing else
Please,help
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Keyboard driver

Post 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?
Learn to read.
Ruslan
Posts: 5
Joined: Wed Jan 25, 2017 9:54 am

Re: Keyboard driver

Post by Ruslan »

Sorry,If I mistaken.I thought is was C++.

So,can you give me some examples?
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Keyboard driver

Post 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.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Keyboard driver

Post 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?
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Keyboard driver

Post 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 :)
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Keyboard driver

Post 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.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
MollenOS
Member
Member
Posts: 202
Joined: Wed Oct 26, 2011 12:00 pm

Re: Keyboard driver

Post 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.
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Keyboard driver

Post 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.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
Ruslan
Posts: 5
Joined: Wed Jan 25, 2017 9:54 am

Re: Keyboard driver

Post 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
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Keyboard driver

Post 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.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
Post Reply