1 | /*
|
---|
2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @defgroup libdrv generic device driver support.
|
---|
31 | * @brief HelenOS generic device driver support.
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 |
|
---|
35 | /** @file
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <assert.h>
|
---|
39 | #include <ipc/services.h>
|
---|
40 | #include <ipc/ns.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <bool.h>
|
---|
45 | #include <fibril_synch.h>
|
---|
46 | #include <stdlib.h>
|
---|
47 | #include <str.h>
|
---|
48 | #include <ctype.h>
|
---|
49 | #include <errno.h>
|
---|
50 | #include <inttypes.h>
|
---|
51 |
|
---|
52 | #include <ipc/driver.h>
|
---|
53 |
|
---|
54 | #include "dev_iface.h"
|
---|
55 | #include "driver.h"
|
---|
56 |
|
---|
57 | /** Driver structure */
|
---|
58 | static driver_t *driver;
|
---|
59 |
|
---|
60 | /** Devices */
|
---|
61 | LIST_INITIALIZE(functions);
|
---|
62 | FIBRIL_MUTEX_INITIALIZE(functions_mutex);
|
---|
63 |
|
---|
64 | /** Interrupts */
|
---|
65 | static interrupt_context_list_t interrupt_contexts;
|
---|
66 |
|
---|
67 | static irq_cmd_t default_cmds[] = {
|
---|
68 | {
|
---|
69 | .cmd = CMD_ACCEPT
|
---|
70 | }
|
---|
71 | };
|
---|
72 |
|
---|
73 | static irq_code_t default_pseudocode = {
|
---|
74 | sizeof(default_cmds) / sizeof(irq_cmd_t),
|
---|
75 | default_cmds
|
---|
76 | };
|
---|
77 |
|
---|
78 | static device_t *create_device(void);
|
---|
79 | static void delete_device(device_t *);
|
---|
80 |
|
---|
81 | static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
|
---|
82 | {
|
---|
83 | int id = (int)IPC_GET_IMETHOD(*icall);
|
---|
84 | interrupt_context_t *ctx;
|
---|
85 |
|
---|
86 | ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
|
---|
87 | if (ctx != NULL && ctx->handler != NULL)
|
---|
88 | (*ctx->handler)(ctx->dev, iid, icall);
|
---|
89 | }
|
---|
90 |
|
---|
91 | interrupt_context_t *create_interrupt_context(void)
|
---|
92 | {
|
---|
93 | interrupt_context_t *ctx;
|
---|
94 |
|
---|
95 | ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
|
---|
96 | if (ctx != NULL)
|
---|
97 | memset(ctx, 0, sizeof(interrupt_context_t));
|
---|
98 |
|
---|
99 | return ctx;
|
---|
100 | }
|
---|
101 |
|
---|
102 | void delete_interrupt_context(interrupt_context_t *ctx)
|
---|
103 | {
|
---|
104 | if (ctx != NULL)
|
---|
105 | free(ctx);
|
---|
106 | }
|
---|
107 |
|
---|
108 | void init_interrupt_context_list(interrupt_context_list_t *list)
|
---|
109 | {
|
---|
110 | memset(list, 0, sizeof(interrupt_context_list_t));
|
---|
111 | fibril_mutex_initialize(&list->mutex);
|
---|
112 | list_initialize(&list->contexts);
|
---|
113 | }
|
---|
114 |
|
---|
115 | void
|
---|
116 | add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
|
---|
117 | {
|
---|
118 | fibril_mutex_lock(&list->mutex);
|
---|
119 | ctx->id = list->curr_id++;
|
---|
120 | list_append(&ctx->link, &list->contexts);
|
---|
121 | fibril_mutex_unlock(&list->mutex);
|
---|
122 | }
|
---|
123 |
|
---|
124 | void remove_interrupt_context(interrupt_context_list_t *list,
|
---|
125 | interrupt_context_t *ctx)
|
---|
126 | {
|
---|
127 | fibril_mutex_lock(&list->mutex);
|
---|
128 | list_remove(&ctx->link);
|
---|
129 | fibril_mutex_unlock(&list->mutex);
|
---|
130 | }
|
---|
131 |
|
---|
132 | interrupt_context_t *
|
---|
133 | find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
|
---|
134 | {
|
---|
135 | fibril_mutex_lock(&list->mutex);
|
---|
136 |
|
---|
137 | link_t *link = list->contexts.next;
|
---|
138 | interrupt_context_t *ctx;
|
---|
139 |
|
---|
140 | while (link != &list->contexts) {
|
---|
141 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
142 | if (ctx->id == id) {
|
---|
143 | fibril_mutex_unlock(&list->mutex);
|
---|
144 | return ctx;
|
---|
145 | }
|
---|
146 | link = link->next;
|
---|
147 | }
|
---|
148 |
|
---|
149 | fibril_mutex_unlock(&list->mutex);
|
---|
150 | return NULL;
|
---|
151 | }
|
---|
152 |
|
---|
153 | interrupt_context_t *
|
---|
154 | find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)
|
---|
155 | {
|
---|
156 | fibril_mutex_lock(&list->mutex);
|
---|
157 |
|
---|
158 | link_t *link = list->contexts.next;
|
---|
159 | interrupt_context_t *ctx;
|
---|
160 |
|
---|
161 | while (link != &list->contexts) {
|
---|
162 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
163 | if (ctx->irq == irq && ctx->dev == dev) {
|
---|
164 | fibril_mutex_unlock(&list->mutex);
|
---|
165 | return ctx;
|
---|
166 | }
|
---|
167 | link = link->next;
|
---|
168 | }
|
---|
169 |
|
---|
170 | fibril_mutex_unlock(&list->mutex);
|
---|
171 | return NULL;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | int
|
---|
176 | register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler,
|
---|
177 | irq_code_t *pseudocode)
|
---|
178 | {
|
---|
179 | interrupt_context_t *ctx = create_interrupt_context();
|
---|
180 |
|
---|
181 | ctx->dev = dev;
|
---|
182 | ctx->irq = irq;
|
---|
183 | ctx->handler = handler;
|
---|
184 |
|
---|
185 | add_interrupt_context(&interrupt_contexts, ctx);
|
---|
186 |
|
---|
187 | if (pseudocode == NULL)
|
---|
188 | pseudocode = &default_pseudocode;
|
---|
189 |
|
---|
190 | int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
|
---|
191 | if (res != EOK) {
|
---|
192 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
193 | delete_interrupt_context(ctx);
|
---|
194 | }
|
---|
195 |
|
---|
196 | return res;
|
---|
197 | }
|
---|
198 |
|
---|
199 | int unregister_interrupt_handler(device_t *dev, int irq)
|
---|
200 | {
|
---|
201 | interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
|
---|
202 | dev, irq);
|
---|
203 | int res = unregister_irq(irq, dev->handle);
|
---|
204 |
|
---|
205 | if (ctx != NULL) {
|
---|
206 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
207 | delete_interrupt_context(ctx);
|
---|
208 | }
|
---|
209 |
|
---|
210 | return res;
|
---|
211 | }
|
---|
212 |
|
---|
213 | static void add_to_functions_list(function_t *fun)
|
---|
214 | {
|
---|
215 | fibril_mutex_lock(&functions_mutex);
|
---|
216 | list_append(&fun->link, &functions);
|
---|
217 | fibril_mutex_unlock(&functions_mutex);
|
---|
218 | }
|
---|
219 |
|
---|
220 | static void remove_from_functions_list(function_t *fun)
|
---|
221 | {
|
---|
222 | fibril_mutex_lock(&functions_mutex);
|
---|
223 | list_remove(&fun->link);
|
---|
224 | fibril_mutex_unlock(&functions_mutex);
|
---|
225 | }
|
---|
226 |
|
---|
227 | static function_t *driver_get_function(link_t *functions, devman_handle_t handle)
|
---|
228 | {
|
---|
229 | function_t *fun = NULL;
|
---|
230 | printf("driver_get_function handle=%" PRIun "\n", handle);
|
---|
231 |
|
---|
232 | fibril_mutex_lock(&functions_mutex);
|
---|
233 | link_t *link = functions->next;
|
---|
234 |
|
---|
235 | while (link != functions) {
|
---|
236 | fun = list_get_instance(link, function_t, link);
|
---|
237 | printf(" - fun handle %" PRIun "\n", fun->handle);
|
---|
238 | if (fun->handle == handle) {
|
---|
239 | fibril_mutex_unlock(&functions_mutex);
|
---|
240 | return fun;
|
---|
241 | }
|
---|
242 |
|
---|
243 | link = link->next;
|
---|
244 | }
|
---|
245 |
|
---|
246 | fibril_mutex_unlock(&functions_mutex);
|
---|
247 |
|
---|
248 | return NULL;
|
---|
249 | }
|
---|
250 |
|
---|
251 | static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
|
---|
252 | {
|
---|
253 | char *dev_name = NULL;
|
---|
254 | int res;
|
---|
255 |
|
---|
256 | devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
---|
257 | devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
|
---|
258 |
|
---|
259 | device_t *dev = create_device();
|
---|
260 | dev->handle = dev_handle;
|
---|
261 |
|
---|
262 | async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
|
---|
263 | dev->name = dev_name;
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * Currently not used, parent fun handle is stored in context
|
---|
267 | * of the connection to the parent device driver.
|
---|
268 | */
|
---|
269 | (void) parent_fun_handle;
|
---|
270 |
|
---|
271 | res = driver->driver_ops->add_device(dev);
|
---|
272 | if (res == EOK) {
|
---|
273 | printf("%s: new device with handle=%" PRIun " was added.\n",
|
---|
274 | driver->name, dev_handle);
|
---|
275 | } else {
|
---|
276 | printf("%s: failed to add a new device with handle = %" PRIun ".\n",
|
---|
277 | driver->name, dev_handle);
|
---|
278 | delete_device(dev);
|
---|
279 | }
|
---|
280 |
|
---|
281 | async_answer_0(iid, res);
|
---|
282 | }
|
---|
283 |
|
---|
284 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
|
---|
285 | {
|
---|
286 | /* Accept connection */
|
---|
287 | async_answer_0(iid, EOK);
|
---|
288 |
|
---|
289 | bool cont = true;
|
---|
290 | while (cont) {
|
---|
291 | ipc_call_t call;
|
---|
292 | ipc_callid_t callid = async_get_call(&call);
|
---|
293 |
|
---|
294 | switch (IPC_GET_IMETHOD(call)) {
|
---|
295 | case IPC_M_PHONE_HUNGUP:
|
---|
296 | cont = false;
|
---|
297 | continue;
|
---|
298 | case DRIVER_ADD_DEVICE:
|
---|
299 | driver_add_device(callid, &call);
|
---|
300 | break;
|
---|
301 | default:
|
---|
302 | async_answer_0(callid, ENOENT);
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Generic client connection handler both for applications and drivers.
|
---|
309 | *
|
---|
310 | * @param drv True for driver client, false for other clients
|
---|
311 | * (applications, services etc.).
|
---|
312 | */
|
---|
313 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
---|
314 | {
|
---|
315 | /*
|
---|
316 | * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
|
---|
317 | * the device to which the client connected.
|
---|
318 | */
|
---|
319 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
320 | function_t *fun = driver_get_function(&functions, handle);
|
---|
321 |
|
---|
322 | if (fun == NULL) {
|
---|
323 | printf("%s: driver_connection_gen error - no function with handle"
|
---|
324 | " %" PRIun " was found.\n", driver->name, handle);
|
---|
325 | async_answer_0(iid, ENOENT);
|
---|
326 | return;
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | /*
|
---|
331 | * TODO - if the client is not a driver, check whether it is allowed to
|
---|
332 | * use the device.
|
---|
333 | */
|
---|
334 |
|
---|
335 | int ret = EOK;
|
---|
336 | /* Open device function */
|
---|
337 | if (fun->ops != NULL && fun->ops->open != NULL)
|
---|
338 | ret = (*fun->ops->open)(fun);
|
---|
339 |
|
---|
340 | async_answer_0(iid, ret);
|
---|
341 | if (ret != EOK)
|
---|
342 | return;
|
---|
343 |
|
---|
344 | while (1) {
|
---|
345 | ipc_callid_t callid;
|
---|
346 | ipc_call_t call;
|
---|
347 | callid = async_get_call(&call);
|
---|
348 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
349 | int iface_idx;
|
---|
350 |
|
---|
351 | switch (method) {
|
---|
352 | case IPC_M_PHONE_HUNGUP:
|
---|
353 | /* Close device function */
|
---|
354 | if (fun->ops != NULL && fun->ops->close != NULL)
|
---|
355 | (*fun->ops->close)(fun);
|
---|
356 | async_answer_0(callid, EOK);
|
---|
357 | return;
|
---|
358 | default:
|
---|
359 | /* convert ipc interface id to interface index */
|
---|
360 |
|
---|
361 | iface_idx = DEV_IFACE_IDX(method);
|
---|
362 |
|
---|
363 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
364 | remote_handler_t *default_handler =
|
---|
365 | function_get_default_handler(fun);
|
---|
366 | if (default_handler != NULL) {
|
---|
367 | (*default_handler)(fun, callid, &call);
|
---|
368 | break;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /*
|
---|
372 | * Function has no such interface and
|
---|
373 | * default handler is not provided.
|
---|
374 | */
|
---|
375 | printf("%s: driver_connection_gen error - "
|
---|
376 | "invalid interface id %d.",
|
---|
377 | driver->name, iface_idx);
|
---|
378 | async_answer_0(callid, ENOTSUP);
|
---|
379 | break;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /* calling one of the function's interfaces */
|
---|
383 |
|
---|
384 | /* Get the interface ops structure. */
|
---|
385 | void *ops = function_get_ops(fun, iface_idx);
|
---|
386 | if (ops == NULL) {
|
---|
387 | printf("%s: driver_connection_gen error - ",
|
---|
388 | driver->name);
|
---|
389 | printf("Function with handle %" PRIun " has no interface "
|
---|
390 | "with id %d.\n", handle, iface_idx);
|
---|
391 | async_answer_0(callid, ENOTSUP);
|
---|
392 | break;
|
---|
393 | }
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * Get the corresponding interface for remote request
|
---|
397 | * handling ("remote interface").
|
---|
398 | */
|
---|
399 | remote_iface_t *rem_iface = get_remote_iface(iface_idx);
|
---|
400 | assert(rem_iface != NULL);
|
---|
401 |
|
---|
402 | /* get the method of the remote interface */
|
---|
403 | sysarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
404 | remote_iface_func_ptr_t iface_method_ptr =
|
---|
405 | get_remote_method(rem_iface, iface_method_idx);
|
---|
406 | if (iface_method_ptr == NULL) {
|
---|
407 | // the interface has not such method
|
---|
408 | printf("%s: driver_connection_gen error - "
|
---|
409 | "invalid interface method.", driver->name);
|
---|
410 | async_answer_0(callid, ENOTSUP);
|
---|
411 | break;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /*
|
---|
415 | * Call the remote interface's method, which will
|
---|
416 | * receive parameters from the remote client and it will
|
---|
417 | * pass it to the corresponding local interface method
|
---|
418 | * associated with the function by its driver.
|
---|
419 | */
|
---|
420 | (*iface_method_ptr)(fun, ops, callid, &call);
|
---|
421 | break;
|
---|
422 | }
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
427 | {
|
---|
428 | driver_connection_gen(iid, icall, true);
|
---|
429 | }
|
---|
430 |
|
---|
431 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
432 | {
|
---|
433 | driver_connection_gen(iid, icall, false);
|
---|
434 | }
|
---|
435 |
|
---|
436 | /** Function for handling connections to device driver. */
|
---|
437 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
438 | {
|
---|
439 | /* Select interface */
|
---|
440 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
441 | case DRIVER_DEVMAN:
|
---|
442 | /* Handle request from device manager */
|
---|
443 | driver_connection_devman(iid, icall);
|
---|
444 | break;
|
---|
445 | case DRIVER_DRIVER:
|
---|
446 | /* Handle request from drivers of child devices */
|
---|
447 | driver_connection_driver(iid, icall);
|
---|
448 | break;
|
---|
449 | case DRIVER_CLIENT:
|
---|
450 | /* Handle request from client applications */
|
---|
451 | driver_connection_client(iid, icall);
|
---|
452 | break;
|
---|
453 | default:
|
---|
454 | /* No such interface */
|
---|
455 | async_answer_0(iid, ENOENT);
|
---|
456 | }
|
---|
457 | }
|
---|
458 |
|
---|
459 | /** Create new device structure.
|
---|
460 | *
|
---|
461 | * @return The device structure.
|
---|
462 | */
|
---|
463 | static device_t *create_device(void)
|
---|
464 | {
|
---|
465 | device_t *dev;
|
---|
466 |
|
---|
467 | dev = malloc(sizeof(device_t));
|
---|
468 | if (dev == NULL)
|
---|
469 | return NULL;
|
---|
470 |
|
---|
471 | memset(dev, 0, sizeof(device_t));
|
---|
472 | return dev;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /** Create new function structure.
|
---|
476 | *
|
---|
477 | * @return The device structure.
|
---|
478 | */
|
---|
479 | static function_t *create_function(void)
|
---|
480 | {
|
---|
481 | function_t *fun;
|
---|
482 |
|
---|
483 | fun = calloc(1, sizeof(function_t));
|
---|
484 | if (fun == NULL)
|
---|
485 | return NULL;
|
---|
486 |
|
---|
487 | init_match_ids(&fun->match_ids);
|
---|
488 | link_initialize(&fun->link);
|
---|
489 |
|
---|
490 | return fun;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /** Delete device structure.
|
---|
494 | *
|
---|
495 | * @param dev The device structure.
|
---|
496 | */
|
---|
497 | static void delete_device(device_t *dev)
|
---|
498 | {
|
---|
499 | free(dev);
|
---|
500 | }
|
---|
501 |
|
---|
502 | /** Delete device structure.
|
---|
503 | *
|
---|
504 | * @param dev The device structure.
|
---|
505 | */
|
---|
506 | static void delete_function(function_t *fun)
|
---|
507 | {
|
---|
508 | clean_match_ids(&fun->match_ids);
|
---|
509 | if (fun->name != NULL)
|
---|
510 | free(fun->name);
|
---|
511 | free(fun);
|
---|
512 | }
|
---|
513 |
|
---|
514 | /** Create a DDF function node.
|
---|
515 | *
|
---|
516 | * Create a DDF function (in memory). Both child devices and external clients
|
---|
517 | * communicate with a device via its functions.
|
---|
518 | *
|
---|
519 | * The created function node is fully formed, but only exists in the memory
|
---|
520 | * of the client task. In order to be visible to the system, the function
|
---|
521 | * must be bound using ddf_fun_bind().
|
---|
522 | *
|
---|
523 | * This function should only fail if there is not enough free memory.
|
---|
524 | * Specifically, this function succeeds even if @a dev already has
|
---|
525 | * a (bound) function with the same name.
|
---|
526 | *
|
---|
527 | * Type: A function of type fun_inner indicates that DDF should attempt
|
---|
528 | * to attach child devices to the function. fun_exposed means that
|
---|
529 | * the function should be exported to external clients (applications).
|
---|
530 | *
|
---|
531 | * @param dev Device to which we are adding function
|
---|
532 | * @param ftype Type of function (fun_inner or fun_exposed)
|
---|
533 | * @param name Name of function
|
---|
534 | *
|
---|
535 | * @return New function or @c NULL if memory is not available
|
---|
536 | */
|
---|
537 | function_t *ddf_fun_create(device_t *dev, fun_type_t ftype, const char *name)
|
---|
538 | {
|
---|
539 | function_t *fun;
|
---|
540 |
|
---|
541 | fun = create_function();
|
---|
542 | if (fun == NULL)
|
---|
543 | return NULL;
|
---|
544 |
|
---|
545 | fun->bound = false;
|
---|
546 | fun->dev = dev;
|
---|
547 | fun->ftype = ftype;
|
---|
548 |
|
---|
549 | fun->name = str_dup(name);
|
---|
550 | if (fun->name == NULL) {
|
---|
551 | delete_function(fun);
|
---|
552 | return NULL;
|
---|
553 | }
|
---|
554 |
|
---|
555 | return fun;
|
---|
556 | }
|
---|
557 |
|
---|
558 | /** Destroy DDF function node.
|
---|
559 | *
|
---|
560 | * Destroy a function previously created with ddf_fun_create(). The function
|
---|
561 | * must not be bound.
|
---|
562 | *
|
---|
563 | * @param fun Function to destroy
|
---|
564 | */
|
---|
565 | void ddf_fun_destroy(function_t *fun)
|
---|
566 | {
|
---|
567 | assert(fun->bound == false);
|
---|
568 | delete_function(fun);
|
---|
569 | }
|
---|
570 |
|
---|
571 | void *function_get_ops(function_t *fun, dev_inferface_idx_t idx)
|
---|
572 | {
|
---|
573 | assert(is_valid_iface_idx(idx));
|
---|
574 | if (fun->ops == NULL)
|
---|
575 | return NULL;
|
---|
576 | return fun->ops->interfaces[idx];
|
---|
577 | }
|
---|
578 |
|
---|
579 | /** Bind a function node.
|
---|
580 | *
|
---|
581 | * Bind the specified function to the system. This effectively makes
|
---|
582 | * the function visible to the system (uploads it to the server).
|
---|
583 | *
|
---|
584 | * This function can fail for several reasons. Specifically,
|
---|
585 | * it will fail if the device already has a bound function of
|
---|
586 | * the same name.
|
---|
587 | *
|
---|
588 | * @param fun Function to bind
|
---|
589 | * @return EOK on success or negative error code
|
---|
590 | */
|
---|
591 | int ddf_fun_bind(function_t *fun)
|
---|
592 | {
|
---|
593 | assert(fun->name != NULL);
|
---|
594 |
|
---|
595 | int res;
|
---|
596 |
|
---|
597 | add_to_functions_list(fun);
|
---|
598 | res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
|
---|
599 | fun->dev->handle, &fun->handle);
|
---|
600 | if (res != EOK) {
|
---|
601 | remove_from_functions_list(fun);
|
---|
602 | return res;
|
---|
603 | }
|
---|
604 |
|
---|
605 | fun->bound = true;
|
---|
606 | return res;
|
---|
607 | }
|
---|
608 |
|
---|
609 | /** Wrapper for child_device_register for devices with single match id.
|
---|
610 | *
|
---|
611 | * @param parent Parent device.
|
---|
612 | * @param child_name Child device name.
|
---|
613 | * @param child_match_id Child device match id.
|
---|
614 | * @param child_match_score Child device match score.
|
---|
615 | * @return Error code.
|
---|
616 | */
|
---|
617 | int register_function_wrapper(device_t *dev, const char *fun_name,
|
---|
618 | const char *match_id, int match_score)
|
---|
619 | {
|
---|
620 | function_t *fun = NULL;
|
---|
621 | match_id_t *m_id = NULL;
|
---|
622 | int rc;
|
---|
623 |
|
---|
624 | fun = ddf_fun_create(dev, fun_inner, fun_name);
|
---|
625 | if (fun == NULL) {
|
---|
626 | rc = ENOMEM;
|
---|
627 | goto failure;
|
---|
628 | }
|
---|
629 |
|
---|
630 | m_id = create_match_id();
|
---|
631 | if (m_id == NULL) {
|
---|
632 | rc = ENOMEM;
|
---|
633 | goto failure;
|
---|
634 | }
|
---|
635 |
|
---|
636 | m_id->id = match_id;
|
---|
637 | m_id->score = match_score;
|
---|
638 | add_match_id(&fun->match_ids, m_id);
|
---|
639 |
|
---|
640 | rc = ddf_fun_bind(fun);
|
---|
641 | if (rc != EOK)
|
---|
642 | goto failure;
|
---|
643 |
|
---|
644 | return EOK;
|
---|
645 |
|
---|
646 | failure:
|
---|
647 | if (m_id != NULL) {
|
---|
648 | m_id->id = NULL;
|
---|
649 | delete_match_id(m_id);
|
---|
650 | }
|
---|
651 |
|
---|
652 | if (fun != NULL) {
|
---|
653 | fun->name = NULL;
|
---|
654 | delete_function(fun);
|
---|
655 | }
|
---|
656 |
|
---|
657 | return rc;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /** Get default handler for client requests */
|
---|
661 | remote_handler_t *function_get_default_handler(function_t *fun)
|
---|
662 | {
|
---|
663 | if (fun->ops == NULL)
|
---|
664 | return NULL;
|
---|
665 | return fun->ops->default_handler;
|
---|
666 | }
|
---|
667 |
|
---|
668 | int add_function_to_class(function_t *fun, const char *class_name)
|
---|
669 | {
|
---|
670 | return devman_add_device_to_class(fun->handle, class_name);
|
---|
671 | }
|
---|
672 |
|
---|
673 | int driver_main(driver_t *drv)
|
---|
674 | {
|
---|
675 | /*
|
---|
676 | * Remember the driver structure - driver_ops will be called by generic
|
---|
677 | * handler for incoming connections.
|
---|
678 | */
|
---|
679 | driver = drv;
|
---|
680 |
|
---|
681 | /* Initialize the list of interrupt contexts. */
|
---|
682 | init_interrupt_context_list(&interrupt_contexts);
|
---|
683 |
|
---|
684 | /* Set generic interrupt handler. */
|
---|
685 | async_set_interrupt_received(driver_irq_handler);
|
---|
686 |
|
---|
687 | /*
|
---|
688 | * Register driver by device manager with generic handler for incoming
|
---|
689 | * connections.
|
---|
690 | */
|
---|
691 | devman_driver_register(driver->name, driver_connection);
|
---|
692 |
|
---|
693 | async_manager();
|
---|
694 |
|
---|
695 | /* Never reached. */
|
---|
696 | return 0;
|
---|
697 | }
|
---|
698 |
|
---|
699 | /**
|
---|
700 | * @}
|
---|
701 | */
|
---|