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(functions);
|
---|
66 | FIBRIL_MUTEX_INITIALIZE(functions_mutex);
|
---|
67 |
|
---|
68 | /** Interrupts */
|
---|
69 | static interrupt_context_list_t interrupt_contexts;
|
---|
70 |
|
---|
71 | static irq_cmd_t default_cmds[] = {
|
---|
72 | {
|
---|
73 | .cmd = CMD_ACCEPT
|
---|
74 | }
|
---|
75 | };
|
---|
76 |
|
---|
77 | static irq_code_t default_pseudocode = {
|
---|
78 | sizeof(default_cmds) / sizeof(irq_cmd_t),
|
---|
79 | default_cmds
|
---|
80 | };
|
---|
81 |
|
---|
82 | static ddf_dev_t *create_device(void);
|
---|
83 | static void delete_device(ddf_dev_t *);
|
---|
84 | static remote_handler_t *function_get_default_handler(ddf_fun_t *);
|
---|
85 | static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
|
---|
86 |
|
---|
87 | static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
|
---|
88 | {
|
---|
89 | int id = (int)IPC_GET_IMETHOD(*icall);
|
---|
90 | interrupt_context_t *ctx;
|
---|
91 |
|
---|
92 | ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
|
---|
93 | if (ctx != NULL && ctx->handler != NULL)
|
---|
94 | (*ctx->handler)(ctx->dev, iid, icall);
|
---|
95 | }
|
---|
96 |
|
---|
97 | interrupt_context_t *create_interrupt_context(void)
|
---|
98 | {
|
---|
99 | interrupt_context_t *ctx;
|
---|
100 |
|
---|
101 | ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
|
---|
102 | if (ctx != NULL)
|
---|
103 | memset(ctx, 0, sizeof(interrupt_context_t));
|
---|
104 |
|
---|
105 | return ctx;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void delete_interrupt_context(interrupt_context_t *ctx)
|
---|
109 | {
|
---|
110 | if (ctx != NULL)
|
---|
111 | free(ctx);
|
---|
112 | }
|
---|
113 |
|
---|
114 | void init_interrupt_context_list(interrupt_context_list_t *list)
|
---|
115 | {
|
---|
116 | memset(list, 0, sizeof(interrupt_context_list_t));
|
---|
117 | fibril_mutex_initialize(&list->mutex);
|
---|
118 | list_initialize(&list->contexts);
|
---|
119 | }
|
---|
120 |
|
---|
121 | void
|
---|
122 | add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
|
---|
123 | {
|
---|
124 | fibril_mutex_lock(&list->mutex);
|
---|
125 | ctx->id = list->curr_id++;
|
---|
126 | list_append(&ctx->link, &list->contexts);
|
---|
127 | fibril_mutex_unlock(&list->mutex);
|
---|
128 | }
|
---|
129 |
|
---|
130 | void remove_interrupt_context(interrupt_context_list_t *list,
|
---|
131 | interrupt_context_t *ctx)
|
---|
132 | {
|
---|
133 | fibril_mutex_lock(&list->mutex);
|
---|
134 | list_remove(&ctx->link);
|
---|
135 | fibril_mutex_unlock(&list->mutex);
|
---|
136 | }
|
---|
137 |
|
---|
138 | interrupt_context_t *
|
---|
139 | find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
|
---|
140 | {
|
---|
141 | interrupt_context_t *ctx;
|
---|
142 |
|
---|
143 | fibril_mutex_lock(&list->mutex);
|
---|
144 |
|
---|
145 | list_foreach(list->contexts, link) {
|
---|
146 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
147 | if (ctx->id == id) {
|
---|
148 | fibril_mutex_unlock(&list->mutex);
|
---|
149 | return ctx;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | fibril_mutex_unlock(&list->mutex);
|
---|
154 | return NULL;
|
---|
155 | }
|
---|
156 |
|
---|
157 | interrupt_context_t *
|
---|
158 | find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
|
---|
159 | {
|
---|
160 | interrupt_context_t *ctx;
|
---|
161 |
|
---|
162 | fibril_mutex_lock(&list->mutex);
|
---|
163 |
|
---|
164 | list_foreach(list->contexts, link) {
|
---|
165 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
166 | if (ctx->irq == irq && ctx->dev == dev) {
|
---|
167 | fibril_mutex_unlock(&list->mutex);
|
---|
168 | return ctx;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | fibril_mutex_unlock(&list->mutex);
|
---|
173 | return NULL;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | int
|
---|
178 | register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
|
---|
179 | irq_code_t *pseudocode)
|
---|
180 | {
|
---|
181 | interrupt_context_t *ctx = create_interrupt_context();
|
---|
182 |
|
---|
183 | ctx->dev = dev;
|
---|
184 | ctx->irq = irq;
|
---|
185 | ctx->handler = handler;
|
---|
186 |
|
---|
187 | add_interrupt_context(&interrupt_contexts, ctx);
|
---|
188 |
|
---|
189 | if (pseudocode == NULL)
|
---|
190 | pseudocode = &default_pseudocode;
|
---|
191 |
|
---|
192 | int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
|
---|
193 | if (res != EOK) {
|
---|
194 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
195 | delete_interrupt_context(ctx);
|
---|
196 | }
|
---|
197 |
|
---|
198 | return res;
|
---|
199 | }
|
---|
200 |
|
---|
201 | int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
|
---|
202 | {
|
---|
203 | interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
|
---|
204 | dev, irq);
|
---|
205 | int res = unregister_irq(irq, dev->handle);
|
---|
206 |
|
---|
207 | if (ctx != NULL) {
|
---|
208 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
209 | delete_interrupt_context(ctx);
|
---|
210 | }
|
---|
211 |
|
---|
212 | return res;
|
---|
213 | }
|
---|
214 |
|
---|
215 | static void add_to_functions_list(ddf_fun_t *fun)
|
---|
216 | {
|
---|
217 | fibril_mutex_lock(&functions_mutex);
|
---|
218 | list_append(&fun->link, &functions);
|
---|
219 | fibril_mutex_unlock(&functions_mutex);
|
---|
220 | }
|
---|
221 |
|
---|
222 | static void remove_from_functions_list(ddf_fun_t *fun)
|
---|
223 | {
|
---|
224 | fibril_mutex_lock(&functions_mutex);
|
---|
225 | list_remove(&fun->link);
|
---|
226 | fibril_mutex_unlock(&functions_mutex);
|
---|
227 | }
|
---|
228 |
|
---|
229 | static ddf_fun_t *driver_get_function(list_t *functions, devman_handle_t handle)
|
---|
230 | {
|
---|
231 | ddf_fun_t *fun = NULL;
|
---|
232 |
|
---|
233 | fibril_mutex_lock(&functions_mutex);
|
---|
234 |
|
---|
235 | list_foreach(*functions, link) {
|
---|
236 | fun = list_get_instance(link, ddf_fun_t, link);
|
---|
237 | if (fun->handle == handle) {
|
---|
238 | fibril_mutex_unlock(&functions_mutex);
|
---|
239 | return fun;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | fibril_mutex_unlock(&functions_mutex);
|
---|
244 |
|
---|
245 | return NULL;
|
---|
246 | }
|
---|
247 |
|
---|
248 | static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
|
---|
249 | {
|
---|
250 | char *dev_name = NULL;
|
---|
251 | int res;
|
---|
252 |
|
---|
253 | devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
---|
254 | devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
|
---|
255 |
|
---|
256 | ddf_dev_t *dev = create_device();
|
---|
257 | dev->handle = dev_handle;
|
---|
258 |
|
---|
259 | async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
|
---|
260 | dev->name = dev_name;
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * Currently not used, parent fun handle is stored in context
|
---|
264 | * of the connection to the parent device driver.
|
---|
265 | */
|
---|
266 | (void) parent_fun_handle;
|
---|
267 |
|
---|
268 | res = driver->driver_ops->add_device(dev);
|
---|
269 | if (res != EOK)
|
---|
270 | delete_device(dev);
|
---|
271 |
|
---|
272 | async_answer_0(iid, res);
|
---|
273 | }
|
---|
274 |
|
---|
275 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
|
---|
276 | {
|
---|
277 | /* Accept connection */
|
---|
278 | async_answer_0(iid, EOK);
|
---|
279 |
|
---|
280 | while (true) {
|
---|
281 | ipc_call_t call;
|
---|
282 | ipc_callid_t callid = async_get_call(&call);
|
---|
283 |
|
---|
284 | if (!IPC_GET_IMETHOD(call))
|
---|
285 | break;
|
---|
286 |
|
---|
287 | switch (IPC_GET_IMETHOD(call)) {
|
---|
288 | case DRIVER_ADD_DEVICE:
|
---|
289 | driver_add_device(callid, &call);
|
---|
290 | break;
|
---|
291 | default:
|
---|
292 | async_answer_0(callid, ENOENT);
|
---|
293 | }
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** Generic client connection handler both for applications and drivers.
|
---|
298 | *
|
---|
299 | * @param drv True for driver client, false for other clients
|
---|
300 | * (applications, services, etc.).
|
---|
301 | *
|
---|
302 | */
|
---|
303 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
---|
304 | {
|
---|
305 | /*
|
---|
306 | * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
|
---|
307 | * the device to which the client connected.
|
---|
308 | */
|
---|
309 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
310 | ddf_fun_t *fun = driver_get_function(&functions, handle);
|
---|
311 |
|
---|
312 | if (fun == NULL) {
|
---|
313 | printf("%s: driver_connection_gen error - no function with handle"
|
---|
314 | " %" PRIun " was found.\n", driver->name, handle);
|
---|
315 | async_answer_0(iid, ENOENT);
|
---|
316 | return;
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (fun->conn_handler != NULL) {
|
---|
320 | /* Driver has a custom connection handler. */
|
---|
321 | (*fun->conn_handler)(iid, icall, (void *)fun);
|
---|
322 | return;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /*
|
---|
326 | * TODO - if the client is not a driver, check whether it is allowed to
|
---|
327 | * use the device.
|
---|
328 | */
|
---|
329 |
|
---|
330 | int ret = EOK;
|
---|
331 | /* Open device function */
|
---|
332 | if (fun->ops != NULL && fun->ops->open != NULL)
|
---|
333 | ret = (*fun->ops->open)(fun);
|
---|
334 |
|
---|
335 | async_answer_0(iid, ret);
|
---|
336 | if (ret != EOK)
|
---|
337 | return;
|
---|
338 |
|
---|
339 | while (true) {
|
---|
340 | ipc_callid_t callid;
|
---|
341 | ipc_call_t call;
|
---|
342 | callid = async_get_call(&call);
|
---|
343 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
344 |
|
---|
345 | if (!method) {
|
---|
346 | /* Close device function */
|
---|
347 | if (fun->ops != NULL && fun->ops->close != NULL)
|
---|
348 | (*fun->ops->close)(fun);
|
---|
349 | async_answer_0(callid, EOK);
|
---|
350 | return;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /* Convert ipc interface id to interface index */
|
---|
354 |
|
---|
355 | int iface_idx = DEV_IFACE_IDX(method);
|
---|
356 |
|
---|
357 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
358 | remote_handler_t *default_handler =
|
---|
359 | function_get_default_handler(fun);
|
---|
360 | if (default_handler != NULL) {
|
---|
361 | (*default_handler)(fun, callid, &call);
|
---|
362 | continue;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /*
|
---|
366 | * Function has no such interface and
|
---|
367 | * default handler is not provided.
|
---|
368 | */
|
---|
369 | printf("%s: driver_connection_gen error - "
|
---|
370 | "invalid interface id %d.",
|
---|
371 | driver->name, iface_idx);
|
---|
372 | async_answer_0(callid, ENOTSUP);
|
---|
373 | continue;
|
---|
374 | }
|
---|
375 |
|
---|
376 | /* Calling one of the function's interfaces */
|
---|
377 |
|
---|
378 | /* Get the interface ops structure. */
|
---|
379 | void *ops = function_get_ops(fun, iface_idx);
|
---|
380 | if (ops == NULL) {
|
---|
381 | printf("%s: driver_connection_gen error - ", driver->name);
|
---|
382 | printf("Function with handle %" PRIun " has no interface "
|
---|
383 | "with id %d.\n", handle, iface_idx);
|
---|
384 | async_answer_0(callid, ENOTSUP);
|
---|
385 | continue;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /*
|
---|
389 | * Get the corresponding interface for remote request
|
---|
390 | * handling ("remote interface").
|
---|
391 | */
|
---|
392 | remote_iface_t *rem_iface = get_remote_iface(iface_idx);
|
---|
393 | assert(rem_iface != NULL);
|
---|
394 |
|
---|
395 | /* get the method of the remote interface */
|
---|
396 | sysarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
397 | remote_iface_func_ptr_t iface_method_ptr =
|
---|
398 | get_remote_method(rem_iface, iface_method_idx);
|
---|
399 | if (iface_method_ptr == NULL) {
|
---|
400 | /* The interface has not such method */
|
---|
401 | printf("%s: driver_connection_gen error - "
|
---|
402 | "invalid interface method.", driver->name);
|
---|
403 | async_answer_0(callid, ENOTSUP);
|
---|
404 | continue;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /*
|
---|
408 | * Call the remote interface's method, which will
|
---|
409 | * receive parameters from the remote client and it will
|
---|
410 | * pass it to the corresponding local interface method
|
---|
411 | * associated with the function by its driver.
|
---|
412 | */
|
---|
413 | (*iface_method_ptr)(fun, ops, callid, &call);
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
418 | {
|
---|
419 | driver_connection_gen(iid, icall, true);
|
---|
420 | }
|
---|
421 |
|
---|
422 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
423 | {
|
---|
424 | driver_connection_gen(iid, icall, false);
|
---|
425 | }
|
---|
426 |
|
---|
427 | /** Function for handling connections to device driver. */
|
---|
428 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
429 | {
|
---|
430 | /* Select interface */
|
---|
431 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
432 | case DRIVER_DEVMAN:
|
---|
433 | /* Handle request from device manager */
|
---|
434 | driver_connection_devman(iid, icall);
|
---|
435 | break;
|
---|
436 | case DRIVER_DRIVER:
|
---|
437 | /* Handle request from drivers of child devices */
|
---|
438 | driver_connection_driver(iid, icall);
|
---|
439 | break;
|
---|
440 | case DRIVER_CLIENT:
|
---|
441 | /* Handle request from client applications */
|
---|
442 | driver_connection_client(iid, icall);
|
---|
443 | break;
|
---|
444 | default:
|
---|
445 | /* No such interface */
|
---|
446 | async_answer_0(iid, ENOENT);
|
---|
447 | }
|
---|
448 | }
|
---|
449 |
|
---|
450 | /** Create new device structure.
|
---|
451 | *
|
---|
452 | * @return The device structure.
|
---|
453 | */
|
---|
454 | static ddf_dev_t *create_device(void)
|
---|
455 | {
|
---|
456 | ddf_dev_t *dev;
|
---|
457 |
|
---|
458 | dev = malloc(sizeof(ddf_dev_t));
|
---|
459 | if (dev == NULL)
|
---|
460 | return NULL;
|
---|
461 |
|
---|
462 | memset(dev, 0, sizeof(ddf_dev_t));
|
---|
463 | return dev;
|
---|
464 | }
|
---|
465 |
|
---|
466 | /** Create new function structure.
|
---|
467 | *
|
---|
468 | * @return The device structure.
|
---|
469 | */
|
---|
470 | static ddf_fun_t *create_function(void)
|
---|
471 | {
|
---|
472 | ddf_fun_t *fun;
|
---|
473 |
|
---|
474 | fun = calloc(1, sizeof(ddf_fun_t));
|
---|
475 | if (fun == NULL)
|
---|
476 | return NULL;
|
---|
477 |
|
---|
478 | init_match_ids(&fun->match_ids);
|
---|
479 | link_initialize(&fun->link);
|
---|
480 |
|
---|
481 | return fun;
|
---|
482 | }
|
---|
483 |
|
---|
484 | /** Delete device structure.
|
---|
485 | *
|
---|
486 | * @param dev The device structure.
|
---|
487 | */
|
---|
488 | static void delete_device(ddf_dev_t *dev)
|
---|
489 | {
|
---|
490 | free(dev);
|
---|
491 | }
|
---|
492 |
|
---|
493 | /** Delete device structure.
|
---|
494 | *
|
---|
495 | * @param dev The device structure.
|
---|
496 | */
|
---|
497 | static void delete_function(ddf_fun_t *fun)
|
---|
498 | {
|
---|
499 | clean_match_ids(&fun->match_ids);
|
---|
500 | if (fun->name != NULL)
|
---|
501 | free(fun->name);
|
---|
502 | free(fun);
|
---|
503 | }
|
---|
504 |
|
---|
505 | /** Create a DDF function node.
|
---|
506 | *
|
---|
507 | * Create a DDF function (in memory). Both child devices and external clients
|
---|
508 | * communicate with a device via its functions.
|
---|
509 | *
|
---|
510 | * The created function node is fully formed, but only exists in the memory
|
---|
511 | * of the client task. In order to be visible to the system, the function
|
---|
512 | * must be bound using ddf_fun_bind().
|
---|
513 | *
|
---|
514 | * This function should only fail if there is not enough free memory.
|
---|
515 | * Specifically, this function succeeds even if @a dev already has
|
---|
516 | * a (bound) function with the same name.
|
---|
517 | *
|
---|
518 | * Type: A function of type fun_inner indicates that DDF should attempt
|
---|
519 | * to attach child devices to the function. fun_exposed means that
|
---|
520 | * the function should be exported to external clients (applications).
|
---|
521 | *
|
---|
522 | * @param dev Device to which we are adding function
|
---|
523 | * @param ftype Type of function (fun_inner or fun_exposed)
|
---|
524 | * @param name Name of function
|
---|
525 | *
|
---|
526 | * @return New function or @c NULL if memory is not available
|
---|
527 | */
|
---|
528 | ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
|
---|
529 | {
|
---|
530 | ddf_fun_t *fun;
|
---|
531 |
|
---|
532 | fun = create_function();
|
---|
533 | if (fun == NULL)
|
---|
534 | return NULL;
|
---|
535 |
|
---|
536 | fun->bound = false;
|
---|
537 | fun->dev = dev;
|
---|
538 | fun->ftype = ftype;
|
---|
539 |
|
---|
540 | fun->name = str_dup(name);
|
---|
541 | if (fun->name == NULL) {
|
---|
542 | delete_function(fun);
|
---|
543 | return NULL;
|
---|
544 | }
|
---|
545 |
|
---|
546 | return fun;
|
---|
547 | }
|
---|
548 |
|
---|
549 | /** Destroy DDF function node.
|
---|
550 | *
|
---|
551 | * Destroy a function previously created with ddf_fun_create(). The function
|
---|
552 | * must not be bound.
|
---|
553 | *
|
---|
554 | * @param fun Function to destroy
|
---|
555 | */
|
---|
556 | void ddf_fun_destroy(ddf_fun_t *fun)
|
---|
557 | {
|
---|
558 | assert(fun->bound == false);
|
---|
559 | delete_function(fun);
|
---|
560 | }
|
---|
561 |
|
---|
562 | static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
|
---|
563 | {
|
---|
564 | assert(is_valid_iface_idx(idx));
|
---|
565 | if (fun->ops == NULL)
|
---|
566 | return NULL;
|
---|
567 | return fun->ops->interfaces[idx];
|
---|
568 | }
|
---|
569 |
|
---|
570 | /** Bind a function node.
|
---|
571 | *
|
---|
572 | * Bind the specified function to the system. This effectively makes
|
---|
573 | * the function visible to the system (uploads it to the server).
|
---|
574 | *
|
---|
575 | * This function can fail for several reasons. Specifically,
|
---|
576 | * it will fail if the device already has a bound function of
|
---|
577 | * the same name.
|
---|
578 | *
|
---|
579 | * @param fun Function to bind
|
---|
580 | * @return EOK on success or negative error code
|
---|
581 | */
|
---|
582 | int ddf_fun_bind(ddf_fun_t *fun)
|
---|
583 | {
|
---|
584 | assert(fun->bound == false);
|
---|
585 | assert(fun->name != NULL);
|
---|
586 |
|
---|
587 | int res;
|
---|
588 |
|
---|
589 | add_to_functions_list(fun);
|
---|
590 | res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
|
---|
591 | fun->dev->handle, &fun->handle);
|
---|
592 | if (res != EOK) {
|
---|
593 | remove_from_functions_list(fun);
|
---|
594 | return res;
|
---|
595 | }
|
---|
596 |
|
---|
597 | fun->bound = true;
|
---|
598 | return res;
|
---|
599 | }
|
---|
600 |
|
---|
601 | /** Unbind a function node.
|
---|
602 | *
|
---|
603 | * Unbind the specified function from the system. This effectively makes
|
---|
604 | * the function invisible to the system.
|
---|
605 | *
|
---|
606 | * @param fun Function to bind
|
---|
607 | * @return EOK on success or negative error code
|
---|
608 | */
|
---|
609 | int ddf_fun_unbind(ddf_fun_t *fun)
|
---|
610 | {
|
---|
611 | int res;
|
---|
612 |
|
---|
613 | assert(fun->bound == true);
|
---|
614 |
|
---|
615 | add_to_functions_list(fun);
|
---|
616 | res = devman_remove_function(fun->handle);
|
---|
617 | if (res != EOK)
|
---|
618 | return res;
|
---|
619 |
|
---|
620 | remove_from_functions_list(fun);
|
---|
621 |
|
---|
622 | fun->bound = false;
|
---|
623 | return EOK;
|
---|
624 | }
|
---|
625 |
|
---|
626 | /** Add single match ID to inner function.
|
---|
627 | *
|
---|
628 | * Construct and add a single match ID to the specified function.
|
---|
629 | * Cannot be called when the function node is bound.
|
---|
630 | *
|
---|
631 | * @param fun Function
|
---|
632 | * @param match_id_str Match string
|
---|
633 | * @param match_score Match score
|
---|
634 | * @return EOK on success, ENOMEM if out of memory.
|
---|
635 | */
|
---|
636 | int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
|
---|
637 | int match_score)
|
---|
638 | {
|
---|
639 | match_id_t *match_id;
|
---|
640 |
|
---|
641 | assert(fun->bound == false);
|
---|
642 | assert(fun->ftype == fun_inner);
|
---|
643 |
|
---|
644 | match_id = create_match_id();
|
---|
645 | if (match_id == NULL)
|
---|
646 | return ENOMEM;
|
---|
647 |
|
---|
648 | match_id->id = str_dup(match_id_str);
|
---|
649 | match_id->score = 90;
|
---|
650 |
|
---|
651 | add_match_id(&fun->match_ids, match_id);
|
---|
652 | return EOK;
|
---|
653 | }
|
---|
654 |
|
---|
655 | /** Get default handler for client requests */
|
---|
656 | static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
|
---|
657 | {
|
---|
658 | if (fun->ops == NULL)
|
---|
659 | return NULL;
|
---|
660 | return fun->ops->default_handler;
|
---|
661 | }
|
---|
662 |
|
---|
663 | /** Add exposed function to category.
|
---|
664 | *
|
---|
665 | * Must only be called when the function is bound.
|
---|
666 | */
|
---|
667 | int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
|
---|
668 | {
|
---|
669 | assert(fun->bound == true);
|
---|
670 | assert(fun->ftype == fun_exposed);
|
---|
671 |
|
---|
672 | return devman_add_device_to_category(fun->handle, cat_name);
|
---|
673 | }
|
---|
674 |
|
---|
675 | int ddf_driver_main(driver_t *drv)
|
---|
676 | {
|
---|
677 | int rc;
|
---|
678 |
|
---|
679 | /*
|
---|
680 | * Remember the driver structure - driver_ops will be called by generic
|
---|
681 | * handler for incoming connections.
|
---|
682 | */
|
---|
683 | driver = drv;
|
---|
684 |
|
---|
685 | /* Initialize the list of interrupt contexts. */
|
---|
686 | init_interrupt_context_list(&interrupt_contexts);
|
---|
687 |
|
---|
688 | /* Set generic interrupt handler. */
|
---|
689 | async_set_interrupt_received(driver_irq_handler);
|
---|
690 |
|
---|
691 | /*
|
---|
692 | * Register driver with device manager using generic handler for
|
---|
693 | * incoming connections.
|
---|
694 | */
|
---|
695 | rc = devman_driver_register(driver->name, driver_connection);
|
---|
696 | if (rc != EOK) {
|
---|
697 | printf("Error: Failed to register driver with device manager "
|
---|
698 | "(%s).\n", (rc == EEXISTS) ? "driver already started" :
|
---|
699 | str_error(rc));
|
---|
700 |
|
---|
701 | return 1;
|
---|
702 | }
|
---|
703 |
|
---|
704 | /* Return success from the task since server has started. */
|
---|
705 | rc = task_retval(0);
|
---|
706 | if (rc != EOK)
|
---|
707 | return 1;
|
---|
708 |
|
---|
709 | async_manager();
|
---|
710 |
|
---|
711 | /* Never reached. */
|
---|
712 | return 0;
|
---|
713 | }
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * @}
|
---|
717 | */
|
---|