/***************************************************************************************** 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 <sys/spinlock.h> #include <lib/kmalloc.h> #include <assert.h> /* Linked list of drivers loaded in the system accessable by the subsystem only */ static struct device_node *device_list = 0x0; static spinLock_t deviceSpinLock = SPIN_LOCK_INITIALIZER; /***************************************************************************************** Function: int device_init() Description: This will initialize device subsystem Notes: *****************************************************************************************/ int device_init() { device_list = 0x0; deviceSpinLock = SPIN_LOCK_INITIALIZER; return(0x0); } /***************************************************************************************** Function: int deviceAdd(int minor,char type,struct device_interface *devInfo); Description: This will add a device to the system Notes: 05/19/2004 - Improving Upon the spec *****************************************************************************************/ int device_add(int minor,char type,struct device_interface *devInfo) { struct device_node *tmpDev = 0x0; spinLock(&deviceSpinLock); tmpDev = (struct device_node *)kmalloc(sizeof(struct device_node)); assert(tmpDev); tmpDev->prev = 0x0; tmpDev->minor = minor; tmpDev->type = type; tmpDev->devInfo = devInfo; tmpDev->next = device_list; device_list = tmpDev; spinUnlock(&deviceSpinLock); if (tmpDev->devInfo->initialized == 0x0) return(tmpDev->devInfo->init(tmpDev)); else return(0x0); } /***************************************************************************************** Function: struct device_node *deviceFind(int major,int minor); Description: This will find a device based on major minor Notes: 05/19/2004 - Improving Upon the spec *****************************************************************************************/ struct device_node *device_find(int major,int minor) { struct device_node *tmpDev = 0x0; spinLock(&deviceSpinLock); for (tmpDev = device_list;tmpDev;tmpDev=tmpDev->next) { if ((tmpDev->devInfo->major == major) && (tmpDev->minor == minor)) { spinUnlock(&deviceSpinLock); return(tmpDev); } } spinUnlock(&deviceSpinLock); return(0x0); } /******************************************************************************************** Function: int deviceRemove(struct *device_node); Description: This will remove a device based on it's pointer *********************************************************************************************/ int device_remove(struct device_node *deviceToDelete) { struct device_node *current, *previous; spinLock(&deviceSpinLock); current = device_list; previous=NULL; while(current != NULL) { if(current==deviceToDelete) break; else { previous = current; current = current->next; } } if(current == NULL) return 1; else { if(current == device_list) device_list = device_list->next; else previous->next = current->next; kfree(current); spinUnlock(&deviceSpinLock); return 1; } spinUnlock(&deviceSpinLock); return 0x0; } /*** END ***/