source: mainline/uspace/drv/test/test1/test1.c@ 2a515dcd

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

Check return code from ddf_add_fun_to_category

  • Property mode set to 100644
File size: 7.8 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
3 * Copyright (c) 2011 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @file
31 */
32
33#include <assert.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#include <str.h>
40
41#include "test1.h"
42
43static errno_t test1_dev_add(ddf_dev_t *dev);
44static errno_t test1_dev_remove(ddf_dev_t *dev);
45static errno_t test1_dev_gone(ddf_dev_t *dev);
46static errno_t test1_fun_online(ddf_fun_t *fun);
47static errno_t test1_fun_offline(ddf_fun_t *fun);
48
49static driver_ops_t driver_ops = {
50 .dev_add = &test1_dev_add,
51 .dev_remove = &test1_dev_remove,
52 .dev_gone = &test1_dev_gone,
53 .fun_online = &test1_fun_online,
54 .fun_offline = &test1_fun_offline
55};
56
57static driver_t test1_driver = {
58 .name = NAME,
59 .driver_ops = &driver_ops
60};
61
62typedef struct {
63 ddf_fun_t *fun_a;
64 ddf_fun_t *clone;
65 ddf_fun_t *child;
66} test1_t;
67
68/** Register child and inform user about it.
69 *
70 * @param parent Parent device.
71 * @param message Message for the user.
72 * @param name Device name.
73 * @param match_id Device match id.
74 * @param score Device match score.
75 */
76static errno_t register_fun_verbose(ddf_dev_t *parent, const char *message,
77 const char *name, const char *match_id, int match_score,
78 errno_t expected_rc, ddf_fun_t **pfun)
79{
80 ddf_fun_t *fun = NULL;
81 errno_t 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 rc = ENOMEM;
89 goto leave;
90 }
91
92 rc = ddf_fun_add_match_id(fun, match_id, match_score);
93 if (rc != EOK) {
94 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
95 name);
96 goto leave;
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 goto leave;
104 }
105
106 ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
107 rc = EOK;
108
109leave:
110 if (rc != expected_rc) {
111 fprintf(stderr,
112 NAME ": Unexpected error registering function `%s'.\n"
113 NAME ": Expected \"%s\" but got \"%s\".\n",
114 name, str_error(expected_rc), str_error(rc));
115 }
116
117 if ((rc != EOK) && (fun != NULL)) {
118 ddf_fun_destroy(fun);
119 }
120
121 if (pfun != NULL)
122 *pfun = fun;
123
124 return rc;
125}
126
127/** Callback when new device is passed to this driver.
128 * This function is the body of the test: it shall register new child
129 * (named `clone') that shall be driven by the same task. When the clone
130 * is added, it registers another child (named `child') that is also driven
131 * by this task. The conditions ensure that we do not recurse indefinitely.
132 * When successful, the device tree shall contain following fragment:
133 *
134 * /virtual/test1
135 * /virtual/test1/clone
136 * /virtual/test1/clone/child
137 *
138 * and the DDF shall not deadlock.
139 *
140 *
141 * @param dev New device.
142 * @return Error code reporting success of the operation.
143 */
144static errno_t test1_dev_add(ddf_dev_t *dev)
145{
146 ddf_fun_t *fun_a;
147 test1_t *test1;
148 const char *dev_name;
149 errno_t rc;
150
151 dev_name = ddf_dev_get_name(dev);
152 ddf_msg(LVL_DEBUG, "dev_add(name=\"%s\", handle=%d)",
153 dev_name, (int) ddf_dev_get_handle(dev));
154
155 test1 = ddf_dev_data_alloc(dev, sizeof(test1_t));
156 if (test1 == NULL) {
157 ddf_msg(LVL_ERROR, "Failed allocating soft state.\n");
158 rc = ENOMEM;
159 goto error;
160 }
161
162 fun_a = ddf_fun_create(dev, fun_exposed, "a");
163 if (fun_a == NULL) {
164 ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
165 rc = ENOMEM;
166 goto error;
167 }
168
169 test1->fun_a = fun_a;
170
171 rc = ddf_fun_bind(fun_a);
172 if (rc != EOK) {
173 ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
174 ddf_fun_destroy(fun_a);
175 goto error;
176 }
177
178 rc = ddf_fun_add_to_category(fun_a, "virtual");
179 if (rc != EOK) {
180 ddf_msg(LVL_ERROR, "Failed adding function 'a' to category "
181 "'virtual'.");
182 ddf_fun_unbind(fun_a);
183 ddf_fun_destroy(fun_a);
184 goto error;
185 }
186
187 if (str_cmp(dev_name, "test1") == 0) {
188 (void) register_fun_verbose(dev,
189 "cloning myself ;-)", "clone",
190 "virtual&test1", 10, EOK, &test1->clone);
191 (void) register_fun_verbose(dev,
192 "cloning myself twice ;-)", "clone",
193 "virtual&test1", 10, EEXIST, NULL);
194 } else if (str_cmp(dev_name, "clone") == 0) {
195 (void) register_fun_verbose(dev,
196 "run by the same task", "child",
197 "virtual&test1&child", 10, EOK, &test1->child);
198 }
199
200 ddf_msg(LVL_DEBUG, "Device `%s' accepted.", dev_name);
201 return EOK;
202error:
203 return rc;
204}
205
206static errno_t fun_remove(ddf_fun_t *fun, const char *name)
207{
208 errno_t rc;
209
210 ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
211 rc = ddf_fun_offline(fun);
212 if (rc != EOK) {
213 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
214 return rc;
215 }
216
217 rc = ddf_fun_unbind(fun);
218 if (rc != EOK) {
219 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
220 return rc;
221 }
222
223 ddf_fun_destroy(fun);
224 return EOK;
225}
226
227static errno_t fun_unbind(ddf_fun_t *fun, const char *name)
228{
229 errno_t rc;
230
231 ddf_msg(LVL_DEBUG, "fun_unbind(%p, '%s')", fun, name);
232 rc = ddf_fun_unbind(fun);
233 if (rc != EOK) {
234 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
235 return rc;
236 }
237
238 ddf_fun_destroy(fun);
239 return EOK;
240}
241
242static errno_t test1_dev_remove(ddf_dev_t *dev)
243{
244 test1_t *test1 = (test1_t *)ddf_dev_data_get(dev);
245 errno_t rc;
246
247 ddf_msg(LVL_DEBUG, "test1_dev_remove(%p)", dev);
248
249 if (test1->fun_a != NULL) {
250 rc = fun_remove(test1->fun_a, "a");
251 if (rc != EOK)
252 return rc;
253 }
254
255 if (test1->clone != NULL) {
256 rc = fun_remove(test1->clone, "clone");
257 if (rc != EOK)
258 return rc;
259 }
260
261 if (test1->child != NULL) {
262 rc = fun_remove(test1->child, "child");
263 if (rc != EOK)
264 return rc;
265 }
266
267 return EOK;
268}
269
270static errno_t test1_dev_gone(ddf_dev_t *dev)
271{
272 test1_t *test1 = (test1_t *)ddf_dev_data_get(dev);
273 errno_t rc;
274
275 ddf_msg(LVL_DEBUG, "test1_dev_remove(%p)", dev);
276
277 if (test1->fun_a != NULL) {
278 rc = fun_unbind(test1->fun_a, "a");
279 if (rc != EOK)
280 return rc;
281 }
282
283 if (test1->clone != NULL) {
284 rc = fun_unbind(test1->clone, "clone");
285 if (rc != EOK)
286 return rc;
287 }
288
289 if (test1->child != NULL) {
290 rc = fun_unbind(test1->child, "child");
291 if (rc != EOK)
292 return rc;
293 }
294
295 return EOK;
296}
297
298static errno_t test1_fun_online(ddf_fun_t *fun)
299{
300 ddf_msg(LVL_DEBUG, "test1_fun_online()");
301 return ddf_fun_online(fun);
302}
303
304static errno_t test1_fun_offline(ddf_fun_t *fun)
305{
306 ddf_msg(LVL_DEBUG, "test1_fun_offline()");
307 return ddf_fun_offline(fun);
308}
309
310int main(int argc, char *argv[])
311{
312 printf(NAME ": HelenOS test1 virtual device driver\n");
313 ddf_log_init(NAME);
314 return ddf_driver_main(&test1_driver);
315}
Note: See TracBrowser for help on using the repository browser.