| 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 <str_error.h>
|
|---|
| 36 | #include <ddf/driver.h>
|
|---|
| 37 | #include <ddf/log.h>
|
|---|
| 38 |
|
|---|
| 39 | #include "test1.h"
|
|---|
| 40 |
|
|---|
| 41 | static int test1_add_device(ddf_dev_t *dev);
|
|---|
| 42 |
|
|---|
| 43 | static driver_ops_t driver_ops = {
|
|---|
| 44 | .add_device = &test1_add_device
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | static driver_t test1_driver = {
|
|---|
| 48 | .name = NAME,
|
|---|
| 49 | .driver_ops = &driver_ops
|
|---|
| 50 | };
|
|---|
| 51 |
|
|---|
| 52 | /** Register child and inform user about it.
|
|---|
| 53 | *
|
|---|
| 54 | * @param parent Parent device.
|
|---|
| 55 | * @param message Message for the user.
|
|---|
| 56 | * @param name Device name.
|
|---|
| 57 | * @param match_id Device match id.
|
|---|
| 58 | * @param score Device match score.
|
|---|
| 59 | */
|
|---|
| 60 | static int register_fun_verbose(ddf_dev_t *parent, const char *message,
|
|---|
| 61 | const char *name, const char *match_id, int match_score,
|
|---|
| 62 | int expected_rc)
|
|---|
| 63 | {
|
|---|
| 64 | ddf_fun_t *fun = NULL;
|
|---|
| 65 | int rc;
|
|---|
| 66 |
|
|---|
| 67 | ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
|
|---|
| 68 |
|
|---|
| 69 | fun = ddf_fun_create(parent, fun_inner, name);
|
|---|
| 70 | if (fun == NULL) {
|
|---|
| 71 | ddf_msg(LVL_ERROR, "Failed creating function %s", name);
|
|---|
| 72 | rc = ENOMEM;
|
|---|
| 73 | goto leave;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | rc = ddf_fun_add_match_id(fun, str_dup(match_id), match_score);
|
|---|
| 77 | if (rc != EOK) {
|
|---|
| 78 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
|---|
| 79 | name);
|
|---|
| 80 | goto leave;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | rc = ddf_fun_bind(fun);
|
|---|
| 84 | if (rc != EOK) {
|
|---|
| 85 | ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
|
|---|
| 86 | str_error(rc));
|
|---|
| 87 | goto leave;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
|
|---|
| 91 | rc = EOK;
|
|---|
| 92 |
|
|---|
| 93 | leave:
|
|---|
| 94 | if (rc != expected_rc) {
|
|---|
| 95 | fprintf(stderr,
|
|---|
| 96 | NAME ": Unexpected error registering function `%s'.\n"
|
|---|
| 97 | NAME ": Expected \"%s\" but got \"%s\".\n",
|
|---|
| 98 | name, str_error(expected_rc), str_error(rc));
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | if ((rc != EOK) && (fun != NULL)) {
|
|---|
| 102 | ddf_fun_destroy(fun);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return rc;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /** Callback when new device is passed to this driver.
|
|---|
| 109 | * This function is the body of the test: it shall register new child
|
|---|
| 110 | * (named `clone') that shall be driven by the same task. When the clone
|
|---|
| 111 | * is added, it registers another child (named `child') that is also driven
|
|---|
| 112 | * by this task. The conditions ensure that we do not recurse indefinitely.
|
|---|
| 113 | * When successful, the device tree shall contain following fragment:
|
|---|
| 114 | *
|
|---|
| 115 | * /virtual/test1
|
|---|
| 116 | * /virtual/test1/clone
|
|---|
| 117 | * /virtual/test1/clone/child
|
|---|
| 118 | *
|
|---|
| 119 | * and devman shall not deadlock.
|
|---|
| 120 | *
|
|---|
| 121 | *
|
|---|
| 122 | * @param dev New device.
|
|---|
| 123 | * @return Error code reporting success of the operation.
|
|---|
| 124 | */
|
|---|
| 125 | static int test1_add_device(ddf_dev_t *dev)
|
|---|
| 126 | {
|
|---|
| 127 | ddf_fun_t *fun_a;
|
|---|
| 128 | int rc;
|
|---|
| 129 |
|
|---|
| 130 | ddf_msg(LVL_DEBUG, "add_device(name=\"%s\", handle=%d)",
|
|---|
| 131 | dev->name, (int) dev->handle);
|
|---|
| 132 |
|
|---|
| 133 | fun_a = ddf_fun_create(dev, fun_exposed, "a");
|
|---|
| 134 | if (fun_a == NULL) {
|
|---|
| 135 | ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
|
|---|
| 136 | return ENOMEM;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | rc = ddf_fun_bind(fun_a);
|
|---|
| 140 | if (rc != EOK) {
|
|---|
| 141 | ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
|
|---|
| 142 | return rc;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | ddf_fun_add_to_class(fun_a, "virtual");
|
|---|
| 146 |
|
|---|
| 147 | if (str_cmp(dev->name, "null") == 0) {
|
|---|
| 148 | fun_a->ops = &char_device_ops;
|
|---|
| 149 | ddf_fun_add_to_class(fun_a, "virt-null");
|
|---|
| 150 | } else if (str_cmp(dev->name, "test1") == 0) {
|
|---|
| 151 | (void) register_fun_verbose(dev,
|
|---|
| 152 | "cloning myself ;-)", "clone",
|
|---|
| 153 | "virtual&test1", 10, EOK);
|
|---|
| 154 | (void) register_fun_verbose(dev,
|
|---|
| 155 | "cloning myself twice ;-)", "clone",
|
|---|
| 156 | "virtual&test1", 10, EEXISTS);
|
|---|
| 157 | } else if (str_cmp(dev->name, "clone") == 0) {
|
|---|
| 158 | (void) register_fun_verbose(dev,
|
|---|
| 159 | "run by the same task", "child",
|
|---|
| 160 | "virtual&test1&child", 10, EOK);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | ddf_msg(LVL_DEBUG, "Device `%s' accepted.", dev->name);
|
|---|
| 164 |
|
|---|
| 165 | return EOK;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | int main(int argc, char *argv[])
|
|---|
| 169 | {
|
|---|
| 170 | printf(NAME ": HelenOS test1 virtual device driver\n");
|
|---|
| 171 | ddf_log_init(NAME, LVL_ERROR);
|
|---|
| 172 | return ddf_driver_main(&test1_driver);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|