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