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
RevLine 
[6f9e7fea]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>
[af6b5157]41#include <ddf/driver.h>
[fc51296]42#include <ddf/log.h>
[6f9e7fea]43
44#define NAME "rootvirt"
45
[8b1e15ac]46/** Virtual function entry */
[6f9e7fea]47typedef struct {
[8b1e15ac]48 /** Function name */
[6f9e7fea]49 const char *name;
[8b1e15ac]50 /** Function match ID */
[6f9e7fea]51 const char *match_id;
[8b1e15ac]52} virtual_function_t;
[6f9e7fea]53
[8b1e15ac]54/** List of existing virtual functions */
55virtual_function_t virtual_functions[] = {
[6f9e7fea]56#include "devices.def"
[8b1e15ac]57 /* Terminating item */
[6f9e7fea]58 {
59 .name = NULL,
60 .match_id = NULL
61 }
62};
63
[0c0f823b]64static int rootvirt_dev_add(ddf_dev_t *dev);
[deac215e]65static int rootvirt_dev_remove(ddf_dev_t *dev);
[1a5b252]66static int rootvirt_fun_online(ddf_fun_t *fun);
67static int rootvirt_fun_offline(ddf_fun_t *fun);
[6f9e7fea]68
69static driver_ops_t rootvirt_ops = {
[0c0f823b]70 .dev_add = &rootvirt_dev_add,
[deac215e]71 .dev_remove = &rootvirt_dev_remove,
[1a5b252]72 .fun_online = &rootvirt_fun_online,
73 .fun_offline = &rootvirt_fun_offline
[6f9e7fea]74};
75
76static driver_t rootvirt_driver = {
77 .name = NAME,
78 .driver_ops = &rootvirt_ops
79};
80
[deac215e]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
[8b1e15ac]96/** Add function to the virtual device.
[6f9e7fea]97 *
[8b1e15ac]98 * @param vdev The virtual device
99 * @param vfun Virtual function description
100 * @return EOK on success or negative error code.
[6f9e7fea]101 */
[deac215e]102static int rootvirt_add_fun(rootvirt_t *rootvirt, virtual_function_t *vfun)
[6f9e7fea]103{
[deac215e]104 ddf_dev_t *vdev = rootvirt->dev;
[83a2f43]105 ddf_fun_t *fun;
[deac215e]106 rootvirt_fun_t *rvfun;
[cd0684d]107 int rc;
108
[ebcb05a]109 ddf_msg(LVL_DEBUG, "Registering function `%s' (match \"%s\")",
[8b1e15ac]110 vfun->name, vfun->match_id);
[6f9e7fea]111
[cd0684d]112 fun = ddf_fun_create(vdev, fun_inner, vfun->name);
113 if (fun == NULL) {
[ebcb05a]114 ddf_msg(LVL_ERROR, "Failed creating function %s", vfun->name);
[cd0684d]115 return ENOMEM;
116 }
[6f9e7fea]117
[deac215e]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
[cd0684d]128 rc = ddf_fun_add_match_id(fun, vfun->match_id, 10);
129 if (rc != EOK) {
[ebcb05a]130 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
[8b1e15ac]131 vfun->name);
[cd0684d]132 ddf_fun_destroy(fun);
133 return rc;
[6f9e7fea]134 }
135
[cd0684d]136 rc = ddf_fun_bind(fun);
137 if (rc != EOK) {
[ebcb05a]138 ddf_msg(LVL_ERROR, "Failed binding function %s: %s",
139 vfun->name, str_error(rc));
[cd0684d]140 ddf_fun_destroy(fun);
141 return rc;
142 }
143
[deac215e]144 list_append(&rvfun->dev_link, &rootvirt->functions);
145
[ebcb05a]146 ddf_msg(LVL_NOTE, "Registered child device `%s'", vfun->name);
[cd0684d]147 return EOK;
[6f9e7fea]148}
149
[deac215e]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
[0c0f823b]174static int rootvirt_dev_add(ddf_dev_t *dev)
[6f9e7fea]175{
[deac215e]176 rootvirt_t *rootvirt;
[6f9e7fea]177
178 /*
179 * Allow only single instance of root virtual device.
180 */
[deac215e]181 if (++instances > 1) {
[6f9e7fea]182 return ELIMIT;
183 }
184
[0c0f823b]185 ddf_msg(LVL_DEBUG, "dev_add(handle=%d)", (int)dev->handle);
[bab6388]186
[deac215e]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
[6f9e7fea]194 /*
[8b1e15ac]195 * Go through all virtual functions and try to add them.
[6f9e7fea]196 * We silently ignore failures.
197 */
[8b1e15ac]198 virtual_function_t *vfun = virtual_functions;
199 while (vfun->name != NULL) {
[deac215e]200 (void) rootvirt_add_fun(rootvirt, vfun);
[8b1e15ac]201 vfun++;
[6f9e7fea]202 }
203
204 return EOK;
205}
206
[deac215e]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
[1a5b252]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
[6f9e7fea]238int main(int argc, char *argv[])
239{
240 printf(NAME ": HelenOS virtual devices root driver\n");
[fc51296]241
242 ddf_log_init(NAME, LVL_ERROR);
[83a2f43]243 return ddf_driver_main(&rootvirt_driver);
[6f9e7fea]244}
245
246/**
247 * @}
248 */
249
Note: See TracBrowser for help on using the repository browser.