[924c75e1] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
[0ca16307] | 3 | * Copyright (c) 2010 Vojtech Horky
|
---|
[68414f4a] | 4 | * Copyright (c) 2011 Jiri Svoboda
|
---|
[924c75e1] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
[c16cf62] | 32 | * @defgroup root Root device driver.
|
---|
[924c75e1] | 33 | * @brief HelenOS root device driver.
|
---|
| 34 | * @{
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | /** @file
|
---|
| 38 | */
|
---|
| 39 |
|
---|
| 40 | #include <assert.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <bool.h>
|
---|
| 44 | #include <fibril_synch.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
[c47e1a8] | 46 | #include <str.h>
|
---|
[659ca07] | 47 | #include <str_error.h>
|
---|
[924c75e1] | 48 | #include <ctype.h>
|
---|
[bda60d9] | 49 | #include <macros.h>
|
---|
[7e752b2] | 50 | #include <inttypes.h>
|
---|
[eff1f033] | 51 | #include <sysinfo.h>
|
---|
[924c75e1] | 52 |
|
---|
[af6b5157] | 53 | #include <ddf/driver.h>
|
---|
[729fa2d6] | 54 | #include <devman.h>
|
---|
[924c75e1] | 55 | #include <ipc/devman.h>
|
---|
| 56 |
|
---|
| 57 | #define NAME "root"
|
---|
| 58 |
|
---|
[68414f4a] | 59 | #define PLATFORM_FUN_NAME "hw"
|
---|
| 60 | #define PLATFORM_FUN_MATCH_ID_FMT "platform/%s"
|
---|
| 61 | #define PLATFORM_FUN_MATCH_SCORE 100
|
---|
[0ca16307] | 62 |
|
---|
[68414f4a] | 63 | #define VIRTUAL_FUN_NAME "virt"
|
---|
| 64 | #define VIRTUAL_FUN_MATCH_ID "rootvirt"
|
---|
| 65 | #define VIRTUAL_FUN_MATCH_SCORE 100
|
---|
[6f9e7fea] | 66 |
|
---|
[83a2f43] | 67 | static int root_add_device(ddf_dev_t *dev);
|
---|
[924c75e1] | 68 |
|
---|
[5291411] | 69 | /** The root device driver's standard operations. */
|
---|
[729fa2d6] | 70 | static driver_ops_t root_ops = {
|
---|
| 71 | .add_device = &root_add_device
|
---|
| 72 | };
|
---|
| 73 |
|
---|
[5291411] | 74 | /** The root device driver structure. */
|
---|
[729fa2d6] | 75 | static driver_t root_driver = {
|
---|
| 76 | .name = NAME,
|
---|
| 77 | .driver_ops = &root_ops
|
---|
| 78 | };
|
---|
| 79 |
|
---|
[68414f4a] | 80 | /** Create the function which represents the root of virtual device tree.
|
---|
[6f9e7fea] | 81 | *
|
---|
[68414f4a] | 82 | * @param dev Device
|
---|
| 83 | * @return EOK on success or negative error code
|
---|
[6f9e7fea] | 84 | */
|
---|
[83a2f43] | 85 | static int add_virtual_root_fun(ddf_dev_t *dev)
|
---|
[6f9e7fea] | 86 | {
|
---|
[659ca07] | 87 | const char *name = VIRTUAL_FUN_NAME;
|
---|
[83a2f43] | 88 | ddf_fun_t *fun;
|
---|
[659ca07] | 89 | int rc;
|
---|
| 90 |
|
---|
[68414f4a] | 91 | printf(NAME ": adding new function for virtual devices.\n");
|
---|
[659ca07] | 92 | printf(NAME ": function node is `%s' (%d %s)\n", name,
|
---|
[68414f4a] | 93 | VIRTUAL_FUN_MATCH_SCORE, VIRTUAL_FUN_MATCH_ID);
|
---|
[6f9e7fea] | 94 |
|
---|
[659ca07] | 95 | fun = ddf_fun_create(dev, fun_inner, name);
|
---|
| 96 | if (fun == NULL) {
|
---|
| 97 | printf(NAME ": error creating function %s\n", name);
|
---|
| 98 | return ENOMEM;
|
---|
| 99 | }
|
---|
[6f9e7fea] | 100 |
|
---|
[659ca07] | 101 | rc = ddf_fun_add_match_id(fun, VIRTUAL_FUN_MATCH_ID,
|
---|
| 102 | VIRTUAL_FUN_MATCH_SCORE);
|
---|
| 103 | if (rc != EOK) {
|
---|
| 104 | printf(NAME ": error adding match IDs to function %s\n", name);
|
---|
| 105 | ddf_fun_destroy(fun);
|
---|
| 106 | return rc;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | rc = ddf_fun_bind(fun);
|
---|
| 110 | if (rc != EOK) {
|
---|
| 111 | printf(NAME ": error binding function %s: %s\n", name,
|
---|
| 112 | str_error(rc));
|
---|
| 113 | ddf_fun_destroy(fun);
|
---|
| 114 | return rc;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | return EOK;
|
---|
[6f9e7fea] | 118 | }
|
---|
| 119 |
|
---|
[68414f4a] | 120 | /** Create the function which represents the root of HW device tree.
|
---|
[5291411] | 121 | *
|
---|
[68414f4a] | 122 | * @param dev Device
|
---|
| 123 | * @return EOK on success or negative error code
|
---|
[66babbd] | 124 | */
|
---|
[83a2f43] | 125 | static int add_platform_fun(ddf_dev_t *dev)
|
---|
[5291411] | 126 | {
|
---|
[eff1f033] | 127 | char *match_id;
|
---|
| 128 | char *platform;
|
---|
| 129 | size_t platform_size;
|
---|
[659ca07] | 130 |
|
---|
| 131 | const char *name = PLATFORM_FUN_NAME;
|
---|
[83a2f43] | 132 | ddf_fun_t *fun;
|
---|
[659ca07] | 133 | int rc;
|
---|
[eff1f033] | 134 |
|
---|
| 135 | /* Get platform name from sysinfo. */
|
---|
| 136 | platform = sysinfo_get_data("platform", &platform_size);
|
---|
| 137 | if (platform == NULL) {
|
---|
| 138 | printf(NAME ": Failed to obtain platform name.\n");
|
---|
| 139 | return ENOENT;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /* Null-terminate string. */
|
---|
| 143 | platform = realloc(platform, platform_size + 1);
|
---|
| 144 | if (platform == NULL) {
|
---|
| 145 | printf(NAME ": Memory allocation failed.\n");
|
---|
| 146 | return ENOMEM;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | platform[platform_size] = '\0';
|
---|
| 150 |
|
---|
| 151 | /* Construct match ID. */
|
---|
[68414f4a] | 152 | if (asprintf(&match_id, PLATFORM_FUN_MATCH_ID_FMT, platform) == -1) {
|
---|
[eff1f033] | 153 | printf(NAME ": Memory allocation failed.\n");
|
---|
| 154 | return ENOMEM;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[68414f4a] | 157 | /* Add function. */
|
---|
| 158 | printf(NAME ": adding platform function\n");
|
---|
| 159 | printf(NAME ": function node is `%s' (%d %s)\n", PLATFORM_FUN_NAME,
|
---|
| 160 | PLATFORM_FUN_MATCH_SCORE, match_id);
|
---|
[eff1f033] | 161 |
|
---|
[659ca07] | 162 | fun = ddf_fun_create(dev, fun_inner, name);
|
---|
| 163 | if (fun == NULL) {
|
---|
| 164 | printf(NAME ": error creating function %s\n", name);
|
---|
| 165 | return ENOMEM;
|
---|
| 166 | }
|
---|
[0ca16307] | 167 |
|
---|
[659ca07] | 168 | rc = ddf_fun_add_match_id(fun, match_id, PLATFORM_FUN_MATCH_SCORE);
|
---|
| 169 | if (rc != EOK) {
|
---|
| 170 | printf(NAME ": error adding match IDs to function %s\n", name);
|
---|
| 171 | ddf_fun_destroy(fun);
|
---|
| 172 | return rc;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | rc = ddf_fun_bind(fun);
|
---|
| 176 | if (rc != EOK) {
|
---|
| 177 | printf(NAME ": error binding function %s: %s\n", name,
|
---|
| 178 | str_error(rc));
|
---|
| 179 | ddf_fun_destroy(fun);
|
---|
| 180 | return rc;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | return EOK;
|
---|
[bda60d9] | 184 | }
|
---|
| 185 |
|
---|
[66babbd] | 186 | /** Get the root device.
|
---|
[5291411] | 187 | *
|
---|
| 188 | * @param dev The device which is root of the whole device tree (both
|
---|
| 189 | * of HW and pseudo devices).
|
---|
[66babbd] | 190 | */
|
---|
[83a2f43] | 191 | static int root_add_device(ddf_dev_t *dev)
|
---|
[c16cf62] | 192 | {
|
---|
[7e752b2] | 193 | printf(NAME ": root_add_device, device handle=%" PRIun "\n",
|
---|
| 194 | dev->handle);
|
---|
[bab6388] | 195 |
|
---|
[6f9e7fea] | 196 | /*
|
---|
| 197 | * Register virtual devices root.
|
---|
| 198 | * We ignore error occurrence because virtual devices shall not be
|
---|
| 199 | * vital for the system.
|
---|
| 200 | */
|
---|
[68414f4a] | 201 | add_virtual_root_fun(dev);
|
---|
[6f9e7fea] | 202 |
|
---|
[5291411] | 203 | /* Register root device's children. */
|
---|
[68414f4a] | 204 | int res = add_platform_fun(dev);
|
---|
[5291411] | 205 | if (EOK != res)
|
---|
[d347b53] | 206 | printf(NAME ": failed to add child device for platform.\n");
|
---|
[bab6388] | 207 |
|
---|
[df747b9c] | 208 | return res;
|
---|
[c16cf62] | 209 | }
|
---|
| 210 |
|
---|
[924c75e1] | 211 | int main(int argc, char *argv[])
|
---|
| 212 | {
|
---|
[5291411] | 213 | printf(NAME ": HelenOS root device driver\n");
|
---|
[83a2f43] | 214 | return ddf_driver_main(&root_driver);
|
---|
[924c75e1] | 215 | }
|
---|
[c16cf62] | 216 |
|
---|
| 217 | /**
|
---|
| 218 | * @}
|
---|
[bda60d9] | 219 | */
|
---|
[5291411] | 220 |
|
---|