| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @file
|
|---|
| 30 | */
|
|---|
| 31 |
|
|---|
| 32 | #include <assert.h>
|
|---|
| 33 | #include <stdio.h>
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <fibril.h>
|
|---|
| 36 | #include <str.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <ddf/driver.h>
|
|---|
| 39 | #include <ddf/log.h>
|
|---|
| 40 |
|
|---|
| 41 | #define NAME "test2"
|
|---|
| 42 |
|
|---|
| 43 | static errno_t test2_dev_add(ddf_dev_t *dev);
|
|---|
| 44 | static errno_t test2_dev_remove(ddf_dev_t *dev);
|
|---|
| 45 | static errno_t test2_dev_gone(ddf_dev_t *dev);
|
|---|
| 46 | static errno_t test2_fun_online(ddf_fun_t *fun);
|
|---|
| 47 | static errno_t test2_fun_offline(ddf_fun_t *fun);
|
|---|
| 48 |
|
|---|
| 49 | static driver_ops_t driver_ops = {
|
|---|
| 50 | .dev_add = &test2_dev_add,
|
|---|
| 51 | .dev_remove = &test2_dev_remove,
|
|---|
| 52 | .dev_gone = &test2_dev_gone,
|
|---|
| 53 | .fun_online = &test2_fun_online,
|
|---|
| 54 | .fun_offline = &test2_fun_offline
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | static driver_t test2_driver = {
|
|---|
| 58 | .name = NAME,
|
|---|
| 59 | .driver_ops = &driver_ops
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | /* Device soft state */
|
|---|
| 63 | typedef struct {
|
|---|
| 64 | ddf_dev_t *dev;
|
|---|
| 65 |
|
|---|
| 66 | ddf_fun_t *fun_a;
|
|---|
| 67 | ddf_fun_t *fun_err;
|
|---|
| 68 | ddf_fun_t *child;
|
|---|
| 69 | ddf_fun_t *test1;
|
|---|
| 70 | } test2_t;
|
|---|
| 71 |
|
|---|
| 72 | /** Register child and inform user about it.
|
|---|
| 73 | *
|
|---|
| 74 | * @param parent Parent device.
|
|---|
| 75 | * @param message Message for the user.
|
|---|
| 76 | * @param name Device name.
|
|---|
| 77 | * @param match_id Device match id.
|
|---|
| 78 | * @param score Device match score.
|
|---|
| 79 | */
|
|---|
| 80 | static errno_t register_fun_verbose(ddf_dev_t *parent, const char *message,
|
|---|
| 81 | const char *name, const char *match_id, int match_score, ddf_fun_t **pfun)
|
|---|
| 82 | {
|
|---|
| 83 | ddf_fun_t *fun;
|
|---|
| 84 | errno_t rc;
|
|---|
| 85 |
|
|---|
| 86 | ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
|
|---|
| 87 |
|
|---|
| 88 | fun = ddf_fun_create(parent, fun_inner, name);
|
|---|
| 89 | if (fun == NULL) {
|
|---|
| 90 | ddf_msg(LVL_ERROR, "Failed creating function %s", name);
|
|---|
| 91 | return ENOMEM;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | rc = ddf_fun_add_match_id(fun, match_id, match_score);
|
|---|
| 95 | if (rc != EOK) {
|
|---|
| 96 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
|---|
| 97 | name);
|
|---|
| 98 | ddf_fun_destroy(fun);
|
|---|
| 99 | return rc;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | rc = ddf_fun_bind(fun);
|
|---|
| 103 | if (rc != EOK) {
|
|---|
| 104 | ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
|
|---|
| 105 | str_error(rc));
|
|---|
| 106 | ddf_fun_destroy(fun);
|
|---|
| 107 | return rc;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | *pfun = fun;
|
|---|
| 111 |
|
|---|
| 112 | ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
|
|---|
| 113 | return EOK;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** Simulate plugging and surprise unplugging.
|
|---|
| 117 | *
|
|---|
| 118 | * @param arg Parent device structure (ddf_dev_t *).
|
|---|
| 119 | * @return Always EOK.
|
|---|
| 120 | */
|
|---|
| 121 | static errno_t plug_unplug(void *arg)
|
|---|
| 122 | {
|
|---|
| 123 | test2_t *test2 = (test2_t *) arg;
|
|---|
| 124 | ddf_fun_t *fun_a;
|
|---|
| 125 | errno_t rc;
|
|---|
| 126 |
|
|---|
| 127 | fibril_usleep(1000);
|
|---|
| 128 |
|
|---|
| 129 | (void) register_fun_verbose(test2->dev, "child driven by the same task",
|
|---|
| 130 | "child", "virtual&test2", 10, &test2->child);
|
|---|
| 131 | (void) register_fun_verbose(test2->dev, "child driven by test1",
|
|---|
| 132 | "test1", "virtual&test1", 10, &test2->test1);
|
|---|
| 133 |
|
|---|
| 134 | fun_a = ddf_fun_create(test2->dev, fun_exposed, "a");
|
|---|
| 135 | if (fun_a == NULL) {
|
|---|
| 136 | ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
|
|---|
| 137 | return ENOMEM;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | rc = ddf_fun_bind(fun_a);
|
|---|
| 141 | if (rc != EOK) {
|
|---|
| 142 | ddf_fun_destroy(fun_a);
|
|---|
| 143 | ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
|
|---|
| 144 | return rc;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | rc = ddf_fun_add_to_category(fun_a, "virtual");
|
|---|
| 148 | if (rc != EOK) {
|
|---|
| 149 | ddf_fun_unbind(fun_a);
|
|---|
| 150 | ddf_fun_destroy(fun_a);
|
|---|
| 151 | ddf_msg(LVL_ERROR, "Failed adding function 'a' to category "
|
|---|
| 152 | "'virtual'.");
|
|---|
| 153 | return rc;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | test2->fun_a = fun_a;
|
|---|
| 157 |
|
|---|
| 158 | fibril_usleep(10000000);
|
|---|
| 159 |
|
|---|
| 160 | ddf_msg(LVL_NOTE, "Unbinding function test1.");
|
|---|
| 161 | ddf_fun_unbind(test2->test1);
|
|---|
| 162 | fibril_usleep(1000000);
|
|---|
| 163 | ddf_msg(LVL_NOTE, "Unbinding function child.");
|
|---|
| 164 | ddf_fun_unbind(test2->child);
|
|---|
| 165 |
|
|---|
| 166 | return EOK;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | static errno_t fun_remove(ddf_fun_t *fun, const char *name)
|
|---|
| 170 | {
|
|---|
| 171 | errno_t rc;
|
|---|
| 172 |
|
|---|
| 173 | ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
|
|---|
| 174 | rc = ddf_fun_offline(fun);
|
|---|
| 175 | if (rc != EOK) {
|
|---|
| 176 | ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
|
|---|
| 177 | return rc;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | rc = ddf_fun_unbind(fun);
|
|---|
| 181 | if (rc != EOK) {
|
|---|
| 182 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
|
|---|
| 183 | return rc;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | ddf_fun_destroy(fun);
|
|---|
| 187 | return EOK;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | static errno_t fun_unbind(ddf_fun_t *fun, const char *name)
|
|---|
| 191 | {
|
|---|
| 192 | errno_t rc;
|
|---|
| 193 |
|
|---|
| 194 | ddf_msg(LVL_DEBUG, "fun_unbind(%p, '%s')", fun, name);
|
|---|
| 195 | rc = ddf_fun_unbind(fun);
|
|---|
| 196 | if (rc != EOK) {
|
|---|
| 197 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
|
|---|
| 198 | return rc;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | ddf_fun_destroy(fun);
|
|---|
| 202 | return EOK;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | static errno_t test2_dev_add(ddf_dev_t *dev)
|
|---|
| 206 | {
|
|---|
| 207 | test2_t *test2;
|
|---|
| 208 | const char *dev_name = ddf_dev_get_name(dev);
|
|---|
| 209 |
|
|---|
| 210 | ddf_msg(LVL_DEBUG, "test2_dev_add(name=\"%s\", handle=%d)",
|
|---|
| 211 | dev_name, (int) ddf_dev_get_handle(dev));
|
|---|
| 212 |
|
|---|
| 213 | test2 = ddf_dev_data_alloc(dev, sizeof(test2_t));
|
|---|
| 214 | if (test2 == NULL) {
|
|---|
| 215 | ddf_msg(LVL_ERROR, "Failed allocating soft state.");
|
|---|
| 216 | return ENOMEM;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | test2->dev = dev;
|
|---|
| 220 |
|
|---|
| 221 | if (str_cmp(dev_name, "child") != 0) {
|
|---|
| 222 | fid_t postpone = fibril_create(plug_unplug, test2);
|
|---|
| 223 | if (postpone == 0) {
|
|---|
| 224 | ddf_msg(LVL_ERROR, "fibril_create() failed.");
|
|---|
| 225 | return ENOMEM;
|
|---|
| 226 | }
|
|---|
| 227 | fibril_add_ready(postpone);
|
|---|
| 228 | } else {
|
|---|
| 229 | (void) register_fun_verbose(dev, "child without available driver",
|
|---|
| 230 | "ERROR", "non-existent.match.id", 10, &test2->fun_err);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | return EOK;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | static errno_t test2_dev_remove(ddf_dev_t *dev)
|
|---|
| 237 | {
|
|---|
| 238 | test2_t *test2 = (test2_t *)ddf_dev_data_get(dev);
|
|---|
| 239 | errno_t rc;
|
|---|
| 240 |
|
|---|
| 241 | ddf_msg(LVL_DEBUG, "test2_dev_remove(%p)", dev);
|
|---|
| 242 |
|
|---|
| 243 | if (test2->fun_a != NULL) {
|
|---|
| 244 | rc = fun_remove(test2->fun_a, "a");
|
|---|
| 245 | if (rc != EOK)
|
|---|
| 246 | return rc;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | if (test2->fun_err != NULL) {
|
|---|
| 250 | rc = fun_remove(test2->fun_err, "ERROR");
|
|---|
| 251 | if (rc != EOK)
|
|---|
| 252 | return rc;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | if (test2->child != NULL) {
|
|---|
| 256 | rc = fun_remove(test2->child, "child");
|
|---|
| 257 | if (rc != EOK)
|
|---|
| 258 | return rc;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | if (test2->test1 != NULL) {
|
|---|
| 262 | rc = fun_remove(test2->test1, "test1");
|
|---|
| 263 | if (rc != EOK)
|
|---|
| 264 | return rc;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | return EOK;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | static errno_t test2_dev_gone(ddf_dev_t *dev)
|
|---|
| 271 | {
|
|---|
| 272 | test2_t *test2 = (test2_t *)ddf_dev_data_get(dev);
|
|---|
| 273 | errno_t rc;
|
|---|
| 274 |
|
|---|
| 275 | ddf_msg(LVL_DEBUG, "test2_dev_gone(%p)", dev);
|
|---|
| 276 |
|
|---|
| 277 | if (test2->fun_a != NULL) {
|
|---|
| 278 | rc = fun_unbind(test2->fun_a, "a");
|
|---|
| 279 | if (rc != EOK)
|
|---|
| 280 | return rc;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | if (test2->fun_err != NULL) {
|
|---|
| 284 | rc = fun_unbind(test2->fun_err, "ERROR");
|
|---|
| 285 | if (rc != EOK)
|
|---|
| 286 | return rc;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | if (test2->child != NULL) {
|
|---|
| 290 | rc = fun_unbind(test2->child, "child");
|
|---|
| 291 | if (rc != EOK)
|
|---|
| 292 | return rc;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | if (test2->test1 != NULL) {
|
|---|
| 296 | rc = fun_unbind(test2->test1, "test1");
|
|---|
| 297 | if (rc != EOK)
|
|---|
| 298 | return rc;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | return EOK;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | static errno_t test2_fun_online(ddf_fun_t *fun)
|
|---|
| 305 | {
|
|---|
| 306 | ddf_msg(LVL_DEBUG, "test2_fun_online()");
|
|---|
| 307 | return ddf_fun_online(fun);
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | static errno_t test2_fun_offline(ddf_fun_t *fun)
|
|---|
| 311 | {
|
|---|
| 312 | ddf_msg(LVL_DEBUG, "test2_fun_offline()");
|
|---|
| 313 | return ddf_fun_offline(fun);
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | int main(int argc, char *argv[])
|
|---|
| 317 | {
|
|---|
| 318 | printf(NAME ": HelenOS test2 virtual device driver\n");
|
|---|
| 319 | ddf_log_init(NAME);
|
|---|
| 320 | return ddf_driver_main(&test2_driver);
|
|---|
| 321 | }
|
|---|