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