Merge master.kernel.org:/pub/scm/linux/kernel/git/sam/kbuild
[linux-2.6-block.git] / drivers / acpi / dispatcher / dsfield.c
1 /******************************************************************************
2  *
3  * Module Name: dsfield - Dispatcher field routines
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
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
44
45 #include <acpi/acpi.h>
46 #include <acpi/amlcode.h>
47 #include <acpi/acdispat.h>
48 #include <acpi/acinterp.h>
49 #include <acpi/acnamesp.h>
50 #include <acpi/acparser.h>
51
52
53 #define _COMPONENT          ACPI_DISPATCHER
54          ACPI_MODULE_NAME    ("dsfield")
55
56 /* Local prototypes */
57
58 static acpi_status
59 acpi_ds_get_field_names (
60         struct acpi_create_field_info   *info,
61         struct acpi_walk_state          *walk_state,
62         union acpi_parse_object         *arg);
63
64
65 /*******************************************************************************
66  *
67  * FUNCTION:    acpi_ds_create_buffer_field
68  *
69  * PARAMETERS:  Op                  - Current parse op (create_xXField)
70  *              walk_state          - Current state
71  *
72  * RETURN:      Status
73  *
74  * DESCRIPTION: Execute the create_field operators:
75  *              create_bit_field_op,
76  *              create_byte_field_op,
77  *              create_word_field_op,
78  *              create_dword_field_op,
79  *              create_qword_field_op,
80  *              create_field_op     (all of which define a field in a buffer)
81  *
82  ******************************************************************************/
83
84 acpi_status
85 acpi_ds_create_buffer_field (
86         union acpi_parse_object         *op,
87         struct acpi_walk_state          *walk_state)
88 {
89         union acpi_parse_object         *arg;
90         struct acpi_namespace_node      *node;
91         acpi_status                     status;
92         union acpi_operand_object       *obj_desc;
93         union acpi_operand_object       *second_desc = NULL;
94         u32                             flags;
95
96
97         ACPI_FUNCTION_TRACE ("ds_create_buffer_field");
98
99
100         /* Get the name_string argument */
101
102         if (op->common.aml_opcode == AML_CREATE_FIELD_OP) {
103                 arg = acpi_ps_get_arg (op, 3);
104         }
105         else {
106                 /* Create Bit/Byte/Word/Dword field */
107
108                 arg = acpi_ps_get_arg (op, 2);
109         }
110
111         if (!arg) {
112                 return_ACPI_STATUS (AE_AML_NO_OPERAND);
113         }
114
115         if (walk_state->deferred_node) {
116                 node = walk_state->deferred_node;
117                 status = AE_OK;
118         }
119         else {
120                 /*
121                  * During the load phase, we want to enter the name of the field into
122                  * the namespace.  During the execute phase (when we evaluate the size
123                  * operand), we want to lookup the name
124                  */
125                 if (walk_state->parse_flags & ACPI_PARSE_EXECUTE) {
126                         flags = ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE;
127                 }
128                 else {
129                         flags = ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE |
130                                         ACPI_NS_ERROR_IF_FOUND;
131                 }
132
133                 /*
134                  * Enter the name_string into the namespace
135                  */
136                 status = acpi_ns_lookup (walk_state->scope_info, arg->common.value.string,
137                                  ACPI_TYPE_ANY, ACPI_IMODE_LOAD_PASS1,
138                                  flags, walk_state, &(node));
139                 if (ACPI_FAILURE (status)) {
140                         ACPI_REPORT_NSERROR (arg->common.value.string, status);
141                         return_ACPI_STATUS (status);
142                 }
143         }
144
145         /* We could put the returned object (Node) on the object stack for later,
146          * but for now, we will put it in the "op" object that the parser uses,
147          * so we can get it again at the end of this scope
148          */
149         op->common.node = node;
150
151         /*
152          * If there is no object attached to the node, this node was just created
153          * and we need to create the field object.  Otherwise, this was a lookup
154          * of an existing node and we don't want to create the field object again.
155          */
156         obj_desc = acpi_ns_get_attached_object (node);
157         if (obj_desc) {
158                 return_ACPI_STATUS (AE_OK);
159         }
160
161         /*
162          * The Field definition is not fully parsed at this time.
163          * (We must save the address of the AML for the buffer and index operands)
164          */
165
166         /* Create the buffer field object */
167
168         obj_desc = acpi_ut_create_internal_object (ACPI_TYPE_BUFFER_FIELD);
169         if (!obj_desc) {
170                 status = AE_NO_MEMORY;
171                 goto cleanup;
172         }
173
174         /*
175          * Remember location in AML stream of the field unit
176          * opcode and operands -- since the buffer and index
177          * operands must be evaluated.
178          */
179         second_desc                 = obj_desc->common.next_object;
180         second_desc->extra.aml_start = op->named.data;
181         second_desc->extra.aml_length = op->named.length;
182         obj_desc->buffer_field.node = node;
183
184         /* Attach constructed field descriptors to parent node */
185
186         status = acpi_ns_attach_object (node, obj_desc, ACPI_TYPE_BUFFER_FIELD);
187         if (ACPI_FAILURE (status)) {
188                 goto cleanup;
189         }
190
191
192 cleanup:
193
194         /* Remove local reference to the object */
195
196         acpi_ut_remove_reference (obj_desc);
197         return_ACPI_STATUS (status);
198 }
199
200
201 /*******************************************************************************
202  *
203  * FUNCTION:    acpi_ds_get_field_names
204  *
205  * PARAMETERS:  Info            - create_field info structure
206  *  `           walk_state      - Current method state
207  *              Arg             - First parser arg for the field name list
208  *
209  * RETURN:      Status
210  *
211  * DESCRIPTION: Process all named fields in a field declaration.  Names are
212  *              entered into the namespace.
213  *
214  ******************************************************************************/
215
216 static acpi_status
217 acpi_ds_get_field_names (
218         struct acpi_create_field_info   *info,
219         struct acpi_walk_state          *walk_state,
220         union acpi_parse_object         *arg)
221 {
222         acpi_status                     status;
223         acpi_integer                    position;
224
225
226         ACPI_FUNCTION_TRACE_PTR ("ds_get_field_names", info);
227
228
229         /* First field starts at bit zero */
230
231         info->field_bit_position = 0;
232
233         /* Process all elements in the field list (of parse nodes) */
234
235         while (arg) {
236                 /*
237                  * Three types of field elements are handled:
238                  * 1) Offset - specifies a bit offset
239                  * 2) access_as - changes the access mode
240                  * 3) Name - Enters a new named field into the namespace
241                  */
242                 switch (arg->common.aml_opcode) {
243                 case AML_INT_RESERVEDFIELD_OP:
244
245                         position = (acpi_integer) info->field_bit_position
246                                          + (acpi_integer) arg->common.value.size;
247
248                         if (position > ACPI_UINT32_MAX) {
249                                 ACPI_REPORT_ERROR ((
250                                         "Bit offset within field too large (> 0xFFFFFFFF)\n"));
251                                 return_ACPI_STATUS (AE_SUPPORT);
252                         }
253
254                         info->field_bit_position = (u32) position;
255                         break;
256
257
258                 case AML_INT_ACCESSFIELD_OP:
259
260                         /*
261                          * Get a new access_type and access_attribute -- to be used for all
262                          * field units that follow, until field end or another access_as
263                          * keyword.
264                          *
265                          * In field_flags, preserve the flag bits other than the
266                          * ACCESS_TYPE bits
267                          */
268                         info->field_flags = (u8)
269                                 ((info->field_flags & ~(AML_FIELD_ACCESS_TYPE_MASK)) |
270                                 ((u8) ((u32) arg->common.value.integer >> 8)));
271
272                         info->attribute = (u8) (arg->common.value.integer);
273                         break;
274
275
276                 case AML_INT_NAMEDFIELD_OP:
277
278                         /* Lookup the name */
279
280                         status = acpi_ns_lookup (walk_state->scope_info,
281                                           (char *) &arg->named.name,
282                                           info->field_type, ACPI_IMODE_EXECUTE,
283                                           ACPI_NS_DONT_OPEN_SCOPE,
284                                           walk_state, &info->field_node);
285                         if (ACPI_FAILURE (status)) {
286                                 ACPI_REPORT_NSERROR ((char *) &arg->named.name, status);
287                                 if (status != AE_ALREADY_EXISTS) {
288                                         return_ACPI_STATUS (status);
289                                 }
290
291                                 /* Already exists, ignore error */
292                         }
293                         else {
294                                 arg->common.node = info->field_node;
295                                 info->field_bit_length = arg->common.value.size;
296
297                                 /* Create and initialize an object for the new Field Node */
298
299                                 status = acpi_ex_prep_field_value (info);
300                                 if (ACPI_FAILURE (status)) {
301                                         return_ACPI_STATUS (status);
302                                 }
303                         }
304
305                         /* Keep track of bit position for the next field */
306
307                         position = (acpi_integer) info->field_bit_position
308                                          + (acpi_integer) arg->common.value.size;
309
310                         if (position > ACPI_UINT32_MAX) {
311                                 ACPI_REPORT_ERROR ((
312                                         "Field [%4.4s] bit offset too large (> 0xFFFFFFFF)\n",
313                                         (char *) &info->field_node->name));
314                                 return_ACPI_STATUS (AE_SUPPORT);
315                         }
316
317                         info->field_bit_position += info->field_bit_length;
318                         break;
319
320
321                 default:
322
323                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
324                                 "Invalid opcode in field list: %X\n",
325                                 arg->common.aml_opcode));
326                         return_ACPI_STATUS (AE_AML_BAD_OPCODE);
327                 }
328
329                 arg = arg->common.next;
330         }
331
332         return_ACPI_STATUS (AE_OK);
333 }
334
335
336 /*******************************************************************************
337  *
338  * FUNCTION:    acpi_ds_create_field
339  *
340  * PARAMETERS:  Op              - Op containing the Field definition and args
341  *              region_node     - Object for the containing Operation Region
342  *  `           walk_state      - Current method state
343  *
344  * RETURN:      Status
345  *
346  * DESCRIPTION: Create a new field in the specified operation region
347  *
348  ******************************************************************************/
349
350 acpi_status
351 acpi_ds_create_field (
352         union acpi_parse_object         *op,
353         struct acpi_namespace_node      *region_node,
354         struct acpi_walk_state          *walk_state)
355 {
356         acpi_status                     status;
357         union acpi_parse_object         *arg;
358         struct acpi_create_field_info   info;
359
360
361         ACPI_FUNCTION_TRACE_PTR ("ds_create_field", op);
362
363
364         /* First arg is the name of the parent op_region (must already exist) */
365
366         arg = op->common.value.arg;
367         if (!region_node) {
368                 status = acpi_ns_lookup (walk_state->scope_info, arg->common.value.name,
369                                   ACPI_TYPE_REGION, ACPI_IMODE_EXECUTE,
370                                   ACPI_NS_SEARCH_PARENT, walk_state, &region_node);
371                 if (ACPI_FAILURE (status)) {
372                         ACPI_REPORT_NSERROR (arg->common.value.name, status);
373                         return_ACPI_STATUS (status);
374                 }
375         }
376
377         /* Second arg is the field flags */
378
379         arg = arg->common.next;
380         info.field_flags = (u8) arg->common.value.integer;
381         info.attribute = 0;
382
383         /* Each remaining arg is a Named Field */
384
385         info.field_type = ACPI_TYPE_LOCAL_REGION_FIELD;
386         info.region_node = region_node;
387
388         status = acpi_ds_get_field_names (&info, walk_state, arg->common.next);
389
390         return_ACPI_STATUS (status);
391 }
392
393
394 /*******************************************************************************
395  *
396  * FUNCTION:    acpi_ds_init_field_objects
397  *
398  * PARAMETERS:  Op              - Op containing the Field definition and args
399  *  `           walk_state      - Current method state
400  *
401  * RETURN:      Status
402  *
403  * DESCRIPTION: For each "Field Unit" name in the argument list that is
404  *              part of the field declaration, enter the name into the
405  *              namespace.
406  *
407  ******************************************************************************/
408
409 acpi_status
410 acpi_ds_init_field_objects (
411         union acpi_parse_object         *op,
412         struct acpi_walk_state          *walk_state)
413 {
414         acpi_status                     status;
415         union acpi_parse_object         *arg = NULL;
416         struct acpi_namespace_node      *node;
417         u8                              type = 0;
418
419
420         ACPI_FUNCTION_TRACE_PTR ("ds_init_field_objects", op);
421
422
423         switch (walk_state->opcode) {
424         case AML_FIELD_OP:
425                 arg = acpi_ps_get_arg (op, 2);
426                 type = ACPI_TYPE_LOCAL_REGION_FIELD;
427                 break;
428
429         case AML_BANK_FIELD_OP:
430                 arg = acpi_ps_get_arg (op, 4);
431                 type = ACPI_TYPE_LOCAL_BANK_FIELD;
432                 break;
433
434         case AML_INDEX_FIELD_OP:
435                 arg = acpi_ps_get_arg (op, 3);
436                 type = ACPI_TYPE_LOCAL_INDEX_FIELD;
437                 break;
438
439         default:
440                 return_ACPI_STATUS (AE_BAD_PARAMETER);
441         }
442
443         /*
444          * Walk the list of entries in the field_list
445          */
446         while (arg) {
447                 /* Ignore OFFSET and ACCESSAS terms here */
448
449                 if (arg->common.aml_opcode == AML_INT_NAMEDFIELD_OP) {
450                         status = acpi_ns_lookup (walk_state->scope_info,
451                                           (char *) &arg->named.name,
452                                           type, ACPI_IMODE_LOAD_PASS1,
453                                           ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE |
454                                           ACPI_NS_ERROR_IF_FOUND,
455                                           walk_state, &node);
456                         if (ACPI_FAILURE (status)) {
457                                 ACPI_REPORT_NSERROR ((char *) &arg->named.name, status);
458                                 if (status != AE_ALREADY_EXISTS) {
459                                         return_ACPI_STATUS (status);
460                                 }
461
462                                 /* Name already exists, just ignore this error */
463
464                                 status = AE_OK;
465                         }
466
467                         arg->common.node = node;
468                 }
469
470                 /* Move to next field in the list */
471
472                 arg = arg->common.next;
473         }
474
475         return_ACPI_STATUS (AE_OK);
476 }
477
478
479 /*******************************************************************************
480  *
481  * FUNCTION:    acpi_ds_create_bank_field
482  *
483  * PARAMETERS:  Op              - Op containing the Field definition and args
484  *              region_node     - Object for the containing Operation Region
485  *  `           walk_state      - Current method state
486  *
487  * RETURN:      Status
488  *
489  * DESCRIPTION: Create a new bank field in the specified operation region
490  *
491  ******************************************************************************/
492
493 acpi_status
494 acpi_ds_create_bank_field (
495         union acpi_parse_object         *op,
496         struct acpi_namespace_node      *region_node,
497         struct acpi_walk_state          *walk_state)
498 {
499         acpi_status                     status;
500         union acpi_parse_object         *arg;
501         struct acpi_create_field_info   info;
502
503
504         ACPI_FUNCTION_TRACE_PTR ("ds_create_bank_field", op);
505
506
507         /* First arg is the name of the parent op_region (must already exist) */
508
509         arg = op->common.value.arg;
510         if (!region_node) {
511                 status = acpi_ns_lookup (walk_state->scope_info, arg->common.value.name,
512                                   ACPI_TYPE_REGION, ACPI_IMODE_EXECUTE,
513                                   ACPI_NS_SEARCH_PARENT, walk_state, &region_node);
514                 if (ACPI_FAILURE (status)) {
515                         ACPI_REPORT_NSERROR (arg->common.value.name, status);
516                         return_ACPI_STATUS (status);
517                 }
518         }
519
520         /* Second arg is the Bank Register (Field) (must already exist) */
521
522         arg = arg->common.next;
523         status = acpi_ns_lookup (walk_state->scope_info, arg->common.value.string,
524                           ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
525                           ACPI_NS_SEARCH_PARENT, walk_state, &info.register_node);
526         if (ACPI_FAILURE (status)) {
527                 ACPI_REPORT_NSERROR (arg->common.value.string, status);
528                 return_ACPI_STATUS (status);
529         }
530
531         /* Third arg is the bank_value */
532
533         arg = arg->common.next;
534         info.bank_value = (u32) arg->common.value.integer;
535
536         /* Fourth arg is the field flags */
537
538         arg = arg->common.next;
539         info.field_flags = (u8) arg->common.value.integer;
540
541         /* Each remaining arg is a Named Field */
542
543         info.field_type = ACPI_TYPE_LOCAL_BANK_FIELD;
544         info.region_node = region_node;
545
546         status = acpi_ds_get_field_names (&info, walk_state, arg->common.next);
547
548         return_ACPI_STATUS (status);
549 }
550
551
552 /*******************************************************************************
553  *
554  * FUNCTION:    acpi_ds_create_index_field
555  *
556  * PARAMETERS:  Op              - Op containing the Field definition and args
557  *              region_node     - Object for the containing Operation Region
558  *  `           walk_state      - Current method state
559  *
560  * RETURN:      Status
561  *
562  * DESCRIPTION: Create a new index field in the specified operation region
563  *
564  ******************************************************************************/
565
566 acpi_status
567 acpi_ds_create_index_field (
568         union acpi_parse_object         *op,
569         struct acpi_namespace_node      *region_node,
570         struct acpi_walk_state          *walk_state)
571 {
572         acpi_status                     status;
573         union acpi_parse_object         *arg;
574         struct acpi_create_field_info   info;
575
576
577         ACPI_FUNCTION_TRACE_PTR ("ds_create_index_field", op);
578
579
580         /* First arg is the name of the Index register (must already exist) */
581
582         arg = op->common.value.arg;
583         status = acpi_ns_lookup (walk_state->scope_info, arg->common.value.string,
584                           ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
585                           ACPI_NS_SEARCH_PARENT, walk_state, &info.register_node);
586         if (ACPI_FAILURE (status)) {
587                 ACPI_REPORT_NSERROR (arg->common.value.string, status);
588                 return_ACPI_STATUS (status);
589         }
590
591         /* Second arg is the data register (must already exist) */
592
593         arg = arg->common.next;
594         status = acpi_ns_lookup (walk_state->scope_info, arg->common.value.string,
595                           ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
596                           ACPI_NS_SEARCH_PARENT, walk_state, &info.data_register_node);
597         if (ACPI_FAILURE (status)) {
598                 ACPI_REPORT_NSERROR (arg->common.value.string, status);
599                 return_ACPI_STATUS (status);
600         }
601
602         /* Next arg is the field flags */
603
604         arg = arg->common.next;
605         info.field_flags = (u8) arg->common.value.integer;
606
607         /* Each remaining arg is a Named Field */
608
609         info.field_type = ACPI_TYPE_LOCAL_INDEX_FIELD;
610         info.region_node = region_node;
611
612         status = acpi_ds_get_field_names (&info, walk_state, arg->common.next);
613
614         return_ACPI_STATUS (status);
615 }
616
617