| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| 3 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 4 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 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 | /**
|
|---|
| 32 | * @defgroup root Root device driver.
|
|---|
| 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>
|
|---|
| 46 | #include <str.h>
|
|---|
| 47 | #include <str_error.h>
|
|---|
| 48 | #include <ctype.h>
|
|---|
| 49 | #include <macros.h>
|
|---|
| 50 | #include <inttypes.h>
|
|---|
| 51 | #include <sysinfo.h>
|
|---|
| 52 |
|
|---|
| 53 | #include <ddf/driver.h>
|
|---|
| 54 | #include <ddf/log.h>
|
|---|
| 55 | #include <devman.h>
|
|---|
| 56 | #include <ipc/devman.h>
|
|---|
| 57 |
|
|---|
| 58 | #define NAME "root"
|
|---|
| 59 |
|
|---|
| 60 | #define PLATFORM_FUN_NAME "hw"
|
|---|
| 61 | #define PLATFORM_FUN_MATCH_ID_FMT "platform/%s"
|
|---|
| 62 | #define PLATFORM_FUN_MATCH_SCORE 100
|
|---|
| 63 |
|
|---|
| 64 | #define VIRTUAL_FUN_NAME "virt"
|
|---|
| 65 | #define VIRTUAL_FUN_MATCH_ID "rootvirt"
|
|---|
| 66 | #define VIRTUAL_FUN_MATCH_SCORE 100
|
|---|
| 67 |
|
|---|
| 68 | static int root_add_device(ddf_dev_t *dev);
|
|---|
| 69 | static int root_fun_online(ddf_fun_t *fun);
|
|---|
| 70 | static int root_fun_offline(ddf_fun_t *fun);
|
|---|
| 71 |
|
|---|
| 72 | /** The root device driver's standard operations. */
|
|---|
| 73 | static driver_ops_t root_ops = {
|
|---|
| 74 | .add_device = &root_add_device,
|
|---|
| 75 | .fun_online = &root_fun_online,
|
|---|
| 76 | .fun_offline = &root_fun_offline
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|
| 79 | /** The root device driver structure. */
|
|---|
| 80 | static driver_t root_driver = {
|
|---|
| 81 | .name = NAME,
|
|---|
| 82 | .driver_ops = &root_ops
|
|---|
| 83 | };
|
|---|
| 84 |
|
|---|
| 85 | /** Create the function which represents the root of virtual device tree.
|
|---|
| 86 | *
|
|---|
| 87 | * @param dev Device
|
|---|
| 88 | * @return EOK on success or negative error code
|
|---|
| 89 | */
|
|---|
| 90 | static int add_virtual_root_fun(ddf_dev_t *dev)
|
|---|
| 91 | {
|
|---|
| 92 | const char *name = VIRTUAL_FUN_NAME;
|
|---|
| 93 | ddf_fun_t *fun;
|
|---|
| 94 | int rc;
|
|---|
| 95 |
|
|---|
| 96 | ddf_msg(LVL_DEBUG, "Adding new function for virtual devices. "
|
|---|
| 97 | "Function node is `%s' (%d %s)", name,
|
|---|
| 98 | VIRTUAL_FUN_MATCH_SCORE, VIRTUAL_FUN_MATCH_ID);
|
|---|
| 99 |
|
|---|
| 100 | fun = ddf_fun_create(dev, fun_inner, name);
|
|---|
| 101 | if (fun == NULL) {
|
|---|
| 102 | ddf_msg(LVL_ERROR, "Failed creating function %s", name);
|
|---|
| 103 | return ENOMEM;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | rc = ddf_fun_add_match_id(fun, VIRTUAL_FUN_MATCH_ID,
|
|---|
| 107 | VIRTUAL_FUN_MATCH_SCORE);
|
|---|
| 108 | if (rc != EOK) {
|
|---|
| 109 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
|---|
| 110 | name);
|
|---|
| 111 | ddf_fun_destroy(fun);
|
|---|
| 112 | return rc;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | rc = ddf_fun_bind(fun);
|
|---|
| 116 | if (rc != EOK) {
|
|---|
| 117 | ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
|
|---|
| 118 | str_error(rc));
|
|---|
| 119 | ddf_fun_destroy(fun);
|
|---|
| 120 | return rc;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | return EOK;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** Create the function which represents the root of HW device tree.
|
|---|
| 127 | *
|
|---|
| 128 | * @param dev Device
|
|---|
| 129 | * @return EOK on success or negative error code
|
|---|
| 130 | */
|
|---|
| 131 | static int add_platform_fun(ddf_dev_t *dev)
|
|---|
| 132 | {
|
|---|
| 133 | char *match_id;
|
|---|
| 134 | char *platform;
|
|---|
| 135 | size_t platform_size;
|
|---|
| 136 |
|
|---|
| 137 | const char *name = PLATFORM_FUN_NAME;
|
|---|
| 138 | ddf_fun_t *fun;
|
|---|
| 139 | int rc;
|
|---|
| 140 |
|
|---|
| 141 | /* Get platform name from sysinfo. */
|
|---|
| 142 | platform = sysinfo_get_data("platform", &platform_size);
|
|---|
| 143 | if (platform == NULL) {
|
|---|
| 144 | ddf_msg(LVL_ERROR, "Failed to obtain platform name.");
|
|---|
| 145 | return ENOENT;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /* Null-terminate string. */
|
|---|
| 149 | platform = realloc(platform, platform_size + 1);
|
|---|
| 150 | if (platform == NULL) {
|
|---|
| 151 | ddf_msg(LVL_ERROR, "Memory allocation failed.");
|
|---|
| 152 | return ENOMEM;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | platform[platform_size] = '\0';
|
|---|
| 156 |
|
|---|
| 157 | /* Construct match ID. */
|
|---|
| 158 | if (asprintf(&match_id, PLATFORM_FUN_MATCH_ID_FMT, platform) == -1) {
|
|---|
| 159 | ddf_msg(LVL_ERROR, "Memory allocation failed.");
|
|---|
| 160 | return ENOMEM;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /* Add function. */
|
|---|
| 164 | ddf_msg(LVL_DEBUG, "Adding platform function. Function node is `%s' "
|
|---|
| 165 | " (%d %s)", PLATFORM_FUN_NAME, PLATFORM_FUN_MATCH_SCORE,
|
|---|
| 166 | match_id);
|
|---|
| 167 |
|
|---|
| 168 | fun = ddf_fun_create(dev, fun_inner, name);
|
|---|
| 169 | if (fun == NULL) {
|
|---|
| 170 | ddf_msg(LVL_ERROR, "Error creating function %s", name);
|
|---|
| 171 | return ENOMEM;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | rc = ddf_fun_add_match_id(fun, match_id, PLATFORM_FUN_MATCH_SCORE);
|
|---|
| 175 | if (rc != EOK) {
|
|---|
| 176 | ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
|
|---|
| 177 | name);
|
|---|
| 178 | ddf_fun_destroy(fun);
|
|---|
| 179 | return rc;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | free(match_id);
|
|---|
| 183 |
|
|---|
| 184 | rc = ddf_fun_bind(fun);
|
|---|
| 185 | if (rc != EOK) {
|
|---|
| 186 | ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
|
|---|
| 187 | str_error(rc));
|
|---|
| 188 | ddf_fun_destroy(fun);
|
|---|
| 189 | return rc;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | return EOK;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /** Get the root device.
|
|---|
| 196 | *
|
|---|
| 197 | * @param dev The device which is root of the whole device tree (both
|
|---|
| 198 | * of HW and pseudo devices).
|
|---|
| 199 | */
|
|---|
| 200 | static int root_add_device(ddf_dev_t *dev)
|
|---|
| 201 | {
|
|---|
| 202 | ddf_msg(LVL_DEBUG, "root_add_device, device handle=%" PRIun,
|
|---|
| 203 | dev->handle);
|
|---|
| 204 |
|
|---|
| 205 | /*
|
|---|
| 206 | * Register virtual devices root.
|
|---|
| 207 | * We ignore error occurrence because virtual devices shall not be
|
|---|
| 208 | * vital for the system.
|
|---|
| 209 | */
|
|---|
| 210 | add_virtual_root_fun(dev);
|
|---|
| 211 |
|
|---|
| 212 | /* Register root device's children. */
|
|---|
| 213 | int res = add_platform_fun(dev);
|
|---|
| 214 | if (EOK != res)
|
|---|
| 215 | ddf_msg(LVL_ERROR, "Failed adding child device for platform.");
|
|---|
| 216 |
|
|---|
| 217 | return res;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | static int root_fun_online(ddf_fun_t *fun)
|
|---|
| 221 | {
|
|---|
| 222 | ddf_msg(LVL_DEBUG, "root_fun_online()");
|
|---|
| 223 | return ddf_fun_online(fun);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | static int root_fun_offline(ddf_fun_t *fun)
|
|---|
| 227 | {
|
|---|
| 228 | ddf_msg(LVL_DEBUG, "root_fun_offline()");
|
|---|
| 229 | return ddf_fun_offline(fun);
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | int main(int argc, char *argv[])
|
|---|
| 233 | {
|
|---|
| 234 | printf(NAME ": HelenOS root device driver\n");
|
|---|
| 235 |
|
|---|
| 236 | ddf_log_init(NAME, LVL_ERROR);
|
|---|
| 237 | return ddf_driver_main(&root_driver);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | /**
|
|---|
| 241 | * @}
|
|---|
| 242 | */
|
|---|
| 243 |
|
|---|