source: mainline/uspace/drv/infrastructure/rootvirt/rootvirt.c@ 5203e256

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5203e256 was 5203e256, checked in by Martin Decky <martin@…>, 14 years ago

keep the drivers source tree tidy by using logical subdirectories

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @defgroup rootvirt Root device driver for virtual devices.
31 * @{
32 */
33
34/** @file
35 */
36
37#include <assert.h>
38#include <stdio.h>
39#include <errno.h>
40#include <str_error.h>
41#include <ddf/driver.h>
42#include <ddf/log.h>
43
44#define NAME "rootvirt"
45
46/** Virtual function entry */
47typedef struct {
48 /** Function name */
49 const char *name;
50 /** Function match ID */
51 const char *match_id;
52} virtual_function_t;
53
54/** List of existing virtual functions */
55virtual_function_t virtual_functions[] = {
56#include "devices.def"
57 /* Terminating item */
58 {
59 .name = NULL,
60 .match_id = NULL
61 }
62};
63
64static int rootvirt_add_device(ddf_dev_t *dev);
65
66static driver_ops_t rootvirt_ops = {
67 .add_device = &rootvirt_add_device
68};
69
70static driver_t rootvirt_driver = {
71 .name = NAME,
72 .driver_ops = &rootvirt_ops
73};
74
75/** Add function to the virtual device.
76 *
77 * @param vdev The virtual device
78 * @param vfun Virtual function description
79 * @return EOK on success or negative error code.
80 */
81static int rootvirt_add_fun(ddf_dev_t *vdev, virtual_function_t *vfun)
82{
83 ddf_fun_t *fun;
84 int rc;
85
86 ddf_msg(LVL_DEBUG, "Registering function `%s' (match \"%s\")",
87 vfun->name, vfun->match_id);
88
89 fun = ddf_fun_create(vdev, fun_inner, vfun->name);
90 if (fun == NULL) {
91 ddf_msg(LVL_ERROR, "Failed creating function %s", vfun->name);
92 return ENOMEM;
93 }
94
95 rc = ddf_fun_add_match_id(fun, vfun->match_id, 10);
96 if (rc != EOK) {
97 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
98 vfun->name);
99 ddf_fun_destroy(fun);
100 return rc;
101 }
102
103 rc = ddf_fun_bind(fun);
104 if (rc != EOK) {
105 ddf_msg(LVL_ERROR, "Failed binding function %s: %s",
106 vfun->name, str_error(rc));
107 ddf_fun_destroy(fun);
108 return rc;
109 }
110
111 ddf_msg(LVL_NOTE, "Registered child device `%s'", vfun->name);
112 return EOK;
113}
114
115static int rootvirt_add_device(ddf_dev_t *dev)
116{
117 static int instances = 0;
118
119 /*
120 * Allow only single instance of root virtual device.
121 */
122 instances++;
123 if (instances > 1) {
124 return ELIMIT;
125 }
126
127 ddf_msg(LVL_DEBUG, "add_device(handle=%d)", (int)dev->handle);
128
129 /*
130 * Go through all virtual functions and try to add them.
131 * We silently ignore failures.
132 */
133 virtual_function_t *vfun = virtual_functions;
134 while (vfun->name != NULL) {
135 (void) rootvirt_add_fun(dev, vfun);
136 vfun++;
137 }
138
139 return EOK;
140}
141
142int main(int argc, char *argv[])
143{
144 printf(NAME ": HelenOS virtual devices root driver\n");
145
146 ddf_log_init(NAME, LVL_ERROR);
147 return ddf_driver_main(&rootvirt_driver);
148}
149
150/**
151 * @}
152 */
153
Note: See TracBrowser for help on using the repository browser.