source: mainline/uspace/drv/root/root.c@ 1b367b4

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

Match platform driver based on platform name instead of architecture name. Obtain platform name from kernel via sysinfo.

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[924c75e1]1/*
2 * Copyright (c) 2010 Lenka Trochtova
[0ca16307]3 * Copyright (c) 2010 Vojtech Horky
[924c75e1]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/**
[c16cf62]31 * @defgroup root Root device driver.
[924c75e1]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>
[c47e1a8]45#include <str.h>
[924c75e1]46#include <ctype.h>
[bda60d9]47#include <macros.h>
[7e752b2]48#include <inttypes.h>
[eff1f033]49#include <sysinfo.h>
[924c75e1]50
[c16cf62]51#include <driver.h>
[729fa2d6]52#include <devman.h>
[924c75e1]53#include <ipc/devman.h>
54
55#define NAME "root"
56
[0ca16307]57#define PLATFORM_DEVICE_NAME "hw"
[eff1f033]58#define PLATFORM_DEVICE_MATCH_ID_FMT "platform/%s"
[0ca16307]59#define PLATFORM_DEVICE_MATCH_SCORE 100
60
[6f9e7fea]61#define VIRTUAL_DEVICE_NAME "virt"
62#define VIRTUAL_DEVICE_MATCH_ID "rootvirt"
63#define VIRTUAL_DEVICE_MATCH_SCORE 100
64
[df747b9c]65static int root_add_device(device_t *dev);
[924c75e1]66
[5291411]67/** The root device driver's standard operations. */
[729fa2d6]68static driver_ops_t root_ops = {
69 .add_device = &root_add_device
70};
71
[5291411]72/** The root device driver structure. */
[729fa2d6]73static driver_t root_driver = {
74 .name = NAME,
75 .driver_ops = &root_ops
76};
77
[6f9e7fea]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 */
83static 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
92 return res;
93}
94
[df747b9c]95/** Create the device which represents the root of HW device tree.
[5291411]96 *
97 * @param parent Parent of the newly created device.
[df747b9c]98 * @return 0 on success, negative error number otherwise.
[66babbd]99 */
[5291411]100static int add_platform_child(device_t *parent)
101{
[eff1f033]102 char *match_id;
103 char *platform;
104 size_t platform_size;
105 int res;
106
107 /* Get platform name from sysinfo. */
108
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
126 if (asprintf(&match_id, PLATFORM_DEVICE_MATCH_ID_FMT, platform) == -1) {
127 printf(NAME ": Memory allocation failed.\n");
128 return ENOMEM;
129 }
130
131 /* Add child. */
132
[bda60d9]133 printf(NAME ": adding new child for platform device.\n");
[0ca16307]134 printf(NAME ": device node is `%s' (%d %s)\n", PLATFORM_DEVICE_NAME,
[eff1f033]135 PLATFORM_DEVICE_MATCH_SCORE, match_id);
136
137 res = child_device_register_wrapper(parent, PLATFORM_DEVICE_NAME,
138 match_id, PLATFORM_DEVICE_MATCH_SCORE);
[0ca16307]139
[5291411]140 return res;
[bda60d9]141}
142
[66babbd]143/** Get the root device.
[5291411]144 *
145 * @param dev The device which is root of the whole device tree (both
146 * of HW and pseudo devices).
[66babbd]147 */
[5291411]148static int root_add_device(device_t *dev)
[c16cf62]149{
[7e752b2]150 printf(NAME ": root_add_device, device handle=%" PRIun "\n",
151 dev->handle);
[bda60d9]152
[6f9e7fea]153 /*
154 * Register virtual devices root.
155 * We ignore error occurrence because virtual devices shall not be
156 * vital for the system.
157 */
158 add_virtual_root_child(dev);
159
[5291411]160 /* Register root device's children. */
161 int res = add_platform_child(dev);
162 if (EOK != res)
[d347b53]163 printf(NAME ": failed to add child device for platform.\n");
[bda60d9]164
[df747b9c]165 return res;
[c16cf62]166}
167
[924c75e1]168int main(int argc, char *argv[])
169{
[5291411]170 printf(NAME ": HelenOS root device driver\n");
[924c75e1]171 return driver_main(&root_driver);
172}
[c16cf62]173
174/**
175 * @}
[bda60d9]176 */
[5291411]177
Note: See TracBrowser for help on using the repository browser.