c1d711b086fd02d5ac13036dff6ba10284b0ae5e
[reactos.git] / reactos / drivers / bus / pcix / pci / id.c
1 /*
2 * PROJECT: ReactOS PCI Bus Driver
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: drivers/bus/pci/pci/id.c
5 * PURPOSE: PCI Device Identification
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <pci.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 /* GLOBALS ********************************************************************/
16
17 /* FUNCTIONS ******************************************************************/
18
19 PWCHAR
20 NTAPI
21 PciGetDescriptionMessage(IN ULONG Identifier,
22 OUT PULONG Length)
23 {
24 PMESSAGE_RESOURCE_ENTRY Entry;
25 ULONG TextLength;
26 PWCHAR Description, Buffer;
27 ANSI_STRING MessageString;
28 UNICODE_STRING UnicodeString;
29 NTSTATUS Status;
30
31 /* Find the message identifier in the message table */
32 MessageString.Buffer = NULL;
33 Status = RtlFindMessage(PciDriverObject->DriverStart,
34 11, // RT_MESSAGETABLE
35 LANG_NEUTRAL,
36 Identifier,
37 &Entry);
38 if (!NT_SUCCESS(Status)) return NULL;
39
40 /* Check if the resource data is Unicode or ANSI */
41 if (Entry->Flags & MESSAGE_RESOURCE_UNICODE)
42 {
43 /* Subtract one space for the end-of-message terminator */
44 TextLength = Entry->Length -
45 FIELD_OFFSET(MESSAGE_RESOURCE_ENTRY, Text) -
46 sizeof(WCHAR);
47
48 /* Grab the text */
49 Description = (PWCHAR)Entry->Text;
50
51 /* Validate valid message length, ending with a newline character */
52 ASSERT(TextLength > 1);
53 ASSERT(Description[TextLength / sizeof(WCHAR) == L'\n']);
54
55 /* Allocate the buffer to hold the message string */
56 Buffer = ExAllocatePoolWithTag(PagedPool, TextLength, 'BicP');
57 if (!Buffer) return NULL;
58
59 /* Copy the message, minus the newline character, and terminate it */
60 RtlCopyMemory(Buffer, Entry->Text, TextLength - 1);
61 Buffer[TextLength / sizeof(WCHAR)] = UNICODE_NULL;
62
63 /* Return the length to the caller */
64 if (Length) *Length = UnicodeString.Length;
65 }
66 else
67 {
68 /* Initialize the entry as a string */
69 RtlInitAnsiString(&MessageString, (PCHAR)Entry->Text);
70
71 /* Remove the newline character */
72 MessageString.Length -= sizeof(CHAR);
73
74 /* Convert it to Unicode */
75 RtlAnsiStringToUnicodeString(&UnicodeString, &MessageString, TRUE);
76 Buffer = UnicodeString.Buffer;
77
78 /* Return the length to the caller */
79 if (Length) *Length = UnicodeString.Length;
80 }
81
82 /* Return the message buffer to the caller */
83 return Buffer;
84 }
85
86 PWCHAR
87 NTAPI
88 PciGetDeviceDescriptionMessage(IN UCHAR BaseClass,
89 IN UCHAR SubClass)
90 {
91 PWCHAR Message;
92 ULONG Identifier;
93
94 /* The message identifier in the table is encoded based on the PCI class */
95 Identifier = (BaseClass << 8) | SubClass;
96
97 /* Go grab the description message for this device */
98 Message = PciGetDescriptionMessage(Identifier, NULL);
99 if (!Message)
100 {
101 /* It wasn't found, allocate a buffer for a generic description */
102 Message = ExAllocatePoolWithTag(PagedPool, sizeof(L"PCI Device"), 'bicP');
103 if (Message) RtlCopyMemory(Message, L"PCI Device", sizeof(L"PCI Device"));
104 }
105
106 /* Return the description message */
107 return Message;
108 }
109
110 /* EOF */