Update syslinux to 4.05 to support mic-0.15
[external/syslinux.git] / com32 / modules / pcitest.c
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
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
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     openconsole(&dev_stdcon_r, &dev_stdcon_w);
107
108     /* Scanning to detect pci buses and devices */
109     printf("PCI: Scanning PCI BUS\n");
110     pci_domain = pci_scan();
111     if (!pci_domain) {
112         printf("PCI: no devices found!\n");
113         return 1;
114     }
115
116     struct pci_device *pci_device;
117     for_each_pci_func(pci_device, pci_domain) {
118         nb_pci_devices++;
119     }
120
121     printf("PCI: %d PCI devices found\n", nb_pci_devices);
122
123     printf("PCI: Looking for device name\n");
124     /* Assigning product & vendor name for each device */
125     return_code = get_name_from_pci_ids(pci_domain, "pci.ids");
126     if (return_code == -ENOPCIIDS) {
127         printf("PCI: ERROR !\n");
128         printf("PCI: Unable to open pci.ids file in current directory.\n");
129         printf("PCI: PCI Device names can't be computed.\n");
130     }
131
132     printf("PCI: Resolving class names\n");
133     /* Assigning class name for each device */
134     return_code = get_class_name_from_pci_ids(pci_domain, "pci.ids");
135     if (return_code == -ENOPCIIDS) {
136         printf("PCI: ERROR !\n");
137         printf("PCI: Unable to open pci.ids file in current directory.\n");
138         printf("PCI: PCI class names can't be computed.\n");
139     }
140
141     printf("PCI: Looking for Kernel modules\n");
142     /* Detecting which kernel module should match each device */
143     return_code = get_module_name_from_pcimap(pci_domain, "modules.pcimap");
144     if (return_code == -ENOMODULESPCIMAP) {
145         printf("PCI: ERROR !\n");
146         printf("PCI: Unable to open modules.pcimap file in current directory.\n");
147         printf("PCI: Kernel Module names can't be computed.\n");
148     }
149
150     /* display the pci devices we found */
151     display_pci_devices(pci_domain);
152     return 0;
153 }