1 /******************************************************************************
3 * Module Name: nsxfname - Public interfaces to the ACPI subsystem
4 * ACPI Namespace oriented interfaces
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, 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 __NSXFNAME_C__
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 */
62 ACPI_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 (AcpiNsValidRootPrefix (Pathname[0]))
125 /* Pathname is fully qualified (starts with '\') */
127 /* Special case for root-only, since we can't search for it */
129 if (!ACPI_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;
182 /* Parameter validation */
184 if (NameType > ACPI_NAME_TYPE_MAX)
186 return (AE_BAD_PARAMETER);
189 Status = AcpiUtValidateBuffer (Buffer);
190 if (ACPI_FAILURE (Status))
195 if (NameType == ACPI_FULL_PATHNAME)
197 /* Get the full pathname (From the namespace root) */
199 Status = AcpiNsHandleToPathname (Handle, Buffer);
204 * Wants the single segment ACPI name.
205 * Validate handle and convert to a namespace Node
207 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
208 if (ACPI_FAILURE (Status))
213 Node = AcpiNsValidateHandle (Handle);
216 Status = AE_BAD_PARAMETER;
220 /* Validate/Allocate/Clear caller buffer */
222 Status = AcpiUtInitializeBuffer (Buffer, ACPI_PATH_SEGMENT_LENGTH);
223 if (ACPI_FAILURE (Status))
228 /* Just copy the ACPI name from the Node and zero terminate it */
230 ACPI_STRNCPY (Buffer->Pointer, AcpiUtGetNodeName (Node),
232 ((char *) Buffer->Pointer) [ACPI_NAME_SIZE] = 0;
238 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
242 ACPI_EXPORT_SYMBOL (AcpiGetName)
245 /******************************************************************************
247 * FUNCTION: AcpiNsCopyDeviceId
249 * PARAMETERS: Dest - Pointer to the destination DEVICE_ID
250 * Source - Pointer to the source DEVICE_ID
251 * StringArea - Pointer to where to copy the dest string
253 * RETURN: Pointer to the next string area
255 * DESCRIPTION: Copy a single DEVICE_ID, including the string data.
257 ******************************************************************************/
261 ACPI_DEVICE_ID *Dest,
262 ACPI_DEVICE_ID *Source,
265 /* Create the destination DEVICE_ID */
267 Dest->String = StringArea;
268 Dest->Length = Source->Length;
270 /* Copy actual string and return a pointer to the next string area */
272 ACPI_MEMCPY (StringArea, Source->String, Source->Length);
273 return (StringArea + Source->Length);
277 /******************************************************************************
279 * FUNCTION: AcpiGetObjectInfo
281 * PARAMETERS: Handle - Object Handle
282 * ReturnBuffer - Where the info is returned
286 * DESCRIPTION: Returns information about an object as gleaned from the
287 * namespace node and possibly by running several standard
288 * control methods (Such as in the case of a device.)
290 * For Device and Processor objects, run the Device _HID, _UID, _CID, _STA,
291 * _ADR, _SxW, and _SxD methods.
293 * Note: Allocates the return buffer, must be freed by the caller.
295 ******************************************************************************/
300 ACPI_DEVICE_INFO **ReturnBuffer)
302 ACPI_NAMESPACE_NODE *Node;
303 ACPI_DEVICE_INFO *Info;
304 ACPI_DEVICE_ID_LIST *CidList = NULL;
305 ACPI_DEVICE_ID *Hid = NULL;
306 ACPI_DEVICE_ID *Uid = NULL;
308 ACPI_OBJECT_TYPE Type;
317 /* Parameter validation */
319 if (!Handle || !ReturnBuffer)
321 return (AE_BAD_PARAMETER);
324 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
325 if (ACPI_FAILURE (Status))
330 Node = AcpiNsValidateHandle (Handle);
333 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
334 return (AE_BAD_PARAMETER);
337 /* Get the namespace node data while the namespace is locked */
339 InfoSize = sizeof (ACPI_DEVICE_INFO);
341 Name = Node->Name.Integer;
343 if (Node->Type == ACPI_TYPE_METHOD)
345 ParamCount = Node->Object->Method.ParamCount;
348 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
349 if (ACPI_FAILURE (Status))
354 if ((Type == ACPI_TYPE_DEVICE) ||
355 (Type == ACPI_TYPE_PROCESSOR))
358 * Get extra info for ACPI Device/Processor objects only:
359 * Run the Device _HID, _UID, and _CID methods.
361 * Note: none of these methods are required, so they may or may
362 * not be present for this device. The Info->Valid bitfield is used
363 * to indicate which methods were found and run successfully.
366 /* Execute the Device._HID method */
368 Status = AcpiUtExecute_HID (Node, &Hid);
369 if (ACPI_SUCCESS (Status))
371 InfoSize += Hid->Length;
372 Valid |= ACPI_VALID_HID;
375 /* Execute the Device._UID method */
377 Status = AcpiUtExecute_UID (Node, &Uid);
378 if (ACPI_SUCCESS (Status))
380 InfoSize += Uid->Length;
381 Valid |= ACPI_VALID_UID;
384 /* Execute the Device._CID method */
386 Status = AcpiUtExecute_CID (Node, &CidList);
387 if (ACPI_SUCCESS (Status))
389 /* Add size of CID strings and CID pointer array */
391 InfoSize += (CidList->ListSize - sizeof (ACPI_DEVICE_ID_LIST));
392 Valid |= ACPI_VALID_CID;
397 * Now that we have the variable-length data, we can allocate the
400 Info = ACPI_ALLOCATE_ZEROED (InfoSize);
403 Status = AE_NO_MEMORY;
407 /* Get the fixed-length data */
409 if ((Type == ACPI_TYPE_DEVICE) ||
410 (Type == ACPI_TYPE_PROCESSOR))
413 * Get extra info for ACPI Device/Processor objects only:
414 * Run the _STA, _ADR and, SxW, and _SxD methods.
416 * Note: none of these methods are required, so they may or may
417 * not be present for this device. The Info->Valid bitfield is used
418 * to indicate which methods were found and run successfully.
421 /* Execute the Device._STA method */
423 Status = AcpiUtExecute_STA (Node, &Info->CurrentStatus);
424 if (ACPI_SUCCESS (Status))
426 Valid |= ACPI_VALID_STA;
429 /* Execute the Device._ADR method */
431 Status = AcpiUtEvaluateNumericObject (METHOD_NAME__ADR, Node,
433 if (ACPI_SUCCESS (Status))
435 Valid |= ACPI_VALID_ADR;
438 /* Execute the Device._SxW methods */
440 Status = AcpiUtExecutePowerMethods (Node,
441 AcpiGbl_LowestDstateNames, ACPI_NUM_SxW_METHODS,
442 Info->LowestDstates);
443 if (ACPI_SUCCESS (Status))
445 Valid |= ACPI_VALID_SXWS;
448 /* Execute the Device._SxD methods */
450 Status = AcpiUtExecutePowerMethods (Node,
451 AcpiGbl_HighestDstateNames, ACPI_NUM_SxD_METHODS,
452 Info->HighestDstates);
453 if (ACPI_SUCCESS (Status))
455 Valid |= ACPI_VALID_SXDS;
460 * Create a pointer to the string area of the return buffer.
461 * Point to the end of the base ACPI_DEVICE_INFO structure.
463 NextIdString = ACPI_CAST_PTR (char, Info->CompatibleIdList.Ids);
466 /* Point past the CID DEVICE_ID array */
468 NextIdString += ((ACPI_SIZE) CidList->Count * sizeof (ACPI_DEVICE_ID));
472 * Copy the HID, UID, and CIDs to the return buffer. The variable-length
473 * strings are copied to the reserved area at the end of the buffer.
475 * For HID and CID, check if the ID is a PCI Root Bridge.
479 NextIdString = AcpiNsCopyDeviceId (&Info->HardwareId,
482 if (AcpiUtIsPciRootBridge (Hid->String))
484 Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
490 NextIdString = AcpiNsCopyDeviceId (&Info->UniqueId,
496 Info->CompatibleIdList.Count = CidList->Count;
497 Info->CompatibleIdList.ListSize = CidList->ListSize;
501 for (i = 0; i < CidList->Count; i++)
503 NextIdString = AcpiNsCopyDeviceId (&Info->CompatibleIdList.Ids[i],
504 &CidList->Ids[i], NextIdString);
506 if (AcpiUtIsPciRootBridge (CidList->Ids[i].String))
508 Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
513 /* Copy the fixed-length data */
515 Info->InfoSize = InfoSize;
518 Info->ParamCount = ParamCount;
521 *ReturnBuffer = Info;
541 ACPI_EXPORT_SYMBOL (AcpiGetObjectInfo)
544 /******************************************************************************
546 * FUNCTION: AcpiInstallMethod
548 * PARAMETERS: Buffer - An ACPI table containing one control method
552 * DESCRIPTION: Install a control method into the namespace. If the method
553 * name already exists in the namespace, it is overwritten. The
554 * input buffer must contain a valid DSDT or SSDT containing a
555 * single control method.
557 ******************************************************************************/
563 ACPI_TABLE_HEADER *Table = ACPI_CAST_PTR (ACPI_TABLE_HEADER, Buffer);
567 ACPI_NAMESPACE_NODE *Node;
568 ACPI_OPERAND_OBJECT *MethodObj;
569 ACPI_PARSE_STATE ParserState;
576 /* Parameter validation */
580 return (AE_BAD_PARAMETER);
583 /* Table must be a DSDT or SSDT */
585 if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT) &&
586 !ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_SSDT))
588 return (AE_BAD_HEADER);
591 /* First AML opcode in the table must be a control method */
593 ParserState.Aml = Buffer + sizeof (ACPI_TABLE_HEADER);
594 Opcode = AcpiPsPeekOpcode (&ParserState);
595 if (Opcode != AML_METHOD_OP)
597 return (AE_BAD_PARAMETER);
600 /* Extract method information from the raw AML */
602 ParserState.Aml += AcpiPsGetOpcodeSize (Opcode);
603 ParserState.PkgEnd = AcpiPsGetNextPackageEnd (&ParserState);
604 Path = AcpiPsGetNextNamestring (&ParserState);
605 MethodFlags = *ParserState.Aml++;
606 AmlStart = ParserState.Aml;
607 AmlLength = ACPI_PTR_DIFF (ParserState.PkgEnd, AmlStart);
610 * Allocate resources up-front. We don't want to have to delete a new
611 * node from the namespace if we cannot allocate memory.
613 AmlBuffer = ACPI_ALLOCATE (AmlLength);
616 return (AE_NO_MEMORY);
619 MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
622 ACPI_FREE (AmlBuffer);
623 return (AE_NO_MEMORY);
626 /* Lock namespace for AcpiNsLookup, we may be creating a new node */
628 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
629 if (ACPI_FAILURE (Status))
634 /* The lookup either returns an existing node or creates a new one */
636 Status = AcpiNsLookup (NULL, Path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
637 ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND, NULL, &Node);
639 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
641 if (ACPI_FAILURE (Status)) /* NsLookup */
643 if (Status != AE_ALREADY_EXISTS)
648 /* Node existed previously, make sure it is a method node */
650 if (Node->Type != ACPI_TYPE_METHOD)
657 /* Copy the method AML to the local buffer */
659 ACPI_MEMCPY (AmlBuffer, AmlStart, AmlLength);
661 /* Initialize the method object with the new method's information */
663 MethodObj->Method.AmlStart = AmlBuffer;
664 MethodObj->Method.AmlLength = AmlLength;
666 MethodObj->Method.ParamCount = (UINT8)
667 (MethodFlags & AML_METHOD_ARG_COUNT);
669 if (MethodFlags & AML_METHOD_SERIALIZED)
671 MethodObj->Method.InfoFlags = ACPI_METHOD_SERIALIZED;
673 MethodObj->Method.SyncLevel = (UINT8)
674 ((MethodFlags & AML_METHOD_SYNC_LEVEL) >> 4);
678 * Now that it is complete, we can attach the new method object to
679 * the method Node (detaches/deletes any existing object)
681 Status = AcpiNsAttachObject (Node, MethodObj, ACPI_TYPE_METHOD);
684 * Flag indicates AML buffer is dynamic, must be deleted later.
685 * Must be set only after attach above.
687 Node->Flags |= ANOBJ_ALLOCATED_BUFFER;
689 /* Remove local reference to the method object */
691 AcpiUtRemoveReference (MethodObj);
697 ACPI_FREE (AmlBuffer);
698 ACPI_FREE (MethodObj);
702 ACPI_EXPORT_SYMBOL (AcpiInstallMethod)