/*
  TTY.c, Copyright  (c) by Lukas Ruf, lr@lpr.ch,
  Swiss Federal Institute of Technology,
  Computer Engineering and Networks Laboratory.
 
  TOPSY -- A Teachable Operating System.
           Implementation of a tiny and simple
           micro kernel for teaching purposes.
 
  For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
  This software is provided under the terms of the GNU General Public Licence.
 
  A full copy of the GNU GPL is provided in the file COPYING found in the 
  development root of Topsy.
*/
/*
	File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/IO/ia32/Drivers/TTY.c,v $
 	Author(s):             Lukas Ruf, lr@lpr.ch
 	Affiliation:           ETH Zuerich, TIK
 	Version:               $Revision: 3039 $
 	Creation Date:         
 	Last Date of Change:   $Date: 2007-06-12 21:43:31 +0200 (Die, 12 Jun 2007) $      by: $Author: sryffel $
*/

#include "Messages.h"
#include "IODevice.h"
#include "Keyboard.h"
#include "Video.h"
#include "TTY.h"
 
IODeviceDesc ia32ConsDesc = {
                              NULL, NULL,
                              "ttya",
                              (Address)0, /* Screen IO: No Direct Memory Access allowed */
                              NULL,       /* buffer must be allocated by driver */
                              T_IR_01,    /* Keyboard Handler */
                              (InterruptHandler)TTY_Interrupt,
                              0, /* thread id filled in after start */
                              TTY_read,
                              TTY_write,
                              TTY_close,
                              TTY_init,
                              NULL        /* private msg handler */,
                              FALSE, NULL /* data extension */
                            };

void TTY_Interrupt(IODevice this)
{
  _KeyboardISR();
}

Error TTY_init(IODevice this)
{
  if (!this->isInitialised)
    KeyboardInit();
  this->isInitialised = TRUE;
  return IO_INITOK;
}


Error TTY_read(IODevice this, ThreadId threadId, char* buffer,
               long int* size)
{
  int i = 0;
  char pc;
  while ((i < *size) && _GetChar(&pc))
    if (pc)
      buffer[i++] = pc; /* ommit reading zero bytes :-) */
  *size = i;
  return IO_READOK;
}


Error TTY_write(IODevice this, ThreadId threadId, char* buffer, long int* size)
{
  int i = 0;
  while (i < *size)
    kprintc(buffer[i++]);
  *size = i;
  return IO_WRITEOK;
}


Error TTY_close(IODevice this)
{
  /* this message is forwarded from ioThread */

  /* release buffers if any */

  return IO_CLOSEOK;
}

