[1b2981aa] | 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>
|
---|
[af6b5157] | 36 | #include <ddf/driver.h>
|
---|
[fc51296] | 37 | #include <ddf/log.h>
|
---|
[af6b5157] | 38 |
|
---|
[424558a] | 39 | #include "test1.h"
|
---|
[1b2981aa] | 40 |
|
---|
[83a2f43] | 41 | static int test1_add_device(ddf_dev_t *dev);
|
---|
[1b2981aa] | 42 |
|
---|
| 43 | static driver_ops_t driver_ops = {
|
---|
[bab6388] | 44 | .add_device = &test1_add_device
|
---|
[1b2981aa] | 45 | };
|
---|
| 46 |
|
---|
[bab6388] | 47 | static driver_t test1_driver = {
|
---|
[1b2981aa] | 48 | .name = NAME,
|
---|
| 49 | .driver_ops = &driver_ops
|
---|
| 50 | };
|
---|
| 51 |
|
---|
[703d19c] | 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 | */
|
---|
[83a2f43] | 60 | static int register_fun_verbose(ddf_dev_t *parent, const char *message,
|
---|
[3fddb55] | 61 | const char *name, const char *match_id, int match_score,
|
---|
| 62 | int expected_rc)
|
---|
[703d19c] | 63 | {
|
---|
[3fddb55] | 64 | ddf_fun_t *fun = NULL;
|
---|
[cd0684d] | 65 | int rc;
|
---|
| 66 |
|
---|
[ebcb05a] | 67 | ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
|
---|
[cd0684d] | 68 |
|
---|
| 69 | fun = ddf_fun_create(parent, fun_inner, name);
|
---|
| 70 | if (fun == NULL) {
|
---|
[ebcb05a] | 71 | ddf_msg(LVL_ERROR, "Failed creating function %s", name);
|
---|
[3fddb55] | 72 | rc = ENOMEM;
|
---|
| 73 | goto leave;
|
---|
[cd0684d] | 74 | }
|
---|
[703d19c] | 75 |
|
---|
[3fddb55] | 76 | rc = ddf_fun_add_match_id(fun, str_dup(match_id), match_score);
|
---|
[cd0684d] | 77 | if (rc != EOK) {
|
---|
[ebcb05a] | 78 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
---|
[fc51296] | 79 | name);
|
---|
[3fddb55] | 80 | goto leave;
|
---|
[cd0684d] | 81 | }
|
---|
[703d19c] | 82 |
|
---|
[cd0684d] | 83 | rc = ddf_fun_bind(fun);
|
---|
| 84 | if (rc != EOK) {
|
---|
[ebcb05a] | 85 | ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
|
---|
[cd0684d] | 86 | str_error(rc));
|
---|
[3fddb55] | 87 | goto leave;
|
---|
[703d19c] | 88 | }
|
---|
[cd0684d] | 89 |
|
---|
[ebcb05a] | 90 | ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
|
---|
[3fddb55] | 91 | rc = EOK;
|
---|
| 92 |
|
---|
| 93 | leave:
|
---|
| 94 | if (rc != expected_rc) {
|
---|
| 95 | fprintf(stderr,
|
---|
[bdbb6f6] | 96 | NAME ": Unexpected error registering function `%s'.\n"
|
---|
[3fddb55] | 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;
|
---|
[703d19c] | 106 | }
|
---|
| 107 |
|
---|
[1b2981aa] | 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 | */
|
---|
[83a2f43] | 125 | static int test1_add_device(ddf_dev_t *dev)
|
---|
[1b2981aa] | 126 | {
|
---|
[83a2f43] | 127 | ddf_fun_t *fun_a;
|
---|
[97a62fe] | 128 | int rc;
|
---|
[8b1e15ac] | 129 |
|
---|
[ebcb05a] | 130 | ddf_msg(LVL_DEBUG, "add_device(name=\"%s\", handle=%d)",
|
---|
[1b2981aa] | 131 | dev->name, (int) dev->handle);
|
---|
| 132 |
|
---|
[97a62fe] | 133 | fun_a = ddf_fun_create(dev, fun_exposed, "a");
|
---|
| 134 | if (fun_a == NULL) {
|
---|
[ebcb05a] | 135 | ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
|
---|
[97a62fe] | 136 | return ENOMEM;
|
---|
| 137 | }
|
---|
[8b1e15ac] | 138 |
|
---|
[97a62fe] | 139 | rc = ddf_fun_bind(fun_a);
|
---|
| 140 | if (rc != EOK) {
|
---|
[ebcb05a] | 141 | ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
|
---|
[97a62fe] | 142 | return rc;
|
---|
| 143 | }
|
---|
[8b1e15ac] | 144 |
|
---|
[83a2f43] | 145 | ddf_fun_add_to_class(fun_a, "virtual");
|
---|
[703d19c] | 146 |
|
---|
[424558a] | 147 | if (str_cmp(dev->name, "null") == 0) {
|
---|
[8b1e15ac] | 148 | fun_a->ops = &char_device_ops;
|
---|
[83a2f43] | 149 | ddf_fun_add_to_class(fun_a, "virt-null");
|
---|
[8b1e15ac] | 150 | } else if (str_cmp(dev->name, "test1") == 0) {
|
---|
[3fddb55] | 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);
|
---|
[1b2981aa] | 157 | } else if (str_cmp(dev->name, "clone") == 0) {
|
---|
[3fddb55] | 158 | (void) register_fun_verbose(dev,
|
---|
| 159 | "run by the same task", "child",
|
---|
| 160 | "virtual&test1&child", 10, EOK);
|
---|
[1b2981aa] | 161 | }
|
---|
| 162 |
|
---|
[ebcb05a] | 163 | ddf_msg(LVL_DEBUG, "Device `%s' accepted.", dev->name);
|
---|
[1b2981aa] | 164 |
|
---|
| 165 | return EOK;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | int main(int argc, char *argv[])
|
---|
| 169 | {
|
---|
| 170 | printf(NAME ": HelenOS test1 virtual device driver\n");
|
---|
[fc51296] | 171 | ddf_log_init(NAME, LVL_ERROR);
|
---|
[83a2f43] | 172 | return ddf_driver_main(&test1_driver);
|
---|
[1b2981aa] | 173 | }
|
---|
| 174 |
|
---|