Fix decoding of changed MAC address.
[helenos.git] / uspace / drv / root / virt / virt.c
blob470dd7b2917a574af81eb0d3462a701c1318d709
1 /*
2 * Copyright (c) 2010 Vojtech Horky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /**
30 * @addtogroup virt
31 * @{
34 /** @file
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>
44 #define NAME "virt"
46 /** Virtual function entry */
47 typedef struct {
48 /** Function name */
49 const char *name;
50 /** Function match ID */
51 const char *match_id;
52 } virtual_function_t;
54 /** List of existing virtual functions */
55 virtual_function_t virtual_functions[] = {
56 #include "devices.def"
57 /* Terminating item */
59 .name = NULL,
60 .match_id = NULL
64 static errno_t virt_dev_add(ddf_dev_t *dev);
65 static errno_t virt_dev_remove(ddf_dev_t *dev);
66 static errno_t virt_fun_online(ddf_fun_t *fun);
67 static errno_t virt_fun_offline(ddf_fun_t *fun);
69 static driver_ops_t virt_ops = {
70 .dev_add = &virt_dev_add,
71 .dev_remove = &virt_dev_remove,
72 .fun_online = &virt_fun_online,
73 .fun_offline = &virt_fun_offline
76 static driver_t virt_driver = {
77 .name = NAME,
78 .driver_ops = &virt_ops
81 /* Device soft state */
82 typedef struct {
83 ddf_dev_t *dev;
84 list_t functions;
85 } virt_t;
87 /* Function soft state */
88 typedef struct {
89 ddf_fun_t *fun;
90 link_t dev_link;
91 } virt_fun_t;
93 static int instances = 0;
95 /** Add function to the virtual device.
97 * @param vdev The virtual device
98 * @param vfun Virtual function description
99 * @return EOK on success or an error code.
101 static errno_t virt_add_fun(virt_t *virt, virtual_function_t *vfun)
103 ddf_dev_t *vdev = virt->dev;
104 ddf_fun_t *fun;
105 virt_fun_t *rvfun;
106 errno_t rc;
108 ddf_msg(LVL_DEBUG, "Registering function `%s' (match \"%s\")",
109 vfun->name, vfun->match_id);
111 fun = ddf_fun_create(vdev, fun_inner, vfun->name);
112 if (fun == NULL) {
113 ddf_msg(LVL_ERROR, "Failed creating function %s", vfun->name);
114 return ENOMEM;
117 rvfun = ddf_fun_data_alloc(fun, sizeof(virt_fun_t));
118 if (rvfun == NULL) {
119 ddf_msg(LVL_ERROR, "Failed allocating soft state for %s.",
120 vfun->name);
121 ddf_fun_destroy(fun);
122 return ENOMEM;
125 rvfun->fun = fun;
127 rc = ddf_fun_add_match_id(fun, vfun->match_id, 10);
128 if (rc != EOK) {
129 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
130 vfun->name);
131 ddf_fun_destroy(fun);
132 return rc;
135 rc = ddf_fun_bind(fun);
136 if (rc != EOK) {
137 ddf_msg(LVL_ERROR, "Failed binding function %s: %s",
138 vfun->name, str_error(rc));
139 ddf_fun_destroy(fun);
140 return rc;
143 list_append(&rvfun->dev_link, &virt->functions);
145 ddf_msg(LVL_NOTE, "Registered child device `%s'", vfun->name);
146 return EOK;
149 static errno_t virt_fun_remove(virt_fun_t *rvfun)
151 errno_t rc;
152 const char *name = ddf_fun_get_name(rvfun->fun);
154 ddf_msg(LVL_DEBUG, "virt_fun_remove('%s')", name);
155 rc = ddf_fun_offline(rvfun->fun);
156 if (rc != EOK) {
157 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
158 return rc;
161 rc = ddf_fun_unbind(rvfun->fun);
162 if (rc != EOK) {
163 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
164 return rc;
167 list_remove(&rvfun->dev_link);
168 ddf_fun_destroy(rvfun->fun);
169 return EOK;
172 static errno_t virt_dev_add(ddf_dev_t *dev)
174 virt_t *virt;
177 * Allow only single instance of root virtual device.
179 if (++instances > 1) {
180 return ELIMIT;
183 ddf_msg(LVL_DEBUG, "dev_add(handle=%d)", (int)ddf_dev_get_handle(dev));
185 virt = ddf_dev_data_alloc(dev, sizeof(virt_t));
186 if (virt == NULL)
187 return ENOMEM;
189 virt->dev = dev;
190 list_initialize(&virt->functions);
193 * Go through all virtual functions and try to add them.
194 * We silently ignore failures.
196 virtual_function_t *vfun = virtual_functions;
197 while (vfun->name != NULL) {
198 (void) virt_add_fun(virt, vfun);
199 vfun++;
202 return EOK;
205 static errno_t virt_dev_remove(ddf_dev_t *dev)
207 virt_t *virt = (virt_t *)ddf_dev_data_get(dev);
208 errno_t rc;
210 while (!list_empty(&virt->functions)) {
211 virt_fun_t *rvfun = list_get_instance(
212 list_first(&virt->functions), virt_fun_t,
213 dev_link);
215 rc = virt_fun_remove(rvfun);
216 if (rc != EOK)
217 return rc;
220 --instances;
221 return EOK;
224 static errno_t virt_fun_online(ddf_fun_t *fun)
226 ddf_msg(LVL_DEBUG, "virt_fun_online()");
227 return ddf_fun_online(fun);
230 static errno_t virt_fun_offline(ddf_fun_t *fun)
232 ddf_msg(LVL_DEBUG, "virt_fun_offline()");
233 return ddf_fun_offline(fun);
236 int main(int argc, char *argv[])
238 printf(NAME ": HelenOS virtual devices root driver\n");
240 ddf_log_init(NAME);
241 return ddf_driver_main(&virt_driver);
245 * @}