source: mainline/uspace/drv/test2/test2.c@ 2bf8f8c

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

Split driver.h into ddf/driver.h and ddf/interrupt.h.

  • Property mode set to 100644
File size: 4.1 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_error.h>
37#include <ddf/driver.h>
38
39#define NAME "test2"
40
41static int test2_add_device(ddf_dev_t *dev);
42
43static driver_ops_t driver_ops = {
44 .add_device = &test2_add_device
45};
46
47static driver_t test2_driver = {
48 .name = NAME,
49 .driver_ops = &driver_ops
50};
51
52/** Register child and inform user about it.
53 *
54 * @param parent Parent device.
55 * @param message Message for the user.
56 * @param name Device name.
57 * @param match_id Device match id.
58 * @param score Device match score.
59 */
60static int register_fun_verbose(ddf_dev_t *parent, const char *message,
61 const char *name, const char *match_id, int match_score)
62{
63 ddf_fun_t *fun;
64 int rc;
65
66 printf(NAME ": registering function `%s': %s.\n", name, message);
67
68 fun = ddf_fun_create(parent, fun_inner, name);
69 if (fun == NULL) {
70 printf(NAME ": error creating function %s\n", name);
71 return ENOMEM;
72 }
73
74 rc = ddf_fun_add_match_id(fun, match_id, match_score);
75 if (rc != EOK) {
76 printf(NAME ": error adding match IDs to function %s\n", name);
77 ddf_fun_destroy(fun);
78 return rc;
79 }
80
81 rc = ddf_fun_bind(fun);
82 if (rc != EOK) {
83 printf(NAME ": error binding function %s: %s\n", name,
84 str_error(rc));
85 ddf_fun_destroy(fun);
86 return rc;
87 }
88
89 printf(NAME ": registered child device `%s'\n", name);
90 return EOK;
91}
92
93/** Add child devices after some sleep.
94 *
95 * @param arg Parent device structure (ddf_dev_t *).
96 * @return Always EOK.
97 */
98static int postponed_birth(void *arg)
99{
100 ddf_dev_t *dev = (ddf_dev_t *) arg;
101 ddf_fun_t *fun_a;
102 int rc;
103
104 async_usleep(1000);
105
106 (void) register_fun_verbose(dev, "child driven by the same task",
107 "child", "virtual&test2", 10);
108 (void) register_fun_verbose(dev, "child driven by test1",
109 "test1", "virtual&test1", 10);
110
111 fun_a = ddf_fun_create(dev, fun_exposed, "a");
112 if (fun_a == NULL) {
113 printf(NAME ": error creating function 'a'.\n");
114 return ENOMEM;
115 }
116
117 rc = ddf_fun_bind(fun_a);
118 if (rc != EOK) {
119 printf(NAME ": error binding function 'a'.\n");
120 return rc;
121 }
122
123 ddf_fun_add_to_class(fun_a, "virtual");
124
125 return EOK;
126}
127
128static int test2_add_device(ddf_dev_t *dev)
129{
130 printf(NAME ": test2_add_device(name=\"%s\", handle=%d)\n",
131 dev->name, (int) dev->handle);
132
133 if (str_cmp(dev->name, "child") != 0) {
134 fid_t postpone = fibril_create(postponed_birth, dev);
135 if (postpone == 0) {
136 printf(NAME ": fibril_create() error\n");
137 return ENOMEM;
138 }
139 fibril_add_ready(postpone);
140 } else {
141 (void) register_fun_verbose(dev, "child without available driver",
142 "ERROR", "non-existent.match.id", 10);
143 }
144
145 return EOK;
146}
147
148int main(int argc, char *argv[])
149{
150 printf(NAME ": HelenOS test2 virtual device driver\n");
151 return ddf_driver_main(&test2_driver);
152}
153
154
Note: See TracBrowser for help on using the repository browser.