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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
25 #include "../../firmware/dcdbas.h"
26 #include "dell-smbios.h"
28 struct calling_interface_structure
{
29 struct dmi_header header
;
33 struct calling_interface_token tokens
[];
36 static struct calling_interface_buffer
*buffer
;
37 static DEFINE_MUTEX(buffer_mutex
);
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
;
44 int dell_smbios_error(int value
)
47 case 0: /* Completed successfully */
49 case -1: /* Completed with error */
51 case -2: /* Function not supported */
53 default: /* Unknown error */
57 EXPORT_SYMBOL_GPL(dell_smbios_error
);
59 struct calling_interface_buffer
*dell_smbios_get_buffer(void)
61 mutex_lock(&buffer_mutex
);
62 dell_smbios_clear_buffer();
65 EXPORT_SYMBOL_GPL(dell_smbios_get_buffer
);
67 void dell_smbios_clear_buffer(void)
69 memset(buffer
, 0, sizeof(struct calling_interface_buffer
));
71 EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer
);
73 void dell_smbios_release_buffer(void)
75 mutex_unlock(&buffer_mutex
);
77 EXPORT_SYMBOL_GPL(dell_smbios_release_buffer
);
79 void dell_smbios_send_request(int class, int select
)
81 struct smi_cmd command
;
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;
89 buffer
->cmd_class
= class;
90 buffer
->cmd_select
= select
;
92 dcdbas_smi_request(&command
);
94 EXPORT_SYMBOL_GPL(dell_smbios_send_request
);
96 struct calling_interface_token
*dell_smbios_find_token(int tokenid
)
100 for (i
= 0; i
< da_num_tokens
; i
++) {
101 if (da_tokens
[i
].tokenID
== tokenid
)
102 return &da_tokens
[i
];
107 EXPORT_SYMBOL_GPL(dell_smbios_find_token
);
109 static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head
);
111 int dell_laptop_register_notifier(struct notifier_block
*nb
)
113 return blocking_notifier_chain_register(&dell_laptop_chain_head
, nb
);
115 EXPORT_SYMBOL_GPL(dell_laptop_register_notifier
);
117 int dell_laptop_unregister_notifier(struct notifier_block
*nb
)
119 return blocking_notifier_chain_unregister(&dell_laptop_chain_head
, nb
);
121 EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier
);
123 void dell_laptop_call_notifier(unsigned long action
, void *data
)
125 blocking_notifier_call_chain(&dell_laptop_chain_head
, action
, data
);
127 EXPORT_SYMBOL_GPL(dell_laptop_call_notifier
);
129 static void __init
parse_da_table(const struct dmi_header
*dm
)
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
);
137 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
143 da_command_address
= table
->cmdIOAddress
;
144 da_command_code
= table
->cmdIOCode
;
146 new_da_tokens
= krealloc(da_tokens
, (da_num_tokens
+ tokens
) *
147 sizeof(struct calling_interface_token
),
152 da_tokens
= new_da_tokens
;
154 memcpy(da_tokens
+da_num_tokens
, table
->tokens
,
155 sizeof(struct calling_interface_token
) * tokens
);
157 da_num_tokens
+= tokens
;
160 static void __init
find_tokens(const struct dmi_header
*dm
, void *dummy
)
163 case 0xd4: /* Indexed IO */
164 case 0xd5: /* Protected Area Type 1 */
165 case 0xd6: /* Protected Area Type 2 */
167 case 0xda: /* Calling interface */
173 static int __init
dell_smbios_init(void)
177 dmi_walk(find_tokens
, NULL
);
180 pr_info("Unable to find dmi tokens\n");
185 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
186 * is passed to SMI handler.
188 buffer
= (void *)__get_free_page(GFP_KERNEL
| GFP_DMA32
);
201 static void __exit
dell_smbios_exit(void)
204 free_page((unsigned long)buffer
);
207 subsys_initcall(dell_smbios_init
);
208 module_exit(dell_smbios_exit
);
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");