source: mainline/uspace/drv/infrastructure/root/root.c@ e882e3a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e882e3a was e882e3a, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Remove include of devman.h where not needed.

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