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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since defaab2 was 2a37b9f, checked in by Jiri Svoboda <jiri@…>, 11 years ago

Reorganize platform drivers.

  • 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/**
[2a37b9f]30 * @defgroup virt Root device driver for virtual devices.
[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
[2a37b9f]64static int virt_dev_add(ddf_dev_t *dev);
65static int virt_dev_remove(ddf_dev_t *dev);
66static int virt_fun_online(ddf_fun_t *fun);
67static int 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
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 */
[2a37b9f]102static int virt_add_fun(virt_t *virt, virtual_function_t *vfun)
[6f9e7fea]103{
[2a37b9f]104 ddf_dev_t *vdev = virt->dev;
[83a2f43]105 ddf_fun_t *fun;
[2a37b9f]106 virt_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
[2a37b9f]118 rvfun = ddf_fun_data_alloc(fun, sizeof(virt_fun_t));
[deac215e]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
[2a37b9f]144 list_append(&rvfun->dev_link, &virt->functions);
[deac215e]145
[ebcb05a]146 ddf_msg(LVL_NOTE, "Registered child device `%s'", vfun->name);
[cd0684d]147 return EOK;
[6f9e7fea]148}
149
[2a37b9f]150static int virt_fun_remove(virt_fun_t *rvfun)
[deac215e]151{
152 int rc;
[56fd7cf]153 const char *name = ddf_fun_get_name(rvfun->fun);
[deac215e]154
[2a37b9f]155 ddf_msg(LVL_DEBUG, "virt_fun_remove('%s')", name);
[deac215e]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
[2a37b9f]174static int virt_dev_add(ddf_dev_t *dev)
[6f9e7fea]175{
[2a37b9f]176 virt_t *virt;
[6f9e7fea]177
178 /*
179 * Allow only single instance of root virtual device.
180 */
[deac215e]181 if (++instances > 1) {
[6f9e7fea]182 return ELIMIT;
183 }
184
[56fd7cf]185 ddf_msg(LVL_DEBUG, "dev_add(handle=%d)", (int)ddf_dev_get_handle(dev));
[bab6388]186
[2a37b9f]187 virt = ddf_dev_data_alloc(dev, sizeof(virt_t));
188 if (virt == NULL)
[deac215e]189 return ENOMEM;
190
[2a37b9f]191 virt->dev = dev;
192 list_initialize(&virt->functions);
[deac215e]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) {
[2a37b9f]200 (void) virt_add_fun(virt, vfun);
[8b1e15ac]201 vfun++;
[6f9e7fea]202 }
203
204 return EOK;
205}
206
[2a37b9f]207static int virt_dev_remove(ddf_dev_t *dev)
[deac215e]208{
[2a37b9f]209 virt_t *virt = (virt_t *)ddf_dev_data_get(dev);
[deac215e]210 int rc;
211
[2a37b9f]212 while (!list_empty(&virt->functions)) {
213 virt_fun_t *rvfun = list_get_instance(
214 list_first(&virt->functions), virt_fun_t,
[deac215e]215 dev_link);
216
[2a37b9f]217 rc = virt_fun_remove(rvfun);
[deac215e]218 if (rc != EOK)
219 return rc;
220 }
221
222 --instances;
223 return EOK;
224}
225
[2a37b9f]226static int virt_fun_online(ddf_fun_t *fun)
[1a5b252]227{
[2a37b9f]228 ddf_msg(LVL_DEBUG, "virt_fun_online()");
[1a5b252]229 return ddf_fun_online(fun);
230}
231
[2a37b9f]232static int virt_fun_offline(ddf_fun_t *fun)
[1a5b252]233{
[2a37b9f]234 ddf_msg(LVL_DEBUG, "virt_fun_offline()");
[1a5b252]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
[267f235]242 ddf_log_init(NAME);
[2a37b9f]243 return ddf_driver_main(&virt_driver);
[6f9e7fea]244}
245
246/**
247 * @}
248 */
249
Note: See TracBrowser for help on using the repository browser.