source: mainline/uspace/app/devctl/devctl.c@ 0511549

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

Add devctl list-drv subcommand to list known drivers.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Svoboda
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/** @addtogroup devctl
30 * @{
31 */
32/** @file Control device framework (devman server).
33 */
34
35#include <devman.h>
36#include <errno.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <str_error.h>
40#include <sys/typefmt.h>
41
42#define NAME "devctl"
43
44#define MAX_NAME_LENGTH 1024
45
46char name[MAX_NAME_LENGTH];
47char drv_name[MAX_NAME_LENGTH];
48
49static int fun_subtree_print(devman_handle_t funh, int lvl)
50{
51 devman_handle_t devh;
52 devman_handle_t *cfuns;
53 size_t count, i;
54 int rc;
55 int j;
56
57 for (j = 0; j < lvl; j++)
58 printf(" ");
59
60 rc = devman_fun_get_name(funh, name, MAX_NAME_LENGTH);
61 if (rc != EOK)
62 return ELIMIT;
63
64 if (name[0] == '\0')
65 str_cpy(name, MAX_NAME_LENGTH, "/");
66
67 rc = devman_fun_get_driver_name(funh, drv_name, MAX_NAME_LENGTH);
68 if (rc != EOK && rc != EINVAL)
69 return ELIMIT;
70
71 if (rc == EINVAL)
72 printf("%s\n", name);
73 else
74 printf("%s : %s\n", name, drv_name);
75
76 rc = devman_fun_get_child(funh, &devh);
77 if (rc == ENOENT)
78 return EOK;
79
80 if (rc != EOK) {
81 printf(NAME ": Failed getting child device for function "
82 "%s.\n", "xxx");
83 return rc;
84 }
85
86 rc = devman_dev_get_functions(devh, &cfuns, &count);
87 if (rc != EOK) {
88 printf(NAME ": Failed getting list of functions for "
89 "device %s.\n", "xxx");
90 return rc;
91 }
92
93 for (i = 0; i < count; i++)
94 fun_subtree_print(cfuns[i], lvl + 1);
95
96 free(cfuns);
97 return EOK;
98}
99
100static int fun_tree_print(void)
101{
102 devman_handle_t root_fun;
103 int rc;
104
105 rc = devman_fun_get_handle("/", &root_fun, 0);
106 if (rc != EOK) {
107 printf(NAME ": Error resolving root function.\n");
108 return EIO;
109 }
110
111 rc = fun_subtree_print(root_fun, 0);
112 if (rc != EOK)
113 return EIO;
114
115 return EOK;
116}
117
118static int fun_online(const char *path)
119{
120 devman_handle_t funh;
121 int rc;
122
123 rc = devman_fun_get_handle(path, &funh, 0);
124 if (rc != EOK) {
125 printf(NAME ": Error resolving device function '%s' (%s)\n",
126 path, str_error(rc));
127 return rc;
128 }
129
130 rc = devman_fun_online(funh);
131 if (rc != EOK) {
132 printf(NAME ": Failed to online function '%s'.\n", path);
133 return rc;
134 }
135
136 return EOK;
137}
138
139static int fun_offline(const char *path)
140{
141 devman_handle_t funh;
142 int rc;
143
144 rc = devman_fun_get_handle(path, &funh, 0);
145 if (rc != EOK) {
146 printf(NAME ": Error resolving device function '%s' (%s)\n",
147 path, str_error(rc));
148 return rc;
149 }
150
151 rc = devman_fun_offline(funh);
152 if (rc != EOK) {
153 printf(NAME ": Failed to offline function '%s' (%s)\n", path,
154 str_error(rc));
155 return rc;
156 }
157
158 return EOK;
159}
160
161static int drv_list(void)
162{
163 devman_handle_t *drvs;
164 size_t ndrvs;
165 size_t i;
166 int rc;
167
168 rc = devman_get_drivers(&drvs, &ndrvs);
169 if (rc != EOK)
170 return rc;
171
172 printf("Got %d handles\n", ndrvs);
173 for (i = 0; i < ndrvs; i++) {
174 rc = devman_driver_get_name(drvs[i], drv_name, MAX_NAME_LENGTH);
175 if (rc != EOK)
176 continue;
177 printf("%3d %s\n", (int)drvs[i], drv_name);
178 }
179 free(drvs);
180
181 return EOK;
182}
183
184static void print_syntax(void)
185{
186 printf("syntax:\n");
187 printf("\tdevctl\n");
188 printf("\tdevctl online <function>]\n");
189 printf("\tdevctl offline <function>]\n");
190 printf("\tdevctl list-drv\n");
191}
192
193int main(int argc, char *argv[])
194{
195 int rc;
196
197 if (argc == 1) {
198 rc = fun_tree_print();
199 if (rc != EOK)
200 return 2;
201 } else if (str_cmp(argv[1], "online") == 0) {
202 if (argc < 3) {
203 printf(NAME ": Argument missing.\n");
204 print_syntax();
205 return 1;
206 }
207
208 rc = fun_online(argv[2]);
209 if (rc != EOK) {
210 return 2;
211 }
212 } else if (str_cmp(argv[1], "offline") == 0) {
213 if (argc < 3) {
214 printf(NAME ": Argument missing.\n");
215 print_syntax();
216 return 1;
217 }
218
219 rc = fun_offline(argv[2]);
220 if (rc != EOK) {
221 return 2;
222 }
223 } else if (str_cmp(argv[1], "list-drv") == 0) {
224 rc = drv_list();
225 if (rc != EOK)
226 return 2;
227 } else {
228 printf(NAME ": Invalid argument '%s'.\n", argv[1]);
229 print_syntax();
230 return 1;
231 }
232
233 return 0;
234}
235
236/** @}
237 */
Note: See TracBrowser for help on using the repository browser.