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

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

make pointer to iface array const.

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