| 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 | * @addtogroup virt
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | /** @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 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>
|
|---|
| 43 |
|
|---|
| 44 | #define NAME "virt"
|
|---|
| 45 |
|
|---|
| 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;
|
|---|
| 53 |
|
|---|
| 54 | /** List of existing virtual functions */
|
|---|
| 55 | virtual_function_t virtual_functions[] = {
|
|---|
| 56 | #include "devices.def"
|
|---|
| 57 | /* Terminating item */
|
|---|
| 58 | {
|
|---|
| 59 | .name = NULL,
|
|---|
| 60 | .match_id = NULL
|
|---|
| 61 | }
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 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);
|
|---|
| 68 |
|
|---|
| 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
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | static driver_t virt_driver = {
|
|---|
| 77 | .name = NAME,
|
|---|
| 78 | .driver_ops = &virt_ops
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | /* Device soft state */
|
|---|
| 82 | typedef struct {
|
|---|
| 83 | ddf_dev_t *dev;
|
|---|
| 84 | list_t functions;
|
|---|
| 85 | } virt_t;
|
|---|
| 86 |
|
|---|
| 87 | /* Function soft state */
|
|---|
| 88 | typedef struct {
|
|---|
| 89 | ddf_fun_t *fun;
|
|---|
| 90 | link_t dev_link;
|
|---|
| 91 | } virt_fun_t;
|
|---|
| 92 |
|
|---|
| 93 | static int instances = 0;
|
|---|
| 94 |
|
|---|
| 95 | /** Add function to the virtual device.
|
|---|
| 96 | *
|
|---|
| 97 | * @param vdev The virtual device
|
|---|
| 98 | * @param vfun Virtual function description
|
|---|
| 99 | * @return EOK on success or an error code.
|
|---|
| 100 | */
|
|---|
| 101 | static errno_t virt_add_fun(virt_t *virt, virtual_function_t *vfun)
|
|---|
| 102 | {
|
|---|
| 103 | ddf_dev_t *vdev = virt->dev;
|
|---|
| 104 | ddf_fun_t *fun;
|
|---|
| 105 | virt_fun_t *rvfun;
|
|---|
| 106 | errno_t rc;
|
|---|
| 107 |
|
|---|
| 108 | ddf_msg(LVL_DEBUG, "Registering function `%s' (match \"%s\")",
|
|---|
| 109 | vfun->name, vfun->match_id);
|
|---|
| 110 |
|
|---|
| 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;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 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;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | rvfun->fun = fun;
|
|---|
| 126 |
|
|---|
| 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;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 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;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | list_append(&rvfun->dev_link, &virt->functions);
|
|---|
| 144 |
|
|---|
| 145 | ddf_msg(LVL_NOTE, "Registered child device `%s'", vfun->name);
|
|---|
| 146 | return EOK;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | static errno_t virt_fun_remove(virt_fun_t *rvfun)
|
|---|
| 150 | {
|
|---|
| 151 | errno_t rc;
|
|---|
| 152 | const char *name = ddf_fun_get_name(rvfun->fun);
|
|---|
| 153 |
|
|---|
| 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;
|
|---|
| 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 |
|
|---|
| 172 | static errno_t virt_dev_add(ddf_dev_t *dev)
|
|---|
| 173 | {
|
|---|
| 174 | virt_t *virt;
|
|---|
| 175 |
|
|---|
| 176 | /*
|
|---|
| 177 | * Allow only single instance of root virtual device.
|
|---|
| 178 | */
|
|---|
| 179 | if (++instances > 1) {
|
|---|
| 180 | return ELIMIT;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | ddf_msg(LVL_DEBUG, "dev_add(handle=%d)", (int)ddf_dev_get_handle(dev));
|
|---|
| 184 |
|
|---|
| 185 | virt = ddf_dev_data_alloc(dev, sizeof(virt_t));
|
|---|
| 186 | if (virt == NULL)
|
|---|
| 187 | return ENOMEM;
|
|---|
| 188 |
|
|---|
| 189 | virt->dev = dev;
|
|---|
| 190 | list_initialize(&virt->functions);
|
|---|
| 191 |
|
|---|
| 192 | /*
|
|---|
| 193 | * Go through all virtual functions and try to add them.
|
|---|
| 194 | * We silently ignore failures.
|
|---|
| 195 | */
|
|---|
| 196 | virtual_function_t *vfun = virtual_functions;
|
|---|
| 197 | while (vfun->name != NULL) {
|
|---|
| 198 | (void) virt_add_fun(virt, vfun);
|
|---|
| 199 | vfun++;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | return EOK;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | static errno_t virt_dev_remove(ddf_dev_t *dev)
|
|---|
| 206 | {
|
|---|
| 207 | virt_t *virt = (virt_t *)ddf_dev_data_get(dev);
|
|---|
| 208 | errno_t rc;
|
|---|
| 209 |
|
|---|
| 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);
|
|---|
| 214 |
|
|---|
| 215 | rc = virt_fun_remove(rvfun);
|
|---|
| 216 | if (rc != EOK)
|
|---|
| 217 | return rc;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | --instances;
|
|---|
| 221 | return EOK;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | static errno_t virt_fun_online(ddf_fun_t *fun)
|
|---|
| 225 | {
|
|---|
| 226 | ddf_msg(LVL_DEBUG, "virt_fun_online()");
|
|---|
| 227 | return ddf_fun_online(fun);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | static errno_t virt_fun_offline(ddf_fun_t *fun)
|
|---|
| 231 | {
|
|---|
| 232 | ddf_msg(LVL_DEBUG, "virt_fun_offline()");
|
|---|
| 233 | return ddf_fun_offline(fun);
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | int main(int argc, char *argv[])
|
|---|
| 237 | {
|
|---|
| 238 | printf(NAME ": HelenOS virtual devices root driver\n");
|
|---|
| 239 |
|
|---|
| 240 | ddf_log_init(NAME);
|
|---|
| 241 | return ddf_driver_main(&virt_driver);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /**
|
|---|
| 245 | * @}
|
|---|
| 246 | */
|
|---|