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

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

Small additional cleanup.

  • Property mode set to 100644
File size: 4.7 KB
Line 
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 <ctype.h>
48#include <macros.h>
49#include <inttypes.h>
50#include <sysinfo.h>
51
52#include <driver.h>
53#include <devman.h>
54#include <ipc/devman.h>
55
56#define NAME "root"
57
58#define PLATFORM_FUN_NAME "hw"
59#define PLATFORM_FUN_MATCH_ID_FMT "platform/%s"
60#define PLATFORM_FUN_MATCH_SCORE 100
61
62#define VIRTUAL_FUN_NAME "virt"
63#define VIRTUAL_FUN_MATCH_ID "rootvirt"
64#define VIRTUAL_FUN_MATCH_SCORE 100
65
66static int root_add_device(device_t *dev);
67
68/** The root device driver's standard operations. */
69static driver_ops_t root_ops = {
70 .add_device = &root_add_device
71};
72
73/** The root device driver structure. */
74static driver_t root_driver = {
75 .name = NAME,
76 .driver_ops = &root_ops
77};
78
79/** Create the function which represents the root of virtual device tree.
80 *
81 * @param dev Device
82 * @return EOK on success or negative error code
83 */
84static int add_virtual_root_fun(device_t *dev)
85{
86 printf(NAME ": adding new function for virtual devices.\n");
87 printf(NAME ": function node is `%s' (%d %s)\n", VIRTUAL_FUN_NAME,
88 VIRTUAL_FUN_MATCH_SCORE, VIRTUAL_FUN_MATCH_ID);
89
90 int res = register_function_wrapper(dev, VIRTUAL_FUN_NAME,
91 VIRTUAL_FUN_MATCH_ID, VIRTUAL_FUN_MATCH_SCORE);
92
93 return res;
94}
95
96/** Create the function which represents the root of HW device tree.
97 *
98 * @param dev Device
99 * @return EOK on success or negative error code
100 */
101static int add_platform_fun(device_t *dev)
102{
103 char *match_id;
104 char *platform;
105 size_t platform_size;
106 int res;
107
108 /* Get platform name from sysinfo. */
109 platform = sysinfo_get_data("platform", &platform_size);
110 if (platform == NULL) {
111 printf(NAME ": Failed to obtain platform name.\n");
112 return ENOENT;
113 }
114
115 /* Null-terminate string. */
116 platform = realloc(platform, platform_size + 1);
117 if (platform == NULL) {
118 printf(NAME ": Memory allocation failed.\n");
119 return ENOMEM;
120 }
121
122 platform[platform_size] = '\0';
123
124 /* Construct match ID. */
125 if (asprintf(&match_id, PLATFORM_FUN_MATCH_ID_FMT, platform) == -1) {
126 printf(NAME ": Memory allocation failed.\n");
127 return ENOMEM;
128 }
129
130 /* Add function. */
131 printf(NAME ": adding platform function\n");
132 printf(NAME ": function node is `%s' (%d %s)\n", PLATFORM_FUN_NAME,
133 PLATFORM_FUN_MATCH_SCORE, match_id);
134
135 res = register_function_wrapper(dev, PLATFORM_FUN_NAME,
136 match_id, PLATFORM_FUN_MATCH_SCORE);
137
138 return res;
139}
140
141/** Get the root device.
142 *
143 * @param dev The device which is root of the whole device tree (both
144 * of HW and pseudo devices).
145 */
146static int root_add_device(device_t *dev)
147{
148 printf(NAME ": root_add_device, device handle=%" PRIun "\n",
149 dev->handle);
150
151 /*
152 * Register virtual devices root.
153 * We ignore error occurrence because virtual devices shall not be
154 * vital for the system.
155 */
156 add_virtual_root_fun(dev);
157
158 /* Register root device's children. */
159 int res = add_platform_fun(dev);
160 if (EOK != res)
161 printf(NAME ": failed to add child device for platform.\n");
162
163 return res;
164}
165
166int main(int argc, char *argv[])
167{
168 printf(NAME ": HelenOS root device driver\n");
169 return driver_main(&root_driver);
170}
171
172/**
173 * @}
174 */
175
Note: See TracBrowser for help on using the repository browser.