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