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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5337491 was 77ad86c, checked in by Martin Decky <martin@…>, 13 years ago

cstyle (no change in functionality)

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