source: mainline/uspace/drv/root/root.c@ fc51296

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

Add logging for device drivers.

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