source: mainline/uspace/drv/infrastructure/rootvirt/rootvirt.c@ 1a5b252

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

DDF support for function offlining and onlining. This allows
(anticipated) hot removal — support needs to be added in individual
drivers, currently there is support in test1 and partially in rootvirt.
Surprise removal is not supported. TODO synchronization.

  • Property mode set to 100644
File size: 4.3 KB
Line 
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 * @defgroup rootvirt Root device driver for virtual devices.
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 "rootvirt"
45
46/** Virtual function entry */
47typedef 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 */
55virtual_function_t virtual_functions[] = {
56#include "devices.def"
57 /* Terminating item */
58 {
59 .name = NULL,
60 .match_id = NULL
61 }
62};
63
64static int rootvirt_add_device(ddf_dev_t *dev);
65static int rootvirt_fun_online(ddf_fun_t *fun);
66static int rootvirt_fun_offline(ddf_fun_t *fun);
67
68static driver_ops_t rootvirt_ops = {
69 .add_device = &rootvirt_add_device,
70 .fun_online = &rootvirt_fun_online,
71 .fun_offline = &rootvirt_fun_offline
72};
73
74static driver_t rootvirt_driver = {
75 .name = NAME,
76 .driver_ops = &rootvirt_ops
77};
78
79/** Add function to the virtual device.
80 *
81 * @param vdev The virtual device
82 * @param vfun Virtual function description
83 * @return EOK on success or negative error code.
84 */
85static int rootvirt_add_fun(ddf_dev_t *vdev, virtual_function_t *vfun)
86{
87 ddf_fun_t *fun;
88 int rc;
89
90 ddf_msg(LVL_DEBUG, "Registering function `%s' (match \"%s\")",
91 vfun->name, vfun->match_id);
92
93 fun = ddf_fun_create(vdev, fun_inner, vfun->name);
94 if (fun == NULL) {
95 ddf_msg(LVL_ERROR, "Failed creating function %s", vfun->name);
96 return ENOMEM;
97 }
98
99 rc = ddf_fun_add_match_id(fun, vfun->match_id, 10);
100 if (rc != EOK) {
101 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
102 vfun->name);
103 ddf_fun_destroy(fun);
104 return rc;
105 }
106
107 rc = ddf_fun_bind(fun);
108 if (rc != EOK) {
109 ddf_msg(LVL_ERROR, "Failed binding function %s: %s",
110 vfun->name, str_error(rc));
111 ddf_fun_destroy(fun);
112 return rc;
113 }
114
115 ddf_msg(LVL_NOTE, "Registered child device `%s'", vfun->name);
116 return EOK;
117}
118
119static int rootvirt_add_device(ddf_dev_t *dev)
120{
121 static int instances = 0;
122
123 /*
124 * Allow only single instance of root virtual device.
125 */
126 instances++;
127 if (instances > 1) {
128 return ELIMIT;
129 }
130
131 ddf_msg(LVL_DEBUG, "add_device(handle=%d)", (int)dev->handle);
132
133 /*
134 * Go through all virtual functions and try to add them.
135 * We silently ignore failures.
136 */
137 virtual_function_t *vfun = virtual_functions;
138 while (vfun->name != NULL) {
139 (void) rootvirt_add_fun(dev, vfun);
140 vfun++;
141 }
142
143 return EOK;
144}
145
146static int rootvirt_fun_online(ddf_fun_t *fun)
147{
148 ddf_msg(LVL_DEBUG, "rootvirt_fun_online()");
149 return ddf_fun_online(fun);
150}
151
152static int rootvirt_fun_offline(ddf_fun_t *fun)
153{
154 ddf_msg(LVL_DEBUG, "rootvirt_fun_offline()");
155 return ddf_fun_offline(fun);
156}
157
158int main(int argc, char *argv[])
159{
160 printf(NAME ": HelenOS virtual devices root driver\n");
161
162 ddf_log_init(NAME, LVL_ERROR);
163 return ddf_driver_main(&rootvirt_driver);
164}
165
166/**
167 * @}
168 */
169
Note: See TracBrowser for help on using the repository browser.