1 /******************************************************************************
3 * Module Name: nsxfname - Public interfaces to the ACPI subsystem
4 * ACPI Namespace oriented interfaces
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2015, 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 ******************************************************************************/
179 ACPI_NAMESPACE_NODE *Node;
183 /* Parameter validation */
185 if (NameType > ACPI_NAME_TYPE_MAX)
187 return (AE_BAD_PARAMETER);
190 Status = AcpiUtValidateBuffer (Buffer);
191 if (ACPI_FAILURE (Status))
196 if (NameType == ACPI_FULL_PATHNAME)
198 /* Get the full pathname (From the namespace root) */
200 Status = AcpiNsHandleToPathname (Handle, Buffer);
205 * Wants the single segment ACPI name.
206 * Validate handle and convert to a namespace Node
208 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
209 if (ACPI_FAILURE (Status))
214 Node = AcpiNsValidateHandle (Handle);
217 Status = AE_BAD_PARAMETER;
221 /* Validate/Allocate/Clear caller buffer */
223 Status = AcpiUtInitializeBuffer (Buffer, ACPI_PATH_SEGMENT_LENGTH);
224 if (ACPI_FAILURE (Status))
229 /* Just copy the ACPI name from the Node and zero terminate it */
231 NodeName = AcpiUtGetNodeName (Node);
232 ACPI_MOVE_NAME (Buffer->Pointer, NodeName);
233 ((char *) Buffer->Pointer) [ACPI_NAME_SIZE] = 0;
239 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
243 ACPI_EXPORT_SYMBOL (AcpiGetName)
246 /******************************************************************************
248 * FUNCTION: AcpiNsCopyDeviceId
250 * PARAMETERS: Dest - Pointer to the destination PNP_DEVICE_ID
251 * Source - Pointer to the source PNP_DEVICE_ID
252 * StringArea - Pointer to where to copy the dest string
254 * RETURN: Pointer to the next string area
256 * DESCRIPTION: Copy a single PNP_DEVICE_ID, including the string data.
258 ******************************************************************************/
262 ACPI_PNP_DEVICE_ID *Dest,
263 ACPI_PNP_DEVICE_ID *Source,
267 /* Create the destination PNP_DEVICE_ID */
269 Dest->String = StringArea;
270 Dest->Length = Source->Length;
272 /* Copy actual string and return a pointer to the next string area */
274 memcpy (StringArea, Source->String, Source->Length);
275 return (StringArea + Source->Length);
279 /******************************************************************************
281 * FUNCTION: AcpiGetObjectInfo
283 * PARAMETERS: Handle - Object Handle
284 * ReturnBuffer - Where the info is returned
288 * DESCRIPTION: Returns information about an object as gleaned from the
289 * namespace node and possibly by running several standard
290 * control methods (Such as in the case of a device.)
292 * For Device and Processor objects, run the Device _HID, _UID, _CID, _SUB,
293 * _CLS, _STA, _ADR, _SxW, and _SxD methods.
295 * Note: Allocates the return buffer, must be freed by the caller.
297 ******************************************************************************/
302 ACPI_DEVICE_INFO **ReturnBuffer)
304 ACPI_NAMESPACE_NODE *Node;
305 ACPI_DEVICE_INFO *Info;
306 ACPI_PNP_DEVICE_ID_LIST *CidList = NULL;
307 ACPI_PNP_DEVICE_ID *Hid = NULL;
308 ACPI_PNP_DEVICE_ID *Uid = NULL;
309 ACPI_PNP_DEVICE_ID *Sub = NULL;
310 ACPI_PNP_DEVICE_ID *Cls = NULL;
312 ACPI_OBJECT_TYPE Type;
321 /* Parameter validation */
323 if (!Handle || !ReturnBuffer)
325 return (AE_BAD_PARAMETER);
328 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
329 if (ACPI_FAILURE (Status))
334 Node = AcpiNsValidateHandle (Handle);
337 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
338 return (AE_BAD_PARAMETER);
341 /* Get the namespace node data while the namespace is locked */
343 InfoSize = sizeof (ACPI_DEVICE_INFO);
345 Name = Node->Name.Integer;
347 if (Node->Type == ACPI_TYPE_METHOD)
349 ParamCount = Node->Object->Method.ParamCount;
352 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
353 if (ACPI_FAILURE (Status))
358 if ((Type == ACPI_TYPE_DEVICE) ||
359 (Type == ACPI_TYPE_PROCESSOR))
362 * Get extra info for ACPI Device/Processor objects only:
363 * Run the Device _HID, _UID, _SUB, _CID, and _CLS methods.
365 * Note: none of these methods are required, so they may or may
366 * not be present for this device. The Info->Valid bitfield is used
367 * to indicate which methods were found and run successfully.
370 /* Execute the Device._HID method */
372 Status = AcpiUtExecute_HID (Node, &Hid);
373 if (ACPI_SUCCESS (Status))
375 InfoSize += Hid->Length;
376 Valid |= ACPI_VALID_HID;
379 /* Execute the Device._UID method */
381 Status = AcpiUtExecute_UID (Node, &Uid);
382 if (ACPI_SUCCESS (Status))
384 InfoSize += Uid->Length;
385 Valid |= ACPI_VALID_UID;
388 /* Execute the Device._SUB method */
390 Status = AcpiUtExecute_SUB (Node, &Sub);
391 if (ACPI_SUCCESS (Status))
393 InfoSize += Sub->Length;
394 Valid |= ACPI_VALID_SUB;
397 /* Execute the Device._CID method */
399 Status = AcpiUtExecute_CID (Node, &CidList);
400 if (ACPI_SUCCESS (Status))
402 /* Add size of CID strings and CID pointer array */
404 InfoSize += (CidList->ListSize - sizeof (ACPI_PNP_DEVICE_ID_LIST));
405 Valid |= ACPI_VALID_CID;
408 /* Execute the Device._CLS method */
410 Status = AcpiUtExecute_CLS (Node, &Cls);
411 if (ACPI_SUCCESS (Status))
413 InfoSize += Cls->Length;
414 Valid |= ACPI_VALID_CLS;
419 * Now that we have the variable-length data, we can allocate the
422 Info = ACPI_ALLOCATE_ZEROED (InfoSize);
425 Status = AE_NO_MEMORY;
429 /* Get the fixed-length data */
431 if ((Type == ACPI_TYPE_DEVICE) ||
432 (Type == ACPI_TYPE_PROCESSOR))
435 * Get extra info for ACPI Device/Processor objects only:
436 * Run the _STA, _ADR and, SxW, and _SxD methods.
438 * Notes: none of these methods are required, so they may or may
439 * not be present for this device. The Info->Valid bitfield is used
440 * to indicate which methods were found and run successfully.
442 * For _STA, if the method does not exist, then (as per the ACPI
443 * specification), the returned CurrentStatus flags will indicate
444 * that the device is present/functional/enabled. Otherwise, the
445 * CurrentStatus flags reflect the value returned from _STA.
448 /* Execute the Device._STA method */
450 Status = AcpiUtExecute_STA (Node, &Info->CurrentStatus);
451 if (ACPI_SUCCESS (Status))
453 Valid |= ACPI_VALID_STA;
456 /* Execute the Device._ADR method */
458 Status = AcpiUtEvaluateNumericObject (METHOD_NAME__ADR, Node,
460 if (ACPI_SUCCESS (Status))
462 Valid |= ACPI_VALID_ADR;
465 /* Execute the Device._SxW methods */
467 Status = AcpiUtExecutePowerMethods (Node,
468 AcpiGbl_LowestDstateNames, ACPI_NUM_SxW_METHODS,
469 Info->LowestDstates);
470 if (ACPI_SUCCESS (Status))
472 Valid |= ACPI_VALID_SXWS;
475 /* Execute the Device._SxD methods */
477 Status = AcpiUtExecutePowerMethods (Node,
478 AcpiGbl_HighestDstateNames, ACPI_NUM_SxD_METHODS,
479 Info->HighestDstates);
480 if (ACPI_SUCCESS (Status))
482 Valid |= ACPI_VALID_SXDS;
487 * Create a pointer to the string area of the return buffer.
488 * Point to the end of the base ACPI_DEVICE_INFO structure.
490 NextIdString = ACPI_CAST_PTR (char, Info->CompatibleIdList.Ids);
493 /* Point past the CID PNP_DEVICE_ID array */
495 NextIdString += ((ACPI_SIZE) CidList->Count * sizeof (ACPI_PNP_DEVICE_ID));
499 * Copy the HID, UID, SUB, and CIDs to the return buffer.
500 * The variable-length strings are copied to the reserved area
501 * at the end of the buffer.
503 * For HID and CID, check if the ID is a PCI Root Bridge.
507 NextIdString = AcpiNsCopyDeviceId (&Info->HardwareId,
510 if (AcpiUtIsPciRootBridge (Hid->String))
512 Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
518 NextIdString = AcpiNsCopyDeviceId (&Info->UniqueId,
524 NextIdString = AcpiNsCopyDeviceId (&Info->SubsystemId,
530 Info->CompatibleIdList.Count = CidList->Count;
531 Info->CompatibleIdList.ListSize = CidList->ListSize;
535 for (i = 0; i < CidList->Count; i++)
537 NextIdString = AcpiNsCopyDeviceId (&Info->CompatibleIdList.Ids[i],
538 &CidList->Ids[i], NextIdString);
540 if (AcpiUtIsPciRootBridge (CidList->Ids[i].String))
542 Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
549 NextIdString = AcpiNsCopyDeviceId (&Info->ClassCode,
553 /* Copy the fixed-length data */
555 Info->InfoSize = InfoSize;
558 Info->ParamCount = ParamCount;
561 *ReturnBuffer = Info;
589 ACPI_EXPORT_SYMBOL (AcpiGetObjectInfo)
592 /******************************************************************************
594 * FUNCTION: AcpiInstallMethod
596 * PARAMETERS: Buffer - An ACPI table containing one control method
600 * DESCRIPTION: Install a control method into the namespace. If the method
601 * name already exists in the namespace, it is overwritten. The
602 * input buffer must contain a valid DSDT or SSDT containing a
603 * single control method.
605 ******************************************************************************/
611 ACPI_TABLE_HEADER *Table = ACPI_CAST_PTR (ACPI_TABLE_HEADER, Buffer);
615 ACPI_NAMESPACE_NODE *Node;
616 ACPI_OPERAND_OBJECT *MethodObj;
617 ACPI_PARSE_STATE ParserState;
624 /* Parameter validation */
628 return (AE_BAD_PARAMETER);
631 /* Table must be a DSDT or SSDT */
633 if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT) &&
634 !ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_SSDT))
636 return (AE_BAD_HEADER);
639 /* First AML opcode in the table must be a control method */
641 ParserState.Aml = Buffer + sizeof (ACPI_TABLE_HEADER);
642 Opcode = AcpiPsPeekOpcode (&ParserState);
643 if (Opcode != AML_METHOD_OP)
645 return (AE_BAD_PARAMETER);
648 /* Extract method information from the raw AML */
650 ParserState.Aml += AcpiPsGetOpcodeSize (Opcode);
651 ParserState.PkgEnd = AcpiPsGetNextPackageEnd (&ParserState);
652 Path = AcpiPsGetNextNamestring (&ParserState);
653 MethodFlags = *ParserState.Aml++;
654 AmlStart = ParserState.Aml;
655 AmlLength = ACPI_PTR_DIFF (ParserState.PkgEnd, AmlStart);
658 * Allocate resources up-front. We don't want to have to delete a new
659 * node from the namespace if we cannot allocate memory.
661 AmlBuffer = ACPI_ALLOCATE (AmlLength);
664 return (AE_NO_MEMORY);
667 MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
670 ACPI_FREE (AmlBuffer);
671 return (AE_NO_MEMORY);
674 /* Lock namespace for AcpiNsLookup, we may be creating a new node */
676 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
677 if (ACPI_FAILURE (Status))
682 /* The lookup either returns an existing node or creates a new one */
684 Status = AcpiNsLookup (NULL, Path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
685 ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND, NULL, &Node);
687 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
689 if (ACPI_FAILURE (Status)) /* NsLookup */
691 if (Status != AE_ALREADY_EXISTS)
696 /* Node existed previously, make sure it is a method node */
698 if (Node->Type != ACPI_TYPE_METHOD)
705 /* Copy the method AML to the local buffer */
707 memcpy (AmlBuffer, AmlStart, AmlLength);
709 /* Initialize the method object with the new method's information */
711 MethodObj->Method.AmlStart = AmlBuffer;
712 MethodObj->Method.AmlLength = AmlLength;
714 MethodObj->Method.ParamCount = (UINT8)
715 (MethodFlags & AML_METHOD_ARG_COUNT);
717 if (MethodFlags & AML_METHOD_SERIALIZED)
719 MethodObj->Method.InfoFlags = ACPI_METHOD_SERIALIZED;
721 MethodObj->Method.SyncLevel = (UINT8)
722 ((MethodFlags & AML_METHOD_SYNC_LEVEL) >> 4);
726 * Now that it is complete, we can attach the new method object to
727 * the method Node (detaches/deletes any existing object)
729 Status = AcpiNsAttachObject (Node, MethodObj, ACPI_TYPE_METHOD);
732 * Flag indicates AML buffer is dynamic, must be deleted later.
733 * Must be set only after attach above.
735 Node->Flags |= ANOBJ_ALLOCATED_BUFFER;
737 /* Remove local reference to the method object */
739 AcpiUtRemoveReference (MethodObj);
745 ACPI_FREE (AmlBuffer);
746 ACPI_FREE (MethodObj);
750 ACPI_EXPORT_SYMBOL (AcpiInstallMethod)