atkbd.c
isa/atkbd.h
isa/8259.h
sys/video.h
sys/idt.h
sys/gdt.h
sys/io.h
lib/kmalloc.h
lib/kprintf.h
ubixos/types.h
ubixos/sched.h
ubixos/endtask.h
ubixos/tty.h
ubixos/spinlock.h
ubixos/kpanic.h
ubixos/vitals.h
sys/thread.h
ubixos/kpanic.h
ubixos/sched.h
ubixos/spinlock.h
ubixos/endtask.h
ubixos/vitals.h
ubixos/types.h
sys/_types.h
sys/video.h
ubixos/time.h
vfs/file.h
sys/tss.h
lib/kmalloc.h
sys/idt.h
vfs/vfs.h
vfs/mount.h
lib/kprintf.h
isa/atkbd.h
sys/gdt.h
ubixos/ubthread.h
ubixfs/dirCache.h
src/sys/isa/atkbd.c
sys/sysproto.h
sys/signal.h
ubixos/tty.h
sys/io.h
isa/8259.h
asm
(".globl atkbd_isr \n""atkbd_isr: \n"" pusha \n"" push %ss \n"" push %ds \n"" push %es \n"" push %fs \n"" push %gs \n"" call keyboardHandler \n"" mov $0x20,%dx \n"" mov $0x20,%ax \n"" outb %al,%dx \n"" pop %gs \n"" pop %fs \n"" pop %es \n"" pop %ds \n"" pop %ss \n"" popa \n"" iret \n")
asm
".globl atkbd_isr \n""atkbd_isr: \n"" pusha \n"" push %ss \n"" push %ds \n"" push %es \n"" push %fs \n"" push %gs \n"" call keyboardHandler \n"" mov $ 0x20
%dx\n""mov $ 0x20
%ax\n""outb% al
%dx\n""pop%gs\n""pop%fs\n""pop%es\n""pop%ds\n""pop%ss\n""popa\n""iret\n"
int int atkbd_init () atkbd_init atkbd_isr atkbd_scan dDpl0 dInt dPresent irqEnable kprintf mVec setLED setVector stdinBuffer x1
int static int atkbd_scan () atkbd_scan inportByte outportByte atkbd_init keyboardHandler
unsigned char unsigned char getch () getch tty_termNode::stdin stdinBuffer tty_termNode::stdinSize stdinSize tty_foreground x1 sysFgetc
void void keyboardHandler () keyboardHandler altKey atkbd_scan atkbdSpinLock backSpace controlKey controlKeys vitalsStruct::freePages inportByte K_PANIC keyboardMap keyMap kprintf ledCapslock ledNumlock ledScrolllock ledStatus outportByte setLED shiftKey spinTryLock spinUnlock tty_termNode::stdin stdinBuffer tty_termNode::stdinSize stdinSize systemVitals tty_change tty_foreground
void void setLED () setLED inportByte ledStatus outportByte atkbd_init keyboardHandler
spinLock_t spinLock_t atkbdSpinLock atkbdSpinLock SPIN_LOCK_INITIALIZER keyboardHandler
uInt32 uInt32 controlKeys controlKeys 0x0 keyboardHandler
unsigned int unsigned int keyboardMap[255][8] [255][8] keyboardMap keyboardHandler
unsigned int unsigned int keyMap keyMap 0x0 keyboardHandler
unsigned int unsigned int ledStatus ledStatus 0x0 keyboardHandler setLED
char char stdinBuffer[512] [512] stdinBuffer atkbd_init getch keyboardHandler
uInt16 uInt16 stdinSize stdinSize getch keyboardHandler

    /*****************************************************************************************
    Copyright(c)2002-2004TheUbixOSProject
    Allrightsreserved.

    Redistributionanduseinsourceandbinaryforms,withorwithoutmodification,are
    permittedprovidedthatthefollowingconditionsaremet:

    Redistributionsofsourcecodemustretaintheabovecopyrightnotice,thislistof
    conditions,thefollowingdisclaimerandthelistofauthors.Redistributionsinbinary
    formmustreproducetheabovecopyrightnotice,thislistofconditions,thefollowing
    disclaimerandthelistofauthorsinthedocumentationand/orothermaterialsprovided
    withthedistribution.NeitherthenameoftheUbixOSProjectnorthenamesofits
    contributorsmaybeusedtoendorseorpromoteproductsderivedfromthissoftware
    withoutspecificpriorwrittenpermission.

    THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS"ASIS"ANDANY
    EXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THEIMPLIEDWARRANTIESOF
    MERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALL
    THECOPYRIGHTOWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,
    SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENT
    OFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)
    HOWEVERCAUSEDANDONANYTHEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,OR
    TORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOFTHIS
    SOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.

    $Id: atkbd_8c.xml 88 2016-01-12 00:11:29Z reddawg $

    *****************************************************************************************/

    #include<isa/atkbd.h>
    #include<isa/8259.h>
    #include<sys/video.h>
    #include<sys/idt.h>
    #include<sys/gdt.h>
    #include<sys/io.h>
    #include<lib/kmalloc.h>
    #include<lib/kprintf.h>
    #include<ubixos/types.h>
    #include<ubixos/sched.h>
    #include<ubixos/endtask.h>
    #include<ubixos/tty.h>
    #include<ubixos/spinlock.h>
    #include<ubixos/kpanic.h>
    #include<ubixos/vitals.h>

    staticintatkbd_scan();

    staticunsignedintkeyMap=0x0;
    staticunsignedintledStatus=0x0;
    staticcharstdinBuffer[512];
    staticuInt16stdinSize;
    staticuInt32controlKeys=0x0;

    staticspinLock_tatkbdSpinLock=SPIN_LOCK_INITIALIZER;

    staticunsignedintkeyboardMap[255][8]={
    /*Ascii,Shift,Ctrl,Alt,Num,Caps,ShiftCaps,ShiftNum*/
    {0,0,0,0,0,0,0,0},
    /*ESC*/{0x1B,0x1B,0x1B,0x1B,0x1B,0x1B,0x1B,0x1B},
    /*1,!*/{0x31,0x21,0,0,0x31,0x31,0x21,0x21},
    /*2,@*/{0x32,0x40,0,0,0x32,0x32,0x40,0x40},
    /*3,#*/{0x33,0x23,0,0,0x33,0x33,0x23,0x23},
    /*4,$*/{0x34,0x24,0,0,0x34,0x34,0x24,0x24},
    /*5,%*/{0x35,0x25,0,0,0x35,0x35,0x25,0x25},
    /*6,^*/{0x36,0x5E,0,0,0x36,0x36,0x5E,0x5E},
    /*7,&*/{0x37,0x26,0,0,0x37,0x37,0x26,0x26},
    /*8,**/{0x38,0x2A,0,0,0x38,0x38,0x2A,0x2A},
    /*9.(*/{0x39,0x28,0,0,0x39,0x39,0x28,0x28},
    /*0,)*/{0x30,0x29,0,0,0x30,0x30,0x29,0x29},
    /*-,_*/{0x2D,0x5F,0,0,0x2D,0x2D,0x5F,0x5F},
    /*=,+*/{0x3D,0x2B,0,0,0x3D,0x3D,0x2B,0x2B},
    /*14*/{0x08,0x08,0x8,0x8,0x08,0x08,0x08,0x08},
    /*15*/{0x09,0,0,0,0,0,0,0},
    /**/{0x71,0x51,0,0,0,0,0,0},
    /**/{0x77,0x57,0,0,0,0,0,0},
    /**/{0x65,0x45,0,0,0,0,0,0},
    /**/{0x72,0x52,0,0,0,0,0,0},
    /**/{0x74,0x54,0,0,0,0,0,0},
    /**/{0x79,0x59,0,0,0,0,0,0},
    /**/{0x75,0x55,0,0,0,0,0,0},
    /**/{0x69,0x49,0,0,0,0,0,0},
    /**/{0x6F,0x4F,0,0,0,0,0,0},
    /**/{0x70,0x50,0,0,0,0,0,0},
    /**/{0x5B,0x7B,0,0,0,0,0,0},
    /**/{0x5D,0x7D,0,0,0,0,0,0},
    /**/{0x0A,0,0,0,0,0,0,0},
    /**/{0,0,0,0,0,0,0,0},
    /*a,A*/{0x61,0x41,0x41,0,0,0,0,0},
    /**/{0x73,0x53,0,0,0,0,0,0},
    /**/{0x64,0x44,0,0,0,0,0,0},
    /**/{0x66,0x46,0,0,0,0,0,0},
    /**/{0x67,0x47,0,0,0,0,0,0},
    /**/{0x68,0x48,0,0,0,0,0,0},
    /**/{0x6A,0x4A,0,0,0,0,0,0},
    /**/{0x6B,0x4B,0,0,0,0,0,0},
    /**/{0x6C,0x4C,0,0,0,0,0,0},
    /**/{0x3B,0x3A,0,0,0,0,0,0},
    /**/{0x27,0x22,0,0,0,0,0,0},
    /**/{0x60,0x7E,0,0,0,0,0,0},
    /**/{0x2A,0x0,0,0,0,0,0,0},
    /**/{0x5C,0x3C,0,0,0,0,0,0},
    /**/{0x7A,0x5A,0,0,0,0,0,0},
    /**/{0x78,0x58,0,0,0,0,0,0},
    /*c,C*/{0x63,0x43,0x3,0x9,0,0,0,0},
    /**/{0x76,0x56,0,0,0,0,0,0},
    /**/{0x62,0x42,0,0,0,0,0,0},
    /**/{0x6E,0x4E,0,0,0,0,0,0},
    /**/{0x6D,0x4D,0,0,0,0,0,0},
    /**/{0x2C,0x3C,0,0,0,0,0,0},
    /**/{0x2E,0x3E,0,0,0,0,0,0},
    /**/{0x2F,0x3F,0,0,0,0,0,0},
    /**/{0,0,0,0,0,0,0,0},
    /**/{0,0,0,0,0,0,0,0},
    /**/{0,0,0,0,0,0,0,0},
    /**/{0x20,0,0,0,0,0,0,0},
    /**/{0,0,0,0,0,0,0,0},
    /*F1*/{0x3000,0,0,0x3000,0,0,0,0},
    /**/{0x3001,0,0,0x3001,0,0,0,0},
    /**/{0x3002,0,0,0x3002,0,0,0,0},
    /**/{0x3003,0,0,0x3003,0,0,0,0},
    /**/{0x3004,0,0,0x3004,0,0,0,0},
    /**/{0x4000,0,0,0,0,0,0,0},
    /**/{0x4100,0,0,0,0,0,0,0},
    /**/{0x4200,0,0,0,0,0,0,0},
    /**/{0x4300,0,0,0,0,0,0,0},
    /**/{0x4400,0,0,0,0,0,0,0},
    /**/{0,0,0,0,0,0,0,0},
    /**/{0,0,0,0,0,0,0,0},
    /**/{0x4700,0,0,0,0,0,0,0},
    /**/{0x4800,0,0,0,0,0,0,0},
    /**/{0x4900,0,0,0,0,0,0,0},
    /**/{0x2D,0,0,0,0,0,0,0},
    /**/{0x4B00,0,0,0,0,0,0,0},
    /**/{0x4C00,0,0,0,0,0,0,0},
    /**/{0x4D00,0,0,0,0,0,0,0},
    /**/{0x2B,0,0,0,0,0,0,0},
    /**/{0x4F00,0,0,0,0,0,0,0},
    /**/{0x5000,0,0,0,0,0,0,0},
    /**/{0x5100,0,0,0,0,0,0,0},
    /**/{0x5200,0,0,0,0,0,0,0},
    /**/{0x5300,0,0,0,0,0,0,0},
    /**/{0,0,0,0,0,0,0,0},
    /**/{0,0,0,0,0,0,0,0}
    };

    /************************************************************************

    Function:intatkbd_init

    Description:Thisfunctionisusedtoturnonthekeyboard

    Notes:

    02/20/2004-Approvedforquality

    ************************************************************************/
    intatkbd_init(){
    /*InserttheIDTvectorforthekeyboardhandler*/
    setVector(&atkbd_isr,mVec+0x1,dPresent+dInt+dDpl0);

    /*SettheLEDStotheirdefaults*/
    setLED();

    /*ClearKeyboard*/
    atkbd_scan();

    /*Turnonthekeyboardvector*/
    irqEnable(0x1);

    /*Printoutinformationonkeyboard*/
    kprintf("atkbd0-Address:[0x%X],KeyboardBuffer:[0x%X],BufferSize[%i]\n",&atkbd_isr,&stdinBuffer,512);

    /*Returnsoweknoweverythingwentwell*/
    return(0x0);
    }

    /*
    *2-23-2004mjiIthinkthepusha/popashouldbepushal/popal
    */

    asm(
    ".globlatkbd_isr\n"
    "atkbd_isr:\n"
    "pusha\n"/*Saveallregisters*/
    "push%ss\n"
    "push%ds\n"
    "push%es\n"
    "push%fs\n"
    "push%gs\n"
    "callkeyboardHandler\n"
    "mov$0x20,%dx\n"
    "mov$0x20,%ax\n"
    "outb%al,%dx\n"
    "pop%gs\n"
    "pop%fs\n"
    "pop%es\n"
    "pop%ds\n"
    "pop%ss\n"
    "popa\n"
    "iret\n"/*Exitinterrupt*/
    );

    staticintatkbd_scan(){
    intcode=0x0;
    intval=0x0;

    code=inportByte(0x60);
    val=inportByte(0x61);

    outportByte(0x61,val|0x80);
    outportByte(0x61,val);

    return(code);
    }

    voidkeyboardHandler(){
    intkey=0x0;

    if(!spinTryLock(&atkbdSpinLock))
    return;

    key=atkbd_scan();

    if(key>255)
    return;

    /*ControlKey*/
    if(key==0x1D&&!(controlKeys&controlKey)){
    controlKeys|=controlKey;
    }
    if(key==0x80+0x1D){
    controlKeys&=(0xFF-controlKey);
    }
    /*ALTKey*/
    if(key==0x38&&!(controlKeys&altKey)){
    controlKeys|=altKey;
    }
    if(key==0x80+0x38){
    controlKeys&=(0xFF-altKey);
    }
    /*ShiftKey*/
    if((key==0x2A||key==0x36)&&!(controlKeys&shiftKey)){
    controlKeys|=shiftKey;
    }
    if((key==0x80+0x2A)||(key==0x80+0x36)){
    controlKeys&=(0xFF-shiftKey);
    }
    /*CapsLock*/
    if(key==0x3A){
    ledStatus^=ledCapslock;
    setLED();
    }
    /*NumLock*/
    if(key==0x45){
    ledStatus^=ledNumlock;
    setLED();
    }
    /*ScrollLock*/
    if(key==0x46){
    ledStatus^=ledScrolllock;
    setLED();
    }
    /*PickWhichKeyMap*/
    if(controlKeys==0){keyMap=0;}
    elseif(controlKeys==1){keyMap=1;}
    elseif(controlKeys==2){keyMap=2;}
    elseif(controlKeys==4){keyMap=3;}
    /*IfKeyIsNotNullAddItToHandler*/
    if(((uInt)(keyboardMap[key][keyMap])>0)&&((uInt32)(keyboardMap[key][keyMap])<0xFF)){
    switch((uInt32)keyboardMap[key][keyMap]){
    case8:
    backSpace();
    if(tty_foreground==0x0){
    stdinBuffer[stdinSize]=keyboardMap[key][keyMap];
    stdinSize++;
    }
    else{
    tty_foreground->stdin[tty_foreground->stdinSize]=keyboardMap[key][keyMap];
    tty_foreground->stdinSize++;
    }
    break;
    case0x3:
    //if(tty_foreground!=0x0)
    //endTask(tty_foreground->owner);
    K_PANIC("CTRL-Cpressed\n");
    kprintf("FreePages:[0x%X]\n",systemVitals->freePages);
    break;
    case0x9:
    kprintf("REBOOTING");
    while(inportByte(0x64)&0x02);
    outportByte(0x64,0xFE);
    break;
    default:
    if(tty_foreground==0x0){
    stdinBuffer[stdinSize]=keyboardMap[key][keyMap];
    stdinSize++;
    }
    else{
    tty_foreground->stdin[tty_foreground->stdinSize]=keyboardMap[key][keyMap];
    tty_foreground->stdinSize++;
    }
    break;
    }
    }
    else{
    switch((keyboardMap[key][keyMap]>>8)){
    case0x30:
    tty_change(keyboardMap[key][keyMap]&0xFF);
    //kprintf("ChangingConsoles[0x%X:0x%X]\n",_current->id,_current);
    break;
    default:
    break;
    }
    }

    /*Return*/
    spinUnlock(&atkbdSpinLock);
    return;
    }

    voidsetLED(){
    outportByte(0x60,0xED);
    while(inportByte(0x64)&2);
    outportByte(0x60,ledStatus);
    while(inportByte(0x64)&2);
    }

    /*Temp*/
    unsignedchargetch(){
    uInt8retKey=0x0;
    uInt32i=0x0;

    /*
    if((stdinSize<=0)&&(tty_foreground==0x0)){
    sched_yield();
    }
    if((tty_foreground!=0x0)&&(tty_foreground->stdinSize<=0x0)){
    sched_yield();
    }
    */

    /*
    if(!spinTryLock(&atkbdSpinLock))
    return(0x0);
    */

    if(tty_foreground==0x0){
    if(stdinSize==0x0){
    //spinUnlock(&atkbdSpinLock);
    return(0x0);
    }

    retKey=stdinBuffer[0];
    stdinSize--;

    for(i=0x0;i<stdinSize;i++){
    stdinBuffer[i]=stdinBuffer[i+0x1];
    }
    }
    else{
    if(tty_foreground->stdinSize==0x0){
    //spinUnlock(&atkbdSpinLock);
    return(0x0);
    }

    retKey=tty_foreground->stdin[0];
    tty_foreground->stdinSize--;

    for(i=0x0;i<tty_foreground->stdinSize;i++){
    tty_foreground->stdin[i]=tty_foreground->stdin[i+0x1];
    }
    }
    //spinUnlock(&atkbdSpinLock);
    return(retKey);
    }

    /***

    $Log: atkbd_8c.xml,v $
    Revision 1.6  2006/12/19 14:12:50  reddawg
    rtld-elf almost workign

    Revision1.52006/12/0517:01:15reddawg
    Modifiedkpanic

    Revision1.42006/12/0514:10:21reddawg
    WorkignDistro

    Revision1.32006/12/0105:12:35reddawg
    We'realmostthere...:)

    Revision1.22006/10/1917:52:17reddawg
    WorkingOnUserland

    Revision1.1.1.12006/06/0112:46:12reddawg
    ubix2

    Revision1.22005/10/1200:13:37reddawg
    Removed

    Revision1.1.1.12005/09/2617:24:01reddawg
    nomessage

    Revision1.292004/09/1121:38:00reddawg
    Fixedafewproblems

    Revision1.282004/09/0823:19:58reddawg
    hmm

    Revision1.272004/09/0721:54:38reddawg
    okrevertedbacktooldschedulingfornow....

    Revision1.262004/09/0622:18:52reddawg
    okbedtime

    Revision1.252004/09/0622:11:29reddawg
    tty:noweachttyhasastdin....

    Revision1.242004/09/0615:13:25reddawg
    LastcommitbeforeFreeBSD6.0

    Revision1.232004/08/2123:47:50reddawg
    ***emptylogmessage***

    Revision1.222004/08/0912:58:05reddawg
    letmeknowwhenyougotthesurce

    Revision1.212004/08/0622:32:16reddawg
    UbixWorksAgain

    Revision1.192004/08/0318:31:19reddawg
    virtualterms

    Revision1.182004/07/2921:32:16reddawg
    Myquicklunchsbreaksworthofupdates....

    Revision1.172004/07/2818:45:39reddawg
    movementoffiles

    Revision1.162004/07/2817:07:25reddawg
    MPI:movedthesyscalls

    Revision1.152004/07/2619:15:49reddawg
    testcode,fixesandthelike

    Revision1.142004/07/2505:32:58reddawg
    fixed

    Revision1.132004/07/2505:24:39reddawg
    atkbd:removedsti...doesitstillmisskeys

    Revision1.122004/07/2420:00:51reddawg
    Lotsofchangestothevmmsubsystem....Pagefaultshavebeenadjusttonowbeblockingonaperthreadbasisnotsystemwide.Thishasresultedinnomoredeadlocks..alsotheadditionofperthreadlockinghasremovedsegfaultsasaresultofCOWinwhichtwotasksfaultthesameCOWpageandtrytomodifyit.

    Revision1.112004/07/2415:12:56reddawg
    NowI'mcurrent

    Revision1.102004/07/2317:49:58reddawg
    atkbd:adjustthetimingissueonthedriverhopefullyitwillworkfinenow

    Revision1.92004/07/2317:37:35reddawg
    Fix

    Revision1.82004/07/2309:10:06reddawg
    ubixfs:cleanedupsomefunctionsplayedwiththecachingabit
    vfs:renamedabunchoffunctions
    cleanedupafewmiscbugs

    Revision1.72004/07/2220:53:07reddawg
    atkbd:fixedproblem

    Revision1.62004/07/0913:34:51reddawg
    keyboard:keyboardInittoatkbd_init
    Adjustedinitializationroutines

    Revision1.52004/06/1714:49:14reddawg
    atkbd:convertedsomevariablestostatic

    Revision1.42004/06/0410:19:42reddawg
    notes:wecompileagain,thankg-danywaysiwasabouttocry

    Revision1.32004/05/1904:07:42reddawg
    kmalloc(size,pid)nomoreitisnokmalloc(size);thewayitshouldofbeen

    Revision1.22004/05/1002:23:24reddawg
    MinorChangesToSourceCodeToPrepareItForOpenSourceRelease

    Revision1.1.1.12004/04/1512:07:09reddawg
    UbixOSv1.0

    Revision1.192004/04/1316:36:33reddawg
    Changedourcopyright,itisallnowunderaBSD-Stylelicense

    END
    ***/