1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/***************************************************************************
*
* drvctl.c : NetBSD drvctl events
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/drvctlio.h>
#include <sys/dkio.h>
#include <sys/stat.h>
#include <sys/drvctlio.h>
#include <glib.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "osspec_netbsd.h"
#include "hotplug.h"
#include "devinfo.h"
#include "devinfo_storage.h"
#include "drvctl.h"
static gboolean drvctl_iochannel_data(GIOChannel *, GIOCondition, gpointer);
static void drvctl_dev_add(gchar *);
static void drvctl_dev_remove(gchar *);
static void drvctl_dev_branch(gchar *);
static int drvctl_fd;
static GIOChannel *drvctl_iochannel;
gboolean
drvctl_init(void)
{
drvctl_fd = open (DRVCTLDEV, O_RDWR);
if (drvctl_fd == -1) {
HAL_INFO (("open(%s, O_RDWR) failed: %s", DRVCTLDEV, strerror(errno)));
return FALSE;
}
drvctl_iochannel = g_io_channel_unix_new (drvctl_fd);
if (drvctl_iochannel == NULL) {
HAL_INFO (("g_io_channel_unix_new failed"));
return FALSE;
}
g_io_add_watch (drvctl_iochannel, G_IO_IN, drvctl_iochannel_data, NULL);
return TRUE;
}
void
drvctl_fini(void)
{
HAL_INFO (("drvctl_fini"));
}
static gboolean
drvctl_iochannel_data (GIOChannel *source,
GIOCondition condition,
gpointer user_data)
{
prop_dictionary_t ev;
const char *event, *device;
int res;
HAL_INFO (("drvctl_iochannel_data"));
res = prop_dictionary_recv_ioctl (drvctl_fd, DRVGETEVENT, &ev);
if (res) {
HAL_WARNING (("DRVGETEVENT failed: %s", strerror(errno)));
return FALSE;
}
if (!prop_dictionary_get_cstring_nocopy (ev, "event", &event)) {
HAL_WARNING (("DRVGETEVENT missing \"event\" parameter"));
goto done;
}
if (!prop_dictionary_get_cstring_nocopy (ev, "device", &device)) {
HAL_WARNING (("DRVGETEVENT missing \"device\" parameter"));
goto done;
}
HAL_INFO (("DRVGETEVENT event=%s device=%s", event, device));
if (strcmp (event, "device-attach") == 0) {
drvctl_dev_add (device);
} else {
drvctl_dev_remove (device);
}
done:
prop_object_release(ev);
return TRUE;
}
static void
drvctl_dev_add(gchar *name)
{
HalDevice *parent, *d;
gchar pdevnode[512];
if (drvctl_find_parent (name, pdevnode) == FALSE) {
HAL_INFO (("dev_add: name=%s orphan", name));
parent = NULL;
} else {
parent = hal_device_store_match_key_value_string (
hald_get_gdl(), "netbsd.device", pdevnode);
if (parent == NULL)
HAL_INFO (("dev_add: name=%s but netbsd.device=%s not found",name, pdevnode));
}
d = devinfo_add_node (parent, name);
if (d == NULL)
HAL_WARNING (("dev_add: couldn't add %s node (parent=%p)", name, parent));
hotplug_event_process_queue ();
}
static void
drvctl_dev_remove(gchar *name)
{
HAL_INFO (("dev_remove: %s", name));
devinfo_remove_branch (name, NULL);
hotplug_event_process_queue ();
}
static void
drvctl_dev_branch(gchar *name)
{
HAL_INFO (("branch_remove: %s", name));
devinfo_remove_branch (name, NULL);
hotplug_event_process_queue ();
}
int
drvctl_list(const gchar *name, struct devlistargs *laa)
{
size_t children;
/* HAL_INFO (("drvctl_list: %s", name)); */
memset (laa, 0, sizeof (*laa));
strlcpy (laa->l_devname, name, sizeof (laa->l_devname));
if (ioctl (drvctl_fd, DRVLISTDEV, laa) == -1) {
HAL_INFO (("DRVLISTDEV/1 failed: %s", strerror(errno)));
return -1;
}
children = laa->l_children;
/* HAL_INFO (("%s: found %d children", name, children)); */
if (children == 0)
return -1;
laa->l_childname = malloc (children * sizeof (laa->l_childname[0]));
if (laa->l_childname == NULL) {
HAL_INFO (("drvctl_list couldn't allocate %d children: %s\n", children, strerror(errno)));
return -1;
}
if (ioctl (drvctl_fd, DRVLISTDEV, laa) == -1) {
HAL_INFO (("DRVLISTDEV/2 failed: %s", strerror(errno)));
return -1;
}
if (children != laa->l_children)
HAL_WARNING (("DRVLISTDEV/3 expected %d children, got %d", children, laa->l_childname));
return 0;
}
gboolean
drvctl_find_device(const gchar *devnode, prop_dictionary_t *properties)
{
prop_dictionary_t command_dict;
prop_dictionary_t args_dict;
prop_dictionary_t results_dict;
int err;
command_dict = prop_dictionary_create ();
args_dict = prop_dictionary_create ();
prop_dictionary_set_cstring_nocopy (command_dict, "drvctl-command", "get-properties");
prop_dictionary_set_cstring_nocopy (args_dict, "device-name", devnode);
prop_dictionary_set (command_dict, "drvctl-arguments", args_dict);
prop_object_release (args_dict);
err = prop_dictionary_sendrecv_ioctl (command_dict, drvctl_fd,
DRVCTLCOMMAND, &results_dict);
prop_object_release (command_dict);
if (err)
return FALSE;
if (prop_dictionary_get_int8 (results_dict, "drvctl-error", &err) == false || err != 0) {
prop_object_release (results_dict);
return FALSE;
}
if (properties) {
prop_dictionary_t result_data;
result_data = prop_dictionary_get (results_dict, "drvctl-result-data");
if (result_data)
*properties = prop_dictionary_copy (result_data);
}
prop_object_release (results_dict);
return TRUE;
}
static gboolean
drvctl_find_device_with_child(const gchar *curnode, const gchar *devnode,
char *parent)
{
struct devlistargs laa;
u_int i;
if (drvctl_list (curnode, &laa) == -1)
return FALSE;
for (i = 0; i < laa.l_children; i++) {
if (strcmp (laa.l_childname[i], devnode) == 0) {
strlcpy(parent, curnode, 16);
free(laa.l_childname);
return TRUE;
}
if (drvctl_find_device_with_child (laa.l_childname[i], devnode, parent) == TRUE) {
free(laa.l_childname);
return TRUE;
}
}
if (laa.l_childname)
free(laa.l_childname);
HAL_INFO (("%s: couldn't find device with child %s", curnode, devnode));
return FALSE;
}
gboolean
drvctl_find_parent(const gchar *devnode, char *parent)
{
gboolean ret;
ret = drvctl_find_device_with_child("mainbus0", devnode, parent);
if (ret == FALSE) {
ret = drvctl_find_device_with_child("armfdt0", devnode, parent);
}
return ret;
}
#if 0
static void
drvctl_lofi_add(gchar *devfs_path, gchar *name)
{
di_node_t node;
const char *parent_udi;
HalDevice *d, *parent;
HAL_INFO (("lofi_add: %s %s", name, devfs_path));
if ((d = hal_device_store_match_key_value_string (hald_get_gdl (),
"solaris.devfs_path", devfs_path)) == NULL) {
HAL_INFO (("device not found in GDL %s", devfs_path));
return;
}
parent_udi = hal_device_property_get_string (d, "info.parent");
if ((parent_udi == NULL) || (strlen(parent_udi) == 0)) {
HAL_INFO (("parent not found in GDL %s", parent_udi));
return;
}
if ((parent = hal_device_store_match_key_value_string (hald_get_gdl (),
"info.udi", parent_udi)) == NULL) {
HAL_INFO (("parent not found in GDL %s", parent_udi));
return;
}
if ((node = di_init (devfs_path, DINFOCPYALL)) == DI_NODE_NIL) {
HAL_INFO (("device not found in devinfo %s", devfs_path));
return;
}
HAL_INFO (("device %s parent %s", hal_device_get_udi (d), parent_udi));
devinfo_lofi_add_major (parent, node, devfs_path, NULL, TRUE, d);
di_fini (node);
hotplug_event_process_queue ();
}
static void
drvctl_lofi_remove(gchar *parent_devfs_path, gchar *name)
{
devinfo_lofi_remove_minor(parent_devfs_path, name);
hotplug_event_process_queue ();
}
#endif
|