source: mainline/uspace/lib/drv/generic/driver.c@ 17b3cc6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 17b3cc6 was f044e96, checked in by Martin Decky <martin@…>, 14 years ago

use systematic names

  • register_irq → irq_register
  • unregister_irq → irq_unregister
  • Property mode set to 100644
File size: 23.5 KB
Line 
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 */
62static driver_t *driver;
63
64/** Devices */
65LIST_INITIALIZE(devices);
66FIBRIL_MUTEX_INITIALIZE(devices_mutex);
67
68/** Functions */
69LIST_INITIALIZE(functions);
70FIBRIL_MUTEX_INITIALIZE(functions_mutex);
71
72/** Interrupts */
73static interrupt_context_list_t interrupt_contexts;
74
75static irq_cmd_t default_cmds[] = {
76 {
77 .cmd = CMD_ACCEPT
78 }
79};
80
81static irq_code_t default_pseudocode = {
82 sizeof(default_cmds) / sizeof(irq_cmd_t),
83 default_cmds
84};
85
86static ddf_dev_t *create_device(void);
87static void delete_device(ddf_dev_t *);
88static void dev_add_ref(ddf_dev_t *);
89static void dev_del_ref(ddf_dev_t *);
90static void fun_add_ref(ddf_fun_t *);
91static void fun_del_ref(ddf_fun_t *);
92static remote_handler_t *function_get_default_handler(ddf_fun_t *);
93static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
94
95static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
96{
97 int id = (int)IPC_GET_IMETHOD(*icall);
98 interrupt_context_t *ctx;
99
100 ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
101 if (ctx != NULL && ctx->handler != NULL)
102 (*ctx->handler)(ctx->dev, iid, icall);
103}
104
105interrupt_context_t *create_interrupt_context(void)
106{
107 interrupt_context_t *ctx;
108
109 ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
110 if (ctx != NULL)
111 memset(ctx, 0, sizeof(interrupt_context_t));
112
113 return ctx;
114}
115
116void delete_interrupt_context(interrupt_context_t *ctx)
117{
118 if (ctx != NULL)
119 free(ctx);
120}
121
122void init_interrupt_context_list(interrupt_context_list_t *list)
123{
124 memset(list, 0, sizeof(interrupt_context_list_t));
125 fibril_mutex_initialize(&list->mutex);
126 list_initialize(&list->contexts);
127}
128
129void
130add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
131{
132 fibril_mutex_lock(&list->mutex);
133 ctx->id = list->curr_id++;
134 list_append(&ctx->link, &list->contexts);
135 fibril_mutex_unlock(&list->mutex);
136}
137
138void remove_interrupt_context(interrupt_context_list_t *list,
139 interrupt_context_t *ctx)
140{
141 fibril_mutex_lock(&list->mutex);
142 list_remove(&ctx->link);
143 fibril_mutex_unlock(&list->mutex);
144}
145
146interrupt_context_t *
147find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
148{
149 interrupt_context_t *ctx;
150
151 fibril_mutex_lock(&list->mutex);
152
153 list_foreach(list->contexts, link) {
154 ctx = list_get_instance(link, interrupt_context_t, link);
155 if (ctx->id == id) {
156 fibril_mutex_unlock(&list->mutex);
157 return ctx;
158 }
159 }
160
161 fibril_mutex_unlock(&list->mutex);
162 return NULL;
163}
164
165interrupt_context_t *
166find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
167{
168 interrupt_context_t *ctx;
169
170 fibril_mutex_lock(&list->mutex);
171
172 list_foreach(list->contexts, link) {
173 ctx = list_get_instance(link, interrupt_context_t, link);
174 if (ctx->irq == irq && ctx->dev == dev) {
175 fibril_mutex_unlock(&list->mutex);
176 return ctx;
177 }
178 }
179
180 fibril_mutex_unlock(&list->mutex);
181 return NULL;
182}
183
184
185int
186register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
187 irq_code_t *pseudocode)
188{
189 interrupt_context_t *ctx = create_interrupt_context();
190
191 ctx->dev = dev;
192 ctx->irq = irq;
193 ctx->handler = handler;
194
195 add_interrupt_context(&interrupt_contexts, ctx);
196
197 if (pseudocode == NULL)
198 pseudocode = &default_pseudocode;
199
200 int res = irq_register(irq, dev->handle, ctx->id, pseudocode);
201 if (res != EOK) {
202 remove_interrupt_context(&interrupt_contexts, ctx);
203 delete_interrupt_context(ctx);
204 }
205
206 return res;
207}
208
209int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
210{
211 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
212 dev, irq);
213 int res = irq_unregister(irq, dev->handle);
214
215 if (ctx != NULL) {
216 remove_interrupt_context(&interrupt_contexts, ctx);
217 delete_interrupt_context(ctx);
218 }
219
220 return res;
221}
222
223static void add_to_functions_list(ddf_fun_t *fun)
224{
225 fibril_mutex_lock(&functions_mutex);
226 list_append(&fun->link, &functions);
227 fibril_mutex_unlock(&functions_mutex);
228}
229
230static void remove_from_functions_list(ddf_fun_t *fun)
231{
232 fibril_mutex_lock(&functions_mutex);
233 list_remove(&fun->link);
234 fibril_mutex_unlock(&functions_mutex);
235}
236
237static ddf_dev_t *driver_get_device(devman_handle_t handle)
238{
239 ddf_dev_t *dev = NULL;
240
241 assert(fibril_mutex_is_locked(&devices_mutex));
242
243 list_foreach(devices, link) {
244 dev = list_get_instance(link, ddf_dev_t, link);
245 if (dev->handle == handle)
246 return dev;
247 }
248
249 return NULL;
250}
251
252static ddf_fun_t *driver_get_function(devman_handle_t handle)
253{
254 ddf_fun_t *fun = NULL;
255
256 assert(fibril_mutex_is_locked(&functions_mutex));
257
258 list_foreach(functions, link) {
259 fun = list_get_instance(link, ddf_fun_t, link);
260 if (fun->handle == handle)
261 return fun;
262 }
263
264 return NULL;
265}
266
267static void driver_dev_add(ipc_callid_t iid, ipc_call_t *icall)
268{
269 char *dev_name = NULL;
270 int res;
271
272 devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
273 devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
274
275 ddf_dev_t *dev = create_device();
276
277 /* Add one reference that will be dropped by driver_dev_remove() */
278 dev_add_ref(dev);
279 dev->handle = dev_handle;
280
281 async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
282 dev->name = dev_name;
283
284 /*
285 * Currently not used, parent fun handle is stored in context
286 * of the connection to the parent device driver.
287 */
288 (void) parent_fun_handle;
289
290 res = driver->driver_ops->dev_add(dev);
291
292 if (res != EOK) {
293 dev_del_ref(dev);
294 async_answer_0(iid, res);
295 return;
296 }
297
298 fibril_mutex_lock(&devices_mutex);
299 list_append(&dev->link, &devices);
300 fibril_mutex_unlock(&devices_mutex);
301
302 async_answer_0(iid, res);
303}
304
305static void driver_dev_added(ipc_callid_t iid, ipc_call_t *icall)
306{
307 fibril_mutex_lock(&devices_mutex);
308 ddf_dev_t *dev = driver_get_device(IPC_GET_ARG1(*icall));
309 fibril_mutex_unlock(&devices_mutex);
310
311 if (dev != NULL && driver->driver_ops->device_added != NULL)
312 driver->driver_ops->device_added(dev);
313}
314
315static void driver_dev_remove(ipc_callid_t iid, ipc_call_t *icall)
316{
317 devman_handle_t devh;
318 ddf_dev_t *dev;
319 int rc;
320
321 devh = IPC_GET_ARG1(*icall);
322
323 fibril_mutex_lock(&devices_mutex);
324 dev = driver_get_device(devh);
325 if (dev != NULL)
326 dev_add_ref(dev);
327 fibril_mutex_unlock(&devices_mutex);
328
329 if (dev == NULL) {
330 async_answer_0(iid, ENOENT);
331 return;
332 }
333
334 if (driver->driver_ops->dev_remove != NULL)
335 rc = driver->driver_ops->dev_remove(dev);
336 else
337 rc = ENOTSUP;
338
339 if (rc == EOK)
340 dev_del_ref(dev);
341
342 async_answer_0(iid, (sysarg_t) rc);
343}
344
345static void driver_dev_gone(ipc_callid_t iid, ipc_call_t *icall)
346{
347 devman_handle_t devh;
348 ddf_dev_t *dev;
349 int rc;
350
351 devh = IPC_GET_ARG1(*icall);
352
353 fibril_mutex_lock(&devices_mutex);
354 dev = driver_get_device(devh);
355 if (dev != NULL)
356 dev_add_ref(dev);
357 fibril_mutex_unlock(&devices_mutex);
358
359 if (dev == NULL) {
360 async_answer_0(iid, ENOENT);
361 return;
362 }
363
364 if (driver->driver_ops->dev_gone != NULL)
365 rc = driver->driver_ops->dev_gone(dev);
366 else
367 rc = ENOTSUP;
368
369 if (rc == EOK)
370 dev_del_ref(dev);
371
372 async_answer_0(iid, (sysarg_t) rc);
373}
374
375static void driver_fun_online(ipc_callid_t iid, ipc_call_t *icall)
376{
377 devman_handle_t funh;
378 ddf_fun_t *fun;
379 int rc;
380
381 funh = IPC_GET_ARG1(*icall);
382
383 /*
384 * Look the function up. Bump reference count so that
385 * the function continues to exist until we return
386 * from the driver.
387 */
388 fibril_mutex_lock(&functions_mutex);
389
390 fun = driver_get_function(funh);
391 if (fun != NULL)
392 fun_add_ref(fun);
393
394 fibril_mutex_unlock(&functions_mutex);
395
396 if (fun == NULL) {
397 async_answer_0(iid, ENOENT);
398 return;
399 }
400
401 /* Call driver entry point */
402 if (driver->driver_ops->fun_online != NULL)
403 rc = driver->driver_ops->fun_online(fun);
404 else
405 rc = ENOTSUP;
406
407 fun_del_ref(fun);
408
409 async_answer_0(iid, (sysarg_t) rc);
410}
411
412static void driver_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
413{
414 devman_handle_t funh;
415 ddf_fun_t *fun;
416 int rc;
417
418 funh = IPC_GET_ARG1(*icall);
419
420 /*
421 * Look the function up. Bump reference count so that
422 * the function continues to exist until we return
423 * from the driver.
424 */
425 fibril_mutex_lock(&functions_mutex);
426
427 fun = driver_get_function(funh);
428 if (fun != NULL)
429 fun_add_ref(fun);
430
431 fibril_mutex_unlock(&functions_mutex);
432
433 if (fun == NULL) {
434 async_answer_0(iid, ENOENT);
435 return;
436 }
437
438 /* Call driver entry point */
439 if (driver->driver_ops->fun_offline != NULL)
440 rc = driver->driver_ops->fun_offline(fun);
441 else
442 rc = ENOTSUP;
443
444 async_answer_0(iid, (sysarg_t) rc);
445}
446
447static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
448{
449 /* Accept connection */
450 async_answer_0(iid, EOK);
451
452 while (true) {
453 ipc_call_t call;
454 ipc_callid_t callid = async_get_call(&call);
455
456 if (!IPC_GET_IMETHOD(call))
457 break;
458
459 switch (IPC_GET_IMETHOD(call)) {
460 case DRIVER_DEV_ADD:
461 driver_dev_add(callid, &call);
462 break;
463 case DRIVER_DEV_ADDED:
464 async_answer_0(callid, EOK);
465 driver_dev_added(callid, &call);
466 break;
467 case DRIVER_DEV_REMOVE:
468 driver_dev_remove(callid, &call);
469 break;
470 case DRIVER_DEV_GONE:
471 driver_dev_gone(callid, &call);
472 break;
473 case DRIVER_FUN_ONLINE:
474 driver_fun_online(callid, &call);
475 break;
476 case DRIVER_FUN_OFFLINE:
477 driver_fun_offline(callid, &call);
478 break;
479 default:
480 async_answer_0(callid, ENOTSUP);
481 }
482 }
483}
484
485/** Generic client connection handler both for applications and drivers.
486 *
487 * @param drv True for driver client, false for other clients
488 * (applications, services, etc.).
489 *
490 */
491static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
492{
493 /*
494 * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
495 * the device to which the client connected.
496 */
497 devman_handle_t handle = IPC_GET_ARG2(*icall);
498
499 fibril_mutex_lock(&functions_mutex);
500 ddf_fun_t *fun = driver_get_function(handle);
501 fibril_mutex_unlock(&functions_mutex);
502 /* XXX Need a lock on fun */
503
504 if (fun == NULL) {
505 printf("%s: driver_connection_gen error - no function with handle"
506 " %" PRIun " was found.\n", driver->name, handle);
507 async_answer_0(iid, ENOENT);
508 return;
509 }
510
511 if (fun->conn_handler != NULL) {
512 /* Driver has a custom connection handler. */
513 (*fun->conn_handler)(iid, icall, (void *)fun);
514 return;
515 }
516
517 /*
518 * TODO - if the client is not a driver, check whether it is allowed to
519 * use the device.
520 */
521
522 int ret = EOK;
523 /* Open device function */
524 if (fun->ops != NULL && fun->ops->open != NULL)
525 ret = (*fun->ops->open)(fun);
526
527 async_answer_0(iid, ret);
528 if (ret != EOK)
529 return;
530
531 while (true) {
532 ipc_callid_t callid;
533 ipc_call_t call;
534 callid = async_get_call(&call);
535 sysarg_t method = IPC_GET_IMETHOD(call);
536
537 if (!method) {
538 /* Close device function */
539 if (fun->ops != NULL && fun->ops->close != NULL)
540 (*fun->ops->close)(fun);
541 async_answer_0(callid, EOK);
542 return;
543 }
544
545 /* Convert ipc interface id to interface index */
546
547 int iface_idx = DEV_IFACE_IDX(method);
548
549 if (!is_valid_iface_idx(iface_idx)) {
550 remote_handler_t *default_handler =
551 function_get_default_handler(fun);
552 if (default_handler != NULL) {
553 (*default_handler)(fun, callid, &call);
554 continue;
555 }
556
557 /*
558 * Function has no such interface and
559 * default handler is not provided.
560 */
561 printf("%s: driver_connection_gen error - "
562 "invalid interface id %d.",
563 driver->name, iface_idx);
564 async_answer_0(callid, ENOTSUP);
565 continue;
566 }
567
568 /* Calling one of the function's interfaces */
569
570 /* Get the interface ops structure. */
571 void *ops = function_get_ops(fun, iface_idx);
572 if (ops == NULL) {
573 printf("%s: driver_connection_gen error - ", driver->name);
574 printf("Function with handle %" PRIun " has no interface "
575 "with id %d.\n", handle, iface_idx);
576 async_answer_0(callid, ENOTSUP);
577 continue;
578 }
579
580 /*
581 * Get the corresponding interface for remote request
582 * handling ("remote interface").
583 */
584 remote_iface_t *rem_iface = get_remote_iface(iface_idx);
585 assert(rem_iface != NULL);
586
587 /* get the method of the remote interface */
588 sysarg_t iface_method_idx = IPC_GET_ARG1(call);
589 remote_iface_func_ptr_t iface_method_ptr =
590 get_remote_method(rem_iface, iface_method_idx);
591 if (iface_method_ptr == NULL) {
592 /* The interface has not such method */
593 printf("%s: driver_connection_gen error - "
594 "invalid interface method.", driver->name);
595 async_answer_0(callid, ENOTSUP);
596 continue;
597 }
598
599 /*
600 * Call the remote interface's method, which will
601 * receive parameters from the remote client and it will
602 * pass it to the corresponding local interface method
603 * associated with the function by its driver.
604 */
605 (*iface_method_ptr)(fun, ops, callid, &call);
606 }
607}
608
609static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
610{
611 driver_connection_gen(iid, icall, true);
612}
613
614static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
615{
616 driver_connection_gen(iid, icall, false);
617}
618
619/** Function for handling connections to device driver. */
620static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
621{
622 sysarg_t conn_type;
623
624 if (iid == 0) {
625 /* Callback connection from devman */
626 /* XXX Use separate handler for this type of connection */
627 conn_type = DRIVER_DEVMAN;
628 } else {
629 conn_type = IPC_GET_ARG1(*icall);
630 }
631
632 /* Select interface */
633 switch (conn_type) {
634 case DRIVER_DEVMAN:
635 /* Handle request from device manager */
636 driver_connection_devman(iid, icall);
637 break;
638 case DRIVER_DRIVER:
639 /* Handle request from drivers of child devices */
640 driver_connection_driver(iid, icall);
641 break;
642 case DRIVER_CLIENT:
643 /* Handle request from client applications */
644 driver_connection_client(iid, icall);
645 break;
646 default:
647 /* No such interface */
648 async_answer_0(iid, ENOENT);
649 }
650}
651
652/** Create new device structure.
653 *
654 * @return The device structure.
655 */
656static ddf_dev_t *create_device(void)
657{
658 ddf_dev_t *dev;
659
660 dev = calloc(1, sizeof(ddf_dev_t));
661 if (dev == NULL)
662 return NULL;
663
664 return dev;
665}
666
667/** Create new function structure.
668 *
669 * @return The device structure.
670 */
671static ddf_fun_t *create_function(void)
672{
673 ddf_fun_t *fun;
674
675 fun = calloc(1, sizeof(ddf_fun_t));
676 if (fun == NULL)
677 return NULL;
678
679 init_match_ids(&fun->match_ids);
680 link_initialize(&fun->link);
681
682 return fun;
683}
684
685/** Delete device structure.
686 *
687 * @param dev The device structure.
688 */
689static void delete_device(ddf_dev_t *dev)
690{
691 if (dev->driver_data != NULL)
692 free(dev->driver_data);
693 free(dev);
694}
695
696/** Delete function structure.
697 *
698 * @param dev The device structure.
699 */
700static void delete_function(ddf_fun_t *fun)
701{
702 clean_match_ids(&fun->match_ids);
703 if (fun->driver_data != NULL)
704 free(fun->driver_data);
705 if (fun->name != NULL)
706 free(fun->name);
707 free(fun);
708}
709
710/** Increase device reference count. */
711static void dev_add_ref(ddf_dev_t *dev)
712{
713 atomic_inc(&dev->refcnt);
714}
715
716/** Decrease device reference count.
717 *
718 * Free the device structure if the reference count drops to zero.
719 */
720static void dev_del_ref(ddf_dev_t *dev)
721{
722 if (atomic_predec(&dev->refcnt) == 0)
723 delete_device(dev);
724}
725
726/** Increase function reference count.
727 *
728 * This also increases reference count on the device. The device structure
729 * will thus not be deallocated while there are some associated function
730 * structures.
731 */
732static void fun_add_ref(ddf_fun_t *fun)
733{
734 dev_add_ref(fun->dev);
735 atomic_inc(&fun->refcnt);
736}
737
738/** Decrease function reference count.
739 *
740 * Free the function structure if the reference count drops to zero.
741 */
742static void fun_del_ref(ddf_fun_t *fun)
743{
744 ddf_dev_t *dev = fun->dev;
745
746 if (atomic_predec(&fun->refcnt) == 0)
747 delete_function(fun);
748
749 dev_del_ref(dev);
750}
751
752/** Allocate driver-specific device data. */
753extern void *ddf_dev_data_alloc(ddf_dev_t *dev, size_t size)
754{
755 void *data;
756
757 assert(dev->driver_data == NULL);
758
759 data = calloc(1, size);
760 if (data == NULL)
761 return NULL;
762
763 dev->driver_data = data;
764 return data;
765}
766
767/** Create a DDF function node.
768 *
769 * Create a DDF function (in memory). Both child devices and external clients
770 * communicate with a device via its functions.
771 *
772 * The created function node is fully formed, but only exists in the memory
773 * of the client task. In order to be visible to the system, the function
774 * must be bound using ddf_fun_bind().
775 *
776 * This function should only fail if there is not enough free memory.
777 * Specifically, this function succeeds even if @a dev already has
778 * a (bound) function with the same name.
779 *
780 * Type: A function of type fun_inner indicates that DDF should attempt
781 * to attach child devices to the function. fun_exposed means that
782 * the function should be exported to external clients (applications).
783 *
784 * @param dev Device to which we are adding function
785 * @param ftype Type of function (fun_inner or fun_exposed)
786 * @param name Name of function
787 *
788 * @return New function or @c NULL if memory is not available
789 */
790ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
791{
792 ddf_fun_t *fun;
793
794 fun = create_function();
795 if (fun == NULL)
796 return NULL;
797
798 /* Add one reference that will be dropped by ddf_fun_destroy() */
799 fun->dev = dev;
800 fun_add_ref(fun);
801
802 fun->bound = false;
803 fun->ftype = ftype;
804
805 fun->name = str_dup(name);
806 if (fun->name == NULL) {
807 delete_function(fun);
808 return NULL;
809 }
810
811 return fun;
812}
813
814/** Allocate driver-specific function data. */
815extern void *ddf_fun_data_alloc(ddf_fun_t *fun, size_t size)
816{
817 void *data;
818
819 assert(fun->bound == false);
820 assert(fun->driver_data == NULL);
821
822 data = calloc(1, size);
823 if (data == NULL)
824 return NULL;
825
826 fun->driver_data = data;
827 return data;
828}
829
830/** Destroy DDF function node.
831 *
832 * Destroy a function previously created with ddf_fun_create(). The function
833 * must not be bound.
834 *
835 * @param fun Function to destroy
836 */
837void ddf_fun_destroy(ddf_fun_t *fun)
838{
839 assert(fun->bound == false);
840
841 /*
842 * Drop the reference added by ddf_fun_create(). This will deallocate
843 * the function as soon as all other references are dropped (i.e.
844 * as soon control leaves all driver entry points called in context
845 * of this function.
846 */
847 fun_del_ref(fun);
848}
849
850static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
851{
852 assert(is_valid_iface_idx(idx));
853 if (fun->ops == NULL)
854 return NULL;
855 return fun->ops->interfaces[idx];
856}
857
858/** Bind a function node.
859 *
860 * Bind the specified function to the system. This effectively makes
861 * the function visible to the system (uploads it to the server).
862 *
863 * This function can fail for several reasons. Specifically,
864 * it will fail if the device already has a bound function of
865 * the same name.
866 *
867 * @param fun Function to bind
868 * @return EOK on success or negative error code
869 */
870int ddf_fun_bind(ddf_fun_t *fun)
871{
872 assert(fun->bound == false);
873 assert(fun->name != NULL);
874
875 int res;
876
877 add_to_functions_list(fun);
878 res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
879 fun->dev->handle, &fun->handle);
880 if (res != EOK) {
881 remove_from_functions_list(fun);
882 return res;
883 }
884
885 fun->bound = true;
886 return res;
887}
888
889/** Unbind a function node.
890 *
891 * Unbind the specified function from the system. This effectively makes
892 * the function invisible to the system.
893 *
894 * @param fun Function to unbind
895 * @return EOK on success or negative error code
896 */
897int ddf_fun_unbind(ddf_fun_t *fun)
898{
899 int res;
900
901 assert(fun->bound == true);
902
903 res = devman_remove_function(fun->handle);
904 if (res != EOK)
905 return res;
906
907 remove_from_functions_list(fun);
908
909 fun->bound = false;
910 return EOK;
911}
912
913/** Online function.
914 *
915 * @param fun Function to online
916 * @return EOK on success or negative error code
917 */
918int ddf_fun_online(ddf_fun_t *fun)
919{
920 int res;
921
922 assert(fun->bound == true);
923
924 res = devman_drv_fun_online(fun->handle);
925 if (res != EOK)
926 return res;
927
928 return EOK;
929}
930
931/** Offline function.
932 *
933 * @param fun Function to offline
934 * @return EOK on success or negative error code
935 */
936int ddf_fun_offline(ddf_fun_t *fun)
937{
938 int res;
939
940 assert(fun->bound == true);
941
942 res = devman_drv_fun_offline(fun->handle);
943 if (res != EOK)
944 return res;
945
946 return EOK;
947}
948
949/** Add single match ID to inner function.
950 *
951 * Construct and add a single match ID to the specified function.
952 * Cannot be called when the function node is bound.
953 *
954 * @param fun Function
955 * @param match_id_str Match string
956 * @param match_score Match score
957 * @return EOK on success, ENOMEM if out of memory.
958 */
959int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
960 int match_score)
961{
962 match_id_t *match_id;
963
964 assert(fun->bound == false);
965 assert(fun->ftype == fun_inner);
966
967 match_id = create_match_id();
968 if (match_id == NULL)
969 return ENOMEM;
970
971 match_id->id = str_dup(match_id_str);
972 match_id->score = match_score;
973
974 add_match_id(&fun->match_ids, match_id);
975 return EOK;
976}
977
978/** Get default handler for client requests */
979static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
980{
981 if (fun->ops == NULL)
982 return NULL;
983 return fun->ops->default_handler;
984}
985
986/** Add exposed function to category.
987 *
988 * Must only be called when the function is bound.
989 */
990int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
991{
992 assert(fun->bound == true);
993 assert(fun->ftype == fun_exposed);
994
995 return devman_add_device_to_category(fun->handle, cat_name);
996}
997
998int ddf_driver_main(driver_t *drv)
999{
1000 int rc;
1001
1002 /*
1003 * Remember the driver structure - driver_ops will be called by generic
1004 * handler for incoming connections.
1005 */
1006 driver = drv;
1007
1008 /* Initialize the list of interrupt contexts. */
1009 init_interrupt_context_list(&interrupt_contexts);
1010
1011 /* Set generic interrupt handler. */
1012 async_set_interrupt_received(driver_irq_handler);
1013
1014 /*
1015 * Register driver with device manager using generic handler for
1016 * incoming connections.
1017 */
1018 rc = devman_driver_register(driver->name, driver_connection);
1019 if (rc != EOK) {
1020 printf("Error: Failed to register driver with device manager "
1021 "(%s).\n", (rc == EEXISTS) ? "driver already started" :
1022 str_error(rc));
1023
1024 return 1;
1025 }
1026
1027 /* Return success from the task since server has started. */
1028 rc = task_retval(0);
1029 if (rc != EOK)
1030 return 1;
1031
1032 async_manager();
1033
1034 /* Never reached. */
1035 return 0;
1036}
1037
1038/**
1039 * @}
1040 */
Note: See TracBrowser for help on using the repository browser.