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

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

use systematic names

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