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
RevLine 
[c16cf62]1/*
2 * Copyright (c) 2010 Lenka Trochtova
[83a2f43]3 * Copyright (c) 2011 Jiri Svoboda
[c16cf62]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 */
[52b7b1bb]38
[c16cf62]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>
[3e6a98c5]45#include <stdbool.h>
[c16cf62]46#include <fibril_synch.h>
47#include <stdlib.h>
[c47e1a8]48#include <str.h>
[3ad7b1c]49#include <str_error.h>
[c16cf62]50#include <ctype.h>
[7e752b2]51#include <inttypes.h>
[af6b5157]52#include <devman.h>
[c16cf62]53
[5fdd7c3]54#include "dev_iface.h"
[af6b5157]55#include "ddf/driver.h"
56#include "ddf/interrupt.h"
[56fd7cf]57#include "private/driver.h"
[c16cf62]58
[36f2b3e]59/** Driver structure */
[0c322fa]60static const driver_t *driver;
[7f8b581]61
[36f2b3e]62/** Devices */
[1a5b252]63LIST_INITIALIZE(devices);
64FIBRIL_MUTEX_INITIALIZE(devices_mutex);
65
66/** Functions */
[8b1e15ac]67LIST_INITIALIZE(functions);
68FIBRIL_MUTEX_INITIALIZE(functions_mutex);
[084ff99]69
[83a2f43]70static ddf_dev_t *create_device(void);
71static void delete_device(ddf_dev_t *);
[0f0f8bc]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 *);
[af6b5157]76static remote_handler_t *function_get_default_handler(ddf_fun_t *);
77static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
[7f8b581]78
[83a2f43]79static void add_to_functions_list(ddf_fun_t *fun)
[9a66bc2e]80{
[8b1e15ac]81 fibril_mutex_lock(&functions_mutex);
82 list_append(&fun->link, &functions);
83 fibril_mutex_unlock(&functions_mutex);
[9a66bc2e]84}
85
[83a2f43]86static void remove_from_functions_list(ddf_fun_t *fun)
[9a66bc2e]87{
[8b1e15ac]88 fibril_mutex_lock(&functions_mutex);
89 list_remove(&fun->link);
90 fibril_mutex_unlock(&functions_mutex);
[9a66bc2e]91}
92
[1a5b252]93static ddf_dev_t *driver_get_device(devman_handle_t handle)
94{
95 assert(fibril_mutex_is_locked(&devices_mutex));
96
[feeac0d]97 list_foreach(devices, link, ddf_dev_t, dev) {
[1a5b252]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)
[52b7b1bb]106{
[1a5b252]107 assert(fibril_mutex_is_locked(&functions_mutex));
[8b1e15ac]108
[feeac0d]109 list_foreach(functions, link, ddf_fun_t, fun) {
[1a5b252]110 if (fun->handle == handle)
[8b1e15ac]111 return fun;
[a1769ee]112 }
[36f2b3e]113
[a1769ee]114 return NULL;
115}
116
[0f0f8bc]117static void driver_dev_add(ipc_callid_t iid, ipc_call_t *icall)
[084ff99]118{
[d35ac1d]119 devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
[818fffe]120 devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
[d35ac1d]121
[77ad86c]122 char *dev_name = NULL;
[f9b2cb4c]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 }
[562a48b]128
129 ddf_dev_t *dev = create_device();
[479cc46]130 if (!dev) {
131 free(dev_name);
132 async_answer_0(iid, ENOMEM);
133 return;
134 }
[562a48b]135
136 /* Add one reference that will be dropped by driver_dev_remove() */
137 dev_add_ref(dev);
138 dev->handle = dev_handle;
[df747b9c]139 dev->name = dev_name;
[77ad86c]140
[8b1e15ac]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;
[0d6915f]146
[77ad86c]147 int res = driver->driver_ops->dev_add(dev);
[0f0f8bc]148
149 if (res != EOK) {
150 dev_del_ref(dev);
151 async_answer_0(iid, res);
152 return;
153 }
[df747b9c]154
[1a5b252]155 fibril_mutex_lock(&devices_mutex);
156 list_append(&dev->link, &devices);
157 fibril_mutex_unlock(&devices_mutex);
158
[ffa2c8ef]159 async_answer_0(iid, res);
[084ff99]160}
[c16cf62]161
[1a5b252]162static void driver_dev_remove(ipc_callid_t iid, ipc_call_t *icall)
163{
[77ad86c]164 devman_handle_t devh = IPC_GET_ARG1(*icall);
[1a5b252]165
166 fibril_mutex_lock(&devices_mutex);
[77ad86c]167 ddf_dev_t *dev = driver_get_device(devh);
[f278930]168 if (dev != NULL)
169 dev_add_ref(dev);
[1a5b252]170 fibril_mutex_unlock(&devices_mutex);
171
172 if (dev == NULL) {
173 async_answer_0(iid, ENOENT);
174 return;
175 }
176
[77ad86c]177 int rc;
178
[1a5b252]179 if (driver->driver_ops->dev_remove != NULL)
180 rc = driver->driver_ops->dev_remove(dev);
181 else
182 rc = ENOTSUP;
183
[0f0f8bc]184 if (rc == EOK)
185 dev_del_ref(dev);
186
[1a5b252]187 async_answer_0(iid, (sysarg_t) rc);
188}
189
[80a96d2]190static void driver_dev_gone(ipc_callid_t iid, ipc_call_t *icall)
191{
[77ad86c]192 devman_handle_t devh = IPC_GET_ARG1(*icall);
[80a96d2]193
194 fibril_mutex_lock(&devices_mutex);
[77ad86c]195 ddf_dev_t *dev = driver_get_device(devh);
[80a96d2]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
[77ad86c]205 int rc;
206
[80a96d2]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
[1a5b252]218static void driver_fun_online(ipc_callid_t iid, ipc_call_t *icall)
219{
[77ad86c]220 devman_handle_t funh = IPC_GET_ARG1(*icall);
[0f0f8bc]221
222 /*
[4d94002d]223 * Look the function up. Bump reference count so that
[0f0f8bc]224 * the function continues to exist until we return
225 * from the driver.
226 */
[1a5b252]227 fibril_mutex_lock(&functions_mutex);
[0f0f8bc]228
[77ad86c]229 ddf_fun_t *fun = driver_get_function(funh);
[0f0f8bc]230 if (fun != NULL)
231 fun_add_ref(fun);
232
[1a5b252]233 fibril_mutex_unlock(&functions_mutex);
234
235 if (fun == NULL) {
236 async_answer_0(iid, ENOENT);
237 return;
238 }
239
[0f0f8bc]240 /* Call driver entry point */
[77ad86c]241 int rc;
242
[1a5b252]243 if (driver->driver_ops->fun_online != NULL)
244 rc = driver->driver_ops->fun_online(fun);
245 else
246 rc = ENOTSUP;
247
[0f0f8bc]248 fun_del_ref(fun);
249
[1a5b252]250 async_answer_0(iid, (sysarg_t) rc);
251}
252
253static void driver_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
254{
[77ad86c]255 devman_handle_t funh = IPC_GET_ARG1(*icall);
[0f0f8bc]256
257 /*
[4d94002d]258 * Look the function up. Bump reference count so that
[0f0f8bc]259 * the function continues to exist until we return
260 * from the driver.
261 */
[1a5b252]262 fibril_mutex_lock(&functions_mutex);
[0f0f8bc]263
[77ad86c]264 ddf_fun_t *fun = driver_get_function(funh);
[0f0f8bc]265 if (fun != NULL)
266 fun_add_ref(fun);
267
[1a5b252]268 fibril_mutex_unlock(&functions_mutex);
269
270 if (fun == NULL) {
271 async_answer_0(iid, ENOENT);
272 return;
273 }
274
[0f0f8bc]275 /* Call driver entry point */
[77ad86c]276 int rc;
277
[1a5b252]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
[f9b2cb4c]286static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall,
287 void *arg)
[c16cf62]288{
289 /* Accept connection */
[ffa2c8ef]290 async_answer_0(iid, EOK);
[36f2b3e]291
[79ae36dd]292 while (true) {
[c16cf62]293 ipc_call_t call;
294 ipc_callid_t callid = async_get_call(&call);
[36f2b3e]295
[79ae36dd]296 if (!IPC_GET_IMETHOD(call))
297 break;
298
[228e490]299 switch (IPC_GET_IMETHOD(call)) {
[1a5b252]300 case DRIVER_DEV_ADD:
[0f0f8bc]301 driver_dev_add(callid, &call);
[c16cf62]302 break;
[1a5b252]303 case DRIVER_DEV_REMOVE:
304 driver_dev_remove(callid, &call);
305 break;
[80a96d2]306 case DRIVER_DEV_GONE:
307 driver_dev_gone(callid, &call);
308 break;
[1a5b252]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;
[c16cf62]315 default:
[1a5b252]316 async_answer_0(callid, ENOTSUP);
[c16cf62]317 }
[52b7b1bb]318 }
[c16cf62]319}
320
[79ae36dd]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.).
[52b7b1bb]325 *
[a1769ee]326 */
[9a66bc2e]327static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
[52b7b1bb]328{
[7a252ec8]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 */
[0b5a4131]333 devman_handle_t handle = IPC_GET_ARG2(*icall);
[1a5b252]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 */
[79ae36dd]339
[8b1e15ac]340 if (fun == NULL) {
341 printf("%s: driver_connection_gen error - no function with handle"
[7e752b2]342 " %" PRIun " was found.\n", driver->name, handle);
[ffa2c8ef]343 async_answer_0(iid, ENOENT);
[a1769ee]344 return;
345 }
[5cd136ab]346
[ff65e91]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
[7a252ec8]353 /*
354 * TODO - if the client is not a driver, check whether it is allowed to
355 * use the device.
356 */
[36f2b3e]357
[25a7e11d]358 int ret = EOK;
[8b1e15ac]359 /* Open device function */
360 if (fun->ops != NULL && fun->ops->open != NULL)
361 ret = (*fun->ops->open)(fun);
[a1769ee]362
[ffa2c8ef]363 async_answer_0(iid, ret);
[36f2b3e]364 if (ret != EOK)
[a6e54c5d]365 return;
[36f2b3e]366
[79ae36dd]367 while (true) {
[a1769ee]368 ipc_callid_t callid;
369 ipc_call_t call;
370 callid = async_get_call(&call);
[228e490]371 sysarg_t method = IPC_GET_IMETHOD(call);
[3843ecb]372
[79ae36dd]373 if (!method) {
[8b1e15ac]374 /* Close device function */
375 if (fun->ops != NULL && fun->ops->close != NULL)
376 (*fun->ops->close)(fun);
[ffa2c8ef]377 async_answer_0(callid, EOK);
[a1769ee]378 return;
[79ae36dd]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);
[79a141a]390 continue;
[52b7b1bb]391 }
392
[7a252ec8]393 /*
[79ae36dd]394 * Function has no such interface and
395 * default handler is not provided.
[7a252ec8]396 */
[79ae36dd]397 printf("%s: driver_connection_gen error - "
398 "invalid interface id %d.",
399 driver->name, iface_idx);
400 async_answer_0(callid, ENOTSUP);
[79a141a]401 continue;
[79ae36dd]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);
[79a141a]413 continue;
[79ae36dd]414 }
415
416 /*
417 * Get the corresponding interface for remote request
418 * handling ("remote interface").
419 */
[d0ca4c5]420 const remote_iface_t *rem_iface = get_remote_iface(iface_idx);
[79ae36dd]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);
[79a141a]432 continue;
[a1769ee]433 }
[79ae36dd]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);
[a1769ee]442 }
443}
444
[f9b2cb4c]445static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall,
446 void *arg)
[c16cf62]447{
[52b7b1bb]448 driver_connection_gen(iid, icall, true);
[c16cf62]449}
450
[f9b2cb4c]451static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall,
452 void *arg)
[c16cf62]453{
[52b7b1bb]454 driver_connection_gen(iid, icall, false);
[c16cf62]455}
456
[5fdd7c3]457/** Create new device structure.
458 *
459 * @return The device structure.
460 */
[83a2f43]461static ddf_dev_t *create_device(void)
[5fdd7c3]462{
[83a2f43]463 ddf_dev_t *dev;
[5fdd7c3]464
[0f0f8bc]465 dev = calloc(1, sizeof(ddf_dev_t));
[8b1e15ac]466 if (dev == NULL)
467 return NULL;
[5fdd7c3]468
469 return dev;
470}
471
[8b1e15ac]472/** Create new function structure.
473 *
474 * @return The device structure.
475 */
[83a2f43]476static ddf_fun_t *create_function(void)
[8b1e15ac]477{
[83a2f43]478 ddf_fun_t *fun;
[8b1e15ac]479
[83a2f43]480 fun = calloc(1, sizeof(ddf_fun_t));
[8b1e15ac]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
[5fdd7c3]490/** Delete device structure.
491 *
492 * @param dev The device structure.
493 */
[83a2f43]494static void delete_device(ddf_dev_t *dev)
[5fdd7c3]495{
[56fd7cf]496 if (dev->parent_sess)
497 async_hangup(dev->parent_sess);
[5f6e25e]498 if (dev->driver_data != NULL)
499 free(dev->driver_data);
[669e9b5]500 if (dev->name)
501 free(dev->name);
[5fdd7c3]502 free(dev);
503}
504
[0f0f8bc]505/** Delete function structure.
[8b1e15ac]506 *
507 * @param dev The device structure.
508 */
[83a2f43]509static void delete_function(ddf_fun_t *fun)
[8b1e15ac]510{
511 clean_match_ids(&fun->match_ids);
[5f6e25e]512 if (fun->driver_data != NULL)
513 free(fun->driver_data);
[8b1e15ac]514 if (fun->name != NULL)
515 free(fun->name);
516 free(fun);
517}
518
[0f0f8bc]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
[bfe7867]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 */
[0f0f8bc]541static void fun_add_ref(ddf_fun_t *fun)
542{
[bfe7867]543 dev_add_ref(fun->dev);
[0f0f8bc]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{
[bfe7867]553 ddf_dev_t *dev = fun->dev;
554
[0f0f8bc]555 if (atomic_predec(&fun->refcnt) == 0)
556 delete_function(fun);
[bfe7867]557
558 dev_del_ref(dev);
[0f0f8bc]559}
560
[c5be39b]561/** Allocate driver-specific device data. */
[bf31e3f]562void *ddf_dev_data_alloc(ddf_dev_t *dev, size_t size)
[c5be39b]563{
564 assert(dev->driver_data == NULL);
[77ad86c]565
566 void *data = calloc(1, size);
[c5be39b]567 if (data == NULL)
568 return NULL;
[77ad86c]569
[c5be39b]570 dev->driver_data = data;
571 return data;
572}
573
[56fd7cf]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 *
[f9b2cb4c]600 * @param dev Device
601 *
602 * @return New session or NULL if session could not be created
603 *
[56fd7cf]604 */
[f9b2cb4c]605async_sess_t *ddf_dev_parent_sess_create(ddf_dev_t *dev)
[56fd7cf]606{
607 assert(dev->parent_sess == NULL);
[f9b2cb4c]608 dev->parent_sess = devman_parent_device_connect(dev->handle,
[56fd7cf]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
[97a62fe]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
[56fd7cf]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.
[97a62fe]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)
[56fd7cf]677 * @param name Name of function or NULL
[97a62fe]678 *
679 * @return New function or @c NULL if memory is not available
680 */
[83a2f43]681ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
[97a62fe]682{
[77ad86c]683 ddf_fun_t *fun = create_function();
[97a62fe]684 if (fun == NULL)
685 return NULL;
[77ad86c]686
[0f0f8bc]687 /* Add one reference that will be dropped by ddf_fun_destroy() */
[bfe7867]688 fun->dev = dev;
[0f0f8bc]689 fun_add_ref(fun);
[77ad86c]690
[97a62fe]691 fun->bound = false;
692 fun->ftype = ftype;
[77ad86c]693
[56fd7cf]694 if (name != NULL) {
695 fun->name = str_dup(name);
696 if (fun->name == NULL) {
697 delete_function(fun);
698 return NULL;
699 }
[97a62fe]700 }
[77ad86c]701
[97a62fe]702 return fun;
703}
704
[c5be39b]705/** Allocate driver-specific function data. */
[bf31e3f]706void *ddf_fun_data_alloc(ddf_fun_t *fun, size_t size)
[c5be39b]707{
708 assert(fun->bound == false);
709 assert(fun->driver_data == NULL);
[77ad86c]710
711 void *data = calloc(1, size);
[c5be39b]712 if (data == NULL)
713 return NULL;
[77ad86c]714
[c5be39b]715 fun->driver_data = data;
716 return data;
717}
718
[56fd7cf]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
[97a62fe]735/** Destroy DDF function node.
736 *
737 * Destroy a function previously created with ddf_fun_create(). The function
738 * must not be bound.
739 *
[77ad86c]740 * @param fun Function to destroy
741 *
[97a62fe]742 */
[83a2f43]743void ddf_fun_destroy(ddf_fun_t *fun)
[97a62fe]744{
745 assert(fun->bound == false);
[77ad86c]746
[0f0f8bc]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);
[97a62fe]754}
755
[af6b5157]756static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
[5fdd7c3]757{
758 assert(is_valid_iface_idx(idx));
[8b1e15ac]759 if (fun->ops == NULL)
[5fdd7c3]760 return NULL;
[77ad86c]761
[8b1e15ac]762 return fun->ops->interfaces[idx];
[5fdd7c3]763}
764
[97a62fe]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 *
[77ad86c]774 * @param fun Function to bind
775 *
776 * @return EOK on success or negative error code
777 *
[97a62fe]778 */
[83a2f43]779int ddf_fun_bind(ddf_fun_t *fun)
[7707954]780{
[d0dd7b5]781 assert(fun->bound == false);
[8b1e15ac]782 assert(fun->name != NULL);
[36f2b3e]783
[8b1e15ac]784 add_to_functions_list(fun);
[77ad86c]785 int res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
[97a62fe]786 fun->dev->handle, &fun->handle);
[36f2b3e]787 if (res != EOK) {
[8b1e15ac]788 remove_from_functions_list(fun);
[df747b9c]789 return res;
[36f2b3e]790 }
791
[97a62fe]792 fun->bound = true;
[df747b9c]793 return res;
[7707954]794}
795
[d0dd7b5]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 *
[77ad86c]801 * @param fun Function to unbind
802 *
803 * @return EOK on success or negative error code
804 *
[d0dd7b5]805 */
806int ddf_fun_unbind(ddf_fun_t *fun)
807{
808 assert(fun->bound == true);
809
[77ad86c]810 int res = devman_remove_function(fun->handle);
[d0dd7b5]811 if (res != EOK)
812 return res;
[77ad86c]813
[d0dd7b5]814 remove_from_functions_list(fun);
815
816 fun->bound = false;
817 return EOK;
818}
819
[1a5b252]820/** Online function.
821 *
[77ad86c]822 * @param fun Function to online
823 *
824 * @return EOK on success or negative error code
825 *
[1a5b252]826 */
827int ddf_fun_online(ddf_fun_t *fun)
828{
829 assert(fun->bound == true);
830
[77ad86c]831 int res = devman_drv_fun_online(fun->handle);
[1a5b252]832 if (res != EOK)
833 return res;
834
835 return EOK;
836}
837
838/** Offline function.
839 *
[77ad86c]840 * @param fun Function to offline
841 *
842 * @return EOK on success or negative error code
843 *
[1a5b252]844 */
845int ddf_fun_offline(ddf_fun_t *fun)
846{
847 assert(fun->bound == true);
848
[77ad86c]849 int res = devman_drv_fun_offline(fun->handle);
[1a5b252]850 if (res != EOK)
851 return res;
852
853 return EOK;
854}
855
[34588a80]856/** Add single match ID to inner function.
[cd0684d]857 *
858 * Construct and add a single match ID to the specified function.
[34588a80]859 * Cannot be called when the function node is bound.
[cd0684d]860 *
[77ad86c]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 *
[cd0684d]868 */
[83a2f43]869int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
[cd0684d]870 int match_score)
871{
[34588a80]872 assert(fun->bound == false);
873 assert(fun->ftype == fun_inner);
874
[77ad86c]875 match_id_t *match_id = create_match_id();
[cd0684d]876 if (match_id == NULL)
877 return ENOMEM;
878
[ef9460b]879 match_id->id = str_dup(match_id_str);
[8a363ab3]880 match_id->score = match_score;
[cd0684d]881
882 add_match_id(&fun->match_ids, match_id);
883 return EOK;
884}
885
[56fd7cf]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 */
[b688fd8]897void ddf_fun_set_conn_handler(ddf_fun_t *fun, async_port_handler_t conn)
[56fd7cf]898{
899 assert(fun->ops == NULL);
900 fun->conn_handler = conn;
901}
902
[5fdd7c3]903/** Get default handler for client requests */
[af6b5157]904static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
[5fdd7c3]905{
[8b1e15ac]906 if (fun->ops == NULL)
[5fdd7c3]907 return NULL;
[8b1e15ac]908 return fun->ops->default_handler;
[5fdd7c3]909}
910
[1dc4a5e]911/** Add exposed function to category.
[34588a80]912 *
913 * Must only be called when the function is bound.
[77ad86c]914 *
[34588a80]915 */
[1dc4a5e]916int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
[5fdd7c3]917{
[34588a80]918 assert(fun->bound == true);
919 assert(fun->ftype == fun_exposed);
920
[1dc4a5e]921 return devman_add_device_to_category(fun->handle, cat_name);
[5fdd7c3]922}
923
[0c322fa]924int ddf_driver_main(const driver_t *drv)
[c16cf62]925{
[7a252ec8]926 /*
927 * Remember the driver structure - driver_ops will be called by generic
928 * handler for incoming connections.
929 */
[c16cf62]930 driver = drv;
[36f2b3e]931
[7a252ec8]932 /*
[033cbf82]933 * Register driver with device manager using generic handler for
934 * incoming connections.
[7a252ec8]935 */
[f9b2cb4c]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);
[033cbf82]950 if (rc != EOK) {
[3ad7b1c]951 printf("Error: Failed to register driver with device manager "
[8a637a4]952 "(%s).\n", (rc == EEXIST) ? "driver already started" :
[3ad7b1c]953 str_error(rc));
954
[77ad86c]955 return rc;
[033cbf82]956 }
[36f2b3e]957
[26fa82bc]958 /* Return success from the task since server has started. */
959 rc = task_retval(0);
960 if (rc != EOK)
[77ad86c]961 return rc;
962
[c16cf62]963 async_manager();
[36f2b3e]964
[7a252ec8]965 /* Never reached. */
[77ad86c]966 return EOK;
[c16cf62]967}
968
969/**
970 * @}
[52b7b1bb]971 */
Note: See TracBrowser for help on using the repository browser.