source: mainline/uspace/lib/drv/generic/driver.c@ 77a69ea

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 77a69ea was 77a69ea, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Allow multiple tasks to register for loc category change events.
Open NIC devices via location service, discover using category change events.
Eliminate device_added driver entry point.
loc_service_get_name() should return fully qualified service name.

  • Property mode set to 100644
File size: 23.2 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_remove(ipc_callid_t iid, ipc_call_t *icall)
306{
307 devman_handle_t devh;
308 ddf_dev_t *dev;
309 int rc;
310
311 devh = IPC_GET_ARG1(*icall);
312
313 fibril_mutex_lock(&devices_mutex);
314 dev = driver_get_device(devh);
315 if (dev != NULL)
316 dev_add_ref(dev);
317 fibril_mutex_unlock(&devices_mutex);
318
319 if (dev == NULL) {
320 async_answer_0(iid, ENOENT);
321 return;
322 }
323
324 if (driver->driver_ops->dev_remove != NULL)
325 rc = driver->driver_ops->dev_remove(dev);
326 else
327 rc = ENOTSUP;
328
329 if (rc == EOK)
330 dev_del_ref(dev);
331
332 async_answer_0(iid, (sysarg_t) rc);
333}
334
335static void driver_dev_gone(ipc_callid_t iid, ipc_call_t *icall)
336{
337 devman_handle_t devh;
338 ddf_dev_t *dev;
339 int rc;
340
341 devh = IPC_GET_ARG1(*icall);
342
343 fibril_mutex_lock(&devices_mutex);
344 dev = driver_get_device(devh);
345 if (dev != NULL)
346 dev_add_ref(dev);
347 fibril_mutex_unlock(&devices_mutex);
348
349 if (dev == NULL) {
350 async_answer_0(iid, ENOENT);
351 return;
352 }
353
354 if (driver->driver_ops->dev_gone != NULL)
355 rc = driver->driver_ops->dev_gone(dev);
356 else
357 rc = ENOTSUP;
358
359 if (rc == EOK)
360 dev_del_ref(dev);
361
362 async_answer_0(iid, (sysarg_t) rc);
363}
364
365static void driver_fun_online(ipc_callid_t iid, ipc_call_t *icall)
366{
367 devman_handle_t funh;
368 ddf_fun_t *fun;
369 int rc;
370
371 funh = IPC_GET_ARG1(*icall);
372
373 /*
374 * Look the function up. Bump reference count so that
375 * the function continues to exist until we return
376 * from the driver.
377 */
378 fibril_mutex_lock(&functions_mutex);
379
380 fun = driver_get_function(funh);
381 if (fun != NULL)
382 fun_add_ref(fun);
383
384 fibril_mutex_unlock(&functions_mutex);
385
386 if (fun == NULL) {
387 async_answer_0(iid, ENOENT);
388 return;
389 }
390
391 /* Call driver entry point */
392 if (driver->driver_ops->fun_online != NULL)
393 rc = driver->driver_ops->fun_online(fun);
394 else
395 rc = ENOTSUP;
396
397 fun_del_ref(fun);
398
399 async_answer_0(iid, (sysarg_t) rc);
400}
401
402static void driver_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
403{
404 devman_handle_t funh;
405 ddf_fun_t *fun;
406 int rc;
407
408 funh = IPC_GET_ARG1(*icall);
409
410 /*
411 * Look the function up. Bump reference count so that
412 * the function continues to exist until we return
413 * from the driver.
414 */
415 fibril_mutex_lock(&functions_mutex);
416
417 fun = driver_get_function(funh);
418 if (fun != NULL)
419 fun_add_ref(fun);
420
421 fibril_mutex_unlock(&functions_mutex);
422
423 if (fun == NULL) {
424 async_answer_0(iid, ENOENT);
425 return;
426 }
427
428 /* Call driver entry point */
429 if (driver->driver_ops->fun_offline != NULL)
430 rc = driver->driver_ops->fun_offline(fun);
431 else
432 rc = ENOTSUP;
433
434 async_answer_0(iid, (sysarg_t) rc);
435}
436
437static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
438{
439 /* Accept connection */
440 async_answer_0(iid, EOK);
441
442 while (true) {
443 ipc_call_t call;
444 ipc_callid_t callid = async_get_call(&call);
445
446 if (!IPC_GET_IMETHOD(call))
447 break;
448
449 switch (IPC_GET_IMETHOD(call)) {
450 case DRIVER_DEV_ADD:
451 driver_dev_add(callid, &call);
452 break;
453 case DRIVER_DEV_REMOVE:
454 driver_dev_remove(callid, &call);
455 break;
456 case DRIVER_DEV_GONE:
457 driver_dev_gone(callid, &call);
458 break;
459 case DRIVER_FUN_ONLINE:
460 driver_fun_online(callid, &call);
461 break;
462 case DRIVER_FUN_OFFLINE:
463 driver_fun_offline(callid, &call);
464 break;
465 default:
466 async_answer_0(callid, ENOTSUP);
467 }
468 }
469}
470
471/** Generic client connection handler both for applications and drivers.
472 *
473 * @param drv True for driver client, false for other clients
474 * (applications, services, etc.).
475 *
476 */
477static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
478{
479 /*
480 * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
481 * the device to which the client connected.
482 */
483 devman_handle_t handle = IPC_GET_ARG2(*icall);
484
485 fibril_mutex_lock(&functions_mutex);
486 ddf_fun_t *fun = driver_get_function(handle);
487 fibril_mutex_unlock(&functions_mutex);
488 /* XXX Need a lock on fun */
489
490 if (fun == NULL) {
491 printf("%s: driver_connection_gen error - no function with handle"
492 " %" PRIun " was found.\n", driver->name, handle);
493 async_answer_0(iid, ENOENT);
494 return;
495 }
496
497 if (fun->conn_handler != NULL) {
498 /* Driver has a custom connection handler. */
499 (*fun->conn_handler)(iid, icall, (void *)fun);
500 return;
501 }
502
503 /*
504 * TODO - if the client is not a driver, check whether it is allowed to
505 * use the device.
506 */
507
508 int ret = EOK;
509 /* Open device function */
510 if (fun->ops != NULL && fun->ops->open != NULL)
511 ret = (*fun->ops->open)(fun);
512
513 async_answer_0(iid, ret);
514 if (ret != EOK)
515 return;
516
517 while (true) {
518 ipc_callid_t callid;
519 ipc_call_t call;
520 callid = async_get_call(&call);
521 sysarg_t method = IPC_GET_IMETHOD(call);
522
523 if (!method) {
524 /* Close device function */
525 if (fun->ops != NULL && fun->ops->close != NULL)
526 (*fun->ops->close)(fun);
527 async_answer_0(callid, EOK);
528 return;
529 }
530
531 /* Convert ipc interface id to interface index */
532
533 int iface_idx = DEV_IFACE_IDX(method);
534
535 if (!is_valid_iface_idx(iface_idx)) {
536 remote_handler_t *default_handler =
537 function_get_default_handler(fun);
538 if (default_handler != NULL) {
539 (*default_handler)(fun, callid, &call);
540 continue;
541 }
542
543 /*
544 * Function has no such interface and
545 * default handler is not provided.
546 */
547 printf("%s: driver_connection_gen error - "
548 "invalid interface id %d.",
549 driver->name, iface_idx);
550 async_answer_0(callid, ENOTSUP);
551 continue;
552 }
553
554 /* Calling one of the function's interfaces */
555
556 /* Get the interface ops structure. */
557 void *ops = function_get_ops(fun, iface_idx);
558 if (ops == NULL) {
559 printf("%s: driver_connection_gen error - ", driver->name);
560 printf("Function with handle %" PRIun " has no interface "
561 "with id %d.\n", handle, iface_idx);
562 async_answer_0(callid, ENOTSUP);
563 continue;
564 }
565
566 /*
567 * Get the corresponding interface for remote request
568 * handling ("remote interface").
569 */
570 remote_iface_t *rem_iface = get_remote_iface(iface_idx);
571 assert(rem_iface != NULL);
572
573 /* get the method of the remote interface */
574 sysarg_t iface_method_idx = IPC_GET_ARG1(call);
575 remote_iface_func_ptr_t iface_method_ptr =
576 get_remote_method(rem_iface, iface_method_idx);
577 if (iface_method_ptr == NULL) {
578 /* The interface has not such method */
579 printf("%s: driver_connection_gen error - "
580 "invalid interface method.", driver->name);
581 async_answer_0(callid, ENOTSUP);
582 continue;
583 }
584
585 /*
586 * Call the remote interface's method, which will
587 * receive parameters from the remote client and it will
588 * pass it to the corresponding local interface method
589 * associated with the function by its driver.
590 */
591 (*iface_method_ptr)(fun, ops, callid, &call);
592 }
593}
594
595static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
596{
597 driver_connection_gen(iid, icall, true);
598}
599
600static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
601{
602 driver_connection_gen(iid, icall, false);
603}
604
605/** Function for handling connections to device driver. */
606static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
607{
608 sysarg_t conn_type;
609
610 if (iid == 0) {
611 /* Callback connection from devman */
612 /* XXX Use separate handler for this type of connection */
613 conn_type = DRIVER_DEVMAN;
614 } else {
615 conn_type = IPC_GET_ARG1(*icall);
616 }
617
618 /* Select interface */
619 switch (conn_type) {
620 case DRIVER_DEVMAN:
621 /* Handle request from device manager */
622 driver_connection_devman(iid, icall);
623 break;
624 case DRIVER_DRIVER:
625 /* Handle request from drivers of child devices */
626 driver_connection_driver(iid, icall);
627 break;
628 case DRIVER_CLIENT:
629 /* Handle request from client applications */
630 driver_connection_client(iid, icall);
631 break;
632 default:
633 /* No such interface */
634 async_answer_0(iid, ENOENT);
635 }
636}
637
638/** Create new device structure.
639 *
640 * @return The device structure.
641 */
642static ddf_dev_t *create_device(void)
643{
644 ddf_dev_t *dev;
645
646 dev = calloc(1, sizeof(ddf_dev_t));
647 if (dev == NULL)
648 return NULL;
649
650 return dev;
651}
652
653/** Create new function structure.
654 *
655 * @return The device structure.
656 */
657static ddf_fun_t *create_function(void)
658{
659 ddf_fun_t *fun;
660
661 fun = calloc(1, sizeof(ddf_fun_t));
662 if (fun == NULL)
663 return NULL;
664
665 init_match_ids(&fun->match_ids);
666 link_initialize(&fun->link);
667
668 return fun;
669}
670
671/** Delete device structure.
672 *
673 * @param dev The device structure.
674 */
675static void delete_device(ddf_dev_t *dev)
676{
677 if (dev->driver_data != NULL)
678 free(dev->driver_data);
679 free(dev);
680}
681
682/** Delete function structure.
683 *
684 * @param dev The device structure.
685 */
686static void delete_function(ddf_fun_t *fun)
687{
688 clean_match_ids(&fun->match_ids);
689 if (fun->driver_data != NULL)
690 free(fun->driver_data);
691 if (fun->name != NULL)
692 free(fun->name);
693 free(fun);
694}
695
696/** Increase device reference count. */
697static void dev_add_ref(ddf_dev_t *dev)
698{
699 atomic_inc(&dev->refcnt);
700}
701
702/** Decrease device reference count.
703 *
704 * Free the device structure if the reference count drops to zero.
705 */
706static void dev_del_ref(ddf_dev_t *dev)
707{
708 if (atomic_predec(&dev->refcnt) == 0)
709 delete_device(dev);
710}
711
712/** Increase function reference count.
713 *
714 * This also increases reference count on the device. The device structure
715 * will thus not be deallocated while there are some associated function
716 * structures.
717 */
718static void fun_add_ref(ddf_fun_t *fun)
719{
720 dev_add_ref(fun->dev);
721 atomic_inc(&fun->refcnt);
722}
723
724/** Decrease function reference count.
725 *
726 * Free the function structure if the reference count drops to zero.
727 */
728static void fun_del_ref(ddf_fun_t *fun)
729{
730 ddf_dev_t *dev = fun->dev;
731
732 if (atomic_predec(&fun->refcnt) == 0)
733 delete_function(fun);
734
735 dev_del_ref(dev);
736}
737
738/** Allocate driver-specific device data. */
739extern void *ddf_dev_data_alloc(ddf_dev_t *dev, size_t size)
740{
741 void *data;
742
743 assert(dev->driver_data == NULL);
744
745 data = calloc(1, size);
746 if (data == NULL)
747 return NULL;
748
749 dev->driver_data = data;
750 return data;
751}
752
753/** Create a DDF function node.
754 *
755 * Create a DDF function (in memory). Both child devices and external clients
756 * communicate with a device via its functions.
757 *
758 * The created function node is fully formed, but only exists in the memory
759 * of the client task. In order to be visible to the system, the function
760 * must be bound using ddf_fun_bind().
761 *
762 * This function should only fail if there is not enough free memory.
763 * Specifically, this function succeeds even if @a dev already has
764 * a (bound) function with the same name.
765 *
766 * Type: A function of type fun_inner indicates that DDF should attempt
767 * to attach child devices to the function. fun_exposed means that
768 * the function should be exported to external clients (applications).
769 *
770 * @param dev Device to which we are adding function
771 * @param ftype Type of function (fun_inner or fun_exposed)
772 * @param name Name of function
773 *
774 * @return New function or @c NULL if memory is not available
775 */
776ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
777{
778 ddf_fun_t *fun;
779
780 fun = create_function();
781 if (fun == NULL)
782 return NULL;
783
784 /* Add one reference that will be dropped by ddf_fun_destroy() */
785 fun->dev = dev;
786 fun_add_ref(fun);
787
788 fun->bound = false;
789 fun->ftype = ftype;
790
791 fun->name = str_dup(name);
792 if (fun->name == NULL) {
793 delete_function(fun);
794 return NULL;
795 }
796
797 return fun;
798}
799
800/** Allocate driver-specific function data. */
801extern void *ddf_fun_data_alloc(ddf_fun_t *fun, size_t size)
802{
803 void *data;
804
805 assert(fun->bound == false);
806 assert(fun->driver_data == NULL);
807
808 data = calloc(1, size);
809 if (data == NULL)
810 return NULL;
811
812 fun->driver_data = data;
813 return data;
814}
815
816/** Destroy DDF function node.
817 *
818 * Destroy a function previously created with ddf_fun_create(). The function
819 * must not be bound.
820 *
821 * @param fun Function to destroy
822 */
823void ddf_fun_destroy(ddf_fun_t *fun)
824{
825 assert(fun->bound == false);
826
827 /*
828 * Drop the reference added by ddf_fun_create(). This will deallocate
829 * the function as soon as all other references are dropped (i.e.
830 * as soon control leaves all driver entry points called in context
831 * of this function.
832 */
833 fun_del_ref(fun);
834}
835
836static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
837{
838 assert(is_valid_iface_idx(idx));
839 if (fun->ops == NULL)
840 return NULL;
841 return fun->ops->interfaces[idx];
842}
843
844/** Bind a function node.
845 *
846 * Bind the specified function to the system. This effectively makes
847 * the function visible to the system (uploads it to the server).
848 *
849 * This function can fail for several reasons. Specifically,
850 * it will fail if the device already has a bound function of
851 * the same name.
852 *
853 * @param fun Function to bind
854 * @return EOK on success or negative error code
855 */
856int ddf_fun_bind(ddf_fun_t *fun)
857{
858 assert(fun->bound == false);
859 assert(fun->name != NULL);
860
861 int res;
862
863 add_to_functions_list(fun);
864 res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
865 fun->dev->handle, &fun->handle);
866 if (res != EOK) {
867 remove_from_functions_list(fun);
868 return res;
869 }
870
871 fun->bound = true;
872 return res;
873}
874
875/** Unbind a function node.
876 *
877 * Unbind the specified function from the system. This effectively makes
878 * the function invisible to the system.
879 *
880 * @param fun Function to unbind
881 * @return EOK on success or negative error code
882 */
883int ddf_fun_unbind(ddf_fun_t *fun)
884{
885 int res;
886
887 assert(fun->bound == true);
888
889 res = devman_remove_function(fun->handle);
890 if (res != EOK)
891 return res;
892
893 remove_from_functions_list(fun);
894
895 fun->bound = false;
896 return EOK;
897}
898
899/** Online function.
900 *
901 * @param fun Function to online
902 * @return EOK on success or negative error code
903 */
904int ddf_fun_online(ddf_fun_t *fun)
905{
906 int res;
907
908 assert(fun->bound == true);
909
910 res = devman_drv_fun_online(fun->handle);
911 if (res != EOK)
912 return res;
913
914 return EOK;
915}
916
917/** Offline function.
918 *
919 * @param fun Function to offline
920 * @return EOK on success or negative error code
921 */
922int ddf_fun_offline(ddf_fun_t *fun)
923{
924 int res;
925
926 assert(fun->bound == true);
927
928 res = devman_drv_fun_offline(fun->handle);
929 if (res != EOK)
930 return res;
931
932 return EOK;
933}
934
935/** Add single match ID to inner function.
936 *
937 * Construct and add a single match ID to the specified function.
938 * Cannot be called when the function node is bound.
939 *
940 * @param fun Function
941 * @param match_id_str Match string
942 * @param match_score Match score
943 * @return EOK on success, ENOMEM if out of memory.
944 */
945int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
946 int match_score)
947{
948 match_id_t *match_id;
949
950 assert(fun->bound == false);
951 assert(fun->ftype == fun_inner);
952
953 match_id = create_match_id();
954 if (match_id == NULL)
955 return ENOMEM;
956
957 match_id->id = str_dup(match_id_str);
958 match_id->score = match_score;
959
960 add_match_id(&fun->match_ids, match_id);
961 return EOK;
962}
963
964/** Get default handler for client requests */
965static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
966{
967 if (fun->ops == NULL)
968 return NULL;
969 return fun->ops->default_handler;
970}
971
972/** Add exposed function to category.
973 *
974 * Must only be called when the function is bound.
975 */
976int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
977{
978 assert(fun->bound == true);
979 assert(fun->ftype == fun_exposed);
980
981 return devman_add_device_to_category(fun->handle, cat_name);
982}
983
984int ddf_driver_main(driver_t *drv)
985{
986 int rc;
987
988 /*
989 * Remember the driver structure - driver_ops will be called by generic
990 * handler for incoming connections.
991 */
992 driver = drv;
993
994 /* Initialize the list of interrupt contexts. */
995 init_interrupt_context_list(&interrupt_contexts);
996
997 /* Set generic interrupt handler. */
998 async_set_interrupt_received(driver_irq_handler);
999
1000 /*
1001 * Register driver with device manager using generic handler for
1002 * incoming connections.
1003 */
1004 async_set_client_connection(driver_connection);
1005 rc = devman_driver_register(driver->name);
1006 if (rc != EOK) {
1007 printf("Error: Failed to register driver with device manager "
1008 "(%s).\n", (rc == EEXISTS) ? "driver already started" :
1009 str_error(rc));
1010
1011 return 1;
1012 }
1013
1014 /* Return success from the task since server has started. */
1015 rc = task_retval(0);
1016 if (rc != EOK)
1017 return 1;
1018
1019 async_manager();
1020
1021 /* Never reached. */
1022 return 0;
1023}
1024
1025/**
1026 * @}
1027 */
Note: See TracBrowser for help on using the repository browser.