| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| 3 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 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 | */
|
|---|
| 38 |
|
|---|
| 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>
|
|---|
| 48 | #include <str.h>
|
|---|
| 49 | #include <str_error.h>
|
|---|
| 50 | #include <ctype.h>
|
|---|
| 51 | #include <errno.h>
|
|---|
| 52 | #include <inttypes.h>
|
|---|
| 53 | #include <devman.h>
|
|---|
| 54 |
|
|---|
| 55 | #include <ipc/driver.h>
|
|---|
| 56 |
|
|---|
| 57 | #include "dev_iface.h"
|
|---|
| 58 | #include "ddf/driver.h"
|
|---|
| 59 | #include "ddf/interrupt.h"
|
|---|
| 60 |
|
|---|
| 61 | /** Driver structure */
|
|---|
| 62 | static driver_t *driver;
|
|---|
| 63 |
|
|---|
| 64 | /** Devices */
|
|---|
| 65 | LIST_INITIALIZE(devices);
|
|---|
| 66 | FIBRIL_MUTEX_INITIALIZE(devices_mutex);
|
|---|
| 67 |
|
|---|
| 68 | /** Functions */
|
|---|
| 69 | LIST_INITIALIZE(functions);
|
|---|
| 70 | FIBRIL_MUTEX_INITIALIZE(functions_mutex);
|
|---|
| 71 |
|
|---|
| 72 | /** Interrupts */
|
|---|
| 73 | static interrupt_context_list_t interrupt_contexts;
|
|---|
| 74 |
|
|---|
| 75 | static irq_cmd_t default_cmds[] = {
|
|---|
| 76 | {
|
|---|
| 77 | .cmd = CMD_ACCEPT
|
|---|
| 78 | }
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | static irq_code_t default_pseudocode = {
|
|---|
| 82 | sizeof(default_cmds) / sizeof(irq_cmd_t),
|
|---|
| 83 | default_cmds
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | static ddf_dev_t *create_device(void);
|
|---|
| 87 | static void delete_device(ddf_dev_t *);
|
|---|
| 88 | static void dev_add_ref(ddf_dev_t *);
|
|---|
| 89 | static void dev_del_ref(ddf_dev_t *);
|
|---|
| 90 | static void fun_add_ref(ddf_fun_t *);
|
|---|
| 91 | static void fun_del_ref(ddf_fun_t *);
|
|---|
| 92 | static remote_handler_t *function_get_default_handler(ddf_fun_t *);
|
|---|
| 93 | static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
|
|---|
| 94 |
|
|---|
| 95 | static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 96 | {
|
|---|
| 97 | int id = (int)IPC_GET_IMETHOD(*icall);
|
|---|
| 98 | interrupt_context_t *ctx;
|
|---|
| 99 |
|
|---|
| 100 | ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
|
|---|
| 101 | if (ctx != NULL && ctx->handler != NULL)
|
|---|
| 102 | (*ctx->handler)(ctx->dev, iid, icall);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | interrupt_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 |
|
|---|
| 116 | void delete_interrupt_context(interrupt_context_t *ctx)
|
|---|
| 117 | {
|
|---|
| 118 | if (ctx != NULL)
|
|---|
| 119 | free(ctx);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | void 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 |
|
|---|
| 129 | void
|
|---|
| 130 | add_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 |
|
|---|
| 138 | void 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 |
|
|---|
| 146 | interrupt_context_t *
|
|---|
| 147 | find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
|
|---|
| 148 | {
|
|---|
| 149 | interrupt_context_t *ctx;
|
|---|
| 150 |
|
|---|
| 151 | fibril_mutex_lock(&list->mutex);
|
|---|
| 152 |
|
|---|
| 153 | list_foreach(list->contexts, link) {
|
|---|
| 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 |
|
|---|
| 165 | interrupt_context_t *
|
|---|
| 166 | find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
|
|---|
| 167 | {
|
|---|
| 168 | interrupt_context_t *ctx;
|
|---|
| 169 |
|
|---|
| 170 | fibril_mutex_lock(&list->mutex);
|
|---|
| 171 |
|
|---|
| 172 | list_foreach(list->contexts, link) {
|
|---|
| 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 |
|
|---|
| 185 | int
|
|---|
| 186 | register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
|
|---|
| 187 | irq_code_t *pseudocode)
|
|---|
| 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 |
|
|---|
| 197 | if (pseudocode == NULL)
|
|---|
| 198 | pseudocode = &default_pseudocode;
|
|---|
| 199 |
|
|---|
| 200 | int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
|
|---|
| 201 | if (res != EOK) {
|
|---|
| 202 | remove_interrupt_context(&interrupt_contexts, ctx);
|
|---|
| 203 | delete_interrupt_context(ctx);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | return res;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
|
|---|
| 210 | {
|
|---|
| 211 | interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
|
|---|
| 212 | dev, irq);
|
|---|
| 213 | int res = unregister_irq(irq, dev->handle);
|
|---|
| 214 |
|
|---|
| 215 | if (ctx != NULL) {
|
|---|
| 216 | remove_interrupt_context(&interrupt_contexts, ctx);
|
|---|
| 217 | delete_interrupt_context(ctx);
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | return res;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | static void add_to_functions_list(ddf_fun_t *fun)
|
|---|
| 224 | {
|
|---|
| 225 | fibril_mutex_lock(&functions_mutex);
|
|---|
| 226 | list_append(&fun->link, &functions);
|
|---|
| 227 | fibril_mutex_unlock(&functions_mutex);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | static void remove_from_functions_list(ddf_fun_t *fun)
|
|---|
| 231 | {
|
|---|
| 232 | fibril_mutex_lock(&functions_mutex);
|
|---|
| 233 | list_remove(&fun->link);
|
|---|
| 234 | fibril_mutex_unlock(&functions_mutex);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | static 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 |
|
|---|
| 252 | static ddf_fun_t *driver_get_function(devman_handle_t handle)
|
|---|
| 253 | {
|
|---|
| 254 | ddf_fun_t *fun = NULL;
|
|---|
| 255 |
|
|---|
| 256 | assert(fibril_mutex_is_locked(&functions_mutex));
|
|---|
| 257 |
|
|---|
| 258 | list_foreach(functions, link) {
|
|---|
| 259 | fun = list_get_instance(link, ddf_fun_t, link);
|
|---|
| 260 | if (fun->handle == handle)
|
|---|
| 261 | return fun;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | return NULL;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | static void driver_dev_add(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 268 | {
|
|---|
| 269 | char *dev_name = NULL;
|
|---|
| 270 | int res;
|
|---|
| 271 |
|
|---|
| 272 | devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
|---|
| 273 | devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
|
|---|
| 274 |
|
|---|
| 275 | ddf_dev_t *dev = create_device();
|
|---|
| 276 |
|
|---|
| 277 | /* Add one reference that will be dropped by driver_dev_remove() */
|
|---|
| 278 | dev_add_ref(dev);
|
|---|
| 279 | dev->handle = dev_handle;
|
|---|
| 280 |
|
|---|
| 281 | async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
|
|---|
| 282 | dev->name = dev_name;
|
|---|
| 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;
|
|---|
| 289 |
|
|---|
| 290 | res = driver->driver_ops->add_device(dev);
|
|---|
| 291 |
|
|---|
| 292 | if (res != EOK) {
|
|---|
| 293 | dev_del_ref(dev);
|
|---|
| 294 | async_answer_0(iid, res);
|
|---|
| 295 | return;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 299 | list_append(&dev->link, &devices);
|
|---|
| 300 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 301 |
|
|---|
| 302 | async_answer_0(iid, res);
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | static void driver_dev_remove(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 306 | {
|
|---|
| 307 | devman_handle_t devh;
|
|---|
| 308 | ddf_dev_t *dev;
|
|---|
| 309 | int rc;
|
|---|
| 310 |
|
|---|
| 311 | printf("libdrv: driver_dev_offline()\n");
|
|---|
| 312 | devh = IPC_GET_ARG1(*icall);
|
|---|
| 313 |
|
|---|
| 314 | fibril_mutex_lock(&devices_mutex);
|
|---|
| 315 | dev = driver_get_device(devh);
|
|---|
| 316 | dev_add_ref(dev);
|
|---|
| 317 | fibril_mutex_unlock(&devices_mutex);
|
|---|
| 318 |
|
|---|
| 319 | if (dev == NULL) {
|
|---|
| 320 | async_answer_0(iid, ENOENT);
|
|---|
| 321 | return;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | if (driver->driver_ops->dev_remove != NULL)
|
|---|
| 325 | rc = driver->driver_ops->dev_remove(dev);
|
|---|
| 326 | else
|
|---|
| 327 | rc = ENOTSUP;
|
|---|
| 328 |
|
|---|
| 329 | if (rc == EOK)
|
|---|
| 330 | dev_del_ref(dev);
|
|---|
| 331 |
|
|---|
| 332 | async_answer_0(iid, (sysarg_t) rc);
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | static void driver_fun_online(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 336 | {
|
|---|
| 337 | devman_handle_t funh;
|
|---|
| 338 | ddf_fun_t *fun;
|
|---|
| 339 | int rc;
|
|---|
| 340 |
|
|---|
| 341 | funh = IPC_GET_ARG1(*icall);
|
|---|
| 342 |
|
|---|
| 343 | /*
|
|---|
| 344 | * Look the function up. Bump reference count so that
|
|---|
| 345 | * the function continues to exist until we return
|
|---|
| 346 | * from the driver.
|
|---|
| 347 | */
|
|---|
| 348 | fibril_mutex_lock(&functions_mutex);
|
|---|
| 349 |
|
|---|
| 350 | fun = driver_get_function(funh);
|
|---|
| 351 | if (fun != NULL)
|
|---|
| 352 | fun_add_ref(fun);
|
|---|
| 353 |
|
|---|
| 354 | fibril_mutex_unlock(&functions_mutex);
|
|---|
| 355 |
|
|---|
| 356 | if (fun == NULL) {
|
|---|
| 357 | async_answer_0(iid, ENOENT);
|
|---|
| 358 | return;
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | /* Call driver entry point */
|
|---|
| 362 | if (driver->driver_ops->fun_online != NULL)
|
|---|
| 363 | rc = driver->driver_ops->fun_online(fun);
|
|---|
| 364 | else
|
|---|
| 365 | rc = ENOTSUP;
|
|---|
| 366 |
|
|---|
| 367 | fun_del_ref(fun);
|
|---|
| 368 |
|
|---|
| 369 | async_answer_0(iid, (sysarg_t) rc);
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | static void driver_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 373 | {
|
|---|
| 374 | devman_handle_t funh;
|
|---|
| 375 | ddf_fun_t *fun;
|
|---|
| 376 | int rc;
|
|---|
| 377 |
|
|---|
| 378 | funh = IPC_GET_ARG1(*icall);
|
|---|
| 379 |
|
|---|
| 380 | /*
|
|---|
| 381 | * Look the function up. Bump reference count so that
|
|---|
| 382 | * the function continues to exist until we return
|
|---|
| 383 | * from the driver.
|
|---|
| 384 | */
|
|---|
| 385 | fibril_mutex_lock(&functions_mutex);
|
|---|
| 386 |
|
|---|
| 387 | fun = driver_get_function(funh);
|
|---|
| 388 | if (fun != NULL)
|
|---|
| 389 | fun_add_ref(fun);
|
|---|
| 390 |
|
|---|
| 391 | fibril_mutex_unlock(&functions_mutex);
|
|---|
| 392 |
|
|---|
| 393 | if (fun == NULL) {
|
|---|
| 394 | async_answer_0(iid, ENOENT);
|
|---|
| 395 | return;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | /* Call driver entry point */
|
|---|
| 399 | if (driver->driver_ops->fun_offline != NULL)
|
|---|
| 400 | rc = driver->driver_ops->fun_offline(fun);
|
|---|
| 401 | else
|
|---|
| 402 | rc = ENOTSUP;
|
|---|
| 403 |
|
|---|
| 404 | async_answer_0(iid, (sysarg_t) rc);
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 408 | {
|
|---|
| 409 | /* Accept connection */
|
|---|
| 410 | async_answer_0(iid, EOK);
|
|---|
| 411 |
|
|---|
| 412 | while (true) {
|
|---|
| 413 | ipc_call_t call;
|
|---|
| 414 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 415 |
|
|---|
| 416 | if (!IPC_GET_IMETHOD(call))
|
|---|
| 417 | break;
|
|---|
| 418 |
|
|---|
| 419 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 420 | case DRIVER_DEV_ADD:
|
|---|
| 421 | driver_dev_add(callid, &call);
|
|---|
| 422 | break;
|
|---|
| 423 | case DRIVER_DEV_REMOVE:
|
|---|
| 424 | driver_dev_remove(callid, &call);
|
|---|
| 425 | break;
|
|---|
| 426 | case DRIVER_FUN_ONLINE:
|
|---|
| 427 | driver_fun_online(callid, &call);
|
|---|
| 428 | break;
|
|---|
| 429 | case DRIVER_FUN_OFFLINE:
|
|---|
| 430 | driver_fun_offline(callid, &call);
|
|---|
| 431 | break;
|
|---|
| 432 | default:
|
|---|
| 433 | async_answer_0(callid, ENOTSUP);
|
|---|
| 434 | }
|
|---|
| 435 | }
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | /** Generic client connection handler both for applications and drivers.
|
|---|
| 439 | *
|
|---|
| 440 | * @param drv True for driver client, false for other clients
|
|---|
| 441 | * (applications, services, etc.).
|
|---|
| 442 | *
|
|---|
| 443 | */
|
|---|
| 444 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
|---|
| 445 | {
|
|---|
| 446 | /*
|
|---|
| 447 | * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
|
|---|
| 448 | * the device to which the client connected.
|
|---|
| 449 | */
|
|---|
| 450 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
|---|
| 451 |
|
|---|
| 452 | fibril_mutex_lock(&functions_mutex);
|
|---|
| 453 | ddf_fun_t *fun = driver_get_function(handle);
|
|---|
| 454 | fibril_mutex_unlock(&functions_mutex);
|
|---|
| 455 | /* XXX Need a lock on fun */
|
|---|
| 456 |
|
|---|
| 457 | if (fun == NULL) {
|
|---|
| 458 | printf("%s: driver_connection_gen error - no function with handle"
|
|---|
| 459 | " %" PRIun " was found.\n", driver->name, handle);
|
|---|
| 460 | async_answer_0(iid, ENOENT);
|
|---|
| 461 | return;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | if (fun->conn_handler != NULL) {
|
|---|
| 465 | /* Driver has a custom connection handler. */
|
|---|
| 466 | (*fun->conn_handler)(iid, icall, (void *)fun);
|
|---|
| 467 | return;
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | /*
|
|---|
| 471 | * TODO - if the client is not a driver, check whether it is allowed to
|
|---|
| 472 | * use the device.
|
|---|
| 473 | */
|
|---|
| 474 |
|
|---|
| 475 | int ret = EOK;
|
|---|
| 476 | /* Open device function */
|
|---|
| 477 | if (fun->ops != NULL && fun->ops->open != NULL)
|
|---|
| 478 | ret = (*fun->ops->open)(fun);
|
|---|
| 479 |
|
|---|
| 480 | async_answer_0(iid, ret);
|
|---|
| 481 | if (ret != EOK)
|
|---|
| 482 | return;
|
|---|
| 483 |
|
|---|
| 484 | while (true) {
|
|---|
| 485 | ipc_callid_t callid;
|
|---|
| 486 | ipc_call_t call;
|
|---|
| 487 | callid = async_get_call(&call);
|
|---|
| 488 | sysarg_t method = IPC_GET_IMETHOD(call);
|
|---|
| 489 |
|
|---|
| 490 | if (!method) {
|
|---|
| 491 | /* Close device function */
|
|---|
| 492 | if (fun->ops != NULL && fun->ops->close != NULL)
|
|---|
| 493 | (*fun->ops->close)(fun);
|
|---|
| 494 | async_answer_0(callid, EOK);
|
|---|
| 495 | return;
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | /* Convert ipc interface id to interface index */
|
|---|
| 499 |
|
|---|
| 500 | int iface_idx = DEV_IFACE_IDX(method);
|
|---|
| 501 |
|
|---|
| 502 | if (!is_valid_iface_idx(iface_idx)) {
|
|---|
| 503 | remote_handler_t *default_handler =
|
|---|
| 504 | function_get_default_handler(fun);
|
|---|
| 505 | if (default_handler != NULL) {
|
|---|
| 506 | (*default_handler)(fun, callid, &call);
|
|---|
| 507 | continue;
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | /*
|
|---|
| 511 | * Function has no such interface and
|
|---|
| 512 | * default handler is not provided.
|
|---|
| 513 | */
|
|---|
| 514 | printf("%s: driver_connection_gen error - "
|
|---|
| 515 | "invalid interface id %d.",
|
|---|
| 516 | driver->name, iface_idx);
|
|---|
| 517 | async_answer_0(callid, ENOTSUP);
|
|---|
| 518 | continue;
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | /* Calling one of the function's interfaces */
|
|---|
| 522 |
|
|---|
| 523 | /* Get the interface ops structure. */
|
|---|
| 524 | void *ops = function_get_ops(fun, iface_idx);
|
|---|
| 525 | if (ops == NULL) {
|
|---|
| 526 | printf("%s: driver_connection_gen error - ", driver->name);
|
|---|
| 527 | printf("Function with handle %" PRIun " has no interface "
|
|---|
| 528 | "with id %d.\n", handle, iface_idx);
|
|---|
| 529 | async_answer_0(callid, ENOTSUP);
|
|---|
| 530 | continue;
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | /*
|
|---|
| 534 | * Get the corresponding interface for remote request
|
|---|
| 535 | * handling ("remote interface").
|
|---|
| 536 | */
|
|---|
| 537 | remote_iface_t *rem_iface = get_remote_iface(iface_idx);
|
|---|
| 538 | assert(rem_iface != NULL);
|
|---|
| 539 |
|
|---|
| 540 | /* get the method of the remote interface */
|
|---|
| 541 | sysarg_t iface_method_idx = IPC_GET_ARG1(call);
|
|---|
| 542 | remote_iface_func_ptr_t iface_method_ptr =
|
|---|
| 543 | get_remote_method(rem_iface, iface_method_idx);
|
|---|
| 544 | if (iface_method_ptr == NULL) {
|
|---|
| 545 | /* The interface has not such method */
|
|---|
| 546 | printf("%s: driver_connection_gen error - "
|
|---|
| 547 | "invalid interface method.", driver->name);
|
|---|
| 548 | async_answer_0(callid, ENOTSUP);
|
|---|
| 549 | continue;
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | /*
|
|---|
| 553 | * Call the remote interface's method, which will
|
|---|
| 554 | * receive parameters from the remote client and it will
|
|---|
| 555 | * pass it to the corresponding local interface method
|
|---|
| 556 | * associated with the function by its driver.
|
|---|
| 557 | */
|
|---|
| 558 | (*iface_method_ptr)(fun, ops, callid, &call);
|
|---|
| 559 | }
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 563 | {
|
|---|
| 564 | driver_connection_gen(iid, icall, true);
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 568 | {
|
|---|
| 569 | driver_connection_gen(iid, icall, false);
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | /** Function for handling connections to device driver. */
|
|---|
| 573 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 574 | {
|
|---|
| 575 | sysarg_t conn_type;
|
|---|
| 576 |
|
|---|
| 577 | if (iid == 0) {
|
|---|
| 578 | /* Callback connection from devman */
|
|---|
| 579 | /* XXX Use separate handler for this type of connection */
|
|---|
| 580 | conn_type = DRIVER_DEVMAN;
|
|---|
| 581 | } else {
|
|---|
| 582 | conn_type = IPC_GET_ARG1(*icall);
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | /* Select interface */
|
|---|
| 586 | switch (conn_type) {
|
|---|
| 587 | case DRIVER_DEVMAN:
|
|---|
| 588 | /* Handle request from device manager */
|
|---|
| 589 | driver_connection_devman(iid, icall);
|
|---|
| 590 | break;
|
|---|
| 591 | case DRIVER_DRIVER:
|
|---|
| 592 | /* Handle request from drivers of child devices */
|
|---|
| 593 | driver_connection_driver(iid, icall);
|
|---|
| 594 | break;
|
|---|
| 595 | case DRIVER_CLIENT:
|
|---|
| 596 | /* Handle request from client applications */
|
|---|
| 597 | driver_connection_client(iid, icall);
|
|---|
| 598 | break;
|
|---|
| 599 | default:
|
|---|
| 600 | /* No such interface */
|
|---|
| 601 | async_answer_0(iid, ENOENT);
|
|---|
| 602 | }
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | /** Create new device structure.
|
|---|
| 606 | *
|
|---|
| 607 | * @return The device structure.
|
|---|
| 608 | */
|
|---|
| 609 | static ddf_dev_t *create_device(void)
|
|---|
| 610 | {
|
|---|
| 611 | ddf_dev_t *dev;
|
|---|
| 612 |
|
|---|
| 613 | dev = calloc(1, sizeof(ddf_dev_t));
|
|---|
| 614 | if (dev == NULL)
|
|---|
| 615 | return NULL;
|
|---|
| 616 |
|
|---|
| 617 | return dev;
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | /** Create new function structure.
|
|---|
| 621 | *
|
|---|
| 622 | * @return The device structure.
|
|---|
| 623 | */
|
|---|
| 624 | static ddf_fun_t *create_function(void)
|
|---|
| 625 | {
|
|---|
| 626 | ddf_fun_t *fun;
|
|---|
| 627 |
|
|---|
| 628 | fun = calloc(1, sizeof(ddf_fun_t));
|
|---|
| 629 | if (fun == NULL)
|
|---|
| 630 | return NULL;
|
|---|
| 631 |
|
|---|
| 632 | init_match_ids(&fun->match_ids);
|
|---|
| 633 | link_initialize(&fun->link);
|
|---|
| 634 |
|
|---|
| 635 | return fun;
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | /** Delete device structure.
|
|---|
| 639 | *
|
|---|
| 640 | * @param dev The device structure.
|
|---|
| 641 | */
|
|---|
| 642 | static void delete_device(ddf_dev_t *dev)
|
|---|
| 643 | {
|
|---|
| 644 | free(dev);
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | /** Delete function structure.
|
|---|
| 648 | *
|
|---|
| 649 | * @param dev The device structure.
|
|---|
| 650 | */
|
|---|
| 651 | static void delete_function(ddf_fun_t *fun)
|
|---|
| 652 | {
|
|---|
| 653 | clean_match_ids(&fun->match_ids);
|
|---|
| 654 | if (fun->name != NULL)
|
|---|
| 655 | free(fun->name);
|
|---|
| 656 | free(fun);
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 | /** Increase device reference count. */
|
|---|
| 660 | static void dev_add_ref(ddf_dev_t *dev)
|
|---|
| 661 | {
|
|---|
| 662 | atomic_inc(&dev->refcnt);
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 | /** Decrease device reference count.
|
|---|
| 666 | *
|
|---|
| 667 | * Free the device structure if the reference count drops to zero.
|
|---|
| 668 | */
|
|---|
| 669 | static void dev_del_ref(ddf_dev_t *dev)
|
|---|
| 670 | {
|
|---|
| 671 | if (atomic_predec(&dev->refcnt) == 0)
|
|---|
| 672 | delete_device(dev);
|
|---|
| 673 | }
|
|---|
| 674 |
|
|---|
| 675 | /** Increase function reference count. */
|
|---|
| 676 | static void fun_add_ref(ddf_fun_t *fun)
|
|---|
| 677 | {
|
|---|
| 678 | atomic_inc(&fun->refcnt);
|
|---|
| 679 | }
|
|---|
| 680 |
|
|---|
| 681 | /** Decrease function reference count.
|
|---|
| 682 | *
|
|---|
| 683 | * Free the function structure if the reference count drops to zero.
|
|---|
| 684 | */
|
|---|
| 685 | static void fun_del_ref(ddf_fun_t *fun)
|
|---|
| 686 | {
|
|---|
| 687 | if (atomic_predec(&fun->refcnt) == 0)
|
|---|
| 688 | delete_function(fun);
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | /** Create a DDF function node.
|
|---|
| 692 | *
|
|---|
| 693 | * Create a DDF function (in memory). Both child devices and external clients
|
|---|
| 694 | * communicate with a device via its functions.
|
|---|
| 695 | *
|
|---|
| 696 | * The created function node is fully formed, but only exists in the memory
|
|---|
| 697 | * of the client task. In order to be visible to the system, the function
|
|---|
| 698 | * must be bound using ddf_fun_bind().
|
|---|
| 699 | *
|
|---|
| 700 | * This function should only fail if there is not enough free memory.
|
|---|
| 701 | * Specifically, this function succeeds even if @a dev already has
|
|---|
| 702 | * a (bound) function with the same name.
|
|---|
| 703 | *
|
|---|
| 704 | * Type: A function of type fun_inner indicates that DDF should attempt
|
|---|
| 705 | * to attach child devices to the function. fun_exposed means that
|
|---|
| 706 | * the function should be exported to external clients (applications).
|
|---|
| 707 | *
|
|---|
| 708 | * @param dev Device to which we are adding function
|
|---|
| 709 | * @param ftype Type of function (fun_inner or fun_exposed)
|
|---|
| 710 | * @param name Name of function
|
|---|
| 711 | *
|
|---|
| 712 | * @return New function or @c NULL if memory is not available
|
|---|
| 713 | */
|
|---|
| 714 | ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
|
|---|
| 715 | {
|
|---|
| 716 | ddf_fun_t *fun;
|
|---|
| 717 |
|
|---|
| 718 | fun = create_function();
|
|---|
| 719 | if (fun == NULL)
|
|---|
| 720 | return NULL;
|
|---|
| 721 |
|
|---|
| 722 | /* Add one reference that will be dropped by ddf_fun_destroy() */
|
|---|
| 723 | fun_add_ref(fun);
|
|---|
| 724 |
|
|---|
| 725 | fun->bound = false;
|
|---|
| 726 | fun->dev = dev;
|
|---|
| 727 | fun->ftype = ftype;
|
|---|
| 728 |
|
|---|
| 729 | fun->name = str_dup(name);
|
|---|
| 730 | if (fun->name == NULL) {
|
|---|
| 731 | delete_function(fun);
|
|---|
| 732 | return NULL;
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 | return fun;
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | /** Destroy DDF function node.
|
|---|
| 739 | *
|
|---|
| 740 | * Destroy a function previously created with ddf_fun_create(). The function
|
|---|
| 741 | * must not be bound.
|
|---|
| 742 | *
|
|---|
| 743 | * @param fun Function to destroy
|
|---|
| 744 | */
|
|---|
| 745 | void ddf_fun_destroy(ddf_fun_t *fun)
|
|---|
| 746 | {
|
|---|
| 747 | assert(fun->bound == false);
|
|---|
| 748 |
|
|---|
| 749 | /*
|
|---|
| 750 | * Drop the reference added by ddf_fun_create(). This will deallocate
|
|---|
| 751 | * the function as soon as all other references are dropped (i.e.
|
|---|
| 752 | * as soon control leaves all driver entry points called in context
|
|---|
| 753 | * of this function.
|
|---|
| 754 | */
|
|---|
| 755 | fun_del_ref(fun);
|
|---|
| 756 | }
|
|---|
| 757 |
|
|---|
| 758 | static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
|
|---|
| 759 | {
|
|---|
| 760 | assert(is_valid_iface_idx(idx));
|
|---|
| 761 | if (fun->ops == NULL)
|
|---|
| 762 | return NULL;
|
|---|
| 763 | return fun->ops->interfaces[idx];
|
|---|
| 764 | }
|
|---|
| 765 |
|
|---|
| 766 | /** Bind a function node.
|
|---|
| 767 | *
|
|---|
| 768 | * Bind the specified function to the system. This effectively makes
|
|---|
| 769 | * the function visible to the system (uploads it to the server).
|
|---|
| 770 | *
|
|---|
| 771 | * This function can fail for several reasons. Specifically,
|
|---|
| 772 | * it will fail if the device already has a bound function of
|
|---|
| 773 | * the same name.
|
|---|
| 774 | *
|
|---|
| 775 | * @param fun Function to bind
|
|---|
| 776 | * @return EOK on success or negative error code
|
|---|
| 777 | */
|
|---|
| 778 | int ddf_fun_bind(ddf_fun_t *fun)
|
|---|
| 779 | {
|
|---|
| 780 | assert(fun->bound == false);
|
|---|
| 781 | assert(fun->name != NULL);
|
|---|
| 782 |
|
|---|
| 783 | int res;
|
|---|
| 784 |
|
|---|
| 785 | add_to_functions_list(fun);
|
|---|
| 786 | res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
|
|---|
| 787 | fun->dev->handle, &fun->handle);
|
|---|
| 788 | if (res != EOK) {
|
|---|
| 789 | remove_from_functions_list(fun);
|
|---|
| 790 | return res;
|
|---|
| 791 | }
|
|---|
| 792 |
|
|---|
| 793 | fun->bound = true;
|
|---|
| 794 | return res;
|
|---|
| 795 | }
|
|---|
| 796 |
|
|---|
| 797 | /** Unbind a function node.
|
|---|
| 798 | *
|
|---|
| 799 | * Unbind the specified function from the system. This effectively makes
|
|---|
| 800 | * the function invisible to the system.
|
|---|
| 801 | *
|
|---|
| 802 | * @param fun Function to unbind
|
|---|
| 803 | * @return EOK on success or negative error code
|
|---|
| 804 | */
|
|---|
| 805 | int ddf_fun_unbind(ddf_fun_t *fun)
|
|---|
| 806 | {
|
|---|
| 807 | int res;
|
|---|
| 808 |
|
|---|
| 809 | assert(fun->bound == true);
|
|---|
| 810 |
|
|---|
| 811 | res = devman_remove_function(fun->handle);
|
|---|
| 812 | if (res != EOK)
|
|---|
| 813 | return res;
|
|---|
| 814 |
|
|---|
| 815 | remove_from_functions_list(fun);
|
|---|
| 816 |
|
|---|
| 817 | fun->bound = false;
|
|---|
| 818 | return EOK;
|
|---|
| 819 | }
|
|---|
| 820 |
|
|---|
| 821 | /** Online function.
|
|---|
| 822 | *
|
|---|
| 823 | * @param fun Function to online
|
|---|
| 824 | * @return EOK on success or negative error code
|
|---|
| 825 | */
|
|---|
| 826 | int ddf_fun_online(ddf_fun_t *fun)
|
|---|
| 827 | {
|
|---|
| 828 | int res;
|
|---|
| 829 |
|
|---|
| 830 | assert(fun->bound == true);
|
|---|
| 831 |
|
|---|
| 832 | res = devman_drv_fun_online(fun->handle);
|
|---|
| 833 | if (res != EOK)
|
|---|
| 834 | return res;
|
|---|
| 835 |
|
|---|
| 836 | return EOK;
|
|---|
| 837 | }
|
|---|
| 838 |
|
|---|
| 839 | /** Offline function.
|
|---|
| 840 | *
|
|---|
| 841 | * @param fun Function to offline
|
|---|
| 842 | * @return EOK on success or negative error code
|
|---|
| 843 | */
|
|---|
| 844 | int ddf_fun_offline(ddf_fun_t *fun)
|
|---|
| 845 | {
|
|---|
| 846 | int res;
|
|---|
| 847 |
|
|---|
| 848 | assert(fun->bound == true);
|
|---|
| 849 |
|
|---|
| 850 | res = devman_drv_fun_offline(fun->handle);
|
|---|
| 851 | if (res != EOK)
|
|---|
| 852 | return res;
|
|---|
| 853 |
|
|---|
| 854 | return EOK;
|
|---|
| 855 | }
|
|---|
| 856 |
|
|---|
| 857 | /** Add single match ID to inner function.
|
|---|
| 858 | *
|
|---|
| 859 | * Construct and add a single match ID to the specified function.
|
|---|
| 860 | * Cannot be called when the function node is bound.
|
|---|
| 861 | *
|
|---|
| 862 | * @param fun Function
|
|---|
| 863 | * @param match_id_str Match string
|
|---|
| 864 | * @param match_score Match score
|
|---|
| 865 | * @return EOK on success, ENOMEM if out of memory.
|
|---|
| 866 | */
|
|---|
| 867 | int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
|
|---|
| 868 | int match_score)
|
|---|
| 869 | {
|
|---|
| 870 | match_id_t *match_id;
|
|---|
| 871 |
|
|---|
| 872 | assert(fun->bound == false);
|
|---|
| 873 | assert(fun->ftype == fun_inner);
|
|---|
| 874 |
|
|---|
| 875 | match_id = create_match_id();
|
|---|
| 876 | if (match_id == NULL)
|
|---|
| 877 | return ENOMEM;
|
|---|
| 878 |
|
|---|
| 879 | match_id->id = str_dup(match_id_str);
|
|---|
| 880 | match_id->score = 90;
|
|---|
| 881 |
|
|---|
| 882 | add_match_id(&fun->match_ids, match_id);
|
|---|
| 883 | return EOK;
|
|---|
| 884 | }
|
|---|
| 885 |
|
|---|
| 886 | /** Get default handler for client requests */
|
|---|
| 887 | static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
|
|---|
| 888 | {
|
|---|
| 889 | if (fun->ops == NULL)
|
|---|
| 890 | return NULL;
|
|---|
| 891 | return fun->ops->default_handler;
|
|---|
| 892 | }
|
|---|
| 893 |
|
|---|
| 894 | /** Add exposed function to category.
|
|---|
| 895 | *
|
|---|
| 896 | * Must only be called when the function is bound.
|
|---|
| 897 | */
|
|---|
| 898 | int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
|
|---|
| 899 | {
|
|---|
| 900 | assert(fun->bound == true);
|
|---|
| 901 | assert(fun->ftype == fun_exposed);
|
|---|
| 902 |
|
|---|
| 903 | return devman_add_device_to_category(fun->handle, cat_name);
|
|---|
| 904 | }
|
|---|
| 905 |
|
|---|
| 906 | int ddf_driver_main(driver_t *drv)
|
|---|
| 907 | {
|
|---|
| 908 | int rc;
|
|---|
| 909 |
|
|---|
| 910 | /*
|
|---|
| 911 | * Remember the driver structure - driver_ops will be called by generic
|
|---|
| 912 | * handler for incoming connections.
|
|---|
| 913 | */
|
|---|
| 914 | driver = drv;
|
|---|
| 915 |
|
|---|
| 916 | /* Initialize the list of interrupt contexts. */
|
|---|
| 917 | init_interrupt_context_list(&interrupt_contexts);
|
|---|
| 918 |
|
|---|
| 919 | /* Set generic interrupt handler. */
|
|---|
| 920 | async_set_interrupt_received(driver_irq_handler);
|
|---|
| 921 |
|
|---|
| 922 | /*
|
|---|
| 923 | * Register driver with device manager using generic handler for
|
|---|
| 924 | * incoming connections.
|
|---|
| 925 | */
|
|---|
| 926 | rc = devman_driver_register(driver->name, driver_connection);
|
|---|
| 927 | if (rc != EOK) {
|
|---|
| 928 | printf("Error: Failed to register driver with device manager "
|
|---|
| 929 | "(%s).\n", (rc == EEXISTS) ? "driver already started" :
|
|---|
| 930 | str_error(rc));
|
|---|
| 931 |
|
|---|
| 932 | return 1;
|
|---|
| 933 | }
|
|---|
| 934 |
|
|---|
| 935 | /* Return success from the task since server has started. */
|
|---|
| 936 | rc = task_retval(0);
|
|---|
| 937 | if (rc != EOK)
|
|---|
| 938 | return 1;
|
|---|
| 939 |
|
|---|
| 940 | async_manager();
|
|---|
| 941 |
|
|---|
| 942 | /* Never reached. */
|
|---|
| 943 | return 0;
|
|---|
| 944 | }
|
|---|
| 945 |
|
|---|
| 946 | /**
|
|---|
| 947 | * @}
|
|---|
| 948 | */
|
|---|