xref: /reactos/drivers/bus/pcix/intrface/routintf.c (revision 40462c92)
1 /*
2  * PROJECT:         ReactOS PCI Bus Driver
3  * LICENSE:         BSD - See COPYING.ARM in the top level directory
4  * FILE:            drivers/bus/pci/intrface/routinf.c
5  * PURPOSE:         Routing Interface
6  * PROGRAMMERS:     ReactOS Portable Systems Group
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include <pci.h>
12 
13 #define NDEBUG
14 #include <debug.h>
15 
16 /* GLOBALS ********************************************************************/
17 
18 PPCI_LEGACY_DEVICE PciLegacyDeviceHead;
19 
20 PCI_INTERFACE PciRoutingInterface =
21 {
22     &GUID_INT_ROUTE_INTERFACE_STANDARD,
23     sizeof(INT_ROUTE_INTERFACE_STANDARD),
24     PCI_INT_ROUTE_INTRF_STANDARD_VER,
25     PCI_INT_ROUTE_INTRF_STANDARD_VER,
26     PCI_INTERFACE_FDO,
27     0,
28     PciInterface_IntRouteHandler,
29     routeintrf_Constructor,
30     routeintrf_Initializer
31 };
32 
33 /* FUNCTIONS ******************************************************************/
34 
35 NTSTATUS
36 NTAPI
37 routeintrf_Initializer(IN PVOID Instance)
38 {
39     UNREFERENCED_PARAMETER(Instance);
40     /* PnP Interfaces don't get Initialized */
41     ASSERTMSG("PCI routeintrf_Initializer, unexpected call.\n", FALSE);
42     return STATUS_UNSUCCESSFUL;
43 }
44 
45 NTSTATUS
46 NTAPI
47 routeintrf_Constructor(IN PVOID DeviceExtension,
48                        IN PVOID Instance,
49                        IN PVOID InterfaceData,
50                        IN USHORT Version,
51                        IN USHORT Size,
52                        IN PINTERFACE Interface)
53 {
54     UNREFERENCED_PARAMETER(DeviceExtension);
55     UNREFERENCED_PARAMETER(Instance);
56     UNREFERENCED_PARAMETER(InterfaceData);
57     UNREFERENCED_PARAMETER(Size);
58     UNREFERENCED_PARAMETER(Interface);
59 
60     /* Only version 1 is supported */
61     if (Version != PCI_INT_ROUTE_INTRF_STANDARD_VER) return STATUS_NOINTERFACE;
62 
63     /* Not yet implemented */
64     UNIMPLEMENTED_DBGBREAK();
65     return STATUS_NOT_IMPLEMENTED;
66 }
67 
68 NTSTATUS
69 NTAPI
70 PciCacheLegacyDeviceRouting(IN PDEVICE_OBJECT DeviceObject,
71                             IN ULONG BusNumber,
72                             IN ULONG SlotNumber,
73                             IN UCHAR InterruptLine,
74                             IN UCHAR InterruptPin,
75                             IN UCHAR BaseClass,
76                             IN UCHAR SubClass,
77                             IN PDEVICE_OBJECT PhysicalDeviceObject,
78                             IN PPCI_PDO_EXTENSION PdoExtension,
79                             OUT PDEVICE_OBJECT *pFoundDeviceObject)
80 {
81     PPCI_LEGACY_DEVICE *Link;
82     PPCI_LEGACY_DEVICE LegacyDevice;
83     PDEVICE_OBJECT FoundDeviceObject;
84     PAGED_CODE();
85 
86     /* Scan current registered devices */
87     LegacyDevice = PciLegacyDeviceHead;
88     Link = &PciLegacyDeviceHead;
89     while (LegacyDevice)
90     {
91         /* Find a match */
92         if ((BusNumber == LegacyDevice->BusNumber) &&
93             (SlotNumber == LegacyDevice->SlotNumber))
94         {
95             /* We already know about this routing */
96             break;
97         }
98 
99         /* We know about device already, but for a different location */
100         if (LegacyDevice->DeviceObject == DeviceObject)
101         {
102             /* Free the existing structure, move to the next one */
103             *Link = LegacyDevice->Next;
104             ExFreePoolWithTag(LegacyDevice, 0);
105             LegacyDevice = *Link;
106         }
107         else
108         {
109             /* Keep going */
110             Link = &LegacyDevice->Next;
111             LegacyDevice = LegacyDevice->Next;
112         }
113     }
114 
115     /* Did we find a match? */
116     if (!LegacyDevice)
117     {
118         /* Allocate a new cache structure */
119         LegacyDevice = ExAllocatePoolWithTag(PagedPool,
120                                              sizeof(PCI_LEGACY_DEVICE),
121                                              'PciR');
122         if (!LegacyDevice) return STATUS_INSUFFICIENT_RESOURCES;
123 
124         /* Save all the data in it */
125         RtlZeroMemory(LegacyDevice, sizeof(PCI_LEGACY_DEVICE));
126         LegacyDevice->BusNumber = BusNumber;
127         LegacyDevice->SlotNumber = SlotNumber;
128         LegacyDevice->InterruptLine = InterruptLine;
129         LegacyDevice->InterruptPin = InterruptPin;
130         LegacyDevice->BaseClass = BaseClass;
131         LegacyDevice->SubClass = SubClass;
132         LegacyDevice->PhysicalDeviceObject = PhysicalDeviceObject;
133         LegacyDevice->DeviceObject = DeviceObject;
134         LegacyDevice->PdoExtension = PdoExtension;
135 
136         /* Link it in the list */
137         LegacyDevice->Next = PciLegacyDeviceHead;
138         PciLegacyDeviceHead = LegacyDevice;
139     }
140 
141     /* Check if we found, or created, a matching caching structure */
142     FoundDeviceObject = LegacyDevice->DeviceObject;
143     if (FoundDeviceObject == DeviceObject)
144     {
145         /* Return the device object and success */
146         if (pFoundDeviceObject) *pFoundDeviceObject = DeviceObject;
147         return STATUS_SUCCESS;
148     }
149 
150     /* Otherwise, this is a new device object for this location */
151     LegacyDevice->DeviceObject = DeviceObject;
152     if (pFoundDeviceObject) *pFoundDeviceObject = FoundDeviceObject;
153     return STATUS_SUCCESS;
154 }
155 
156 /* EOF */
157