[c245f16e] | 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>
|
---|
[af6b5157] | 33 | #include <async.h>
|
---|
[c245f16e] | 34 | #include <stdio.h>
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <str_error.h>
|
---|
[af6b5157] | 37 | #include <ddf/driver.h>
|
---|
[fc51296] | 38 | #include <ddf/log.h>
|
---|
[c245f16e] | 39 |
|
---|
| 40 | #define NAME "test2"
|
---|
| 41 |
|
---|
[83a2f43] | 42 | static int test2_add_device(ddf_dev_t *dev);
|
---|
[c245f16e] | 43 |
|
---|
| 44 | static driver_ops_t driver_ops = {
|
---|
[bab6388] | 45 | .add_device = &test2_add_device
|
---|
[c245f16e] | 46 | };
|
---|
| 47 |
|
---|
[bab6388] | 48 | static driver_t test2_driver = {
|
---|
[c245f16e] | 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 | */
|
---|
[83a2f43] | 61 | static int register_fun_verbose(ddf_dev_t *parent, const char *message,
|
---|
[c245f16e] | 62 | const char *name, const char *match_id, int match_score)
|
---|
| 63 | {
|
---|
[83a2f43] | 64 | ddf_fun_t *fun;
|
---|
[cd0684d] | 65 | int rc;
|
---|
[c245f16e] | 66 |
|
---|
[ebcb05a] | 67 | ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
|
---|
[c245f16e] | 68 |
|
---|
[cd0684d] | 69 | fun = ddf_fun_create(parent, fun_inner, name);
|
---|
| 70 | if (fun == NULL) {
|
---|
[ebcb05a] | 71 | ddf_msg(LVL_ERROR, "Failed creating function %s", name);
|
---|
[cd0684d] | 72 | return ENOMEM;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | rc = ddf_fun_add_match_id(fun, match_id, match_score);
|
---|
| 76 | if (rc != EOK) {
|
---|
[ebcb05a] | 77 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
---|
[fc51296] | 78 | name);
|
---|
[cd0684d] | 79 | ddf_fun_destroy(fun);
|
---|
| 80 | return rc;
|
---|
[c245f16e] | 81 | }
|
---|
[cd0684d] | 82 |
|
---|
| 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));
|
---|
| 87 | ddf_fun_destroy(fun);
|
---|
| 88 | return rc;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[ebcb05a] | 91 | ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
|
---|
[cd0684d] | 92 | return EOK;
|
---|
[c245f16e] | 93 | }
|
---|
| 94 |
|
---|
| 95 | /** Add child devices after some sleep.
|
---|
| 96 | *
|
---|
[83a2f43] | 97 | * @param arg Parent device structure (ddf_dev_t *).
|
---|
[c245f16e] | 98 | * @return Always EOK.
|
---|
| 99 | */
|
---|
| 100 | static int postponed_birth(void *arg)
|
---|
| 101 | {
|
---|
[83a2f43] | 102 | ddf_dev_t *dev = (ddf_dev_t *) arg;
|
---|
| 103 | ddf_fun_t *fun_a;
|
---|
[97a62fe] | 104 | int rc;
|
---|
[c245f16e] | 105 |
|
---|
| 106 | async_usleep(1000);
|
---|
| 107 |
|
---|
[cd0684d] | 108 | (void) register_fun_verbose(dev, "child driven by the same task",
|
---|
[c245f16e] | 109 | "child", "virtual&test2", 10);
|
---|
[cd0684d] | 110 | (void) register_fun_verbose(dev, "child driven by test1",
|
---|
[c245f16e] | 111 | "test1", "virtual&test1", 10);
|
---|
| 112 |
|
---|
[97a62fe] | 113 | fun_a = ddf_fun_create(dev, fun_exposed, "a");
|
---|
| 114 | if (fun_a == NULL) {
|
---|
[ebcb05a] | 115 | ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
|
---|
[97a62fe] | 116 | return ENOMEM;
|
---|
| 117 | }
|
---|
[8b1e15ac] | 118 |
|
---|
[97a62fe] | 119 | rc = ddf_fun_bind(fun_a);
|
---|
| 120 | if (rc != EOK) {
|
---|
[ebcb05a] | 121 | ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
|
---|
[97a62fe] | 122 | return rc;
|
---|
| 123 | }
|
---|
[8b1e15ac] | 124 |
|
---|
[83a2f43] | 125 | ddf_fun_add_to_class(fun_a, "virtual");
|
---|
[c245f16e] | 126 |
|
---|
| 127 | return EOK;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[83a2f43] | 130 | static int test2_add_device(ddf_dev_t *dev)
|
---|
[c245f16e] | 131 | {
|
---|
[ebcb05a] | 132 | ddf_msg(LVL_DEBUG, "test2_add_device(name=\"%s\", handle=%d)",
|
---|
[c245f16e] | 133 | dev->name, (int) dev->handle);
|
---|
| 134 |
|
---|
[8b1e15ac] | 135 | if (str_cmp(dev->name, "child") != 0) {
|
---|
[c245f16e] | 136 | fid_t postpone = fibril_create(postponed_birth, dev);
|
---|
[86d7bfa] | 137 | if (postpone == 0) {
|
---|
[ebcb05a] | 138 | ddf_msg(LVL_ERROR, "fibril_create() failed.");
|
---|
[86d7bfa] | 139 | return ENOMEM;
|
---|
| 140 | }
|
---|
[c245f16e] | 141 | fibril_add_ready(postpone);
|
---|
| 142 | } else {
|
---|
[cd0684d] | 143 | (void) register_fun_verbose(dev, "child without available driver",
|
---|
[c245f16e] | 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");
|
---|
[fc51296] | 153 | ddf_log_init(NAME, LVL_ERROR);
|
---|
[83a2f43] | 154 | return ddf_driver_main(&test2_driver);
|
---|
[c245f16e] | 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|