1 | /* GPIOLIB interface implementation |
---|
2 | * |
---|
3 | * COPYRIGHT (c) 2009. |
---|
4 | * Cobham Gaisler AB. |
---|
5 | * |
---|
6 | * The license and distribution terms for this file may be |
---|
7 | * found in the file LICENSE in this distribution or at |
---|
8 | * http://www.rtems.com/license/LICENSE. |
---|
9 | */ |
---|
10 | |
---|
11 | #include <stdlib.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <string.h> |
---|
14 | |
---|
15 | #include <gpiolib.h> |
---|
16 | |
---|
17 | struct gpiolib_port; |
---|
18 | |
---|
19 | struct gpiolib_port { |
---|
20 | struct gpiolib_port *next; |
---|
21 | int minor; |
---|
22 | struct gpiolib_drv *drv; |
---|
23 | void *handle; |
---|
24 | |
---|
25 | int open; |
---|
26 | }; |
---|
27 | |
---|
28 | /* Root of GPIO Ports */ |
---|
29 | struct gpiolib_port *gpiolib_ports; |
---|
30 | |
---|
31 | /* Number of GPIO ports registered */ |
---|
32 | static int port_nr; |
---|
33 | |
---|
34 | /* 1 if libraray initialized */ |
---|
35 | static int gpiolib_initied = 0; |
---|
36 | |
---|
37 | /* Insert a port first in ports list */ |
---|
38 | void gpiolib_list_add(struct gpiolib_port *port) |
---|
39 | { |
---|
40 | port->next = gpiolib_ports; |
---|
41 | gpiolib_ports = port; |
---|
42 | } |
---|
43 | |
---|
44 | struct gpiolib_port *gpiolib_find(int minor) |
---|
45 | { |
---|
46 | struct gpiolib_port *p; |
---|
47 | |
---|
48 | p = gpiolib_ports; |
---|
49 | while ( p && (p->minor != minor) ) { |
---|
50 | p = p->next; |
---|
51 | } |
---|
52 | return p; |
---|
53 | } |
---|
54 | |
---|
55 | struct gpiolib_port *gpiolib_find_by_name(char *name) |
---|
56 | { |
---|
57 | struct gpiolib_port *p; |
---|
58 | struct gpiolib_info info; |
---|
59 | int (*get_info)(void *, struct gpiolib_info *); |
---|
60 | |
---|
61 | p = gpiolib_ports; |
---|
62 | while ( p ) { |
---|
63 | get_info = p->drv->ops->get_info; |
---|
64 | if ( get_info && (get_info(p->handle, &info) == 0) ) { |
---|
65 | if ( strncmp(name, (char *)&info.devName[0], 64) == 0 ) { |
---|
66 | break; |
---|
67 | } |
---|
68 | } |
---|
69 | p = p->next; |
---|
70 | } |
---|
71 | return p; |
---|
72 | } |
---|
73 | |
---|
74 | void gpiolib_list_remove(struct gpiolib_port *port) |
---|
75 | { |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | int gpiolib_drv_register(struct gpiolib_drv *drv, void *handle) |
---|
80 | { |
---|
81 | struct gpiolib_port *port; |
---|
82 | |
---|
83 | if ( !drv || !drv->ops ) |
---|
84 | return -1; |
---|
85 | |
---|
86 | port = malloc(sizeof(*port)); |
---|
87 | if ( port == NULL ) |
---|
88 | return -1; |
---|
89 | |
---|
90 | memset(port, 0, sizeof(*port)); |
---|
91 | port->handle = handle; |
---|
92 | port->minor = port_nr++; |
---|
93 | port->drv = drv; |
---|
94 | |
---|
95 | gpiolib_list_add(port); |
---|
96 | |
---|
97 | return 0; |
---|
98 | } |
---|
99 | |
---|
100 | void gpiolib_show(int port, void *handle) |
---|
101 | { |
---|
102 | struct gpiolib_port *p; |
---|
103 | |
---|
104 | if ( port == -1 ) { |
---|
105 | p = gpiolib_ports; |
---|
106 | while (p != NULL) { |
---|
107 | if ( p->drv->ops->show ) |
---|
108 | p->drv->ops->show(p->handle); |
---|
109 | p = p->next; |
---|
110 | } |
---|
111 | } else { |
---|
112 | if ( handle ) { |
---|
113 | p = handle; |
---|
114 | } else { |
---|
115 | p = gpiolib_find(port); |
---|
116 | } |
---|
117 | if ( p == NULL ) { |
---|
118 | printf("PORT %d NOT FOUND\n", port); |
---|
119 | return; |
---|
120 | } |
---|
121 | if ( p->drv->ops->show ) |
---|
122 | p->drv->ops->show(p->handle); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | void *gpiolib_open_internal(int port, char *devName) |
---|
127 | { |
---|
128 | struct gpiolib_port *p; |
---|
129 | |
---|
130 | if ( gpiolib_initied == 0 ) |
---|
131 | return NULL; |
---|
132 | |
---|
133 | /* Find */ |
---|
134 | if ( port >= 0 ) { |
---|
135 | p = gpiolib_find(port); |
---|
136 | } else { |
---|
137 | p = gpiolib_find_by_name(devName); |
---|
138 | } |
---|
139 | if ( p == NULL ) |
---|
140 | return NULL; |
---|
141 | |
---|
142 | if ( p->open ) |
---|
143 | return NULL; |
---|
144 | |
---|
145 | p->open = 1; |
---|
146 | return p; |
---|
147 | } |
---|
148 | |
---|
149 | void *gpiolib_open(int port) |
---|
150 | { |
---|
151 | return gpiolib_open_internal(port, NULL); |
---|
152 | } |
---|
153 | |
---|
154 | void *gpiolib_open_by_name(char *devName) |
---|
155 | { |
---|
156 | return gpiolib_open_internal(-1, devName); |
---|
157 | } |
---|
158 | |
---|
159 | void gpiolib_close(void *handle) |
---|
160 | { |
---|
161 | struct gpiolib_port *p = handle; |
---|
162 | |
---|
163 | if ( p && p->open ) { |
---|
164 | p->open = 0; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | int gpiolib_set_config(void *handle, struct gpiolib_config *cfg) |
---|
169 | { |
---|
170 | struct gpiolib_port *port = handle; |
---|
171 | |
---|
172 | if ( !port || !cfg ) |
---|
173 | return -1; |
---|
174 | |
---|
175 | if ( !port->drv->ops->config ) |
---|
176 | return -1; |
---|
177 | |
---|
178 | return port->drv->ops->config(port->handle, cfg); |
---|
179 | } |
---|
180 | |
---|
181 | int gpiolib_set(void *handle, int dir, int outval) |
---|
182 | { |
---|
183 | struct gpiolib_port *port = handle; |
---|
184 | |
---|
185 | if ( !port ) |
---|
186 | return -1; |
---|
187 | |
---|
188 | if ( !port->drv->ops->set ) |
---|
189 | return -1; |
---|
190 | |
---|
191 | return port->drv->ops->set(port->handle, dir, outval); |
---|
192 | } |
---|
193 | |
---|
194 | int gpiolib_get(void *handle, int *inval) |
---|
195 | { |
---|
196 | struct gpiolib_port *port = handle; |
---|
197 | |
---|
198 | if ( !port || !inval) |
---|
199 | return -1; |
---|
200 | |
---|
201 | if ( !port->drv->ops->get ) |
---|
202 | return -1; |
---|
203 | |
---|
204 | return port->drv->ops->get(port->handle, inval); |
---|
205 | } |
---|
206 | |
---|
207 | /*** IRQ Functions ***/ |
---|
208 | int gpiolib_irq_register(void *handle, void *func, void *arg) |
---|
209 | { |
---|
210 | struct gpiolib_port *port = handle; |
---|
211 | |
---|
212 | if ( !port ) |
---|
213 | return -1; |
---|
214 | |
---|
215 | if ( !port->drv->ops->irq_register ) |
---|
216 | return -1; |
---|
217 | |
---|
218 | return port->drv->ops->irq_register(port->handle, func, arg); |
---|
219 | } |
---|
220 | |
---|
221 | int gpiolib_irq_opts(void *handle, unsigned int options) |
---|
222 | { |
---|
223 | struct gpiolib_port *port = handle; |
---|
224 | |
---|
225 | if ( !port ) |
---|
226 | return -1; |
---|
227 | |
---|
228 | if ( !port->drv->ops->irq_opts ) |
---|
229 | return -1; |
---|
230 | |
---|
231 | return port->drv->ops->irq_opts(port->handle, options); |
---|
232 | } |
---|
233 | |
---|
234 | int gpiolib_irq_clear(void *handle) |
---|
235 | { |
---|
236 | return gpiolib_irq_opts(handle, GPIOLIB_IRQ_CLEAR); |
---|
237 | } |
---|
238 | |
---|
239 | int gpiolib_irq_force(void *handle) |
---|
240 | { |
---|
241 | return gpiolib_irq_opts(handle, GPIOLIB_IRQ_FORCE); |
---|
242 | } |
---|
243 | |
---|
244 | int gpiolib_irq_enable(void *handle) |
---|
245 | { |
---|
246 | return gpiolib_irq_opts(handle, GPIOLIB_IRQ_ENABLE); |
---|
247 | } |
---|
248 | |
---|
249 | int gpiolib_irq_disable(void *handle) |
---|
250 | { |
---|
251 | return gpiolib_irq_opts(handle, GPIOLIB_IRQ_DISABLE); |
---|
252 | } |
---|
253 | |
---|
254 | int gpiolib_irq_mask(void *handle) |
---|
255 | { |
---|
256 | return gpiolib_irq_opts(handle, GPIOLIB_IRQ_MASK); |
---|
257 | } |
---|
258 | |
---|
259 | int gpiolib_irq_unmask(void *handle) |
---|
260 | { |
---|
261 | return gpiolib_irq_opts(handle, GPIOLIB_IRQ_UNMASK); |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | /*** Initialization ***/ |
---|
266 | int gpiolib_initialize(void) |
---|
267 | { |
---|
268 | if ( gpiolib_initied != 0 ) |
---|
269 | return 0; |
---|
270 | |
---|
271 | /* Initialize Libarary */ |
---|
272 | port_nr = 0; |
---|
273 | gpiolib_ports = 0; |
---|
274 | gpiolib_initied = 1; |
---|
275 | return 0; |
---|
276 | } |
---|