source: mainline/uspace/drv/root/virt/virt.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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