source: mainline/uspace/drv/test/test1/test1.c@ d15797d

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

Remove unnecessary references to devman.

  • Property mode set to 100644
File size: 7.6 KB
RevLine 
[1b2981aa]1/*
2 * Copyright (c) 2010 Vojtech Horky
[1a5b252]3 * Copyright (c) 2011 Jiri Svoboda
[1b2981aa]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>
[af6b5157]37#include <ddf/driver.h>
[fc51296]38#include <ddf/log.h>
[af6b5157]39
[424558a]40#include "test1.h"
[1b2981aa]41
[0c0f823b]42static int test1_dev_add(ddf_dev_t *dev);
[1a5b252]43static int test1_dev_remove(ddf_dev_t *dev);
[80a96d2]44static int test1_dev_gone(ddf_dev_t *dev);
[1a5b252]45static int test1_fun_online(ddf_fun_t *fun);
46static int test1_fun_offline(ddf_fun_t *fun);
[1b2981aa]47
48static driver_ops_t driver_ops = {
[0c0f823b]49 .dev_add = &test1_dev_add,
[1a5b252]50 .dev_remove = &test1_dev_remove,
[80a96d2]51 .dev_gone = &test1_dev_gone,
[1a5b252]52 .fun_online = &test1_fun_online,
53 .fun_offline = &test1_fun_offline
[1b2981aa]54};
55
[bab6388]56static driver_t test1_driver = {
[1b2981aa]57 .name = NAME,
58 .driver_ops = &driver_ops
59};
60
[1a5b252]61typedef struct {
62 ddf_fun_t *fun_a;
63 ddf_fun_t *clone;
64 ddf_fun_t *child;
65} test1_t;
66
[703d19c]67/** Register child and inform user about it.
68 *
69 * @param parent Parent device.
70 * @param message Message for the user.
71 * @param name Device name.
72 * @param match_id Device match id.
73 * @param score Device match score.
74 */
[83a2f43]75static int register_fun_verbose(ddf_dev_t *parent, const char *message,
[3fddb55]76 const char *name, const char *match_id, int match_score,
[1a5b252]77 int expected_rc, ddf_fun_t **pfun)
[703d19c]78{
[3fddb55]79 ddf_fun_t *fun = NULL;
[cd0684d]80 int rc;
81
[ebcb05a]82 ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
[cd0684d]83
84 fun = ddf_fun_create(parent, fun_inner, name);
85 if (fun == NULL) {
[ebcb05a]86 ddf_msg(LVL_ERROR, "Failed creating function %s", name);
[3fddb55]87 rc = ENOMEM;
88 goto leave;
[cd0684d]89 }
[703d19c]90
[ef9460b]91 rc = ddf_fun_add_match_id(fun, match_id, match_score);
[cd0684d]92 if (rc != EOK) {
[ebcb05a]93 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
[fc51296]94 name);
[3fddb55]95 goto leave;
[cd0684d]96 }
[703d19c]97
[cd0684d]98 rc = ddf_fun_bind(fun);
99 if (rc != EOK) {
[ebcb05a]100 ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
[cd0684d]101 str_error(rc));
[3fddb55]102 goto leave;
[703d19c]103 }
[cd0684d]104
[ebcb05a]105 ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
[3fddb55]106 rc = EOK;
107
108leave:
109 if (rc != expected_rc) {
110 fprintf(stderr,
[bdbb6f6]111 NAME ": Unexpected error registering function `%s'.\n"
[3fddb55]112 NAME ": Expected \"%s\" but got \"%s\".\n",
113 name, str_error(expected_rc), str_error(rc));
114 }
115
116 if ((rc != EOK) && (fun != NULL)) {
117 ddf_fun_destroy(fun);
118 }
119
[1a5b252]120 if (pfun != NULL)
121 *pfun = fun;
122
[3fddb55]123 return rc;
[703d19c]124}
125
[1b2981aa]126/** Callback when new device is passed to this driver.
127 * This function is the body of the test: it shall register new child
128 * (named `clone') that shall be driven by the same task. When the clone
129 * is added, it registers another child (named `child') that is also driven
130 * by this task. The conditions ensure that we do not recurse indefinitely.
131 * When successful, the device tree shall contain following fragment:
132 *
133 * /virtual/test1
134 * /virtual/test1/clone
135 * /virtual/test1/clone/child
136 *
[d15797d]137 * and the DDF shall not deadlock.
[1b2981aa]138 *
139 *
140 * @param dev New device.
141 * @return Error code reporting success of the operation.
142 */
[0c0f823b]143static int test1_dev_add(ddf_dev_t *dev)
[1b2981aa]144{
[83a2f43]145 ddf_fun_t *fun_a;
[1a5b252]146 test1_t *test1;
[56fd7cf]147 const char *dev_name;
[97a62fe]148 int rc;
[8b1e15ac]149
[56fd7cf]150 dev_name = ddf_dev_get_name(dev);
[0c0f823b]151 ddf_msg(LVL_DEBUG, "dev_add(name=\"%s\", handle=%d)",
[56fd7cf]152 dev_name, (int) ddf_dev_get_handle(dev));
[1b2981aa]153
[c5be39b]154 test1 = ddf_dev_data_alloc(dev, sizeof(test1_t));
[1a5b252]155 if (test1 == NULL) {
[c5be39b]156 ddf_msg(LVL_ERROR, "Failed allocating soft state.\n");
[56fd7cf]157 rc = ENOMEM;
158 goto error;
[1a5b252]159 }
160
[97a62fe]161 fun_a = ddf_fun_create(dev, fun_exposed, "a");
162 if (fun_a == NULL) {
[ebcb05a]163 ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
[56fd7cf]164 rc = ENOMEM;
165 goto error;
[97a62fe]166 }
[8b1e15ac]167
[c5be39b]168 test1->fun_a = fun_a;
169
[97a62fe]170 rc = ddf_fun_bind(fun_a);
171 if (rc != EOK) {
[ebcb05a]172 ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
[1a5b252]173 ddf_fun_destroy(fun_a);
[56fd7cf]174 goto error;
[97a62fe]175 }
[8b1e15ac]176
[1dc4a5e]177 ddf_fun_add_to_category(fun_a, "virtual");
[703d19c]178
[56fd7cf]179 if (str_cmp(dev_name, "null") == 0) {
180 ddf_fun_set_ops(fun_a, &char_device_ops);
[1dc4a5e]181 ddf_fun_add_to_category(fun_a, "virt-null");
[56fd7cf]182 } else if (str_cmp(dev_name, "test1") == 0) {
[3fddb55]183 (void) register_fun_verbose(dev,
184 "cloning myself ;-)", "clone",
[1a5b252]185 "virtual&test1", 10, EOK, &test1->clone);
[3fddb55]186 (void) register_fun_verbose(dev,
187 "cloning myself twice ;-)", "clone",
[8a637a4]188 "virtual&test1", 10, EEXIST, NULL);
[56fd7cf]189 } else if (str_cmp(dev_name, "clone") == 0) {
[3fddb55]190 (void) register_fun_verbose(dev,
191 "run by the same task", "child",
[1a5b252]192 "virtual&test1&child", 10, EOK, &test1->child);
[1b2981aa]193 }
194
[56fd7cf]195 ddf_msg(LVL_DEBUG, "Device `%s' accepted.", dev_name);
[1a5b252]196 return EOK;
[56fd7cf]197error:
198 return rc;
[1a5b252]199}
200
201static int fun_remove(ddf_fun_t *fun, const char *name)
202{
203 int rc;
204
[deac215e]205 ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
[1a5b252]206 rc = ddf_fun_offline(fun);
207 if (rc != EOK) {
208 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
209 return rc;
210 }
211
212 rc = ddf_fun_unbind(fun);
213 if (rc != EOK) {
[7fff38c1]214 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
[1a5b252]215 return rc;
216 }
217
218 ddf_fun_destroy(fun);
[1b2981aa]219 return EOK;
220}
221
[80a96d2]222static int fun_unbind(ddf_fun_t *fun, const char *name)
223{
224 int rc;
225
226 ddf_msg(LVL_DEBUG, "fun_unbind(%p, '%s')", fun, name);
227 rc = ddf_fun_unbind(fun);
228 if (rc != EOK) {
229 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
230 return rc;
231 }
232
233 ddf_fun_destroy(fun);
234 return EOK;
235}
236
[1a5b252]237static int test1_dev_remove(ddf_dev_t *dev)
238{
[56fd7cf]239 test1_t *test1 = (test1_t *)ddf_dev_data_get(dev);
[1a5b252]240 int rc;
241
242 ddf_msg(LVL_DEBUG, "test1_dev_remove(%p)", dev);
243
244 if (test1->fun_a != NULL) {
245 rc = fun_remove(test1->fun_a, "a");
246 if (rc != EOK)
247 return rc;
248 }
249
250 if (test1->clone != NULL) {
251 rc = fun_remove(test1->clone, "clone");
252 if (rc != EOK)
253 return rc;
254 }
255
256 if (test1->child != NULL) {
257 rc = fun_remove(test1->child, "child");
258 if (rc != EOK)
259 return rc;
260 }
261
262 return EOK;
263}
264
[80a96d2]265static int test1_dev_gone(ddf_dev_t *dev)
266{
[56fd7cf]267 test1_t *test1 = (test1_t *)ddf_dev_data_get(dev);
[80a96d2]268 int rc;
269
270 ddf_msg(LVL_DEBUG, "test1_dev_remove(%p)", dev);
271
272 if (test1->fun_a != NULL) {
273 rc = fun_unbind(test1->fun_a, "a");
274 if (rc != EOK)
275 return rc;
276 }
277
278 if (test1->clone != NULL) {
279 rc = fun_unbind(test1->clone, "clone");
280 if (rc != EOK)
281 return rc;
282 }
283
284 if (test1->child != NULL) {
285 rc = fun_unbind(test1->child, "child");
286 if (rc != EOK)
287 return rc;
288 }
289
290 return EOK;
291}
292
[1a5b252]293static int test1_fun_online(ddf_fun_t *fun)
294{
295 ddf_msg(LVL_DEBUG, "test1_fun_online()");
296 return ddf_fun_online(fun);
297}
298
299static int test1_fun_offline(ddf_fun_t *fun)
300{
301 ddf_msg(LVL_DEBUG, "test1_fun_offline()");
302 return ddf_fun_offline(fun);
303}
304
[1b2981aa]305int main(int argc, char *argv[])
306{
307 printf(NAME ": HelenOS test1 virtual device driver\n");
[267f235]308 ddf_log_init(NAME);
[83a2f43]309 return ddf_driver_main(&test1_driver);
[1b2981aa]310}
311
Note: See TracBrowser for help on using the repository browser.