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

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

support for kernel notification multiplexing in the async framework

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