1/***************************************************************************
2 *
3 * devinfo_pci.c : PCI devices
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 "../ids.h"
29#include "devinfo_pci.h"
30
31HalDevice *devinfo_pci_add (HalDevice *parent, di_node_t node, char *devfs_path, char *device_type);
32
33DevinfoDevHandler devinfo_pci_handler = {
34        devinfo_pci_add,
35	NULL,
36	NULL,
37	NULL,
38	NULL,
39        NULL
40};
41
42HalDevice *devinfo_pci_add (HalDevice *parent, di_node_t node, char *devfs_path, char *device_type)
43{
44	HalDevice *d;
45	char	*s;
46	int	*i;
47	int	vid, pid, svid, spid;
48
49	if ((device_type == NULL) ||
50	    ((strcmp (device_type, "pci") != 0) &&
51	    (strcmp (device_type, "pci-ide") != 0))) {
52		if (parent == NULL) {
53			return (NULL);
54		} else {
55			s = (char *)hal_device_property_get_string (parent, "info.subsystem");
56			if ((s == NULL) || (strcmp (s, "pci") != 0)) {
57				return (NULL);
58			}
59		}
60	}
61
62	d = hal_device_new ();
63	devinfo_set_default_properties (d, parent, node, devfs_path);
64
65	hal_device_property_set_string (d, "info.subsystem", "pci");
66
67	vid = pid = svid = spid = 0;
68        if (di_prop_lookup_ints (DDI_DEV_T_ANY, node, "vendor-id", &i) > 0) {
69		vid = i[0];
70	}
71        if (di_prop_lookup_ints (DDI_DEV_T_ANY, node, "device-id", &i) > 0) {
72		pid = i[0];
73	}
74        if (di_prop_lookup_ints (DDI_DEV_T_ANY, node, "subsystem-vendor-id", &i) > 0) {
75		svid = i[0];
76	}
77        if (di_prop_lookup_ints (DDI_DEV_T_ANY, node, "subsystem-id", &i) > 0) {
78		spid = i[0];
79	}
80	hal_device_property_set_int (d, "pci.vendor_id", vid);
81	hal_device_property_set_int (d, "pci.product_id", pid);
82	hal_device_property_set_int (d, "pci.subsys_vendor_id", svid);
83	hal_device_property_set_int (d, "pci.subsys_product_id", spid);
84
85        {
86                char *vendor_name;
87                char *product_name;
88                char *subsys_vendor_name;
89                char *subsys_product_name;
90
91                ids_find_pci (hal_device_property_get_int (d, "pci.vendor_id"),
92                              hal_device_property_get_int (d, "pci.product_id"),
93                              hal_device_property_get_int (d, "pci.subsys_vendor_id"),
94                              hal_device_property_get_int (d, "pci.subsys_product_id"),
95                              &vendor_name, &product_name, &subsys_vendor_name,
96&subsys_product_name);
97
98                if (vendor_name != NULL) {
99                        hal_device_property_set_string (d, "pci.vendor", vendor_name);
100                        hal_device_property_set_string (d, "info.vendor", vendor_name);
101                }
102
103                if (product_name != NULL) {
104                        hal_device_property_set_string (d, "pci.product", product_name);
105                        hal_device_property_set_string (d, "info.product", product_name);
106                }
107
108                if (subsys_vendor_name != NULL) {
109                        hal_device_property_set_string (d, "pci.subsys_vendor",
110subsys_vendor_name);
111                }
112
113                if (subsys_product_name != NULL) {
114                        hal_device_property_set_string (d, "pci.subsys_product", subsys_product_name);
115                }
116        }
117
118	devinfo_add_enqueue (d, devfs_path, &devinfo_pci_handler);
119
120	return (d);
121}
122
123