| [c245f16e] | 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>
|
|---|
| [af6b5157] | 33 | #include <async.h>
|
|---|
| [c245f16e] | 34 | #include <stdio.h>
|
|---|
| 35 | #include <errno.h>
|
|---|
| [1d6dd2a] | 36 | #include <str.h>
|
|---|
| [c245f16e] | 37 | #include <str_error.h>
|
|---|
| [af6b5157] | 38 | #include <ddf/driver.h>
|
|---|
| [fc51296] | 39 | #include <ddf/log.h>
|
|---|
| [c245f16e] | 40 |
|
|---|
| 41 | #define NAME "test2"
|
|---|
| 42 |
|
|---|
| [b7fd2a0] | 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);
|
|---|
| [c245f16e] | 48 |
|
|---|
| 49 | static driver_ops_t driver_ops = {
|
|---|
| [0c0f823b] | 50 | .dev_add = &test2_dev_add,
|
|---|
| [7fff38c1] | 51 | .dev_remove = &test2_dev_remove,
|
|---|
| [80a96d2] | 52 | .dev_gone = &test2_dev_gone,
|
|---|
| [7fff38c1] | 53 | .fun_online = &test2_fun_online,
|
|---|
| 54 | .fun_offline = &test2_fun_offline
|
|---|
| [c245f16e] | 55 | };
|
|---|
| 56 |
|
|---|
| [bab6388] | 57 | static driver_t test2_driver = {
|
|---|
| [c245f16e] | 58 | .name = NAME,
|
|---|
| 59 | .driver_ops = &driver_ops
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| [7fff38c1] | 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 |
|
|---|
| [c245f16e] | 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 | */
|
|---|
| [b7fd2a0] | 80 | static errno_t register_fun_verbose(ddf_dev_t *parent, const char *message,
|
|---|
| [7fff38c1] | 81 | const char *name, const char *match_id, int match_score, ddf_fun_t **pfun)
|
|---|
| [c245f16e] | 82 | {
|
|---|
| [83a2f43] | 83 | ddf_fun_t *fun;
|
|---|
| [b7fd2a0] | 84 | errno_t rc;
|
|---|
| [c245f16e] | 85 |
|
|---|
| [ebcb05a] | 86 | ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
|
|---|
| [c245f16e] | 87 |
|
|---|
| [cd0684d] | 88 | fun = ddf_fun_create(parent, fun_inner, name);
|
|---|
| 89 | if (fun == NULL) {
|
|---|
| [ebcb05a] | 90 | ddf_msg(LVL_ERROR, "Failed creating function %s", name);
|
|---|
| [cd0684d] | 91 | return ENOMEM;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | rc = ddf_fun_add_match_id(fun, match_id, match_score);
|
|---|
| 95 | if (rc != EOK) {
|
|---|
| [ebcb05a] | 96 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
|---|
| [fc51296] | 97 | name);
|
|---|
| [cd0684d] | 98 | ddf_fun_destroy(fun);
|
|---|
| 99 | return rc;
|
|---|
| [c245f16e] | 100 | }
|
|---|
| [cd0684d] | 101 |
|
|---|
| 102 | rc = ddf_fun_bind(fun);
|
|---|
| 103 | if (rc != EOK) {
|
|---|
| [ebcb05a] | 104 | ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
|
|---|
| [cd0684d] | 105 | str_error(rc));
|
|---|
| 106 | ddf_fun_destroy(fun);
|
|---|
| 107 | return rc;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| [7fff38c1] | 110 | *pfun = fun;
|
|---|
| 111 |
|
|---|
| [ebcb05a] | 112 | ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
|
|---|
| [cd0684d] | 113 | return EOK;
|
|---|
| [c245f16e] | 114 | }
|
|---|
| 115 |
|
|---|
| [80a96d2] | 116 | /** Simulate plugging and surprise unplugging.
|
|---|
| [c245f16e] | 117 | *
|
|---|
| [83a2f43] | 118 | * @param arg Parent device structure (ddf_dev_t *).
|
|---|
| [c245f16e] | 119 | * @return Always EOK.
|
|---|
| 120 | */
|
|---|
| [b7fd2a0] | 121 | static errno_t plug_unplug(void *arg)
|
|---|
| [c245f16e] | 122 | {
|
|---|
| [7fff38c1] | 123 | test2_t *test2 = (test2_t *) arg;
|
|---|
| [83a2f43] | 124 | ddf_fun_t *fun_a;
|
|---|
| [b7fd2a0] | 125 | errno_t rc;
|
|---|
| [c245f16e] | 126 |
|
|---|
| 127 | async_usleep(1000);
|
|---|
| 128 |
|
|---|
| [7fff38c1] | 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);
|
|---|
| [c245f16e] | 133 |
|
|---|
| [7fff38c1] | 134 | fun_a = ddf_fun_create(test2->dev, fun_exposed, "a");
|
|---|
| [97a62fe] | 135 | if (fun_a == NULL) {
|
|---|
| [ebcb05a] | 136 | ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
|
|---|
| [97a62fe] | 137 | return ENOMEM;
|
|---|
| 138 | }
|
|---|
| [8b1e15ac] | 139 |
|
|---|
| [97a62fe] | 140 | rc = ddf_fun_bind(fun_a);
|
|---|
| 141 | if (rc != EOK) {
|
|---|
| [ebcb05a] | 142 | ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
|
|---|
| [97a62fe] | 143 | return rc;
|
|---|
| 144 | }
|
|---|
| [8b1e15ac] | 145 |
|
|---|
| [1dc4a5e] | 146 | ddf_fun_add_to_category(fun_a, "virtual");
|
|---|
| [7fff38c1] | 147 | test2->fun_a = fun_a;
|
|---|
| [c245f16e] | 148 |
|
|---|
| [80a96d2] | 149 | async_usleep(10000000);
|
|---|
| 150 |
|
|---|
| 151 | ddf_msg(LVL_NOTE, "Unbinding function test1.");
|
|---|
| 152 | ddf_fun_unbind(test2->test1);
|
|---|
| 153 | async_usleep(1000000);
|
|---|
| 154 | ddf_msg(LVL_NOTE, "Unbinding function child.");
|
|---|
| 155 | ddf_fun_unbind(test2->child);
|
|---|
| 156 |
|
|---|
| [c245f16e] | 157 | return EOK;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| [b7fd2a0] | 160 | static errno_t fun_remove(ddf_fun_t *fun, const char *name)
|
|---|
| [7fff38c1] | 161 | {
|
|---|
| [b7fd2a0] | 162 | errno_t rc;
|
|---|
| [7fff38c1] | 163 |
|
|---|
| [deac215e] | 164 | ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
|
|---|
| [7fff38c1] | 165 | rc = ddf_fun_offline(fun);
|
|---|
| 166 | if (rc != EOK) {
|
|---|
| 167 | ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
|
|---|
| 168 | return rc;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | rc = ddf_fun_unbind(fun);
|
|---|
| 172 | if (rc != EOK) {
|
|---|
| 173 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
|
|---|
| 174 | return rc;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | ddf_fun_destroy(fun);
|
|---|
| 178 | return EOK;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| [b7fd2a0] | 181 | static errno_t fun_unbind(ddf_fun_t *fun, const char *name)
|
|---|
| [80a96d2] | 182 | {
|
|---|
| [b7fd2a0] | 183 | errno_t rc;
|
|---|
| [80a96d2] | 184 |
|
|---|
| 185 | ddf_msg(LVL_DEBUG, "fun_unbind(%p, '%s')", fun, name);
|
|---|
| 186 | rc = ddf_fun_unbind(fun);
|
|---|
| 187 | if (rc != EOK) {
|
|---|
| 188 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
|
|---|
| 189 | return rc;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | ddf_fun_destroy(fun);
|
|---|
| 193 | return EOK;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| [b7fd2a0] | 196 | static errno_t test2_dev_add(ddf_dev_t *dev)
|
|---|
| [c245f16e] | 197 | {
|
|---|
| [7fff38c1] | 198 | test2_t *test2;
|
|---|
| [56fd7cf] | 199 | const char *dev_name = ddf_dev_get_name(dev);
|
|---|
| [7fff38c1] | 200 |
|
|---|
| [0c0f823b] | 201 | ddf_msg(LVL_DEBUG, "test2_dev_add(name=\"%s\", handle=%d)",
|
|---|
| [56fd7cf] | 202 | dev_name, (int) ddf_dev_get_handle(dev));
|
|---|
| [c245f16e] | 203 |
|
|---|
| [7fff38c1] | 204 | test2 = ddf_dev_data_alloc(dev, sizeof(test2_t));
|
|---|
| 205 | if (test2 == NULL) {
|
|---|
| 206 | ddf_msg(LVL_ERROR, "Failed allocating soft state.");
|
|---|
| 207 | return ENOMEM;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | test2->dev = dev;
|
|---|
| 211 |
|
|---|
| [56fd7cf] | 212 | if (str_cmp(dev_name, "child") != 0) {
|
|---|
| [80a96d2] | 213 | fid_t postpone = fibril_create(plug_unplug, test2);
|
|---|
| [86d7bfa] | 214 | if (postpone == 0) {
|
|---|
| [ebcb05a] | 215 | ddf_msg(LVL_ERROR, "fibril_create() failed.");
|
|---|
| [86d7bfa] | 216 | return ENOMEM;
|
|---|
| 217 | }
|
|---|
| [c245f16e] | 218 | fibril_add_ready(postpone);
|
|---|
| 219 | } else {
|
|---|
| [cd0684d] | 220 | (void) register_fun_verbose(dev, "child without available driver",
|
|---|
| [7fff38c1] | 221 | "ERROR", "non-existent.match.id", 10, &test2->fun_err);
|
|---|
| [c245f16e] | 222 | }
|
|---|
| 223 |
|
|---|
| 224 | return EOK;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| [b7fd2a0] | 227 | static errno_t test2_dev_remove(ddf_dev_t *dev)
|
|---|
| [7fff38c1] | 228 | {
|
|---|
| [56fd7cf] | 229 | test2_t *test2 = (test2_t *)ddf_dev_data_get(dev);
|
|---|
| [b7fd2a0] | 230 | errno_t rc;
|
|---|
| [7fff38c1] | 231 |
|
|---|
| 232 | ddf_msg(LVL_DEBUG, "test2_dev_remove(%p)", dev);
|
|---|
| 233 |
|
|---|
| 234 | if (test2->fun_a != NULL) {
|
|---|
| 235 | rc = fun_remove(test2->fun_a, "a");
|
|---|
| 236 | if (rc != EOK)
|
|---|
| 237 | return rc;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | if (test2->fun_err != NULL) {
|
|---|
| 241 | rc = fun_remove(test2->fun_err, "ERROR");
|
|---|
| 242 | if (rc != EOK)
|
|---|
| 243 | return rc;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | if (test2->child != NULL) {
|
|---|
| 247 | rc = fun_remove(test2->child, "child");
|
|---|
| 248 | if (rc != EOK)
|
|---|
| 249 | return rc;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | if (test2->test1 != NULL) {
|
|---|
| 253 | rc = fun_remove(test2->test1, "test1");
|
|---|
| 254 | if (rc != EOK)
|
|---|
| 255 | return rc;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | return EOK;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| [b7fd2a0] | 261 | static errno_t test2_dev_gone(ddf_dev_t *dev)
|
|---|
| [80a96d2] | 262 | {
|
|---|
| [56fd7cf] | 263 | test2_t *test2 = (test2_t *)ddf_dev_data_get(dev);
|
|---|
| [b7fd2a0] | 264 | errno_t rc;
|
|---|
| [80a96d2] | 265 |
|
|---|
| 266 | ddf_msg(LVL_DEBUG, "test2_dev_gone(%p)", dev);
|
|---|
| 267 |
|
|---|
| 268 | if (test2->fun_a != NULL) {
|
|---|
| 269 | rc = fun_unbind(test2->fun_a, "a");
|
|---|
| 270 | if (rc != EOK)
|
|---|
| 271 | return rc;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | if (test2->fun_err != NULL) {
|
|---|
| 275 | rc = fun_unbind(test2->fun_err, "ERROR");
|
|---|
| 276 | if (rc != EOK)
|
|---|
| 277 | return rc;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | if (test2->child != NULL) {
|
|---|
| 281 | rc = fun_unbind(test2->child, "child");
|
|---|
| 282 | if (rc != EOK)
|
|---|
| 283 | return rc;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | if (test2->test1 != NULL) {
|
|---|
| 287 | rc = fun_unbind(test2->test1, "test1");
|
|---|
| 288 | if (rc != EOK)
|
|---|
| 289 | return rc;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | return EOK;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| [7fff38c1] | 295 |
|
|---|
| [b7fd2a0] | 296 | static errno_t test2_fun_online(ddf_fun_t *fun)
|
|---|
| [7fff38c1] | 297 | {
|
|---|
| 298 | ddf_msg(LVL_DEBUG, "test2_fun_online()");
|
|---|
| 299 | return ddf_fun_online(fun);
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| [b7fd2a0] | 302 | static errno_t test2_fun_offline(ddf_fun_t *fun)
|
|---|
| [7fff38c1] | 303 | {
|
|---|
| 304 | ddf_msg(LVL_DEBUG, "test2_fun_offline()");
|
|---|
| 305 | return ddf_fun_offline(fun);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| [c245f16e] | 308 | int main(int argc, char *argv[])
|
|---|
| 309 | {
|
|---|
| 310 | printf(NAME ": HelenOS test2 virtual device driver\n");
|
|---|
| [267f235] | 311 | ddf_log_init(NAME);
|
|---|
| [83a2f43] | 312 | return ddf_driver_main(&test2_driver);
|
|---|
| [c245f16e] | 313 | }
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|