1 /******************************************************************************
3 * Module Name: nsxfname - Public interfaces to the ACPI subsystem
4 * ACPI Namespace oriented interfaces
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2017, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #define EXPORT_ACPI_INTERFACES
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
50 #include <contrib/dev/acpica/include/acparser.h>
51 #include <contrib/dev/acpica/include/amlcode.h>
54 #define _COMPONENT ACPI_NAMESPACE
55 ACPI_MODULE_NAME ("nsxfname")
57 /* Local prototypes */
61 ACPI_PNP_DEVICE_ID *Dest,
62 ACPI_PNP_DEVICE_ID *Source,
66 /******************************************************************************
68 * FUNCTION: AcpiGetHandle
70 * PARAMETERS: Parent - Object to search under (search scope).
71 * Pathname - Pointer to an asciiz string containing the
73 * RetHandle - Where the return handle is returned
77 * DESCRIPTION: This routine will search for a caller specified name in the
78 * name space. The caller can restrict the search region by
79 * specifying a non NULL parent. The parent value is itself a
82 ******************************************************************************/
88 ACPI_HANDLE *RetHandle)
91 ACPI_NAMESPACE_NODE *Node = NULL;
92 ACPI_NAMESPACE_NODE *PrefixNode = NULL;
95 ACPI_FUNCTION_ENTRY ();
98 /* Parameter Validation */
100 if (!RetHandle || !Pathname)
102 return (AE_BAD_PARAMETER);
105 /* Convert a parent handle to a prefix node */
109 PrefixNode = AcpiNsValidateHandle (Parent);
112 return (AE_BAD_PARAMETER);
118 * 1) Fully qualified pathname
119 * 2) Parent + Relative pathname
121 * Error for <null Parent + relative path>
123 if (ACPI_IS_ROOT_PREFIX (Pathname[0]))
125 /* Pathname is fully qualified (starts with '\') */
127 /* Special case for root-only, since we can't search for it */
129 if (!strcmp (Pathname, ACPI_NS_ROOT_PATH))
131 *RetHandle = ACPI_CAST_PTR (ACPI_HANDLE, AcpiGbl_RootNode);
135 else if (!PrefixNode)
137 /* Relative path with null prefix is disallowed */
139 return (AE_BAD_PARAMETER);
142 /* Find the Node and convert to a handle */
144 Status = AcpiNsGetNode (PrefixNode, Pathname, ACPI_NS_NO_UPSEARCH, &Node);
145 if (ACPI_SUCCESS (Status))
147 *RetHandle = ACPI_CAST_PTR (ACPI_HANDLE, Node);
153 ACPI_EXPORT_SYMBOL (AcpiGetHandle)
156 /******************************************************************************
158 * FUNCTION: AcpiGetName
160 * PARAMETERS: Handle - Handle to be converted to a pathname
161 * NameType - Full pathname or single segment
162 * Buffer - Buffer for returned path
164 * RETURN: Pointer to a string containing the fully qualified Name.
166 * DESCRIPTION: This routine returns the fully qualified name associated with
167 * the Handle parameter. This and the AcpiPathnameToHandle are
168 * complementary functions.
170 ******************************************************************************/
181 /* Parameter validation */
183 if (NameType > ACPI_NAME_TYPE_MAX)
185 return (AE_BAD_PARAMETER);
188 Status = AcpiUtValidateBuffer (Buffer);
189 if (ACPI_FAILURE (Status))
195 * Wants the single segment ACPI name.
196 * Validate handle and convert to a namespace Node
198 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
199 if (ACPI_FAILURE (Status))
204 if (NameType == ACPI_FULL_PATHNAME ||
205 NameType == ACPI_FULL_PATHNAME_NO_TRAILING)
207 /* Get the full pathname (From the namespace root) */
209 Status = AcpiNsHandleToPathname (Handle, Buffer,
210 NameType == ACPI_FULL_PATHNAME ? FALSE : TRUE);
214 /* Get the single name */
216 Status = AcpiNsHandleToName (Handle, Buffer);
219 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
223 ACPI_EXPORT_SYMBOL (AcpiGetName)
226 /******************************************************************************
228 * FUNCTION: AcpiNsCopyDeviceId
230 * PARAMETERS: Dest - Pointer to the destination PNP_DEVICE_ID
231 * Source - Pointer to the source PNP_DEVICE_ID
232 * StringArea - Pointer to where to copy the dest string
234 * RETURN: Pointer to the next string area
236 * DESCRIPTION: Copy a single PNP_DEVICE_ID, including the string data.
238 ******************************************************************************/
242 ACPI_PNP_DEVICE_ID *Dest,
243 ACPI_PNP_DEVICE_ID *Source,
246 /* Create the destination PNP_DEVICE_ID */
248 Dest->String = StringArea;
249 Dest->Length = Source->Length;
251 /* Copy actual string and return a pointer to the next string area */
253 memcpy (StringArea, Source->String, Source->Length);
254 return (StringArea + Source->Length);
258 /******************************************************************************
260 * FUNCTION: AcpiGetObjectInfo
262 * PARAMETERS: Handle - Object Handle
263 * ReturnBuffer - Where the info is returned
267 * DESCRIPTION: Returns information about an object as gleaned from the
268 * namespace node and possibly by running several standard
269 * control methods (Such as in the case of a device.)
271 * For Device and Processor objects, run the Device _HID, _UID, _CID, _STA,
272 * _CLS, _ADR, _SxW, and _SxD methods.
274 * Note: Allocates the return buffer, must be freed by the caller.
276 * Note: This interface is intended to be used during the initial device
277 * discovery namespace traversal. Therefore, no complex methods can be
278 * executed, especially those that access operation regions. Therefore, do
279 * not add any additional methods that could cause problems in this area.
280 * this was the fate of the _SUB method which was found to cause such
281 * problems and was removed (11/2015).
283 ******************************************************************************/
288 ACPI_DEVICE_INFO **ReturnBuffer)
290 ACPI_NAMESPACE_NODE *Node;
291 ACPI_DEVICE_INFO *Info;
292 ACPI_PNP_DEVICE_ID_LIST *CidList = NULL;
293 ACPI_PNP_DEVICE_ID *Hid = NULL;
294 ACPI_PNP_DEVICE_ID *Uid = NULL;
295 ACPI_PNP_DEVICE_ID *Cls = NULL;
297 ACPI_OBJECT_TYPE Type;
306 /* Parameter validation */
308 if (!Handle || !ReturnBuffer)
310 return (AE_BAD_PARAMETER);
313 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
314 if (ACPI_FAILURE (Status))
319 Node = AcpiNsValidateHandle (Handle);
322 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
323 return (AE_BAD_PARAMETER);
326 /* Get the namespace node data while the namespace is locked */
328 InfoSize = sizeof (ACPI_DEVICE_INFO);
330 Name = Node->Name.Integer;
332 if (Node->Type == ACPI_TYPE_METHOD)
334 ParamCount = Node->Object->Method.ParamCount;
337 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
338 if (ACPI_FAILURE (Status))
343 if ((Type == ACPI_TYPE_DEVICE) ||
344 (Type == ACPI_TYPE_PROCESSOR))
347 * Get extra info for ACPI Device/Processor objects only:
348 * Run the Device _HID, _UID, _CLS, and _CID methods.
350 * Note: none of these methods are required, so they may or may
351 * not be present for this device. The Info->Valid bitfield is used
352 * to indicate which methods were found and run successfully.
355 /* Execute the Device._HID method */
357 Status = AcpiUtExecute_HID (Node, &Hid);
358 if (ACPI_SUCCESS (Status))
360 InfoSize += Hid->Length;
361 Valid |= ACPI_VALID_HID;
364 /* Execute the Device._UID method */
366 Status = AcpiUtExecute_UID (Node, &Uid);
367 if (ACPI_SUCCESS (Status))
369 InfoSize += Uid->Length;
370 Valid |= ACPI_VALID_UID;
373 /* Execute the Device._CID method */
375 Status = AcpiUtExecute_CID (Node, &CidList);
376 if (ACPI_SUCCESS (Status))
378 /* Add size of CID strings and CID pointer array */
380 InfoSize += (CidList->ListSize - sizeof (ACPI_PNP_DEVICE_ID_LIST));
381 Valid |= ACPI_VALID_CID;
384 /* Execute the Device._CLS method */
386 Status = AcpiUtExecute_CLS (Node, &Cls);
387 if (ACPI_SUCCESS (Status))
389 InfoSize += Cls->Length;
390 Valid |= ACPI_VALID_CLS;
395 * Now that we have the variable-length data, we can allocate the
398 Info = ACPI_ALLOCATE_ZEROED (InfoSize);
401 Status = AE_NO_MEMORY;
405 /* Get the fixed-length data */
407 if ((Type == ACPI_TYPE_DEVICE) ||
408 (Type == ACPI_TYPE_PROCESSOR))
411 * Get extra info for ACPI Device/Processor objects only:
412 * Run the _STA, _ADR and, SxW, and _SxD methods.
414 * Notes: none of these methods are required, so they may or may
415 * not be present for this device. The Info->Valid bitfield is used
416 * to indicate which methods were found and run successfully.
418 * For _STA, if the method does not exist, then (as per the ACPI
419 * specification), the returned CurrentStatus flags will indicate
420 * that the device is present/functional/enabled. Otherwise, the
421 * CurrentStatus flags reflect the value returned from _STA.
424 /* Execute the Device._STA method */
426 Status = AcpiUtExecute_STA (Node, &Info->CurrentStatus);
427 if (ACPI_SUCCESS (Status))
429 Valid |= ACPI_VALID_STA;
432 /* Execute the Device._ADR method */
434 Status = AcpiUtEvaluateNumericObject (METHOD_NAME__ADR, Node,
436 if (ACPI_SUCCESS (Status))
438 Valid |= ACPI_VALID_ADR;
441 /* Execute the Device._SxW methods */
443 Status = AcpiUtExecutePowerMethods (Node,
444 AcpiGbl_LowestDstateNames, ACPI_NUM_SxW_METHODS,
445 Info->LowestDstates);
446 if (ACPI_SUCCESS (Status))
448 Valid |= ACPI_VALID_SXWS;
451 /* Execute the Device._SxD methods */
453 Status = AcpiUtExecutePowerMethods (Node,
454 AcpiGbl_HighestDstateNames, ACPI_NUM_SxD_METHODS,
455 Info->HighestDstates);
456 if (ACPI_SUCCESS (Status))
458 Valid |= ACPI_VALID_SXDS;
463 * Create a pointer to the string area of the return buffer.
464 * Point to the end of the base ACPI_DEVICE_INFO structure.
466 NextIdString = ACPI_CAST_PTR (char, Info->CompatibleIdList.Ids);
469 /* Point past the CID PNP_DEVICE_ID array */
471 NextIdString += ((ACPI_SIZE) CidList->Count * sizeof (ACPI_PNP_DEVICE_ID));
475 * Copy the HID, UID, and CIDs to the return buffer. The variable-length
476 * strings are copied to the reserved area at the end of the buffer.
478 * For HID and CID, check if the ID is a PCI Root Bridge.
482 NextIdString = AcpiNsCopyDeviceId (&Info->HardwareId,
485 if (AcpiUtIsPciRootBridge (Hid->String))
487 Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
493 NextIdString = AcpiNsCopyDeviceId (&Info->UniqueId,
499 Info->CompatibleIdList.Count = CidList->Count;
500 Info->CompatibleIdList.ListSize = CidList->ListSize;
504 for (i = 0; i < CidList->Count; i++)
506 NextIdString = AcpiNsCopyDeviceId (&Info->CompatibleIdList.Ids[i],
507 &CidList->Ids[i], NextIdString);
509 if (AcpiUtIsPciRootBridge (CidList->Ids[i].String))
511 Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
518 NextIdString = AcpiNsCopyDeviceId (&Info->ClassCode,
522 /* Copy the fixed-length data */
524 Info->InfoSize = InfoSize;
527 Info->ParamCount = ParamCount;
530 *ReturnBuffer = Info;
554 ACPI_EXPORT_SYMBOL (AcpiGetObjectInfo)
557 /******************************************************************************
559 * FUNCTION: AcpiInstallMethod
561 * PARAMETERS: Buffer - An ACPI table containing one control method
565 * DESCRIPTION: Install a control method into the namespace. If the method
566 * name already exists in the namespace, it is overwritten. The
567 * input buffer must contain a valid DSDT or SSDT containing a
568 * single control method.
570 ******************************************************************************/
576 ACPI_TABLE_HEADER *Table = ACPI_CAST_PTR (ACPI_TABLE_HEADER, Buffer);
580 ACPI_NAMESPACE_NODE *Node;
581 ACPI_OPERAND_OBJECT *MethodObj;
582 ACPI_PARSE_STATE ParserState;
589 /* Parameter validation */
593 return (AE_BAD_PARAMETER);
596 /* Table must be a DSDT or SSDT */
598 if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT) &&
599 !ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_SSDT))
601 return (AE_BAD_HEADER);
604 /* First AML opcode in the table must be a control method */
606 ParserState.Aml = Buffer + sizeof (ACPI_TABLE_HEADER);
607 Opcode = AcpiPsPeekOpcode (&ParserState);
608 if (Opcode != AML_METHOD_OP)
610 return (AE_BAD_PARAMETER);
613 /* Extract method information from the raw AML */
615 ParserState.Aml += AcpiPsGetOpcodeSize (Opcode);
616 ParserState.PkgEnd = AcpiPsGetNextPackageEnd (&ParserState);
617 Path = AcpiPsGetNextNamestring (&ParserState);
619 MethodFlags = *ParserState.Aml++;
620 AmlStart = ParserState.Aml;
621 AmlLength = ACPI_PTR_DIFF (ParserState.PkgEnd, AmlStart);
624 * Allocate resources up-front. We don't want to have to delete a new
625 * node from the namespace if we cannot allocate memory.
627 AmlBuffer = ACPI_ALLOCATE (AmlLength);
630 return (AE_NO_MEMORY);
633 MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
636 ACPI_FREE (AmlBuffer);
637 return (AE_NO_MEMORY);
640 /* Lock namespace for AcpiNsLookup, we may be creating a new node */
642 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
643 if (ACPI_FAILURE (Status))
648 /* The lookup either returns an existing node or creates a new one */
650 Status = AcpiNsLookup (NULL, Path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
651 ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND, NULL, &Node);
653 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
655 if (ACPI_FAILURE (Status)) /* NsLookup */
657 if (Status != AE_ALREADY_EXISTS)
662 /* Node existed previously, make sure it is a method node */
664 if (Node->Type != ACPI_TYPE_METHOD)
671 /* Copy the method AML to the local buffer */
673 memcpy (AmlBuffer, AmlStart, AmlLength);
675 /* Initialize the method object with the new method's information */
677 MethodObj->Method.AmlStart = AmlBuffer;
678 MethodObj->Method.AmlLength = AmlLength;
680 MethodObj->Method.ParamCount = (UINT8)
681 (MethodFlags & AML_METHOD_ARG_COUNT);
683 if (MethodFlags & AML_METHOD_SERIALIZED)
685 MethodObj->Method.InfoFlags = ACPI_METHOD_SERIALIZED;
687 MethodObj->Method.SyncLevel = (UINT8)
688 ((MethodFlags & AML_METHOD_SYNC_LEVEL) >> 4);
692 * Now that it is complete, we can attach the new method object to
693 * the method Node (detaches/deletes any existing object)
695 Status = AcpiNsAttachObject (Node, MethodObj, ACPI_TYPE_METHOD);
698 * Flag indicates AML buffer is dynamic, must be deleted later.
699 * Must be set only after attach above.
701 Node->Flags |= ANOBJ_ALLOCATED_BUFFER;
703 /* Remove local reference to the method object */
705 AcpiUtRemoveReference (MethodObj);
711 ACPI_FREE (AmlBuffer);
712 ACPI_FREE (MethodObj);
716 ACPI_EXPORT_SYMBOL (AcpiInstallMethod)