[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>
|
---|
[3e6a98c5] | 45 | #include <stdbool.h>
|
---|
[c16cf62] | 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>
|
---|
[7e752b2] | 51 | #include <inttypes.h>
|
---|
[af6b5157] | 52 | #include <devman.h>
|
---|
[c16cf62] | 53 |
|
---|
[5fdd7c3] | 54 | #include "dev_iface.h"
|
---|
[af6b5157] | 55 | #include "ddf/driver.h"
|
---|
| 56 | #include "ddf/interrupt.h"
|
---|
[56fd7cf] | 57 | #include "private/driver.h"
|
---|
[c16cf62] | 58 |
|
---|
[36f2b3e] | 59 | /** Driver structure */
|
---|
[0c322fa] | 60 | static const driver_t *driver;
|
---|
[7f8b581] | 61 |
|
---|
[36f2b3e] | 62 | /** Devices */
|
---|
[1a5b252] | 63 | LIST_INITIALIZE(devices);
|
---|
| 64 | FIBRIL_MUTEX_INITIALIZE(devices_mutex);
|
---|
| 65 |
|
---|
| 66 | /** Functions */
|
---|
[8b1e15ac] | 67 | LIST_INITIALIZE(functions);
|
---|
| 68 | FIBRIL_MUTEX_INITIALIZE(functions_mutex);
|
---|
[084ff99] | 69 |
|
---|
[81685dd9] | 70 | FIBRIL_RWLOCK_INITIALIZE(stopping_lock);
|
---|
| 71 | static bool stopping = false;
|
---|
| 72 |
|
---|
[83a2f43] | 73 | static ddf_dev_t *create_device(void);
|
---|
| 74 | static void delete_device(ddf_dev_t *);
|
---|
[0f0f8bc] | 75 | static void dev_add_ref(ddf_dev_t *);
|
---|
| 76 | static void dev_del_ref(ddf_dev_t *);
|
---|
| 77 | static void fun_add_ref(ddf_fun_t *);
|
---|
| 78 | static void fun_del_ref(ddf_fun_t *);
|
---|
[af6b5157] | 79 | static remote_handler_t *function_get_default_handler(ddf_fun_t *);
|
---|
| 80 | static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
|
---|
[7f8b581] | 81 |
|
---|
[83a2f43] | 82 | static void add_to_functions_list(ddf_fun_t *fun)
|
---|
[9a66bc2e] | 83 | {
|
---|
[8b1e15ac] | 84 | fibril_mutex_lock(&functions_mutex);
|
---|
| 85 | list_append(&fun->link, &functions);
|
---|
| 86 | fibril_mutex_unlock(&functions_mutex);
|
---|
[9a66bc2e] | 87 | }
|
---|
| 88 |
|
---|
[83a2f43] | 89 | static void remove_from_functions_list(ddf_fun_t *fun)
|
---|
[9a66bc2e] | 90 | {
|
---|
[8b1e15ac] | 91 | fibril_mutex_lock(&functions_mutex);
|
---|
| 92 | list_remove(&fun->link);
|
---|
| 93 | fibril_mutex_unlock(&functions_mutex);
|
---|
[9a66bc2e] | 94 | }
|
---|
| 95 |
|
---|
[1a5b252] | 96 | static ddf_dev_t *driver_get_device(devman_handle_t handle)
|
---|
| 97 | {
|
---|
| 98 | assert(fibril_mutex_is_locked(&devices_mutex));
|
---|
[a35b458] | 99 |
|
---|
[feeac0d] | 100 | list_foreach(devices, link, ddf_dev_t, dev) {
|
---|
[1a5b252] | 101 | if (dev->handle == handle)
|
---|
| 102 | return dev;
|
---|
| 103 | }
|
---|
[a35b458] | 104 |
|
---|
[1a5b252] | 105 | return NULL;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | static ddf_fun_t *driver_get_function(devman_handle_t handle)
|
---|
[52b7b1bb] | 109 | {
|
---|
[1a5b252] | 110 | assert(fibril_mutex_is_locked(&functions_mutex));
|
---|
[a35b458] | 111 |
|
---|
[feeac0d] | 112 | list_foreach(functions, link, ddf_fun_t, fun) {
|
---|
[1a5b252] | 113 | if (fun->handle == handle)
|
---|
[8b1e15ac] | 114 | return fun;
|
---|
[a1769ee] | 115 | }
|
---|
[a35b458] | 116 |
|
---|
[a1769ee] | 117 | return NULL;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[0f0f8bc] | 120 | static void driver_dev_add(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[084ff99] | 121 | {
|
---|
[d35ac1d] | 122 | devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
---|
[818fffe] | 123 | devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
|
---|
[a35b458] | 124 |
|
---|
[77ad86c] | 125 | char *dev_name = NULL;
|
---|
[5a6cc679] | 126 | errno_t rc = async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
|
---|
[f9b2cb4c] | 127 | if (rc != EOK) {
|
---|
| 128 | async_answer_0(iid, rc);
|
---|
| 129 | return;
|
---|
| 130 | }
|
---|
[a35b458] | 131 |
|
---|
[81685dd9] | 132 | fibril_rwlock_read_lock(&stopping_lock);
|
---|
| 133 |
|
---|
| 134 | if (stopping) {
|
---|
| 135 | fibril_rwlock_read_unlock(&stopping_lock);
|
---|
| 136 | async_answer_0(iid, EIO);
|
---|
| 137 | return;
|
---|
| 138 | }
|
---|
[a35b458] | 139 |
|
---|
[562a48b] | 140 | ddf_dev_t *dev = create_device();
|
---|
[479cc46] | 141 | if (!dev) {
|
---|
[81685dd9] | 142 | fibril_rwlock_read_unlock(&stopping_lock);
|
---|
[479cc46] | 143 | free(dev_name);
|
---|
| 144 | async_answer_0(iid, ENOMEM);
|
---|
| 145 | return;
|
---|
| 146 | }
|
---|
[a35b458] | 147 |
|
---|
[562a48b] | 148 | /* Add one reference that will be dropped by driver_dev_remove() */
|
---|
| 149 | dev_add_ref(dev);
|
---|
| 150 | dev->handle = dev_handle;
|
---|
[df747b9c] | 151 | dev->name = dev_name;
|
---|
[a35b458] | 152 |
|
---|
[8b1e15ac] | 153 | /*
|
---|
| 154 | * Currently not used, parent fun handle is stored in context
|
---|
| 155 | * of the connection to the parent device driver.
|
---|
| 156 | */
|
---|
| 157 | (void) parent_fun_handle;
|
---|
[a35b458] | 158 |
|
---|
[5a6cc679] | 159 | errno_t res = driver->driver_ops->dev_add(dev);
|
---|
[a35b458] | 160 |
|
---|
[0f0f8bc] | 161 | if (res != EOK) {
|
---|
[81685dd9] | 162 | fibril_rwlock_read_unlock(&stopping_lock);
|
---|
[0f0f8bc] | 163 | dev_del_ref(dev);
|
---|
| 164 | async_answer_0(iid, res);
|
---|
| 165 | return;
|
---|
| 166 | }
|
---|
[a35b458] | 167 |
|
---|
[1a5b252] | 168 | fibril_mutex_lock(&devices_mutex);
|
---|
| 169 | list_append(&dev->link, &devices);
|
---|
| 170 | fibril_mutex_unlock(&devices_mutex);
|
---|
[81685dd9] | 171 | fibril_rwlock_read_unlock(&stopping_lock);
|
---|
[a35b458] | 172 |
|
---|
[ffa2c8ef] | 173 | async_answer_0(iid, res);
|
---|
[084ff99] | 174 | }
|
---|
[c16cf62] | 175 |
|
---|
[1a5b252] | 176 | static void driver_dev_remove(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 177 | {
|
---|
[77ad86c] | 178 | devman_handle_t devh = IPC_GET_ARG1(*icall);
|
---|
[a35b458] | 179 |
|
---|
[1a5b252] | 180 | fibril_mutex_lock(&devices_mutex);
|
---|
[77ad86c] | 181 | ddf_dev_t *dev = driver_get_device(devh);
|
---|
[f278930] | 182 | if (dev != NULL)
|
---|
| 183 | dev_add_ref(dev);
|
---|
[1a5b252] | 184 | fibril_mutex_unlock(&devices_mutex);
|
---|
[a35b458] | 185 |
|
---|
[1a5b252] | 186 | if (dev == NULL) {
|
---|
| 187 | async_answer_0(iid, ENOENT);
|
---|
| 188 | return;
|
---|
| 189 | }
|
---|
[a35b458] | 190 |
|
---|
[5a6cc679] | 191 | errno_t rc;
|
---|
[a35b458] | 192 |
|
---|
[1a5b252] | 193 | if (driver->driver_ops->dev_remove != NULL)
|
---|
| 194 | rc = driver->driver_ops->dev_remove(dev);
|
---|
| 195 | else
|
---|
| 196 | rc = ENOTSUP;
|
---|
[a35b458] | 197 |
|
---|
[702729e] | 198 | if (rc == EOK) {
|
---|
| 199 | fibril_mutex_lock(&devices_mutex);
|
---|
| 200 | list_remove(&dev->link);
|
---|
| 201 | fibril_mutex_unlock(&devices_mutex);
|
---|
[0f0f8bc] | 202 | dev_del_ref(dev);
|
---|
[702729e] | 203 | }
|
---|
[a35b458] | 204 |
|
---|
[203b80c] | 205 | dev_del_ref(dev);
|
---|
[25a179e] | 206 | async_answer_0(iid, rc);
|
---|
[1a5b252] | 207 | }
|
---|
| 208 |
|
---|
[80a96d2] | 209 | static void driver_dev_gone(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 210 | {
|
---|
[77ad86c] | 211 | devman_handle_t devh = IPC_GET_ARG1(*icall);
|
---|
[a35b458] | 212 |
|
---|
[80a96d2] | 213 | fibril_mutex_lock(&devices_mutex);
|
---|
[77ad86c] | 214 | ddf_dev_t *dev = driver_get_device(devh);
|
---|
[80a96d2] | 215 | if (dev != NULL)
|
---|
| 216 | dev_add_ref(dev);
|
---|
| 217 | fibril_mutex_unlock(&devices_mutex);
|
---|
[a35b458] | 218 |
|
---|
[80a96d2] | 219 | if (dev == NULL) {
|
---|
| 220 | async_answer_0(iid, ENOENT);
|
---|
| 221 | return;
|
---|
| 222 | }
|
---|
[a35b458] | 223 |
|
---|
[5a6cc679] | 224 | errno_t rc;
|
---|
[a35b458] | 225 |
|
---|
[80a96d2] | 226 | if (driver->driver_ops->dev_gone != NULL)
|
---|
| 227 | rc = driver->driver_ops->dev_gone(dev);
|
---|
| 228 | else
|
---|
| 229 | rc = ENOTSUP;
|
---|
[a35b458] | 230 |
|
---|
[702729e] | 231 | if (rc == EOK) {
|
---|
| 232 | fibril_mutex_lock(&devices_mutex);
|
---|
| 233 | list_remove(&dev->link);
|
---|
| 234 | fibril_mutex_unlock(&devices_mutex);
|
---|
[80a96d2] | 235 | dev_del_ref(dev);
|
---|
[702729e] | 236 | }
|
---|
[a35b458] | 237 |
|
---|
[203b80c] | 238 | dev_del_ref(dev);
|
---|
[25a179e] | 239 | async_answer_0(iid, rc);
|
---|
[80a96d2] | 240 | }
|
---|
| 241 |
|
---|
[1a5b252] | 242 | static void driver_fun_online(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 243 | {
|
---|
[77ad86c] | 244 | devman_handle_t funh = IPC_GET_ARG1(*icall);
|
---|
[a35b458] | 245 |
|
---|
[0f0f8bc] | 246 | /*
|
---|
[4d94002d] | 247 | * Look the function up. Bump reference count so that
|
---|
[0f0f8bc] | 248 | * the function continues to exist until we return
|
---|
| 249 | * from the driver.
|
---|
| 250 | */
|
---|
[1a5b252] | 251 | fibril_mutex_lock(&functions_mutex);
|
---|
[a35b458] | 252 |
|
---|
[77ad86c] | 253 | ddf_fun_t *fun = driver_get_function(funh);
|
---|
[0f0f8bc] | 254 | if (fun != NULL)
|
---|
| 255 | fun_add_ref(fun);
|
---|
[a35b458] | 256 |
|
---|
[1a5b252] | 257 | fibril_mutex_unlock(&functions_mutex);
|
---|
[a35b458] | 258 |
|
---|
[1a5b252] | 259 | if (fun == NULL) {
|
---|
| 260 | async_answer_0(iid, ENOENT);
|
---|
| 261 | return;
|
---|
| 262 | }
|
---|
[a35b458] | 263 |
|
---|
[0f0f8bc] | 264 | /* Call driver entry point */
|
---|
[5a6cc679] | 265 | errno_t rc;
|
---|
[a35b458] | 266 |
|
---|
[1a5b252] | 267 | if (driver->driver_ops->fun_online != NULL)
|
---|
| 268 | rc = driver->driver_ops->fun_online(fun);
|
---|
| 269 | else
|
---|
| 270 | rc = ENOTSUP;
|
---|
[a35b458] | 271 |
|
---|
[0f0f8bc] | 272 | fun_del_ref(fun);
|
---|
[a35b458] | 273 |
|
---|
[25a179e] | 274 | async_answer_0(iid, rc);
|
---|
[1a5b252] | 275 | }
|
---|
| 276 |
|
---|
| 277 | static void driver_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 278 | {
|
---|
[77ad86c] | 279 | devman_handle_t funh = IPC_GET_ARG1(*icall);
|
---|
[a35b458] | 280 |
|
---|
[0f0f8bc] | 281 | /*
|
---|
[4d94002d] | 282 | * Look the function up. Bump reference count so that
|
---|
[0f0f8bc] | 283 | * the function continues to exist until we return
|
---|
| 284 | * from the driver.
|
---|
| 285 | */
|
---|
[1a5b252] | 286 | fibril_mutex_lock(&functions_mutex);
|
---|
[a35b458] | 287 |
|
---|
[77ad86c] | 288 | ddf_fun_t *fun = driver_get_function(funh);
|
---|
[0f0f8bc] | 289 | if (fun != NULL)
|
---|
| 290 | fun_add_ref(fun);
|
---|
[a35b458] | 291 |
|
---|
[1a5b252] | 292 | fibril_mutex_unlock(&functions_mutex);
|
---|
[a35b458] | 293 |
|
---|
[1a5b252] | 294 | if (fun == NULL) {
|
---|
| 295 | async_answer_0(iid, ENOENT);
|
---|
| 296 | return;
|
---|
| 297 | }
|
---|
[a35b458] | 298 |
|
---|
[0f0f8bc] | 299 | /* Call driver entry point */
|
---|
[5a6cc679] | 300 | errno_t rc;
|
---|
[a35b458] | 301 |
|
---|
[1a5b252] | 302 | if (driver->driver_ops->fun_offline != NULL)
|
---|
| 303 | rc = driver->driver_ops->fun_offline(fun);
|
---|
| 304 | else
|
---|
| 305 | rc = ENOTSUP;
|
---|
[a35b458] | 306 |
|
---|
[25a179e] | 307 | async_answer_0(iid, rc);
|
---|
[1a5b252] | 308 | }
|
---|
| 309 |
|
---|
[81685dd9] | 310 | static void driver_stop(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 311 | {
|
---|
| 312 | /* Prevent new devices from being added */
|
---|
| 313 | fibril_rwlock_write_lock(&stopping_lock);
|
---|
| 314 | stopping = true;
|
---|
| 315 |
|
---|
| 316 | /* Check if there are any devices */
|
---|
| 317 | fibril_mutex_lock(&devices_mutex);
|
---|
| 318 | if (list_first(&devices) != NULL) {
|
---|
| 319 | /* Devices exist, roll back */
|
---|
| 320 | fibril_mutex_unlock(&devices_mutex);
|
---|
| 321 | stopping = false;
|
---|
| 322 | fibril_rwlock_write_unlock(&stopping_lock);
|
---|
| 323 | async_answer_0(iid, EBUSY);
|
---|
| 324 | return;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | fibril_rwlock_write_unlock(&stopping_lock);
|
---|
| 328 |
|
---|
| 329 | /* There should be no functions at this point */
|
---|
| 330 | fibril_mutex_lock(&functions_mutex);
|
---|
| 331 | assert(list_first(&functions) == NULL);
|
---|
| 332 | fibril_mutex_unlock(&functions_mutex);
|
---|
| 333 |
|
---|
| 334 | /* Reply with success and terminate */
|
---|
| 335 | async_answer_0(iid, EOK);
|
---|
| 336 | exit(0);
|
---|
| 337 | }
|
---|
| 338 |
|
---|
[f9b2cb4c] | 339 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall,
|
---|
| 340 | void *arg)
|
---|
[c16cf62] | 341 | {
|
---|
| 342 | /* Accept connection */
|
---|
[ffa2c8ef] | 343 | async_answer_0(iid, EOK);
|
---|
[a35b458] | 344 |
|
---|
[79ae36dd] | 345 | while (true) {
|
---|
[c16cf62] | 346 | ipc_call_t call;
|
---|
| 347 | ipc_callid_t callid = async_get_call(&call);
|
---|
[a35b458] | 348 |
|
---|
[79ae36dd] | 349 | if (!IPC_GET_IMETHOD(call))
|
---|
| 350 | break;
|
---|
[a35b458] | 351 |
|
---|
[228e490] | 352 | switch (IPC_GET_IMETHOD(call)) {
|
---|
[1a5b252] | 353 | case DRIVER_DEV_ADD:
|
---|
[0f0f8bc] | 354 | driver_dev_add(callid, &call);
|
---|
[c16cf62] | 355 | break;
|
---|
[1a5b252] | 356 | case DRIVER_DEV_REMOVE:
|
---|
| 357 | driver_dev_remove(callid, &call);
|
---|
| 358 | break;
|
---|
[80a96d2] | 359 | case DRIVER_DEV_GONE:
|
---|
| 360 | driver_dev_gone(callid, &call);
|
---|
| 361 | break;
|
---|
[1a5b252] | 362 | case DRIVER_FUN_ONLINE:
|
---|
| 363 | driver_fun_online(callid, &call);
|
---|
| 364 | break;
|
---|
| 365 | case DRIVER_FUN_OFFLINE:
|
---|
| 366 | driver_fun_offline(callid, &call);
|
---|
| 367 | break;
|
---|
[81685dd9] | 368 | case DRIVER_STOP:
|
---|
| 369 | driver_stop(callid, &call);
|
---|
| 370 | break;
|
---|
[c16cf62] | 371 | default:
|
---|
[1a5b252] | 372 | async_answer_0(callid, ENOTSUP);
|
---|
[c16cf62] | 373 | }
|
---|
[52b7b1bb] | 374 | }
|
---|
[c16cf62] | 375 | }
|
---|
| 376 |
|
---|
[79ae36dd] | 377 | /** Generic client connection handler both for applications and drivers.
|
---|
| 378 | *
|
---|
| 379 | * @param drv True for driver client, false for other clients
|
---|
| 380 | * (applications, services, etc.).
|
---|
[52b7b1bb] | 381 | *
|
---|
[a1769ee] | 382 | */
|
---|
[9a66bc2e] | 383 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
---|
[52b7b1bb] | 384 | {
|
---|
[7a252ec8] | 385 | /*
|
---|
| 386 | * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
|
---|
| 387 | * the device to which the client connected.
|
---|
| 388 | */
|
---|
[0b5a4131] | 389 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
[1a5b252] | 390 |
|
---|
| 391 | fibril_mutex_lock(&functions_mutex);
|
---|
| 392 | ddf_fun_t *fun = driver_get_function(handle);
|
---|
[b454a22] | 393 | if (fun != NULL)
|
---|
| 394 | fun_add_ref(fun);
|
---|
[1a5b252] | 395 | fibril_mutex_unlock(&functions_mutex);
|
---|
[a35b458] | 396 |
|
---|
[8b1e15ac] | 397 | if (fun == NULL) {
|
---|
| 398 | printf("%s: driver_connection_gen error - no function with handle"
|
---|
[7e752b2] | 399 | " %" PRIun " was found.\n", driver->name, handle);
|
---|
[ffa2c8ef] | 400 | async_answer_0(iid, ENOENT);
|
---|
[a1769ee] | 401 | return;
|
---|
| 402 | }
|
---|
[a35b458] | 403 |
|
---|
[ff65e91] | 404 | if (fun->conn_handler != NULL) {
|
---|
| 405 | /* Driver has a custom connection handler. */
|
---|
| 406 | (*fun->conn_handler)(iid, icall, (void *)fun);
|
---|
[b454a22] | 407 | fun_del_ref(fun);
|
---|
[ff65e91] | 408 | return;
|
---|
| 409 | }
|
---|
[a35b458] | 410 |
|
---|
[7a252ec8] | 411 | /*
|
---|
| 412 | * TODO - if the client is not a driver, check whether it is allowed to
|
---|
| 413 | * use the device.
|
---|
| 414 | */
|
---|
[a35b458] | 415 |
|
---|
[5a6cc679] | 416 | errno_t ret = EOK;
|
---|
[8b1e15ac] | 417 | /* Open device function */
|
---|
| 418 | if (fun->ops != NULL && fun->ops->open != NULL)
|
---|
| 419 | ret = (*fun->ops->open)(fun);
|
---|
[a35b458] | 420 |
|
---|
[ffa2c8ef] | 421 | async_answer_0(iid, ret);
|
---|
[b454a22] | 422 | if (ret != EOK) {
|
---|
| 423 | fun_del_ref(fun);
|
---|
[a6e54c5d] | 424 | return;
|
---|
[b454a22] | 425 | }
|
---|
[a35b458] | 426 |
|
---|
[79ae36dd] | 427 | while (true) {
|
---|
[a1769ee] | 428 | ipc_callid_t callid;
|
---|
| 429 | ipc_call_t call;
|
---|
| 430 | callid = async_get_call(&call);
|
---|
[228e490] | 431 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
[a35b458] | 432 |
|
---|
[79ae36dd] | 433 | if (!method) {
|
---|
[8b1e15ac] | 434 | /* Close device function */
|
---|
| 435 | if (fun->ops != NULL && fun->ops->close != NULL)
|
---|
| 436 | (*fun->ops->close)(fun);
|
---|
[ffa2c8ef] | 437 | async_answer_0(callid, EOK);
|
---|
[b454a22] | 438 | fun_del_ref(fun);
|
---|
[a1769ee] | 439 | return;
|
---|
[79ae36dd] | 440 | }
|
---|
[a35b458] | 441 |
|
---|
[79ae36dd] | 442 | /* Convert ipc interface id to interface index */
|
---|
[a35b458] | 443 |
|
---|
[79ae36dd] | 444 | int iface_idx = DEV_IFACE_IDX(method);
|
---|
[a35b458] | 445 |
|
---|
[79ae36dd] | 446 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
| 447 | remote_handler_t *default_handler =
|
---|
| 448 | function_get_default_handler(fun);
|
---|
| 449 | if (default_handler != NULL) {
|
---|
| 450 | (*default_handler)(fun, callid, &call);
|
---|
[79a141a] | 451 | continue;
|
---|
[52b7b1bb] | 452 | }
|
---|
[a35b458] | 453 |
|
---|
[7a252ec8] | 454 | /*
|
---|
[79ae36dd] | 455 | * Function has no such interface and
|
---|
| 456 | * default handler is not provided.
|
---|
[7a252ec8] | 457 | */
|
---|
[79ae36dd] | 458 | printf("%s: driver_connection_gen error - "
|
---|
| 459 | "invalid interface id %d.",
|
---|
| 460 | driver->name, iface_idx);
|
---|
| 461 | async_answer_0(callid, ENOTSUP);
|
---|
[79a141a] | 462 | continue;
|
---|
[79ae36dd] | 463 | }
|
---|
[a35b458] | 464 |
|
---|
[79ae36dd] | 465 | /* Calling one of the function's interfaces */
|
---|
[a35b458] | 466 |
|
---|
[79ae36dd] | 467 | /* Get the interface ops structure. */
|
---|
| 468 | void *ops = function_get_ops(fun, iface_idx);
|
---|
| 469 | if (ops == NULL) {
|
---|
| 470 | printf("%s: driver_connection_gen error - ", driver->name);
|
---|
| 471 | printf("Function with handle %" PRIun " has no interface "
|
---|
| 472 | "with id %d.\n", handle, iface_idx);
|
---|
| 473 | async_answer_0(callid, ENOTSUP);
|
---|
[79a141a] | 474 | continue;
|
---|
[79ae36dd] | 475 | }
|
---|
[a35b458] | 476 |
|
---|
[79ae36dd] | 477 | /*
|
---|
| 478 | * Get the corresponding interface for remote request
|
---|
| 479 | * handling ("remote interface").
|
---|
| 480 | */
|
---|
[d0ca4c5] | 481 | const remote_iface_t *rem_iface = get_remote_iface(iface_idx);
|
---|
[79ae36dd] | 482 | assert(rem_iface != NULL);
|
---|
[a35b458] | 483 |
|
---|
[79ae36dd] | 484 | /* get the method of the remote interface */
|
---|
| 485 | sysarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
| 486 | remote_iface_func_ptr_t iface_method_ptr =
|
---|
| 487 | get_remote_method(rem_iface, iface_method_idx);
|
---|
| 488 | if (iface_method_ptr == NULL) {
|
---|
| 489 | /* The interface has not such method */
|
---|
| 490 | printf("%s: driver_connection_gen error - "
|
---|
| 491 | "invalid interface method.", driver->name);
|
---|
| 492 | async_answer_0(callid, ENOTSUP);
|
---|
[79a141a] | 493 | continue;
|
---|
[a1769ee] | 494 | }
|
---|
[a35b458] | 495 |
|
---|
[79ae36dd] | 496 | /*
|
---|
| 497 | * Call the remote interface's method, which will
|
---|
| 498 | * receive parameters from the remote client and it will
|
---|
| 499 | * pass it to the corresponding local interface method
|
---|
| 500 | * associated with the function by its driver.
|
---|
| 501 | */
|
---|
| 502 | (*iface_method_ptr)(fun, ops, callid, &call);
|
---|
[a1769ee] | 503 | }
|
---|
| 504 | }
|
---|
| 505 |
|
---|
[f9b2cb4c] | 506 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall,
|
---|
| 507 | void *arg)
|
---|
[c16cf62] | 508 | {
|
---|
[52b7b1bb] | 509 | driver_connection_gen(iid, icall, true);
|
---|
[c16cf62] | 510 | }
|
---|
| 511 |
|
---|
[f9b2cb4c] | 512 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall,
|
---|
| 513 | void *arg)
|
---|
[c16cf62] | 514 | {
|
---|
[52b7b1bb] | 515 | driver_connection_gen(iid, icall, false);
|
---|
[c16cf62] | 516 | }
|
---|
| 517 |
|
---|
[5fdd7c3] | 518 | /** Create new device structure.
|
---|
| 519 | *
|
---|
| 520 | * @return The device structure.
|
---|
| 521 | */
|
---|
[83a2f43] | 522 | static ddf_dev_t *create_device(void)
|
---|
[5fdd7c3] | 523 | {
|
---|
[83a2f43] | 524 | ddf_dev_t *dev;
|
---|
[5fdd7c3] | 525 |
|
---|
[0f0f8bc] | 526 | dev = calloc(1, sizeof(ddf_dev_t));
|
---|
[8b1e15ac] | 527 | if (dev == NULL)
|
---|
| 528 | return NULL;
|
---|
[5fdd7c3] | 529 |
|
---|
| 530 | return dev;
|
---|
| 531 | }
|
---|
| 532 |
|
---|
[8b1e15ac] | 533 | /** Create new function structure.
|
---|
| 534 | *
|
---|
| 535 | * @return The device structure.
|
---|
| 536 | */
|
---|
[83a2f43] | 537 | static ddf_fun_t *create_function(void)
|
---|
[8b1e15ac] | 538 | {
|
---|
[83a2f43] | 539 | ddf_fun_t *fun;
|
---|
[8b1e15ac] | 540 |
|
---|
[83a2f43] | 541 | fun = calloc(1, sizeof(ddf_fun_t));
|
---|
[8b1e15ac] | 542 | if (fun == NULL)
|
---|
| 543 | return NULL;
|
---|
| 544 |
|
---|
| 545 | init_match_ids(&fun->match_ids);
|
---|
| 546 | link_initialize(&fun->link);
|
---|
| 547 |
|
---|
| 548 | return fun;
|
---|
| 549 | }
|
---|
| 550 |
|
---|
[5fdd7c3] | 551 | /** Delete device structure.
|
---|
| 552 | *
|
---|
| 553 | * @param dev The device structure.
|
---|
| 554 | */
|
---|
[83a2f43] | 555 | static void delete_device(ddf_dev_t *dev)
|
---|
[5fdd7c3] | 556 | {
|
---|
[56fd7cf] | 557 | if (dev->parent_sess)
|
---|
| 558 | async_hangup(dev->parent_sess);
|
---|
[5f6e25e] | 559 | if (dev->driver_data != NULL)
|
---|
| 560 | free(dev->driver_data);
|
---|
[669e9b5] | 561 | if (dev->name)
|
---|
| 562 | free(dev->name);
|
---|
[5fdd7c3] | 563 | free(dev);
|
---|
| 564 | }
|
---|
| 565 |
|
---|
[0f0f8bc] | 566 | /** Delete function structure.
|
---|
[8b1e15ac] | 567 | *
|
---|
| 568 | * @param dev The device structure.
|
---|
| 569 | */
|
---|
[83a2f43] | 570 | static void delete_function(ddf_fun_t *fun)
|
---|
[8b1e15ac] | 571 | {
|
---|
| 572 | clean_match_ids(&fun->match_ids);
|
---|
[5f6e25e] | 573 | if (fun->driver_data != NULL)
|
---|
| 574 | free(fun->driver_data);
|
---|
[8b1e15ac] | 575 | if (fun->name != NULL)
|
---|
| 576 | free(fun->name);
|
---|
| 577 | free(fun);
|
---|
| 578 | }
|
---|
| 579 |
|
---|
[0f0f8bc] | 580 | /** Increase device reference count. */
|
---|
| 581 | static void dev_add_ref(ddf_dev_t *dev)
|
---|
| 582 | {
|
---|
| 583 | atomic_inc(&dev->refcnt);
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | /** Decrease device reference count.
|
---|
| 587 | *
|
---|
| 588 | * Free the device structure if the reference count drops to zero.
|
---|
| 589 | */
|
---|
| 590 | static void dev_del_ref(ddf_dev_t *dev)
|
---|
| 591 | {
|
---|
| 592 | if (atomic_predec(&dev->refcnt) == 0)
|
---|
| 593 | delete_device(dev);
|
---|
| 594 | }
|
---|
| 595 |
|
---|
[bfe7867] | 596 | /** Increase function reference count.
|
---|
| 597 | *
|
---|
| 598 | * This also increases reference count on the device. The device structure
|
---|
| 599 | * will thus not be deallocated while there are some associated function
|
---|
| 600 | * structures.
|
---|
| 601 | */
|
---|
[0f0f8bc] | 602 | static void fun_add_ref(ddf_fun_t *fun)
|
---|
| 603 | {
|
---|
[bfe7867] | 604 | dev_add_ref(fun->dev);
|
---|
[0f0f8bc] | 605 | atomic_inc(&fun->refcnt);
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | /** Decrease function reference count.
|
---|
| 609 | *
|
---|
| 610 | * Free the function structure if the reference count drops to zero.
|
---|
| 611 | */
|
---|
| 612 | static void fun_del_ref(ddf_fun_t *fun)
|
---|
| 613 | {
|
---|
[bfe7867] | 614 | ddf_dev_t *dev = fun->dev;
|
---|
| 615 |
|
---|
[0f0f8bc] | 616 | if (atomic_predec(&fun->refcnt) == 0)
|
---|
| 617 | delete_function(fun);
|
---|
[bfe7867] | 618 |
|
---|
| 619 | dev_del_ref(dev);
|
---|
[0f0f8bc] | 620 | }
|
---|
| 621 |
|
---|
[c5be39b] | 622 | /** Allocate driver-specific device data. */
|
---|
[bf31e3f] | 623 | void *ddf_dev_data_alloc(ddf_dev_t *dev, size_t size)
|
---|
[c5be39b] | 624 | {
|
---|
| 625 | assert(dev->driver_data == NULL);
|
---|
[a35b458] | 626 |
|
---|
[77ad86c] | 627 | void *data = calloc(1, size);
|
---|
[c5be39b] | 628 | if (data == NULL)
|
---|
| 629 | return NULL;
|
---|
[a35b458] | 630 |
|
---|
[c5be39b] | 631 | dev->driver_data = data;
|
---|
| 632 | return data;
|
---|
| 633 | }
|
---|
| 634 |
|
---|
[56fd7cf] | 635 | /** Return driver-specific device data. */
|
---|
| 636 | void *ddf_dev_data_get(ddf_dev_t *dev)
|
---|
| 637 | {
|
---|
| 638 | return dev->driver_data;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | /** Get device handle. */
|
---|
| 642 | devman_handle_t ddf_dev_get_handle(ddf_dev_t *dev)
|
---|
| 643 | {
|
---|
| 644 | return dev->handle;
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | /** Return device name.
|
---|
| 648 | *
|
---|
| 649 | * @param dev Device
|
---|
| 650 | * @return Device name. Valid as long as @a dev is valid.
|
---|
| 651 | */
|
---|
| 652 | const char *ddf_dev_get_name(ddf_dev_t *dev)
|
---|
| 653 | {
|
---|
| 654 | return dev->name;
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | /** Return existing session with the parent function.
|
---|
| 658 | *
|
---|
| 659 | * @param dev Device
|
---|
[2fd26bb] | 660 | * @return Session with parent function or NULL upon failure
|
---|
[56fd7cf] | 661 | */
|
---|
| 662 | async_sess_t *ddf_dev_parent_sess_get(ddf_dev_t *dev)
|
---|
| 663 | {
|
---|
[2fd26bb] | 664 | if (dev->parent_sess == NULL) {
|
---|
| 665 | dev->parent_sess = devman_parent_device_connect(dev->handle,
|
---|
| 666 | IPC_FLAG_BLOCKING);
|
---|
| 667 | }
|
---|
| 668 |
|
---|
[56fd7cf] | 669 | return dev->parent_sess;
|
---|
| 670 | }
|
---|
| 671 |
|
---|
| 672 | /** Set function name (if it was not specified when node was created.)
|
---|
| 673 | *
|
---|
| 674 | * @param dev Device whose name has not been set yet
|
---|
| 675 | * @param name Name, will be copied
|
---|
| 676 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 677 | */
|
---|
[5a6cc679] | 678 | errno_t ddf_fun_set_name(ddf_fun_t *dev, const char *name)
|
---|
[56fd7cf] | 679 | {
|
---|
| 680 | assert(dev->name == NULL);
|
---|
| 681 |
|
---|
| 682 | dev->name = str_dup(name);
|
---|
| 683 | if (dev->name == NULL)
|
---|
| 684 | return ENOENT;
|
---|
| 685 |
|
---|
| 686 | return EOK;
|
---|
| 687 | }
|
---|
| 688 |
|
---|
| 689 | /** Get device to which function belongs. */
|
---|
| 690 | ddf_dev_t *ddf_fun_get_dev(ddf_fun_t *fun)
|
---|
| 691 | {
|
---|
| 692 | return fun->dev;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 | /** Get function handle.
|
---|
| 696 | *
|
---|
| 697 | * XXX USB uses this, but its use should be eliminated.
|
---|
| 698 | */
|
---|
| 699 | devman_handle_t ddf_fun_get_handle(ddf_fun_t *fun)
|
---|
| 700 | {
|
---|
| 701 | return fun->handle;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
[97a62fe] | 704 | /** Create a DDF function node.
|
---|
| 705 | *
|
---|
| 706 | * Create a DDF function (in memory). Both child devices and external clients
|
---|
| 707 | * communicate with a device via its functions.
|
---|
| 708 | *
|
---|
| 709 | * The created function node is fully formed, but only exists in the memory
|
---|
| 710 | * of the client task. In order to be visible to the system, the function
|
---|
| 711 | * must be bound using ddf_fun_bind().
|
---|
| 712 | *
|
---|
| 713 | * This function should only fail if there is not enough free memory.
|
---|
| 714 | * Specifically, this function succeeds even if @a dev already has
|
---|
[56fd7cf] | 715 | * a (bound) function with the same name. @a name can be NULL in which
|
---|
| 716 | * case the caller will set the name later using ddf_fun_set_name().
|
---|
| 717 | * He must do this before binding the function.
|
---|
[97a62fe] | 718 | *
|
---|
| 719 | * Type: A function of type fun_inner indicates that DDF should attempt
|
---|
| 720 | * to attach child devices to the function. fun_exposed means that
|
---|
| 721 | * the function should be exported to external clients (applications).
|
---|
| 722 | *
|
---|
| 723 | * @param dev Device to which we are adding function
|
---|
| 724 | * @param ftype Type of function (fun_inner or fun_exposed)
|
---|
[56fd7cf] | 725 | * @param name Name of function or NULL
|
---|
[97a62fe] | 726 | *
|
---|
| 727 | * @return New function or @c NULL if memory is not available
|
---|
| 728 | */
|
---|
[83a2f43] | 729 | ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
|
---|
[97a62fe] | 730 | {
|
---|
[77ad86c] | 731 | ddf_fun_t *fun = create_function();
|
---|
[97a62fe] | 732 | if (fun == NULL)
|
---|
| 733 | return NULL;
|
---|
[a35b458] | 734 |
|
---|
[0f0f8bc] | 735 | /* Add one reference that will be dropped by ddf_fun_destroy() */
|
---|
[bfe7867] | 736 | fun->dev = dev;
|
---|
[0f0f8bc] | 737 | fun_add_ref(fun);
|
---|
[a35b458] | 738 |
|
---|
[97a62fe] | 739 | fun->bound = false;
|
---|
| 740 | fun->ftype = ftype;
|
---|
[a35b458] | 741 |
|
---|
[56fd7cf] | 742 | if (name != NULL) {
|
---|
| 743 | fun->name = str_dup(name);
|
---|
| 744 | if (fun->name == NULL) {
|
---|
| 745 | delete_function(fun);
|
---|
| 746 | return NULL;
|
---|
| 747 | }
|
---|
[97a62fe] | 748 | }
|
---|
[a35b458] | 749 |
|
---|
[97a62fe] | 750 | return fun;
|
---|
| 751 | }
|
---|
| 752 |
|
---|
[c5be39b] | 753 | /** Allocate driver-specific function data. */
|
---|
[bf31e3f] | 754 | void *ddf_fun_data_alloc(ddf_fun_t *fun, size_t size)
|
---|
[c5be39b] | 755 | {
|
---|
| 756 | assert(fun->bound == false);
|
---|
| 757 | assert(fun->driver_data == NULL);
|
---|
[a35b458] | 758 |
|
---|
[77ad86c] | 759 | void *data = calloc(1, size);
|
---|
[c5be39b] | 760 | if (data == NULL)
|
---|
| 761 | return NULL;
|
---|
[a35b458] | 762 |
|
---|
[c5be39b] | 763 | fun->driver_data = data;
|
---|
| 764 | return data;
|
---|
| 765 | }
|
---|
| 766 |
|
---|
[56fd7cf] | 767 | /** Return driver-specific function data. */
|
---|
| 768 | void *ddf_fun_data_get(ddf_fun_t *fun)
|
---|
| 769 | {
|
---|
| 770 | return fun->driver_data;
|
---|
| 771 | }
|
---|
| 772 |
|
---|
| 773 | /** Return function name.
|
---|
| 774 | *
|
---|
| 775 | * @param fun Function
|
---|
| 776 | * @return Function name. Valid as long as @a fun is valid.
|
---|
| 777 | */
|
---|
| 778 | const char *ddf_fun_get_name(ddf_fun_t *fun)
|
---|
| 779 | {
|
---|
| 780 | return fun->name;
|
---|
| 781 | }
|
---|
| 782 |
|
---|
[97a62fe] | 783 | /** Destroy DDF function node.
|
---|
| 784 | *
|
---|
| 785 | * Destroy a function previously created with ddf_fun_create(). The function
|
---|
| 786 | * must not be bound.
|
---|
| 787 | *
|
---|
[77ad86c] | 788 | * @param fun Function to destroy
|
---|
| 789 | *
|
---|
[97a62fe] | 790 | */
|
---|
[83a2f43] | 791 | void ddf_fun_destroy(ddf_fun_t *fun)
|
---|
[97a62fe] | 792 | {
|
---|
| 793 | assert(fun->bound == false);
|
---|
[a35b458] | 794 |
|
---|
[0f0f8bc] | 795 | /*
|
---|
| 796 | * Drop the reference added by ddf_fun_create(). This will deallocate
|
---|
| 797 | * the function as soon as all other references are dropped (i.e.
|
---|
| 798 | * as soon control leaves all driver entry points called in context
|
---|
| 799 | * of this function.
|
---|
| 800 | */
|
---|
| 801 | fun_del_ref(fun);
|
---|
[97a62fe] | 802 | }
|
---|
| 803 |
|
---|
[af6b5157] | 804 | static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
|
---|
[5fdd7c3] | 805 | {
|
---|
| 806 | assert(is_valid_iface_idx(idx));
|
---|
[8b1e15ac] | 807 | if (fun->ops == NULL)
|
---|
[5fdd7c3] | 808 | return NULL;
|
---|
[a35b458] | 809 |
|
---|
[8b1e15ac] | 810 | return fun->ops->interfaces[idx];
|
---|
[5fdd7c3] | 811 | }
|
---|
| 812 |
|
---|
[97a62fe] | 813 | /** Bind a function node.
|
---|
| 814 | *
|
---|
| 815 | * Bind the specified function to the system. This effectively makes
|
---|
| 816 | * the function visible to the system (uploads it to the server).
|
---|
| 817 | *
|
---|
| 818 | * This function can fail for several reasons. Specifically,
|
---|
| 819 | * it will fail if the device already has a bound function of
|
---|
| 820 | * the same name.
|
---|
| 821 | *
|
---|
[77ad86c] | 822 | * @param fun Function to bind
|
---|
| 823 | *
|
---|
[cde999a] | 824 | * @return EOK on success or an error code
|
---|
[77ad86c] | 825 | *
|
---|
[97a62fe] | 826 | */
|
---|
[5a6cc679] | 827 | errno_t ddf_fun_bind(ddf_fun_t *fun)
|
---|
[7707954] | 828 | {
|
---|
[d0dd7b5] | 829 | assert(fun->bound == false);
|
---|
[8b1e15ac] | 830 | assert(fun->name != NULL);
|
---|
[9e79a2f] | 831 | assert(fun->dev != NULL);
|
---|
[a35b458] | 832 |
|
---|
[8b1e15ac] | 833 | add_to_functions_list(fun);
|
---|
[5a6cc679] | 834 | errno_t res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
|
---|
[97a62fe] | 835 | fun->dev->handle, &fun->handle);
|
---|
[36f2b3e] | 836 | if (res != EOK) {
|
---|
[8b1e15ac] | 837 | remove_from_functions_list(fun);
|
---|
[df747b9c] | 838 | return res;
|
---|
[36f2b3e] | 839 | }
|
---|
[a35b458] | 840 |
|
---|
[97a62fe] | 841 | fun->bound = true;
|
---|
[df747b9c] | 842 | return res;
|
---|
[7707954] | 843 | }
|
---|
| 844 |
|
---|
[d0dd7b5] | 845 | /** Unbind a function node.
|
---|
| 846 | *
|
---|
| 847 | * Unbind the specified function from the system. This effectively makes
|
---|
| 848 | * the function invisible to the system.
|
---|
| 849 | *
|
---|
[77ad86c] | 850 | * @param fun Function to unbind
|
---|
| 851 | *
|
---|
[cde999a] | 852 | * @return EOK on success or an error code
|
---|
[77ad86c] | 853 | *
|
---|
[d0dd7b5] | 854 | */
|
---|
[5a6cc679] | 855 | errno_t ddf_fun_unbind(ddf_fun_t *fun)
|
---|
[d0dd7b5] | 856 | {
|
---|
| 857 | assert(fun->bound == true);
|
---|
[a35b458] | 858 |
|
---|
[5a6cc679] | 859 | errno_t res = devman_remove_function(fun->handle);
|
---|
[d0dd7b5] | 860 | if (res != EOK)
|
---|
| 861 | return res;
|
---|
[a35b458] | 862 |
|
---|
[d0dd7b5] | 863 | remove_from_functions_list(fun);
|
---|
[a35b458] | 864 |
|
---|
[d0dd7b5] | 865 | fun->bound = false;
|
---|
| 866 | return EOK;
|
---|
| 867 | }
|
---|
| 868 |
|
---|
[1a5b252] | 869 | /** Online function.
|
---|
| 870 | *
|
---|
[77ad86c] | 871 | * @param fun Function to online
|
---|
| 872 | *
|
---|
[cde999a] | 873 | * @return EOK on success or an error code
|
---|
[77ad86c] | 874 | *
|
---|
[1a5b252] | 875 | */
|
---|
[5a6cc679] | 876 | errno_t ddf_fun_online(ddf_fun_t *fun)
|
---|
[1a5b252] | 877 | {
|
---|
| 878 | assert(fun->bound == true);
|
---|
[a35b458] | 879 |
|
---|
[5a6cc679] | 880 | errno_t res = devman_drv_fun_online(fun->handle);
|
---|
[1a5b252] | 881 | if (res != EOK)
|
---|
| 882 | return res;
|
---|
[a35b458] | 883 |
|
---|
[1a5b252] | 884 | return EOK;
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 | /** Offline function.
|
---|
| 888 | *
|
---|
[77ad86c] | 889 | * @param fun Function to offline
|
---|
| 890 | *
|
---|
[cde999a] | 891 | * @return EOK on success or an error code
|
---|
[77ad86c] | 892 | *
|
---|
[1a5b252] | 893 | */
|
---|
[5a6cc679] | 894 | errno_t ddf_fun_offline(ddf_fun_t *fun)
|
---|
[1a5b252] | 895 | {
|
---|
| 896 | assert(fun->bound == true);
|
---|
[a35b458] | 897 |
|
---|
[5a6cc679] | 898 | errno_t res = devman_drv_fun_offline(fun->handle);
|
---|
[1a5b252] | 899 | if (res != EOK)
|
---|
| 900 | return res;
|
---|
[a35b458] | 901 |
|
---|
[1a5b252] | 902 | return EOK;
|
---|
| 903 | }
|
---|
| 904 |
|
---|
[34588a80] | 905 | /** Add single match ID to inner function.
|
---|
[cd0684d] | 906 | *
|
---|
| 907 | * Construct and add a single match ID to the specified function.
|
---|
[34588a80] | 908 | * Cannot be called when the function node is bound.
|
---|
[cd0684d] | 909 | *
|
---|
[77ad86c] | 910 | * @param fun Function
|
---|
| 911 | * @param match_id_str Match string
|
---|
| 912 | * @param match_score Match score
|
---|
| 913 | *
|
---|
| 914 | * @return EOK on success.
|
---|
| 915 | * @return ENOMEM if out of memory.
|
---|
| 916 | *
|
---|
[cd0684d] | 917 | */
|
---|
[5a6cc679] | 918 | errno_t ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
|
---|
[cd0684d] | 919 | int match_score)
|
---|
| 920 | {
|
---|
[34588a80] | 921 | assert(fun->bound == false);
|
---|
| 922 | assert(fun->ftype == fun_inner);
|
---|
[a35b458] | 923 |
|
---|
[77ad86c] | 924 | match_id_t *match_id = create_match_id();
|
---|
[cd0684d] | 925 | if (match_id == NULL)
|
---|
| 926 | return ENOMEM;
|
---|
[a35b458] | 927 |
|
---|
[ef9460b] | 928 | match_id->id = str_dup(match_id_str);
|
---|
[8a363ab3] | 929 | match_id->score = match_score;
|
---|
[a35b458] | 930 |
|
---|
[cd0684d] | 931 | add_match_id(&fun->match_ids, match_id);
|
---|
| 932 | return EOK;
|
---|
| 933 | }
|
---|
| 934 |
|
---|
[56fd7cf] | 935 | /** Set function ops. */
|
---|
[facc34d] | 936 | void ddf_fun_set_ops(ddf_fun_t *fun, const ddf_dev_ops_t *dev_ops)
|
---|
[56fd7cf] | 937 | {
|
---|
| 938 | assert(fun->conn_handler == NULL);
|
---|
| 939 | fun->ops = dev_ops;
|
---|
| 940 | }
|
---|
| 941 |
|
---|
| 942 | /** Set user-defined connection handler.
|
---|
| 943 | *
|
---|
| 944 | * This allows handling connections the non-devman way.
|
---|
| 945 | */
|
---|
[b688fd8] | 946 | void ddf_fun_set_conn_handler(ddf_fun_t *fun, async_port_handler_t conn)
|
---|
[56fd7cf] | 947 | {
|
---|
| 948 | assert(fun->ops == NULL);
|
---|
| 949 | fun->conn_handler = conn;
|
---|
| 950 | }
|
---|
| 951 |
|
---|
[5fdd7c3] | 952 | /** Get default handler for client requests */
|
---|
[af6b5157] | 953 | static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
|
---|
[5fdd7c3] | 954 | {
|
---|
[8b1e15ac] | 955 | if (fun->ops == NULL)
|
---|
[5fdd7c3] | 956 | return NULL;
|
---|
[8b1e15ac] | 957 | return fun->ops->default_handler;
|
---|
[5fdd7c3] | 958 | }
|
---|
| 959 |
|
---|
[1dc4a5e] | 960 | /** Add exposed function to category.
|
---|
[34588a80] | 961 | *
|
---|
| 962 | * Must only be called when the function is bound.
|
---|
[77ad86c] | 963 | *
|
---|
[34588a80] | 964 | */
|
---|
[5a6cc679] | 965 | errno_t ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
|
---|
[5fdd7c3] | 966 | {
|
---|
[34588a80] | 967 | assert(fun->bound == true);
|
---|
| 968 | assert(fun->ftype == fun_exposed);
|
---|
[a35b458] | 969 |
|
---|
[1dc4a5e] | 970 | return devman_add_device_to_category(fun->handle, cat_name);
|
---|
[5fdd7c3] | 971 | }
|
---|
| 972 |
|
---|
[45457265] | 973 | errno_t ddf_driver_main(const driver_t *drv)
|
---|
[c16cf62] | 974 | {
|
---|
[7a252ec8] | 975 | /*
|
---|
| 976 | * Remember the driver structure - driver_ops will be called by generic
|
---|
| 977 | * handler for incoming connections.
|
---|
| 978 | */
|
---|
[c16cf62] | 979 | driver = drv;
|
---|
[a35b458] | 980 |
|
---|
[7a252ec8] | 981 | /*
|
---|
[033cbf82] | 982 | * Register driver with device manager using generic handler for
|
---|
| 983 | * incoming connections.
|
---|
[7a252ec8] | 984 | */
|
---|
[f9b2cb4c] | 985 | port_id_t port;
|
---|
[5a6cc679] | 986 | errno_t rc = async_create_port(INTERFACE_DDF_DRIVER, driver_connection_driver,
|
---|
[f9b2cb4c] | 987 | NULL, &port);
|
---|
[7b616e2] | 988 | if (rc != EOK) {
|
---|
| 989 | printf("Error: Failed to create driver port.\n");
|
---|
[f9b2cb4c] | 990 | return rc;
|
---|
[7b616e2] | 991 | }
|
---|
[a35b458] | 992 |
|
---|
[f9b2cb4c] | 993 | rc = async_create_port(INTERFACE_DDF_DEVMAN, driver_connection_devman,
|
---|
| 994 | NULL, &port);
|
---|
[7b616e2] | 995 | if (rc != EOK) {
|
---|
| 996 | printf("Error: Failed to create devman port.\n");
|
---|
[f9b2cb4c] | 997 | return rc;
|
---|
[7b616e2] | 998 | }
|
---|
[a35b458] | 999 |
|
---|
[f9b2cb4c] | 1000 | async_set_fallback_port_handler(driver_connection_client, NULL);
|
---|
[a35b458] | 1001 |
|
---|
[f9b2cb4c] | 1002 | rc = devman_driver_register(driver->name);
|
---|
[033cbf82] | 1003 | if (rc != EOK) {
|
---|
[3ad7b1c] | 1004 | printf("Error: Failed to register driver with device manager "
|
---|
[8a637a4] | 1005 | "(%s).\n", (rc == EEXIST) ? "driver already started" :
|
---|
[3ad7b1c] | 1006 | str_error(rc));
|
---|
[a35b458] | 1007 |
|
---|
[77ad86c] | 1008 | return rc;
|
---|
[033cbf82] | 1009 | }
|
---|
[a35b458] | 1010 |
|
---|
[26fa82bc] | 1011 | /* Return success from the task since server has started. */
|
---|
| 1012 | rc = task_retval(0);
|
---|
[7b616e2] | 1013 | if (rc != EOK) {
|
---|
| 1014 | printf("Error: Failed returning task value.\n");
|
---|
[77ad86c] | 1015 | return rc;
|
---|
[7b616e2] | 1016 | }
|
---|
[a35b458] | 1017 |
|
---|
[c16cf62] | 1018 | async_manager();
|
---|
[a35b458] | 1019 |
|
---|
[7a252ec8] | 1020 | /* Never reached. */
|
---|
[77ad86c] | 1021 | return EOK;
|
---|
[c16cf62] | 1022 | }
|
---|
| 1023 |
|
---|
| 1024 | /**
|
---|
| 1025 | * @}
|
---|
[52b7b1bb] | 1026 | */
|
---|