dell-smbios.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Common functions for kernel modules using Dell SMBIOS
  3. *
  4. * Copyright (c) Red Hat <mjg@redhat.com>
  5. * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
  6. * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
  7. *
  8. * Based on documentation in the libsmbios package:
  9. * Copyright (C) 2005-2014 Dell Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/dmi.h>
  18. #include <linux/err.h>
  19. #include <linux/gfp.h>
  20. #include <linux/mutex.h>
  21. #include <linux/slab.h>
  22. #include <linux/io.h>
  23. #include "../../firmware/dcdbas.h"
  24. #include "dell-smbios.h"
  25. struct calling_interface_structure {
  26. struct dmi_header header;
  27. u16 cmdIOAddress;
  28. u8 cmdIOCode;
  29. u32 supportedCmds;
  30. struct calling_interface_token tokens[];
  31. } __packed;
  32. static struct calling_interface_buffer *buffer;
  33. static DEFINE_MUTEX(buffer_mutex);
  34. static int da_command_address;
  35. static int da_command_code;
  36. static int da_num_tokens;
  37. static struct calling_interface_token *da_tokens;
  38. int dell_smbios_error(int value)
  39. {
  40. switch (value) {
  41. case 0: /* Completed successfully */
  42. return 0;
  43. case -1: /* Completed with error */
  44. return -EIO;
  45. case -2: /* Function not supported */
  46. return -ENXIO;
  47. default: /* Unknown error */
  48. return -EINVAL;
  49. }
  50. }
  51. EXPORT_SYMBOL_GPL(dell_smbios_error);
  52. struct calling_interface_buffer *dell_smbios_get_buffer(void)
  53. {
  54. mutex_lock(&buffer_mutex);
  55. dell_smbios_clear_buffer();
  56. return buffer;
  57. }
  58. EXPORT_SYMBOL_GPL(dell_smbios_get_buffer);
  59. void dell_smbios_clear_buffer(void)
  60. {
  61. memset(buffer, 0, sizeof(struct calling_interface_buffer));
  62. }
  63. EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer);
  64. void dell_smbios_release_buffer(void)
  65. {
  66. mutex_unlock(&buffer_mutex);
  67. }
  68. EXPORT_SYMBOL_GPL(dell_smbios_release_buffer);
  69. void dell_smbios_send_request(int class, int select)
  70. {
  71. struct smi_cmd command;
  72. command.magic = SMI_CMD_MAGIC;
  73. command.command_address = da_command_address;
  74. command.command_code = da_command_code;
  75. command.ebx = virt_to_phys(buffer);
  76. command.ecx = 0x42534931;
  77. buffer->class = class;
  78. buffer->select = select;
  79. dcdbas_smi_request(&command);
  80. }
  81. EXPORT_SYMBOL_GPL(dell_smbios_send_request);
  82. struct calling_interface_token *dell_smbios_find_token(int tokenid)
  83. {
  84. int i;
  85. for (i = 0; i < da_num_tokens; i++) {
  86. if (da_tokens[i].tokenID == tokenid)
  87. return &da_tokens[i];
  88. }
  89. return NULL;
  90. }
  91. EXPORT_SYMBOL_GPL(dell_smbios_find_token);
  92. static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head);
  93. int dell_laptop_register_notifier(struct notifier_block *nb)
  94. {
  95. return blocking_notifier_chain_register(&dell_laptop_chain_head, nb);
  96. }
  97. EXPORT_SYMBOL_GPL(dell_laptop_register_notifier);
  98. int dell_laptop_unregister_notifier(struct notifier_block *nb)
  99. {
  100. return blocking_notifier_chain_unregister(&dell_laptop_chain_head, nb);
  101. }
  102. EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier);
  103. void dell_laptop_call_notifier(unsigned long action, void *data)
  104. {
  105. blocking_notifier_call_chain(&dell_laptop_chain_head, action, data);
  106. }
  107. EXPORT_SYMBOL_GPL(dell_laptop_call_notifier);
  108. static void __init parse_da_table(const struct dmi_header *dm)
  109. {
  110. /* Final token is a terminator, so we don't want to copy it */
  111. int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
  112. struct calling_interface_token *new_da_tokens;
  113. struct calling_interface_structure *table =
  114. container_of(dm, struct calling_interface_structure, header);
  115. /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
  116. 6 bytes of entry */
  117. if (dm->length < 17)
  118. return;
  119. da_command_address = table->cmdIOAddress;
  120. da_command_code = table->cmdIOCode;
  121. new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
  122. sizeof(struct calling_interface_token),
  123. GFP_KERNEL);
  124. if (!new_da_tokens)
  125. return;
  126. da_tokens = new_da_tokens;
  127. memcpy(da_tokens+da_num_tokens, table->tokens,
  128. sizeof(struct calling_interface_token) * tokens);
  129. da_num_tokens += tokens;
  130. }
  131. static void __init find_tokens(const struct dmi_header *dm, void *dummy)
  132. {
  133. switch (dm->type) {
  134. case 0xd4: /* Indexed IO */
  135. case 0xd5: /* Protected Area Type 1 */
  136. case 0xd6: /* Protected Area Type 2 */
  137. break;
  138. case 0xda: /* Calling interface */
  139. parse_da_table(dm);
  140. break;
  141. }
  142. }
  143. static int __init dell_smbios_init(void)
  144. {
  145. int ret;
  146. dmi_walk(find_tokens, NULL);
  147. if (!da_tokens) {
  148. pr_info("Unable to find dmi tokens\n");
  149. return -ENODEV;
  150. }
  151. /*
  152. * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
  153. * is passed to SMI handler.
  154. */
  155. buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
  156. if (!buffer) {
  157. ret = -ENOMEM;
  158. goto fail_buffer;
  159. }
  160. return 0;
  161. fail_buffer:
  162. kfree(da_tokens);
  163. return ret;
  164. }
  165. static void __exit dell_smbios_exit(void)
  166. {
  167. kfree(da_tokens);
  168. free_page((unsigned long)buffer);
  169. }
  170. subsys_initcall(dell_smbios_init);
  171. module_exit(dell_smbios_exit);
  172. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  173. MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
  174. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  175. MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
  176. MODULE_LICENSE("GPL");