2 * Copyright (c) 2010 Vojtech Horky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
40 #include <str_error.h>
41 #include <ddf/driver.h>
46 /** Virtual function entry */
50 /** Function match ID */
54 /** List of existing virtual functions */
55 virtual_function_t virtual_functions
[] = {
56 #include "devices.def"
57 /* Terminating item */
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
= {
78 .driver_ops
= &virt_ops
81 /* Device soft state */
87 /* Function soft state */
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
;
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
);
113 ddf_msg(LVL_ERROR
, "Failed creating function %s", vfun
->name
);
117 rvfun
= ddf_fun_data_alloc(fun
, sizeof(virt_fun_t
));
119 ddf_msg(LVL_ERROR
, "Failed allocating soft state for %s.",
121 ddf_fun_destroy(fun
);
127 rc
= ddf_fun_add_match_id(fun
, vfun
->match_id
, 10);
129 ddf_msg(LVL_ERROR
, "Failed adding match IDs to function %s",
131 ddf_fun_destroy(fun
);
135 rc
= ddf_fun_bind(fun
);
137 ddf_msg(LVL_ERROR
, "Failed binding function %s: %s",
138 vfun
->name
, str_error(rc
));
139 ddf_fun_destroy(fun
);
143 list_append(&rvfun
->dev_link
, &virt
->functions
);
145 ddf_msg(LVL_NOTE
, "Registered child device `%s'", vfun
->name
);
149 static errno_t
virt_fun_remove(virt_fun_t
*rvfun
)
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
);
157 ddf_msg(LVL_ERROR
, "Error offlining function '%s'.", name
);
161 rc
= ddf_fun_unbind(rvfun
->fun
);
163 ddf_msg(LVL_ERROR
, "Failed unbinding function '%s'.", name
);
167 list_remove(&rvfun
->dev_link
);
168 ddf_fun_destroy(rvfun
->fun
);
172 static errno_t
virt_dev_add(ddf_dev_t
*dev
)
177 * Allow only single instance of root virtual device.
179 if (++instances
> 1) {
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
));
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
);
205 static errno_t
virt_dev_remove(ddf_dev_t
*dev
)
207 virt_t
*virt
= (virt_t
*)ddf_dev_data_get(dev
);
210 while (!list_empty(&virt
->functions
)) {
211 virt_fun_t
*rvfun
= list_get_instance(
212 list_first(&virt
->functions
), virt_fun_t
,
215 rc
= virt_fun_remove(rvfun
);
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");
241 return ddf_driver_main(&virt_driver
);