[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] | 62 | static driver_t *driver;
|
---|
[7f8b581] | 63 |
|
---|
[36f2b3e] | 64 | /** Devices */
|
---|
[8b1e15ac] | 65 | LIST_INITIALIZE(functions);
|
---|
| 66 | FIBRIL_MUTEX_INITIALIZE(functions_mutex);
|
---|
[084ff99] | 67 |
|
---|
[36f2b3e] | 68 | /** Interrupts */
|
---|
[7f8b581] | 69 | static interrupt_context_list_t interrupt_contexts;
|
---|
| 70 |
|
---|
| 71 | static irq_cmd_t default_cmds[] = {
|
---|
| 72 | {
|
---|
| 73 | .cmd = CMD_ACCEPT
|
---|
| 74 | }
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | static irq_code_t default_pseudocode = {
|
---|
| 78 | sizeof(default_cmds) / sizeof(irq_cmd_t),
|
---|
| 79 | default_cmds
|
---|
| 80 | };
|
---|
| 81 |
|
---|
[83a2f43] | 82 | static ddf_dev_t *create_device(void);
|
---|
| 83 | static void delete_device(ddf_dev_t *);
|
---|
[af6b5157] | 84 | static remote_handler_t *function_get_default_handler(ddf_fun_t *);
|
---|
| 85 | static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
|
---|
[7f8b581] | 86 |
|
---|
| 87 | static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[7a252ec8] | 88 | {
|
---|
[228e490] | 89 | int id = (int)IPC_GET_IMETHOD(*icall);
|
---|
[7a252ec8] | 90 | interrupt_context_t *ctx;
|
---|
| 91 |
|
---|
| 92 | ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
|
---|
[36f2b3e] | 93 | if (ctx != NULL && ctx->handler != NULL)
|
---|
[7a252ec8] | 94 | (*ctx->handler)(ctx->dev, iid, icall);
|
---|
[7f8b581] | 95 | }
|
---|
| 96 |
|
---|
[5fdd7c3] | 97 | interrupt_context_t *create_interrupt_context(void)
|
---|
| 98 | {
|
---|
| 99 | interrupt_context_t *ctx;
|
---|
| 100 |
|
---|
| 101 | ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
|
---|
| 102 | if (ctx != NULL)
|
---|
| 103 | memset(ctx, 0, sizeof(interrupt_context_t));
|
---|
| 104 |
|
---|
| 105 | return ctx;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void delete_interrupt_context(interrupt_context_t *ctx)
|
---|
| 109 | {
|
---|
| 110 | if (ctx != NULL)
|
---|
| 111 | free(ctx);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | void init_interrupt_context_list(interrupt_context_list_t *list)
|
---|
| 115 | {
|
---|
| 116 | memset(list, 0, sizeof(interrupt_context_list_t));
|
---|
| 117 | fibril_mutex_initialize(&list->mutex);
|
---|
| 118 | list_initialize(&list->contexts);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void
|
---|
| 122 | add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
|
---|
| 123 | {
|
---|
| 124 | fibril_mutex_lock(&list->mutex);
|
---|
| 125 | ctx->id = list->curr_id++;
|
---|
| 126 | list_append(&ctx->link, &list->contexts);
|
---|
| 127 | fibril_mutex_unlock(&list->mutex);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | void remove_interrupt_context(interrupt_context_list_t *list,
|
---|
| 131 | interrupt_context_t *ctx)
|
---|
| 132 | {
|
---|
| 133 | fibril_mutex_lock(&list->mutex);
|
---|
| 134 | list_remove(&ctx->link);
|
---|
| 135 | fibril_mutex_unlock(&list->mutex);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | interrupt_context_t *
|
---|
| 139 | find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
|
---|
| 140 | {
|
---|
| 141 | interrupt_context_t *ctx;
|
---|
| 142 |
|
---|
[b72efe8] | 143 | fibril_mutex_lock(&list->mutex);
|
---|
| 144 |
|
---|
| 145 | list_foreach(list->contexts, link) {
|
---|
[5fdd7c3] | 146 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
| 147 | if (ctx->id == id) {
|
---|
| 148 | fibril_mutex_unlock(&list->mutex);
|
---|
| 149 | return ctx;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | fibril_mutex_unlock(&list->mutex);
|
---|
| 154 | return NULL;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | interrupt_context_t *
|
---|
[83a2f43] | 158 | find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
|
---|
[5fdd7c3] | 159 | {
|
---|
| 160 | interrupt_context_t *ctx;
|
---|
| 161 |
|
---|
[b72efe8] | 162 | fibril_mutex_lock(&list->mutex);
|
---|
| 163 |
|
---|
| 164 | list_foreach(list->contexts, link) {
|
---|
[5fdd7c3] | 165 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
| 166 | if (ctx->irq == irq && ctx->dev == dev) {
|
---|
| 167 | fibril_mutex_unlock(&list->mutex);
|
---|
| 168 | return ctx;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | fibril_mutex_unlock(&list->mutex);
|
---|
| 173 | return NULL;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 |
|
---|
[7a252ec8] | 177 | int
|
---|
[83a2f43] | 178 | register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
|
---|
[7a252ec8] | 179 | irq_code_t *pseudocode)
|
---|
[7f8b581] | 180 | {
|
---|
| 181 | interrupt_context_t *ctx = create_interrupt_context();
|
---|
| 182 |
|
---|
| 183 | ctx->dev = dev;
|
---|
| 184 | ctx->irq = irq;
|
---|
| 185 | ctx->handler = handler;
|
---|
| 186 |
|
---|
| 187 | add_interrupt_context(&interrupt_contexts, ctx);
|
---|
| 188 |
|
---|
[36f2b3e] | 189 | if (pseudocode == NULL)
|
---|
[7f8b581] | 190 | pseudocode = &default_pseudocode;
|
---|
| 191 |
|
---|
[ffa2c8ef] | 192 | int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
|
---|
[36f2b3e] | 193 | if (res != EOK) {
|
---|
[7f8b581] | 194 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
| 195 | delete_interrupt_context(ctx);
|
---|
| 196 | }
|
---|
[7a252ec8] | 197 |
|
---|
| 198 | return res;
|
---|
[7f8b581] | 199 | }
|
---|
| 200 |
|
---|
[83a2f43] | 201 | int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
|
---|
[7f8b581] | 202 | {
|
---|
[7a252ec8] | 203 | interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
|
---|
| 204 | dev, irq);
|
---|
[ffa2c8ef] | 205 | int res = unregister_irq(irq, dev->handle);
|
---|
[36f2b3e] | 206 |
|
---|
| 207 | if (ctx != NULL) {
|
---|
[7f8b581] | 208 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
[7a252ec8] | 209 | delete_interrupt_context(ctx);
|
---|
[7f8b581] | 210 | }
|
---|
[36f2b3e] | 211 |
|
---|
[7f8b581] | 212 | return res;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[83a2f43] | 215 | static void add_to_functions_list(ddf_fun_t *fun)
|
---|
[9a66bc2e] | 216 | {
|
---|
[8b1e15ac] | 217 | fibril_mutex_lock(&functions_mutex);
|
---|
| 218 | list_append(&fun->link, &functions);
|
---|
| 219 | fibril_mutex_unlock(&functions_mutex);
|
---|
[9a66bc2e] | 220 | }
|
---|
| 221 |
|
---|
[83a2f43] | 222 | static void remove_from_functions_list(ddf_fun_t *fun)
|
---|
[9a66bc2e] | 223 | {
|
---|
[8b1e15ac] | 224 | fibril_mutex_lock(&functions_mutex);
|
---|
| 225 | list_remove(&fun->link);
|
---|
| 226 | fibril_mutex_unlock(&functions_mutex);
|
---|
[9a66bc2e] | 227 | }
|
---|
| 228 |
|
---|
[b72efe8] | 229 | static ddf_fun_t *driver_get_function(list_t *functions, devman_handle_t handle)
|
---|
[52b7b1bb] | 230 | {
|
---|
[83a2f43] | 231 | ddf_fun_t *fun = NULL;
|
---|
[8b1e15ac] | 232 |
|
---|
| 233 | fibril_mutex_lock(&functions_mutex);
|
---|
| 234 |
|
---|
[b72efe8] | 235 | list_foreach(*functions, link) {
|
---|
[83a2f43] | 236 | fun = list_get_instance(link, ddf_fun_t, link);
|
---|
[8b1e15ac] | 237 | if (fun->handle == handle) {
|
---|
| 238 | fibril_mutex_unlock(&functions_mutex);
|
---|
| 239 | return fun;
|
---|
[a1769ee] | 240 | }
|
---|
| 241 | }
|
---|
[36f2b3e] | 242 |
|
---|
[8b1e15ac] | 243 | fibril_mutex_unlock(&functions_mutex);
|
---|
[36f2b3e] | 244 |
|
---|
[a1769ee] | 245 | return NULL;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[52b7b1bb] | 248 | static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[084ff99] | 249 | {
|
---|
[df747b9c] | 250 | char *dev_name = NULL;
|
---|
[36f2b3e] | 251 | int res;
|
---|
[9a66bc2e] | 252 |
|
---|
[d35ac1d] | 253 | devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
---|
[8b1e15ac] | 254 | devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
|
---|
[d35ac1d] | 255 |
|
---|
[83a2f43] | 256 | ddf_dev_t *dev = create_device();
|
---|
[084ff99] | 257 | dev->handle = dev_handle;
|
---|
[8b1e15ac] | 258 |
|
---|
[7a252ec8] | 259 | async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
|
---|
[df747b9c] | 260 | dev->name = dev_name;
|
---|
[8b1e15ac] | 261 |
|
---|
| 262 | /*
|
---|
| 263 | * Currently not used, parent fun handle is stored in context
|
---|
| 264 | * of the connection to the parent device driver.
|
---|
| 265 | */
|
---|
| 266 | (void) parent_fun_handle;
|
---|
[0d6915f] | 267 |
|
---|
[df747b9c] | 268 | res = driver->driver_ops->add_device(dev);
|
---|
[fc51296] | 269 | if (res != EOK)
|
---|
[7a252ec8] | 270 | delete_device(dev);
|
---|
[df747b9c] | 271 |
|
---|
[ffa2c8ef] | 272 | async_answer_0(iid, res);
|
---|
[084ff99] | 273 | }
|
---|
[c16cf62] | 274 |
|
---|
| 275 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 276 | {
|
---|
| 277 | /* Accept connection */
|
---|
[ffa2c8ef] | 278 | async_answer_0(iid, EOK);
|
---|
[36f2b3e] | 279 |
|
---|
[79ae36dd] | 280 | while (true) {
|
---|
[c16cf62] | 281 | ipc_call_t call;
|
---|
| 282 | ipc_callid_t callid = async_get_call(&call);
|
---|
[36f2b3e] | 283 |
|
---|
[79ae36dd] | 284 | if (!IPC_GET_IMETHOD(call))
|
---|
| 285 | break;
|
---|
| 286 |
|
---|
[228e490] | 287 | switch (IPC_GET_IMETHOD(call)) {
|
---|
[c16cf62] | 288 | case DRIVER_ADD_DEVICE:
|
---|
[084ff99] | 289 | driver_add_device(callid, &call);
|
---|
[c16cf62] | 290 | break;
|
---|
| 291 | default:
|
---|
[ffa2c8ef] | 292 | async_answer_0(callid, ENOENT);
|
---|
[c16cf62] | 293 | }
|
---|
[52b7b1bb] | 294 | }
|
---|
[c16cf62] | 295 | }
|
---|
| 296 |
|
---|
[79ae36dd] | 297 | /** Generic client connection handler both for applications and drivers.
|
---|
| 298 | *
|
---|
| 299 | * @param drv True for driver client, false for other clients
|
---|
| 300 | * (applications, services, etc.).
|
---|
[52b7b1bb] | 301 | *
|
---|
[a1769ee] | 302 | */
|
---|
[9a66bc2e] | 303 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
---|
[52b7b1bb] | 304 | {
|
---|
[7a252ec8] | 305 | /*
|
---|
| 306 | * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
|
---|
| 307 | * the device to which the client connected.
|
---|
| 308 | */
|
---|
[0b5a4131] | 309 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
[83a2f43] | 310 | ddf_fun_t *fun = driver_get_function(&functions, handle);
|
---|
[79ae36dd] | 311 |
|
---|
[8b1e15ac] | 312 | if (fun == NULL) {
|
---|
| 313 | printf("%s: driver_connection_gen error - no function with handle"
|
---|
[7e752b2] | 314 | " %" PRIun " was found.\n", driver->name, handle);
|
---|
[ffa2c8ef] | 315 | async_answer_0(iid, ENOENT);
|
---|
[a1769ee] | 316 | return;
|
---|
| 317 | }
|
---|
[5cd136ab] | 318 |
|
---|
[ff65e91] | 319 | if (fun->conn_handler != NULL) {
|
---|
| 320 | /* Driver has a custom connection handler. */
|
---|
| 321 | (*fun->conn_handler)(iid, icall, (void *)fun);
|
---|
| 322 | return;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[7a252ec8] | 325 | /*
|
---|
| 326 | * TODO - if the client is not a driver, check whether it is allowed to
|
---|
| 327 | * use the device.
|
---|
| 328 | */
|
---|
[36f2b3e] | 329 |
|
---|
[25a7e11d] | 330 | int ret = EOK;
|
---|
[8b1e15ac] | 331 | /* Open device function */
|
---|
| 332 | if (fun->ops != NULL && fun->ops->open != NULL)
|
---|
| 333 | ret = (*fun->ops->open)(fun);
|
---|
[a1769ee] | 334 |
|
---|
[ffa2c8ef] | 335 | async_answer_0(iid, ret);
|
---|
[36f2b3e] | 336 | if (ret != EOK)
|
---|
[a6e54c5d] | 337 | return;
|
---|
[36f2b3e] | 338 |
|
---|
[79ae36dd] | 339 | while (true) {
|
---|
[a1769ee] | 340 | ipc_callid_t callid;
|
---|
| 341 | ipc_call_t call;
|
---|
| 342 | callid = async_get_call(&call);
|
---|
[228e490] | 343 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
[3843ecb] | 344 |
|
---|
[79ae36dd] | 345 | if (!method) {
|
---|
[8b1e15ac] | 346 | /* Close device function */
|
---|
| 347 | if (fun->ops != NULL && fun->ops->close != NULL)
|
---|
| 348 | (*fun->ops->close)(fun);
|
---|
[ffa2c8ef] | 349 | async_answer_0(callid, EOK);
|
---|
[a1769ee] | 350 | return;
|
---|
[79ae36dd] | 351 | }
|
---|
| 352 |
|
---|
| 353 | /* Convert ipc interface id to interface index */
|
---|
| 354 |
|
---|
| 355 | int iface_idx = DEV_IFACE_IDX(method);
|
---|
| 356 |
|
---|
| 357 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
| 358 | remote_handler_t *default_handler =
|
---|
| 359 | function_get_default_handler(fun);
|
---|
| 360 | if (default_handler != NULL) {
|
---|
| 361 | (*default_handler)(fun, callid, &call);
|
---|
[79a141a] | 362 | continue;
|
---|
[52b7b1bb] | 363 | }
|
---|
| 364 |
|
---|
[7a252ec8] | 365 | /*
|
---|
[79ae36dd] | 366 | * Function has no such interface and
|
---|
| 367 | * default handler is not provided.
|
---|
[7a252ec8] | 368 | */
|
---|
[79ae36dd] | 369 | printf("%s: driver_connection_gen error - "
|
---|
| 370 | "invalid interface id %d.",
|
---|
| 371 | driver->name, iface_idx);
|
---|
| 372 | async_answer_0(callid, ENOTSUP);
|
---|
[79a141a] | 373 | continue;
|
---|
[79ae36dd] | 374 | }
|
---|
| 375 |
|
---|
| 376 | /* Calling one of the function's interfaces */
|
---|
| 377 |
|
---|
| 378 | /* Get the interface ops structure. */
|
---|
| 379 | void *ops = function_get_ops(fun, iface_idx);
|
---|
| 380 | if (ops == NULL) {
|
---|
| 381 | printf("%s: driver_connection_gen error - ", driver->name);
|
---|
| 382 | printf("Function with handle %" PRIun " has no interface "
|
---|
| 383 | "with id %d.\n", handle, iface_idx);
|
---|
| 384 | async_answer_0(callid, ENOTSUP);
|
---|
[79a141a] | 385 | continue;
|
---|
[79ae36dd] | 386 | }
|
---|
| 387 |
|
---|
| 388 | /*
|
---|
| 389 | * Get the corresponding interface for remote request
|
---|
| 390 | * handling ("remote interface").
|
---|
| 391 | */
|
---|
| 392 | remote_iface_t *rem_iface = get_remote_iface(iface_idx);
|
---|
| 393 | assert(rem_iface != NULL);
|
---|
| 394 |
|
---|
| 395 | /* get the method of the remote interface */
|
---|
| 396 | sysarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
| 397 | remote_iface_func_ptr_t iface_method_ptr =
|
---|
| 398 | get_remote_method(rem_iface, iface_method_idx);
|
---|
| 399 | if (iface_method_ptr == NULL) {
|
---|
| 400 | /* The interface has not such method */
|
---|
| 401 | printf("%s: driver_connection_gen error - "
|
---|
| 402 | "invalid interface method.", driver->name);
|
---|
| 403 | async_answer_0(callid, ENOTSUP);
|
---|
[79a141a] | 404 | continue;
|
---|
[a1769ee] | 405 | }
|
---|
[79ae36dd] | 406 |
|
---|
| 407 | /*
|
---|
| 408 | * Call the remote interface's method, which will
|
---|
| 409 | * receive parameters from the remote client and it will
|
---|
| 410 | * pass it to the corresponding local interface method
|
---|
| 411 | * associated with the function by its driver.
|
---|
| 412 | */
|
---|
| 413 | (*iface_method_ptr)(fun, ops, callid, &call);
|
---|
[a1769ee] | 414 | }
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[c16cf62] | 417 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 418 | {
|
---|
[52b7b1bb] | 419 | driver_connection_gen(iid, icall, true);
|
---|
[c16cf62] | 420 | }
|
---|
| 421 |
|
---|
| 422 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 423 | {
|
---|
[52b7b1bb] | 424 | driver_connection_gen(iid, icall, false);
|
---|
[c16cf62] | 425 | }
|
---|
| 426 |
|
---|
[7a252ec8] | 427 | /** Function for handling connections to device driver. */
|
---|
[9934f7d] | 428 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[c16cf62] | 429 | {
|
---|
| 430 | /* Select interface */
|
---|
[96b02eb9] | 431 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
[c16cf62] | 432 | case DRIVER_DEVMAN:
|
---|
[36f2b3e] | 433 | /* Handle request from device manager */
|
---|
[c16cf62] | 434 | driver_connection_devman(iid, icall);
|
---|
| 435 | break;
|
---|
| 436 | case DRIVER_DRIVER:
|
---|
[36f2b3e] | 437 | /* Handle request from drivers of child devices */
|
---|
[c16cf62] | 438 | driver_connection_driver(iid, icall);
|
---|
| 439 | break;
|
---|
| 440 | case DRIVER_CLIENT:
|
---|
[36f2b3e] | 441 | /* Handle request from client applications */
|
---|
[c16cf62] | 442 | driver_connection_client(iid, icall);
|
---|
| 443 | break;
|
---|
| 444 | default:
|
---|
[52b7b1bb] | 445 | /* No such interface */
|
---|
[ffa2c8ef] | 446 | async_answer_0(iid, ENOENT);
|
---|
[c16cf62] | 447 | }
|
---|
| 448 | }
|
---|
| 449 |
|
---|
[5fdd7c3] | 450 | /** Create new device structure.
|
---|
| 451 | *
|
---|
| 452 | * @return The device structure.
|
---|
| 453 | */
|
---|
[83a2f43] | 454 | static ddf_dev_t *create_device(void)
|
---|
[5fdd7c3] | 455 | {
|
---|
[83a2f43] | 456 | ddf_dev_t *dev;
|
---|
[5fdd7c3] | 457 |
|
---|
[83a2f43] | 458 | dev = malloc(sizeof(ddf_dev_t));
|
---|
[8b1e15ac] | 459 | if (dev == NULL)
|
---|
| 460 | return NULL;
|
---|
[5fdd7c3] | 461 |
|
---|
[83a2f43] | 462 | memset(dev, 0, sizeof(ddf_dev_t));
|
---|
[5fdd7c3] | 463 | return dev;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
[8b1e15ac] | 466 | /** Create new function structure.
|
---|
| 467 | *
|
---|
| 468 | * @return The device structure.
|
---|
| 469 | */
|
---|
[83a2f43] | 470 | static ddf_fun_t *create_function(void)
|
---|
[8b1e15ac] | 471 | {
|
---|
[83a2f43] | 472 | ddf_fun_t *fun;
|
---|
[8b1e15ac] | 473 |
|
---|
[83a2f43] | 474 | fun = calloc(1, sizeof(ddf_fun_t));
|
---|
[8b1e15ac] | 475 | if (fun == NULL)
|
---|
| 476 | return NULL;
|
---|
| 477 |
|
---|
| 478 | init_match_ids(&fun->match_ids);
|
---|
| 479 | link_initialize(&fun->link);
|
---|
| 480 |
|
---|
| 481 | return fun;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
[5fdd7c3] | 484 | /** Delete device structure.
|
---|
| 485 | *
|
---|
| 486 | * @param dev The device structure.
|
---|
| 487 | */
|
---|
[83a2f43] | 488 | static void delete_device(ddf_dev_t *dev)
|
---|
[5fdd7c3] | 489 | {
|
---|
| 490 | free(dev);
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[8b1e15ac] | 493 | /** Delete device structure.
|
---|
| 494 | *
|
---|
| 495 | * @param dev The device structure.
|
---|
| 496 | */
|
---|
[83a2f43] | 497 | static void delete_function(ddf_fun_t *fun)
|
---|
[8b1e15ac] | 498 | {
|
---|
| 499 | clean_match_ids(&fun->match_ids);
|
---|
| 500 | if (fun->name != NULL)
|
---|
| 501 | free(fun->name);
|
---|
| 502 | free(fun);
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[97a62fe] | 505 | /** Create a DDF function node.
|
---|
| 506 | *
|
---|
| 507 | * Create a DDF function (in memory). Both child devices and external clients
|
---|
| 508 | * communicate with a device via its functions.
|
---|
| 509 | *
|
---|
| 510 | * The created function node is fully formed, but only exists in the memory
|
---|
| 511 | * of the client task. In order to be visible to the system, the function
|
---|
| 512 | * must be bound using ddf_fun_bind().
|
---|
| 513 | *
|
---|
| 514 | * This function should only fail if there is not enough free memory.
|
---|
| 515 | * Specifically, this function succeeds even if @a dev already has
|
---|
| 516 | * a (bound) function with the same name.
|
---|
| 517 | *
|
---|
| 518 | * Type: A function of type fun_inner indicates that DDF should attempt
|
---|
| 519 | * to attach child devices to the function. fun_exposed means that
|
---|
| 520 | * the function should be exported to external clients (applications).
|
---|
| 521 | *
|
---|
| 522 | * @param dev Device to which we are adding function
|
---|
| 523 | * @param ftype Type of function (fun_inner or fun_exposed)
|
---|
| 524 | * @param name Name of function
|
---|
| 525 | *
|
---|
| 526 | * @return New function or @c NULL if memory is not available
|
---|
| 527 | */
|
---|
[83a2f43] | 528 | ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
|
---|
[97a62fe] | 529 | {
|
---|
[83a2f43] | 530 | ddf_fun_t *fun;
|
---|
[97a62fe] | 531 |
|
---|
| 532 | fun = create_function();
|
---|
| 533 | if (fun == NULL)
|
---|
| 534 | return NULL;
|
---|
| 535 |
|
---|
| 536 | fun->bound = false;
|
---|
| 537 | fun->dev = dev;
|
---|
| 538 | fun->ftype = ftype;
|
---|
| 539 |
|
---|
| 540 | fun->name = str_dup(name);
|
---|
| 541 | if (fun->name == NULL) {
|
---|
| 542 | delete_function(fun);
|
---|
| 543 | return NULL;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | return fun;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | /** Destroy DDF function node.
|
---|
| 550 | *
|
---|
| 551 | * Destroy a function previously created with ddf_fun_create(). The function
|
---|
| 552 | * must not be bound.
|
---|
| 553 | *
|
---|
| 554 | * @param fun Function to destroy
|
---|
| 555 | */
|
---|
[83a2f43] | 556 | void ddf_fun_destroy(ddf_fun_t *fun)
|
---|
[97a62fe] | 557 | {
|
---|
| 558 | assert(fun->bound == false);
|
---|
| 559 | delete_function(fun);
|
---|
| 560 | }
|
---|
| 561 |
|
---|
[af6b5157] | 562 | static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
|
---|
[5fdd7c3] | 563 | {
|
---|
| 564 | assert(is_valid_iface_idx(idx));
|
---|
[8b1e15ac] | 565 | if (fun->ops == NULL)
|
---|
[5fdd7c3] | 566 | return NULL;
|
---|
[8b1e15ac] | 567 | return fun->ops->interfaces[idx];
|
---|
[5fdd7c3] | 568 | }
|
---|
| 569 |
|
---|
[97a62fe] | 570 | /** Bind a function node.
|
---|
| 571 | *
|
---|
| 572 | * Bind the specified function to the system. This effectively makes
|
---|
| 573 | * the function visible to the system (uploads it to the server).
|
---|
| 574 | *
|
---|
| 575 | * This function can fail for several reasons. Specifically,
|
---|
| 576 | * it will fail if the device already has a bound function of
|
---|
| 577 | * the same name.
|
---|
| 578 | *
|
---|
| 579 | * @param fun Function to bind
|
---|
| 580 | * @return EOK on success or negative error code
|
---|
| 581 | */
|
---|
[83a2f43] | 582 | int ddf_fun_bind(ddf_fun_t *fun)
|
---|
[7707954] | 583 | {
|
---|
[d0dd7b5] | 584 | assert(fun->bound == false);
|
---|
[8b1e15ac] | 585 | assert(fun->name != NULL);
|
---|
[36f2b3e] | 586 |
|
---|
[df747b9c] | 587 | int res;
|
---|
| 588 |
|
---|
[8b1e15ac] | 589 | add_to_functions_list(fun);
|
---|
| 590 | res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
|
---|
[97a62fe] | 591 | fun->dev->handle, &fun->handle);
|
---|
[36f2b3e] | 592 | if (res != EOK) {
|
---|
[8b1e15ac] | 593 | remove_from_functions_list(fun);
|
---|
[df747b9c] | 594 | return res;
|
---|
[36f2b3e] | 595 | }
|
---|
| 596 |
|
---|
[97a62fe] | 597 | fun->bound = true;
|
---|
[df747b9c] | 598 | return res;
|
---|
[7707954] | 599 | }
|
---|
| 600 |
|
---|
[d0dd7b5] | 601 | /** Unbind a function node.
|
---|
| 602 | *
|
---|
| 603 | * Unbind the specified function from the system. This effectively makes
|
---|
| 604 | * the function invisible to the system.
|
---|
| 605 | *
|
---|
| 606 | * @param fun Function to bind
|
---|
| 607 | * @return EOK on success or negative error code
|
---|
| 608 | */
|
---|
| 609 | int ddf_fun_unbind(ddf_fun_t *fun)
|
---|
| 610 | {
|
---|
| 611 | int res;
|
---|
| 612 |
|
---|
| 613 | assert(fun->bound == true);
|
---|
| 614 |
|
---|
| 615 | add_to_functions_list(fun);
|
---|
| 616 | res = devman_remove_function(fun->handle);
|
---|
| 617 | if (res != EOK)
|
---|
| 618 | return res;
|
---|
| 619 |
|
---|
| 620 | remove_from_functions_list(fun);
|
---|
| 621 |
|
---|
| 622 | fun->bound = false;
|
---|
| 623 | return EOK;
|
---|
| 624 | }
|
---|
| 625 |
|
---|
[34588a80] | 626 | /** Add single match ID to inner function.
|
---|
[cd0684d] | 627 | *
|
---|
| 628 | * Construct and add a single match ID to the specified function.
|
---|
[34588a80] | 629 | * Cannot be called when the function node is bound.
|
---|
[cd0684d] | 630 | *
|
---|
| 631 | * @param fun Function
|
---|
| 632 | * @param match_id_str Match string
|
---|
| 633 | * @param match_score Match score
|
---|
| 634 | * @return EOK on success, ENOMEM if out of memory.
|
---|
| 635 | */
|
---|
[83a2f43] | 636 | int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
|
---|
[cd0684d] | 637 | int match_score)
|
---|
| 638 | {
|
---|
| 639 | match_id_t *match_id;
|
---|
| 640 |
|
---|
[34588a80] | 641 | assert(fun->bound == false);
|
---|
| 642 | assert(fun->ftype == fun_inner);
|
---|
| 643 |
|
---|
[cd0684d] | 644 | match_id = create_match_id();
|
---|
| 645 | if (match_id == NULL)
|
---|
| 646 | return ENOMEM;
|
---|
| 647 |
|
---|
[ef9460b] | 648 | match_id->id = str_dup(match_id_str);
|
---|
[cd0684d] | 649 | match_id->score = 90;
|
---|
| 650 |
|
---|
| 651 | add_match_id(&fun->match_ids, match_id);
|
---|
| 652 | return EOK;
|
---|
| 653 | }
|
---|
| 654 |
|
---|
[5fdd7c3] | 655 | /** Get default handler for client requests */
|
---|
[af6b5157] | 656 | static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
|
---|
[5fdd7c3] | 657 | {
|
---|
[8b1e15ac] | 658 | if (fun->ops == NULL)
|
---|
[5fdd7c3] | 659 | return NULL;
|
---|
[8b1e15ac] | 660 | return fun->ops->default_handler;
|
---|
[5fdd7c3] | 661 | }
|
---|
| 662 |
|
---|
[1dc4a5e] | 663 | /** Add exposed function to category.
|
---|
[34588a80] | 664 | *
|
---|
| 665 | * Must only be called when the function is bound.
|
---|
| 666 | */
|
---|
[1dc4a5e] | 667 | int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
|
---|
[5fdd7c3] | 668 | {
|
---|
[34588a80] | 669 | assert(fun->bound == true);
|
---|
| 670 | assert(fun->ftype == fun_exposed);
|
---|
| 671 |
|
---|
[1dc4a5e] | 672 | return devman_add_device_to_category(fun->handle, cat_name);
|
---|
[5fdd7c3] | 673 | }
|
---|
| 674 |
|
---|
[83a2f43] | 675 | int ddf_driver_main(driver_t *drv)
|
---|
[c16cf62] | 676 | {
|
---|
[033cbf82] | 677 | int rc;
|
---|
| 678 |
|
---|
[7a252ec8] | 679 | /*
|
---|
| 680 | * Remember the driver structure - driver_ops will be called by generic
|
---|
| 681 | * handler for incoming connections.
|
---|
| 682 | */
|
---|
[c16cf62] | 683 | driver = drv;
|
---|
[36f2b3e] | 684 |
|
---|
[7a252ec8] | 685 | /* Initialize the list of interrupt contexts. */
|
---|
[7f8b581] | 686 | init_interrupt_context_list(&interrupt_contexts);
|
---|
| 687 |
|
---|
[7a252ec8] | 688 | /* Set generic interrupt handler. */
|
---|
[7f8b581] | 689 | async_set_interrupt_received(driver_irq_handler);
|
---|
| 690 |
|
---|
[7a252ec8] | 691 | /*
|
---|
[033cbf82] | 692 | * Register driver with device manager using generic handler for
|
---|
| 693 | * incoming connections.
|
---|
[7a252ec8] | 694 | */
|
---|
[033cbf82] | 695 | rc = devman_driver_register(driver->name, driver_connection);
|
---|
| 696 | if (rc != EOK) {
|
---|
[3ad7b1c] | 697 | printf("Error: Failed to register driver with device manager "
|
---|
| 698 | "(%s).\n", (rc == EEXISTS) ? "driver already started" :
|
---|
| 699 | str_error(rc));
|
---|
| 700 |
|
---|
[033cbf82] | 701 | return 1;
|
---|
| 702 | }
|
---|
[36f2b3e] | 703 |
|
---|
[26fa82bc] | 704 | /* Return success from the task since server has started. */
|
---|
| 705 | rc = task_retval(0);
|
---|
| 706 | if (rc != EOK)
|
---|
| 707 | return 1;
|
---|
| 708 |
|
---|
[c16cf62] | 709 | async_manager();
|
---|
[36f2b3e] | 710 |
|
---|
[7a252ec8] | 711 | /* Never reached. */
|
---|
[52b7b1bb] | 712 | return 0;
|
---|
[c16cf62] | 713 | }
|
---|
| 714 |
|
---|
| 715 | /**
|
---|
| 716 | * @}
|
---|
[52b7b1bb] | 717 | */
|
---|