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