drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / acpica / exstore.c
CommitLineData
1da177e4
LT
1/******************************************************************************
2 *
3 * Module Name: exstore - AML Interpreter object store support
4 *
5 *****************************************************************************/
6
7/*
25f044e6 8 * Copyright (C) 2000 - 2013, Intel Corp.
1da177e4
LT
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
1da177e4 44#include <acpi/acpi.h>
e2f7a777
LB
45#include "accommon.h"
46#include "acdispat.h"
47#include "acinterp.h"
48#include "amlcode.h"
49#include "acnamesp.h"
1da177e4 50
1da177e4 51#define _COMPONENT ACPI_EXECUTER
4be44fcd 52ACPI_MODULE_NAME("exstore")
1da177e4 53
44f6c012 54/* Local prototypes */
44f6c012 55static acpi_status
4be44fcd
LB
56acpi_ex_store_object_to_index(union acpi_operand_object *val_desc,
57 union acpi_operand_object *dest_desc,
58 struct acpi_walk_state *walk_state);
44f6c012 59
217e7268
BM
60static acpi_status
61acpi_ex_store_direct_to_node(union acpi_operand_object *source_desc,
62 struct acpi_namespace_node *node,
63 struct acpi_walk_state *walk_state);
64
1da177e4
LT
65/*******************************************************************************
66 *
67 * FUNCTION: acpi_ex_store
68 *
69 * PARAMETERS: *source_desc - Value to be stored
cf48958e
BM
70 * *dest_desc - Where to store it. Must be an NS node
71 * or union acpi_operand_object of type
1da177e4
LT
72 * Reference;
73 * walk_state - Current walk state
74 *
75 * RETURN: Status
76 *
77 * DESCRIPTION: Store the value described by source_desc into the location
78 * described by dest_desc. Called by various interpreter
79 * functions to store the result of an operation into
80 * the destination operand -- not just simply the actual "Store"
81 * ASL operator.
82 *
83 ******************************************************************************/
84
85acpi_status
4be44fcd
LB
86acpi_ex_store(union acpi_operand_object *source_desc,
87 union acpi_operand_object *dest_desc,
88 struct acpi_walk_state *walk_state)
1da177e4 89{
4be44fcd
LB
90 acpi_status status = AE_OK;
91 union acpi_operand_object *ref_desc = dest_desc;
1da177e4 92
b229cf92 93 ACPI_FUNCTION_TRACE_PTR(ex_store, dest_desc);
1da177e4
LT
94
95 /* Validate parameters */
96
97 if (!source_desc || !dest_desc) {
b8e4d893 98 ACPI_ERROR((AE_INFO, "Null parameter"));
4be44fcd 99 return_ACPI_STATUS(AE_AML_NO_OPERAND);
1da177e4
LT
100 }
101
102 /* dest_desc can be either a namespace node or an ACPI object */
103
4be44fcd 104 if (ACPI_GET_DESCRIPTOR_TYPE(dest_desc) == ACPI_DESC_TYPE_NAMED) {
1da177e4
LT
105 /*
106 * Dest is a namespace node,
107 * Storing an object into a Named node.
108 */
4be44fcd
LB
109 status = acpi_ex_store_object_to_node(source_desc,
110 (struct
111 acpi_namespace_node *)
112 dest_desc, walk_state,
113 ACPI_IMPLICIT_CONVERSION);
1da177e4 114
4be44fcd 115 return_ACPI_STATUS(status);
1da177e4
LT
116 }
117
118 /* Destination object must be a Reference or a Constant object */
119
3371c19c 120 switch (dest_desc->common.type) {
1da177e4
LT
121 case ACPI_TYPE_LOCAL_REFERENCE:
122 break;
123
124 case ACPI_TYPE_INTEGER:
125
126 /* Allow stores to Constants -- a Noop as per ACPI spec */
127
128 if (dest_desc->common.flags & AOPOBJ_AML_CONSTANT) {
4be44fcd 129 return_ACPI_STATUS(AE_OK);
1da177e4
LT
130 }
131
132 /*lint -fallthrough */
133
134 default:
135
136 /* Destination is not a Reference object */
137
b8e4d893
BM
138 ACPI_ERROR((AE_INFO,
139 "Target is not a Reference or Constant object - %s [%p]",
140 acpi_ut_get_object_type_name(dest_desc),
141 dest_desc));
1da177e4 142
4be44fcd 143 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
1da177e4
LT
144 }
145
146 /*
1044f1f6 147 * Examine the Reference class. These cases are handled:
1da177e4
LT
148 *
149 * 1) Store to Name (Change the object associated with a name)
150 * 2) Store to an indexed area of a Buffer or Package
151 * 3) Store to a Method Local or Arg
152 * 4) Store to the debug object
153 */
1044f1f6
BM
154 switch (ref_desc->reference.class) {
155 case ACPI_REFCLASS_REFOF:
1da177e4
LT
156
157 /* Storing an object into a Name "container" */
158
4be44fcd
LB
159 status = acpi_ex_store_object_to_node(source_desc,
160 ref_desc->reference.
161 object, walk_state,
162 ACPI_IMPLICIT_CONVERSION);
1da177e4
LT
163 break;
164
1044f1f6 165 case ACPI_REFCLASS_INDEX:
1da177e4
LT
166
167 /* Storing to an Index (pointer into a packager or buffer) */
168
4be44fcd
LB
169 status =
170 acpi_ex_store_object_to_index(source_desc, ref_desc,
171 walk_state);
1da177e4
LT
172 break;
173
1044f1f6
BM
174 case ACPI_REFCLASS_LOCAL:
175 case ACPI_REFCLASS_ARG:
1da177e4
LT
176
177 /* Store to a method local/arg */
178
4be44fcd 179 status =
1044f1f6
BM
180 acpi_ds_store_object_to_local(ref_desc->reference.class,
181 ref_desc->reference.value,
4be44fcd 182 source_desc, walk_state);
1da177e4
LT
183 break;
184
1044f1f6 185 case ACPI_REFCLASS_DEBUG:
1da177e4
LT
186
187 /*
188 * Storing to the Debug object causes the value stored to be
189 * displayed and otherwise has no effect -- see ACPI Specification
190 */
4be44fcd
LB
191 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
192 "**** Write to Debug Object: Object %p %s ****:\n\n",
193 source_desc,
194 acpi_ut_get_object_type_name(source_desc)));
1da177e4 195
4cdf1a56 196 ACPI_DEBUG_OBJECT(source_desc, 0, 0);
1da177e4
LT
197 break;
198
1da177e4
LT
199 default:
200
f6a22b0b 201 ACPI_ERROR((AE_INFO, "Unknown Reference Class 0x%2.2X",
1044f1f6 202 ref_desc->reference.class));
23d3e055 203 ACPI_DUMP_ENTRY(ref_desc, ACPI_LV_INFO);
1da177e4
LT
204
205 status = AE_AML_INTERNAL;
206 break;
207 }
208
4be44fcd 209 return_ACPI_STATUS(status);
1da177e4
LT
210}
211
1da177e4
LT
212/*******************************************************************************
213 *
214 * FUNCTION: acpi_ex_store_object_to_index
215 *
216 * PARAMETERS: *source_desc - Value to be stored
217 * *dest_desc - Named object to receive the value
218 * walk_state - Current walk state
219 *
220 * RETURN: Status
221 *
222 * DESCRIPTION: Store the object to indexed Buffer or Package element
223 *
224 ******************************************************************************/
225
44f6c012 226static acpi_status
4be44fcd
LB
227acpi_ex_store_object_to_index(union acpi_operand_object *source_desc,
228 union acpi_operand_object *index_desc,
229 struct acpi_walk_state *walk_state)
1da177e4 230{
4be44fcd
LB
231 acpi_status status = AE_OK;
232 union acpi_operand_object *obj_desc;
233 union acpi_operand_object *new_desc;
234 u8 value = 0;
235 u32 i;
1da177e4 236
b229cf92 237 ACPI_FUNCTION_TRACE(ex_store_object_to_index);
1da177e4
LT
238
239 /*
240 * Destination must be a reference pointer, and
241 * must point to either a buffer or a package
242 */
243 switch (index_desc->reference.target_type) {
244 case ACPI_TYPE_PACKAGE:
245 /*
246 * Storing to a package element. Copy the object and replace
247 * any existing object with the new object. No implicit
248 * conversion is performed.
249 *
250 * The object at *(index_desc->Reference.Where) is the
251 * element within the package that is to be modified.
252 * The parent package object is at index_desc->Reference.Object
253 */
254 obj_desc = *(index_desc->reference.where);
255
3371c19c
BM
256 if (source_desc->common.type == ACPI_TYPE_LOCAL_REFERENCE &&
257 source_desc->reference.class == ACPI_REFCLASS_TABLE) {
bc7a36ab
LM
258
259 /* This is a DDBHandle, just add a reference to it */
260
261 acpi_ut_add_reference(source_desc);
262 new_desc = source_desc;
263 } else {
264 /* Normal object, copy it */
265
266 status =
267 acpi_ut_copy_iobject_to_iobject(source_desc,
268 &new_desc,
269 walk_state);
270 if (ACPI_FAILURE(status)) {
271 return_ACPI_STATUS(status);
272 }
1da177e4
LT
273 }
274
275 if (obj_desc) {
52fc0b02 276
1da177e4
LT
277 /* Decrement reference count by the ref count of the parent package */
278
4be44fcd
LB
279 for (i = 0; i < ((union acpi_operand_object *)
280 index_desc->reference.object)->common.
281 reference_count; i++) {
282 acpi_ut_remove_reference(obj_desc);
1da177e4
LT
283 }
284 }
285
286 *(index_desc->reference.where) = new_desc;
287
44f6c012 288 /* Increment ref count by the ref count of the parent package-1 */
1da177e4 289
4be44fcd
LB
290 for (i = 1; i < ((union acpi_operand_object *)
291 index_desc->reference.object)->common.
292 reference_count; i++) {
293 acpi_ut_add_reference(new_desc);
1da177e4
LT
294 }
295
296 break;
297
1da177e4
LT
298 case ACPI_TYPE_BUFFER_FIELD:
299
300 /*
301 * Store into a Buffer or String (not actually a real buffer_field)
302 * at a location defined by an Index.
303 *
304 * The first 8-bit element of the source object is written to the
305 * 8-bit Buffer location defined by the Index destination object,
306 * according to the ACPI 2.0 specification.
307 */
308
309 /*
310 * Make sure the target is a Buffer or String. An error should
311 * not happen here, since the reference_object was constructed
312 * by the INDEX_OP code.
313 */
314 obj_desc = index_desc->reference.object;
3371c19c
BM
315 if ((obj_desc->common.type != ACPI_TYPE_BUFFER) &&
316 (obj_desc->common.type != ACPI_TYPE_STRING)) {
4be44fcd 317 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
1da177e4
LT
318 }
319
320 /*
321 * The assignment of the individual elements will be slightly
322 * different for each source type.
323 */
3371c19c 324 switch (source_desc->common.type) {
1da177e4
LT
325 case ACPI_TYPE_INTEGER:
326
327 /* Use the least-significant byte of the integer */
328
329 value = (u8) (source_desc->integer.value);
330 break;
331
332 case ACPI_TYPE_BUFFER:
333 case ACPI_TYPE_STRING:
334
335 /* Note: Takes advantage of common string/buffer fields */
336
337 value = source_desc->buffer.pointer[0];
338 break;
339
340 default:
341
342 /* All other types are invalid */
343
b8e4d893
BM
344 ACPI_ERROR((AE_INFO,
345 "Source must be Integer/Buffer/String type, not %s",
346 acpi_ut_get_object_type_name(source_desc)));
4be44fcd 347 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
1da177e4
LT
348 }
349
350 /* Store the source value into the target buffer byte */
351
1044f1f6 352 obj_desc->buffer.pointer[index_desc->reference.value] = value;
1da177e4
LT
353 break;
354
1da177e4 355 default:
b229cf92 356 ACPI_ERROR((AE_INFO, "Target is not a Package or BufferField"));
1da177e4
LT
357 status = AE_AML_OPERAND_TYPE;
358 break;
359 }
360
4be44fcd 361 return_ACPI_STATUS(status);
1da177e4
LT
362}
363
1da177e4
LT
364/*******************************************************************************
365 *
366 * FUNCTION: acpi_ex_store_object_to_node
367 *
368 * PARAMETERS: source_desc - Value to be stored
ba494bee 369 * node - Named object to receive the value
1da177e4
LT
370 * walk_state - Current walk state
371 * implicit_conversion - Perform implicit conversion (yes/no)
372 *
373 * RETURN: Status
374 *
375 * DESCRIPTION: Store the object to the named object.
376 *
377 * The Assignment of an object to a named object is handled here
378 * The value passed in will replace the current value (if any)
379 * with the input value.
380 *
381 * When storing into an object the data is converted to the
73a3090a 382 * target object type then stored in the object. This means
1da177e4 383 * that the target object type (for an initialized target) will
217e7268
BM
384 * not be changed by a store operation. A copy_object can change
385 * the target type, however.
386 *
387 * The implicit_conversion flag is set to NO/FALSE only when
388 * storing to an arg_x -- as per the rules of the ACPI spec.
1da177e4
LT
389 *
390 * Assumes parameters are already validated.
391 *
392 ******************************************************************************/
393
394acpi_status
4be44fcd
LB
395acpi_ex_store_object_to_node(union acpi_operand_object *source_desc,
396 struct acpi_namespace_node *node,
397 struct acpi_walk_state *walk_state,
398 u8 implicit_conversion)
1da177e4 399{
4be44fcd
LB
400 acpi_status status = AE_OK;
401 union acpi_operand_object *target_desc;
402 union acpi_operand_object *new_desc;
403 acpi_object_type target_type;
1da177e4 404
b229cf92 405 ACPI_FUNCTION_TRACE_PTR(ex_store_object_to_node, source_desc);
1da177e4 406
44f6c012
RM
407 /* Get current type of the node, and object attached to Node */
408
4be44fcd
LB
409 target_type = acpi_ns_get_type(node);
410 target_desc = acpi_ns_get_attached_object(node);
1da177e4 411
217e7268 412 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Storing %p (%s) to node %p (%s)\n",
4be44fcd
LB
413 source_desc,
414 acpi_ut_get_object_type_name(source_desc), node,
415 acpi_ut_get_type_name(target_type)));
1da177e4
LT
416
417 /*
418 * Resolve the source object to an actual value
419 * (If it is a reference object)
420 */
4be44fcd
LB
421 status = acpi_ex_resolve_object(&source_desc, target_type, walk_state);
422 if (ACPI_FAILURE(status)) {
423 return_ACPI_STATUS(status);
1da177e4
LT
424 }
425
44f6c012
RM
426 /* Do the actual store operation */
427
1da177e4 428 switch (target_type) {
1da177e4
LT
429 case ACPI_TYPE_INTEGER:
430 case ACPI_TYPE_STRING:
431 case ACPI_TYPE_BUFFER:
432
433 /*
217e7268
BM
434 * The simple data types all support implicit source operand
435 * conversion before the store.
1da177e4 436 */
217e7268
BM
437
438 if ((walk_state->opcode == AML_COPY_OP) || !implicit_conversion) {
439 /*
440 * However, copy_object and Stores to arg_x do not perform
441 * an implicit conversion, as per the ACPI specification.
442 * A direct store is performed instead.
443 */
444 status = acpi_ex_store_direct_to_node(source_desc, node,
445 walk_state);
446 break;
447 }
448
449 /* Store with implicit source operand conversion support */
450
4be44fcd
LB
451 status =
452 acpi_ex_store_object_to_object(source_desc, target_desc,
453 &new_desc, walk_state);
454 if (ACPI_FAILURE(status)) {
455 return_ACPI_STATUS(status);
1da177e4
LT
456 }
457
458 if (new_desc != target_desc) {
459 /*
460 * Store the new new_desc as the new value of the Name, and set
461 * the Name's type to that of the value being stored in it.
462 * source_desc reference count is incremented by attach_object.
463 *
217e7268
BM
464 * Note: This may change the type of the node if an explicit
465 * store has been performed such that the node/object type
466 * has been changed.
1da177e4 467 */
217e7268
BM
468 status = acpi_ns_attach_object(node, new_desc,
469 new_desc->common.type);
4be44fcd
LB
470
471 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
472 "Store %s into %s via Convert/Attach\n",
473 acpi_ut_get_object_type_name
474 (source_desc),
475 acpi_ut_get_object_type_name
476 (new_desc)));
1da177e4
LT
477 }
478 break;
479
217e7268
BM
480 case ACPI_TYPE_BUFFER_FIELD:
481 case ACPI_TYPE_LOCAL_REGION_FIELD:
482 case ACPI_TYPE_LOCAL_BANK_FIELD:
483 case ACPI_TYPE_LOCAL_INDEX_FIELD:
484 /*
485 * For all fields, always write the source data to the target
486 * field. Any required implicit source operand conversion is
487 * performed in the function below as necessary. Note, field
488 * objects must retain their original type permanently.
489 */
490 status = acpi_ex_write_data_to_field(source_desc, target_desc,
491 &walk_state->result_obj);
492 break;
1da177e4 493
217e7268 494 default:
3f654bad
BM
495 /*
496 * No conversions for all other types. Directly store a copy of
217e7268
BM
497 * the source object. This is the ACPI spec-defined behavior for
498 * the copy_object operator.
3f654bad 499 *
217e7268
BM
500 * NOTE: For the Store operator, this is a departure from the
501 * ACPI spec, which states "If conversion is impossible, abort
502 * the running control method". Instead, this code implements
503 * "If conversion is impossible, treat the Store operation as
504 * a CopyObject".
3f654bad 505 */
217e7268
BM
506 status = acpi_ex_store_direct_to_node(source_desc, node,
507 walk_state);
1da177e4
LT
508 break;
509 }
510
4be44fcd 511 return_ACPI_STATUS(status);
1da177e4 512}
217e7268
BM
513
514/*******************************************************************************
515 *
516 * FUNCTION: acpi_ex_store_direct_to_node
517 *
518 * PARAMETERS: source_desc - Value to be stored
519 * node - Named object to receive the value
520 * walk_state - Current walk state
521 *
522 * RETURN: Status
523 *
524 * DESCRIPTION: "Store" an object directly to a node. This involves a copy
525 * and an attach.
526 *
527 ******************************************************************************/
528
529static acpi_status
530acpi_ex_store_direct_to_node(union acpi_operand_object *source_desc,
531 struct acpi_namespace_node *node,
532 struct acpi_walk_state *walk_state)
533{
534 acpi_status status;
535 union acpi_operand_object *new_desc;
536
537 ACPI_FUNCTION_TRACE(ex_store_direct_to_node);
538
539 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
540 "Storing [%s] (%p) directly into node [%s] (%p)"
541 " with no implicit conversion\n",
542 acpi_ut_get_object_type_name(source_desc),
543 source_desc, acpi_ut_get_type_name(node->type),
544 node));
545
546 /* Copy the source object to a new object */
547
548 status =
549 acpi_ut_copy_iobject_to_iobject(source_desc, &new_desc, walk_state);
550 if (ACPI_FAILURE(status)) {
551 return_ACPI_STATUS(status);
552 }
553
554 /* Attach the new object to the node */
555
556 status = acpi_ns_attach_object(node, new_desc, new_desc->common.type);
557 acpi_ut_remove_reference(new_desc);
558 return_ACPI_STATUS(status);
559}