Rev 666 |
Rev 826 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*
* Project: S.Ha.R.K.
*
* Coordinators:
* Giorgio Buttazzo <giorgio@sssup.it>
* Paolo Gai <pj@gandalf.sssup.it>
*
* Authors :
* Paolo Gai <pj@gandalf.sssup.it>
* Massimiliano Giorgi <massy@gandalf.sssup.it>
* Luca Abeni <luca@gandalf.sssup.it>
* Mauro Marinoni <mauro.marinoni@unipv.it>
* (see the web pages for full authors list)
*
* ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
*
* http://www.sssup.it
* http://retis.sssup.it
* http://shark.sssup.it
*/
//#define __KEYB_DEBUG__
#define KEYB_TASK
#include <kernel/kern.h>
#include <signal.h>
#include <modules/hartport.h>
#include "../include/drivers/shark_input26.h"
#include "../include/drivers/shark_keyb26.h"
/* Devices */
extern int atkbd_init
(void);
extern int atkbd_exit
(void);
/* Handlers */
extern int kbd_init
(void);
extern int kbd_exit
(void);
/* Functions */
extern int kbd_enable
(void);
extern int kbd_disable
(void);
extern int kbd_get
(unsigned int *data
, BYTE access
);
extern void kbd_setleds
(unsigned int led
);
extern int kbd_rate
(unsigned int *delay
, unsigned int *period
);
extern void kbd_mksound
(unsigned int hz
, unsigned int ticks
);
/*
* The following tables contains the ascii code corresponding to the
* scan codes, with shift & no shift...
*/
char keyTable
[TABLE_KEY_SIZE
];
char keyShiftTable
[TABLE_KEY_SIZE
];
char keyAltGrTable
[TABLE_KEY_SIZE
];
BYTE useAltGr
= FALSE
;
/* Status variables */
static BYTE shift
= FALSE
;
static BYTE ctrl
= FALSE
;
static BYTE alt
= FALSE
;
static BYTE altgr
= FALSE
;
static BYTE capslock
= FALSE
;
static BYTE numlock
= TRUE
;
static BYTE scrolllock
= FALSE
;
/* Keyboard Flags (if shift, alt or control are pressed...); */
static BYTE keyFlag
= 0x00;
static BYTE status
;
/* Keyboard driver currently installed */
static int keyb_installed
= FALSE
;
#define MAX_KEY_EXC 50
static struct keyexctable
{
KEY_EVT evt
;
void (*func
) (KEY_EVT
* k
);
unsigned char lock
;
} keyExcTable
[MAX_KEY_EXC
];
static int lastExc
;
/* Communication port between the extern process & the user */
static PORT pkeyPort
, ukeyPort
;
#ifdef KEYB_TASK
/* keyboard task PID */
static PID keybpid
;
#else
static void keyProc
(void);
#endif
/*
* Start keyProc Task or exec it as function
*/
void shark_kbd_exec
(void)
{
#ifdef KEYB_TASK
task_activate
(keybpid
);
#else
keyProc
();
#endif
}
/*
* This function get a scan code, return an ascii code and set
* the status vars in order to handle the special keys of the AT keyboard
*/
WORD scanCode
(unsigned int c
, int d
)
{
//printk("scanCode: c (%x) - d (%d)\n", c, d);
/* KEY_EVT status setting */
status
= 1 << d
;
switch (c
) {
/* CapsLock pressed*/
case KEY_CPSLOCK
:
if (d
== KEY_PRESSED
) {
capslock
= capslock
? FALSE
: TRUE
;
/* light the caps lock led */
kbd_setleds
(scrolllock
+ (numlock
<< 1) + (capslock
<< 2));
}
return 0;
/* NumLock pressed */
case PAD_NUMLOCK
:
if (d
== KEY_PRESSED
) {
numlock
= numlock
? FALSE
: TRUE
;
/* light the num lock led */
kbd_setleds
(scrolllock
+ (numlock
<< 1) + (capslock
<< 2));
}
return 0;
/* ScrollLock pressed*/
case EXT_SCRLOCK
:
if (d
== KEY_PRESSED
) {
scrolllock
= scrolllock
? FALSE
: TRUE
;
/* light the scroll lock led */
kbd_setleds
(scrolllock
+ (numlock
<< 1) + (capslock
<< 2));
}
return 0;
/* Shift pressed or released */
case KEY_SHL
:
if (d
) {
shift
= TRUE
;
if (c
== KEY_SHL
)
keyFlag
|= SHFL_BIT
;
} else {
keyFlag
&= (!SHFL_BIT
);
if (!(keyFlag
& SHFR_BIT
))
shift
= FALSE
;
}
return 0;
/* Shift pressed or released */
case KEY_SHR
:
if (d
) {
shift
= TRUE
;
if (c
== KEY_SHR
)
keyFlag
|= SHFR_BIT
;
} else {
keyFlag
&= (!SHFR_BIT
);
if (!(keyFlag
& SHFL_BIT
))
shift
= FALSE
;
}
return 0;
/* Control pressed or released */
case KEY_CTRLL
:
if (d
) {
ctrl
= TRUE
;
keyFlag
|= CNTL_BIT
;
} else {
keyFlag
&= (!CNTL_BIT
);
if (!(keyFlag
& CNTR_BIT
))
ctrl
= FALSE
;
}
return 0;
/* Control pressed or released */
case KEY_CTRLR
:
if (d
) {
ctrl
= TRUE
;
keyFlag
|= CNTR_BIT
;
} else {
keyFlag
&= (!CNTR_BIT
);
if (!(keyFlag
& CNTL_BIT
))
ctrl
= FALSE
;
}
return 0;
/* Alt Left pressed */
case KEY_ALTL
:
if (d
) {
alt
= TRUE
;
keyFlag
|= ALTL_BIT
;
} else {
alt
= FALSE
;
keyFlag
&= (!ALTL_BIT
);
}
return 0;
/* Alt Right (AltGr) pressed */
case KEY_ALTR
:
if (d
) {
altgr
= TRUE
;
keyFlag
|= ALTR_BIT
;
} else {
altgr
= FALSE
;
keyFlag
&= (!ALTR_BIT
);
if ((!useAltGr
) && (!(keyFlag
& ALTL_BIT
)))
alt
= FALSE
;
}
return 0;
/* Delete */
case EXT_DEL
:
return DELETE
;
case PAD_DEL
:
if (numlock
|| shift
)
return 0x18;
else
break;
/* Pad Enter */
case PAD_ENT
:
return 0x0d;
/* Pad Add */
case PAD_PLUS
:
return 0x2b;
/* Pad Sub */
case PAD_SUB
:
return 0x2d;
/* Pad Slash */
case PAD_SLH
:
return 0x2f;
/* Numbers & simbols */
case KEY_1
: case KEY_2
: case KEY_3
: case KEY_4
: case KEY_5
:
case KEY_6
: case KEY_7
: case KEY_8
: case KEY_9
: case KEY_0
:
case KEY_SUB
: case KEY_PLUS
: case KEY_BRL
: case KEY_BRR
:
case KEY_COL
: case KEY_API
: case KEY_TIL
: case KEY_BSL
:
case KEY_LT
: case KEY_GT
: case KEY_SLH
:
/* AlrGR enabled & pressed */
if (altgr
&& useAltGr
) {
return keyAltGrTable
[c
];
}
/* Control Shift status */
if (shift
)
return keyShiftTable
[c
];
else
return keyTable
[c
];
}
/* Pad Keys */
if (numlock
|| shift
)
switch (c
) {
case PAD_DEL
:
return 0x18;
case PAD_INS
:
return 0x30;
case PAD_END
:
return 0x31;
case PAD_DOWN
:
return 0x32;
case PAD_PGDW
:
return 0x33;
case PAD_RIGHT
:
return 0x34;
case PAD_5
:
return 0x35;
case PAD_LEFT
:
return 0x36;
case PAD_HOME
:
return 0x37;
case PAD_UP
:
return 0x38;
case PAD_PGUP
:
return 0x39;
}
/* Characters keys */
if (((c
>= KEY_Q
) && (c
<= KEY_P
)) ||
((c
>= KEY_A
) && (c
<= KEY_L
)) ||
((c
>= KEY_Z
) && (c
<= KEY_M
))) {
/* AlrGR enabled & pressed */
if (altgr
&& useAltGr
) {
return keyAltGrTable
[c
];
}
/* Control CapsLock & Shift status */
if (capslock
) {
if (shift
)
return keyTable
[c
];
else
return keyShiftTable
[c
];
} else {
if (shift
)
return keyShiftTable
[c
];
else
return keyTable
[c
];
}
}
/* Remaining keys */
if (c
< TABLE_KEY_SIZE
)
/* Left 'low' keys (Esc, BackSpace, Space, ...) */
return keyTable
[c
];
else
/* Default - Return as keycode */
return (0xff00 | c
);
}
#ifdef KEYB_TASK
TASK keyProc
(void)
#else
static void keyProc
(void)
#endif
{
WORD code
;
BYTE found
;
KEY_EVT dt
;
int i
, res
;
unsigned int dato
;
#ifdef KEYB_TASK
while (1) {
#endif
res
= kbd_get
(&dato
, NON_BLOCK
);
if (res
>= 0) {
code
= scanCode
(dato
, res
);
if (code
!= 0) {
if (code
& 0xff00)
/* It's a scan code, set the right bit */
dt.
flag = (keyFlag
| SCAN_BIT
);
else
/* Simply send the keyFlag status */
dt.
flag = keyFlag
;
dt.
status = status
;
dt.
ascii = (BYTE
) (code
& 0x00FF);
dt.
scan = dato
;
#ifdef __KEYB_DEBUG__
printk
("shark_keyb.c: KEY_EVT ( %2x - %c - %2x - %1d)\n", dt.
scan, dt.
ascii, dt.
flag, dt.
status);
#endif
found
= FALSE
;
for (i
= 0; i
< lastExc
; i
++)
if (((keyExcTable
[i
].
evt.
flag & ~SCAN_BIT
) == (dt.
flag & ~SCAN_BIT
)) &&
((keyExcTable
[i
].
evt.
status & dt.
status) != 0)) {
if ( ((keyExcTable
[i
].
evt.
scan == dt.
scan) || (keyExcTable
[i
].
evt.
scan == 0)) &&
((keyExcTable
[i
].
evt.
ascii == dt.
ascii) || (keyExcTable
[i
].
evt.
ascii == 0)) ){
#ifdef __KEYB_DEBUG__
printk
("shark_keyb.c: Key_Hook ( %2x - %2x - %1d) -> ( %2x - %2x - %1d)\n",
dt.
scan, dt.
ascii, dt.
flag, dt.
status,
keyExcTable
[i
].
evt.
scan, keyExcTable
[i
].
evt.
ascii, keyExcTable
[i
].
evt.
flag, keyExcTable
[i
].
evt.
status);
#endif
keyExcTable
[i
].
func(&dt
);
if (keyExcTable
[i
].
lock == TRUE
)
found
= TRUE
;
}
}
/* when the port is full, data is lost */
if (!found
)
port_send
(pkeyPort
, (BYTE
*) (&dt
), NON_BLOCK
);
}
}
#ifdef KEYB_TASK
task_endcycle
();
}
#endif
}
/* default function called on ctrl-c */
void default_ctrlChandler
(KEY_EVT
* k
) {
set_active_page
(0);
set_visual_page
(0);
//cputs("Ctrl-C pressed!\n");
sys_end
();
}
/**** Start User Functions ****/
/* Function that returns the ascii code */
BYTE keyb_getch
(BYTE wait
)
{
KEY_EVT ev
;
BYTE fl
;
fl
= port_receive
(ukeyPort
, &ev
, wait
);
if (fl
) {
#ifdef __KEYB_DEBUG__
printk
("shark_keyb.c: GetChar ( %2x - %c - %2x - %1d)\n", ev.
scan, ev.
ascii, ev.
flag, ev.
status);
#endif
if (!isScanCode
(ev
) && !isReleased
(ev
))
return (ev.
ascii);
else
return 0;
} else
return 0;
}
/*
* Function that returns a structure containing the flags status, the ascii
* code, and the scan code
*/
int keyb_getcode
(KEY_EVT
* k
, BYTE wait
)
{
return (port_receive
(ukeyPort
, (BYTE
*) (k
), wait
));
}
void keyb_hook
(KEY_EVT k
, void (*f
) (KEY_EVT
* k
), unsigned char l
)
{
if (lastExc
>= MAX_KEY_EXC
)
return;
keyExcTable
[lastExc
].
evt = k
;
keyExcTable
[lastExc
].
func = f
;
keyExcTable
[lastExc
++].
lock = l
;
return;
}
/* This function disable the keyboard */
int keyb_disable
(void)
{
kbd_disable
();
return 0;
}
/* This function enable the keyboard */
int keyb_enable
(void)
{
kbd_enable
();
return 0;
}
/**** End User Functions ****/
int KEYB26_installed
(void)
{
return keyb_installed
;
}
int KEYB26_init
(KEYB_PARMS
*s
)
{
KEYB_PARMS kparms
= BASE_KEYB
;
WORD i
;
int status
= 0;
#ifdef KEYB_TASK
SOFT_TASK_MODEL base_m
;
TASK_MODEL
*m
;
#endif
if (keyb_installed
== TRUE
) return 0;
/* if a NULL is passed */
if (s
== NULL
)
s
= &kparms
;
for (i
= 0; i
< TABLE_KEY_SIZE
; i
++) {
keyTable
[i
] = 0;
keyShiftTable
[i
] = 0;
keyAltGrTable
[i
] = 0;
}
/* keymap */
if (s
->keymap
== (unsigned char)KEYB_DEFAULT
) s
->keymap
= KEYMAP_US
;
keyb_set_map
(s
->keymap
);
pkeyPort
= port_create
("KeybPort", sizeof(KEY_EVT
), 20, STREAM
, WRITE
);
if (pkeyPort
== -1)
return -2;
ukeyPort
= port_connect
("KeybPort", sizeof(KEY_EVT
), STREAM
, READ
);
if (ukeyPort
== -1) {
port_delete
(pkeyPort
);
return -3;
}
/* remove all key pressed handlers */
lastExc
= 0;
/* and add a ctrl-c handler if requested */
if (s
->ctrlcfunc
== (void *) KEYB_DEFAULT
)
s
->ctrlcfunc
= (void *) default_ctrlChandler
;
if (s
->ctrlcfunc
!= NULL
) {
KEY_EVT emerg
;
emerg.
ascii = 'c';
emerg.
scan = KEY_C
;
emerg.
status = KEY_PRESSED
;
emerg.
flag = CNTL_BIT
;
keyb_hook
(emerg
, s
->ctrlcfunc
, TRUE
);
emerg.
flag = CNTR_BIT
;
keyb_hook
(emerg
, s
->ctrlcfunc
, TRUE
);
}
#ifdef KEYB_TASK
/* keyb task */
if (s
->tm
== (TASK_MODEL
*) KEYB_DEFAULT
) {
soft_task_default_model
(base_m
);
soft_task_def_wcet
(base_m
, 2000);
soft_task_def_met
(base_m
, 800);
soft_task_def_period
(base_m
, 25000);
soft_task_def_system
(base_m
);
soft_task_def_aperiodic
(base_m
);
m
= (TASK_MODEL
*) & base_m
;
} else
m
= s
->tm
;
#ifdef __KEYB_DEBUG__
//printk(KERN_DEBUG "keyb task: %li %li %li %li\n", (long)s->pclass, (long)APERIODIC, (long)s->dline, (long)s->wcet);
#endif
keybpid
= task_create
("KeyTask", keyProc
, m
, NULL
);
if (keybpid
== -1) {
port_delete
(pkeyPort
);
port_delete
(ukeyPort
);
return -1;
}
#endif
if (INPUT26_installed
() == FALSE
)
if (INPUT26_init
()) {
printk
(KERN_ERR
"shark_keyb.c: Unable to open Input SubSystem.\n");
port_delete
(pkeyPort
);
port_delete
(ukeyPort
);
#ifdef KEYB_TASK
task_kill
(keybpid
);
#endif
return -1;
}
status
= kbd_init
();
if (status
) {
printk
(KERN_ERR
"shark_keyb.c: Kbd_Init return: %d\n", status
);
port_delete
(pkeyPort
);
port_delete
(ukeyPort
);
#ifdef KEYB_TASK
task_kill
(keybpid
);
#endif
return -1;
}
status
= atkbd_init
();
if (status
) {
printk
(KERN_ERR
"shark_keyb.c: AtKbd_Init return: %d\n", status
);
port_delete
(pkeyPort
);
port_delete
(ukeyPort
);
#ifdef KEYB_TASK
task_kill
(keybpid
);
#endif
return -1;
}
kbd_setleds
(scrolllock
+ (numlock
<< 1) + (capslock
<< 2));
kbd_enable
();
keyb_installed
= TRUE
;
return status
;
}
/*
* KEYB module cleanup
* (must be called if it's needed to re-call keyb_init() with other parameters)
* -1 -> ERROR
*/
int KEYB26_close
(void)
{
#ifdef KEYB_TASK
int free;
SYS_FLAGS f
;
#endif
if (!keyb_installed
)
return -1;
kbd_disable
();
atkbd_exit
();
kbd_exit
();
#ifdef KEYB_TASK
f
= kern_fsave
();
free = (proc_table
[keybpid
].
status == FREE
);
kern_frestore
(f
);
#ifdef __KEYB_DEBUG__
printk
(KERN_DEBUG
"shark_keyb.c: KeyTask is %s.\n", free ? "killed" : "alive");
#endif
if (free)
task_kill
(keybpid
);
#endif
/*port_delete (pkeyPort);
port_delete (ukeyPort);*/
keyb_installed
= FALSE
;
return 0;
}