/*****************************************************************************************
 Copyright (c) 2002-2004 The UbixOS Project
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this list of
 conditions, the following disclaimer and the list of authors.  Redistributions in binary
 form must reproduce the above copyright notice, this list of conditions, the following
 disclaimer and the list of authors in the documentation and/or other materials provided
 with the distribution. Neither the name of the UbixOS Project nor the names of its
 contributors may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id$

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

#include <sys/device.h>
#include <ubixos/spinlock.h>
#include <lib/kmalloc.h>

/* Linked list of drivers loaded in the system accessable by the subsystem only */
static struct deviceNode *devices = 0x0;
static spinLock_t deviceSpinLock = SPIN_LOCK_INITIALIZER;

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

 Function: int deviceAdd(int minor,char type,struct deviceInterface *devInfo);

 Description: This will add a device to the system

 Notes: 

 05/19/2004 - Improving Upon the spec

*****************************************************************************************/
int deviceAdd(int minor,char type,struct deviceInterface *devInfo) {
  struct deviceNode *tmpDev = 0x0;
  
  spinLock(&deviceSpinLock);
  
  tmpDev = (struct deviceNode *)kmalloc(sizeof(struct deviceNode));

  tmpDev->prev    = 0x0;
  tmpDev->minor   = minor;
  tmpDev->type    = type;
  tmpDev->devInfo = devInfo;
  tmpDev->next    = devices;
  devices         = tmpDev;

  spinUnlock(&deviceSpinLock);

  return(tmpDev->devInfo->init(tmpDev));
  }

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

 Function: struct deviceNode *deviceFind(int major,int minor);

 Description: This will find a device based on major minor

 Notes: 

 05/19/2004 - Improving Upon the spec

*****************************************************************************************/
struct deviceNode *deviceFind(int major,int minor) {
  struct deviceNode *tmpDev = 0x0;
 
  spinLock(&deviceSpinLock);

  for (tmpDev = devices;tmpDev;tmpDev=tmpDev->next) {
    if ((tmpDev->devInfo->major == major) && (tmpDev->minor == minor)) {
      spinUnlock(&deviceSpinLock);
      return(tmpDev);
      }
    }

  spinUnlock(&deviceSpinLock);
  return(0x0);
  }  


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

Function: int deviceRemove(struct *deviceNode);

Description: This will remove a device based on it's pointer

*********************************************************************************************/
int deviceRemove(struct deviceNode *deviceToDelete)
{
struct deviceNode *current, *previous;

spinLock(&deviceSpinLock);

current = devices;
previous=NULL;
               while(current != NULL)
               {
                  if(current==deviceToDelete) break;
                  else
                  {
                     previous = current;
                     current = current->next;
                  }
               }
               if(current == NULL) return 1;
               else
               {
                  if(current == devices) devices = devices->next;
                  else previous->next = current->next;
                  kfree(current);
                  spinUnlock(&deviceSpinLock);
                  return 1;
               }

  spinUnlock(&deviceSpinLock);
return 0x0;
}


/***

 $Log$
 Revision 1.9  2004/05/22 02:40:04  ionix


 fixed typo in device.h and initialized previous in device.c :)

 Revision 1.8  2004/05/22 02:34:03  ionix


 Added proto

 Revision 1.7  2004/05/22 02:28:34  ionix

 Added deviceRemove

 Revision 1.6  2004/05/19 15:31:28  reddawg
 Fixed up the rest of the references

 Revision 1.5  2004/05/19 15:06:48  reddawg
 Fixing Device Driver System Changed Interface

 Revision 1.4  2004/05/19 14:57:25  reddawg
 Changed The Device Interface

 Revision 1.3  2004/05/19 04:07:43  reddawg
 kmalloc(size,pid) no more it is no kmalloc(size); the way it should of been

 Revision 1.2  2004/04/27 12:05:12  reddawg
 Updates To Driver Subsystem

 Revision 1.1  2004/04/27 03:49:41  reddawg
 Start of new driver system

 END
 ***/

