apple.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * apple.c - Apple ACPI quirks
  3. * Copyright (C) 2017 Lukas Wunner <lukas@wunner.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License (version 2) as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/bitmap.h>
  11. #include <linux/platform_data/x86/apple.h>
  12. #include <linux/uuid.h>
  13. /* Apple _DSM device properties GUID */
  14. static const guid_t apple_prp_guid =
  15. GUID_INIT(0xa0b5b7c6, 0x1318, 0x441c,
  16. 0xb0, 0xc9, 0xfe, 0x69, 0x5e, 0xaf, 0x94, 0x9b);
  17. /**
  18. * acpi_extract_apple_properties - retrieve and convert Apple _DSM properties
  19. * @adev: ACPI device for which to retrieve the properties
  20. *
  21. * Invoke Apple's custom _DSM once to check the protocol version and once more
  22. * to retrieve the properties. They are marshalled up in a single package as
  23. * alternating key/value elements, unlike _DSD which stores them as a package
  24. * of 2-element packages. Convert to _DSD format and make them available under
  25. * the primary fwnode.
  26. */
  27. void acpi_extract_apple_properties(struct acpi_device *adev)
  28. {
  29. unsigned int i, j = 0, newsize = 0, numprops, numvalid;
  30. union acpi_object *props, *newprops;
  31. unsigned long *valid = NULL;
  32. void *free_space;
  33. if (!x86_apple_machine)
  34. return;
  35. props = acpi_evaluate_dsm_typed(adev->handle, &apple_prp_guid, 1, 0,
  36. NULL, ACPI_TYPE_BUFFER);
  37. if (!props)
  38. return;
  39. if (!props->buffer.length)
  40. goto out_free;
  41. if (props->buffer.pointer[0] != 3) {
  42. acpi_handle_info(adev->handle, FW_INFO
  43. "unsupported properties version %*ph\n",
  44. props->buffer.length, props->buffer.pointer);
  45. goto out_free;
  46. }
  47. ACPI_FREE(props);
  48. props = acpi_evaluate_dsm_typed(adev->handle, &apple_prp_guid, 1, 1,
  49. NULL, ACPI_TYPE_PACKAGE);
  50. if (!props)
  51. return;
  52. numprops = props->package.count / 2;
  53. if (!numprops)
  54. goto out_free;
  55. valid = bitmap_zalloc(numprops, GFP_KERNEL);
  56. if (!valid)
  57. goto out_free;
  58. /* newsize = key length + value length of each tuple */
  59. for (i = 0; i < numprops; i++) {
  60. union acpi_object *key = &props->package.elements[i * 2];
  61. union acpi_object *val = &props->package.elements[i * 2 + 1];
  62. if ( key->type != ACPI_TYPE_STRING ||
  63. (val->type != ACPI_TYPE_INTEGER &&
  64. val->type != ACPI_TYPE_BUFFER))
  65. continue; /* skip invalid properties */
  66. __set_bit(i, valid);
  67. newsize += key->string.length + 1;
  68. if ( val->type == ACPI_TYPE_BUFFER)
  69. newsize += val->buffer.length;
  70. }
  71. numvalid = bitmap_weight(valid, numprops);
  72. if (numprops > numvalid)
  73. acpi_handle_info(adev->handle, FW_INFO
  74. "skipped %u properties: wrong type\n",
  75. numprops - numvalid);
  76. if (numvalid == 0)
  77. goto out_free;
  78. /* newsize += top-level package + 3 objects for each key/value tuple */
  79. newsize += (1 + 3 * numvalid) * sizeof(union acpi_object);
  80. newprops = ACPI_ALLOCATE_ZEROED(newsize);
  81. if (!newprops)
  82. goto out_free;
  83. /* layout: top-level package | packages | key/value tuples | strings */
  84. newprops->type = ACPI_TYPE_PACKAGE;
  85. newprops->package.count = numvalid;
  86. newprops->package.elements = &newprops[1];
  87. free_space = &newprops[1 + 3 * numvalid];
  88. for_each_set_bit(i, valid, numprops) {
  89. union acpi_object *key = &props->package.elements[i * 2];
  90. union acpi_object *val = &props->package.elements[i * 2 + 1];
  91. unsigned int k = 1 + numvalid + j * 2; /* index into newprops */
  92. unsigned int v = k + 1;
  93. newprops[1 + j].type = ACPI_TYPE_PACKAGE;
  94. newprops[1 + j].package.count = 2;
  95. newprops[1 + j].package.elements = &newprops[k];
  96. newprops[k].type = ACPI_TYPE_STRING;
  97. newprops[k].string.length = key->string.length;
  98. newprops[k].string.pointer = free_space;
  99. memcpy(free_space, key->string.pointer, key->string.length);
  100. free_space += key->string.length + 1;
  101. newprops[v].type = val->type;
  102. if (val->type == ACPI_TYPE_INTEGER) {
  103. newprops[v].integer.value = val->integer.value;
  104. } else {
  105. newprops[v].buffer.length = val->buffer.length;
  106. newprops[v].buffer.pointer = free_space;
  107. memcpy(free_space, val->buffer.pointer,
  108. val->buffer.length);
  109. free_space += val->buffer.length;
  110. }
  111. j++; /* count valid properties */
  112. }
  113. WARN_ON(free_space != (void *)newprops + newsize);
  114. adev->data.pointer = newprops;
  115. acpi_data_add_props(&adev->data, &apple_prp_guid, newprops);
  116. out_free:
  117. ACPI_FREE(props);
  118. bitmap_free(valid);
  119. }