Path: blob/main/sys/contrib/dev/acpica/components/utilities/utids.c
48406 views
/******************************************************************************1*2* Module Name: utids - support for device IDs - HID, UID, CID, SUB, CLS3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#include <contrib/dev/acpica/include/acpi.h>152#include <contrib/dev/acpica/include/accommon.h>153#include <contrib/dev/acpica/include/acinterp.h>154155156#define _COMPONENT ACPI_UTILITIES157ACPI_MODULE_NAME ("utids")158159160/*******************************************************************************161*162* FUNCTION: AcpiUtExecute_HID163*164* PARAMETERS: DeviceNode - Node for the device165* ReturnId - Where the string HID is returned166*167* RETURN: Status168*169* DESCRIPTION: Executes the _HID control method that returns the hardware170* ID of the device. The HID is either an 32-bit encoded EISAID171* Integer or a String. A string is always returned. An EISAID172* is converted to a string.173*174* NOTE: Internal function, no parameter validation175*176******************************************************************************/177178ACPI_STATUS179AcpiUtExecute_HID (180ACPI_NAMESPACE_NODE *DeviceNode,181ACPI_PNP_DEVICE_ID **ReturnId)182{183ACPI_OPERAND_OBJECT *ObjDesc;184ACPI_PNP_DEVICE_ID *Hid;185UINT32 Length;186ACPI_STATUS Status;187188189ACPI_FUNCTION_TRACE (UtExecute_HID);190191192Status = AcpiUtEvaluateObject (DeviceNode, METHOD_NAME__HID,193ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING, &ObjDesc);194if (ACPI_FAILURE (Status))195{196return_ACPI_STATUS (Status);197}198199/* Get the size of the String to be returned, includes null terminator */200201if (ObjDesc->Common.Type == ACPI_TYPE_INTEGER)202{203Length = ACPI_EISAID_STRING_SIZE;204}205else206{207Length = ObjDesc->String.Length + 1;208}209210/* Allocate a buffer for the HID */211212Hid = ACPI_ALLOCATE_ZEROED (213sizeof (ACPI_PNP_DEVICE_ID) + (ACPI_SIZE) Length);214if (!Hid)215{216Status = AE_NO_MEMORY;217goto Cleanup;218}219220/* Area for the string starts after PNP_DEVICE_ID struct */221222Hid->String = ACPI_ADD_PTR (char, Hid, sizeof (ACPI_PNP_DEVICE_ID));223224/* Convert EISAID to a string or simply copy existing string */225226if (ObjDesc->Common.Type == ACPI_TYPE_INTEGER)227{228AcpiExEisaIdToString (Hid->String, ObjDesc->Integer.Value);229}230else231{232strcpy (Hid->String, ObjDesc->String.Pointer);233}234235Hid->Length = Length;236*ReturnId = Hid;237238239Cleanup:240241/* On exit, we must delete the return object */242243AcpiUtRemoveReference (ObjDesc);244return_ACPI_STATUS (Status);245}246247248/*******************************************************************************249*250* FUNCTION: AcpiUtExecute_UID251*252* PARAMETERS: DeviceNode - Node for the device253* ReturnId - Where the string UID is returned254*255* RETURN: Status256*257* DESCRIPTION: Executes the _UID control method that returns the unique258* ID of the device. The UID is either a 64-bit Integer (NOT an259* EISAID) or a string. Always returns a string. A 64-bit integer260* is converted to a decimal string.261*262* NOTE: Internal function, no parameter validation263*264******************************************************************************/265266ACPI_STATUS267AcpiUtExecute_UID (268ACPI_NAMESPACE_NODE *DeviceNode,269ACPI_PNP_DEVICE_ID **ReturnId)270{271ACPI_OPERAND_OBJECT *ObjDesc;272ACPI_PNP_DEVICE_ID *Uid;273UINT32 Length;274ACPI_STATUS Status;275276277ACPI_FUNCTION_TRACE (UtExecute_UID);278279280Status = AcpiUtEvaluateObject (DeviceNode, METHOD_NAME__UID,281ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING, &ObjDesc);282if (ACPI_FAILURE (Status))283{284return_ACPI_STATUS (Status);285}286287/* Get the size of the String to be returned, includes null terminator */288289if (ObjDesc->Common.Type == ACPI_TYPE_INTEGER)290{291Length = ACPI_MAX64_DECIMAL_DIGITS + 1;292}293else294{295Length = ObjDesc->String.Length + 1;296}297298/* Allocate a buffer for the UID */299300Uid = ACPI_ALLOCATE_ZEROED (301sizeof (ACPI_PNP_DEVICE_ID) + (ACPI_SIZE) Length);302if (!Uid)303{304Status = AE_NO_MEMORY;305goto Cleanup;306}307308/* Area for the string starts after PNP_DEVICE_ID struct */309310Uid->String = ACPI_ADD_PTR (char, Uid, sizeof (ACPI_PNP_DEVICE_ID));311312/* Convert an Integer to string, or just copy an existing string */313314if (ObjDesc->Common.Type == ACPI_TYPE_INTEGER)315{316AcpiExIntegerToString (Uid->String, ObjDesc->Integer.Value);317}318else319{320strcpy (Uid->String, ObjDesc->String.Pointer);321}322323Uid->Length = Length;324*ReturnId = Uid;325326327Cleanup:328329/* On exit, we must delete the return object */330331AcpiUtRemoveReference (ObjDesc);332return_ACPI_STATUS (Status);333}334335336/*******************************************************************************337*338* FUNCTION: AcpiUtExecute_CID339*340* PARAMETERS: DeviceNode - Node for the device341* ReturnCidList - Where the CID list is returned342*343* RETURN: Status, list of CID strings344*345* DESCRIPTION: Executes the _CID control method that returns one or more346* compatible hardware IDs for the device.347*348* NOTE: Internal function, no parameter validation349*350* A _CID method can return either a single compatible ID or a package of351* compatible IDs. Each compatible ID can be one of the following:352* 1) Integer (32 bit compressed EISA ID) or353* 2) String (PCI ID format, e.g. "PCI\VEN_vvvv&DEV_dddd&SUBSYS_ssssssss")354*355* The Integer CIDs are converted to string format by this function.356*357******************************************************************************/358359ACPI_STATUS360AcpiUtExecute_CID (361ACPI_NAMESPACE_NODE *DeviceNode,362ACPI_PNP_DEVICE_ID_LIST **ReturnCidList)363{364ACPI_OPERAND_OBJECT **CidObjects;365ACPI_OPERAND_OBJECT *ObjDesc;366ACPI_PNP_DEVICE_ID_LIST *CidList;367char *NextIdString;368UINT32 StringAreaSize;369UINT32 Length;370UINT32 CidListSize;371ACPI_STATUS Status;372UINT32 Count;373UINT32 i;374375376ACPI_FUNCTION_TRACE (UtExecute_CID);377378379/* Evaluate the _CID method for this device */380381Status = AcpiUtEvaluateObject (DeviceNode, METHOD_NAME__CID,382ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING | ACPI_BTYPE_PACKAGE,383&ObjDesc);384if (ACPI_FAILURE (Status))385{386return_ACPI_STATUS (Status);387}388389/*390* Get the count and size of the returned _CIDs. _CID can return either391* a Package of Integers/Strings or a single Integer or String.392* Note: This section also validates that all CID elements are of the393* correct type (Integer or String).394*/395if (ObjDesc->Common.Type == ACPI_TYPE_PACKAGE)396{397Count = ObjDesc->Package.Count;398CidObjects = ObjDesc->Package.Elements;399}400else /* Single Integer or String CID */401{402Count = 1;403CidObjects = &ObjDesc;404}405406StringAreaSize = 0;407for (i = 0; i < Count; i++)408{409/* String lengths include null terminator */410411switch (CidObjects[i]->Common.Type)412{413case ACPI_TYPE_INTEGER:414415StringAreaSize += ACPI_EISAID_STRING_SIZE;416break;417418case ACPI_TYPE_STRING:419420StringAreaSize += CidObjects[i]->String.Length + 1;421break;422423default:424425Status = AE_TYPE;426goto Cleanup;427}428}429430/*431* Now that we know the length of the CIDs, allocate return buffer:432* 1) Size of the base structure +433* 2) Size of the CID PNP_DEVICE_ID array +434* 3) Size of the actual CID strings435*/436CidListSize = sizeof (ACPI_PNP_DEVICE_ID_LIST) +437(Count * sizeof (ACPI_PNP_DEVICE_ID)) +438StringAreaSize;439440CidList = ACPI_ALLOCATE_ZEROED (CidListSize);441if (!CidList)442{443Status = AE_NO_MEMORY;444goto Cleanup;445}446447/* Area for CID strings starts after the CID PNP_DEVICE_ID array */448449NextIdString = ACPI_CAST_PTR (char, CidList->Ids) +450((ACPI_SIZE) Count * sizeof (ACPI_PNP_DEVICE_ID));451452/* Copy/convert the CIDs to the return buffer */453454for (i = 0; i < Count; i++)455{456if (CidObjects[i]->Common.Type == ACPI_TYPE_INTEGER)457{458/* Convert the Integer (EISAID) CID to a string */459460AcpiExEisaIdToString (461NextIdString, CidObjects[i]->Integer.Value);462Length = ACPI_EISAID_STRING_SIZE;463}464else /* ACPI_TYPE_STRING */465{466/* Copy the String CID from the returned object */467468strcpy (NextIdString, CidObjects[i]->String.Pointer);469Length = CidObjects[i]->String.Length + 1;470}471472CidList->Ids[i].String = NextIdString;473CidList->Ids[i].Length = Length;474NextIdString += Length;475}476477/* Finish the CID list */478479CidList->Count = Count;480CidList->ListSize = CidListSize;481*ReturnCidList = CidList;482483484Cleanup:485486/* On exit, we must delete the _CID return object */487488AcpiUtRemoveReference (ObjDesc);489return_ACPI_STATUS (Status);490}491492493/*******************************************************************************494*495* FUNCTION: AcpiUtExecute_CLS496*497* PARAMETERS: DeviceNode - Node for the device498* ReturnId - Where the _CLS is returned499*500* RETURN: Status501*502* DESCRIPTION: Executes the _CLS control method that returns PCI-defined503* class code of the device. The _CLS value is always a package504* containing PCI class information as a list of integers.505* The returned string has format "BBSSPP", where:506* BB = Base-class code507* SS = Sub-class code508* PP = Programming Interface code509*510******************************************************************************/511512ACPI_STATUS513AcpiUtExecute_CLS (514ACPI_NAMESPACE_NODE *DeviceNode,515ACPI_PNP_DEVICE_ID **ReturnId)516{517ACPI_OPERAND_OBJECT *ObjDesc;518ACPI_OPERAND_OBJECT **ClsObjects;519UINT32 Count;520ACPI_PNP_DEVICE_ID *Cls;521UINT32 Length;522ACPI_STATUS Status;523UINT8 ClassCode[3] = {0, 0, 0};524525526ACPI_FUNCTION_TRACE (UtExecute_CLS);527528529Status = AcpiUtEvaluateObject (DeviceNode, METHOD_NAME__CLS,530ACPI_BTYPE_PACKAGE, &ObjDesc);531if (ACPI_FAILURE (Status))532{533return_ACPI_STATUS (Status);534}535536/* Get the size of the String to be returned, includes null terminator */537538Length = ACPI_PCICLS_STRING_SIZE;539ClsObjects = ObjDesc->Package.Elements;540Count = ObjDesc->Package.Count;541542if (ObjDesc->Common.Type == ACPI_TYPE_PACKAGE)543{544if (Count > 0 && ClsObjects[0]->Common.Type == ACPI_TYPE_INTEGER)545{546ClassCode[0] = (UINT8) ClsObjects[0]->Integer.Value;547}548if (Count > 1 && ClsObjects[1]->Common.Type == ACPI_TYPE_INTEGER)549{550ClassCode[1] = (UINT8) ClsObjects[1]->Integer.Value;551}552if (Count > 2 && ClsObjects[2]->Common.Type == ACPI_TYPE_INTEGER)553{554ClassCode[2] = (UINT8) ClsObjects[2]->Integer.Value;555}556}557558/* Allocate a buffer for the CLS */559560Cls = ACPI_ALLOCATE_ZEROED (561sizeof (ACPI_PNP_DEVICE_ID) + (ACPI_SIZE) Length);562if (!Cls)563{564Status = AE_NO_MEMORY;565goto Cleanup;566}567568/* Area for the string starts after PNP_DEVICE_ID struct */569570Cls->String = ACPI_ADD_PTR (char, Cls, sizeof (ACPI_PNP_DEVICE_ID));571572/* Simply copy existing string */573574AcpiExPciClsToString (Cls->String, ClassCode);575Cls->Length = Length;576*ReturnId = Cls;577578579Cleanup:580581/* On exit, we must delete the return object */582583AcpiUtRemoveReference (ObjDesc);584return_ACPI_STATUS (Status);585}586587588