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

Last change on this file since 5c38838 was 4122410, checked in by Jakub Jermar <jakub@…>, 7 years ago

Improve Doxygen documentaion

This is stil WiP. A number of libraries, drivers and services were
converted to using a more hierarchical and decentralized scheme when it
comes to specifying to which doxygen group they belong.

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