blob: 0a5723468bfffdd6f16df829e5131c90d7e38034 [file] [log] [blame]
Michał Kępień2f9f26b2016-01-22 15:27:13 +01001/*
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ńe8edf532016-03-04 14:09:06 +010019#include <linux/err.h>
Michał Kępień2f9f26b2016-01-22 15:27:13 +010020#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
27struct 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ń92ebd0d2016-01-22 15:27:21 +010035static struct calling_interface_buffer *buffer;
Michał Kępień2f9f26b2016-01-22 15:27:13 +010036static DEFINE_MUTEX(buffer_mutex);
37
38static int da_command_address;
39static int da_command_code;
40static int da_num_tokens;
Michał Kępieńb7bca2d2016-01-22 15:27:26 +010041static struct calling_interface_token *da_tokens;
Michał Kępień2f9f26b2016-01-22 15:27:13 +010042
Michał Kępień0db21802016-03-04 14:09:07 +010043int dell_smbios_error(int value)
Michał Kępieńe8edf532016-03-04 14:09:06 +010044{
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ń0db21802016-03-04 14:09:07 +010056EXPORT_SYMBOL_GPL(dell_smbios_error);
Michał Kępieńe8edf532016-03-04 14:09:06 +010057
Michał Kępieńbc2104c2016-01-22 15:27:20 +010058struct calling_interface_buffer *dell_smbios_get_buffer(void)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010059{
60 mutex_lock(&buffer_mutex);
Michał Kępieńb6aa7e12016-01-22 15:27:15 +010061 dell_smbios_clear_buffer();
Michał Kępieńbc2104c2016-01-22 15:27:20 +010062 return buffer;
Michał Kępień2f9f26b2016-01-22 15:27:13 +010063}
Michał Kępieńee83c472016-01-22 15:27:14 +010064EXPORT_SYMBOL_GPL(dell_smbios_get_buffer);
Michał Kępień2f9f26b2016-01-22 15:27:13 +010065
Michał Kępieńb6aa7e12016-01-22 15:27:15 +010066void dell_smbios_clear_buffer(void)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010067{
68 memset(buffer, 0, sizeof(struct calling_interface_buffer));
69}
Michał Kępieńb6aa7e12016-01-22 15:27:15 +010070EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer);
Michał Kępień2f9f26b2016-01-22 15:27:13 +010071
Michał Kępieńcb1617632016-01-22 15:27:16 +010072void dell_smbios_release_buffer(void)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010073{
74 mutex_unlock(&buffer_mutex);
75}
Michał Kępieńcb1617632016-01-22 15:27:16 +010076EXPORT_SYMBOL_GPL(dell_smbios_release_buffer);
Michał Kępień2f9f26b2016-01-22 15:27:13 +010077
Michał Kępieńc42831c2016-01-22 15:27:19 +010078void dell_smbios_send_request(int class, int select)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010079{
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ń2f9f26b2016-01-22 15:27:13 +010092}
Michał Kępień2f262132016-01-22 15:27:17 +010093EXPORT_SYMBOL_GPL(dell_smbios_send_request);
Michał Kępień2f9f26b2016-01-22 15:27:13 +010094
Michał Kępień96f7ef92016-01-22 15:27:22 +010095struct 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}
106EXPORT_SYMBOL_GPL(dell_smbios_find_token);
107
Hans de Goede504b0252017-03-16 11:55:32 +0100108static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head);
109
110int dell_laptop_register_notifier(struct notifier_block *nb)
111{
112 return blocking_notifier_chain_register(&dell_laptop_chain_head, nb);
113}
114EXPORT_SYMBOL_GPL(dell_laptop_register_notifier);
115
116int dell_laptop_unregister_notifier(struct notifier_block *nb)
117{
118 return blocking_notifier_chain_unregister(&dell_laptop_chain_head, nb);
119}
120EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier);
121
122void dell_laptop_call_notifier(unsigned long action, void *data)
123{
124 blocking_notifier_call_chain(&dell_laptop_chain_head, action, data);
125}
126EXPORT_SYMBOL_GPL(dell_laptop_call_notifier);
127
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100128static 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
159static 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
172static 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
195fail_buffer:
196 kfree(da_tokens);
197 return ret;
198}
199
200static void __exit dell_smbios_exit(void)
201{
202 kfree(da_tokens);
203 free_page((unsigned long)buffer);
204}
205
206subsys_initcall(dell_smbios_init);
207module_exit(dell_smbios_exit);
208
209MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
210MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
211MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
212MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
213MODULE_LICENSE("GPL");