dell-smbios.c 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  Common functions for kernel modules using Dell SMBIOS
 *
 *  Copyright (c) Red Hat <mjg@redhat.com>
 *  Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
 *  Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
 *
 *  Based on documentation in the libsmbios package:
 *  Copyright (C) 2005-2014 Dell Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 */
15
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 17 18

#include <linux/kernel.h>
#include <linux/module.h>
19
#include <linux/capability.h>
20
#include <linux/dmi.h>
21
#include <linux/err.h>
22
#include <linux/mutex.h>
23
#include <linux/platform_device.h>
24 25 26 27
#include <linux/slab.h>
#include "dell-smbios.h"

static int da_num_tokens;
28
static struct platform_device *platform_device;
29
static struct calling_interface_token *da_tokens;
30 31 32
static struct device_attribute *token_location_attrs;
static struct device_attribute *token_value_attrs;
static struct attribute **token_attrs;
33 34 35 36 37 38 39 40 41
static DEFINE_MUTEX(smbios_mutex);

struct smbios_device {
	struct list_head list;
	struct device *device;
	int (*call_fn)(struct calling_interface_buffer *);
};

static LIST_HEAD(smbios_device_list);
42

43
int dell_smbios_error(int value)
44 45 46 47 48 49 50 51 52 53 54 55
{
	switch (value) {
	case 0: /* Completed successfully */
		return 0;
	case -1: /* Completed with error */
		return -EIO;
	case -2: /* Function not supported */
		return -ENXIO;
	default: /* Unknown error */
		return -EINVAL;
	}
}
56
EXPORT_SYMBOL_GPL(dell_smbios_error);
57

58
int dell_smbios_register_device(struct device *d, void *call_fn)
59
{
60
	struct smbios_device *priv;
61

62 63 64 65 66 67 68 69 70 71 72
	priv = devm_kzalloc(d, sizeof(struct smbios_device), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
	get_device(d);
	priv->device = d;
	priv->call_fn = call_fn;
	mutex_lock(&smbios_mutex);
	list_add_tail(&priv->list, &smbios_device_list);
	mutex_unlock(&smbios_mutex);
	dev_dbg(d, "Added device: %s\n", d->driver->name);
	return 0;
73
}
74
EXPORT_SYMBOL_GPL(dell_smbios_register_device);
75

76
void dell_smbios_unregister_device(struct device *d)
77
{
78 79 80 81 82 83 84 85 86 87 88 89
	struct smbios_device *priv;

	mutex_lock(&smbios_mutex);
	list_for_each_entry(priv, &smbios_device_list, list) {
		if (priv->device == d) {
			list_del(&priv->list);
			put_device(d);
			break;
		}
	}
	mutex_unlock(&smbios_mutex);
	dev_dbg(d, "Remove device: %s\n", d->driver->name);
90
}
91
EXPORT_SYMBOL_GPL(dell_smbios_unregister_device);
92

93
int dell_smbios_call(struct calling_interface_buffer *buffer)
94
{
95 96 97 98 99 100 101 102 103 104 105 106 107 108
	int (*call_fn)(struct calling_interface_buffer *) = NULL;
	struct device *selected_dev = NULL;
	struct smbios_device *priv;
	int ret;

	mutex_lock(&smbios_mutex);
	list_for_each_entry(priv, &smbios_device_list, list) {
		if (!selected_dev || priv->device->id >= selected_dev->id) {
			dev_dbg(priv->device, "Trying device ID: %d\n",
				priv->device->id);
			call_fn = priv->call_fn;
			selected_dev = priv->device;
		}
	}
109

110 111 112 113 114
	if (!selected_dev) {
		ret = -ENODEV;
		pr_err("No dell-smbios drivers are loaded\n");
		goto out_smbios_call;
	}
115

116
	ret = call_fn(buffer);
117

118 119 120
out_smbios_call:
	mutex_unlock(&smbios_mutex);
	return ret;
121
}
122
EXPORT_SYMBOL_GPL(dell_smbios_call);
123

124 125 126 127 128 129 130 131 132 133 134 135 136
struct calling_interface_token *dell_smbios_find_token(int tokenid)
{
	int i;

	for (i = 0; i < da_num_tokens; i++) {
		if (da_tokens[i].tokenID == tokenid)
			return &da_tokens[i];
	}

	return NULL;
}
EXPORT_SYMBOL_GPL(dell_smbios_find_token);

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head);

int dell_laptop_register_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_register(&dell_laptop_chain_head, nb);
}
EXPORT_SYMBOL_GPL(dell_laptop_register_notifier);

int dell_laptop_unregister_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(&dell_laptop_chain_head, nb);
}
EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier);

void dell_laptop_call_notifier(unsigned long action, void *data)
{
	blocking_notifier_call_chain(&dell_laptop_chain_head, action, data);
}
EXPORT_SYMBOL_GPL(dell_laptop_call_notifier);

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
static void __init parse_da_table(const struct dmi_header *dm)
{
	/* Final token is a terminator, so we don't want to copy it */
	int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
	struct calling_interface_token *new_da_tokens;
	struct calling_interface_structure *table =
		container_of(dm, struct calling_interface_structure, header);

	/* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
	   6 bytes of entry */

	if (dm->length < 17)
		return;

	new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
				 sizeof(struct calling_interface_token),
				 GFP_KERNEL);

	if (!new_da_tokens)
		return;
	da_tokens = new_da_tokens;

	memcpy(da_tokens+da_num_tokens, table->tokens,
	       sizeof(struct calling_interface_token) * tokens);

	da_num_tokens += tokens;
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
static void zero_duplicates(struct device *dev)
{
	int i, j;

	for (i = 0; i < da_num_tokens; i++) {
		if (da_tokens[i].tokenID == 0)
			continue;
		for (j = i+1; j < da_num_tokens; j++) {
			if (da_tokens[j].tokenID == 0)
				continue;
			if (da_tokens[i].tokenID == da_tokens[j].tokenID) {
				dev_dbg(dev, "Zeroing dup token ID %x(%x/%x)\n",
					da_tokens[j].tokenID,
					da_tokens[j].location,
					da_tokens[j].value);
				da_tokens[j].tokenID = 0;
			}
		}
	}
}

206 207 208 209 210 211 212 213 214 215 216 217 218
static void __init find_tokens(const struct dmi_header *dm, void *dummy)
{
	switch (dm->type) {
	case 0xd4: /* Indexed IO */
	case 0xd5: /* Protected Area Type 1 */
	case 0xd6: /* Protected Area Type 2 */
		break;
	case 0xda: /* Calling interface */
		parse_da_table(dm);
		break;
	}
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
static int match_attribute(struct device *dev,
			   struct device_attribute *attr)
{
	int i;

	for (i = 0; i < da_num_tokens * 2; i++) {
		if (!token_attrs[i])
			continue;
		if (strcmp(token_attrs[i]->name, attr->attr.name) == 0)
			return i/2;
	}
	dev_dbg(dev, "couldn't match: %s\n", attr->attr.name);
	return -EINVAL;
}

static ssize_t location_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	int i;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	i = match_attribute(dev, attr);
	if (i > 0)
		return scnprintf(buf, PAGE_SIZE, "%08x", da_tokens[i].location);
	return 0;
}

static ssize_t value_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	int i;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	i = match_attribute(dev, attr);
	if (i > 0)
		return scnprintf(buf, PAGE_SIZE, "%08x", da_tokens[i].value);
	return 0;
}

static struct attribute_group smbios_attribute_group = {
	.name = "tokens"
};

static struct platform_driver platform_driver = {
	.driver = {
		.name = "dell-smbios",
	},
};

static int build_tokens_sysfs(struct platform_device *dev)
{
	char buffer_location[13];
	char buffer_value[10];
	char *location_name;
	char *value_name;
	size_t size;
	int ret;
	int i, j;

	/* (number of tokens  + 1 for null terminated */
	size = sizeof(struct device_attribute) * (da_num_tokens + 1);
	token_location_attrs = kzalloc(size, GFP_KERNEL);
	if (!token_location_attrs)
		return -ENOMEM;
	token_value_attrs = kzalloc(size, GFP_KERNEL);
	if (!token_value_attrs)
		goto out_allocate_value;

	/* need to store both location and value + terminator*/
	size = sizeof(struct attribute *) * ((2 * da_num_tokens) + 1);
	token_attrs = kzalloc(size, GFP_KERNEL);
	if (!token_attrs)
		goto out_allocate_attrs;

	for (i = 0, j = 0; i < da_num_tokens; i++) {
		/* skip empty */
		if (da_tokens[i].tokenID == 0)
			continue;
		/* add location */
		sprintf(buffer_location, "%04x_location",
			da_tokens[i].tokenID);
		location_name = kstrdup(buffer_location, GFP_KERNEL);
		if (location_name == NULL)
			goto out_unwind_strings;
		sysfs_attr_init(&token_location_attrs[i].attr);
		token_location_attrs[i].attr.name = location_name;
		token_location_attrs[i].attr.mode = 0444;
		token_location_attrs[i].show = location_show;
		token_attrs[j++] = &token_location_attrs[i].attr;

		/* add value */
		sprintf(buffer_value, "%04x_value",
			da_tokens[i].tokenID);
		value_name = kstrdup(buffer_value, GFP_KERNEL);
		if (value_name == NULL)
			goto loop_fail_create_value;
		sysfs_attr_init(&token_value_attrs[i].attr);
		token_value_attrs[i].attr.name = value_name;
		token_value_attrs[i].attr.mode = 0444;
		token_value_attrs[i].show = value_show;
		token_attrs[j++] = &token_value_attrs[i].attr;
		continue;

loop_fail_create_value:
		kfree(value_name);
		goto out_unwind_strings;
	}
	smbios_attribute_group.attrs = token_attrs;

	ret = sysfs_create_group(&dev->dev.kobj, &smbios_attribute_group);
	if (ret)
		goto out_unwind_strings;
	return 0;

out_unwind_strings:
	for (i = i-1; i > 0; i--) {
		kfree(token_location_attrs[i].attr.name);
		kfree(token_value_attrs[i].attr.name);
	}
	kfree(token_attrs);
out_allocate_attrs:
	kfree(token_value_attrs);
out_allocate_value:
	kfree(token_location_attrs);

	return -ENOMEM;
}

static void free_group(struct platform_device *pdev)
{
	int i;

	sysfs_remove_group(&pdev->dev.kobj,
				&smbios_attribute_group);
	for (i = 0; i < da_num_tokens; i++) {
		kfree(token_location_attrs[i].attr.name);
		kfree(token_value_attrs[i].attr.name);
	}
	kfree(token_attrs);
	kfree(token_value_attrs);
	kfree(token_location_attrs);
}

366 367
static int __init dell_smbios_init(void)
{
368
	const struct dmi_device *valid;
369 370
	int ret;

371 372 373 374 375 376
	valid = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Dell System", NULL);
	if (!valid) {
		pr_err("Unable to run on non-Dell system\n");
		return -ENODEV;
	}

377 378 379 380 381 382 383
	dmi_walk(find_tokens, NULL);

	if (!da_tokens)  {
		pr_info("Unable to find dmi tokens\n");
		return -ENODEV;
	}

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	ret = platform_driver_register(&platform_driver);
	if (ret)
		goto fail_platform_driver;

	platform_device = platform_device_alloc("dell-smbios", 0);
	if (!platform_device) {
		ret = -ENOMEM;
		goto fail_platform_device_alloc;
	}
	ret = platform_device_add(platform_device);
	if (ret)
		goto fail_platform_device_add;

	/* duplicate tokens will cause problems building sysfs files */
	zero_duplicates(&platform_device->dev);

	ret = build_tokens_sysfs(platform_device);
	if (ret)
		goto fail_create_group;
403 404 405

	return 0;

406 407 408 409 410 411 412 413 414 415
fail_create_group:
	platform_device_del(platform_device);

fail_platform_device_add:
	platform_device_put(platform_device);

fail_platform_device_alloc:
	platform_driver_unregister(&platform_driver);

fail_platform_driver:
416 417 418 419 420 421
	kfree(da_tokens);
	return ret;
}

static void __exit dell_smbios_exit(void)
{
422
	mutex_lock(&smbios_mutex);
423 424 425 426 427 428
	if (platform_device) {
		free_group(platform_device);
		platform_device_unregister(platform_device);
		platform_driver_unregister(&platform_driver);
	}
	kfree(da_tokens);
429
	mutex_unlock(&smbios_mutex);
430 431 432 433 434 435 436 437 438 439
}

subsys_initcall(dell_smbios_init);
module_exit(dell_smbios_exit);

MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
MODULE_LICENSE("GPL");