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 | /**
|
---|
30 | * @defgroup rootvirt Root device driver for virtual devices.
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <str_error.h>
|
---|
41 | #include <driver.h>
|
---|
42 |
|
---|
43 | #define NAME "rootvirt"
|
---|
44 |
|
---|
45 | /** Virtual device entry. */
|
---|
46 | typedef struct {
|
---|
47 | /** Device name. */
|
---|
48 | const char *name;
|
---|
49 | /** Device match id. */
|
---|
50 | const char *match_id;
|
---|
51 | } virtual_device_t;
|
---|
52 |
|
---|
53 | /** List of existing virtual devices. */
|
---|
54 | virtual_device_t virtual_devices[] = {
|
---|
55 | #include "devices.def"
|
---|
56 | /* Terminating item. */
|
---|
57 | {
|
---|
58 | .name = NULL,
|
---|
59 | .match_id = NULL
|
---|
60 | }
|
---|
61 | };
|
---|
62 |
|
---|
63 | static int add_device(device_t *dev);
|
---|
64 |
|
---|
65 | static driver_ops_t rootvirt_ops = {
|
---|
66 | .add_device = &add_device
|
---|
67 | };
|
---|
68 |
|
---|
69 | static driver_t rootvirt_driver = {
|
---|
70 | .name = NAME,
|
---|
71 | .driver_ops = &rootvirt_ops
|
---|
72 | };
|
---|
73 |
|
---|
74 | /** Add child device.
|
---|
75 | *
|
---|
76 | * @param parent Parent device.
|
---|
77 | * @param virt_dev Virtual device to add.
|
---|
78 | * @return Error code.
|
---|
79 | */
|
---|
80 | static int add_child(device_t *parent, virtual_device_t *virt_dev)
|
---|
81 | {
|
---|
82 | printf(NAME ": registering child device `%s' (match \"%s\")\n",
|
---|
83 | virt_dev->name, virt_dev->match_id);
|
---|
84 |
|
---|
85 | int rc = child_device_register_wrapper(parent, virt_dev->name,
|
---|
86 | virt_dev->match_id, 10);
|
---|
87 |
|
---|
88 | if (rc == EOK) {
|
---|
89 | printf(NAME ": registered child device `%s'\n",
|
---|
90 | virt_dev->name);
|
---|
91 | } else {
|
---|
92 | printf(NAME ": failed to register child device `%s': %s\n",
|
---|
93 | virt_dev->name, str_error(rc));
|
---|
94 | }
|
---|
95 |
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static int add_device(device_t *dev)
|
---|
100 | {
|
---|
101 | static int instances = 0;
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Allow only single instance of root virtual device.
|
---|
105 | */
|
---|
106 | instances++;
|
---|
107 | if (instances > 1) {
|
---|
108 | return ELIMIT;
|
---|
109 | }
|
---|
110 |
|
---|
111 | printf(NAME ": add_device(name=\"%s\", handle=%d)\n",
|
---|
112 | dev->name, (int)dev->handle);
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Go through all virtual devices and try to add them.
|
---|
116 | * We silently ignore failures.
|
---|
117 | */
|
---|
118 | virtual_device_t *virt_dev = virtual_devices;
|
---|
119 | while (virt_dev->name != NULL) {
|
---|
120 | (void) add_child(dev, virt_dev);
|
---|
121 | virt_dev++;
|
---|
122 | }
|
---|
123 |
|
---|
124 | return EOK;
|
---|
125 | }
|
---|
126 |
|
---|
127 | int main(int argc, char *argv[])
|
---|
128 | {
|
---|
129 | printf(NAME ": HelenOS virtual devices root driver\n");
|
---|
130 | return driver_main(&rootvirt_driver);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * @}
|
---|
135 | */
|
---|
136 |
|
---|