2 * Common functions for kernel modules using Dell SMBIOS
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>
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
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.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/dmi.h>
19 #include <linux/gfp.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
23 #include "../../firmware/dcdbas.h"
24 #include "dell-smbios.h"
26 struct calling_interface_structure {
27 struct dmi_header header;
31 struct calling_interface_token tokens[];
34 struct calling_interface_buffer *buffer;
35 EXPORT_SYMBOL_GPL(buffer);
36 static DEFINE_MUTEX(buffer_mutex);
38 static int da_command_address;
39 static int da_command_code;
40 static int da_num_tokens;
41 struct calling_interface_token *da_tokens;
42 EXPORT_SYMBOL_GPL(da_tokens);
44 void dell_smbios_get_buffer(void)
46 mutex_lock(&buffer_mutex);
47 dell_smbios_clear_buffer();
49 EXPORT_SYMBOL_GPL(dell_smbios_get_buffer);
51 void dell_smbios_clear_buffer(void)
53 memset(buffer, 0, sizeof(struct calling_interface_buffer));
55 EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer);
57 void dell_smbios_release_buffer(void)
59 mutex_unlock(&buffer_mutex);
61 EXPORT_SYMBOL_GPL(dell_smbios_release_buffer);
63 struct calling_interface_buffer *
64 dell_send_request(struct calling_interface_buffer *buffer,
65 int class, int select)
67 struct smi_cmd command;
69 command.magic = SMI_CMD_MAGIC;
70 command.command_address = da_command_address;
71 command.command_code = da_command_code;
72 command.ebx = virt_to_phys(buffer);
73 command.ecx = 0x42534931;
75 buffer->class = class;
76 buffer->select = select;
78 dcdbas_smi_request(&command);
82 EXPORT_SYMBOL_GPL(dell_send_request);
84 int find_token_id(int tokenid)
88 for (i = 0; i < da_num_tokens; i++) {
89 if (da_tokens[i].tokenID == tokenid)
95 EXPORT_SYMBOL_GPL(find_token_id);
97 int find_token_location(int tokenid)
101 id = find_token_id(tokenid);
105 return da_tokens[id].location;
107 EXPORT_SYMBOL_GPL(find_token_location);
109 static void __init parse_da_table(const struct dmi_header *dm)
111 /* Final token is a terminator, so we don't want to copy it */
112 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
113 struct calling_interface_token *new_da_tokens;
114 struct calling_interface_structure *table =
115 container_of(dm, struct calling_interface_structure, header);
117 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
123 da_command_address = table->cmdIOAddress;
124 da_command_code = table->cmdIOCode;
126 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
127 sizeof(struct calling_interface_token),
132 da_tokens = new_da_tokens;
134 memcpy(da_tokens+da_num_tokens, table->tokens,
135 sizeof(struct calling_interface_token) * tokens);
137 da_num_tokens += tokens;
140 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
143 case 0xd4: /* Indexed IO */
144 case 0xd5: /* Protected Area Type 1 */
145 case 0xd6: /* Protected Area Type 2 */
147 case 0xda: /* Calling interface */
153 static int __init dell_smbios_init(void)
157 dmi_walk(find_tokens, NULL);
160 pr_info("Unable to find dmi tokens\n");
165 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
166 * is passed to SMI handler.
168 buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
181 static void __exit dell_smbios_exit(void)
184 free_page((unsigned long)buffer);
187 subsys_initcall(dell_smbios_init);
188 module_exit(dell_smbios_exit);
190 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
191 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
192 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
193 MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
194 MODULE_LICENSE("GPL");