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

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

code review and cstyle cleanup (no change in functionality)

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