nsrepair2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /******************************************************************************
  2. *
  3. * Module Name: nsrepair2 - Repair for objects returned by specific
  4. * predefined methods
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2011, Intel Corp.
  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. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #include "acnamesp.h"
  46. #define _COMPONENT ACPI_NAMESPACE
  47. ACPI_MODULE_NAME("nsrepair2")
  48. /*
  49. * Information structure and handler for ACPI predefined names that can
  50. * be repaired on a per-name basis.
  51. */
  52. typedef
  53. acpi_status(*acpi_repair_function) (struct acpi_predefined_data *data,
  54. union acpi_operand_object **return_object_ptr);
  55. typedef struct acpi_repair_info {
  56. char name[ACPI_NAME_SIZE];
  57. acpi_repair_function repair_function;
  58. } acpi_repair_info;
  59. /* Local prototypes */
  60. static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
  61. acpi_namespace_node
  62. *node);
  63. static acpi_status
  64. acpi_ns_repair_ALR(struct acpi_predefined_data *data,
  65. union acpi_operand_object **return_object_ptr);
  66. static acpi_status
  67. acpi_ns_repair_CID(struct acpi_predefined_data *data,
  68. union acpi_operand_object **return_object_ptr);
  69. static acpi_status
  70. acpi_ns_repair_FDE(struct acpi_predefined_data *data,
  71. union acpi_operand_object **return_object_ptr);
  72. static acpi_status
  73. acpi_ns_repair_HID(struct acpi_predefined_data *data,
  74. union acpi_operand_object **return_object_ptr);
  75. static acpi_status
  76. acpi_ns_repair_PSS(struct acpi_predefined_data *data,
  77. union acpi_operand_object **return_object_ptr);
  78. static acpi_status
  79. acpi_ns_repair_TSS(struct acpi_predefined_data *data,
  80. union acpi_operand_object **return_object_ptr);
  81. static acpi_status
  82. acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
  83. union acpi_operand_object *return_object,
  84. u32 expected_count,
  85. u32 sort_index,
  86. u8 sort_direction, char *sort_key_name);
  87. static void
  88. acpi_ns_sort_list(union acpi_operand_object **elements,
  89. u32 count, u32 index, u8 sort_direction);
  90. /* Values for sort_direction above */
  91. #define ACPI_SORT_ASCENDING 0
  92. #define ACPI_SORT_DESCENDING 1
  93. /*
  94. * This table contains the names of the predefined methods for which we can
  95. * perform more complex repairs.
  96. *
  97. * As necessary:
  98. *
  99. * _ALR: Sort the list ascending by ambient_illuminance
  100. * _CID: Strings: uppercase all, remove any leading asterisk
  101. * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
  102. * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
  103. * _HID: Strings: uppercase all, remove any leading asterisk
  104. * _PSS: Sort the list descending by Power
  105. * _TSS: Sort the list descending by Power
  106. *
  107. * Names that must be packages, but cannot be sorted:
  108. *
  109. * _BCL: Values are tied to the Package index where they appear, and cannot
  110. * be moved or sorted. These index values are used for _BQC and _BCM.
  111. * However, we can fix the case where a buffer is returned, by converting
  112. * it to a Package of integers.
  113. */
  114. static const struct acpi_repair_info acpi_ns_repairable_names[] = {
  115. {"_ALR", acpi_ns_repair_ALR},
  116. {"_CID", acpi_ns_repair_CID},
  117. {"_FDE", acpi_ns_repair_FDE},
  118. {"_GTM", acpi_ns_repair_FDE}, /* _GTM has same repair as _FDE */
  119. {"_HID", acpi_ns_repair_HID},
  120. {"_PSS", acpi_ns_repair_PSS},
  121. {"_TSS", acpi_ns_repair_TSS},
  122. {{0, 0, 0, 0}, NULL} /* Table terminator */
  123. };
  124. #define ACPI_FDE_FIELD_COUNT 5
  125. #define ACPI_FDE_BYTE_BUFFER_SIZE 5
  126. #define ACPI_FDE_DWORD_BUFFER_SIZE (ACPI_FDE_FIELD_COUNT * sizeof (u32))
  127. /******************************************************************************
  128. *
  129. * FUNCTION: acpi_ns_complex_repairs
  130. *
  131. * PARAMETERS: Data - Pointer to validation data structure
  132. * Node - Namespace node for the method/object
  133. * validate_status - Original status of earlier validation
  134. * return_object_ptr - Pointer to the object returned from the
  135. * evaluation of a method or object
  136. *
  137. * RETURN: Status. AE_OK if repair was successful. If name is not
  138. * matched, validate_status is returned.
  139. *
  140. * DESCRIPTION: Attempt to repair/convert a return object of a type that was
  141. * not expected.
  142. *
  143. *****************************************************************************/
  144. acpi_status
  145. acpi_ns_complex_repairs(struct acpi_predefined_data *data,
  146. struct acpi_namespace_node *node,
  147. acpi_status validate_status,
  148. union acpi_operand_object **return_object_ptr)
  149. {
  150. const struct acpi_repair_info *predefined;
  151. acpi_status status;
  152. /* Check if this name is in the list of repairable names */
  153. predefined = acpi_ns_match_repairable_name(node);
  154. if (!predefined) {
  155. return (validate_status);
  156. }
  157. status = predefined->repair_function(data, return_object_ptr);
  158. return (status);
  159. }
  160. /******************************************************************************
  161. *
  162. * FUNCTION: acpi_ns_match_repairable_name
  163. *
  164. * PARAMETERS: Node - Namespace node for the method/object
  165. *
  166. * RETURN: Pointer to entry in repair table. NULL indicates not found.
  167. *
  168. * DESCRIPTION: Check an object name against the repairable object list.
  169. *
  170. *****************************************************************************/
  171. static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
  172. acpi_namespace_node
  173. *node)
  174. {
  175. const struct acpi_repair_info *this_name;
  176. /* Search info table for a repairable predefined method/object name */
  177. this_name = acpi_ns_repairable_names;
  178. while (this_name->repair_function) {
  179. if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
  180. return (this_name);
  181. }
  182. this_name++;
  183. }
  184. return (NULL); /* Not found */
  185. }
  186. /******************************************************************************
  187. *
  188. * FUNCTION: acpi_ns_repair_ALR
  189. *
  190. * PARAMETERS: Data - Pointer to validation data structure
  191. * return_object_ptr - Pointer to the object returned from the
  192. * evaluation of a method or object
  193. *
  194. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  195. *
  196. * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
  197. * ascending by the ambient illuminance values.
  198. *
  199. *****************************************************************************/
  200. static acpi_status
  201. acpi_ns_repair_ALR(struct acpi_predefined_data *data,
  202. union acpi_operand_object **return_object_ptr)
  203. {
  204. union acpi_operand_object *return_object = *return_object_ptr;
  205. acpi_status status;
  206. status = acpi_ns_check_sorted_list(data, return_object, 2, 1,
  207. ACPI_SORT_ASCENDING,
  208. "AmbientIlluminance");
  209. return (status);
  210. }
  211. /******************************************************************************
  212. *
  213. * FUNCTION: acpi_ns_repair_FDE
  214. *
  215. * PARAMETERS: Data - Pointer to validation data structure
  216. * return_object_ptr - Pointer to the object returned from the
  217. * evaluation of a method or object
  218. *
  219. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  220. *
  221. * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
  222. * value is a Buffer of 5 DWORDs. This function repairs a common
  223. * problem where the return value is a Buffer of BYTEs, not
  224. * DWORDs.
  225. *
  226. *****************************************************************************/
  227. static acpi_status
  228. acpi_ns_repair_FDE(struct acpi_predefined_data *data,
  229. union acpi_operand_object **return_object_ptr)
  230. {
  231. union acpi_operand_object *return_object = *return_object_ptr;
  232. union acpi_operand_object *buffer_object;
  233. u8 *byte_buffer;
  234. u32 *dword_buffer;
  235. u32 i;
  236. ACPI_FUNCTION_NAME(ns_repair_FDE);
  237. switch (return_object->common.type) {
  238. case ACPI_TYPE_BUFFER:
  239. /* This is the expected type. Length should be (at least) 5 DWORDs */
  240. if (return_object->buffer.length >= ACPI_FDE_DWORD_BUFFER_SIZE) {
  241. return (AE_OK);
  242. }
  243. /* We can only repair if we have exactly 5 BYTEs */
  244. if (return_object->buffer.length != ACPI_FDE_BYTE_BUFFER_SIZE) {
  245. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
  246. data->node_flags,
  247. "Incorrect return buffer length %u, expected %u",
  248. return_object->buffer.length,
  249. ACPI_FDE_DWORD_BUFFER_SIZE));
  250. return (AE_AML_OPERAND_TYPE);
  251. }
  252. /* Create the new (larger) buffer object */
  253. buffer_object =
  254. acpi_ut_create_buffer_object(ACPI_FDE_DWORD_BUFFER_SIZE);
  255. if (!buffer_object) {
  256. return (AE_NO_MEMORY);
  257. }
  258. /* Expand each byte to a DWORD */
  259. byte_buffer = return_object->buffer.pointer;
  260. dword_buffer =
  261. ACPI_CAST_PTR(u32, buffer_object->buffer.pointer);
  262. for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++) {
  263. *dword_buffer = (u32) *byte_buffer;
  264. dword_buffer++;
  265. byte_buffer++;
  266. }
  267. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  268. "%s Expanded Byte Buffer to expected DWord Buffer\n",
  269. data->pathname));
  270. break;
  271. default:
  272. return (AE_AML_OPERAND_TYPE);
  273. }
  274. /* Delete the original return object, return the new buffer object */
  275. acpi_ut_remove_reference(return_object);
  276. *return_object_ptr = buffer_object;
  277. data->flags |= ACPI_OBJECT_REPAIRED;
  278. return (AE_OK);
  279. }
  280. /******************************************************************************
  281. *
  282. * FUNCTION: acpi_ns_repair_CID
  283. *
  284. * PARAMETERS: Data - Pointer to validation data structure
  285. * return_object_ptr - Pointer to the object returned from the
  286. * evaluation of a method or object
  287. *
  288. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  289. *
  290. * DESCRIPTION: Repair for the _CID object. If a string, ensure that all
  291. * letters are uppercase and that there is no leading asterisk.
  292. * If a Package, ensure same for all string elements.
  293. *
  294. *****************************************************************************/
  295. static acpi_status
  296. acpi_ns_repair_CID(struct acpi_predefined_data *data,
  297. union acpi_operand_object **return_object_ptr)
  298. {
  299. acpi_status status;
  300. union acpi_operand_object *return_object = *return_object_ptr;
  301. union acpi_operand_object **element_ptr;
  302. union acpi_operand_object *original_element;
  303. u16 original_ref_count;
  304. u32 i;
  305. /* Check for _CID as a simple string */
  306. if (return_object->common.type == ACPI_TYPE_STRING) {
  307. status = acpi_ns_repair_HID(data, return_object_ptr);
  308. return (status);
  309. }
  310. /* Exit if not a Package */
  311. if (return_object->common.type != ACPI_TYPE_PACKAGE) {
  312. return (AE_OK);
  313. }
  314. /* Examine each element of the _CID package */
  315. element_ptr = return_object->package.elements;
  316. for (i = 0; i < return_object->package.count; i++) {
  317. original_element = *element_ptr;
  318. original_ref_count = original_element->common.reference_count;
  319. status = acpi_ns_repair_HID(data, element_ptr);
  320. if (ACPI_FAILURE(status)) {
  321. return (status);
  322. }
  323. /* Take care with reference counts */
  324. if (original_element != *element_ptr) {
  325. /* Element was replaced */
  326. (*element_ptr)->common.reference_count =
  327. original_ref_count;
  328. acpi_ut_remove_reference(original_element);
  329. }
  330. element_ptr++;
  331. }
  332. return (AE_OK);
  333. }
  334. /******************************************************************************
  335. *
  336. * FUNCTION: acpi_ns_repair_HID
  337. *
  338. * PARAMETERS: Data - Pointer to validation data structure
  339. * return_object_ptr - Pointer to the object returned from the
  340. * evaluation of a method or object
  341. *
  342. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  343. *
  344. * DESCRIPTION: Repair for the _HID object. If a string, ensure that all
  345. * letters are uppercase and that there is no leading asterisk.
  346. *
  347. *****************************************************************************/
  348. static acpi_status
  349. acpi_ns_repair_HID(struct acpi_predefined_data *data,
  350. union acpi_operand_object **return_object_ptr)
  351. {
  352. union acpi_operand_object *return_object = *return_object_ptr;
  353. union acpi_operand_object *new_string;
  354. char *source;
  355. char *dest;
  356. ACPI_FUNCTION_NAME(ns_repair_HID);
  357. /* We only care about string _HID objects (not integers) */
  358. if (return_object->common.type != ACPI_TYPE_STRING) {
  359. return (AE_OK);
  360. }
  361. if (return_object->string.length == 0) {
  362. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  363. "Invalid zero-length _HID or _CID string"));
  364. /* Return AE_OK anyway, let driver handle it */
  365. data->flags |= ACPI_OBJECT_REPAIRED;
  366. return (AE_OK);
  367. }
  368. /* It is simplest to always create a new string object */
  369. new_string = acpi_ut_create_string_object(return_object->string.length);
  370. if (!new_string) {
  371. return (AE_NO_MEMORY);
  372. }
  373. /*
  374. * Remove a leading asterisk if present. For some unknown reason, there
  375. * are many machines in the field that contains IDs like this.
  376. *
  377. * Examples: "*PNP0C03", "*ACPI0003"
  378. */
  379. source = return_object->string.pointer;
  380. if (*source == '*') {
  381. source++;
  382. new_string->string.length--;
  383. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  384. "%s: Removed invalid leading asterisk\n",
  385. data->pathname));
  386. }
  387. /*
  388. * Copy and uppercase the string. From the ACPI specification:
  389. *
  390. * A valid PNP ID must be of the form "AAA####" where A is an uppercase
  391. * letter and # is a hex digit. A valid ACPI ID must be of the form
  392. * "ACPI####" where # is a hex digit.
  393. */
  394. for (dest = new_string->string.pointer; *source; dest++, source++) {
  395. *dest = (char)ACPI_TOUPPER(*source);
  396. }
  397. acpi_ut_remove_reference(return_object);
  398. *return_object_ptr = new_string;
  399. return (AE_OK);
  400. }
  401. /******************************************************************************
  402. *
  403. * FUNCTION: acpi_ns_repair_TSS
  404. *
  405. * PARAMETERS: Data - Pointer to validation data structure
  406. * return_object_ptr - Pointer to the object returned from the
  407. * evaluation of a method or object
  408. *
  409. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  410. *
  411. * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
  412. * descending by the power dissipation values.
  413. *
  414. *****************************************************************************/
  415. static acpi_status
  416. acpi_ns_repair_TSS(struct acpi_predefined_data *data,
  417. union acpi_operand_object **return_object_ptr)
  418. {
  419. union acpi_operand_object *return_object = *return_object_ptr;
  420. acpi_status status;
  421. status = acpi_ns_check_sorted_list(data, return_object, 5, 1,
  422. ACPI_SORT_DESCENDING,
  423. "PowerDissipation");
  424. return (status);
  425. }
  426. /******************************************************************************
  427. *
  428. * FUNCTION: acpi_ns_repair_PSS
  429. *
  430. * PARAMETERS: Data - Pointer to validation data structure
  431. * return_object_ptr - Pointer to the object returned from the
  432. * evaluation of a method or object
  433. *
  434. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  435. *
  436. * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
  437. * by the CPU frequencies. Check that the power dissipation values
  438. * are all proportional to CPU frequency (i.e., sorting by
  439. * frequency should be the same as sorting by power.)
  440. *
  441. *****************************************************************************/
  442. static acpi_status
  443. acpi_ns_repair_PSS(struct acpi_predefined_data *data,
  444. union acpi_operand_object **return_object_ptr)
  445. {
  446. union acpi_operand_object *return_object = *return_object_ptr;
  447. union acpi_operand_object **outer_elements;
  448. u32 outer_element_count;
  449. union acpi_operand_object **elements;
  450. union acpi_operand_object *obj_desc;
  451. u32 previous_value;
  452. acpi_status status;
  453. u32 i;
  454. /*
  455. * Entries (sub-packages) in the _PSS Package must be sorted by power
  456. * dissipation, in descending order. If it appears that the list is
  457. * incorrectly sorted, sort it. We sort by cpu_frequency, since this
  458. * should be proportional to the power.
  459. */
  460. status = acpi_ns_check_sorted_list(data, return_object, 6, 0,
  461. ACPI_SORT_DESCENDING,
  462. "CpuFrequency");
  463. if (ACPI_FAILURE(status)) {
  464. return (status);
  465. }
  466. /*
  467. * We now know the list is correctly sorted by CPU frequency. Check if
  468. * the power dissipation values are proportional.
  469. */
  470. previous_value = ACPI_UINT32_MAX;
  471. outer_elements = return_object->package.elements;
  472. outer_element_count = return_object->package.count;
  473. for (i = 0; i < outer_element_count; i++) {
  474. elements = (*outer_elements)->package.elements;
  475. obj_desc = elements[1]; /* Index1 = power_dissipation */
  476. if ((u32) obj_desc->integer.value > previous_value) {
  477. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
  478. data->node_flags,
  479. "SubPackage[%u,%u] - suspicious power dissipation values",
  480. i - 1, i));
  481. }
  482. previous_value = (u32) obj_desc->integer.value;
  483. outer_elements++;
  484. }
  485. return (AE_OK);
  486. }
  487. /******************************************************************************
  488. *
  489. * FUNCTION: acpi_ns_check_sorted_list
  490. *
  491. * PARAMETERS: Data - Pointer to validation data structure
  492. * return_object - Pointer to the top-level returned object
  493. * expected_count - Minimum length of each sub-package
  494. * sort_index - Sub-package entry to sort on
  495. * sort_direction - Ascending or descending
  496. * sort_key_name - Name of the sort_index field
  497. *
  498. * RETURN: Status. AE_OK if the list is valid and is sorted correctly or
  499. * has been repaired by sorting the list.
  500. *
  501. * DESCRIPTION: Check if the package list is valid and sorted correctly by the
  502. * sort_index. If not, then sort the list.
  503. *
  504. *****************************************************************************/
  505. static acpi_status
  506. acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
  507. union acpi_operand_object *return_object,
  508. u32 expected_count,
  509. u32 sort_index,
  510. u8 sort_direction, char *sort_key_name)
  511. {
  512. u32 outer_element_count;
  513. union acpi_operand_object **outer_elements;
  514. union acpi_operand_object **elements;
  515. union acpi_operand_object *obj_desc;
  516. u32 i;
  517. u32 previous_value;
  518. ACPI_FUNCTION_NAME(ns_check_sorted_list);
  519. /* The top-level object must be a package */
  520. if (return_object->common.type != ACPI_TYPE_PACKAGE) {
  521. return (AE_AML_OPERAND_TYPE);
  522. }
  523. /*
  524. * NOTE: assumes list of sub-packages contains no NULL elements.
  525. * Any NULL elements should have been removed by earlier call
  526. * to acpi_ns_remove_null_elements.
  527. */
  528. outer_elements = return_object->package.elements;
  529. outer_element_count = return_object->package.count;
  530. if (!outer_element_count) {
  531. return (AE_AML_PACKAGE_LIMIT);
  532. }
  533. previous_value = 0;
  534. if (sort_direction == ACPI_SORT_DESCENDING) {
  535. previous_value = ACPI_UINT32_MAX;
  536. }
  537. /* Examine each subpackage */
  538. for (i = 0; i < outer_element_count; i++) {
  539. /* Each element of the top-level package must also be a package */
  540. if ((*outer_elements)->common.type != ACPI_TYPE_PACKAGE) {
  541. return (AE_AML_OPERAND_TYPE);
  542. }
  543. /* Each sub-package must have the minimum length */
  544. if ((*outer_elements)->package.count < expected_count) {
  545. return (AE_AML_PACKAGE_LIMIT);
  546. }
  547. elements = (*outer_elements)->package.elements;
  548. obj_desc = elements[sort_index];
  549. if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
  550. return (AE_AML_OPERAND_TYPE);
  551. }
  552. /*
  553. * The list must be sorted in the specified order. If we detect a
  554. * discrepancy, sort the entire list.
  555. */
  556. if (((sort_direction == ACPI_SORT_ASCENDING) &&
  557. (obj_desc->integer.value < previous_value)) ||
  558. ((sort_direction == ACPI_SORT_DESCENDING) &&
  559. (obj_desc->integer.value > previous_value))) {
  560. acpi_ns_sort_list(return_object->package.elements,
  561. outer_element_count, sort_index,
  562. sort_direction);
  563. data->flags |= ACPI_OBJECT_REPAIRED;
  564. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  565. "%s: Repaired unsorted list - now sorted by %s\n",
  566. data->pathname, sort_key_name));
  567. return (AE_OK);
  568. }
  569. previous_value = (u32) obj_desc->integer.value;
  570. outer_elements++;
  571. }
  572. return (AE_OK);
  573. }
  574. /******************************************************************************
  575. *
  576. * FUNCTION: acpi_ns_sort_list
  577. *
  578. * PARAMETERS: Elements - Package object element list
  579. * Count - Element count for above
  580. * Index - Sort by which package element
  581. * sort_direction - Ascending or Descending sort
  582. *
  583. * RETURN: None
  584. *
  585. * DESCRIPTION: Sort the objects that are in a package element list.
  586. *
  587. * NOTE: Assumes that all NULL elements have been removed from the package,
  588. * and that all elements have been verified to be of type Integer.
  589. *
  590. *****************************************************************************/
  591. static void
  592. acpi_ns_sort_list(union acpi_operand_object **elements,
  593. u32 count, u32 index, u8 sort_direction)
  594. {
  595. union acpi_operand_object *obj_desc1;
  596. union acpi_operand_object *obj_desc2;
  597. union acpi_operand_object *temp_obj;
  598. u32 i;
  599. u32 j;
  600. /* Simple bubble sort */
  601. for (i = 1; i < count; i++) {
  602. for (j = (count - 1); j >= i; j--) {
  603. obj_desc1 = elements[j - 1]->package.elements[index];
  604. obj_desc2 = elements[j]->package.elements[index];
  605. if (((sort_direction == ACPI_SORT_ASCENDING) &&
  606. (obj_desc1->integer.value >
  607. obj_desc2->integer.value))
  608. || ((sort_direction == ACPI_SORT_DESCENDING)
  609. && (obj_desc1->integer.value <
  610. obj_desc2->integer.value))) {
  611. temp_obj = elements[j - 1];
  612. elements[j - 1] = elements[j];
  613. elements[j] = temp_obj;
  614. }
  615. }
  616. }
  617. }