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