/*
    IODevice.h, Copyright  (c) by George Fankhauser, Claudio Jeker
    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/IODevice.h,v $
 	Author(s):             George Fankhauser
 	Affiliation:           ETH Zuerich, TIK
 	Version:               $Revision: 2922 $
 	Creation Date:         
 	Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $      by: $Author: baderma $
*/
#ifndef _IODEVICE_H_
#define _IODEVICE_H_

#include "Topsy.h"
#include "Threads.h"

/* driver messages */
#define IO_DRIVERCLOSE -1

/* return value from read/write functions to prevent a msg being sent */
#define IO_LATER -1

/* major minor macros */
#define SETMAJOR(x, val)	((x) = (((x) & 0xFFFF)     | (val) << 16))
#define SETMINOR(x, val)	((x) = (((x) & 0xFFFF0000) | (val) ))
#define GETMAJOR(x)			( ((x) >> 16) & 0xFFFF )
#define GETMINOR(x)			(  (x)        & 0xFFFF )

/* Forward declaration of IODevice
 */
typedef struct IODeviceDesc_t * IODevice;

typedef Error(*ReadFunction)(IODevice this, ThreadId, char*, long int*);
typedef Error(*WriteFunction)(IODevice this, ThreadId, char*, long int*);
typedef Error(*CloseFunction)(IODevice this);
typedef Error(*InitFunction)(IODevice this);
typedef void(*MessageHandler)(IODevice this, Message*);

typedef struct IODeviceDesc_t
{
  IODevice next; /* doubly linked list */
  IODevice prev;
  char* name;
  Address base;
  char* buffer;
  InterruptId interrupt;
  InterruptHandler interruptHandler;
  ThreadId threadId;
  ReadFunction read;
  WriteFunction write;
  CloseFunction close;
  InitFunction init;
  MessageHandler handleMsg;
  Boolean isInitialised;
  void* extension;
  short major; /* will be filled by ioLoadDriver */
  short minor; /* will be filled by ioLoadDriver */
  long  count; /* will be filled by ioLoadDriver */
}
IODeviceDesc;

void ioDeviceMain(IODevice this);
void ioRemoveDriver(IODevice this);

int ioDelayAtLeastCycles( int nbLoops);

#endif
