| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 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 | /** @file
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | #include <assert.h>
|
|---|
| 34 | #include <stdio.h>
|
|---|
| 35 | #include <errno.h>
|
|---|
| 36 | #include <str_error.h>
|
|---|
| 37 | #include <ddf/driver.h>
|
|---|
| 38 | #include <ddf/log.h>
|
|---|
| 39 |
|
|---|
| 40 | #include "test1.h"
|
|---|
| 41 |
|
|---|
| 42 | static int test1_add_device(ddf_dev_t *dev);
|
|---|
| 43 | static int test1_dev_remove(ddf_dev_t *dev);
|
|---|
| 44 | static int test1_dev_gone(ddf_dev_t *dev);
|
|---|
| 45 | static int test1_fun_online(ddf_fun_t *fun);
|
|---|
| 46 | static int test1_fun_offline(ddf_fun_t *fun);
|
|---|
| 47 |
|
|---|
| 48 | static driver_ops_t driver_ops = {
|
|---|
| 49 | .add_device = &test1_add_device,
|
|---|
| 50 | .dev_remove = &test1_dev_remove,
|
|---|
| 51 | .dev_gone = &test1_dev_gone,
|
|---|
| 52 | .fun_online = &test1_fun_online,
|
|---|
| 53 | .fun_offline = &test1_fun_offline
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | static driver_t test1_driver = {
|
|---|
| 57 | .name = NAME,
|
|---|
| 58 | .driver_ops = &driver_ops
|
|---|
| 59 | };
|
|---|
| 60 |
|
|---|
| 61 | typedef struct {
|
|---|
| 62 | ddf_fun_t *fun_a;
|
|---|
| 63 | ddf_fun_t *clone;
|
|---|
| 64 | ddf_fun_t *child;
|
|---|
| 65 | } test1_t;
|
|---|
| 66 |
|
|---|
| 67 | /** Register child and inform user about it.
|
|---|
| 68 | *
|
|---|
| 69 | * @param parent Parent device.
|
|---|
| 70 | * @param message Message for the user.
|
|---|
| 71 | * @param name Device name.
|
|---|
| 72 | * @param match_id Device match id.
|
|---|
| 73 | * @param score Device match score.
|
|---|
| 74 | */
|
|---|
| 75 | static int register_fun_verbose(ddf_dev_t *parent, const char *message,
|
|---|
| 76 | const char *name, const char *match_id, int match_score,
|
|---|
| 77 | int expected_rc, ddf_fun_t **pfun)
|
|---|
| 78 | {
|
|---|
| 79 | ddf_fun_t *fun = NULL;
|
|---|
| 80 | int rc;
|
|---|
| 81 |
|
|---|
| 82 | ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
|
|---|
| 83 |
|
|---|
| 84 | fun = ddf_fun_create(parent, fun_inner, name);
|
|---|
| 85 | if (fun == NULL) {
|
|---|
| 86 | ddf_msg(LVL_ERROR, "Failed creating function %s", name);
|
|---|
| 87 | rc = ENOMEM;
|
|---|
| 88 | goto leave;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | rc = ddf_fun_add_match_id(fun, match_id, match_score);
|
|---|
| 92 | if (rc != EOK) {
|
|---|
| 93 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
|---|
| 94 | name);
|
|---|
| 95 | goto leave;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | rc = ddf_fun_bind(fun);
|
|---|
| 99 | if (rc != EOK) {
|
|---|
| 100 | ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
|
|---|
| 101 | str_error(rc));
|
|---|
| 102 | goto leave;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
|
|---|
| 106 | rc = EOK;
|
|---|
| 107 |
|
|---|
| 108 | leave:
|
|---|
| 109 | if (rc != expected_rc) {
|
|---|
| 110 | fprintf(stderr,
|
|---|
| 111 | NAME ": Unexpected error registering function `%s'.\n"
|
|---|
| 112 | NAME ": Expected \"%s\" but got \"%s\".\n",
|
|---|
| 113 | name, str_error(expected_rc), str_error(rc));
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | if ((rc != EOK) && (fun != NULL)) {
|
|---|
| 117 | ddf_fun_destroy(fun);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | if (pfun != NULL)
|
|---|
| 121 | *pfun = fun;
|
|---|
| 122 |
|
|---|
| 123 | return rc;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** Callback when new device is passed to this driver.
|
|---|
| 127 | * This function is the body of the test: it shall register new child
|
|---|
| 128 | * (named `clone') that shall be driven by the same task. When the clone
|
|---|
| 129 | * is added, it registers another child (named `child') that is also driven
|
|---|
| 130 | * by this task. The conditions ensure that we do not recurse indefinitely.
|
|---|
| 131 | * When successful, the device tree shall contain following fragment:
|
|---|
| 132 | *
|
|---|
| 133 | * /virtual/test1
|
|---|
| 134 | * /virtual/test1/clone
|
|---|
| 135 | * /virtual/test1/clone/child
|
|---|
| 136 | *
|
|---|
| 137 | * and devman shall not deadlock.
|
|---|
| 138 | *
|
|---|
| 139 | *
|
|---|
| 140 | * @param dev New device.
|
|---|
| 141 | * @return Error code reporting success of the operation.
|
|---|
| 142 | */
|
|---|
| 143 | static int test1_add_device(ddf_dev_t *dev)
|
|---|
| 144 | {
|
|---|
| 145 | ddf_fun_t *fun_a;
|
|---|
| 146 | test1_t *test1;
|
|---|
| 147 | int rc;
|
|---|
| 148 |
|
|---|
| 149 | ddf_msg(LVL_DEBUG, "add_device(name=\"%s\", handle=%d)",
|
|---|
| 150 | dev->name, (int) dev->handle);
|
|---|
| 151 |
|
|---|
| 152 | test1 = ddf_dev_data_alloc(dev, sizeof(test1_t));
|
|---|
| 153 | if (test1 == NULL) {
|
|---|
| 154 | ddf_msg(LVL_ERROR, "Failed allocating soft state.\n");
|
|---|
| 155 | return ENOMEM;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | fun_a = ddf_fun_create(dev, fun_exposed, "a");
|
|---|
| 159 | if (fun_a == NULL) {
|
|---|
| 160 | ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
|
|---|
| 161 | return ENOMEM;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | test1->fun_a = fun_a;
|
|---|
| 165 |
|
|---|
| 166 | rc = ddf_fun_bind(fun_a);
|
|---|
| 167 | if (rc != EOK) {
|
|---|
| 168 | ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
|
|---|
| 169 | ddf_fun_destroy(fun_a);
|
|---|
| 170 | return rc;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | ddf_fun_add_to_category(fun_a, "virtual");
|
|---|
| 174 |
|
|---|
| 175 | if (str_cmp(dev->name, "null") == 0) {
|
|---|
| 176 | fun_a->ops = &char_device_ops;
|
|---|
| 177 | ddf_fun_add_to_category(fun_a, "virt-null");
|
|---|
| 178 | } else if (str_cmp(dev->name, "test1") == 0) {
|
|---|
| 179 | (void) register_fun_verbose(dev,
|
|---|
| 180 | "cloning myself ;-)", "clone",
|
|---|
| 181 | "virtual&test1", 10, EOK, &test1->clone);
|
|---|
| 182 | (void) register_fun_verbose(dev,
|
|---|
| 183 | "cloning myself twice ;-)", "clone",
|
|---|
| 184 | "virtual&test1", 10, EEXISTS, NULL);
|
|---|
| 185 | } else if (str_cmp(dev->name, "clone") == 0) {
|
|---|
| 186 | (void) register_fun_verbose(dev,
|
|---|
| 187 | "run by the same task", "child",
|
|---|
| 188 | "virtual&test1&child", 10, EOK, &test1->child);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | ddf_msg(LVL_DEBUG, "Device `%s' accepted.", dev->name);
|
|---|
| 192 |
|
|---|
| 193 | return EOK;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | static int fun_remove(ddf_fun_t *fun, const char *name)
|
|---|
| 197 | {
|
|---|
| 198 | int rc;
|
|---|
| 199 |
|
|---|
| 200 | ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
|
|---|
| 201 | rc = ddf_fun_offline(fun);
|
|---|
| 202 | if (rc != EOK) {
|
|---|
| 203 | ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
|
|---|
| 204 | return rc;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | rc = ddf_fun_unbind(fun);
|
|---|
| 208 | if (rc != EOK) {
|
|---|
| 209 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
|
|---|
| 210 | return rc;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | ddf_fun_destroy(fun);
|
|---|
| 214 | return EOK;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | static int fun_unbind(ddf_fun_t *fun, const char *name)
|
|---|
| 218 | {
|
|---|
| 219 | int rc;
|
|---|
| 220 |
|
|---|
| 221 | ddf_msg(LVL_DEBUG, "fun_unbind(%p, '%s')", fun, name);
|
|---|
| 222 | rc = ddf_fun_unbind(fun);
|
|---|
| 223 | if (rc != EOK) {
|
|---|
| 224 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
|
|---|
| 225 | return rc;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | ddf_fun_destroy(fun);
|
|---|
| 229 | return EOK;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | static int test1_dev_remove(ddf_dev_t *dev)
|
|---|
| 233 | {
|
|---|
| 234 | test1_t *test1 = (test1_t *)dev->driver_data;
|
|---|
| 235 | int rc;
|
|---|
| 236 |
|
|---|
| 237 | ddf_msg(LVL_DEBUG, "test1_dev_remove(%p)", dev);
|
|---|
| 238 |
|
|---|
| 239 | if (test1->fun_a != NULL) {
|
|---|
| 240 | rc = fun_remove(test1->fun_a, "a");
|
|---|
| 241 | if (rc != EOK)
|
|---|
| 242 | return rc;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | if (test1->clone != NULL) {
|
|---|
| 246 | rc = fun_remove(test1->clone, "clone");
|
|---|
| 247 | if (rc != EOK)
|
|---|
| 248 | return rc;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | if (test1->child != NULL) {
|
|---|
| 252 | rc = fun_remove(test1->child, "child");
|
|---|
| 253 | if (rc != EOK)
|
|---|
| 254 | return rc;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | return EOK;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | static int test1_dev_gone(ddf_dev_t *dev)
|
|---|
| 261 | {
|
|---|
| 262 | test1_t *test1 = (test1_t *)dev->driver_data;
|
|---|
| 263 | int rc;
|
|---|
| 264 |
|
|---|
| 265 | ddf_msg(LVL_DEBUG, "test1_dev_remove(%p)", dev);
|
|---|
| 266 |
|
|---|
| 267 | if (test1->fun_a != NULL) {
|
|---|
| 268 | rc = fun_unbind(test1->fun_a, "a");
|
|---|
| 269 | if (rc != EOK)
|
|---|
| 270 | return rc;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | if (test1->clone != NULL) {
|
|---|
| 274 | rc = fun_unbind(test1->clone, "clone");
|
|---|
| 275 | if (rc != EOK)
|
|---|
| 276 | return rc;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | if (test1->child != NULL) {
|
|---|
| 280 | rc = fun_unbind(test1->child, "child");
|
|---|
| 281 | if (rc != EOK)
|
|---|
| 282 | return rc;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | return EOK;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | static int test1_fun_online(ddf_fun_t *fun)
|
|---|
| 289 | {
|
|---|
| 290 | ddf_msg(LVL_DEBUG, "test1_fun_online()");
|
|---|
| 291 | return ddf_fun_online(fun);
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | static int test1_fun_offline(ddf_fun_t *fun)
|
|---|
| 295 | {
|
|---|
| 296 | ddf_msg(LVL_DEBUG, "test1_fun_offline()");
|
|---|
| 297 | return ddf_fun_offline(fun);
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | int main(int argc, char *argv[])
|
|---|
| 301 | {
|
|---|
| 302 | printf(NAME ": HelenOS test1 virtual device driver\n");
|
|---|
| 303 | ddf_log_init(NAME, LVL_ERROR);
|
|---|
| 304 | return ddf_driver_main(&test1_driver);
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|