source: mainline/uspace/drv/infrastructure/rootvirt/rootvirt.c@ 1170ea3c

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

Rename DDF entry point add_device to dev_add.

  • Property mode set to 100644
File size: 5.9 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_dev_add(ddf_dev_t *dev);
65static int rootvirt_dev_remove(ddf_dev_t *dev);
66static int rootvirt_fun_online(ddf_fun_t *fun);
67static int rootvirt_fun_offline(ddf_fun_t *fun);
68
69static driver_ops_t rootvirt_ops = {
70 .dev_add = &rootvirt_dev_add,
71 .dev_remove = &rootvirt_dev_remove,
72 .fun_online = &rootvirt_fun_online,
73 .fun_offline = &rootvirt_fun_offline
74};
75
76static driver_t rootvirt_driver = {
77 .name = NAME,
78 .driver_ops = &rootvirt_ops
79};
80
81/* Device soft state */
82typedef struct {
83 ddf_dev_t *dev;
84 list_t functions;
85} rootvirt_t;
86
87/* Function soft state */
88typedef struct {
89 ddf_fun_t *fun;
90 link_t dev_link;
91} rootvirt_fun_t;
92
93static int instances = 0;
94
95
96/** Add function to the virtual device.
97 *
98 * @param vdev The virtual device
99 * @param vfun Virtual function description
100 * @return EOK on success or negative error code.
101 */
102static int rootvirt_add_fun(rootvirt_t *rootvirt, virtual_function_t *vfun)
103{
104 ddf_dev_t *vdev = rootvirt->dev;
105 ddf_fun_t *fun;
106 rootvirt_fun_t *rvfun;
107 int rc;
108
109 ddf_msg(LVL_DEBUG, "Registering function `%s' (match \"%s\")",
110 vfun->name, vfun->match_id);
111
112 fun = ddf_fun_create(vdev, fun_inner, vfun->name);
113 if (fun == NULL) {
114 ddf_msg(LVL_ERROR, "Failed creating function %s", vfun->name);
115 return ENOMEM;
116 }
117
118 rvfun = ddf_fun_data_alloc(fun, sizeof(rootvirt_fun_t));
119 if (rvfun == NULL) {
120 ddf_msg(LVL_ERROR, "Failed allocating soft state for %s.",
121 vfun->name);
122 ddf_fun_destroy(fun);
123 return ENOMEM;
124 }
125
126 rvfun->fun = fun;
127
128 rc = ddf_fun_add_match_id(fun, vfun->match_id, 10);
129 if (rc != EOK) {
130 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
131 vfun->name);
132 ddf_fun_destroy(fun);
133 return rc;
134 }
135
136 rc = ddf_fun_bind(fun);
137 if (rc != EOK) {
138 ddf_msg(LVL_ERROR, "Failed binding function %s: %s",
139 vfun->name, str_error(rc));
140 ddf_fun_destroy(fun);
141 return rc;
142 }
143
144 list_append(&rvfun->dev_link, &rootvirt->functions);
145
146 ddf_msg(LVL_NOTE, "Registered child device `%s'", vfun->name);
147 return EOK;
148}
149
150static int rootvirt_fun_remove(rootvirt_fun_t *rvfun)
151{
152 int rc;
153 const char *name = rvfun->fun->name;
154
155 ddf_msg(LVL_DEBUG, "rootvirt_fun_remove('%s')", name);
156 rc = ddf_fun_offline(rvfun->fun);
157 if (rc != EOK) {
158 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
159 return rc;
160 }
161
162 rc = ddf_fun_unbind(rvfun->fun);
163 if (rc != EOK) {
164 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
165 return rc;
166 }
167
168 list_remove(&rvfun->dev_link);
169 ddf_fun_destroy(rvfun->fun);
170 return EOK;
171}
172
173
174static int rootvirt_dev_add(ddf_dev_t *dev)
175{
176 rootvirt_t *rootvirt;
177
178 /*
179 * Allow only single instance of root virtual device.
180 */
181 if (++instances > 1) {
182 return ELIMIT;
183 }
184
185 ddf_msg(LVL_DEBUG, "dev_add(handle=%d)", (int)dev->handle);
186
187 rootvirt = ddf_dev_data_alloc(dev, sizeof(rootvirt_t));
188 if (rootvirt == NULL)
189 return ENOMEM;
190
191 rootvirt->dev = dev;
192 list_initialize(&rootvirt->functions);
193
194 /*
195 * Go through all virtual functions and try to add them.
196 * We silently ignore failures.
197 */
198 virtual_function_t *vfun = virtual_functions;
199 while (vfun->name != NULL) {
200 (void) rootvirt_add_fun(rootvirt, vfun);
201 vfun++;
202 }
203
204 return EOK;
205}
206
207static int rootvirt_dev_remove(ddf_dev_t *dev)
208{
209 rootvirt_t *rootvirt = (rootvirt_t *)dev->driver_data;
210 int rc;
211
212 while (!list_empty(&rootvirt->functions)) {
213 rootvirt_fun_t *rvfun = list_get_instance(
214 list_first(&rootvirt->functions), rootvirt_fun_t,
215 dev_link);
216
217 rc = rootvirt_fun_remove(rvfun);
218 if (rc != EOK)
219 return rc;
220 }
221
222 --instances;
223 return EOK;
224}
225
226static int rootvirt_fun_online(ddf_fun_t *fun)
227{
228 ddf_msg(LVL_DEBUG, "rootvirt_fun_online()");
229 return ddf_fun_online(fun);
230}
231
232static int rootvirt_fun_offline(ddf_fun_t *fun)
233{
234 ddf_msg(LVL_DEBUG, "rootvirt_fun_offline()");
235 return ddf_fun_offline(fun);
236}
237
238int main(int argc, char *argv[])
239{
240 printf(NAME ": HelenOS virtual devices root driver\n");
241
242 ddf_log_init(NAME, LVL_ERROR);
243 return ddf_driver_main(&rootvirt_driver);
244}
245
246/**
247 * @}
248 */
249
Note: See TracBrowser for help on using the repository browser.