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