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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e6a98c5 was 3e6a98c5, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Standards-compliant boolean type.

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