• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2006 Erwan Velu - All Rights Reserved
4  *
5  *   Permission is hereby granted, free of charge, to any person
6  *   obtaining a copy of this software and associated documentation
7  *   files (the "Software"), to deal in the Software without
8  *   restriction, including without limitation the rights to use,
9  *   copy, modify, merge, publish, distribute, sublicense, and/or
10  *   sell copies of the Software, and to permit persons to whom
11  *   the Software is furnished to do so, subject to the following
12  *   conditions:
13  *
14  *   The above copyright notice and this permission notice shall
15  *   be included in all copies or substantial portions of the Software.
16  *
17  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19  *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21  *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22  *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  *   OTHER DEALINGS IN THE SOFTWARE.
25  *
26  * -----------------------------------------------------------------------
27 */
28 
29 /*
30  * pcitest.c
31  *
32  */
33 
34 #include <inttypes.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <console.h>
39 #include <com32.h>
40 #include <sys/pci.h>
41 #include <stdbool.h>
42 #include <dprintf.h>
43 
44 char display_line = 0;
45 #define moreprintf(...)				\
46   do {						\
47     display_line++;				\
48     if (display_line == 24) {			\
49       char tempbuf[10];				\
50       display_line=0;				\
51       printf("Press Enter to continue\n");	\
52       fgets(tempbuf, sizeof tempbuf, stdin);	\
53     }						\
54     printf ( __VA_ARGS__);			\
55   } while (0);
56 
display_pci_devices(struct pci_domain * pci_domain)57 void display_pci_devices(struct pci_domain *pci_domain)
58 {
59     struct pci_device *pci_device;
60     char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
61 			MAX_KERNEL_MODULES_PER_PCI_DEVICE];
62 
63     for_each_pci_func(pci_device, pci_domain) {
64 
65 	memset(kernel_modules, 0, sizeof kernel_modules);
66 
67 /*	printf("PCI: found %d kernel modules for  %04x:%04x[%04x:%04x]\n",
68 		  pci_device->dev_info->linux_kernel_module_count,
69 		  pci_device->vendor, pci_device->product,
70 		  pci_device->sub_vendor, pci_device->sub_product);
71 */
72 	for (int i = 0; i < pci_device->dev_info->linux_kernel_module_count;
73 	     i++) {
74 	    if (i > 0) {
75 		strncat(kernel_modules, " | ", 3);
76 	    }
77 	    strncat(kernel_modules,
78 		    pci_device->dev_info->linux_kernel_module[i],
79 		    LINUX_KERNEL_MODULE_SIZE - 1);
80 	}
81 
82 	moreprintf("%04x:%04x[%04x:%04x]: %s\n",
83 		   pci_device->vendor, pci_device->product,
84 		   pci_device->sub_vendor, pci_device->sub_product,
85 		   pci_device->dev_info->class_name);
86 
87 	moreprintf(" Vendor Name      : %s\n",
88 		   pci_device->dev_info->vendor_name);
89 	moreprintf(" Product Name     : %s\n",
90 		   pci_device->dev_info->product_name);
91 	moreprintf(" PCI bus position : %02x:%02x.%01x\n", __pci_bus,
92 		   __pci_slot, __pci_func);
93 	moreprintf(" Kernel modules   : %s\n\n", kernel_modules);
94     }
95 }
96 
main(int argc,char * argv[])97 int main(int argc, char *argv[])
98 {
99     struct pci_domain *pci_domain;
100     int return_code = 0;
101     int nb_pci_devices = 0;
102 
103     (void)argc;
104     (void)argv;
105 
106     /* Scanning to detect pci buses and devices */
107     printf("PCI: Scanning PCI BUS\n");
108     pci_domain = pci_scan();
109     if (!pci_domain) {
110 	printf("PCI: no devices found!\n");
111 	return 1;
112     }
113 
114     struct pci_device *pci_device;
115     for_each_pci_func(pci_device, pci_domain) {
116 	nb_pci_devices++;
117     }
118 
119     printf("PCI: %d PCI devices found\n", nb_pci_devices);
120 
121     printf("PCI: Looking for device name\n");
122     /* Assigning product & vendor name for each device */
123     return_code = get_name_from_pci_ids(pci_domain, "pci.ids");
124     if (return_code == -ENOPCIIDS) {
125 	printf("PCI: ERROR !\n");
126 	printf("PCI: Unable to open pci.ids file in current directory.\n");
127 	printf("PCI: PCI Device names can't be computed.\n");
128     }
129 
130     printf("PCI: Resolving class names\n");
131     /* Assigning class name for each device */
132     return_code = get_class_name_from_pci_ids(pci_domain, "pci.ids");
133     if (return_code == -ENOPCIIDS) {
134 	printf("PCI: ERROR !\n");
135 	printf("PCI: Unable to open pci.ids file in current directory.\n");
136 	printf("PCI: PCI class names can't be computed.\n");
137     }
138 
139     printf("PCI: Looking for Kernel modules\n");
140     /* Detecting which kernel module should match each device */
141     return_code = get_module_name_from_pcimap(pci_domain, "modules.pcimap");
142     if (return_code == -ENOMODULESPCIMAP) {
143 	printf("PCI: ERROR !\n");
144 	printf("PCI: Unable to open modules.pcimap file in current directory.\n");
145 	printf("PCI: Kernel Module names can't be computed.\n");
146     }
147 
148     /* display the pci devices we found */
149     display_pci_devices(pci_domain);
150     return 0;
151 }
152