nsrepair2.c 24 KB

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