source: mainline/uspace/lib/drv/generic/driver.c@ 0c322fa

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0c322fa was 0c322fa, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libdrv: Use const pointer to driver structure.

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