40
40
#define NAME "test2"
42
42
static int test2_add_device(ddf_dev_t *dev);
43
static int test2_dev_remove(ddf_dev_t *dev);
44
static int test2_dev_gone(ddf_dev_t *dev);
45
static int test2_fun_online(ddf_fun_t *fun);
46
static int test2_fun_offline(ddf_fun_t *fun);
44
48
static driver_ops_t driver_ops = {
45
.add_device = &test2_add_device
49
.add_device = &test2_add_device,
50
.dev_remove = &test2_dev_remove,
51
.dev_gone = &test2_dev_gone,
52
.fun_online = &test2_fun_online,
53
.fun_offline = &test2_fun_offline
48
56
static driver_t test2_driver = {
59
77
* @param score Device match score.
61
79
static int register_fun_verbose(ddf_dev_t *parent, const char *message,
62
const char *name, const char *match_id, int match_score)
80
const char *name, const char *match_id, int match_score, ddf_fun_t **pfun)
91
111
ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
95
/** Add child devices after some sleep.
115
/** Simulate plugging and surprise unplugging.
97
117
* @param arg Parent device structure (ddf_dev_t *).
98
118
* @return Always EOK.
100
static int postponed_birth(void *arg)
120
static int plug_unplug(void *arg)
102
ddf_dev_t *dev = (ddf_dev_t *) arg;
122
test2_t *test2 = (test2_t *) arg;
103
123
ddf_fun_t *fun_a;
106
126
async_usleep(1000);
108
(void) register_fun_verbose(dev, "child driven by the same task",
109
"child", "virtual&test2", 10);
110
(void) register_fun_verbose(dev, "child driven by test1",
111
"test1", "virtual&test1", 10);
128
(void) register_fun_verbose(test2->dev, "child driven by the same task",
129
"child", "virtual&test2", 10, &test2->child);
130
(void) register_fun_verbose(test2->dev, "child driven by test1",
131
"test1", "virtual&test1", 10, &test2->test1);
113
fun_a = ddf_fun_create(dev, fun_exposed, "a");
133
fun_a = ddf_fun_create(test2->dev, fun_exposed, "a");
114
134
if (fun_a == NULL) {
115
135
ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
125
ddf_fun_add_to_class(fun_a, "virtual");
145
ddf_fun_add_to_category(fun_a, "virtual");
146
test2->fun_a = fun_a;
148
async_usleep(10000000);
150
ddf_msg(LVL_NOTE, "Unbinding function test1.");
151
ddf_fun_unbind(test2->test1);
152
async_usleep(1000000);
153
ddf_msg(LVL_NOTE, "Unbinding function child.");
154
ddf_fun_unbind(test2->child);
159
static int fun_remove(ddf_fun_t *fun, const char *name)
163
ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
164
rc = ddf_fun_offline(fun);
166
ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
170
rc = ddf_fun_unbind(fun);
172
ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
176
ddf_fun_destroy(fun);
180
static int fun_unbind(ddf_fun_t *fun, const char *name)
184
ddf_msg(LVL_DEBUG, "fun_unbind(%p, '%s')", fun, name);
185
rc = ddf_fun_unbind(fun);
187
ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
191
ddf_fun_destroy(fun);
130
195
static int test2_add_device(ddf_dev_t *dev)
132
199
ddf_msg(LVL_DEBUG, "test2_add_device(name=\"%s\", handle=%d)",
133
200
dev->name, (int) dev->handle);
202
test2 = ddf_dev_data_alloc(dev, sizeof(test2_t));
204
ddf_msg(LVL_ERROR, "Failed allocating soft state.");
135
210
if (str_cmp(dev->name, "child") != 0) {
136
fid_t postpone = fibril_create(postponed_birth, dev);
211
fid_t postpone = fibril_create(plug_unplug, test2);
137
212
if (postpone == 0) {
138
213
ddf_msg(LVL_ERROR, "fibril_create() failed.");
141
216
fibril_add_ready(postpone);
143
218
(void) register_fun_verbose(dev, "child without available driver",
144
"ERROR", "non-existent.match.id", 10);
219
"ERROR", "non-existent.match.id", 10, &test2->fun_err);
225
static int test2_dev_remove(ddf_dev_t *dev)
227
test2_t *test2 = (test2_t *)dev->driver_data;
230
ddf_msg(LVL_DEBUG, "test2_dev_remove(%p)", dev);
232
if (test2->fun_a != NULL) {
233
rc = fun_remove(test2->fun_a, "a");
238
if (test2->fun_err != NULL) {
239
rc = fun_remove(test2->fun_err, "ERROR");
244
if (test2->child != NULL) {
245
rc = fun_remove(test2->child, "child");
250
if (test2->test1 != NULL) {
251
rc = fun_remove(test2->test1, "test1");
259
static int test2_dev_gone(ddf_dev_t *dev)
261
test2_t *test2 = (test2_t *)dev->driver_data;
264
ddf_msg(LVL_DEBUG, "test2_dev_gone(%p)", dev);
266
if (test2->fun_a != NULL) {
267
rc = fun_unbind(test2->fun_a, "a");
272
if (test2->fun_err != NULL) {
273
rc = fun_unbind(test2->fun_err, "ERROR");
278
if (test2->child != NULL) {
279
rc = fun_unbind(test2->child, "child");
284
if (test2->test1 != NULL) {
285
rc = fun_unbind(test2->test1, "test1");
294
static int test2_fun_online(ddf_fun_t *fun)
296
ddf_msg(LVL_DEBUG, "test2_fun_online()");
297
return ddf_fun_online(fun);
300
static int test2_fun_offline(ddf_fun_t *fun)
302
ddf_msg(LVL_DEBUG, "test2_fun_offline()");
303
return ddf_fun_offline(fun);
150
306
int main(int argc, char *argv[])
152
308
printf(NAME ": HelenOS test2 virtual device driver\n");
153
ddf_log_init(NAME, LVL_ERROR);
309
ddf_log_init(NAME, LVL_NOTE);
154
310
return ddf_driver_main(&test2_driver);