[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 |
|
---|
[4f87a85a] | 178 | rc = ddf_fun_add_to_category(fun_a, "virtual");
|
---|
| 179 | if (rc != EOK) {
|
---|
| 180 | ddf_msg(LVL_ERROR, "Failed adding function 'a' to category "
|
---|
| 181 | "'virtual'.");
|
---|
| 182 | ddf_fun_unbind(fun_a);
|
---|
| 183 | ddf_fun_destroy(fun_a);
|
---|
| 184 | goto error;
|
---|
| 185 | }
|
---|
[703d19c] | 186 |
|
---|
[74017ce] | 187 | if (str_cmp(dev_name, "test1") == 0) {
|
---|
[3fddb55] | 188 | (void) register_fun_verbose(dev,
|
---|
| 189 | "cloning myself ;-)", "clone",
|
---|
[1a5b252] | 190 | "virtual&test1", 10, EOK, &test1->clone);
|
---|
[3fddb55] | 191 | (void) register_fun_verbose(dev,
|
---|
| 192 | "cloning myself twice ;-)", "clone",
|
---|
[8a637a4] | 193 | "virtual&test1", 10, EEXIST, NULL);
|
---|
[56fd7cf] | 194 | } else if (str_cmp(dev_name, "clone") == 0) {
|
---|
[3fddb55] | 195 | (void) register_fun_verbose(dev,
|
---|
| 196 | "run by the same task", "child",
|
---|
[1a5b252] | 197 | "virtual&test1&child", 10, EOK, &test1->child);
|
---|
[1b2981aa] | 198 | }
|
---|
| 199 |
|
---|
[56fd7cf] | 200 | ddf_msg(LVL_DEBUG, "Device `%s' accepted.", dev_name);
|
---|
[1a5b252] | 201 | return EOK;
|
---|
[56fd7cf] | 202 | error:
|
---|
| 203 | return rc;
|
---|
[1a5b252] | 204 | }
|
---|
| 205 |
|
---|
[b7fd2a0] | 206 | static errno_t fun_remove(ddf_fun_t *fun, const char *name)
|
---|
[1a5b252] | 207 | {
|
---|
[b7fd2a0] | 208 | errno_t rc;
|
---|
[1a5b252] | 209 |
|
---|
[deac215e] | 210 | ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
|
---|
[1a5b252] | 211 | rc = ddf_fun_offline(fun);
|
---|
| 212 | if (rc != EOK) {
|
---|
| 213 | ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
|
---|
| 214 | return rc;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | rc = ddf_fun_unbind(fun);
|
---|
| 218 | if (rc != EOK) {
|
---|
[7fff38c1] | 219 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
|
---|
[1a5b252] | 220 | return rc;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | ddf_fun_destroy(fun);
|
---|
[1b2981aa] | 224 | return EOK;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[b7fd2a0] | 227 | static errno_t fun_unbind(ddf_fun_t *fun, const char *name)
|
---|
[80a96d2] | 228 | {
|
---|
[b7fd2a0] | 229 | errno_t rc;
|
---|
[80a96d2] | 230 |
|
---|
| 231 | ddf_msg(LVL_DEBUG, "fun_unbind(%p, '%s')", fun, name);
|
---|
| 232 | rc = ddf_fun_unbind(fun);
|
---|
| 233 | if (rc != EOK) {
|
---|
| 234 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
|
---|
| 235 | return rc;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | ddf_fun_destroy(fun);
|
---|
| 239 | return EOK;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[b7fd2a0] | 242 | static errno_t test1_dev_remove(ddf_dev_t *dev)
|
---|
[1a5b252] | 243 | {
|
---|
[56fd7cf] | 244 | test1_t *test1 = (test1_t *)ddf_dev_data_get(dev);
|
---|
[b7fd2a0] | 245 | errno_t rc;
|
---|
[1a5b252] | 246 |
|
---|
| 247 | ddf_msg(LVL_DEBUG, "test1_dev_remove(%p)", dev);
|
---|
| 248 |
|
---|
| 249 | if (test1->fun_a != NULL) {
|
---|
| 250 | rc = fun_remove(test1->fun_a, "a");
|
---|
| 251 | if (rc != EOK)
|
---|
| 252 | return rc;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | if (test1->clone != NULL) {
|
---|
| 256 | rc = fun_remove(test1->clone, "clone");
|
---|
| 257 | if (rc != EOK)
|
---|
| 258 | return rc;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | if (test1->child != NULL) {
|
---|
| 262 | rc = fun_remove(test1->child, "child");
|
---|
| 263 | if (rc != EOK)
|
---|
| 264 | return rc;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | return EOK;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[b7fd2a0] | 270 | static errno_t test1_dev_gone(ddf_dev_t *dev)
|
---|
[80a96d2] | 271 | {
|
---|
[56fd7cf] | 272 | test1_t *test1 = (test1_t *)ddf_dev_data_get(dev);
|
---|
[b7fd2a0] | 273 | errno_t rc;
|
---|
[80a96d2] | 274 |
|
---|
| 275 | ddf_msg(LVL_DEBUG, "test1_dev_remove(%p)", dev);
|
---|
| 276 |
|
---|
| 277 | if (test1->fun_a != NULL) {
|
---|
| 278 | rc = fun_unbind(test1->fun_a, "a");
|
---|
| 279 | if (rc != EOK)
|
---|
| 280 | return rc;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | if (test1->clone != NULL) {
|
---|
| 284 | rc = fun_unbind(test1->clone, "clone");
|
---|
| 285 | if (rc != EOK)
|
---|
| 286 | return rc;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | if (test1->child != NULL) {
|
---|
| 290 | rc = fun_unbind(test1->child, "child");
|
---|
| 291 | if (rc != EOK)
|
---|
| 292 | return rc;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | return EOK;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[b7fd2a0] | 298 | static errno_t test1_fun_online(ddf_fun_t *fun)
|
---|
[1a5b252] | 299 | {
|
---|
| 300 | ddf_msg(LVL_DEBUG, "test1_fun_online()");
|
---|
| 301 | return ddf_fun_online(fun);
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[b7fd2a0] | 304 | static errno_t test1_fun_offline(ddf_fun_t *fun)
|
---|
[1a5b252] | 305 | {
|
---|
| 306 | ddf_msg(LVL_DEBUG, "test1_fun_offline()");
|
---|
| 307 | return ddf_fun_offline(fun);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[1b2981aa] | 310 | int main(int argc, char *argv[])
|
---|
| 311 | {
|
---|
| 312 | printf(NAME ": HelenOS test1 virtual device driver\n");
|
---|
[267f235] | 313 | ddf_log_init(NAME);
|
---|
[83a2f43] | 314 | return ddf_driver_main(&test1_driver);
|
---|
[1b2981aa] | 315 | }
|
---|