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