1 /* $NetBSD: drvctl.c,v 1.3 2021/06/21 03:04:27 christos Exp $ */
2 
3 /*
4  * Copyright (c) 2010 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <stdbool.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 #include "drvctl.h"
35 
36 static int
drvctl_list(int fd,const char * name,struct devlistargs * laa)37 drvctl_list(int fd, const char *name, struct devlistargs *laa)
38 {
39           size_t children;
40 
41           memset(laa, 0, sizeof(*laa));
42           strlcpy(laa->l_devname, name, sizeof(laa->l_devname));
43           if (ioctl(fd, DRVLISTDEV, laa) == -1)
44                     return -1;
45           children = laa->l_children;
46           laa->l_childname = malloc(children * sizeof(laa->l_childname[0]));
47           if (laa->l_childname == NULL)
48                     return -1;
49           if (ioctl(fd, DRVLISTDEV, laa) == -1)
50                     return -1;
51           return 0;
52 }
53 
54 static bool
drvctl_get_properties(int fd,const char * devnode,prop_dictionary_t * props)55 drvctl_get_properties(int fd, const char *devnode, prop_dictionary_t *props)
56 {
57           prop_dictionary_t command_dict;
58           prop_dictionary_t args_dict;
59           prop_dictionary_t results_dict;
60           int8_t perr;
61           int error;
62           bool rv;
63 
64           command_dict = prop_dictionary_create();
65           args_dict = prop_dictionary_create();
66 
67           prop_dictionary_set_string_nocopy(command_dict, "drvctl-command",
68               "get-properties");
69           prop_dictionary_set_string_nocopy(args_dict, "device-name", devnode);
70           prop_dictionary_set(command_dict, "drvctl-arguments", args_dict);
71           prop_object_release(args_dict);
72 
73           error = prop_dictionary_sendrecv_ioctl(command_dict, fd,
74               DRVCTLCOMMAND, &results_dict);
75           prop_object_release(command_dict);
76           if (error)
77                     return false;
78 
79           rv = prop_dictionary_get_int8(results_dict, "drvctl-error", &perr);
80           if (rv == false || perr != 0) {
81                     prop_object_release(results_dict);
82                     return false;
83           }
84 
85           if (props) {
86                     prop_dictionary_t result_data;
87                     result_data = prop_dictionary_get(results_dict,
88                         "drvctl-result-data");
89                     if (result_data)
90                               *props = prop_dictionary_copy(result_data);
91           }
92 
93           prop_object_release(results_dict);
94 
95           return true;
96 }
97 
98 static int
drvctl_search(int fd,const char * curnode,const char * dvname,void (* callback)(void *,const char *,const char *,unsigned int),void * args)99 drvctl_search(int fd, const char *curnode, const char *dvname,
100     void (*callback)(void *, const char *, const char *, unsigned int),
101     void *args)
102 {
103           struct devlistargs laa;
104           unsigned int i;
105           uint32_t unit;
106           bool rv;
107 
108           if (drvctl_list(fd, curnode, &laa) == -1)
109                     return -1;
110 
111           for (i = 0; i < laa.l_children; i++) {
112                     prop_dictionary_t props = NULL;
113                     const char *curdvname;
114 
115                     rv = drvctl_get_properties(fd, laa.l_childname[i], &props);
116                     if (rv == false || props == NULL)
117                               continue;
118                     rv = prop_dictionary_get_string(props,
119                         "device-driver", &curdvname);
120                     if (rv == true && strcmp(curdvname, dvname) == 0) {
121                               rv = prop_dictionary_get_uint32(props,
122                                   "device-unit", &unit);
123                               if (rv == true)
124                                         callback(args, curnode,
125                                             laa.l_childname[i], unit);
126                     }
127                     prop_object_release(props);
128 
129                     if (drvctl_search(fd, laa.l_childname[i], dvname,
130                         callback, args) == -1)
131                               return -1;
132           }
133 
134           return 0;
135 }
136 
137 int
drvctl_foreach(int fd,const char * dvname,void (* callback)(void *,const char *,const char *,unsigned int),void * args)138 drvctl_foreach(int fd, const char *dvname,
139     void (*callback)(void *, const char *, const char *, unsigned int),
140     void *args)
141 {
142           return drvctl_search(fd, "", dvname, callback, args);
143 }
144