[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>
|
---|
| 33 | #include <stdio.h>
|
---|
| 34 | #include <errno.h>
|
---|
[fec7ba0] | 35 | #include <fibril.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 |
|
---|
[5f97ef44] | 127 | fibril_usleep(1000);
|
---|
[c245f16e] | 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) {
|
---|
[4f87a85a] | 142 | ddf_fun_destroy(fun_a);
|
---|
[ebcb05a] | 143 | ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
|
---|
[97a62fe] | 144 | return rc;
|
---|
| 145 | }
|
---|
[8b1e15ac] | 146 |
|
---|
[4f87a85a] | 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 |
|
---|
[7fff38c1] | 156 | test2->fun_a = fun_a;
|
---|
[c245f16e] | 157 |
|
---|
[5f97ef44] | 158 | fibril_usleep(10000000);
|
---|
[80a96d2] | 159 |
|
---|
| 160 | ddf_msg(LVL_NOTE, "Unbinding function test1.");
|
---|
| 161 | ddf_fun_unbind(test2->test1);
|
---|
[5f97ef44] | 162 | fibril_usleep(1000000);
|
---|
[80a96d2] | 163 | ddf_msg(LVL_NOTE, "Unbinding function child.");
|
---|
| 164 | ddf_fun_unbind(test2->child);
|
---|
| 165 |
|
---|
[c245f16e] | 166 | return EOK;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[b7fd2a0] | 169 | static errno_t fun_remove(ddf_fun_t *fun, const char *name)
|
---|
[7fff38c1] | 170 | {
|
---|
[b7fd2a0] | 171 | errno_t rc;
|
---|
[7fff38c1] | 172 |
|
---|
[deac215e] | 173 | ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
|
---|
[7fff38c1] | 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 |
|
---|
[b7fd2a0] | 190 | static errno_t fun_unbind(ddf_fun_t *fun, const char *name)
|
---|
[80a96d2] | 191 | {
|
---|
[b7fd2a0] | 192 | errno_t rc;
|
---|
[80a96d2] | 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 |
|
---|
[b7fd2a0] | 205 | static errno_t test2_dev_add(ddf_dev_t *dev)
|
---|
[c245f16e] | 206 | {
|
---|
[7fff38c1] | 207 | test2_t *test2;
|
---|
[56fd7cf] | 208 | const char *dev_name = ddf_dev_get_name(dev);
|
---|
[7fff38c1] | 209 |
|
---|
[0c0f823b] | 210 | ddf_msg(LVL_DEBUG, "test2_dev_add(name=\"%s\", handle=%d)",
|
---|
[56fd7cf] | 211 | dev_name, (int) ddf_dev_get_handle(dev));
|
---|
[c245f16e] | 212 |
|
---|
[7fff38c1] | 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 |
|
---|
[56fd7cf] | 221 | if (str_cmp(dev_name, "child") != 0) {
|
---|
[80a96d2] | 222 | fid_t postpone = fibril_create(plug_unplug, test2);
|
---|
[86d7bfa] | 223 | if (postpone == 0) {
|
---|
[ebcb05a] | 224 | ddf_msg(LVL_ERROR, "fibril_create() failed.");
|
---|
[86d7bfa] | 225 | return ENOMEM;
|
---|
| 226 | }
|
---|
[c245f16e] | 227 | fibril_add_ready(postpone);
|
---|
| 228 | } else {
|
---|
[cd0684d] | 229 | (void) register_fun_verbose(dev, "child without available driver",
|
---|
[7fff38c1] | 230 | "ERROR", "non-existent.match.id", 10, &test2->fun_err);
|
---|
[c245f16e] | 231 | }
|
---|
| 232 |
|
---|
| 233 | return EOK;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[b7fd2a0] | 236 | static errno_t test2_dev_remove(ddf_dev_t *dev)
|
---|
[7fff38c1] | 237 | {
|
---|
[56fd7cf] | 238 | test2_t *test2 = (test2_t *)ddf_dev_data_get(dev);
|
---|
[b7fd2a0] | 239 | errno_t rc;
|
---|
[7fff38c1] | 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 |
|
---|
[b7fd2a0] | 270 | static errno_t test2_dev_gone(ddf_dev_t *dev)
|
---|
[80a96d2] | 271 | {
|
---|
[56fd7cf] | 272 | test2_t *test2 = (test2_t *)ddf_dev_data_get(dev);
|
---|
[b7fd2a0] | 273 | errno_t rc;
|
---|
[80a96d2] | 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 |
|
---|
[b7fd2a0] | 304 | static errno_t test2_fun_online(ddf_fun_t *fun)
|
---|
[7fff38c1] | 305 | {
|
---|
| 306 | ddf_msg(LVL_DEBUG, "test2_fun_online()");
|
---|
| 307 | return ddf_fun_online(fun);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[b7fd2a0] | 310 | static errno_t test2_fun_offline(ddf_fun_t *fun)
|
---|
[7fff38c1] | 311 | {
|
---|
| 312 | ddf_msg(LVL_DEBUG, "test2_fun_offline()");
|
---|
| 313 | return ddf_fun_offline(fun);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[c245f16e] | 316 | int main(int argc, char *argv[])
|
---|
| 317 | {
|
---|
| 318 | printf(NAME ": HelenOS test2 virtual device driver\n");
|
---|
[267f235] | 319 | ddf_log_init(NAME);
|
---|
[83a2f43] | 320 | return ddf_driver_main(&test2_driver);
|
---|
[c245f16e] | 321 | }
|
---|