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 | /**
|
---|
30 | * @defgroup rootvirt Root device driver for virtual devices.
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <str_error.h>
|
---|
41 | #include <ddf/driver.h>
|
---|
42 |
|
---|
43 | #define NAME "rootvirt"
|
---|
44 |
|
---|
45 | /** Virtual function entry */
|
---|
46 | typedef struct {
|
---|
47 | /** Function name */
|
---|
48 | const char *name;
|
---|
49 | /** Function match ID */
|
---|
50 | const char *match_id;
|
---|
51 | } virtual_function_t;
|
---|
52 |
|
---|
53 | /** List of existing virtual functions */
|
---|
54 | virtual_function_t virtual_functions[] = {
|
---|
55 | #include "devices.def"
|
---|
56 | /* Terminating item */
|
---|
57 | {
|
---|
58 | .name = NULL,
|
---|
59 | .match_id = NULL
|
---|
60 | }
|
---|
61 | };
|
---|
62 |
|
---|
63 | static int rootvirt_add_device(ddf_dev_t *dev);
|
---|
64 |
|
---|
65 | static driver_ops_t rootvirt_ops = {
|
---|
66 | .add_device = &rootvirt_add_device
|
---|
67 | };
|
---|
68 |
|
---|
69 | static driver_t rootvirt_driver = {
|
---|
70 | .name = NAME,
|
---|
71 | .driver_ops = &rootvirt_ops
|
---|
72 | };
|
---|
73 |
|
---|
74 | /** Add function to the virtual device.
|
---|
75 | *
|
---|
76 | * @param vdev The virtual device
|
---|
77 | * @param vfun Virtual function description
|
---|
78 | * @return EOK on success or negative error code.
|
---|
79 | */
|
---|
80 | static int rootvirt_add_fun(ddf_dev_t *vdev, virtual_function_t *vfun)
|
---|
81 | {
|
---|
82 | ddf_fun_t *fun;
|
---|
83 | int rc;
|
---|
84 |
|
---|
85 | printf(NAME ": registering function `%s' (match \"%s\")\n",
|
---|
86 | vfun->name, vfun->match_id);
|
---|
87 |
|
---|
88 | fun = ddf_fun_create(vdev, fun_inner, vfun->name);
|
---|
89 | if (fun == NULL) {
|
---|
90 | printf(NAME ": error creating function %s\n", vfun->name);
|
---|
91 | return ENOMEM;
|
---|
92 | }
|
---|
93 |
|
---|
94 | rc = ddf_fun_add_match_id(fun, vfun->match_id, 10);
|
---|
95 | if (rc != EOK) {
|
---|
96 | printf(NAME ": error adding match IDs to function %s\n",
|
---|
97 | vfun->name);
|
---|
98 | ddf_fun_destroy(fun);
|
---|
99 | return rc;
|
---|
100 | }
|
---|
101 |
|
---|
102 | rc = ddf_fun_bind(fun);
|
---|
103 | if (rc != EOK) {
|
---|
104 | printf(NAME ": error binding function %s: %s\n", vfun->name,
|
---|
105 | str_error(rc));
|
---|
106 | ddf_fun_destroy(fun);
|
---|
107 | return rc;
|
---|
108 | }
|
---|
109 |
|
---|
110 | printf(NAME ": registered child device `%s'\n", vfun->name);
|
---|
111 | return EOK;
|
---|
112 | }
|
---|
113 |
|
---|
114 | static int rootvirt_add_device(ddf_dev_t *dev)
|
---|
115 | {
|
---|
116 | static int instances = 0;
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Allow only single instance of root virtual device.
|
---|
120 | */
|
---|
121 | instances++;
|
---|
122 | if (instances > 1) {
|
---|
123 | return ELIMIT;
|
---|
124 | }
|
---|
125 |
|
---|
126 | printf(NAME ": add_device(handle=%d)\n", (int)dev->handle);
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * Go through all virtual functions and try to add them.
|
---|
130 | * We silently ignore failures.
|
---|
131 | */
|
---|
132 | virtual_function_t *vfun = virtual_functions;
|
---|
133 | while (vfun->name != NULL) {
|
---|
134 | (void) rootvirt_add_fun(dev, vfun);
|
---|
135 | vfun++;
|
---|
136 | }
|
---|
137 |
|
---|
138 | return EOK;
|
---|
139 | }
|
---|
140 |
|
---|
141 | int main(int argc, char *argv[])
|
---|
142 | {
|
---|
143 | printf(NAME ": HelenOS virtual devices root driver\n");
|
---|
144 | return ddf_driver_main(&rootvirt_driver);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * @}
|
---|
149 | */
|
---|
150 |
|
---|