1/* ----------------------------------------------------------------------- *
2 *
3 *   Copyright 2009 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#include "hdt-menu.h"
30
31/* Dynamic submenu for pci devices */
32static void compute_pci_device(struct s_my_menu *menu,
33			       struct pci_device *pci_device,
34			       int pci_bus, int pci_slot, int pci_func,
35			       struct s_hardware *hardware)
36{
37    char buffer[56];
38    char statbuffer[STATLEN];
39    char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
40			MAX_KERNEL_MODULES_PER_PCI_DEVICE];
41
42    menu->menu = add_menu(" Details ", -1);
43    menu->items_count = 0;
44    set_menu_pos(5, 17);
45
46    snprintf(buffer, sizeof buffer, "Vendor  : %s",
47	     pci_device->dev_info->vendor_name);
48    snprintf(statbuffer, sizeof statbuffer, "Vendor Name: %s",
49	     pci_device->dev_info->vendor_name);
50    add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
51    menu->items_count++;
52
53    snprintf(buffer, sizeof buffer, "Product : %s",
54	     pci_device->dev_info->product_name);
55    snprintf(statbuffer, sizeof statbuffer, "Product Name  %s",
56	     pci_device->dev_info->product_name);
57    add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
58    menu->items_count++;
59
60    snprintf(buffer, sizeof buffer, "Class   : %s",
61	     pci_device->dev_info->class_name);
62    snprintf(statbuffer, sizeof statbuffer, "Class Name: %s",
63	     pci_device->dev_info->class_name);
64    add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
65    menu->items_count++;
66
67    snprintf(buffer, sizeof buffer, "Location: %02x:%02x.%01x", pci_bus,
68	     pci_slot, pci_func);
69    snprintf(statbuffer, sizeof statbuffer,
70	     "Location on the PCI Bus: %02x:%02x.%01x", pci_bus, pci_slot,
71	     pci_func);
72    add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
73    menu->items_count++;
74
75    snprintf(buffer, sizeof buffer, "PCI ID  : %04x:%04x[%04x:%04x]",
76	     pci_device->vendor, pci_device->product,
77	     pci_device->sub_vendor, pci_device->sub_product);
78    snprintf(statbuffer, sizeof statbuffer,
79	     "vendor:product[sub_vendor:sub_product] : %04x:%04x[%04x:%04x]",
80	     pci_device->vendor, pci_device->product,
81	     pci_device->sub_vendor, pci_device->sub_product);
82    add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
83    menu->items_count++;
84
85    if ((pci_device->dev_info->irq > 0) && (pci_device->dev_info->irq < 255)) {
86	snprintf(buffer, sizeof buffer, "IRQ     : %d",
87		 pci_device->dev_info->irq);
88	snprintf(statbuffer, sizeof statbuffer, "IRQ : %d",
89		 pci_device->dev_info->irq);
90	add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
91	menu->items_count++;
92    }
93
94    snprintf(buffer, sizeof buffer, "Latency : %d",
95	     pci_device->dev_info->latency);
96    snprintf(statbuffer, sizeof statbuffer, "Latency : %d",
97	     pci_device->dev_info->latency);
98    add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
99    menu->items_count++;
100
101    memset(kernel_modules, 0, sizeof(kernel_modules));
102
103    if (pci_device->dev_info->linux_kernel_module_count > 1) {
104	for (int i = 0;
105	     i < pci_device->dev_info->linux_kernel_module_count; i++) {
106	    if (i > 0) {
107		strncat(kernel_modules, " | ", 3);
108	    }
109	    strncat(kernel_modules,
110		    pci_device->dev_info->linux_kernel_module[i],
111		    LINUX_KERNEL_MODULE_SIZE - 1);
112	}
113	snprintf(buffer, sizeof buffer, "Modules : %s", kernel_modules);
114	snprintf(statbuffer, sizeof statbuffer, "Kernel Modules: %s",
115		 kernel_modules);
116    } else {
117	snprintf(buffer, sizeof buffer, "Module  : %s",
118		 pci_device->dev_info->linux_kernel_module[0]);
119	snprintf(statbuffer, sizeof statbuffer, "Kernel Module: %s",
120		 pci_device->dev_info->linux_kernel_module[0]);
121    }
122    add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
123    menu->items_count++;
124
125    if (hardware->is_pxe_valid == true) {
126	if ((hardware->pxe.pci_device != NULL)
127	    && (hardware->pxe.pci_device == pci_device)) {
128
129	    snprintf(buffer, sizeof buffer, "MAC Addr: %s",
130		     hardware->pxe.mac_addr);
131	    snprintf(statbuffer, sizeof statbuffer, "MAC Address : %s",
132		     hardware->pxe.mac_addr);
133	    add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
134	    menu->items_count++;
135
136	    snprintf(buffer, sizeof buffer, "PXE     : %s",
137		     "Current Boot device");
138	    snprintf(statbuffer, sizeof statbuffer, "PXE : %s",
139		     "Current Boot device");
140	    add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
141	    menu->items_count++;
142	}
143    }
144}
145
146/* Main PCI menu */
147int compute_PCI(struct s_hdt_menu *hdt_menu, struct s_hardware *hardware)
148{
149    int i = 0;
150    char menuname[255][MENULEN + 1];
151    char infobar[255][STATLEN + 1];
152    struct pci_device *pci_device;
153    char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
154			MAX_KERNEL_MODULES_PER_PCI_DEVICE];
155
156    /* For every detected pci device, compute its submenu */
157    for_each_pci_func(pci_device, hardware->pci_domain) {
158	memset(kernel_modules, 0, sizeof kernel_modules);
159	for (int kmod = 0;
160	     kmod < pci_device->dev_info->linux_kernel_module_count; kmod++) {
161	    if (kmod > 0) {
162		strncat(kernel_modules, " | ", 3);
163	    }
164	    strncat(kernel_modules,
165		    pci_device->dev_info->linux_kernel_module[kmod],
166		    LINUX_KERNEL_MODULE_SIZE - 1);
167	}
168	if (pci_device->dev_info->linux_kernel_module_count == 0)
169	    strlcpy(kernel_modules, "unknown", 7);
170
171	compute_pci_device(&(hdt_menu->pci_sub_menu[i]), pci_device,
172			   __pci_bus, __pci_slot, __pci_func, hardware);
173	snprintf(menuname[i], 59, "%s|%s",
174		 pci_device->dev_info->vendor_name,
175		 pci_device->dev_info->product_name);
176	snprintf(infobar[i], STATLEN,
177		 "%02x:%02x.%01x # %s # ID:%04x:%04x[%04x:%04x] # Kmod:%s",
178		 __pci_bus, __pci_slot, __pci_func,
179		 pci_device->dev_info->class_name, pci_device->vendor,
180		 pci_device->product, pci_device->sub_vendor,
181		 pci_device->sub_product, kernel_modules);
182	i++;
183    }
184
185    hdt_menu->pci_menu.menu = add_menu(" PCI Devices ", -1);
186    hdt_menu->pci_menu.items_count = 0;
187    if (hardware->pci_ids_return_code == -ENOPCIIDS) {
188	add_item("The pci.ids file is missing", "Missing pci.ids file",
189		 OPT_INACTIVE, NULL, 0);
190	add_item("PCI Device names  can't be computed.",
191		 "Missing pci.ids file", OPT_INACTIVE, NULL, 0);
192	add_item("Please put one in same dir as hdt",
193		 "Missing pci.ids file", OPT_INACTIVE, NULL, 0);
194	add_item("", "", OPT_SEP, "", 0);
195    }
196    for (int j = 0; j < i; j++) {
197	add_item(menuname[j], infobar[j], OPT_SUBMENU, NULL,
198		 hdt_menu->pci_sub_menu[j].menu);
199	hdt_menu->pci_menu.items_count++;
200    }
201    printf("MENU: PCI menu done (%d items)\n", hdt_menu->pci_menu.items_count);
202    return 0;
203}
204