source: mainline/uspace/drv/test/test2/test2.c@ deac215e

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

rootvirt removal support.

  • Property mode set to 100644
File size: 6.1 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/** @file
30 */
31
32#include <assert.h>
33#include <async.h>
34#include <stdio.h>
35#include <errno.h>
36#include <str_error.h>
37#include <ddf/driver.h>
38#include <ddf/log.h>
39
40#define NAME "test2"
41
42static int test2_add_device(ddf_dev_t *dev);
43static int test2_dev_remove(ddf_dev_t *dev);
44static int test2_fun_online(ddf_fun_t *fun);
45static int test2_fun_offline(ddf_fun_t *fun);
46
47static driver_ops_t driver_ops = {
48 .add_device = &test2_add_device,
49 .dev_remove = &test2_dev_remove,
50 .fun_online = &test2_fun_online,
51 .fun_offline = &test2_fun_offline
52};
53
54static driver_t test2_driver = {
55 .name = NAME,
56 .driver_ops = &driver_ops
57};
58
59/* Device soft state */
60typedef struct {
61 ddf_dev_t *dev;
62
63 ddf_fun_t *fun_a;
64 ddf_fun_t *fun_err;
65 ddf_fun_t *child;
66 ddf_fun_t *test1;
67} test2_t;
68
69/** Register child and inform user about it.
70 *
71 * @param parent Parent device.
72 * @param message Message for the user.
73 * @param name Device name.
74 * @param match_id Device match id.
75 * @param score Device match score.
76 */
77static int register_fun_verbose(ddf_dev_t *parent, const char *message,
78 const char *name, const char *match_id, int match_score, ddf_fun_t **pfun)
79{
80 ddf_fun_t *fun;
81 int rc;
82
83 ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
84
85 fun = ddf_fun_create(parent, fun_inner, name);
86 if (fun == NULL) {
87 ddf_msg(LVL_ERROR, "Failed creating function %s", name);
88 return ENOMEM;
89 }
90
91 rc = ddf_fun_add_match_id(fun, match_id, match_score);
92 if (rc != EOK) {
93 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
94 name);
95 ddf_fun_destroy(fun);
96 return rc;
97 }
98
99 rc = ddf_fun_bind(fun);
100 if (rc != EOK) {
101 ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
102 str_error(rc));
103 ddf_fun_destroy(fun);
104 return rc;
105 }
106
107 *pfun = fun;
108
109 ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
110 return EOK;
111}
112
113/** Add child devices after some sleep.
114 *
115 * @param arg Parent device structure (ddf_dev_t *).
116 * @return Always EOK.
117 */
118static int postponed_birth(void *arg)
119{
120 test2_t *test2 = (test2_t *) arg;
121 ddf_fun_t *fun_a;
122 int rc;
123
124 async_usleep(1000);
125
126 (void) register_fun_verbose(test2->dev, "child driven by the same task",
127 "child", "virtual&test2", 10, &test2->child);
128 (void) register_fun_verbose(test2->dev, "child driven by test1",
129 "test1", "virtual&test1", 10, &test2->test1);
130
131 fun_a = ddf_fun_create(test2->dev, fun_exposed, "a");
132 if (fun_a == NULL) {
133 ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
134 return ENOMEM;
135 }
136
137 rc = ddf_fun_bind(fun_a);
138 if (rc != EOK) {
139 ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
140 return rc;
141 }
142
143 ddf_fun_add_to_category(fun_a, "virtual");
144 test2->fun_a = fun_a;
145
146 return EOK;
147}
148
149static int fun_remove(ddf_fun_t *fun, const char *name)
150{
151 int rc;
152
153 ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
154 rc = ddf_fun_offline(fun);
155 if (rc != EOK) {
156 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
157 return rc;
158 }
159
160 rc = ddf_fun_unbind(fun);
161 if (rc != EOK) {
162 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
163 return rc;
164 }
165
166 ddf_fun_destroy(fun);
167 return EOK;
168}
169
170static int test2_add_device(ddf_dev_t *dev)
171{
172 test2_t *test2;
173
174 ddf_msg(LVL_DEBUG, "test2_add_device(name=\"%s\", handle=%d)",
175 dev->name, (int) dev->handle);
176
177 test2 = ddf_dev_data_alloc(dev, sizeof(test2_t));
178 if (test2 == NULL) {
179 ddf_msg(LVL_ERROR, "Failed allocating soft state.");
180 return ENOMEM;
181 }
182
183 test2->dev = dev;
184
185 if (str_cmp(dev->name, "child") != 0) {
186 fid_t postpone = fibril_create(postponed_birth, test2);
187 if (postpone == 0) {
188 ddf_msg(LVL_ERROR, "fibril_create() failed.");
189 return ENOMEM;
190 }
191 fibril_add_ready(postpone);
192 } else {
193 (void) register_fun_verbose(dev, "child without available driver",
194 "ERROR", "non-existent.match.id", 10, &test2->fun_err);
195 }
196
197 return EOK;
198}
199
200static int test2_dev_remove(ddf_dev_t *dev)
201{
202 test2_t *test2 = (test2_t *)dev->driver_data;
203 int rc;
204
205 ddf_msg(LVL_DEBUG, "test2_dev_remove(%p)", dev);
206
207 if (test2->fun_a != NULL) {
208 rc = fun_remove(test2->fun_a, "a");
209 if (rc != EOK)
210 return rc;
211 }
212
213 if (test2->fun_err != NULL) {
214 rc = fun_remove(test2->fun_err, "ERROR");
215 if (rc != EOK)
216 return rc;
217 }
218
219 if (test2->child != NULL) {
220 rc = fun_remove(test2->child, "child");
221 if (rc != EOK)
222 return rc;
223 }
224
225 if (test2->test1 != NULL) {
226 rc = fun_remove(test2->test1, "test1");
227 if (rc != EOK)
228 return rc;
229 }
230
231 return EOK;
232}
233
234
235static int test2_fun_online(ddf_fun_t *fun)
236{
237 ddf_msg(LVL_DEBUG, "test2_fun_online()");
238 return ddf_fun_online(fun);
239}
240
241static int test2_fun_offline(ddf_fun_t *fun)
242{
243 ddf_msg(LVL_DEBUG, "test2_fun_offline()");
244 return ddf_fun_offline(fun);
245}
246
247int main(int argc, char *argv[])
248{
249 printf(NAME ": HelenOS test2 virtual device driver\n");
250 ddf_log_init(NAME, LVL_ERROR);
251 return ddf_driver_main(&test2_driver);
252}
253
254
Note: See TracBrowser for help on using the repository browser.