| 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 <async.h>
|
|---|
| 34 | #include <stdio.h>
|
|---|
| 35 | #include <errno.h>
|
|---|
| 36 | #include <str_error.h>
|
|---|
| 37 | #include <ddf/driver.h>
|
|---|
| 38 | #include <ddf/log.h>
|
|---|
| 39 |
|
|---|
| 40 | #define NAME "test2"
|
|---|
| 41 |
|
|---|
| 42 | static int test2_add_device(ddf_dev_t *dev);
|
|---|
| 43 |
|
|---|
| 44 | static driver_ops_t driver_ops = {
|
|---|
| 45 | .add_device = &test2_add_device
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | static driver_t test2_driver = {
|
|---|
| 49 | .name = NAME,
|
|---|
| 50 | .driver_ops = &driver_ops
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 | /** Register child and inform user about it.
|
|---|
| 54 | *
|
|---|
| 55 | * @param parent Parent device.
|
|---|
| 56 | * @param message Message for the user.
|
|---|
| 57 | * @param name Device name.
|
|---|
| 58 | * @param match_id Device match id.
|
|---|
| 59 | * @param score Device match score.
|
|---|
| 60 | */
|
|---|
| 61 | static int register_fun_verbose(ddf_dev_t *parent, const char *message,
|
|---|
| 62 | const char *name, const char *match_id, int match_score)
|
|---|
| 63 | {
|
|---|
| 64 | ddf_fun_t *fun;
|
|---|
| 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 | return ENOMEM;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | rc = ddf_fun_add_match_id(fun, match_id, match_score);
|
|---|
| 76 | if (rc != EOK) {
|
|---|
| 77 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
|---|
| 78 | name);
|
|---|
| 79 | ddf_fun_destroy(fun);
|
|---|
| 80 | return rc;
|
|---|
| 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 | ddf_fun_destroy(fun);
|
|---|
| 88 | return rc;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
|
|---|
| 92 | return EOK;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /** Add child devices after some sleep.
|
|---|
| 96 | *
|
|---|
| 97 | * @param arg Parent device structure (ddf_dev_t *).
|
|---|
| 98 | * @return Always EOK.
|
|---|
| 99 | */
|
|---|
| 100 | static int postponed_birth(void *arg)
|
|---|
| 101 | {
|
|---|
| 102 | ddf_dev_t *dev = (ddf_dev_t *) arg;
|
|---|
| 103 | ddf_fun_t *fun_a;
|
|---|
| 104 | int rc;
|
|---|
| 105 |
|
|---|
| 106 | async_usleep(1000);
|
|---|
| 107 |
|
|---|
| 108 | (void) register_fun_verbose(dev, "child driven by the same task",
|
|---|
| 109 | "child", "virtual&test2", 10);
|
|---|
| 110 | (void) register_fun_verbose(dev, "child driven by test1",
|
|---|
| 111 | "test1", "virtual&test1", 10);
|
|---|
| 112 |
|
|---|
| 113 | fun_a = ddf_fun_create(dev, fun_exposed, "a");
|
|---|
| 114 | if (fun_a == NULL) {
|
|---|
| 115 | ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
|
|---|
| 116 | return ENOMEM;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | rc = ddf_fun_bind(fun_a);
|
|---|
| 120 | if (rc != EOK) {
|
|---|
| 121 | ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
|
|---|
| 122 | return rc;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | ddf_fun_add_to_class(fun_a, "virtual");
|
|---|
| 126 |
|
|---|
| 127 | return EOK;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | static int test2_add_device(ddf_dev_t *dev)
|
|---|
| 131 | {
|
|---|
| 132 | ddf_msg(LVL_DEBUG, "test2_add_device(name=\"%s\", handle=%d)",
|
|---|
| 133 | dev->name, (int) dev->handle);
|
|---|
| 134 |
|
|---|
| 135 | if (str_cmp(dev->name, "child") != 0) {
|
|---|
| 136 | fid_t postpone = fibril_create(postponed_birth, dev);
|
|---|
| 137 | if (postpone == 0) {
|
|---|
| 138 | ddf_msg(LVL_ERROR, "fibril_create() failed.");
|
|---|
| 139 | return ENOMEM;
|
|---|
| 140 | }
|
|---|
| 141 | fibril_add_ready(postpone);
|
|---|
| 142 | } else {
|
|---|
| 143 | (void) register_fun_verbose(dev, "child without available driver",
|
|---|
| 144 | "ERROR", "non-existent.match.id", 10);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | return EOK;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | int main(int argc, char *argv[])
|
|---|
| 151 | {
|
|---|
| 152 | printf(NAME ": HelenOS test2 virtual device driver\n");
|
|---|
| 153 | ddf_log_init(NAME, LVL_ERROR);
|
|---|
| 154 | return ddf_driver_main(&test2_driver);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|