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