| [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>
|
|---|
| 36 | #include <str_error.h>
|
|---|
| [af6b5157] | 37 | #include <ddf/driver.h>
|
|---|
| [fc51296] | 38 | #include <ddf/log.h>
|
|---|
| [c245f16e] | 39 |
|
|---|
| 40 | #define NAME "test2"
|
|---|
| 41 |
|
|---|
| [83a2f43] | 42 | static int test2_add_device(ddf_dev_t *dev);
|
|---|
| [7fff38c1] | 43 | static int test2_dev_remove(ddf_dev_t *dev);
|
|---|
| 44 | static int test2_fun_online(ddf_fun_t *fun);
|
|---|
| 45 | static int test2_fun_offline(ddf_fun_t *fun);
|
|---|
| [c245f16e] | 46 |
|
|---|
| 47 | static driver_ops_t driver_ops = {
|
|---|
| [7fff38c1] | 48 | .add_device = &test2_add_device,
|
|---|
| 49 | .dev_remove = &test2_dev_remove,
|
|---|
| 50 | .fun_online = &test2_fun_online,
|
|---|
| 51 | .fun_offline = &test2_fun_offline
|
|---|
| [c245f16e] | 52 | };
|
|---|
| 53 |
|
|---|
| [bab6388] | 54 | static driver_t test2_driver = {
|
|---|
| [c245f16e] | 55 | .name = NAME,
|
|---|
| 56 | .driver_ops = &driver_ops
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| [7fff38c1] | 59 | /* Device soft state */
|
|---|
| 60 | typedef struct {
|
|---|
| 61 | ddf_dev_t *dev;
|
|---|
| 62 |
|
|---|
| 63 | ddf_fun_t *fun_a;
|
|---|
| 64 | ddf_fun_t *fun_err;
|
|---|
| 65 | ddf_fun_t *child;
|
|---|
| 66 | ddf_fun_t *test1;
|
|---|
| 67 | } test2_t;
|
|---|
| 68 |
|
|---|
| [c245f16e] | 69 | /** Register child and inform user about it.
|
|---|
| 70 | *
|
|---|
| 71 | * @param parent Parent device.
|
|---|
| 72 | * @param message Message for the user.
|
|---|
| 73 | * @param name Device name.
|
|---|
| 74 | * @param match_id Device match id.
|
|---|
| 75 | * @param score Device match score.
|
|---|
| 76 | */
|
|---|
| [83a2f43] | 77 | static int register_fun_verbose(ddf_dev_t *parent, const char *message,
|
|---|
| [7fff38c1] | 78 | const char *name, const char *match_id, int match_score, ddf_fun_t **pfun)
|
|---|
| [c245f16e] | 79 | {
|
|---|
| [83a2f43] | 80 | ddf_fun_t *fun;
|
|---|
| [cd0684d] | 81 | int rc;
|
|---|
| [c245f16e] | 82 |
|
|---|
| [ebcb05a] | 83 | ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
|
|---|
| [c245f16e] | 84 |
|
|---|
| [cd0684d] | 85 | fun = ddf_fun_create(parent, fun_inner, name);
|
|---|
| 86 | if (fun == NULL) {
|
|---|
| [ebcb05a] | 87 | ddf_msg(LVL_ERROR, "Failed creating function %s", name);
|
|---|
| [cd0684d] | 88 | return ENOMEM;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | rc = ddf_fun_add_match_id(fun, match_id, match_score);
|
|---|
| 92 | if (rc != EOK) {
|
|---|
| [ebcb05a] | 93 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
|---|
| [fc51296] | 94 | name);
|
|---|
| [cd0684d] | 95 | ddf_fun_destroy(fun);
|
|---|
| 96 | return rc;
|
|---|
| [c245f16e] | 97 | }
|
|---|
| [cd0684d] | 98 |
|
|---|
| 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));
|
|---|
| 103 | ddf_fun_destroy(fun);
|
|---|
| 104 | return rc;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| [7fff38c1] | 107 | *pfun = fun;
|
|---|
| 108 |
|
|---|
| [ebcb05a] | 109 | ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
|
|---|
| [cd0684d] | 110 | return EOK;
|
|---|
| [c245f16e] | 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /** Add child devices after some sleep.
|
|---|
| 114 | *
|
|---|
| [83a2f43] | 115 | * @param arg Parent device structure (ddf_dev_t *).
|
|---|
| [c245f16e] | 116 | * @return Always EOK.
|
|---|
| 117 | */
|
|---|
| 118 | static int postponed_birth(void *arg)
|
|---|
| 119 | {
|
|---|
| [7fff38c1] | 120 | test2_t *test2 = (test2_t *) arg;
|
|---|
| [83a2f43] | 121 | ddf_fun_t *fun_a;
|
|---|
| [97a62fe] | 122 | int rc;
|
|---|
| [c245f16e] | 123 |
|
|---|
| 124 | async_usleep(1000);
|
|---|
| 125 |
|
|---|
| [7fff38c1] | 126 | (void) register_fun_verbose(test2->dev, "child driven by the same task",
|
|---|
| 127 | "child", "virtual&test2", 10, &test2->child);
|
|---|
| 128 | (void) register_fun_verbose(test2->dev, "child driven by test1",
|
|---|
| 129 | "test1", "virtual&test1", 10, &test2->test1);
|
|---|
| [c245f16e] | 130 |
|
|---|
| [7fff38c1] | 131 | fun_a = ddf_fun_create(test2->dev, fun_exposed, "a");
|
|---|
| [97a62fe] | 132 | if (fun_a == NULL) {
|
|---|
| [ebcb05a] | 133 | ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
|
|---|
| [97a62fe] | 134 | return ENOMEM;
|
|---|
| 135 | }
|
|---|
| [8b1e15ac] | 136 |
|
|---|
| [97a62fe] | 137 | rc = ddf_fun_bind(fun_a);
|
|---|
| 138 | if (rc != EOK) {
|
|---|
| [ebcb05a] | 139 | ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
|
|---|
| [97a62fe] | 140 | return rc;
|
|---|
| 141 | }
|
|---|
| [8b1e15ac] | 142 |
|
|---|
| [1dc4a5e] | 143 | ddf_fun_add_to_category(fun_a, "virtual");
|
|---|
| [7fff38c1] | 144 | test2->fun_a = fun_a;
|
|---|
| [c245f16e] | 145 |
|
|---|
| 146 | return EOK;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| [7fff38c1] | 149 | static int fun_remove(ddf_fun_t *fun, const char *name)
|
|---|
| 150 | {
|
|---|
| 151 | int rc;
|
|---|
| 152 |
|
|---|
| [deac215e] | 153 | ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
|
|---|
| [7fff38c1] | 154 | rc = ddf_fun_offline(fun);
|
|---|
| 155 | if (rc != EOK) {
|
|---|
| 156 | ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
|
|---|
| 157 | return rc;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | rc = ddf_fun_unbind(fun);
|
|---|
| 161 | if (rc != EOK) {
|
|---|
| 162 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
|
|---|
| 163 | return rc;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | ddf_fun_destroy(fun);
|
|---|
| 167 | return EOK;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| [83a2f43] | 170 | static int test2_add_device(ddf_dev_t *dev)
|
|---|
| [c245f16e] | 171 | {
|
|---|
| [7fff38c1] | 172 | test2_t *test2;
|
|---|
| 173 |
|
|---|
| [ebcb05a] | 174 | ddf_msg(LVL_DEBUG, "test2_add_device(name=\"%s\", handle=%d)",
|
|---|
| [c245f16e] | 175 | dev->name, (int) dev->handle);
|
|---|
| 176 |
|
|---|
| [7fff38c1] | 177 | test2 = ddf_dev_data_alloc(dev, sizeof(test2_t));
|
|---|
| 178 | if (test2 == NULL) {
|
|---|
| 179 | ddf_msg(LVL_ERROR, "Failed allocating soft state.");
|
|---|
| 180 | return ENOMEM;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | test2->dev = dev;
|
|---|
| 184 |
|
|---|
| [8b1e15ac] | 185 | if (str_cmp(dev->name, "child") != 0) {
|
|---|
| [7fff38c1] | 186 | fid_t postpone = fibril_create(postponed_birth, test2);
|
|---|
| [86d7bfa] | 187 | if (postpone == 0) {
|
|---|
| [ebcb05a] | 188 | ddf_msg(LVL_ERROR, "fibril_create() failed.");
|
|---|
| [86d7bfa] | 189 | return ENOMEM;
|
|---|
| 190 | }
|
|---|
| [c245f16e] | 191 | fibril_add_ready(postpone);
|
|---|
| 192 | } else {
|
|---|
| [cd0684d] | 193 | (void) register_fun_verbose(dev, "child without available driver",
|
|---|
| [7fff38c1] | 194 | "ERROR", "non-existent.match.id", 10, &test2->fun_err);
|
|---|
| [c245f16e] | 195 | }
|
|---|
| 196 |
|
|---|
| 197 | return EOK;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| [7fff38c1] | 200 | static int test2_dev_remove(ddf_dev_t *dev)
|
|---|
| 201 | {
|
|---|
| 202 | test2_t *test2 = (test2_t *)dev->driver_data;
|
|---|
| 203 | int rc;
|
|---|
| 204 |
|
|---|
| 205 | ddf_msg(LVL_DEBUG, "test2_dev_remove(%p)", dev);
|
|---|
| 206 |
|
|---|
| 207 | if (test2->fun_a != NULL) {
|
|---|
| 208 | rc = fun_remove(test2->fun_a, "a");
|
|---|
| 209 | if (rc != EOK)
|
|---|
| 210 | return rc;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | if (test2->fun_err != NULL) {
|
|---|
| 214 | rc = fun_remove(test2->fun_err, "ERROR");
|
|---|
| 215 | if (rc != EOK)
|
|---|
| 216 | return rc;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | if (test2->child != NULL) {
|
|---|
| 220 | rc = fun_remove(test2->child, "child");
|
|---|
| 221 | if (rc != EOK)
|
|---|
| 222 | return rc;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | if (test2->test1 != NULL) {
|
|---|
| 226 | rc = fun_remove(test2->test1, "test1");
|
|---|
| 227 | if (rc != EOK)
|
|---|
| 228 | return rc;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | return EOK;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 | static int test2_fun_online(ddf_fun_t *fun)
|
|---|
| 236 | {
|
|---|
| 237 | ddf_msg(LVL_DEBUG, "test2_fun_online()");
|
|---|
| 238 | return ddf_fun_online(fun);
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | static int test2_fun_offline(ddf_fun_t *fun)
|
|---|
| 242 | {
|
|---|
| 243 | ddf_msg(LVL_DEBUG, "test2_fun_offline()");
|
|---|
| 244 | return ddf_fun_offline(fun);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| [c245f16e] | 247 | int main(int argc, char *argv[])
|
|---|
| 248 | {
|
|---|
| 249 | printf(NAME ": HelenOS test2 virtual device driver\n");
|
|---|
| [fc51296] | 250 | ddf_log_init(NAME, LVL_ERROR);
|
|---|
| [83a2f43] | 251 | return ddf_driver_main(&test2_driver);
|
|---|
| [c245f16e] | 252 | }
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|