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 |
|
---|
43 | static errno_t test3_dev_add(ddf_dev_t *dev);
|
---|
44 | static errno_t test3_dev_remove(ddf_dev_t *dev);
|
---|
45 | static errno_t test3_fun_online(ddf_fun_t *fun);
|
---|
46 | static errno_t test3_fun_offline(ddf_fun_t *fun);
|
---|
47 |
|
---|
48 | static 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 |
|
---|
55 | static driver_t test3_driver = {
|
---|
56 | .name = NAME,
|
---|
57 | .driver_ops = &driver_ops
|
---|
58 | };
|
---|
59 |
|
---|
60 | typedef struct {
|
---|
61 | ddf_dev_t *dev;
|
---|
62 | ddf_fun_t *fun[NUM_FUNCS];
|
---|
63 | } test3_t;
|
---|
64 |
|
---|
65 | static errno_t 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 | errno_t rc;
|
---|
71 | char *fun_name = NULL;
|
---|
72 | bool bound = false;
|
---|
73 |
|
---|
74 | if (asprintf(&fun_name, "%s%zu", base_name, index) < 0) {
|
---|
75 | ddf_msg(LVL_ERROR, "Failed to format string: No memory");
|
---|
76 | rc = ENOMEM;
|
---|
77 | goto leave;
|
---|
78 | }
|
---|
79 |
|
---|
80 | fun = ddf_fun_create(parent, fun_exposed, fun_name);
|
---|
81 | if (fun == NULL) {
|
---|
82 | ddf_msg(LVL_ERROR, "Failed creating function %s", fun_name);
|
---|
83 | rc = ENOMEM;
|
---|
84 | goto leave;
|
---|
85 | }
|
---|
86 |
|
---|
87 | rc = ddf_fun_bind(fun);
|
---|
88 | if (rc != EOK) {
|
---|
89 | ddf_msg(LVL_ERROR, "Failed binding function %s: %s", fun_name,
|
---|
90 | str_error(rc));
|
---|
91 | goto leave;
|
---|
92 | }
|
---|
93 |
|
---|
94 | bound = true;
|
---|
95 |
|
---|
96 | rc = ddf_fun_add_to_category(fun, class_name);
|
---|
97 | if (rc != EOK) {
|
---|
98 | ddf_msg(LVL_ERROR, "Failed addning function %s to category %s: "
|
---|
99 | "%s", fun_name, class_name, str_error(rc));
|
---|
100 | goto leave;
|
---|
101 | }
|
---|
102 |
|
---|
103 | ddf_msg(LVL_NOTE, "Registered exposed function `%s'.", fun_name);
|
---|
104 |
|
---|
105 | leave:
|
---|
106 | free(fun_name);
|
---|
107 |
|
---|
108 | if (bound)
|
---|
109 | ddf_fun_unbind(fun);
|
---|
110 | if ((rc != EOK) && (fun != NULL)) {
|
---|
111 | ddf_fun_destroy(fun);
|
---|
112 | }
|
---|
113 |
|
---|
114 | *pfun = fun;
|
---|
115 | return rc;
|
---|
116 | }
|
---|
117 |
|
---|
118 | static errno_t fun_remove(ddf_fun_t *fun, const char *name)
|
---|
119 | {
|
---|
120 | errno_t rc;
|
---|
121 |
|
---|
122 | ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
|
---|
123 | rc = ddf_fun_offline(fun);
|
---|
124 | if (rc != EOK) {
|
---|
125 | ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
|
---|
126 | return rc;
|
---|
127 | }
|
---|
128 |
|
---|
129 | rc = ddf_fun_unbind(fun);
|
---|
130 | if (rc != EOK) {
|
---|
131 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
|
---|
132 | return rc;
|
---|
133 | }
|
---|
134 |
|
---|
135 | ddf_fun_destroy(fun);
|
---|
136 | return EOK;
|
---|
137 | }
|
---|
138 |
|
---|
139 | static errno_t test3_dev_add(ddf_dev_t *dev)
|
---|
140 | {
|
---|
141 | errno_t rc = EOK;
|
---|
142 | test3_t *test3;
|
---|
143 |
|
---|
144 | ddf_msg(LVL_DEBUG, "dev_add(name=\"%s\", handle=%d)",
|
---|
145 | ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
|
---|
146 |
|
---|
147 | test3 = ddf_dev_data_alloc(dev, sizeof(test3_t));
|
---|
148 | if (test3 == NULL) {
|
---|
149 | ddf_msg(LVL_ERROR, "Failed allocating soft state.");
|
---|
150 | return ENOMEM;
|
---|
151 | }
|
---|
152 |
|
---|
153 | size_t i;
|
---|
154 | for (i = 0; i < NUM_FUNCS; i++) {
|
---|
155 | rc = register_fun_and_add_to_category(dev,
|
---|
156 | "test3_", i, "test3", &test3->fun[i]);
|
---|
157 | if (rc != EOK) {
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 | static errno_t test3_dev_remove(ddf_dev_t *dev)
|
---|
166 | {
|
---|
167 | test3_t *test3 = (test3_t *)ddf_dev_data_get(dev);
|
---|
168 | char *fun_name;
|
---|
169 | errno_t rc;
|
---|
170 | size_t i;
|
---|
171 |
|
---|
172 | for (i = 0; i < NUM_FUNCS; i++) {
|
---|
173 | if (asprintf(&fun_name, "test3_%zu", i) < 0) {
|
---|
174 | ddf_msg(LVL_ERROR, "Failed to format string: No memory");
|
---|
175 | return ENOMEM;
|
---|
176 | }
|
---|
177 |
|
---|
178 | rc = fun_remove(test3->fun[i], fun_name);
|
---|
179 | if (rc != EOK)
|
---|
180 | return rc;
|
---|
181 |
|
---|
182 | free(fun_name);
|
---|
183 | }
|
---|
184 |
|
---|
185 | return EOK;
|
---|
186 | }
|
---|
187 |
|
---|
188 | static errno_t test3_fun_online(ddf_fun_t *fun)
|
---|
189 | {
|
---|
190 | ddf_msg(LVL_DEBUG, "test3_fun_online()");
|
---|
191 | return ddf_fun_online(fun);
|
---|
192 | }
|
---|
193 |
|
---|
194 | static errno_t test3_fun_offline(ddf_fun_t *fun)
|
---|
195 | {
|
---|
196 | ddf_msg(LVL_DEBUG, "test3_fun_offline()");
|
---|
197 | return ddf_fun_offline(fun);
|
---|
198 | }
|
---|
199 |
|
---|
200 | int main(int argc, char *argv[])
|
---|
201 | {
|
---|
202 | printf(NAME ": HelenOS test3 virtual device driver\n");
|
---|
203 | ddf_log_init(NAME);
|
---|
204 | return ddf_driver_main(&test3_driver);
|
---|
205 | }
|
---|