UbixOS V2 2.0

device.c
Go to the documentation of this file.
1 /*-
2 * Copyright (c) 2002-2018 The UbixOS Project.
3 * All rights reserved.
4 *
5 * This was developed by Christopher W. Olsen for the UbixOS Project.
6 *
7 * Redistribution and use in source and binary forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1) Redistributions of source code must retain the above copyright notice, this list of
11 * conditions, the following disclaimer and the list of authors.
12 * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13 * conditions, the following disclaimer and the list of authors in the documentation and/or
14 * other materials provided with the distribution.
15 * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16 * endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/device.h>
30 #include <ubixos/spinlock.h>
31 #include <lib/kmalloc.h>
32 #include <lib/kprintf.h>
33 #include <assert.h>
34
35 /* Linked list of drivers loaded in the system accessable by the subsystem only */
36 static struct device_node *devices = 0x0;
37 static struct spinLock deviceSpinLock = SPIN_LOCK_INITIALIZER;
38
39 /*****************************************************************************************
40
41 Function: int deviceAdd(int minor,char type,struct device_interface *devInfo);
42
43 Description: This will add a device to the system
44
45 Notes:
46
47 05/19/2004 - Improving Upon the spec
48
49 *****************************************************************************************/
50 int device_add(int minor, char type, struct device_interface *devInfo) {
51 struct device_node *tmpDev = 0x0;
52
53 tmpDev = (struct device_node *) kmalloc(sizeof(struct device_node));
54 if (tmpDev == NULL)
55 kprintf("Error Adding Device: memory failure\n");
56
57 tmpDev->prev = 0x0;
58 tmpDev->minor = minor;
59 tmpDev->type = type;
60 tmpDev->devInfo = devInfo;
61
62 spinLock(&deviceSpinLock);
63 tmpDev->next = devices;
64 devices = tmpDev;
65 spinUnlock(&deviceSpinLock);
66
67 if (tmpDev->devInfo->initialized == 0x0)
68 return (tmpDev->devInfo->init(tmpDev));
69 else
70 return (0x0);
71 }
72
73 /*****************************************************************************************
74
75 Function: struct device_node *deviceFind(int major,int minor);
76
77 Description: This will find a device based on major minor
78
79 Notes:
80
81 05/19/2004 - Improving Upon the spec
82
83 *****************************************************************************************/
84 struct device_node *device_find(int major, int minor) {
85 struct device_node *tmpDev = 0x0;
86
87 spinLock(&deviceSpinLock);
88
89 for (tmpDev = devices; tmpDev; tmpDev = tmpDev->next) {
90 if ((tmpDev->devInfo->major == major) && (tmpDev->minor == minor)) {
91 spinUnlock(&deviceSpinLock);
92 return (tmpDev);
93 }
94 }
95
96 spinUnlock(&deviceSpinLock);
97 return (0x0);
98 }
99
100 /********************************************************************************************
101
102 Function: int deviceRemove(struct *device_node);
103
104 Description: This will remove a device based on it's pointer
105
106 *********************************************************************************************/
107 int device_remove(struct device_node *deviceToDelete) {
108 struct device_node *current, *previous;
109
110 current = devices;
111 previous = NULL;
112 spinLock(&deviceSpinLock);
113 while (current != NULL) {
114 if (current == deviceToDelete)
115 break;
116 else {
117 previous = current;
118 current = current->next;
119 }
120 }
121 if (current == NULL) {
122 spinUnlock(&deviceSpinLock);
123 return 1;
124 }
125 else {
126 if (current == devices)
127 devices = devices->next;
128 else
129 previous->next = current->next;
130 kfree(current);
131 spinUnlock(&deviceSpinLock);
132 return 1;
133 }
134
135 spinUnlock(&deviceSpinLock);
136 return 0x0;
137 }
device_node::type
char type
Definition: device.h:39
device_remove
int device_remove(struct device_node *deviceToDelete)
Definition: device.c:104
device_interface::initialized
uInt8 initialized
Definition: device.h:48
spinlock.h
device_interface
Definition: device.h:47
device_node::next
struct device_node * next
Definition: device.h:36
device_add
int device_add(int minor, char type, struct device_interface *devInfo)
Definition: device.c:49
kfree
void kfree(void *baseAddr)
Definition: kmalloc.c:342
assert.h
spinUnlock
void spinUnlock(spinLock_t *lock)
Definition: spinlock.c:36
device_node::prev
struct device_node * prev
Definition: device.h:35
SPIN_LOCK_INITIALIZER
#define SPIN_LOCK_INITIALIZER
Definition: spinlock.h:36
spinLock
void spinLock(spinLock_t *lock)
Definition: spinlock.c:55
device_node::minor
int minor
Definition: device.h:40
kprintf.h
device_node
Definition: device.h:34
device_interface::init
int(* init)(void *)
Definition: device.h:55
device_find
struct device_node * device_find(int major, int minor)
Definition: device.c:82
device.h
kmalloc
void * kmalloc(uInt32 len)
Definition: kmalloc.c:241
spinLock
Definition: spinlock.h:41
device_node::devInfo
struct device_interface * devInfo
Definition: device.h:37
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
device_interface::major
int major
Definition: device.h:50
kmalloc.h
NULL
#define NULL
Definition: fat_string.h:17