source: mainline/uspace/lib/drv/generic/driver.c@ 1558d85

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1558d85 was 1558d85, checked in by Jakub Jermar <jakub@…>, 9 years ago

Remove duplicate includes

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