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