[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>
|
---|
| 37 |
|
---|
[424558a] | 38 | #include "test1.h"
|
---|
[1b2981aa] | 39 |
|
---|
[83a2f43] | 40 | static int test1_add_device(ddf_dev_t *dev);
|
---|
[1b2981aa] | 41 |
|
---|
| 42 | static driver_ops_t driver_ops = {
|
---|
[bab6388] | 43 | .add_device = &test1_add_device
|
---|
[1b2981aa] | 44 | };
|
---|
| 45 |
|
---|
[bab6388] | 46 | static driver_t test1_driver = {
|
---|
[1b2981aa] | 47 | .name = NAME,
|
---|
| 48 | .driver_ops = &driver_ops
|
---|
| 49 | };
|
---|
| 50 |
|
---|
[703d19c] | 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 | */
|
---|
[83a2f43] | 59 | static int register_fun_verbose(ddf_dev_t *parent, const char *message,
|
---|
[703d19c] | 60 | const char *name, const char *match_id, int match_score)
|
---|
| 61 | {
|
---|
[83a2f43] | 62 | ddf_fun_t *fun;
|
---|
[cd0684d] | 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 | }
|
---|
[703d19c] | 72 |
|
---|
[cd0684d] | 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 | }
|
---|
[703d19c] | 79 |
|
---|
[cd0684d] | 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;
|
---|
[703d19c] | 86 | }
|
---|
[cd0684d] | 87 |
|
---|
| 88 | printf(NAME ": registered child device `%s'\n", name);
|
---|
| 89 | return EOK;
|
---|
[703d19c] | 90 | }
|
---|
| 91 |
|
---|
[1b2981aa] | 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 | */
|
---|
[83a2f43] | 109 | static int test1_add_device(ddf_dev_t *dev)
|
---|
[1b2981aa] | 110 | {
|
---|
[83a2f43] | 111 | ddf_fun_t *fun_a;
|
---|
[97a62fe] | 112 | int rc;
|
---|
[8b1e15ac] | 113 |
|
---|
[1b2981aa] | 114 | printf(NAME ": add_device(name=\"%s\", handle=%d)\n",
|
---|
| 115 | dev->name, (int) dev->handle);
|
---|
| 116 |
|
---|
[97a62fe] | 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 | }
|
---|
[8b1e15ac] | 122 |
|
---|
[97a62fe] | 123 | rc = ddf_fun_bind(fun_a);
|
---|
| 124 | if (rc != EOK) {
|
---|
| 125 | printf(NAME ": error binding function 'a'.\n");
|
---|
| 126 | return rc;
|
---|
| 127 | }
|
---|
[8b1e15ac] | 128 |
|
---|
[83a2f43] | 129 | ddf_fun_add_to_class(fun_a, "virtual");
|
---|
[703d19c] | 130 |
|
---|
[424558a] | 131 | if (str_cmp(dev->name, "null") == 0) {
|
---|
[8b1e15ac] | 132 | fun_a->ops = &char_device_ops;
|
---|
[83a2f43] | 133 | ddf_fun_add_to_class(fun_a, "virt-null");
|
---|
[8b1e15ac] | 134 | } else if (str_cmp(dev->name, "test1") == 0) {
|
---|
[cd0684d] | 135 | (void) register_fun_verbose(dev, "cloning myself ;-)", "clone",
|
---|
[1b2981aa] | 136 | "virtual&test1", 10);
|
---|
| 137 | } else if (str_cmp(dev->name, "clone") == 0) {
|
---|
[cd0684d] | 138 | (void) register_fun_verbose(dev, "run by the same task", "child",
|
---|
[1b2981aa] | 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");
|
---|
[83a2f43] | 150 | return ddf_driver_main(&test1_driver);
|
---|
[1b2981aa] | 151 | }
|
---|
| 152 |
|
---|