Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 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 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/dmi.h> |
Michał Kępień | e8edf53 | 2016-03-04 14:09:06 +0100 | [diff] [blame] | 19 | #include <linux/err.h> |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 20 | #include <linux/gfp.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/io.h> |
| 24 | #include "../../firmware/dcdbas.h" |
| 25 | #include "dell-smbios.h" |
| 26 | |
| 27 | struct calling_interface_structure { |
| 28 | struct dmi_header header; |
| 29 | u16 cmdIOAddress; |
| 30 | u8 cmdIOCode; |
| 31 | u32 supportedCmds; |
| 32 | struct calling_interface_token tokens[]; |
| 33 | } __packed; |
| 34 | |
Michał Kępień | 92ebd0d | 2016-01-22 15:27:21 +0100 | [diff] [blame] | 35 | static struct calling_interface_buffer *buffer; |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 36 | static DEFINE_MUTEX(buffer_mutex); |
| 37 | |
| 38 | static int da_command_address; |
| 39 | static int da_command_code; |
| 40 | static int da_num_tokens; |
Michał Kępień | b7bca2d | 2016-01-22 15:27:26 +0100 | [diff] [blame] | 41 | static struct calling_interface_token *da_tokens; |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 42 | |
Michał Kępień | 0db2180 | 2016-03-04 14:09:07 +0100 | [diff] [blame] | 43 | int dell_smbios_error(int value) |
Michał Kępień | e8edf53 | 2016-03-04 14:09:06 +0100 | [diff] [blame] | 44 | { |
| 45 | switch (value) { |
| 46 | case 0: /* Completed successfully */ |
| 47 | return 0; |
| 48 | case -1: /* Completed with error */ |
| 49 | return -EIO; |
| 50 | case -2: /* Function not supported */ |
| 51 | return -ENXIO; |
| 52 | default: /* Unknown error */ |
| 53 | return -EINVAL; |
| 54 | } |
| 55 | } |
Michał Kępień | 0db2180 | 2016-03-04 14:09:07 +0100 | [diff] [blame] | 56 | EXPORT_SYMBOL_GPL(dell_smbios_error); |
Michał Kępień | e8edf53 | 2016-03-04 14:09:06 +0100 | [diff] [blame] | 57 | |
Michał Kępień | bc2104c | 2016-01-22 15:27:20 +0100 | [diff] [blame] | 58 | struct calling_interface_buffer *dell_smbios_get_buffer(void) |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 59 | { |
| 60 | mutex_lock(&buffer_mutex); |
Michał Kępień | b6aa7e1 | 2016-01-22 15:27:15 +0100 | [diff] [blame] | 61 | dell_smbios_clear_buffer(); |
Michał Kępień | bc2104c | 2016-01-22 15:27:20 +0100 | [diff] [blame] | 62 | return buffer; |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 63 | } |
Michał Kępień | ee83c47 | 2016-01-22 15:27:14 +0100 | [diff] [blame] | 64 | EXPORT_SYMBOL_GPL(dell_smbios_get_buffer); |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 65 | |
Michał Kępień | b6aa7e1 | 2016-01-22 15:27:15 +0100 | [diff] [blame] | 66 | void dell_smbios_clear_buffer(void) |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 67 | { |
| 68 | memset(buffer, 0, sizeof(struct calling_interface_buffer)); |
| 69 | } |
Michał Kępień | b6aa7e1 | 2016-01-22 15:27:15 +0100 | [diff] [blame] | 70 | EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer); |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 71 | |
Michał Kępień | cb161763 | 2016-01-22 15:27:16 +0100 | [diff] [blame] | 72 | void dell_smbios_release_buffer(void) |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 73 | { |
| 74 | mutex_unlock(&buffer_mutex); |
| 75 | } |
Michał Kępień | cb161763 | 2016-01-22 15:27:16 +0100 | [diff] [blame] | 76 | EXPORT_SYMBOL_GPL(dell_smbios_release_buffer); |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 77 | |
Michał Kępień | c42831c | 2016-01-22 15:27:19 +0100 | [diff] [blame] | 78 | void dell_smbios_send_request(int class, int select) |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 79 | { |
| 80 | struct smi_cmd command; |
| 81 | |
| 82 | command.magic = SMI_CMD_MAGIC; |
| 83 | command.command_address = da_command_address; |
| 84 | command.command_code = da_command_code; |
| 85 | command.ebx = virt_to_phys(buffer); |
| 86 | command.ecx = 0x42534931; |
| 87 | |
| 88 | buffer->class = class; |
| 89 | buffer->select = select; |
| 90 | |
| 91 | dcdbas_smi_request(&command); |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 92 | } |
Michał Kępień | 2f26213 | 2016-01-22 15:27:17 +0100 | [diff] [blame] | 93 | EXPORT_SYMBOL_GPL(dell_smbios_send_request); |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 94 | |
Michał Kępień | 96f7ef9 | 2016-01-22 15:27:22 +0100 | [diff] [blame] | 95 | struct calling_interface_token *dell_smbios_find_token(int tokenid) |
| 96 | { |
| 97 | int i; |
| 98 | |
| 99 | for (i = 0; i < da_num_tokens; i++) { |
| 100 | if (da_tokens[i].tokenID == tokenid) |
| 101 | return &da_tokens[i]; |
| 102 | } |
| 103 | |
| 104 | return NULL; |
| 105 | } |
| 106 | EXPORT_SYMBOL_GPL(dell_smbios_find_token); |
| 107 | |
Hans de Goede | 504b025 | 2017-03-16 11:55:32 +0100 | [diff] [blame^] | 108 | static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head); |
| 109 | |
| 110 | int dell_laptop_register_notifier(struct notifier_block *nb) |
| 111 | { |
| 112 | return blocking_notifier_chain_register(&dell_laptop_chain_head, nb); |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(dell_laptop_register_notifier); |
| 115 | |
| 116 | int dell_laptop_unregister_notifier(struct notifier_block *nb) |
| 117 | { |
| 118 | return blocking_notifier_chain_unregister(&dell_laptop_chain_head, nb); |
| 119 | } |
| 120 | EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier); |
| 121 | |
| 122 | void dell_laptop_call_notifier(unsigned long action, void *data) |
| 123 | { |
| 124 | blocking_notifier_call_chain(&dell_laptop_chain_head, action, data); |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(dell_laptop_call_notifier); |
| 127 | |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 128 | static void __init parse_da_table(const struct dmi_header *dm) |
| 129 | { |
| 130 | /* Final token is a terminator, so we don't want to copy it */ |
| 131 | int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1; |
| 132 | struct calling_interface_token *new_da_tokens; |
| 133 | struct calling_interface_structure *table = |
| 134 | container_of(dm, struct calling_interface_structure, header); |
| 135 | |
| 136 | /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least |
| 137 | 6 bytes of entry */ |
| 138 | |
| 139 | if (dm->length < 17) |
| 140 | return; |
| 141 | |
| 142 | da_command_address = table->cmdIOAddress; |
| 143 | da_command_code = table->cmdIOCode; |
| 144 | |
| 145 | new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) * |
| 146 | sizeof(struct calling_interface_token), |
| 147 | GFP_KERNEL); |
| 148 | |
| 149 | if (!new_da_tokens) |
| 150 | return; |
| 151 | da_tokens = new_da_tokens; |
| 152 | |
| 153 | memcpy(da_tokens+da_num_tokens, table->tokens, |
| 154 | sizeof(struct calling_interface_token) * tokens); |
| 155 | |
| 156 | da_num_tokens += tokens; |
| 157 | } |
| 158 | |
| 159 | static void __init find_tokens(const struct dmi_header *dm, void *dummy) |
| 160 | { |
| 161 | switch (dm->type) { |
| 162 | case 0xd4: /* Indexed IO */ |
| 163 | case 0xd5: /* Protected Area Type 1 */ |
| 164 | case 0xd6: /* Protected Area Type 2 */ |
| 165 | break; |
| 166 | case 0xda: /* Calling interface */ |
| 167 | parse_da_table(dm); |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static int __init dell_smbios_init(void) |
| 173 | { |
| 174 | int ret; |
| 175 | |
| 176 | dmi_walk(find_tokens, NULL); |
| 177 | |
| 178 | if (!da_tokens) { |
| 179 | pr_info("Unable to find dmi tokens\n"); |
| 180 | return -ENODEV; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Allocate buffer below 4GB for SMI data--only 32-bit physical addr |
| 185 | * is passed to SMI handler. |
| 186 | */ |
| 187 | buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32); |
| 188 | if (!buffer) { |
| 189 | ret = -ENOMEM; |
| 190 | goto fail_buffer; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | |
| 195 | fail_buffer: |
| 196 | kfree(da_tokens); |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | static void __exit dell_smbios_exit(void) |
| 201 | { |
| 202 | kfree(da_tokens); |
| 203 | free_page((unsigned long)buffer); |
| 204 | } |
| 205 | |
| 206 | subsys_initcall(dell_smbios_init); |
| 207 | module_exit(dell_smbios_exit); |
| 208 | |
| 209 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); |
| 210 | MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>"); |
| 211 | MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); |
| 212 | MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS"); |
| 213 | MODULE_LICENSE("GPL"); |