00001 /***************************************************************************************** 00002 Copyright (c) 2002-2005 The UbixOS Project 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are 00006 permitted provided that the following conditions are met: 00007 00008 Redistributions of source code must retain the above copyright notice, this list of 00009 conditions, the following disclaimer and the list of authors. Redistributions in binary 00010 form must reproduce the above copyright notice, this list of conditions, the following 00011 disclaimer and the list of authors in the documentation and/or other materials provided 00012 with the distribution. Neither the name of the UbixOS Project nor the names of its 00013 contributors may be used to endorse or promote products derived from this software 00014 without specific prior written permission. 00015 00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 00017 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00018 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 00019 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00020 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00021 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00022 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 00023 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00024 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00025 00026 $Id: device_8c-source.html 88 2016-01-12 00:11:29Z reddawg $ 00027 00028 *****************************************************************************************/ 00029 00030 #include <sys/device.h> 00031 #include <ubixos/spinlock.h> 00032 #include <lib/kmalloc.h> 00033 #include <lib/kprintf.h> 00034 #include <assert.h> 00035 00036 /* Linked list of drivers loaded in the system accessable by the subsystem only */ 00037 static struct device_node *devices = 0x0; 00038 static spinLock_t deviceSpinLock = SPIN_LOCK_INITIALIZER; 00039 00040 /***************************************************************************************** 00041 00042 Function: int deviceAdd(int minor,char type,struct device_interface *devInfo); 00043 00044 Description: This will add a device to the system 00045 00046 Notes: 00047 00048 05/19/2004 - Improving Upon the spec 00049 00050 *****************************************************************************************/ 00051 int device_add(int minor,char type,struct device_interface *devInfo) { 00052 struct device_node *tmpDev = 0x0; 00053 00054 00055 tmpDev = (struct device_node *)kmalloc(sizeof(struct device_node)); 00056 if(tmpDev == NULL) 00057 kprintf("Error Adding Device: memory failure\n"); 00058 00059 tmpDev->prev = 0x0; 00060 tmpDev->minor = minor; 00061 tmpDev->type = type; 00062 tmpDev->devInfo = devInfo; 00063 00064 spinLock(&deviceSpinLock); 00065 tmpDev->next = devices; 00066 devices = tmpDev; 00067 spinUnlock(&deviceSpinLock); 00068 00069 if (tmpDev->devInfo->initialized == 0x0) 00070 return(tmpDev->devInfo->init(tmpDev)); 00071 else 00072 return(0x0); 00073 } 00074 00075 /***************************************************************************************** 00076 00077 Function: struct device_node *deviceFind(int major,int minor); 00078 00079 Description: This will find a device based on major minor 00080 00081 Notes: 00082 00083 05/19/2004 - Improving Upon the spec 00084 00085 *****************************************************************************************/ 00086 struct device_node *device_find(int major,int minor) { 00087 struct device_node *tmpDev = 0x0; 00088 00089 spinLock(&deviceSpinLock); 00090 00091 for (tmpDev = devices;tmpDev;tmpDev=tmpDev->next) { 00092 if ((tmpDev->devInfo->major == major) && (tmpDev->minor == minor)) { 00093 spinUnlock(&deviceSpinLock); 00094 return(tmpDev); 00095 } 00096 } 00097 00098 spinUnlock(&deviceSpinLock); 00099 return(0x0); 00100 } 00101 00102 00103 /******************************************************************************************** 00104 00105 Function: int deviceRemove(struct *device_node); 00106 00107 Description: This will remove a device based on it's pointer 00108 00109 *********************************************************************************************/ 00110 int device_remove(struct device_node *deviceToDelete) 00111 { 00112 struct device_node *current, *previous; 00113 00114 00115 current = devices; 00116 previous=NULL; 00117 spinLock(&deviceSpinLock); 00118 while(current != NULL) 00119 { 00120 if(current==deviceToDelete) break; 00121 else 00122 { 00123 previous = current; 00124 current = current->next; 00125 } 00126 } 00127 if(current == NULL) 00128 { 00129 spinUnlock(&deviceSpinLock); 00130 return 1; 00131 } 00132 else 00133 { 00134 if(current == devices) 00135 devices = devices->next; 00136 else 00137 previous->next = current->next; 00138 kfree(current); 00139 spinUnlock(&deviceSpinLock); 00140 return 1; 00141 } 00142 00143 spinUnlock(&deviceSpinLock); 00144 return 0x0; 00145 } 00146 00147 00148 /*** 00149 END 00150 ***/