source: mainline/uspace/drv/test/test2/test2.c@ 9364ced

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9364ced 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.4 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.h>
37#include <str_error.h>
38#include <ddf/driver.h>
39#include <ddf/log.h>
40
41#define NAME "test2"
42
43static errno_t test2_dev_add(ddf_dev_t *dev);
44static errno_t test2_dev_remove(ddf_dev_t *dev);
45static errno_t test2_dev_gone(ddf_dev_t *dev);
46static errno_t test2_fun_online(ddf_fun_t *fun);
47static errno_t test2_fun_offline(ddf_fun_t *fun);
48
49static driver_ops_t driver_ops = {
50 .dev_add = &test2_dev_add,
51 .dev_remove = &test2_dev_remove,
52 .dev_gone = &test2_dev_gone,
53 .fun_online = &test2_fun_online,
54 .fun_offline = &test2_fun_offline
55};
56
57static driver_t test2_driver = {
58 .name = NAME,
59 .driver_ops = &driver_ops
60};
61
62/* Device soft state */
63typedef struct {
64 ddf_dev_t *dev;
65
66 ddf_fun_t *fun_a;
67 ddf_fun_t *fun_err;
68 ddf_fun_t *child;
69 ddf_fun_t *test1;
70} test2_t;
71
72/** Register child and inform user about it.
73 *
74 * @param parent Parent device.
75 * @param message Message for the user.
76 * @param name Device name.
77 * @param match_id Device match id.
78 * @param score Device match score.
79 */
80static errno_t register_fun_verbose(ddf_dev_t *parent, const char *message,
81 const char *name, const char *match_id, int match_score, ddf_fun_t **pfun)
82{
83 ddf_fun_t *fun;
84 errno_t rc;
85
86 ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
87
88 fun = ddf_fun_create(parent, fun_inner, name);
89 if (fun == NULL) {
90 ddf_msg(LVL_ERROR, "Failed creating function %s", name);
91 return ENOMEM;
92 }
93
94 rc = ddf_fun_add_match_id(fun, match_id, match_score);
95 if (rc != EOK) {
96 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
97 name);
98 ddf_fun_destroy(fun);
99 return rc;
100 }
101
102 rc = ddf_fun_bind(fun);
103 if (rc != EOK) {
104 ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
105 str_error(rc));
106 ddf_fun_destroy(fun);
107 return rc;
108 }
109
110 *pfun = fun;
111
112 ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
113 return EOK;
114}
115
116/** Simulate plugging and surprise unplugging.
117 *
118 * @param arg Parent device structure (ddf_dev_t *).
119 * @return Always EOK.
120 */
121static errno_t plug_unplug(void *arg)
122{
123 test2_t *test2 = (test2_t *) arg;
124 ddf_fun_t *fun_a;
125 errno_t rc;
126
127 async_usleep(1000);
128
129 (void) register_fun_verbose(test2->dev, "child driven by the same task",
130 "child", "virtual&test2", 10, &test2->child);
131 (void) register_fun_verbose(test2->dev, "child driven by test1",
132 "test1", "virtual&test1", 10, &test2->test1);
133
134 fun_a = ddf_fun_create(test2->dev, fun_exposed, "a");
135 if (fun_a == NULL) {
136 ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
137 return ENOMEM;
138 }
139
140 rc = ddf_fun_bind(fun_a);
141 if (rc != EOK) {
142 ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
143 return rc;
144 }
145
146 ddf_fun_add_to_category(fun_a, "virtual");
147 test2->fun_a = fun_a;
148
149 async_usleep(10000000);
150
151 ddf_msg(LVL_NOTE, "Unbinding function test1.");
152 ddf_fun_unbind(test2->test1);
153 async_usleep(1000000);
154 ddf_msg(LVL_NOTE, "Unbinding function child.");
155 ddf_fun_unbind(test2->child);
156
157 return EOK;
158}
159
160static errno_t fun_remove(ddf_fun_t *fun, const char *name)
161{
162 errno_t rc;
163
164 ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
165 rc = ddf_fun_offline(fun);
166 if (rc != EOK) {
167 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
168 return rc;
169 }
170
171 rc = ddf_fun_unbind(fun);
172 if (rc != EOK) {
173 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
174 return rc;
175 }
176
177 ddf_fun_destroy(fun);
178 return EOK;
179}
180
181static errno_t fun_unbind(ddf_fun_t *fun, const char *name)
182{
183 errno_t rc;
184
185 ddf_msg(LVL_DEBUG, "fun_unbind(%p, '%s')", fun, name);
186 rc = ddf_fun_unbind(fun);
187 if (rc != EOK) {
188 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
189 return rc;
190 }
191
192 ddf_fun_destroy(fun);
193 return EOK;
194}
195
196static errno_t test2_dev_add(ddf_dev_t *dev)
197{
198 test2_t *test2;
199 const char *dev_name = ddf_dev_get_name(dev);
200
201 ddf_msg(LVL_DEBUG, "test2_dev_add(name=\"%s\", handle=%d)",
202 dev_name, (int) ddf_dev_get_handle(dev));
203
204 test2 = ddf_dev_data_alloc(dev, sizeof(test2_t));
205 if (test2 == NULL) {
206 ddf_msg(LVL_ERROR, "Failed allocating soft state.");
207 return ENOMEM;
208 }
209
210 test2->dev = dev;
211
212 if (str_cmp(dev_name, "child") != 0) {
213 fid_t postpone = fibril_create(plug_unplug, test2);
214 if (postpone == 0) {
215 ddf_msg(LVL_ERROR, "fibril_create() failed.");
216 return ENOMEM;
217 }
218 fibril_add_ready(postpone);
219 } else {
220 (void) register_fun_verbose(dev, "child without available driver",
221 "ERROR", "non-existent.match.id", 10, &test2->fun_err);
222 }
223
224 return EOK;
225}
226
227static errno_t test2_dev_remove(ddf_dev_t *dev)
228{
229 test2_t *test2 = (test2_t *)ddf_dev_data_get(dev);
230 errno_t rc;
231
232 ddf_msg(LVL_DEBUG, "test2_dev_remove(%p)", dev);
233
234 if (test2->fun_a != NULL) {
235 rc = fun_remove(test2->fun_a, "a");
236 if (rc != EOK)
237 return rc;
238 }
239
240 if (test2->fun_err != NULL) {
241 rc = fun_remove(test2->fun_err, "ERROR");
242 if (rc != EOK)
243 return rc;
244 }
245
246 if (test2->child != NULL) {
247 rc = fun_remove(test2->child, "child");
248 if (rc != EOK)
249 return rc;
250 }
251
252 if (test2->test1 != NULL) {
253 rc = fun_remove(test2->test1, "test1");
254 if (rc != EOK)
255 return rc;
256 }
257
258 return EOK;
259}
260
261static errno_t test2_dev_gone(ddf_dev_t *dev)
262{
263 test2_t *test2 = (test2_t *)ddf_dev_data_get(dev);
264 errno_t rc;
265
266 ddf_msg(LVL_DEBUG, "test2_dev_gone(%p)", dev);
267
268 if (test2->fun_a != NULL) {
269 rc = fun_unbind(test2->fun_a, "a");
270 if (rc != EOK)
271 return rc;
272 }
273
274 if (test2->fun_err != NULL) {
275 rc = fun_unbind(test2->fun_err, "ERROR");
276 if (rc != EOK)
277 return rc;
278 }
279
280 if (test2->child != NULL) {
281 rc = fun_unbind(test2->child, "child");
282 if (rc != EOK)
283 return rc;
284 }
285
286 if (test2->test1 != NULL) {
287 rc = fun_unbind(test2->test1, "test1");
288 if (rc != EOK)
289 return rc;
290 }
291
292 return EOK;
293}
294
295
296static errno_t test2_fun_online(ddf_fun_t *fun)
297{
298 ddf_msg(LVL_DEBUG, "test2_fun_online()");
299 return ddf_fun_online(fun);
300}
301
302static errno_t test2_fun_offline(ddf_fun_t *fun)
303{
304 ddf_msg(LVL_DEBUG, "test2_fun_offline()");
305 return ddf_fun_offline(fun);
306}
307
308int main(int argc, char *argv[])
309{
310 printf(NAME ": HelenOS test2 virtual device driver\n");
311 ddf_log_init(NAME);
312 return ddf_driver_main(&test2_driver);
313}
314
315
Note: See TracBrowser for help on using the repository browser.