Makefiles: try to make "make install" do the right thing
[profile/ivi/syslinux.git] / com32 / hdt / hdt-cli-pci.c
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 <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include "hdt-cli.h"
34 #include "hdt-common.h"
35
36 void show_pci_device(struct s_hardware *hardware, const char *item)
37 {
38   int i = 0;
39   struct pci_device *pci_device = NULL, *temp_pci_device;
40   long pcidev = strtol(item, (char **)NULL, 10);
41   bool nopciids = false;
42   bool nomodulespcimap = false;
43   char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
44           MAX_KERNEL_MODULES_PER_PCI_DEVICE];
45   int bus = 0, slot = 0, func = 0;
46
47   if (errno == ERANGE) {
48     printf("This PCI device number is incorrect\n");
49     return;
50   }
51   if ((pcidev > hardware->nb_pci_devices) || (pcidev <= 0)) {
52     printf("PCI device %d  doesn't exists\n", pcidev);
53     return;
54   }
55   if (hardware->pci_ids_return_code == -ENOPCIIDS) {
56     nopciids = true;
57   }
58   if (hardware->modules_pcimap_return_code == -ENOMODULESPCIMAP) {
59     nomodulespcimap = true;
60   }
61
62   for_each_pci_func(temp_pci_device, hardware->pci_domain) {
63     i++;
64     if (i == pcidev) {
65       bus = __pci_bus;
66       slot = __pci_slot;
67       func = __pci_func;
68       pci_device = temp_pci_device;
69     }
70   }
71
72   if (pci_device == NULL) {
73     printf("We were enabled to find PCI device %d\n", pcidev);
74     return;
75   }
76
77   memset(kernel_modules, 0, sizeof kernel_modules);
78   for (int kmod = 0;
79        kmod < pci_device->dev_info->linux_kernel_module_count; kmod++) {
80     if (kmod > 0) {
81       strncat(kernel_modules, " | ", 3);
82     }
83     strncat(kernel_modules,
84       pci_device->dev_info->linux_kernel_module[kmod],
85       LINUX_KERNEL_MODULE_SIZE - 1);
86   }
87   if (pci_device->dev_info->linux_kernel_module_count == 0)
88     strlcpy(kernel_modules, "unknown", 7);
89
90   clear_screen();
91   printf("PCI Device %d\n", pcidev);
92
93   if (nopciids == false) {
94     more_printf("Vendor Name   : %s\n",
95           pci_device->dev_info->vendor_name);
96     more_printf("Product Name  : %s\n",
97           pci_device->dev_info->product_name);
98     more_printf("Class Name    : %s\n",
99           pci_device->dev_info->class_name);
100   }
101
102   if (nomodulespcimap == false) {
103     more_printf("Kernel module : %s\n", kernel_modules);
104   }
105
106   more_printf("Vendor ID     : %04x\n", pci_device->vendor);
107   more_printf("Product ID    : %04x\n", pci_device->product);
108   more_printf("SubVendor ID  : %04x\n", pci_device->sub_vendor);
109   more_printf("SubProduct ID : %04x\n", pci_device->sub_product);
110   more_printf("Class ID      : %02x.%02x.%02x\n", pci_device->class[2],
111         pci_device->class[1], pci_device->class[0]);
112   more_printf("Revision      : %02x\n", pci_device->revision);
113   if ((pci_device->dev_info->irq > 0)
114       && (pci_device->dev_info->irq < 255))
115     more_printf("IRQ           : %0d\n", pci_device->dev_info->irq);
116   more_printf("Latency       : %0d\n",pci_device->dev_info->latency);
117   more_printf("PCI Bus       : %02d\n", bus);
118   more_printf("PCI Slot      : %02d\n", slot);
119   more_printf("PCI Func      : %02d\n", func);
120
121   if (hardware->is_pxe_valid == true) {
122     more_printf("Mac Address   : %s\n", hardware->pxe.mac_addr);
123     if ((hardware->pxe.pci_device != NULL)
124         && (hardware->pxe.pci_device == pci_device))
125       more_printf("PXE           : Current boot device\n",
126             func);
127   }
128 }
129
130 static void show_pci_devices(struct s_hardware *hardware)
131 {
132   int i = 1;
133   struct pci_device *pci_device;
134   char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
135           MAX_KERNEL_MODULES_PER_PCI_DEVICE];
136   bool nopciids = false;
137   bool nomodulespcimap = false;
138   char first_line[81];
139   char second_line[81];
140
141   clear_screen();
142   more_printf("%d PCI devices detected\n", hardware->nb_pci_devices);
143
144   if (hardware->pci_ids_return_code == -ENOPCIIDS) {
145     nopciids = true;
146   }
147   if (hardware->modules_pcimap_return_code == -ENOMODULESPCIMAP) {
148     nomodulespcimap = true;
149   }
150
151   /* For every detected pci device, compute its submenu */
152   for_each_pci_func(pci_device, hardware->pci_domain) {
153     memset(kernel_modules, 0, sizeof kernel_modules);
154     for (int kmod = 0;
155          kmod < pci_device->dev_info->linux_kernel_module_count;
156          kmod++) {
157       if (kmod > 0) {
158         strncat(kernel_modules, " | ", 3);
159       }
160       strncat(kernel_modules,
161         pci_device->dev_info->linux_kernel_module[kmod],
162         LINUX_KERNEL_MODULE_SIZE - 1);
163     }
164     if (pci_device->dev_info->linux_kernel_module_count == 0)
165       strlcpy(kernel_modules, "unknown", 7);
166
167     if (nopciids == false) {
168       snprintf(first_line, sizeof(first_line),
169          "%02d: %s %s \n", i,
170          pci_device->dev_info->vendor_name,
171          pci_device->dev_info->product_name);
172       if (nomodulespcimap == false)
173         snprintf(second_line, sizeof(second_line),
174            "    # %-25s # Kmod: %s\n",
175            pci_device->dev_info->class_name,
176            kernel_modules);
177       else
178         snprintf(second_line, sizeof(second_line),
179            "    # %-25s # ID:%04x:%04x[%04x:%04x]\n",
180            pci_device->dev_info->class_name,
181            pci_device->vendor,
182            pci_device->product,
183            pci_device->sub_vendor,
184            pci_device->sub_product);
185
186       more_printf(first_line);
187       more_printf(second_line);
188       more_printf("\n");
189     } else if (nopciids == true) {
190       if (nomodulespcimap == true) {
191         more_printf("%02d: %04x:%04x [%04x:%04x] \n",
192               i, pci_device->vendor,
193               pci_device->product,
194               pci_device->sub_vendor,
195               pci_device->sub_product);
196       } else {
197         more_printf
198             ("%02d: %04x:%04x [%04x:%04x] Kmod:%s\n", i,
199              pci_device->vendor, pci_device->product,
200              pci_device->sub_vendor,
201              pci_device->sub_product, kernel_modules,
202              pci_device->sub_product, kernel_modules);
203       }
204     }
205     i++;
206   }
207
208 }
209
210 static void show_pci_irq(struct s_hardware *hardware)
211 {
212   struct pci_device *pci_device;
213   bool nopciids = false;
214
215   clear_screen();
216   more_printf("%d PCI devices detected\n", hardware->nb_pci_devices);
217   more_printf("IRQ : product\n");
218   more_printf("-------------\n");
219
220   if (hardware->pci_ids_return_code == -ENOPCIIDS) {
221     nopciids = true;
222   }
223
224   /* For every detected pci device, compute its submenu */
225   for_each_pci_func(pci_device, hardware->pci_domain) {
226     /* Only display valid IRQs */
227     if ((pci_device->dev_info->irq > 0) && (pci_device->dev_info->irq < 255)) {
228       if (nopciids == false) {
229         more_printf("%02d  : %s %s \n",
230               pci_device->dev_info->irq,
231               pci_device->dev_info->vendor_name,
232               pci_device->dev_info->product_name);
233       } else {
234         more_printf("%02d  : %04x:%04x [%04x:%04x] \n",
235               pci_device->dev_info->irq, pci_device->vendor,
236               pci_device->product,
237               pci_device->sub_vendor,
238               pci_device->sub_product);
239       }
240     }
241   }
242 }
243
244 static void show_pci_help()
245 {
246   more_printf("Show supports the following commands : \n");
247   more_printf(" %s\n", CLI_SHOW_LIST);
248   more_printf(" %s <device_number>\n", CLI_PCI_DEVICE);
249   more_printf(" %s\n",CLI_IRQ);
250 }
251
252 static void pci_show(char *item, struct s_hardware *hardware)
253 {
254   if (!strncmp(item, CLI_SHOW_LIST, sizeof(CLI_SHOW_LIST) - 1)) {
255     show_pci_devices(hardware);
256     return;
257   }
258   if (!strncmp(item, CLI_IRQ, sizeof(CLI_IRQ) - 1)) {
259     show_pci_irq(hardware);
260     return;
261   }
262   if (!strncmp(item, CLI_PCI_DEVICE, sizeof(CLI_PCI_DEVICE) - 1)) {
263     show_pci_device(hardware, item + sizeof(CLI_PCI_DEVICE) - 1);
264     return;
265   }
266   show_pci_help();
267 }
268
269 void handle_pci_commands(char *cli_line, struct s_hardware *hardware)
270 {
271   if (!strncmp(cli_line, CLI_SHOW, sizeof(CLI_SHOW) - 1)) {
272     pci_show(strstr(cli_line, "show") + sizeof(CLI_SHOW), hardware);
273     return;
274   }
275 }
276
277 void cli_detect_pci(struct s_hardware *hardware)
278 {
279   bool error = false;
280   if (hardware->pci_detection == false) {
281     detect_pci(hardware);
282     if (hardware->pci_ids_return_code == -ENOPCIIDS) {
283       more_printf
284           ("The pci.ids file is missing, device names can't be computed.\n");
285       more_printf("Please put one in same dir as hdt\n");
286       error = true;
287     }
288     if (hardware->modules_pcimap_return_code == -ENOMODULESPCIMAP) {
289       more_printf
290           ("The modules.pcimap file is missing, device names can't be computed.\n");
291       more_printf("Please put one in same dir as hdt\n");
292       error = true;
293     }
294     if (error == true) {
295       char tempbuf[10];
296       printf("Press enter to continue\n");
297       fgets(tempbuf, sizeof(tempbuf), stdin);
298     }
299   }
300 }
301
302 void main_show_pci(struct s_hardware *hardware)
303 {
304   cli_detect_pci(hardware);
305
306   more_printf("PCI\n");
307   more_printf(" NB Devices   : %d\n", hardware->nb_pci_devices);
308 }