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/err.h>
20 #include <linux/gfp.h>
21 #include <linux/mutex.h>
22 #include <linux/slab.h>
24 #include "../../firmware/dcdbas.h"
25 #include "dell-smbios.h"
27 struct calling_interface_structure {
28 struct dmi_header header;
32 struct calling_interface_token tokens[];
35 static struct calling_interface_buffer *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 static struct calling_interface_token *da_tokens;
43 int dell_smbios_error(int value)
46 case 0: /* Completed successfully */
48 case -1: /* Completed with error */
50 case -2: /* Function not supported */
52 default: /* Unknown error */
56 EXPORT_SYMBOL_GPL(dell_smbios_error);
58 struct calling_interface_buffer *dell_smbios_get_buffer(void)
60 mutex_lock(&buffer_mutex);
61 dell_smbios_clear_buffer();
64 EXPORT_SYMBOL_GPL(dell_smbios_get_buffer);
66 void dell_smbios_clear_buffer(void)
68 memset(buffer, 0, sizeof(struct calling_interface_buffer));
70 EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer);
72 void dell_smbios_release_buffer(void)
74 mutex_unlock(&buffer_mutex);
76 EXPORT_SYMBOL_GPL(dell_smbios_release_buffer);
78 void dell_smbios_send_request(int class, int select)
80 struct smi_cmd command;
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;
88 buffer->class = class;
89 buffer->select = select;
91 dcdbas_smi_request(&command);
93 EXPORT_SYMBOL_GPL(dell_smbios_send_request);
95 struct calling_interface_token *dell_smbios_find_token(int tokenid)
99 for (i = 0; i < da_num_tokens; i++) {
100 if (da_tokens[i].tokenID == tokenid)
101 return &da_tokens[i];
106 EXPORT_SYMBOL_GPL(dell_smbios_find_token);
108 static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head);
110 int dell_laptop_register_notifier(struct notifier_block *nb)
112 return blocking_notifier_chain_register(&dell_laptop_chain_head, nb);
114 EXPORT_SYMBOL_GPL(dell_laptop_register_notifier);
116 int dell_laptop_unregister_notifier(struct notifier_block *nb)
118 return blocking_notifier_chain_unregister(&dell_laptop_chain_head, nb);
120 EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier);
122 void dell_laptop_call_notifier(unsigned long action, void *data)
124 blocking_notifier_call_chain(&dell_laptop_chain_head, action, data);
126 EXPORT_SYMBOL_GPL(dell_laptop_call_notifier);
128 static void __init parse_da_table(const struct dmi_header *dm)
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);
136 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
142 da_command_address = table->cmdIOAddress;
143 da_command_code = table->cmdIOCode;
145 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
146 sizeof(struct calling_interface_token),
151 da_tokens = new_da_tokens;
153 memcpy(da_tokens+da_num_tokens, table->tokens,
154 sizeof(struct calling_interface_token) * tokens);
156 da_num_tokens += tokens;
159 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
162 case 0xd4: /* Indexed IO */
163 case 0xd5: /* Protected Area Type 1 */
164 case 0xd6: /* Protected Area Type 2 */
166 case 0xda: /* Calling interface */
172 static int __init dell_smbios_init(void)
176 dmi_walk(find_tokens, NULL);
179 pr_info("Unable to find dmi tokens\n");
184 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
185 * is passed to SMI handler.
187 buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
200 static void __exit dell_smbios_exit(void)
203 free_page((unsigned long)buffer);
206 subsys_initcall(dell_smbios_init);
207 module_exit(dell_smbios_exit);
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");