source: mainline/uspace/drv/test/test1/test1.c@ 1d6dd2a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d6dd2a was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Remove unnecessary includes from <stdio.h>.

  • Property mode set to 100644
File size: 7.6 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 ddf_fun_add_to_category(fun_a, "virtual");
179
180 if (str_cmp(dev_name, "test1") == 0) {
181 (void) register_fun_verbose(dev,
182 "cloning myself ;-)", "clone",
183 "virtual&test1", 10, EOK, &test1->clone);
184 (void) register_fun_verbose(dev,
185 "cloning myself twice ;-)", "clone",
186 "virtual&test1", 10, EEXIST, NULL);
187 } else if (str_cmp(dev_name, "clone") == 0) {
188 (void) register_fun_verbose(dev,
189 "run by the same task", "child",
190 "virtual&test1&child", 10, EOK, &test1->child);
191 }
192
193 ddf_msg(LVL_DEBUG, "Device `%s' accepted.", dev_name);
194 return EOK;
195error:
196 return rc;
197}
198
199static errno_t fun_remove(ddf_fun_t *fun, const char *name)
200{
201 errno_t rc;
202
203 ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
204 rc = ddf_fun_offline(fun);
205 if (rc != EOK) {
206 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
207 return rc;
208 }
209
210 rc = ddf_fun_unbind(fun);
211 if (rc != EOK) {
212 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
213 return rc;
214 }
215
216 ddf_fun_destroy(fun);
217 return EOK;
218}
219
220static errno_t fun_unbind(ddf_fun_t *fun, const char *name)
221{
222 errno_t rc;
223
224 ddf_msg(LVL_DEBUG, "fun_unbind(%p, '%s')", fun, name);
225 rc = ddf_fun_unbind(fun);
226 if (rc != EOK) {
227 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
228 return rc;
229 }
230
231 ddf_fun_destroy(fun);
232 return EOK;
233}
234
235static errno_t test1_dev_remove(ddf_dev_t *dev)
236{
237 test1_t *test1 = (test1_t *)ddf_dev_data_get(dev);
238 errno_t rc;
239
240 ddf_msg(LVL_DEBUG, "test1_dev_remove(%p)", dev);
241
242 if (test1->fun_a != NULL) {
243 rc = fun_remove(test1->fun_a, "a");
244 if (rc != EOK)
245 return rc;
246 }
247
248 if (test1->clone != NULL) {
249 rc = fun_remove(test1->clone, "clone");
250 if (rc != EOK)
251 return rc;
252 }
253
254 if (test1->child != NULL) {
255 rc = fun_remove(test1->child, "child");
256 if (rc != EOK)
257 return rc;
258 }
259
260 return EOK;
261}
262
263static errno_t test1_dev_gone(ddf_dev_t *dev)
264{
265 test1_t *test1 = (test1_t *)ddf_dev_data_get(dev);
266 errno_t rc;
267
268 ddf_msg(LVL_DEBUG, "test1_dev_remove(%p)", dev);
269
270 if (test1->fun_a != NULL) {
271 rc = fun_unbind(test1->fun_a, "a");
272 if (rc != EOK)
273 return rc;
274 }
275
276 if (test1->clone != NULL) {
277 rc = fun_unbind(test1->clone, "clone");
278 if (rc != EOK)
279 return rc;
280 }
281
282 if (test1->child != NULL) {
283 rc = fun_unbind(test1->child, "child");
284 if (rc != EOK)
285 return rc;
286 }
287
288 return EOK;
289}
290
291static errno_t test1_fun_online(ddf_fun_t *fun)
292{
293 ddf_msg(LVL_DEBUG, "test1_fun_online()");
294 return ddf_fun_online(fun);
295}
296
297static errno_t test1_fun_offline(ddf_fun_t *fun)
298{
299 ddf_msg(LVL_DEBUG, "test1_fun_offline()");
300 return ddf_fun_offline(fun);
301}
302
303int main(int argc, char *argv[])
304{
305 printf(NAME ": HelenOS test1 virtual device driver\n");
306 ddf_log_init(NAME);
307 return ddf_driver_main(&test1_driver);
308}
309
Note: See TracBrowser for help on using the repository browser.