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