[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>
|
---|
| 33 | #include <stdio.h>
|
---|
| 34 | #include <errno.h>
|
---|
| 35 | #include <str_error.h>
|
---|
| 36 | #include <driver.h>
|
---|
| 37 |
|
---|
| 38 | #define NAME "test2"
|
---|
| 39 |
|
---|
| 40 | static int add_device(device_t *dev);
|
---|
| 41 |
|
---|
| 42 | static driver_ops_t driver_ops = {
|
---|
| 43 | .add_device = &add_device
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | static driver_t the_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 void register_child_verbose(device_t *parent, const char *message,
|
---|
| 60 | const char *name, const char *match_id, int match_score)
|
---|
| 61 | {
|
---|
| 62 | printf(NAME ": registering child device `%s': %s.\n",
|
---|
| 63 | name, message);
|
---|
| 64 |
|
---|
[8b1e15ac] | 65 | int rc = register_function_wrapper(parent, name,
|
---|
[c245f16e] | 66 | match_id, match_score);
|
---|
| 67 |
|
---|
| 68 | if (rc == EOK) {
|
---|
| 69 | printf(NAME ": registered child device `%s'.\n", name);
|
---|
| 70 | } else {
|
---|
| 71 | printf(NAME ": failed to register child `%s' (%s).\n",
|
---|
| 72 | name, str_error(rc));
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /** Add child devices after some sleep.
|
---|
| 77 | *
|
---|
| 78 | * @param arg Parent device structure (device_t *).
|
---|
| 79 | * @return Always EOK.
|
---|
| 80 | */
|
---|
| 81 | static int postponed_birth(void *arg)
|
---|
| 82 | {
|
---|
| 83 | device_t *dev = (device_t *) arg;
|
---|
[8b1e15ac] | 84 | function_t *fun;
|
---|
[c245f16e] | 85 |
|
---|
| 86 | async_usleep(1000);
|
---|
| 87 |
|
---|
| 88 | register_child_verbose(dev, "child driven by the same task",
|
---|
| 89 | "child", "virtual&test2", 10);
|
---|
| 90 | register_child_verbose(dev, "child driven by test1",
|
---|
| 91 | "test1", "virtual&test1", 10);
|
---|
| 92 |
|
---|
[8b1e15ac] | 93 | fun = create_function();
|
---|
| 94 | fun->ftype = fun_exposed;
|
---|
| 95 | fun->name = "a";
|
---|
| 96 |
|
---|
| 97 | register_function(fun, dev);
|
---|
| 98 |
|
---|
| 99 | add_function_to_class(fun, "virtual");
|
---|
[c245f16e] | 100 |
|
---|
| 101 | return EOK;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | static int add_device(device_t *dev)
|
---|
| 105 | {
|
---|
| 106 | printf(NAME ": add_device(name=\"%s\", handle=%d)\n",
|
---|
| 107 | dev->name, (int) dev->handle);
|
---|
| 108 |
|
---|
[8b1e15ac] | 109 | if (str_cmp(dev->name, "child") != 0) {
|
---|
[c245f16e] | 110 | fid_t postpone = fibril_create(postponed_birth, dev);
|
---|
[86d7bfa] | 111 | if (postpone == 0) {
|
---|
| 112 | printf(NAME ": fibril_create() error\n");
|
---|
| 113 | return ENOMEM;
|
---|
| 114 | }
|
---|
[c245f16e] | 115 | fibril_add_ready(postpone);
|
---|
| 116 | } else {
|
---|
| 117 | register_child_verbose(dev, "child without available driver",
|
---|
| 118 | "ERROR", "non-existent.match.id", 10);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | return EOK;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | int main(int argc, char *argv[])
|
---|
| 125 | {
|
---|
| 126 | printf(NAME ": HelenOS test2 virtual device driver\n");
|
---|
| 127 | return driver_main(&the_driver);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|