source: mainline/uspace/drv/test2/test2.c@ d3594362

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

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

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[c245f16e]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>
[af6b5157]33#include <async.h>
[c245f16e]34#include <stdio.h>
35#include <errno.h>
36#include <str_error.h>
[af6b5157]37#include <ddf/driver.h>
[c245f16e]38
39#define NAME "test2"
40
[83a2f43]41static int test2_add_device(ddf_dev_t *dev);
[c245f16e]42
43static driver_ops_t driver_ops = {
[bab6388]44 .add_device = &test2_add_device
[c245f16e]45};
46
[bab6388]47static driver_t test2_driver = {
[c245f16e]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 */
[83a2f43]60static int register_fun_verbose(ddf_dev_t *parent, const char *message,
[c245f16e]61 const char *name, const char *match_id, int match_score)
62{
[83a2f43]63 ddf_fun_t *fun;
[cd0684d]64 int rc;
[c245f16e]65
[cd0684d]66 printf(NAME ": registering function `%s': %s.\n", name, message);
[c245f16e]67
[cd0684d]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;
[c245f16e]79 }
[cd0684d]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;
[c245f16e]91}
92
93/** Add child devices after some sleep.
94 *
[83a2f43]95 * @param arg Parent device structure (ddf_dev_t *).
[c245f16e]96 * @return Always EOK.
97 */
98static int postponed_birth(void *arg)
99{
[83a2f43]100 ddf_dev_t *dev = (ddf_dev_t *) arg;
101 ddf_fun_t *fun_a;
[97a62fe]102 int rc;
[c245f16e]103
104 async_usleep(1000);
105
[cd0684d]106 (void) register_fun_verbose(dev, "child driven by the same task",
[c245f16e]107 "child", "virtual&test2", 10);
[cd0684d]108 (void) register_fun_verbose(dev, "child driven by test1",
[c245f16e]109 "test1", "virtual&test1", 10);
110
[97a62fe]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 }
[8b1e15ac]116
[97a62fe]117 rc = ddf_fun_bind(fun_a);
118 if (rc != EOK) {
119 printf(NAME ": error binding function 'a'.\n");
120 return rc;
121 }
[8b1e15ac]122
[83a2f43]123 ddf_fun_add_to_class(fun_a, "virtual");
[c245f16e]124
125 return EOK;
126}
127
[83a2f43]128static int test2_add_device(ddf_dev_t *dev)
[c245f16e]129{
[bab6388]130 printf(NAME ": test2_add_device(name=\"%s\", handle=%d)\n",
[c245f16e]131 dev->name, (int) dev->handle);
132
[8b1e15ac]133 if (str_cmp(dev->name, "child") != 0) {
[c245f16e]134 fid_t postpone = fibril_create(postponed_birth, dev);
[86d7bfa]135 if (postpone == 0) {
136 printf(NAME ": fibril_create() error\n");
137 return ENOMEM;
138 }
[c245f16e]139 fibril_add_ready(postpone);
140 } else {
[cd0684d]141 (void) register_fun_verbose(dev, "child without available driver",
[c245f16e]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");
[83a2f43]151 return ddf_driver_main(&test2_driver);
[c245f16e]152}
153
154
Note: See TracBrowser for help on using the repository browser.