1N/A/***************************************************************************
1N/A *
1N/A * devinfo.c : main file for libdevinfo-based device enumeration
1N/A *
1N/A * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
1N/A *
1N/A * Licensed under the Academic Free License version 2.1
1N/A *
1N/A **************************************************************************/
1N/A
1N/A#ifdef HAVE_CONFIG_H
1N/A# include <config.h>
1N/A#endif
1N/A
1N/A#include <stdio.h>
1N/A#include <string.h>
1N/A#include <libdevinfo.h>
1N/A
1N/A#include "../osspec.h"
1N/A#include "../logger.h"
1N/A#include "../hald.h"
1N/A#include "../hald_dbus.h"
1N/A#include "../device_info.h"
1N/A#include "../util.h"
1N/A#include "../hald_runner.h"
1N/A#include "osspec_solaris.h"
1N/A#include "hotplug.h"
1N/A#include "devinfo.h"
1N/A#include "devinfo_pci.h"
1N/A#include "devinfo_storage.h"
1N/A#include "devinfo_ieee1394.h"
1N/A#include "devinfo_usb.h"
1N/A#include "devinfo_misc.h"
1N/A#include "devinfo_acpi.h"
1N/A#include "devinfo_cpu.h"
1N/A
1N/Avoid devinfo_add_subtree(HalDevice *parent, di_node_t node, gboolean is_root);
1N/AHalDevice *devinfo_add_node(HalDevice *parent, di_node_t node);
1N/A
1N/Avoid
1N/Adevinfo_add(HalDevice *parent, gchar *path)
1N/A{
1N/A di_node_t root;
1N/A
1N/A if (strcmp (path, "/") == 0) {
1N/A if ((root = di_init(path, DINFOCACHE)) == DI_NODE_NIL) {
1N/A HAL_INFO (("di_init() failed %d", errno));
1N/A return;
1N/A }
1N/A } else {
1N/A if ((root = di_init(path, DINFOCPYALL)) == DI_NODE_NIL) {
1N/A HAL_INFO (("di_init() failed %d", errno));
1N/A return;
1N/A }
1N/A }
1N/A
1N/A devinfo_add_subtree(parent, root, TRUE);
1N/A
1N/A di_fini (root);
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_add_subtree(HalDevice *parent, di_node_t node, gboolean is_root)
1N/A{
1N/A HalDevice *d;
1N/A di_node_t root_node, child_node;
1N/A
1N/A HAL_INFO (("add_subtree: %s", di_node_name (node)));
1N/A
1N/A root_node = node;
1N/A do {
1N/A d = devinfo_add_node (parent, node);
1N/A
1N/A if ((d != NULL) &&
1N/A (child_node = di_child_node (node)) != DI_NODE_NIL) {
1N/A devinfo_add_subtree (d, child_node, FALSE);
1N/A }
1N/A
1N/A node = di_sibling_node (node);
1N/A } while ((node != DI_NODE_NIL) &&
1N/A (!is_root || di_parent_node (node) == root_node));
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_set_default_properties (HalDevice *d, HalDevice *parent, di_node_t node, char *devfs_path)
1N/A{
1N/A char *driver_name, *s;
1N/A const char *s1;
1N/A char udi[HAL_PATH_MAX];
1N/A
1N/A if (parent != NULL) {
1N/A hal_device_property_set_string (d, "info.parent", hal_device_get_udi (parent));
1N/A } else {
1N/A hal_device_property_set_string (d, "info.parent", "/org/freedesktop/Hal/devices/local");
1N/A }
1N/A
1N/A hal_util_compute_udi (hald_get_gdl (), udi, sizeof (udi),
1N/A "/org/freedesktop/Hal/devices%s_%d",
1N/A devfs_path,
1N/A di_instance (node));
1N/A hal_device_set_udi (d, udi);
1N/A hal_device_property_set_string (d, "info.udi", udi);
1N/A
1N/A if (di_prop_lookup_strings (DDI_DEV_T_ANY, node, "model", &s) > 0) {
1N/A hal_device_property_set_string (d, "info.product", s);
1N/A } else {
1N/A hal_device_property_set_string (d, "info.product", di_node_name (node));
1N/A }
1N/A
1N/A hal_device_property_set_string (d, "solaris.devfs_path", devfs_path);
1N/A
1N/A if ((driver_name = di_driver_name (node)) != NULL) {
1N/A hal_device_property_set_string (d, "info.solaris.driver",
1N/A driver_name);
1N/A }
1N/A
1N/A
1N/A /* inherit parent's claim attributes */
1N/A if (hal_device_property_get_bool (parent, "info.claimed")) {
1N/A s1 = hal_device_property_get_string (parent, "info.claimed.service");
1N/A if (s1 != NULL) {
1N/A hal_device_property_set_bool (d, "info.claimed", TRUE);
1N/A hal_device_property_set_string (d, "info.claimed.service", s1);
1N/A }
1N/A }
1N/A}
1N/A
1N/A/* device handlers, ordered specific to generic */
1N/Astatic DevinfoDevHandler *devinfo_handlers[] = {
1N/A &devinfo_computer_handler,
1N/A &devinfo_cpu_handler,
1N/A &devinfo_ide_handler,
1N/A &devinfo_scsi_handler,
1N/A &devinfo_pcata_handler,
1N/A &devinfo_blkdev_handler,
1N/A &devinfo_floppy_handler,
1N/A &devinfo_usb_handler,
1N/A &devinfo_ieee1394_handler,
1N/A &devinfo_lofi_handler,
1N/A &devinfo_acpi_handler,
1N/A &devinfo_power_button_handler,
1N/A &devinfo_keyboard_handler,
1N/A &devinfo_mouse_handler,
1N/A &devinfo_pci_handler,
1N/A &devinfo_default_handler,
1N/A NULL
1N/A};
1N/A
1N/AHalDevice *
1N/Adevinfo_add_node(HalDevice *parent, di_node_t node)
1N/A{
1N/A HalDevice *d = NULL;
1N/A char *devfs_path;
1N/A char *device_type = NULL;
1N/A DevinfoDevHandler *handler;
1N/A int i;
1N/A
1N/A devfs_path = di_devfs_path (node);
1N/A
1N/A (void) di_prop_lookup_strings (DDI_DEV_T_ANY, node, "device_type",
1N/A &device_type);
1N/A
1N/A for (i = 0; (d == NULL) && (devinfo_handlers[i] != NULL); i++) {
1N/A handler = devinfo_handlers[i];
1N/A d = handler->add (parent, node, devfs_path, device_type);
1N/A }
1N/A
1N/A di_devfs_path_free(devfs_path);
1N/A
1N/A HAL_INFO (("add_node: %s", d ? hal_device_get_udi (d) : "none"));
1N/A return (d);
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_hotplug_enqueue(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler, int action, int front)
1N/A{
1N/A HotplugEvent *hotplug_event;
1N/A
1N/A hotplug_event = g_new0 (HotplugEvent, 1);
1N/A hotplug_event->action = action;
1N/A hotplug_event->type = HOTPLUG_EVENT_DEVFS;
1N/A hotplug_event->d = d;
1N/A strlcpy (hotplug_event->un.devfs.devfs_path, devfs_path,
1N/A sizeof (hotplug_event->un.devfs.devfs_path));
1N/A hotplug_event->un.devfs.handler = handler;
1N/A
1N/A hotplug_event_enqueue (hotplug_event, front);
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_add_enqueue(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler)
1N/A{
1N/A devinfo_hotplug_enqueue (d, devfs_path, handler, HOTPLUG_ACTION_ADD, 0);
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_add_enqueue_at_front(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler)
1N/A{
1N/A devinfo_hotplug_enqueue (d, devfs_path, handler, HOTPLUG_ACTION_ADD, 1);
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_remove_enqueue(gchar *devfs_path, DevinfoDevHandler *handler)
1N/A{
1N/A devinfo_hotplug_enqueue (NULL, devfs_path, handler, HOTPLUG_ACTION_REMOVE, 0);
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_callouts_add_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
1N/A{
1N/A void *end_token = (void *) userdata1;
1N/A
1N/A /* Move from temporary to global device store */
1N/A hal_device_store_remove (hald_get_tdl (), d);
1N/A hal_device_store_add (hald_get_gdl (), d);
1N/A
1N/A hotplug_event_end (end_token);
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_callouts_probing_done (HalDevice *d, guint32 exit_type, gint return_code, char **error, gpointer userdata1, gpointer userdata2)
1N/A{
1N/A void *end_token = (void *) userdata1;
1N/A
1N/A /* Discard device if probing reports failure */
1N/A if (exit_type != HALD_RUN_SUCCESS || (return_code != 0)) {
1N/A HAL_INFO (("Probing for %s failed %d", hal_device_get_udi (d), return_code));
1N/A hal_device_store_remove (hald_get_tdl (), d);
1N/A g_object_unref (d);
1N/A hotplug_event_end (end_token);
1N/A return;
1N/A }
1N/A
1N/A /* Merge properties from .fdi files */
1N/A di_search_and_merge (d, DEVICE_INFO_TYPE_INFORMATION);
1N/A di_search_and_merge (d, DEVICE_INFO_TYPE_POLICY);
1N/A
1N/A hal_util_callout_device_add (d, devinfo_callouts_add_done, end_token, NULL);
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_callouts_preprobing_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
1N/A{
1N/A void *end_token = (void *) userdata1;
1N/A DevinfoDevHandler *handler = (DevinfoDevHandler *) userdata2;
1N/A void (*probing_done) (HalDevice *, guint32, gint, char **, gpointer, gpointer);
1N/A const gchar *prober;
1N/A int prober_timeout;
1N/A
1N/A if (hal_device_property_get_bool (d, "info.ignore")) {
1N/A HAL_INFO (("Preprobing merged info.ignore==TRUE"));
1N/A
1N/A /* Leave device with info.ignore==TRUE so we won't pick up children */
1N/A hal_device_property_remove (d, "info.category");
1N/A hal_device_property_remove (d, "info.capabilities");
1N/A
1N/A hal_device_store_remove (hald_get_tdl (), d);
1N/A hal_device_store_add (hald_get_gdl (), d);
1N/A
1N/A hotplug_event_end (end_token);
1N/A return;
1N/A }
1N/A
1N/A if (handler != NULL && handler->get_prober != NULL) {
1N/A prober = handler->get_prober (d, &prober_timeout);
1N/A } else {
1N/A prober = NULL;
1N/A }
1N/A
1N/A if (handler->probing_done != NULL) {
1N/A probing_done = handler->probing_done;
1N/A } else {
1N/A probing_done = devinfo_callouts_probing_done;
1N/A }
1N/A
1N/A if (prober != NULL) {
1N/A /* probe the device */
1N/A HAL_INFO(("Probing udi=%s", hal_device_get_udi (d)));
1N/A hald_runner_run (d,
1N/A prober, NULL,
1N/A prober_timeout,
1N/A probing_done,
1N/A (gpointer) end_token, (gpointer) handler);
1N/A } else {
1N/A probing_done (d, 0, 0, NULL, userdata1, userdata2);
1N/A }
1N/A}
1N/A
1N/A/* This is the beginning of hotplug even handling */
1N/Avoid
1N/Ahotplug_event_begin_add_devinfo (HalDevice *d, HalDevice *parent, DevinfoDevHandler *handler, void *end_token)
1N/A{
1N/A HotplugEvent *hotplug_event = (HotplugEvent *)end_token;
1N/A
1N/A HAL_INFO(("Preprobing udi=%s", hal_device_get_udi (d)));
1N/A
1N/A if (parent == NULL && (strcmp(hotplug_event->un.devfs.devfs_path, "/") != 0)) {
1N/A HAL_ERROR (("Parent is NULL, devfs_path=%s", hotplug_event->un.devfs.devfs_path));
1N/A
1N/A goto skip;
1N/A }
1N/A
1N/A
1N/A if (parent != NULL && hal_device_property_get_bool (parent, "info.ignore")) {
1N/A HAL_INFO (("Ignoring device since parent has info.ignore==TRUE"));
1N/A
1N/A goto skip;
1N/A }
1N/A
1N/A if (hal_device_store_find (hald_get_tdl (), hal_device_get_udi (d)) == NULL) {
1N/A
1N/A /* add to TDL so preprobing callouts and prober can access it */
1N/A hal_device_store_add (hald_get_tdl (), d);
1N/A }
1N/A
1N/A /* Process preprobe fdi files */
1N/A di_search_and_merge (d, DEVICE_INFO_TYPE_PREPROBE);
1N/A
1N/A /* Run preprobe callouts */
1N/A hal_util_callout_device_preprobe (d, devinfo_callouts_preprobing_done, end_token, handler);
1N/A
1N/A return;
1N/A
1N/Askip:
1N/A if (hal_device_store_find (hald_get_tdl (), hal_device_get_udi (d)))
1N/A hal_device_store_remove (hald_get_tdl (), d);
1N/A
1N/A g_object_unref (d);
1N/A hotplug_event_end (end_token);
1N/A
1N/A return;
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_remove (gchar *devfs_path)
1N/A{
1N/A devinfo_remove_enqueue ((gchar *)devfs_path, NULL);
1N/A}
1N/A
1N/A/* generate hotplug event for each device in this branch */
1N/Avoid
1N/Adevinfo_remove_branch (gchar *devfs_path, HalDevice *d)
1N/A{
1N/A GSList *i;
1N/A GSList *children;
1N/A HalDevice *child;
1N/A char *child_devfs_path;
1N/A
1N/A if (d == NULL) {
1N/A d = hal_device_store_match_key_value_string (hald_get_gdl (),
1N/A "solaris.devfs_path", devfs_path);
1N/A if (d == NULL)
1N/A return;
1N/A }
1N/A
1N/A HAL_INFO (("remove_branch: %s %s\n", devfs_path, hal_device_get_udi (d)));
1N/A
1N/A /* first remove children */
1N/A children = hal_device_store_match_multiple_key_value_string (hald_get_gdl(),
1N/A "info.parent", hal_device_get_udi (d));
1N/A for (i = children; i != NULL; i = g_slist_next (i)) {
1N/A child = HAL_DEVICE (i->data);
1N/A HAL_INFO (("remove_branch: child %s\n", hal_device_get_udi (child)));
1N/A devinfo_remove_branch ((gchar *)hal_device_property_get_string (child, "solaris.devfs_path"), child);
1N/A }
1N/A g_slist_free (children);
1N/A HAL_INFO (("remove_branch: done with children"));
1N/A
1N/A /* then remove self */
1N/A HAL_INFO (("remove_branch: queueing %s", devfs_path));
1N/A devinfo_remove_enqueue (devfs_path, NULL);
1N/A}
1N/A
1N/Avoid
1N/Adevinfo_callouts_remove_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
1N/A{
1N/A void *end_token = (void *) userdata1;
1N/A
1N/A HAL_INFO (("Remove callouts completed udi=%s", hal_device_get_udi (d)));
1N/A
1N/A if (!hal_device_store_remove (hald_get_gdl (), d)) {
1N/A HAL_WARNING (("Error removing device"));
1N/A }
1N/A g_object_unref (d);
1N/A
1N/A hotplug_event_end (end_token);
1N/A}
1N/A
1N/Avoid
1N/Ahotplug_event_begin_remove_devinfo (HalDevice *d, gchar *devfs_path, void *end_token)
1N/A{
1N/A if (hal_device_has_capability (d, "volume")) {
1N/A devinfo_volume_hotplug_begin_remove (d, devfs_path, end_token);
1N/A } else {
1N/A hal_util_callout_device_remove (d, devinfo_callouts_remove_done, end_token, NULL);
1N/A }
1N/A}
1N/A
1N/Agboolean
1N/Adevinfo_device_rescan (HalDevice *d)
1N/A{
1N/A if (hal_device_has_capability (d, "block")) {
1N/A return (devinfo_storage_device_rescan (d));
1N/A } else if (hal_device_has_capability (d, "button")) {
1N/A return (devinfo_lid_rescan (d));
1N/A } else {
1N/A return (FALSE);
1N/A }
1N/A}
1N/A
1N/Astatic int
1N/Awalk_devlinks(di_devlink_t devlink, void *arg)
1N/A{
1N/A char **path= (char **)arg;
1N/A
1N/A *path = strdup(di_devlink_path(devlink));
1N/A
1N/A return (DI_WALK_TERMINATE);
1N/A}
1N/A
1N/Achar *
1N/Aget_devlink(di_devlink_handle_t devlink_hdl, char *re, char *path)
1N/A{
1N/A char *devlink_path = NULL;
1N/A
1N/A (void) di_devlink_walk(devlink_hdl, re, path,
1N/A DI_PRIMARY_LINK, &devlink_path, walk_devlinks);
1N/A
1N/A return (devlink_path);
1N/A}