xref: /haiku/headers/private/shared/pci-utils.h (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2006, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  * 	Jérôme Duval
7  */
8 
9 #include <stdio.h>
10 
11 static void
12 get_class_info(uint8 pci_class_base_id, uint8 pci_class_sub_id, uint8 pci_class_api_id, char *classInfo, size_t size)
13 {
14 	if (pci_class_base_id == 0x80)
15 		snprintf(classInfo, size, " (Other)");
16 	else {
17 		PCI_CLASSCODETABLE *foundItem = NULL;
18 		int i;
19 		for (i = 0; i < (int)PCI_CLASSCODETABLE_LEN; i++) {
20 			if ((pci_class_base_id == PciClassCodeTable[i].BaseClass)
21 				&& (pci_class_sub_id == PciClassCodeTable[i].SubClass)) {
22 				foundItem = &PciClassCodeTable[i];
23 				if (pci_class_api_id == PciClassCodeTable[i].ProgIf)
24 					break;
25 			}
26 		}
27 		if (foundItem) {
28 			if (pci_class_sub_id != 0x80)
29 				snprintf(classInfo, size, "%s (%s%s%s)", foundItem->BaseDesc, foundItem->SubDesc,
30 					(foundItem->ProgDesc && strcmp("", foundItem->ProgDesc)) ? ", " : "", foundItem->ProgDesc);
31 			else
32 				snprintf(classInfo, size, "%s", foundItem->BaseDesc);
33 		} else
34 				snprintf(classInfo, size, "%s (%u:%u:%u)", "(Unknown)",
35 					pci_class_base_id, pci_class_sub_id, pci_class_api_id);
36 	}
37 }
38 
39 static void
40 get_vendor_info(uint16 vendorID, const char **venShort, const char **venFull)
41 {
42 	int i;
43 	for (i = 0; i < (int)PCI_VENTABLE_LEN; i++) {
44 		if (PciVenTable[i].VenId == vendorID) {
45 			if (PciVenTable[i].VenShort && PciVenTable[i].VenFull
46 				&& 0 == strcmp(PciVenTable[i].VenShort, PciVenTable[i].VenFull)) {
47 				*venShort = PciVenTable[i].VenShort[0] ? PciVenTable[i].VenShort : NULL;
48 				*venFull = NULL;
49 			} else {
50 				*venShort = PciVenTable[i].VenShort && PciVenTable[i].VenShort[0] ? PciVenTable[i].VenShort : NULL;
51 				*venFull = PciVenTable[i].VenFull && PciVenTable[i].VenFull[0] ? PciVenTable[i].VenFull : NULL;
52 			}
53 			return;
54 		}
55 	}
56 	*venShort = NULL;
57 	*venFull = NULL;
58 }
59 
60 
61 static void
62 get_device_info(uint16 vendorID, uint16 deviceID,
63 	uint16 subvendorID, uint16 subsystemID, const char **devShort, const char **devFull)
64 {
65 	int i;
66 	*devShort = NULL;
67 	*devFull = NULL;
68 
69 	// search for the device
70 	for (i = 0; i < (int)PCI_DEVTABLE_LEN; i++) {
71 		if (PciDevTable[i].VenId == vendorID && PciDevTable[i].DevId == deviceID ) {
72 			*devShort = PciDevTable[i].ChipDesc && PciDevTable[i].ChipDesc[0] ? PciDevTable[i].ChipDesc : NULL;
73 			if (PciDevTable[i].SubVenId == 0 || PciDevTable[i].SubDevId == 0)
74 				i++;
75 			break;
76 		}
77 	}
78 
79 	// search for the subsystem eventually
80 	for (; i < (int)PCI_DEVTABLE_LEN; i++) {
81 		if (PciDevTable[i].VenId != vendorID || PciDevTable[i].DevId != deviceID)
82 			break;
83 		if (PciDevTable[i].SubVenId == subvendorID && PciDevTable[i].SubDevId == subsystemID) {
84 			*devFull = PciDevTable[i].ChipDesc && PciDevTable[i].ChipDesc[0] ? PciDevTable[i].ChipDesc : NULL;
85 			break;
86 		}
87 	}
88 }
89