xref: /titanic_44/usr/src/cmd/hal/hald/solaris/devinfo.c (revision 33649781048a478eaa5fa15460fc764530f86c8c)
1 /***************************************************************************
2  *
3  * devinfo.c : main file for libdevinfo-based device enumeration
4  *
5  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
6  * Use is subject to license terms.
7  *
8  * Licensed under the Academic Free License version 2.1
9  *
10  **************************************************************************/
11 
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13 
14 #ifdef HAVE_CONFIG_H
15 #  include <config.h>
16 #endif
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <libdevinfo.h>
21 
22 #include "../osspec.h"
23 #include "../logger.h"
24 #include "../hald.h"
25 #include "../hald_dbus.h"
26 #include "../device_info.h"
27 #include "../util.h"
28 #include "../hald_runner.h"
29 #include "osspec_solaris.h"
30 #include "hotplug.h"
31 #include "devinfo.h"
32 #include "devinfo_pci.h"
33 #include "devinfo_storage.h"
34 #include "devinfo_ieee1394.h"
35 #include "devinfo_usb.h"
36 #include "devinfo_misc.h"
37 #include "devinfo_acpi.h"
38 
39 void devinfo_add_subtree(HalDevice *parent, di_node_t node, gboolean is_root);
40 HalDevice *devinfo_add_node(HalDevice *parent, di_node_t node);
41 
42 void
43 devinfo_add(HalDevice *parent, gchar *path)
44 {
45 	di_node_t	root;
46 
47 	if (strcmp (path, "/") == 0) {
48 		if ((root = di_init(path, DINFOCACHE)) == DI_NODE_NIL) {
49 			HAL_INFO (("di_init() failed %d", errno));
50 			return;
51 		}
52 	} else {
53 		if ((root = di_init(path, DINFOCPYALL)) == DI_NODE_NIL) {
54 			HAL_INFO (("di_init() failed %d", errno));
55 			return;
56 		}
57 	}
58 
59 	devinfo_add_subtree(parent, root, TRUE);
60 
61 	di_fini (root);
62 }
63 
64 void
65 devinfo_add_subtree(HalDevice *parent, di_node_t node, gboolean is_root)
66 {
67 	HalDevice *d;
68 	di_node_t root_node, child_node;
69 
70 	HAL_INFO (("add_subtree: %s", di_node_name (node)));
71 
72 	root_node = node;
73 	do {
74 		d = devinfo_add_node (parent, node);
75 
76 		if ((d != NULL) &&
77 		    (child_node = di_child_node (node)) != DI_NODE_NIL) {
78 			devinfo_add_subtree (d, child_node, FALSE);
79 		}
80 
81 		node = di_sibling_node (node);
82 	} while ((node != DI_NODE_NIL) &&
83 		(!is_root || di_parent_node (node) == root_node));
84 }
85 
86 void
87 devinfo_set_default_properties (HalDevice *d, HalDevice *parent, di_node_t node, char *devfs_path)
88 {
89 	char	*driver_name, *s;
90 	const char *s1;
91 	char	udi[HAL_PATH_MAX];
92 
93 	if (parent != NULL) {
94 		hal_device_property_set_string (d, "info.parent", hal_device_get_udi (parent));
95 	} else {
96 		hal_device_property_set_string (d, "info.parent", "/org/freedesktop/Hal/devices/local");
97 	}
98 
99 	hal_util_compute_udi (hald_get_gdl (), udi, sizeof (udi),
100 				"/org/freedesktop/Hal/devices%s_%d",
101 				devfs_path,
102 				di_instance (node));
103 	hal_device_set_udi (d, udi);
104 	hal_device_property_set_string (d, "info.udi", udi);
105 
106 	if (di_prop_lookup_strings (DDI_DEV_T_ANY, node, "model", &s) > 0) {
107 		hal_device_property_set_string (d, "info.product", s);
108 	} else {
109 		hal_device_property_set_string (d, "info.product", di_node_name (node));
110 	}
111 
112 	hal_device_property_set_string (d, "solaris.devfs_path", devfs_path);
113 
114 	if ((driver_name = di_driver_name (node)) != NULL) {
115 		hal_device_property_set_string (d, "info.solaris.driver",
116 						driver_name);
117 	}
118 
119 
120 	/* inherit parent's claim attributes */
121 	if (hal_device_property_get_bool (parent, "info.claimed")) {
122 		s1 = hal_device_property_get_string (parent, "info.claimed.service");
123 		if (s1 != NULL) {
124 			hal_device_property_set_bool (d, "info.claimed", TRUE);
125 			hal_device_property_set_string (d, "info.claimed.service", s1);
126 		}
127 	}
128 }
129 
130 /* device handlers, ordered specific to generic */
131 static DevinfoDevHandler *devinfo_handlers[] = {
132 	&devinfo_computer_handler,
133 	&devinfo_cpu_handler,
134 	&devinfo_ide_handler,
135 	&devinfo_scsi_handler,
136 	&devinfo_pcata_handler,
137 	&devinfo_floppy_handler,
138 	&devinfo_usb_handler,
139 	&devinfo_ieee1394_handler,
140 	&devinfo_pci_handler,
141 	&devinfo_lofi_handler,
142 	&devinfo_acpi_handler,
143 	&devinfo_battery_handler,
144 	&devinfo_power_button_handler,
145 	&devinfo_keyboard_handler,
146 	&devinfo_default_handler,
147 	NULL
148 };
149 
150 HalDevice *
151 devinfo_add_node(HalDevice *parent, di_node_t node)
152 {
153 	HalDevice *d = NULL;
154 	char	*devfs_path;
155 	char	*device_type = NULL;
156 	DevinfoDevHandler *handler;
157 	int	i;
158 
159 	devfs_path = di_devfs_path (node);
160 
161         (void) di_prop_lookup_strings (DDI_DEV_T_ANY, node, "device_type",
162 	    &device_type);
163 
164 	for (i = 0; (d == NULL) && (devinfo_handlers[i] != NULL); i++) {
165 		handler = devinfo_handlers[i];
166 		d = handler->add (parent, node, devfs_path, device_type);
167 	}
168 
169 	di_devfs_path_free(devfs_path);
170 
171 	HAL_INFO (("add_node: %s", d ? hal_device_get_udi (d) : "none"));
172 	return (d);
173 }
174 
175 void
176 devinfo_hotplug_enqueue(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler, int action, int front)
177 {
178 	HotplugEvent *hotplug_event;
179 
180 	hotplug_event = g_new0 (HotplugEvent, 1);
181 	hotplug_event->action = action;
182 	hotplug_event->type = HOTPLUG_EVENT_DEVFS;
183 	hotplug_event->d = d;
184 	strlcpy (hotplug_event->un.devfs.devfs_path, devfs_path,
185 		sizeof (hotplug_event->un.devfs.devfs_path));
186 	hotplug_event->un.devfs.handler = handler;
187 
188 	hotplug_event_enqueue (hotplug_event, front);
189 }
190 
191 void
192 devinfo_add_enqueue(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler)
193 {
194 	devinfo_hotplug_enqueue (d, devfs_path, handler, HOTPLUG_ACTION_ADD, 0);
195 }
196 
197 void
198 devinfo_add_enqueue_at_front(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler)
199 {
200 	devinfo_hotplug_enqueue (d, devfs_path, handler, HOTPLUG_ACTION_ADD, 1);
201 }
202 
203 void
204 devinfo_remove_enqueue(gchar *devfs_path, DevinfoDevHandler *handler)
205 {
206 	devinfo_hotplug_enqueue (NULL, devfs_path, handler, HOTPLUG_ACTION_REMOVE, 0);
207 }
208 
209 void
210 devinfo_callouts_add_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
211 {
212         void *end_token = (void *) userdata1;
213 
214         /* Move from temporary to global device store */
215         hal_device_store_remove (hald_get_tdl (), d);
216         hal_device_store_add (hald_get_gdl (), d);
217 
218         hotplug_event_end (end_token);
219 }
220 
221 void
222 devinfo_callouts_probing_done (HalDevice *d, guint32 exit_type, gint return_code, char **error, gpointer userdata1, gpointer userdata2)
223 {
224         void *end_token = (void *) userdata1;
225 
226         /* Discard device if probing reports failure */
227         if (exit_type != HALD_RUN_SUCCESS || (return_code != 0)) {
228 		HAL_INFO (("Probing for %s failed %d", hal_device_get_udi (d), return_code));
229                 hal_device_store_remove (hald_get_tdl (), d);
230                 g_object_unref (d);
231                 hotplug_event_end (end_token);
232 		return;
233         }
234 
235         /* Merge properties from .fdi files */
236         di_search_and_merge (d, DEVICE_INFO_TYPE_INFORMATION);
237         di_search_and_merge (d, DEVICE_INFO_TYPE_POLICY);
238 
239 	hal_util_callout_device_add (d, devinfo_callouts_add_done, end_token, NULL);
240 }
241 
242 void
243 devinfo_callouts_preprobing_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
244 {
245         void *end_token = (void *) userdata1;
246 	DevinfoDevHandler *handler = (DevinfoDevHandler *) userdata2;
247 	void (*probing_done) (HalDevice *, guint32, gint, char **, gpointer, gpointer);
248 	const gchar *prober;
249 	int prober_timeout;
250 
251         if (hal_device_property_get_bool (d, "info.ignore")) {
252 		HAL_INFO (("Preprobing merged info.ignore==TRUE"));
253 
254                 /* Leave device with info.ignore==TRUE so we won't pick up children */
255 		hal_device_property_remove (d, "info.category");
256 		hal_device_property_remove (d, "info.capabilities");
257 
258 		hal_device_store_remove (hald_get_tdl (), d);
259 		hal_device_store_add (hald_get_gdl (), d);
260 
261 		hotplug_event_end (end_token);
262 		return;
263         }
264 
265         if (handler != NULL && handler->get_prober != NULL) {
266                 prober = handler->get_prober (d, &prober_timeout);
267         } else {
268                 prober = NULL;
269 	}
270 
271 	if (handler->probing_done != NULL) {
272 		probing_done = handler->probing_done;
273 	} else {
274 		probing_done = devinfo_callouts_probing_done;
275 	}
276 
277         if (prober != NULL) {
278                 /* probe the device */
279 		HAL_INFO(("Probing udi=%s", hal_device_get_udi (d)));
280                 hald_runner_run (d,
281 				prober, NULL,
282 				prober_timeout,
283 				probing_done,
284 				(gpointer) end_token, (gpointer) handler);
285 	} else {
286 		probing_done (d, 0, 0, NULL, userdata1, userdata2);
287 	}
288 }
289 
290 /* This is the beginning of hotplug even handling */
291 void
292 hotplug_event_begin_add_devinfo (HalDevice *d, HalDevice *parent, DevinfoDevHandler *handler, void *end_token)
293 {
294 	HAL_INFO(("Preprobing udi=%s", hal_device_get_udi (d)));
295 
296 	if (parent != NULL && hal_device_property_get_bool (parent, "info.ignore")) {
297 		HAL_INFO (("Ignoring device since parent has info.ignore==TRUE"));
298 
299 		if (hal_device_store_find (hald_get_tdl (), hal_device_get_udi (d)))
300 			hal_device_store_remove (hald_get_tdl (), d);
301 
302 		hotplug_event_end (end_token);
303 		return;
304 	}
305 
306 	if (hal_device_store_find (hald_get_tdl (), hal_device_get_udi (d)) == NULL) {
307 
308 		/* add to TDL so preprobing callouts and prober can access it */
309 		hal_device_store_add (hald_get_tdl (), d);
310 	}
311 
312         /* Process preprobe fdi files */
313         di_search_and_merge (d, DEVICE_INFO_TYPE_PREPROBE);
314 
315         /* Run preprobe callouts */
316         hal_util_callout_device_preprobe (d, devinfo_callouts_preprobing_done, end_token, handler);
317 }
318 
319 void
320 devinfo_remove (gchar *devfs_path)
321 {
322 	devinfo_remove_enqueue ((gchar *)devfs_path, NULL);
323 }
324 
325 /* generate hotplug event for each device in this branch */
326 void
327 devinfo_remove_branch (gchar *devfs_path, HalDevice *d)
328 {
329 	GSList *i;
330 	GSList *children;
331 	HalDevice *child;
332 	char *child_devfs_path;
333 
334 	if (d == NULL) {
335 		d = hal_device_store_match_key_value_string (hald_get_gdl (),
336 			"solaris.devfs_path", devfs_path);
337 		if (d == NULL)
338 			return;
339 	}
340 
341 	HAL_INFO (("remove_branch: %s %s\n", devfs_path, hal_device_get_udi (d)));
342 
343 	/* first remove children */
344 	children = hal_device_store_match_multiple_key_value_string (hald_get_gdl(),
345 		"info.parent", hal_device_get_udi (d));
346         for (i = children; i != NULL; i = g_slist_next (i)) {
347                 child = HAL_DEVICE (i->data);
348 		HAL_INFO (("remove_branch: child %s\n", hal_device_get_udi (child)));
349 		devinfo_remove_branch ((gchar *)hal_device_property_get_string (child, "solaris.devfs_path"), child);
350 	}
351 	g_slist_free (children);
352 	HAL_INFO (("remove_branch: done with children"));
353 
354 	/* then remove self */
355 	HAL_INFO (("remove_branch: queueing %s", devfs_path));
356 	devinfo_remove_enqueue (devfs_path, NULL);
357 }
358 
359 void
360 devinfo_callouts_remove_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
361 {
362         void *end_token = (void *) userdata1;
363 
364         HAL_INFO (("Remove callouts completed udi=%s", hal_device_get_udi (d)));
365 
366         if (!hal_device_store_remove (hald_get_gdl (), d)) {
367                 HAL_WARNING (("Error removing device"));
368         }
369         g_object_unref (d);
370 
371         hotplug_event_end (end_token);
372 }
373 
374 void
375 hotplug_event_begin_remove_devinfo (HalDevice *d, gchar *devfs_path, void *end_token)
376 {
377 	if (hal_device_has_capability (d, "volume")) {
378 		devinfo_volume_hotplug_begin_remove (d, devfs_path, end_token);
379 	} else {
380 		hal_util_callout_device_remove (d, devinfo_callouts_remove_done, end_token, NULL);
381 	}
382 }
383 
384 gboolean
385 devinfo_device_rescan (HalDevice *d)
386 {
387 	if (hal_device_has_capability (d, "block")) {
388 		return (devinfo_storage_device_rescan (d));
389         } else {
390 		return (FALSE);
391 	}
392 }
393 
394 static int
395 walk_devlinks(di_devlink_t devlink, void *arg)
396 {
397         char    **path= (char **)arg;
398 
399         *path = strdup(di_devlink_path(devlink));
400 
401         return (DI_WALK_TERMINATE);
402 }
403 
404 char *
405 get_devlink(di_devlink_handle_t devlink_hdl, char *re, char *path)
406 {
407         char    *devlink_path = NULL;
408 
409         (void) di_devlink_walk(devlink_hdl, re, path,
410             DI_PRIMARY_LINK, &devlink_path, walk_devlinks);
411 
412         return (devlink_path);
413 }
414 
415