1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Utility                                                             */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _ux_utility_pci_class_scan                          PORTABLE C      */
35 /*                                                           6.1          */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function scans the PCI bus from a certain position for a       */
43 /*    specific PCI class.                                                 */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    pci_class                             PCI class requested           */
48 /*    bus_number                            PCI bus number                */
49 /*    device_number                         Device number                 */
50 /*    function_number                       Function number               */
51 /*    current_bus_number                    Current bus number            */
52 /*    current_device_number                 Current device number         */
53 /*    current_function_number               Current function number       */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    32-bit value                                                        */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_utility_pci_read                  PCI read utility              */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    USBX Components                                                     */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_ux_utility_pci_class_scan(ULONG pci_class,ULONG bus_number,ULONG device_number,ULONG function_number,ULONG * current_bus_number,ULONG * current_device_number,ULONG * current_function_number)76 ULONG  _ux_utility_pci_class_scan(ULONG pci_class, ULONG bus_number, ULONG device_number,
77                    ULONG function_number, ULONG *current_bus_number,
78                    ULONG *current_device_number, ULONG *current_function_number)
79 {
80 
81 ULONG   bus_number_index;
82 ULONG   device_number_index;
83 ULONG   function_number_index;
84 ULONG   value;
85 ULONG   current_pci_class;
86 
87 
88     /* Scan all bus.  */
89     for (bus_number_index = bus_number; bus_number_index <= UX_PCI_NB_BUS; bus_number_index++)
90     {
91 
92         /* Scan all devices.  */
93         for(device_number_index = device_number;device_number_index <= UX_PCI_NB_DEVICE; device_number_index++)
94         {
95 
96             /* Scan all functions.  */
97             for(function_number_index = function_number; function_number_index <= UX_PCI_NB_FUNCTIONS; function_number_index++)
98             {
99 
100                 /* Reset all PCI address for next loop.  */
101                 function_number = 0;
102                 device_number =   0;
103                 bus_number =      0;
104 
105                 /* Read the PCI class bus/device/function.  */
106                 value =  _ux_utility_pci_read(bus_number_index, device_number_index, function_number_index,
107                                     UX_PCI_CFG_REVISION,32);
108 
109                 /* Isolate the class code which is in the upper 3 bytes.  */
110                 current_pci_class =  (value >> 8) & 0x00ffffff;
111 
112                 /* Do we have a match with the demanded class?  */
113                 if(current_pci_class == pci_class)
114                 {
115 
116                     /* Return the position of this device on the PCI */
117                     *current_bus_number =       bus_number_index;
118                     *current_device_number =    device_number_index;
119                     *current_function_number =  function_number_index;
120 
121                     /* Return success!  */
122                     return(UX_SUCCESS);
123                 }
124             }
125         }
126     }
127 
128     /* Return an error since we didn't find anything.  */
129     return(UX_ERROR);
130 }
131 
132