1 | /*
|
---|
2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @defgroup libdrv generic device driver support.
|
---|
32 | * @brief HelenOS generic device driver support.
|
---|
33 | * @{
|
---|
34 | */
|
---|
35 |
|
---|
36 | /** @file
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <assert.h>
|
---|
40 | #include <ipc/services.h>
|
---|
41 | #include <ipc/ns.h>
|
---|
42 | #include <async.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <bool.h>
|
---|
46 | #include <fibril_synch.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <str.h>
|
---|
49 | #include <str_error.h>
|
---|
50 | #include <ctype.h>
|
---|
51 | #include <errno.h>
|
---|
52 | #include <inttypes.h>
|
---|
53 | #include <devman.h>
|
---|
54 |
|
---|
55 | #include <ipc/driver.h>
|
---|
56 |
|
---|
57 | #include "dev_iface.h"
|
---|
58 | #include "ddf/driver.h"
|
---|
59 | #include "ddf/interrupt.h"
|
---|
60 |
|
---|
61 | /** Driver structure */
|
---|
62 | static driver_t *driver;
|
---|
63 |
|
---|
64 | /** Devices */
|
---|
65 | LIST_INITIALIZE(devices);
|
---|
66 | FIBRIL_MUTEX_INITIALIZE(devices_mutex);
|
---|
67 |
|
---|
68 | /** Functions */
|
---|
69 | LIST_INITIALIZE(functions);
|
---|
70 | FIBRIL_MUTEX_INITIALIZE(functions_mutex);
|
---|
71 |
|
---|
72 | static ddf_dev_t *create_device(void);
|
---|
73 | static void delete_device(ddf_dev_t *);
|
---|
74 | static void dev_add_ref(ddf_dev_t *);
|
---|
75 | static void dev_del_ref(ddf_dev_t *);
|
---|
76 | static void fun_add_ref(ddf_fun_t *);
|
---|
77 | static void fun_del_ref(ddf_fun_t *);
|
---|
78 | static remote_handler_t *function_get_default_handler(ddf_fun_t *);
|
---|
79 | static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
|
---|
80 |
|
---|
81 | static void add_to_functions_list(ddf_fun_t *fun)
|
---|
82 | {
|
---|
83 | fibril_mutex_lock(&functions_mutex);
|
---|
84 | list_append(&fun->link, &functions);
|
---|
85 | fibril_mutex_unlock(&functions_mutex);
|
---|
86 | }
|
---|
87 |
|
---|
88 | static void remove_from_functions_list(ddf_fun_t *fun)
|
---|
89 | {
|
---|
90 | fibril_mutex_lock(&functions_mutex);
|
---|
91 | list_remove(&fun->link);
|
---|
92 | fibril_mutex_unlock(&functions_mutex);
|
---|
93 | }
|
---|
94 |
|
---|
95 | static ddf_dev_t *driver_get_device(devman_handle_t handle)
|
---|
96 | {
|
---|
97 | ddf_dev_t *dev = NULL;
|
---|
98 |
|
---|
99 | assert(fibril_mutex_is_locked(&devices_mutex));
|
---|
100 |
|
---|
101 | list_foreach(devices, link) {
|
---|
102 | dev = list_get_instance(link, ddf_dev_t, link);
|
---|
103 | if (dev->handle == handle)
|
---|
104 | return dev;
|
---|
105 | }
|
---|
106 |
|
---|
107 | return NULL;
|
---|
108 | }
|
---|
109 |
|
---|
110 | static ddf_fun_t *driver_get_function(devman_handle_t handle)
|
---|
111 | {
|
---|
112 | ddf_fun_t *fun = NULL;
|
---|
113 |
|
---|
114 | assert(fibril_mutex_is_locked(&functions_mutex));
|
---|
115 |
|
---|
116 | list_foreach(functions, link) {
|
---|
117 | fun = list_get_instance(link, ddf_fun_t, link);
|
---|
118 | if (fun->handle == handle)
|
---|
119 | return fun;
|
---|
120 | }
|
---|
121 |
|
---|
122 | return NULL;
|
---|
123 | }
|
---|
124 |
|
---|
125 | static void driver_dev_add(ipc_callid_t iid, ipc_call_t *icall)
|
---|
126 | {
|
---|
127 | char *dev_name = NULL;
|
---|
128 | int res;
|
---|
129 |
|
---|
130 | devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
---|
131 | devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
|
---|
132 |
|
---|
133 | ddf_dev_t *dev = create_device();
|
---|
134 |
|
---|
135 | /* Add one reference that will be dropped by driver_dev_remove() */
|
---|
136 | dev_add_ref(dev);
|
---|
137 | dev->handle = dev_handle;
|
---|
138 |
|
---|
139 | async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
|
---|
140 | dev->name = dev_name;
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Currently not used, parent fun handle is stored in context
|
---|
144 | * of the connection to the parent device driver.
|
---|
145 | */
|
---|
146 | (void) parent_fun_handle;
|
---|
147 |
|
---|
148 | res = driver->driver_ops->dev_add(dev);
|
---|
149 |
|
---|
150 | if (res != EOK) {
|
---|
151 | dev_del_ref(dev);
|
---|
152 | async_answer_0(iid, res);
|
---|
153 | return;
|
---|
154 | }
|
---|
155 |
|
---|
156 | fibril_mutex_lock(&devices_mutex);
|
---|
157 | list_append(&dev->link, &devices);
|
---|
158 | fibril_mutex_unlock(&devices_mutex);
|
---|
159 |
|
---|
160 | async_answer_0(iid, res);
|
---|
161 | }
|
---|
162 |
|
---|
163 | static void driver_dev_remove(ipc_callid_t iid, ipc_call_t *icall)
|
---|
164 | {
|
---|
165 | devman_handle_t devh;
|
---|
166 | ddf_dev_t *dev;
|
---|
167 | int rc;
|
---|
168 |
|
---|
169 | devh = IPC_GET_ARG1(*icall);
|
---|
170 |
|
---|
171 | fibril_mutex_lock(&devices_mutex);
|
---|
172 | dev = driver_get_device(devh);
|
---|
173 | if (dev != NULL)
|
---|
174 | dev_add_ref(dev);
|
---|
175 | fibril_mutex_unlock(&devices_mutex);
|
---|
176 |
|
---|
177 | if (dev == NULL) {
|
---|
178 | async_answer_0(iid, ENOENT);
|
---|
179 | return;
|
---|
180 | }
|
---|
181 |
|
---|
182 | if (driver->driver_ops->dev_remove != NULL)
|
---|
183 | rc = driver->driver_ops->dev_remove(dev);
|
---|
184 | else
|
---|
185 | rc = ENOTSUP;
|
---|
186 |
|
---|
187 | if (rc == EOK)
|
---|
188 | dev_del_ref(dev);
|
---|
189 |
|
---|
190 | async_answer_0(iid, (sysarg_t) rc);
|
---|
191 | }
|
---|
192 |
|
---|
193 | static void driver_dev_gone(ipc_callid_t iid, ipc_call_t *icall)
|
---|
194 | {
|
---|
195 | devman_handle_t devh;
|
---|
196 | ddf_dev_t *dev;
|
---|
197 | int rc;
|
---|
198 |
|
---|
199 | devh = IPC_GET_ARG1(*icall);
|
---|
200 |
|
---|
201 | fibril_mutex_lock(&devices_mutex);
|
---|
202 | dev = driver_get_device(devh);
|
---|
203 | if (dev != NULL)
|
---|
204 | dev_add_ref(dev);
|
---|
205 | fibril_mutex_unlock(&devices_mutex);
|
---|
206 |
|
---|
207 | if (dev == NULL) {
|
---|
208 | async_answer_0(iid, ENOENT);
|
---|
209 | return;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (driver->driver_ops->dev_gone != NULL)
|
---|
213 | rc = driver->driver_ops->dev_gone(dev);
|
---|
214 | else
|
---|
215 | rc = ENOTSUP;
|
---|
216 |
|
---|
217 | if (rc == EOK)
|
---|
218 | dev_del_ref(dev);
|
---|
219 |
|
---|
220 | async_answer_0(iid, (sysarg_t) rc);
|
---|
221 | }
|
---|
222 |
|
---|
223 | static void driver_fun_online(ipc_callid_t iid, ipc_call_t *icall)
|
---|
224 | {
|
---|
225 | devman_handle_t funh;
|
---|
226 | ddf_fun_t *fun;
|
---|
227 | int rc;
|
---|
228 |
|
---|
229 | funh = IPC_GET_ARG1(*icall);
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * Look the function up. Bump reference count so that
|
---|
233 | * the function continues to exist until we return
|
---|
234 | * from the driver.
|
---|
235 | */
|
---|
236 | fibril_mutex_lock(&functions_mutex);
|
---|
237 |
|
---|
238 | fun = driver_get_function(funh);
|
---|
239 | if (fun != NULL)
|
---|
240 | fun_add_ref(fun);
|
---|
241 |
|
---|
242 | fibril_mutex_unlock(&functions_mutex);
|
---|
243 |
|
---|
244 | if (fun == NULL) {
|
---|
245 | async_answer_0(iid, ENOENT);
|
---|
246 | return;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /* Call driver entry point */
|
---|
250 | if (driver->driver_ops->fun_online != NULL)
|
---|
251 | rc = driver->driver_ops->fun_online(fun);
|
---|
252 | else
|
---|
253 | rc = ENOTSUP;
|
---|
254 |
|
---|
255 | fun_del_ref(fun);
|
---|
256 |
|
---|
257 | async_answer_0(iid, (sysarg_t) rc);
|
---|
258 | }
|
---|
259 |
|
---|
260 | static void driver_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
|
---|
261 | {
|
---|
262 | devman_handle_t funh;
|
---|
263 | ddf_fun_t *fun;
|
---|
264 | int rc;
|
---|
265 |
|
---|
266 | funh = IPC_GET_ARG1(*icall);
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Look the function up. Bump reference count so that
|
---|
270 | * the function continues to exist until we return
|
---|
271 | * from the driver.
|
---|
272 | */
|
---|
273 | fibril_mutex_lock(&functions_mutex);
|
---|
274 |
|
---|
275 | fun = driver_get_function(funh);
|
---|
276 | if (fun != NULL)
|
---|
277 | fun_add_ref(fun);
|
---|
278 |
|
---|
279 | fibril_mutex_unlock(&functions_mutex);
|
---|
280 |
|
---|
281 | if (fun == NULL) {
|
---|
282 | async_answer_0(iid, ENOENT);
|
---|
283 | return;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /* Call driver entry point */
|
---|
287 | if (driver->driver_ops->fun_offline != NULL)
|
---|
288 | rc = driver->driver_ops->fun_offline(fun);
|
---|
289 | else
|
---|
290 | rc = ENOTSUP;
|
---|
291 |
|
---|
292 | async_answer_0(iid, (sysarg_t) rc);
|
---|
293 | }
|
---|
294 |
|
---|
295 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
|
---|
296 | {
|
---|
297 | /* Accept connection */
|
---|
298 | async_answer_0(iid, EOK);
|
---|
299 |
|
---|
300 | while (true) {
|
---|
301 | ipc_call_t call;
|
---|
302 | ipc_callid_t callid = async_get_call(&call);
|
---|
303 |
|
---|
304 | if (!IPC_GET_IMETHOD(call))
|
---|
305 | break;
|
---|
306 |
|
---|
307 | switch (IPC_GET_IMETHOD(call)) {
|
---|
308 | case DRIVER_DEV_ADD:
|
---|
309 | driver_dev_add(callid, &call);
|
---|
310 | break;
|
---|
311 | case DRIVER_DEV_REMOVE:
|
---|
312 | driver_dev_remove(callid, &call);
|
---|
313 | break;
|
---|
314 | case DRIVER_DEV_GONE:
|
---|
315 | driver_dev_gone(callid, &call);
|
---|
316 | break;
|
---|
317 | case DRIVER_FUN_ONLINE:
|
---|
318 | driver_fun_online(callid, &call);
|
---|
319 | break;
|
---|
320 | case DRIVER_FUN_OFFLINE:
|
---|
321 | driver_fun_offline(callid, &call);
|
---|
322 | break;
|
---|
323 | default:
|
---|
324 | async_answer_0(callid, ENOTSUP);
|
---|
325 | }
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | /** Generic client connection handler both for applications and drivers.
|
---|
330 | *
|
---|
331 | * @param drv True for driver client, false for other clients
|
---|
332 | * (applications, services, etc.).
|
---|
333 | *
|
---|
334 | */
|
---|
335 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
---|
336 | {
|
---|
337 | /*
|
---|
338 | * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
|
---|
339 | * the device to which the client connected.
|
---|
340 | */
|
---|
341 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
342 |
|
---|
343 | fibril_mutex_lock(&functions_mutex);
|
---|
344 | ddf_fun_t *fun = driver_get_function(handle);
|
---|
345 | fibril_mutex_unlock(&functions_mutex);
|
---|
346 | /* XXX Need a lock on fun */
|
---|
347 |
|
---|
348 | if (fun == NULL) {
|
---|
349 | printf("%s: driver_connection_gen error - no function with handle"
|
---|
350 | " %" PRIun " was found.\n", driver->name, handle);
|
---|
351 | async_answer_0(iid, ENOENT);
|
---|
352 | return;
|
---|
353 | }
|
---|
354 |
|
---|
355 | if (fun->conn_handler != NULL) {
|
---|
356 | /* Driver has a custom connection handler. */
|
---|
357 | (*fun->conn_handler)(iid, icall, (void *)fun);
|
---|
358 | return;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /*
|
---|
362 | * TODO - if the client is not a driver, check whether it is allowed to
|
---|
363 | * use the device.
|
---|
364 | */
|
---|
365 |
|
---|
366 | int ret = EOK;
|
---|
367 | /* Open device function */
|
---|
368 | if (fun->ops != NULL && fun->ops->open != NULL)
|
---|
369 | ret = (*fun->ops->open)(fun);
|
---|
370 |
|
---|
371 | async_answer_0(iid, ret);
|
---|
372 | if (ret != EOK)
|
---|
373 | return;
|
---|
374 |
|
---|
375 | while (true) {
|
---|
376 | ipc_callid_t callid;
|
---|
377 | ipc_call_t call;
|
---|
378 | callid = async_get_call(&call);
|
---|
379 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
380 |
|
---|
381 | if (!method) {
|
---|
382 | /* Close device function */
|
---|
383 | if (fun->ops != NULL && fun->ops->close != NULL)
|
---|
384 | (*fun->ops->close)(fun);
|
---|
385 | async_answer_0(callid, EOK);
|
---|
386 | return;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /* Convert ipc interface id to interface index */
|
---|
390 |
|
---|
391 | int iface_idx = DEV_IFACE_IDX(method);
|
---|
392 |
|
---|
393 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
394 | remote_handler_t *default_handler =
|
---|
395 | function_get_default_handler(fun);
|
---|
396 | if (default_handler != NULL) {
|
---|
397 | (*default_handler)(fun, callid, &call);
|
---|
398 | continue;
|
---|
399 | }
|
---|
400 |
|
---|
401 | /*
|
---|
402 | * Function has no such interface and
|
---|
403 | * default handler is not provided.
|
---|
404 | */
|
---|
405 | printf("%s: driver_connection_gen error - "
|
---|
406 | "invalid interface id %d.",
|
---|
407 | driver->name, iface_idx);
|
---|
408 | async_answer_0(callid, ENOTSUP);
|
---|
409 | continue;
|
---|
410 | }
|
---|
411 |
|
---|
412 | /* Calling one of the function's interfaces */
|
---|
413 |
|
---|
414 | /* Get the interface ops structure. */
|
---|
415 | void *ops = function_get_ops(fun, iface_idx);
|
---|
416 | if (ops == NULL) {
|
---|
417 | printf("%s: driver_connection_gen error - ", driver->name);
|
---|
418 | printf("Function with handle %" PRIun " has no interface "
|
---|
419 | "with id %d.\n", handle, iface_idx);
|
---|
420 | async_answer_0(callid, ENOTSUP);
|
---|
421 | continue;
|
---|
422 | }
|
---|
423 |
|
---|
424 | /*
|
---|
425 | * Get the corresponding interface for remote request
|
---|
426 | * handling ("remote interface").
|
---|
427 | */
|
---|
428 | remote_iface_t *rem_iface = get_remote_iface(iface_idx);
|
---|
429 | assert(rem_iface != NULL);
|
---|
430 |
|
---|
431 | /* get the method of the remote interface */
|
---|
432 | sysarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
433 | remote_iface_func_ptr_t iface_method_ptr =
|
---|
434 | get_remote_method(rem_iface, iface_method_idx);
|
---|
435 | if (iface_method_ptr == NULL) {
|
---|
436 | /* The interface has not such method */
|
---|
437 | printf("%s: driver_connection_gen error - "
|
---|
438 | "invalid interface method.", driver->name);
|
---|
439 | async_answer_0(callid, ENOTSUP);
|
---|
440 | continue;
|
---|
441 | }
|
---|
442 |
|
---|
443 | /*
|
---|
444 | * Call the remote interface's method, which will
|
---|
445 | * receive parameters from the remote client and it will
|
---|
446 | * pass it to the corresponding local interface method
|
---|
447 | * associated with the function by its driver.
|
---|
448 | */
|
---|
449 | (*iface_method_ptr)(fun, ops, callid, &call);
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
454 | {
|
---|
455 | driver_connection_gen(iid, icall, true);
|
---|
456 | }
|
---|
457 |
|
---|
458 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
459 | {
|
---|
460 | driver_connection_gen(iid, icall, false);
|
---|
461 | }
|
---|
462 |
|
---|
463 | /** Function for handling connections to device driver. */
|
---|
464 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
465 | {
|
---|
466 | sysarg_t conn_type;
|
---|
467 |
|
---|
468 | if (iid == 0) {
|
---|
469 | /* Callback connection from devman */
|
---|
470 | /* XXX Use separate handler for this type of connection */
|
---|
471 | conn_type = DRIVER_DEVMAN;
|
---|
472 | } else {
|
---|
473 | conn_type = IPC_GET_ARG1(*icall);
|
---|
474 | }
|
---|
475 |
|
---|
476 | /* Select interface */
|
---|
477 | switch (conn_type) {
|
---|
478 | case DRIVER_DEVMAN:
|
---|
479 | /* Handle request from device manager */
|
---|
480 | driver_connection_devman(iid, icall);
|
---|
481 | break;
|
---|
482 | case DRIVER_DRIVER:
|
---|
483 | /* Handle request from drivers of child devices */
|
---|
484 | driver_connection_driver(iid, icall);
|
---|
485 | break;
|
---|
486 | case DRIVER_CLIENT:
|
---|
487 | /* Handle request from client applications */
|
---|
488 | driver_connection_client(iid, icall);
|
---|
489 | break;
|
---|
490 | default:
|
---|
491 | /* No such interface */
|
---|
492 | async_answer_0(iid, ENOENT);
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | /** Create new device structure.
|
---|
497 | *
|
---|
498 | * @return The device structure.
|
---|
499 | */
|
---|
500 | static ddf_dev_t *create_device(void)
|
---|
501 | {
|
---|
502 | ddf_dev_t *dev;
|
---|
503 |
|
---|
504 | dev = calloc(1, sizeof(ddf_dev_t));
|
---|
505 | if (dev == NULL)
|
---|
506 | return NULL;
|
---|
507 |
|
---|
508 | return dev;
|
---|
509 | }
|
---|
510 |
|
---|
511 | /** Create new function structure.
|
---|
512 | *
|
---|
513 | * @return The device structure.
|
---|
514 | */
|
---|
515 | static ddf_fun_t *create_function(void)
|
---|
516 | {
|
---|
517 | ddf_fun_t *fun;
|
---|
518 |
|
---|
519 | fun = calloc(1, sizeof(ddf_fun_t));
|
---|
520 | if (fun == NULL)
|
---|
521 | return NULL;
|
---|
522 |
|
---|
523 | init_match_ids(&fun->match_ids);
|
---|
524 | link_initialize(&fun->link);
|
---|
525 |
|
---|
526 | return fun;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /** Delete device structure.
|
---|
530 | *
|
---|
531 | * @param dev The device structure.
|
---|
532 | */
|
---|
533 | static void delete_device(ddf_dev_t *dev)
|
---|
534 | {
|
---|
535 | if (dev->driver_data != NULL)
|
---|
536 | free(dev->driver_data);
|
---|
537 | free(dev);
|
---|
538 | }
|
---|
539 |
|
---|
540 | /** Delete function structure.
|
---|
541 | *
|
---|
542 | * @param dev The device structure.
|
---|
543 | */
|
---|
544 | static void delete_function(ddf_fun_t *fun)
|
---|
545 | {
|
---|
546 | clean_match_ids(&fun->match_ids);
|
---|
547 | if (fun->driver_data != NULL)
|
---|
548 | free(fun->driver_data);
|
---|
549 | if (fun->name != NULL)
|
---|
550 | free(fun->name);
|
---|
551 | free(fun);
|
---|
552 | }
|
---|
553 |
|
---|
554 | /** Increase device reference count. */
|
---|
555 | static void dev_add_ref(ddf_dev_t *dev)
|
---|
556 | {
|
---|
557 | atomic_inc(&dev->refcnt);
|
---|
558 | }
|
---|
559 |
|
---|
560 | /** Decrease device reference count.
|
---|
561 | *
|
---|
562 | * Free the device structure if the reference count drops to zero.
|
---|
563 | */
|
---|
564 | static void dev_del_ref(ddf_dev_t *dev)
|
---|
565 | {
|
---|
566 | if (atomic_predec(&dev->refcnt) == 0)
|
---|
567 | delete_device(dev);
|
---|
568 | }
|
---|
569 |
|
---|
570 | /** Increase function reference count.
|
---|
571 | *
|
---|
572 | * This also increases reference count on the device. The device structure
|
---|
573 | * will thus not be deallocated while there are some associated function
|
---|
574 | * structures.
|
---|
575 | */
|
---|
576 | static void fun_add_ref(ddf_fun_t *fun)
|
---|
577 | {
|
---|
578 | dev_add_ref(fun->dev);
|
---|
579 | atomic_inc(&fun->refcnt);
|
---|
580 | }
|
---|
581 |
|
---|
582 | /** Decrease function reference count.
|
---|
583 | *
|
---|
584 | * Free the function structure if the reference count drops to zero.
|
---|
585 | */
|
---|
586 | static void fun_del_ref(ddf_fun_t *fun)
|
---|
587 | {
|
---|
588 | ddf_dev_t *dev = fun->dev;
|
---|
589 |
|
---|
590 | if (atomic_predec(&fun->refcnt) == 0)
|
---|
591 | delete_function(fun);
|
---|
592 |
|
---|
593 | dev_del_ref(dev);
|
---|
594 | }
|
---|
595 |
|
---|
596 | /** Allocate driver-specific device data. */
|
---|
597 | void *ddf_dev_data_alloc(ddf_dev_t *dev, size_t size)
|
---|
598 | {
|
---|
599 | void *data;
|
---|
600 |
|
---|
601 | assert(dev->driver_data == NULL);
|
---|
602 |
|
---|
603 | data = calloc(1, size);
|
---|
604 | if (data == NULL)
|
---|
605 | return NULL;
|
---|
606 |
|
---|
607 | dev->driver_data = data;
|
---|
608 | return data;
|
---|
609 | }
|
---|
610 |
|
---|
611 | /** Create a DDF function node.
|
---|
612 | *
|
---|
613 | * Create a DDF function (in memory). Both child devices and external clients
|
---|
614 | * communicate with a device via its functions.
|
---|
615 | *
|
---|
616 | * The created function node is fully formed, but only exists in the memory
|
---|
617 | * of the client task. In order to be visible to the system, the function
|
---|
618 | * must be bound using ddf_fun_bind().
|
---|
619 | *
|
---|
620 | * This function should only fail if there is not enough free memory.
|
---|
621 | * Specifically, this function succeeds even if @a dev already has
|
---|
622 | * a (bound) function with the same name.
|
---|
623 | *
|
---|
624 | * Type: A function of type fun_inner indicates that DDF should attempt
|
---|
625 | * to attach child devices to the function. fun_exposed means that
|
---|
626 | * the function should be exported to external clients (applications).
|
---|
627 | *
|
---|
628 | * @param dev Device to which we are adding function
|
---|
629 | * @param ftype Type of function (fun_inner or fun_exposed)
|
---|
630 | * @param name Name of function
|
---|
631 | *
|
---|
632 | * @return New function or @c NULL if memory is not available
|
---|
633 | */
|
---|
634 | ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
|
---|
635 | {
|
---|
636 | ddf_fun_t *fun;
|
---|
637 |
|
---|
638 | fun = create_function();
|
---|
639 | if (fun == NULL)
|
---|
640 | return NULL;
|
---|
641 |
|
---|
642 | /* Add one reference that will be dropped by ddf_fun_destroy() */
|
---|
643 | fun->dev = dev;
|
---|
644 | fun_add_ref(fun);
|
---|
645 |
|
---|
646 | fun->bound = false;
|
---|
647 | fun->ftype = ftype;
|
---|
648 |
|
---|
649 | fun->name = str_dup(name);
|
---|
650 | if (fun->name == NULL) {
|
---|
651 | delete_function(fun);
|
---|
652 | return NULL;
|
---|
653 | }
|
---|
654 |
|
---|
655 | return fun;
|
---|
656 | }
|
---|
657 |
|
---|
658 | /** Allocate driver-specific function data. */
|
---|
659 | void *ddf_fun_data_alloc(ddf_fun_t *fun, size_t size)
|
---|
660 | {
|
---|
661 | void *data;
|
---|
662 |
|
---|
663 | assert(fun->bound == false);
|
---|
664 | assert(fun->driver_data == NULL);
|
---|
665 |
|
---|
666 | data = calloc(1, size);
|
---|
667 | if (data == NULL)
|
---|
668 | return NULL;
|
---|
669 |
|
---|
670 | fun->driver_data = data;
|
---|
671 | return data;
|
---|
672 | }
|
---|
673 |
|
---|
674 | /** Destroy DDF function node.
|
---|
675 | *
|
---|
676 | * Destroy a function previously created with ddf_fun_create(). The function
|
---|
677 | * must not be bound.
|
---|
678 | *
|
---|
679 | * @param fun Function to destroy
|
---|
680 | */
|
---|
681 | void ddf_fun_destroy(ddf_fun_t *fun)
|
---|
682 | {
|
---|
683 | assert(fun->bound == false);
|
---|
684 |
|
---|
685 | /*
|
---|
686 | * Drop the reference added by ddf_fun_create(). This will deallocate
|
---|
687 | * the function as soon as all other references are dropped (i.e.
|
---|
688 | * as soon control leaves all driver entry points called in context
|
---|
689 | * of this function.
|
---|
690 | */
|
---|
691 | fun_del_ref(fun);
|
---|
692 | }
|
---|
693 |
|
---|
694 | static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
|
---|
695 | {
|
---|
696 | assert(is_valid_iface_idx(idx));
|
---|
697 | if (fun->ops == NULL)
|
---|
698 | return NULL;
|
---|
699 | return fun->ops->interfaces[idx];
|
---|
700 | }
|
---|
701 |
|
---|
702 | /** Bind a function node.
|
---|
703 | *
|
---|
704 | * Bind the specified function to the system. This effectively makes
|
---|
705 | * the function visible to the system (uploads it to the server).
|
---|
706 | *
|
---|
707 | * This function can fail for several reasons. Specifically,
|
---|
708 | * it will fail if the device already has a bound function of
|
---|
709 | * the same name.
|
---|
710 | *
|
---|
711 | * @param fun Function to bind
|
---|
712 | * @return EOK on success or negative error code
|
---|
713 | */
|
---|
714 | int ddf_fun_bind(ddf_fun_t *fun)
|
---|
715 | {
|
---|
716 | assert(fun->bound == false);
|
---|
717 | assert(fun->name != NULL);
|
---|
718 |
|
---|
719 | int res;
|
---|
720 |
|
---|
721 | add_to_functions_list(fun);
|
---|
722 | res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
|
---|
723 | fun->dev->handle, &fun->handle);
|
---|
724 | if (res != EOK) {
|
---|
725 | remove_from_functions_list(fun);
|
---|
726 | return res;
|
---|
727 | }
|
---|
728 |
|
---|
729 | fun->bound = true;
|
---|
730 | return res;
|
---|
731 | }
|
---|
732 |
|
---|
733 | /** Unbind a function node.
|
---|
734 | *
|
---|
735 | * Unbind the specified function from the system. This effectively makes
|
---|
736 | * the function invisible to the system.
|
---|
737 | *
|
---|
738 | * @param fun Function to unbind
|
---|
739 | * @return EOK on success or negative error code
|
---|
740 | */
|
---|
741 | int ddf_fun_unbind(ddf_fun_t *fun)
|
---|
742 | {
|
---|
743 | int res;
|
---|
744 |
|
---|
745 | assert(fun->bound == true);
|
---|
746 |
|
---|
747 | res = devman_remove_function(fun->handle);
|
---|
748 | if (res != EOK)
|
---|
749 | return res;
|
---|
750 |
|
---|
751 | remove_from_functions_list(fun);
|
---|
752 |
|
---|
753 | fun->bound = false;
|
---|
754 | return EOK;
|
---|
755 | }
|
---|
756 |
|
---|
757 | /** Online function.
|
---|
758 | *
|
---|
759 | * @param fun Function to online
|
---|
760 | * @return EOK on success or negative error code
|
---|
761 | */
|
---|
762 | int ddf_fun_online(ddf_fun_t *fun)
|
---|
763 | {
|
---|
764 | int res;
|
---|
765 |
|
---|
766 | assert(fun->bound == true);
|
---|
767 |
|
---|
768 | res = devman_drv_fun_online(fun->handle);
|
---|
769 | if (res != EOK)
|
---|
770 | return res;
|
---|
771 |
|
---|
772 | return EOK;
|
---|
773 | }
|
---|
774 |
|
---|
775 | /** Offline function.
|
---|
776 | *
|
---|
777 | * @param fun Function to offline
|
---|
778 | * @return EOK on success or negative error code
|
---|
779 | */
|
---|
780 | int ddf_fun_offline(ddf_fun_t *fun)
|
---|
781 | {
|
---|
782 | int res;
|
---|
783 |
|
---|
784 | assert(fun->bound == true);
|
---|
785 |
|
---|
786 | res = devman_drv_fun_offline(fun->handle);
|
---|
787 | if (res != EOK)
|
---|
788 | return res;
|
---|
789 |
|
---|
790 | return EOK;
|
---|
791 | }
|
---|
792 |
|
---|
793 | /** Add single match ID to inner function.
|
---|
794 | *
|
---|
795 | * Construct and add a single match ID to the specified function.
|
---|
796 | * Cannot be called when the function node is bound.
|
---|
797 | *
|
---|
798 | * @param fun Function
|
---|
799 | * @param match_id_str Match string
|
---|
800 | * @param match_score Match score
|
---|
801 | * @return EOK on success, ENOMEM if out of memory.
|
---|
802 | */
|
---|
803 | int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
|
---|
804 | int match_score)
|
---|
805 | {
|
---|
806 | match_id_t *match_id;
|
---|
807 |
|
---|
808 | assert(fun->bound == false);
|
---|
809 | assert(fun->ftype == fun_inner);
|
---|
810 |
|
---|
811 | match_id = create_match_id();
|
---|
812 | if (match_id == NULL)
|
---|
813 | return ENOMEM;
|
---|
814 |
|
---|
815 | match_id->id = str_dup(match_id_str);
|
---|
816 | match_id->score = match_score;
|
---|
817 |
|
---|
818 | add_match_id(&fun->match_ids, match_id);
|
---|
819 | return EOK;
|
---|
820 | }
|
---|
821 |
|
---|
822 | /** Get default handler for client requests */
|
---|
823 | static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
|
---|
824 | {
|
---|
825 | if (fun->ops == NULL)
|
---|
826 | return NULL;
|
---|
827 | return fun->ops->default_handler;
|
---|
828 | }
|
---|
829 |
|
---|
830 | /** Add exposed function to category.
|
---|
831 | *
|
---|
832 | * Must only be called when the function is bound.
|
---|
833 | */
|
---|
834 | int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
|
---|
835 | {
|
---|
836 | assert(fun->bound == true);
|
---|
837 | assert(fun->ftype == fun_exposed);
|
---|
838 |
|
---|
839 | return devman_add_device_to_category(fun->handle, cat_name);
|
---|
840 | }
|
---|
841 |
|
---|
842 | int ddf_driver_main(driver_t *drv)
|
---|
843 | {
|
---|
844 | int rc;
|
---|
845 |
|
---|
846 | /*
|
---|
847 | * Remember the driver structure - driver_ops will be called by generic
|
---|
848 | * handler for incoming connections.
|
---|
849 | */
|
---|
850 | driver = drv;
|
---|
851 |
|
---|
852 | /* Initialize interrupt module */
|
---|
853 | interrupt_init();
|
---|
854 |
|
---|
855 | /*
|
---|
856 | * Register driver with device manager using generic handler for
|
---|
857 | * incoming connections.
|
---|
858 | */
|
---|
859 | async_set_client_connection(driver_connection);
|
---|
860 | rc = devman_driver_register(driver->name);
|
---|
861 | if (rc != EOK) {
|
---|
862 | printf("Error: Failed to register driver with device manager "
|
---|
863 | "(%s).\n", (rc == EEXISTS) ? "driver already started" :
|
---|
864 | str_error(rc));
|
---|
865 |
|
---|
866 | return 1;
|
---|
867 | }
|
---|
868 |
|
---|
869 | /* Return success from the task since server has started. */
|
---|
870 | rc = task_retval(0);
|
---|
871 | if (rc != EOK)
|
---|
872 | return 1;
|
---|
873 |
|
---|
874 | async_manager();
|
---|
875 |
|
---|
876 | /* Never reached. */
|
---|
877 | return 0;
|
---|
878 | }
|
---|
879 |
|
---|
880 | /**
|
---|
881 | * @}
|
---|
882 | */
|
---|