source: mainline/uspace/drv/test/test3/test3.c@ 61e27e80

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 61e27e80 was 267f235, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

log_init() needs only one parameter

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2011 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 <stdio.h>
34#include <errno.h>
35#include <str_error.h>
36#include <ddf/driver.h>
37#include <ddf/log.h>
38
39#define NAME "test3"
40
41#define NUM_FUNCS 20
42
43static int test3_dev_add(ddf_dev_t *dev);
44static int test3_dev_remove(ddf_dev_t *dev);
45static int test3_fun_online(ddf_fun_t *fun);
46static int test3_fun_offline(ddf_fun_t *fun);
47
48static driver_ops_t driver_ops = {
49 .dev_add = &test3_dev_add,
50 .dev_remove = &test3_dev_remove,
51 .fun_online = &test3_fun_online,
52 .fun_offline = &test3_fun_offline,
53};
54
55static driver_t test3_driver = {
56 .name = NAME,
57 .driver_ops = &driver_ops
58};
59
60typedef struct {
61 ddf_dev_t *dev;
62 ddf_fun_t *fun[NUM_FUNCS];
63} test3_t;
64
65static int register_fun_and_add_to_category(ddf_dev_t *parent,
66 const char *base_name, size_t index, const char *class_name,
67 ddf_fun_t **pfun)
68{
69 ddf_fun_t *fun = NULL;
70 int rc;
71 char *fun_name = NULL;
72
73 rc = asprintf(&fun_name, "%s%zu", base_name, index);
74 if (rc < 0) {
75 ddf_msg(LVL_ERROR, "Failed to format string: %s", str_error(rc));
76 goto leave;
77 }
78
79 fun = ddf_fun_create(parent, fun_exposed, fun_name);
80 if (fun == NULL) {
81 ddf_msg(LVL_ERROR, "Failed creating function %s", fun_name);
82 rc = ENOMEM;
83 goto leave;
84 }
85
86 rc = ddf_fun_bind(fun);
87 if (rc != EOK) {
88 ddf_msg(LVL_ERROR, "Failed binding function %s: %s", fun_name,
89 str_error(rc));
90 goto leave;
91 }
92
93 ddf_fun_add_to_category(fun, class_name);
94
95 ddf_msg(LVL_NOTE, "Registered exposed function `%s'.", fun_name);
96
97leave:
98 free(fun_name);
99
100 if ((rc != EOK) && (fun != NULL)) {
101 ddf_fun_destroy(fun);
102 }
103
104 *pfun = fun;
105 return rc;
106}
107
108static int fun_remove(ddf_fun_t *fun, const char *name)
109{
110 int rc;
111
112 ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
113 rc = ddf_fun_offline(fun);
114 if (rc != EOK) {
115 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
116 return rc;
117 }
118
119 rc = ddf_fun_unbind(fun);
120 if (rc != EOK) {
121 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
122 return rc;
123 }
124
125 ddf_fun_destroy(fun);
126 return EOK;
127}
128
129static int test3_dev_add(ddf_dev_t *dev)
130{
131 int rc = EOK;
132 test3_t *test3;
133
134 ddf_msg(LVL_DEBUG, "dev_add(name=\"%s\", handle=%d)",
135 ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
136
137 test3 = ddf_dev_data_alloc(dev, sizeof(test3_t));
138 if (test3 == NULL) {
139 ddf_msg(LVL_ERROR, "Failed allocating soft state.");
140 return ENOMEM;
141 }
142
143 size_t i;
144 for (i = 0; i < NUM_FUNCS; i++) {
145 rc = register_fun_and_add_to_category(dev,
146 "test3_", i, "test3", &test3->fun[i]);
147 if (rc != EOK) {
148 break;
149 }
150 }
151
152 return rc;
153}
154
155static int test3_dev_remove(ddf_dev_t *dev)
156{
157 test3_t *test3 = (test3_t *)ddf_dev_data_get(dev);
158 char *fun_name;
159 int rc;
160 size_t i;
161
162 for (i = 0; i < NUM_FUNCS; i++) {
163 rc = asprintf(&fun_name, "test3_%zu", i);
164 if (rc < 0) {
165 ddf_msg(LVL_ERROR, "Failed to format string: %s", str_error(rc));
166 return ENOMEM;
167 }
168
169 rc = fun_remove(test3->fun[i], fun_name);
170 if (rc != EOK)
171 return rc;
172
173 free(fun_name);
174 }
175
176 return EOK;
177}
178
179static int test3_fun_online(ddf_fun_t *fun)
180{
181 ddf_msg(LVL_DEBUG, "test3_fun_online()");
182 return ddf_fun_online(fun);
183}
184
185static int test3_fun_offline(ddf_fun_t *fun)
186{
187 ddf_msg(LVL_DEBUG, "test3_fun_offline()");
188 return ddf_fun_offline(fun);
189}
190
191
192int main(int argc, char *argv[])
193{
194 printf(NAME ": HelenOS test3 virtual device driver\n");
195 ddf_log_init(NAME);
196 return ddf_driver_main(&test3_driver);
197}
198
Note: See TracBrowser for help on using the repository browser.