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