[c16cf62] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * @defgroup libdrv generic device driver support.
|
---|
| 31 | * @brief HelenOS generic device driver support.
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | /** @file
|
---|
| 36 | */
|
---|
[52b7b1bb] | 37 |
|
---|
[c16cf62] | 38 | #include <assert.h>
|
---|
| 39 | #include <ipc/services.h>
|
---|
| 40 | #include <ipc/ns.h>
|
---|
| 41 | #include <async.h>
|
---|
| 42 | #include <stdio.h>
|
---|
| 43 | #include <errno.h>
|
---|
| 44 | #include <bool.h>
|
---|
| 45 | #include <fibril_synch.h>
|
---|
| 46 | #include <stdlib.h>
|
---|
[c47e1a8] | 47 | #include <str.h>
|
---|
[c16cf62] | 48 | #include <ctype.h>
|
---|
[66babbd] | 49 | #include <errno.h>
|
---|
[7e752b2] | 50 | #include <inttypes.h>
|
---|
[c16cf62] | 51 |
|
---|
[084ff99] | 52 | #include <ipc/driver.h>
|
---|
[c16cf62] | 53 |
|
---|
[5fdd7c3] | 54 | #include "dev_iface.h"
|
---|
[c16cf62] | 55 | #include "driver.h"
|
---|
| 56 |
|
---|
[36f2b3e] | 57 | /** Driver structure */
|
---|
[c16cf62] | 58 | static driver_t *driver;
|
---|
[7f8b581] | 59 |
|
---|
[36f2b3e] | 60 | /** Devices */
|
---|
[8b1e15ac] | 61 | LIST_INITIALIZE(functions);
|
---|
| 62 | FIBRIL_MUTEX_INITIALIZE(functions_mutex);
|
---|
[084ff99] | 63 |
|
---|
[36f2b3e] | 64 | /** Interrupts */
|
---|
[7f8b581] | 65 | static interrupt_context_list_t interrupt_contexts;
|
---|
| 66 |
|
---|
| 67 | static irq_cmd_t default_cmds[] = {
|
---|
| 68 | {
|
---|
| 69 | .cmd = CMD_ACCEPT
|
---|
| 70 | }
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 | static irq_code_t default_pseudocode = {
|
---|
| 74 | sizeof(default_cmds) / sizeof(irq_cmd_t),
|
---|
| 75 | default_cmds
|
---|
| 76 | };
|
---|
| 77 |
|
---|
[7df0477e] | 78 | static device_t *create_device(void);
|
---|
| 79 | static void delete_device(device_t *);
|
---|
[7f8b581] | 80 |
|
---|
| 81 | static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[7a252ec8] | 82 | {
|
---|
[228e490] | 83 | int id = (int)IPC_GET_IMETHOD(*icall);
|
---|
[7a252ec8] | 84 | interrupt_context_t *ctx;
|
---|
| 85 |
|
---|
| 86 | ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
|
---|
[36f2b3e] | 87 | if (ctx != NULL && ctx->handler != NULL)
|
---|
[7a252ec8] | 88 | (*ctx->handler)(ctx->dev, iid, icall);
|
---|
[7f8b581] | 89 | }
|
---|
| 90 |
|
---|
[5fdd7c3] | 91 | interrupt_context_t *create_interrupt_context(void)
|
---|
| 92 | {
|
---|
| 93 | interrupt_context_t *ctx;
|
---|
| 94 |
|
---|
| 95 | ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
|
---|
| 96 | if (ctx != NULL)
|
---|
| 97 | memset(ctx, 0, sizeof(interrupt_context_t));
|
---|
| 98 |
|
---|
| 99 | return ctx;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void delete_interrupt_context(interrupt_context_t *ctx)
|
---|
| 103 | {
|
---|
| 104 | if (ctx != NULL)
|
---|
| 105 | free(ctx);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void init_interrupt_context_list(interrupt_context_list_t *list)
|
---|
| 109 | {
|
---|
| 110 | memset(list, 0, sizeof(interrupt_context_list_t));
|
---|
| 111 | fibril_mutex_initialize(&list->mutex);
|
---|
| 112 | list_initialize(&list->contexts);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | void
|
---|
| 116 | add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
|
---|
| 117 | {
|
---|
| 118 | fibril_mutex_lock(&list->mutex);
|
---|
| 119 | ctx->id = list->curr_id++;
|
---|
| 120 | list_append(&ctx->link, &list->contexts);
|
---|
| 121 | fibril_mutex_unlock(&list->mutex);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | void remove_interrupt_context(interrupt_context_list_t *list,
|
---|
| 125 | interrupt_context_t *ctx)
|
---|
| 126 | {
|
---|
| 127 | fibril_mutex_lock(&list->mutex);
|
---|
| 128 | list_remove(&ctx->link);
|
---|
| 129 | fibril_mutex_unlock(&list->mutex);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | interrupt_context_t *
|
---|
| 133 | find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
|
---|
| 134 | {
|
---|
| 135 | fibril_mutex_lock(&list->mutex);
|
---|
| 136 |
|
---|
| 137 | link_t *link = list->contexts.next;
|
---|
| 138 | interrupt_context_t *ctx;
|
---|
| 139 |
|
---|
| 140 | while (link != &list->contexts) {
|
---|
| 141 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
| 142 | if (ctx->id == id) {
|
---|
| 143 | fibril_mutex_unlock(&list->mutex);
|
---|
| 144 | return ctx;
|
---|
| 145 | }
|
---|
| 146 | link = link->next;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | fibril_mutex_unlock(&list->mutex);
|
---|
| 150 | return NULL;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | interrupt_context_t *
|
---|
| 154 | find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)
|
---|
| 155 | {
|
---|
| 156 | fibril_mutex_lock(&list->mutex);
|
---|
| 157 |
|
---|
| 158 | link_t *link = list->contexts.next;
|
---|
| 159 | interrupt_context_t *ctx;
|
---|
| 160 |
|
---|
| 161 | while (link != &list->contexts) {
|
---|
| 162 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
| 163 | if (ctx->irq == irq && ctx->dev == dev) {
|
---|
| 164 | fibril_mutex_unlock(&list->mutex);
|
---|
| 165 | return ctx;
|
---|
| 166 | }
|
---|
| 167 | link = link->next;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | fibril_mutex_unlock(&list->mutex);
|
---|
| 171 | return NULL;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 |
|
---|
[7a252ec8] | 175 | int
|
---|
| 176 | register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler,
|
---|
| 177 | irq_code_t *pseudocode)
|
---|
[7f8b581] | 178 | {
|
---|
| 179 | interrupt_context_t *ctx = create_interrupt_context();
|
---|
| 180 |
|
---|
| 181 | ctx->dev = dev;
|
---|
| 182 | ctx->irq = irq;
|
---|
| 183 | ctx->handler = handler;
|
---|
| 184 |
|
---|
| 185 | add_interrupt_context(&interrupt_contexts, ctx);
|
---|
| 186 |
|
---|
[36f2b3e] | 187 | if (pseudocode == NULL)
|
---|
[7f8b581] | 188 | pseudocode = &default_pseudocode;
|
---|
| 189 |
|
---|
[ffa2c8ef] | 190 | int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
|
---|
[36f2b3e] | 191 | if (res != EOK) {
|
---|
[7f8b581] | 192 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
| 193 | delete_interrupt_context(ctx);
|
---|
| 194 | }
|
---|
[7a252ec8] | 195 |
|
---|
| 196 | return res;
|
---|
[7f8b581] | 197 | }
|
---|
| 198 |
|
---|
| 199 | int unregister_interrupt_handler(device_t *dev, int irq)
|
---|
| 200 | {
|
---|
[7a252ec8] | 201 | interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
|
---|
| 202 | dev, irq);
|
---|
[ffa2c8ef] | 203 | int res = unregister_irq(irq, dev->handle);
|
---|
[36f2b3e] | 204 |
|
---|
| 205 | if (ctx != NULL) {
|
---|
[7f8b581] | 206 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
[7a252ec8] | 207 | delete_interrupt_context(ctx);
|
---|
[7f8b581] | 208 | }
|
---|
[36f2b3e] | 209 |
|
---|
[7f8b581] | 210 | return res;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[8b1e15ac] | 213 | static void add_to_functions_list(function_t *fun)
|
---|
[9a66bc2e] | 214 | {
|
---|
[8b1e15ac] | 215 | fibril_mutex_lock(&functions_mutex);
|
---|
| 216 | list_append(&fun->link, &functions);
|
---|
| 217 | fibril_mutex_unlock(&functions_mutex);
|
---|
[9a66bc2e] | 218 | }
|
---|
| 219 |
|
---|
[8b1e15ac] | 220 | static void remove_from_functions_list(function_t *fun)
|
---|
[9a66bc2e] | 221 | {
|
---|
[8b1e15ac] | 222 | fibril_mutex_lock(&functions_mutex);
|
---|
| 223 | list_remove(&fun->link);
|
---|
| 224 | fibril_mutex_unlock(&functions_mutex);
|
---|
[9a66bc2e] | 225 | }
|
---|
| 226 |
|
---|
[8b1e15ac] | 227 | static function_t *driver_get_function(link_t *functions, devman_handle_t handle)
|
---|
[52b7b1bb] | 228 | {
|
---|
[8b1e15ac] | 229 | function_t *fun = NULL;
|
---|
| 230 | printf("driver_get_function handle=%" PRIun "\n", handle);
|
---|
| 231 |
|
---|
| 232 | fibril_mutex_lock(&functions_mutex);
|
---|
| 233 | link_t *link = functions->next;
|
---|
| 234 |
|
---|
| 235 | while (link != functions) {
|
---|
| 236 | fun = list_get_instance(link, function_t, link);
|
---|
| 237 | printf(" - fun handle %" PRIun "\n", fun->handle);
|
---|
| 238 | if (fun->handle == handle) {
|
---|
| 239 | fibril_mutex_unlock(&functions_mutex);
|
---|
| 240 | return fun;
|
---|
[a1769ee] | 241 | }
|
---|
[8b1e15ac] | 242 |
|
---|
[9a66bc2e] | 243 | link = link->next;
|
---|
[a1769ee] | 244 | }
|
---|
[36f2b3e] | 245 |
|
---|
[8b1e15ac] | 246 | fibril_mutex_unlock(&functions_mutex);
|
---|
[36f2b3e] | 247 |
|
---|
[a1769ee] | 248 | return NULL;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[52b7b1bb] | 251 | static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[084ff99] | 252 | {
|
---|
[df747b9c] | 253 | char *dev_name = NULL;
|
---|
[36f2b3e] | 254 | int res;
|
---|
[9a66bc2e] | 255 |
|
---|
[d35ac1d] | 256 | devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
---|
[8b1e15ac] | 257 | devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
|
---|
[d35ac1d] | 258 |
|
---|
[5af21c5] | 259 | device_t *dev = create_device();
|
---|
[084ff99] | 260 | dev->handle = dev_handle;
|
---|
[8b1e15ac] | 261 |
|
---|
[7a252ec8] | 262 | async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
|
---|
[df747b9c] | 263 | dev->name = dev_name;
|
---|
[8b1e15ac] | 264 |
|
---|
| 265 | /*
|
---|
| 266 | * Currently not used, parent fun handle is stored in context
|
---|
| 267 | * of the connection to the parent device driver.
|
---|
| 268 | */
|
---|
| 269 | (void) parent_fun_handle;
|
---|
[0d6915f] | 270 |
|
---|
[df747b9c] | 271 | res = driver->driver_ops->add_device(dev);
|
---|
[36f2b3e] | 272 | if (res == EOK) {
|
---|
[7e752b2] | 273 | printf("%s: new device with handle=%" PRIun " was added.\n",
|
---|
[7a252ec8] | 274 | driver->name, dev_handle);
|
---|
[9a66bc2e] | 275 | } else {
|
---|
[7e752b2] | 276 | printf("%s: failed to add a new device with handle = %" PRIun ".\n",
|
---|
[7a252ec8] | 277 | driver->name, dev_handle);
|
---|
| 278 | delete_device(dev);
|
---|
[084ff99] | 279 | }
|
---|
[df747b9c] | 280 |
|
---|
[ffa2c8ef] | 281 | async_answer_0(iid, res);
|
---|
[084ff99] | 282 | }
|
---|
[c16cf62] | 283 |
|
---|
| 284 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 285 | {
|
---|
| 286 | /* Accept connection */
|
---|
[ffa2c8ef] | 287 | async_answer_0(iid, EOK);
|
---|
[36f2b3e] | 288 |
|
---|
[c16cf62] | 289 | bool cont = true;
|
---|
| 290 | while (cont) {
|
---|
| 291 | ipc_call_t call;
|
---|
| 292 | ipc_callid_t callid = async_get_call(&call);
|
---|
[36f2b3e] | 293 |
|
---|
[228e490] | 294 | switch (IPC_GET_IMETHOD(call)) {
|
---|
[c16cf62] | 295 | case IPC_M_PHONE_HUNGUP:
|
---|
| 296 | cont = false;
|
---|
| 297 | continue;
|
---|
| 298 | case DRIVER_ADD_DEVICE:
|
---|
[084ff99] | 299 | driver_add_device(callid, &call);
|
---|
[c16cf62] | 300 | break;
|
---|
| 301 | default:
|
---|
[ffa2c8ef] | 302 | async_answer_0(callid, ENOENT);
|
---|
[c16cf62] | 303 | }
|
---|
[52b7b1bb] | 304 | }
|
---|
[c16cf62] | 305 | }
|
---|
| 306 |
|
---|
[52b7b1bb] | 307 | /**
|
---|
[a1769ee] | 308 | * Generic client connection handler both for applications and drivers.
|
---|
[52b7b1bb] | 309 | *
|
---|
[7a252ec8] | 310 | * @param drv True for driver client, false for other clients
|
---|
| 311 | * (applications, services etc.).
|
---|
[a1769ee] | 312 | */
|
---|
[9a66bc2e] | 313 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
---|
[52b7b1bb] | 314 | {
|
---|
[7a252ec8] | 315 | /*
|
---|
| 316 | * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
|
---|
| 317 | * the device to which the client connected.
|
---|
| 318 | */
|
---|
[0b5a4131] | 319 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
[8b1e15ac] | 320 | function_t *fun = driver_get_function(&functions, handle);
|
---|
[52b7b1bb] | 321 |
|
---|
[8b1e15ac] | 322 | if (fun == NULL) {
|
---|
| 323 | printf("%s: driver_connection_gen error - no function with handle"
|
---|
[7e752b2] | 324 | " %" PRIun " was found.\n", driver->name, handle);
|
---|
[ffa2c8ef] | 325 | async_answer_0(iid, ENOENT);
|
---|
[a1769ee] | 326 | return;
|
---|
| 327 | }
|
---|
[5cd136ab] | 328 |
|
---|
| 329 |
|
---|
[7a252ec8] | 330 | /*
|
---|
| 331 | * TODO - if the client is not a driver, check whether it is allowed to
|
---|
| 332 | * use the device.
|
---|
| 333 | */
|
---|
[36f2b3e] | 334 |
|
---|
[25a7e11d] | 335 | int ret = EOK;
|
---|
[8b1e15ac] | 336 | /* Open device function */
|
---|
| 337 | if (fun->ops != NULL && fun->ops->open != NULL)
|
---|
| 338 | ret = (*fun->ops->open)(fun);
|
---|
[a1769ee] | 339 |
|
---|
[ffa2c8ef] | 340 | async_answer_0(iid, ret);
|
---|
[36f2b3e] | 341 | if (ret != EOK)
|
---|
[a6e54c5d] | 342 | return;
|
---|
[36f2b3e] | 343 |
|
---|
[a1769ee] | 344 | while (1) {
|
---|
| 345 | ipc_callid_t callid;
|
---|
| 346 | ipc_call_t call;
|
---|
| 347 | callid = async_get_call(&call);
|
---|
[228e490] | 348 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
[3843ecb] | 349 | int iface_idx;
|
---|
| 350 |
|
---|
[a1769ee] | 351 | switch (method) {
|
---|
[d35ac1d] | 352 | case IPC_M_PHONE_HUNGUP:
|
---|
[8b1e15ac] | 353 | /* Close device function */
|
---|
| 354 | if (fun->ops != NULL && fun->ops->close != NULL)
|
---|
| 355 | (*fun->ops->close)(fun);
|
---|
[ffa2c8ef] | 356 | async_answer_0(callid, EOK);
|
---|
[a1769ee] | 357 | return;
|
---|
[d35ac1d] | 358 | default:
|
---|
[7a252ec8] | 359 | /* convert ipc interface id to interface index */
|
---|
[3843ecb] | 360 |
|
---|
| 361 | iface_idx = DEV_IFACE_IDX(method);
|
---|
| 362 |
|
---|
| 363 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
[7a252ec8] | 364 | remote_handler_t *default_handler =
|
---|
[8b1e15ac] | 365 | function_get_default_handler(fun);
|
---|
[36f2b3e] | 366 | if (default_handler != NULL) {
|
---|
[8b1e15ac] | 367 | (*default_handler)(fun, callid, &call);
|
---|
[08d9525a] | 368 | break;
|
---|
| 369 | }
|
---|
[8b1e15ac] | 370 |
|
---|
[7a252ec8] | 371 | /*
|
---|
[8b1e15ac] | 372 | * Function has no such interface and
|
---|
[7a252ec8] | 373 | * default handler is not provided.
|
---|
| 374 | */
|
---|
| 375 | printf("%s: driver_connection_gen error - "
|
---|
| 376 | "invalid interface id %d.",
|
---|
| 377 | driver->name, iface_idx);
|
---|
[ffa2c8ef] | 378 | async_answer_0(callid, ENOTSUP);
|
---|
[52b7b1bb] | 379 | break;
|
---|
| 380 | }
|
---|
[36f2b3e] | 381 |
|
---|
[8b1e15ac] | 382 | /* calling one of the function's interfaces */
|
---|
[52b7b1bb] | 383 |
|
---|
[d35ac1d] | 384 | /* Get the interface ops structure. */
|
---|
[8b1e15ac] | 385 | void *ops = function_get_ops(fun, iface_idx);
|
---|
[d35ac1d] | 386 | if (ops == NULL) {
|
---|
[7a252ec8] | 387 | printf("%s: driver_connection_gen error - ",
|
---|
| 388 | driver->name);
|
---|
[8b1e15ac] | 389 | printf("Function with handle %" PRIun " has no interface "
|
---|
[7a252ec8] | 390 | "with id %d.\n", handle, iface_idx);
|
---|
[ffa2c8ef] | 391 | async_answer_0(callid, ENOTSUP);
|
---|
[52b7b1bb] | 392 | break;
|
---|
[a1769ee] | 393 | }
|
---|
[36f2b3e] | 394 |
|
---|
[7a252ec8] | 395 | /*
|
---|
| 396 | * Get the corresponding interface for remote request
|
---|
| 397 | * handling ("remote interface").
|
---|
| 398 | */
|
---|
[d35ac1d] | 399 | remote_iface_t *rem_iface = get_remote_iface(iface_idx);
|
---|
[36f2b3e] | 400 | assert(rem_iface != NULL);
|
---|
| 401 |
|
---|
[7a252ec8] | 402 | /* get the method of the remote interface */
|
---|
[96b02eb9] | 403 | sysarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
[7a252ec8] | 404 | remote_iface_func_ptr_t iface_method_ptr =
|
---|
| 405 | get_remote_method(rem_iface, iface_method_idx);
|
---|
[36f2b3e] | 406 | if (iface_method_ptr == NULL) {
|
---|
[52b7b1bb] | 407 | // the interface has not such method
|
---|
[7a252ec8] | 408 | printf("%s: driver_connection_gen error - "
|
---|
| 409 | "invalid interface method.", driver->name);
|
---|
[ffa2c8ef] | 410 | async_answer_0(callid, ENOTSUP);
|
---|
[52b7b1bb] | 411 | break;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[7a252ec8] | 414 | /*
|
---|
| 415 | * Call the remote interface's method, which will
|
---|
| 416 | * receive parameters from the remote client and it will
|
---|
| 417 | * pass it to the corresponding local interface method
|
---|
[8b1e15ac] | 418 | * associated with the function by its driver.
|
---|
[7a252ec8] | 419 | */
|
---|
[8b1e15ac] | 420 | (*iface_method_ptr)(fun, ops, callid, &call);
|
---|
[a1769ee] | 421 | break;
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 |
|
---|
[c16cf62] | 426 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 427 | {
|
---|
[52b7b1bb] | 428 | driver_connection_gen(iid, icall, true);
|
---|
[c16cf62] | 429 | }
|
---|
| 430 |
|
---|
| 431 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 432 | {
|
---|
[52b7b1bb] | 433 | driver_connection_gen(iid, icall, false);
|
---|
[c16cf62] | 434 | }
|
---|
| 435 |
|
---|
[7a252ec8] | 436 | /** Function for handling connections to device driver. */
|
---|
[c16cf62] | 437 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 438 | {
|
---|
| 439 | /* Select interface */
|
---|
[96b02eb9] | 440 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
[c16cf62] | 441 | case DRIVER_DEVMAN:
|
---|
[36f2b3e] | 442 | /* Handle request from device manager */
|
---|
[c16cf62] | 443 | driver_connection_devman(iid, icall);
|
---|
| 444 | break;
|
---|
| 445 | case DRIVER_DRIVER:
|
---|
[36f2b3e] | 446 | /* Handle request from drivers of child devices */
|
---|
[c16cf62] | 447 | driver_connection_driver(iid, icall);
|
---|
| 448 | break;
|
---|
| 449 | case DRIVER_CLIENT:
|
---|
[36f2b3e] | 450 | /* Handle request from client applications */
|
---|
[c16cf62] | 451 | driver_connection_client(iid, icall);
|
---|
| 452 | break;
|
---|
| 453 | default:
|
---|
[52b7b1bb] | 454 | /* No such interface */
|
---|
[ffa2c8ef] | 455 | async_answer_0(iid, ENOENT);
|
---|
[c16cf62] | 456 | }
|
---|
| 457 | }
|
---|
| 458 |
|
---|
[5fdd7c3] | 459 | /** Create new device structure.
|
---|
| 460 | *
|
---|
| 461 | * @return The device structure.
|
---|
| 462 | */
|
---|
[7df0477e] | 463 | static device_t *create_device(void)
|
---|
[5fdd7c3] | 464 | {
|
---|
[8b1e15ac] | 465 | device_t *dev;
|
---|
[5fdd7c3] | 466 |
|
---|
[8b1e15ac] | 467 | dev = malloc(sizeof(device_t));
|
---|
| 468 | if (dev == NULL)
|
---|
| 469 | return NULL;
|
---|
[5fdd7c3] | 470 |
|
---|
[8b1e15ac] | 471 | memset(dev, 0, sizeof(device_t));
|
---|
[5fdd7c3] | 472 | return dev;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[8b1e15ac] | 475 | /** Create new function structure.
|
---|
| 476 | *
|
---|
| 477 | * @return The device structure.
|
---|
| 478 | */
|
---|
[97a62fe] | 479 | static function_t *create_function(void)
|
---|
[8b1e15ac] | 480 | {
|
---|
| 481 | function_t *fun;
|
---|
| 482 |
|
---|
[97a62fe] | 483 | fun = calloc(1, sizeof(function_t));
|
---|
[8b1e15ac] | 484 | if (fun == NULL)
|
---|
| 485 | return NULL;
|
---|
| 486 |
|
---|
| 487 | init_match_ids(&fun->match_ids);
|
---|
| 488 | link_initialize(&fun->link);
|
---|
| 489 |
|
---|
| 490 | return fun;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[5fdd7c3] | 493 | /** Delete device structure.
|
---|
| 494 | *
|
---|
| 495 | * @param dev The device structure.
|
---|
| 496 | */
|
---|
[7df0477e] | 497 | static void delete_device(device_t *dev)
|
---|
[5fdd7c3] | 498 | {
|
---|
| 499 | free(dev);
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[8b1e15ac] | 502 | /** Delete device structure.
|
---|
| 503 | *
|
---|
| 504 | * @param dev The device structure.
|
---|
| 505 | */
|
---|
[97a62fe] | 506 | static void delete_function(function_t *fun)
|
---|
[8b1e15ac] | 507 | {
|
---|
| 508 | clean_match_ids(&fun->match_ids);
|
---|
| 509 | if (fun->name != NULL)
|
---|
| 510 | free(fun->name);
|
---|
| 511 | free(fun);
|
---|
| 512 | }
|
---|
| 513 |
|
---|
[97a62fe] | 514 | /** Create a DDF function node.
|
---|
| 515 | *
|
---|
| 516 | * Create a DDF function (in memory). Both child devices and external clients
|
---|
| 517 | * communicate with a device via its functions.
|
---|
| 518 | *
|
---|
| 519 | * The created function node is fully formed, but only exists in the memory
|
---|
| 520 | * of the client task. In order to be visible to the system, the function
|
---|
| 521 | * must be bound using ddf_fun_bind().
|
---|
| 522 | *
|
---|
| 523 | * This function should only fail if there is not enough free memory.
|
---|
| 524 | * Specifically, this function succeeds even if @a dev already has
|
---|
| 525 | * a (bound) function with the same name.
|
---|
| 526 | *
|
---|
| 527 | * Type: A function of type fun_inner indicates that DDF should attempt
|
---|
| 528 | * to attach child devices to the function. fun_exposed means that
|
---|
| 529 | * the function should be exported to external clients (applications).
|
---|
| 530 | *
|
---|
| 531 | * @param dev Device to which we are adding function
|
---|
| 532 | * @param ftype Type of function (fun_inner or fun_exposed)
|
---|
| 533 | * @param name Name of function
|
---|
| 534 | *
|
---|
| 535 | * @return New function or @c NULL if memory is not available
|
---|
| 536 | */
|
---|
| 537 | function_t *ddf_fun_create(device_t *dev, fun_type_t ftype, const char *name)
|
---|
| 538 | {
|
---|
| 539 | function_t *fun;
|
---|
| 540 |
|
---|
| 541 | fun = create_function();
|
---|
| 542 | if (fun == NULL)
|
---|
| 543 | return NULL;
|
---|
| 544 |
|
---|
| 545 | fun->bound = false;
|
---|
| 546 | fun->dev = dev;
|
---|
| 547 | fun->ftype = ftype;
|
---|
| 548 |
|
---|
| 549 | fun->name = str_dup(name);
|
---|
| 550 | if (fun->name == NULL) {
|
---|
| 551 | delete_function(fun);
|
---|
| 552 | return NULL;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | return fun;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | /** Destroy DDF function node.
|
---|
| 559 | *
|
---|
| 560 | * Destroy a function previously created with ddf_fun_create(). The function
|
---|
| 561 | * must not be bound.
|
---|
| 562 | *
|
---|
| 563 | * @param fun Function to destroy
|
---|
| 564 | */
|
---|
| 565 | void ddf_fun_destroy(function_t *fun)
|
---|
| 566 | {
|
---|
| 567 | assert(fun->bound == false);
|
---|
| 568 | delete_function(fun);
|
---|
| 569 | }
|
---|
| 570 |
|
---|
[8b1e15ac] | 571 | void *function_get_ops(function_t *fun, dev_inferface_idx_t idx)
|
---|
[5fdd7c3] | 572 | {
|
---|
| 573 | assert(is_valid_iface_idx(idx));
|
---|
[8b1e15ac] | 574 | if (fun->ops == NULL)
|
---|
[5fdd7c3] | 575 | return NULL;
|
---|
[8b1e15ac] | 576 | return fun->ops->interfaces[idx];
|
---|
[5fdd7c3] | 577 | }
|
---|
| 578 |
|
---|
[97a62fe] | 579 | /** Bind a function node.
|
---|
| 580 | *
|
---|
| 581 | * Bind the specified function to the system. This effectively makes
|
---|
| 582 | * the function visible to the system (uploads it to the server).
|
---|
| 583 | *
|
---|
| 584 | * This function can fail for several reasons. Specifically,
|
---|
| 585 | * it will fail if the device already has a bound function of
|
---|
| 586 | * the same name.
|
---|
| 587 | *
|
---|
| 588 | * @param fun Function to bind
|
---|
| 589 | * @return EOK on success or negative error code
|
---|
| 590 | */
|
---|
| 591 | int ddf_fun_bind(function_t *fun)
|
---|
[7707954] | 592 | {
|
---|
[8b1e15ac] | 593 | assert(fun->name != NULL);
|
---|
[36f2b3e] | 594 |
|
---|
[df747b9c] | 595 | int res;
|
---|
| 596 |
|
---|
[8b1e15ac] | 597 | add_to_functions_list(fun);
|
---|
| 598 | res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
|
---|
[97a62fe] | 599 | fun->dev->handle, &fun->handle);
|
---|
[36f2b3e] | 600 | if (res != EOK) {
|
---|
[8b1e15ac] | 601 | remove_from_functions_list(fun);
|
---|
[df747b9c] | 602 | return res;
|
---|
[36f2b3e] | 603 | }
|
---|
| 604 |
|
---|
[97a62fe] | 605 | fun->bound = true;
|
---|
[df747b9c] | 606 | return res;
|
---|
[7707954] | 607 | }
|
---|
| 608 |
|
---|
[0ca16307] | 609 | /** Wrapper for child_device_register for devices with single match id.
|
---|
| 610 | *
|
---|
| 611 | * @param parent Parent device.
|
---|
| 612 | * @param child_name Child device name.
|
---|
| 613 | * @param child_match_id Child device match id.
|
---|
| 614 | * @param child_match_score Child device match score.
|
---|
| 615 | * @return Error code.
|
---|
| 616 | */
|
---|
[8b1e15ac] | 617 | int register_function_wrapper(device_t *dev, const char *fun_name,
|
---|
| 618 | const char *match_id, int match_score)
|
---|
[0ca16307] | 619 | {
|
---|
[8b1e15ac] | 620 | function_t *fun = NULL;
|
---|
| 621 | match_id_t *m_id = NULL;
|
---|
[0ca16307] | 622 | int rc;
|
---|
[36f2b3e] | 623 |
|
---|
[97a62fe] | 624 | fun = ddf_fun_create(dev, fun_inner, fun_name);
|
---|
[8b1e15ac] | 625 | if (fun == NULL) {
|
---|
[0ca16307] | 626 | rc = ENOMEM;
|
---|
| 627 | goto failure;
|
---|
| 628 | }
|
---|
[36f2b3e] | 629 |
|
---|
[8b1e15ac] | 630 | m_id = create_match_id();
|
---|
| 631 | if (m_id == NULL) {
|
---|
[0ca16307] | 632 | rc = ENOMEM;
|
---|
| 633 | goto failure;
|
---|
| 634 | }
|
---|
[36f2b3e] | 635 |
|
---|
[8b1e15ac] | 636 | m_id->id = match_id;
|
---|
| 637 | m_id->score = match_score;
|
---|
| 638 | add_match_id(&fun->match_ids, m_id);
|
---|
[36f2b3e] | 639 |
|
---|
[97a62fe] | 640 | rc = ddf_fun_bind(fun);
|
---|
[36f2b3e] | 641 | if (rc != EOK)
|
---|
[0ca16307] | 642 | goto failure;
|
---|
[36f2b3e] | 643 |
|
---|
[4006447] | 644 | return EOK;
|
---|
[36f2b3e] | 645 |
|
---|
[0ca16307] | 646 | failure:
|
---|
[8b1e15ac] | 647 | if (m_id != NULL) {
|
---|
| 648 | m_id->id = NULL;
|
---|
| 649 | delete_match_id(m_id);
|
---|
[0ca16307] | 650 | }
|
---|
[36f2b3e] | 651 |
|
---|
[8b1e15ac] | 652 | if (fun != NULL) {
|
---|
| 653 | fun->name = NULL;
|
---|
| 654 | delete_function(fun);
|
---|
[0ca16307] | 655 | }
|
---|
[36f2b3e] | 656 |
|
---|
[0ca16307] | 657 | return rc;
|
---|
| 658 | }
|
---|
| 659 |
|
---|
[5fdd7c3] | 660 | /** Get default handler for client requests */
|
---|
[8b1e15ac] | 661 | remote_handler_t *function_get_default_handler(function_t *fun)
|
---|
[5fdd7c3] | 662 | {
|
---|
[8b1e15ac] | 663 | if (fun->ops == NULL)
|
---|
[5fdd7c3] | 664 | return NULL;
|
---|
[8b1e15ac] | 665 | return fun->ops->default_handler;
|
---|
[5fdd7c3] | 666 | }
|
---|
| 667 |
|
---|
[8b1e15ac] | 668 | int add_function_to_class(function_t *fun, const char *class_name)
|
---|
[5fdd7c3] | 669 | {
|
---|
[8b1e15ac] | 670 | return devman_add_device_to_class(fun->handle, class_name);
|
---|
[5fdd7c3] | 671 | }
|
---|
| 672 |
|
---|
[52b7b1bb] | 673 | int driver_main(driver_t *drv)
|
---|
[c16cf62] | 674 | {
|
---|
[7a252ec8] | 675 | /*
|
---|
| 676 | * Remember the driver structure - driver_ops will be called by generic
|
---|
| 677 | * handler for incoming connections.
|
---|
| 678 | */
|
---|
[c16cf62] | 679 | driver = drv;
|
---|
[36f2b3e] | 680 |
|
---|
[7a252ec8] | 681 | /* Initialize the list of interrupt contexts. */
|
---|
[7f8b581] | 682 | init_interrupt_context_list(&interrupt_contexts);
|
---|
| 683 |
|
---|
[7a252ec8] | 684 | /* Set generic interrupt handler. */
|
---|
[7f8b581] | 685 | async_set_interrupt_received(driver_irq_handler);
|
---|
| 686 |
|
---|
[7a252ec8] | 687 | /*
|
---|
| 688 | * Register driver by device manager with generic handler for incoming
|
---|
| 689 | * connections.
|
---|
| 690 | */
|
---|
[52b7b1bb] | 691 | devman_driver_register(driver->name, driver_connection);
|
---|
[36f2b3e] | 692 |
|
---|
[c16cf62] | 693 | async_manager();
|
---|
[36f2b3e] | 694 |
|
---|
[7a252ec8] | 695 | /* Never reached. */
|
---|
[52b7b1bb] | 696 | return 0;
|
---|
[c16cf62] | 697 | }
|
---|
| 698 |
|
---|
| 699 | /**
|
---|
| 700 | * @}
|
---|
[52b7b1bb] | 701 | */
|
---|