source: mainline/uspace/lib/drv/generic/driver.c@ 7b616e2

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

Name service should communicate using async.h.

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