dell-laptop.c 14.9 KB
Newer Older
M
Matthew Garrett 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  Driver for Dell laptop extras
 *
 *  Copyright (c) Red Hat <mjg@redhat.com>
 *
 *  Based on documentation in the libsmbios package, Copyright (C) 2005 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.
 */

14 15
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

M
Matthew Garrett 已提交
16 17 18 19 20 21 22 23 24 25
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/backlight.h>
#include <linux/err.h>
#include <linux/dmi.h>
#include <linux/io.h>
#include <linux/power_supply.h>
#include <linux/acpi.h>
26
#include <linux/mm.h>
27
#include <linux/i8042.h>
28
#include <linux/slab.h>
K
Keng-Yu Lin 已提交
29 30
#include <linux/debugfs.h>
#include <linux/seq_file.h>
31
#include "../../firmware/dcdbas.h"
M
Matthew Garrett 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

#define BRIGHTNESS_TOKEN 0x7d

/* This structure will be modified by the firmware when we enter
 * system management mode, hence the volatiles */

struct calling_interface_buffer {
	u16 class;
	u16 select;
	volatile u32 input[4];
	volatile u32 output[4];
} __packed;

struct calling_interface_token {
	u16 tokenID;
	u16 location;
	union {
		u16 value;
		u16 stringlength;
	};
};

struct calling_interface_structure {
	struct dmi_header header;
	u16 cmdIOAddress;
	u8 cmdIOCode;
	u32 supportedCmds;
	struct calling_interface_token tokens[];
} __packed;

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
struct quirk_entry {
	u8 touchpad_led;
};

static struct quirk_entry *quirks;

static struct quirk_entry quirk_dell_vostro_v130 = {
	.touchpad_led = 1,
};

static int dmi_matched(const struct dmi_system_id *dmi)
{
	quirks = dmi->driver_data;
	return 1;
}

M
Matthew Garrett 已提交
78 79 80 81 82
static int da_command_address;
static int da_command_code;
static int da_num_tokens;
static struct calling_interface_token *da_tokens;

83 84 85 86 87 88 89 90
static struct platform_driver platform_driver = {
	.driver = {
		.name = "dell-laptop",
		.owner = THIS_MODULE,
	}
};

static struct platform_device *platform_device;
M
Matthew Garrett 已提交
91 92
static struct backlight_device *dell_backlight_device;

93
static const struct dmi_system_id dell_device_table[] __initconst = {
M
Matthew Garrett 已提交
94 95 96 97 98 99 100
	{
		.ident = "Dell laptop",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
		},
	},
101 102 103 104 105 106
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
		},
	},
107 108 109 110 111 112 113
	{
		.ident = "Dell Computer Corporation",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
		},
	},
M
Matthew Garrett 已提交
114 115
	{ }
};
116
MODULE_DEVICE_TABLE(dmi, dell_device_table);
M
Matthew Garrett 已提交
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
static struct dmi_system_id __devinitdata dell_quirks[] = {
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro V130",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro V131",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
A
Ang Way Chuang 已提交
137 138 139 140 141 142 143 144 145
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3350",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3555",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron N311z",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron M5110",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3360",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3460",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3560",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
200 201 202 203 204 205 206 207 208
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3450",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
209 210 211 212 213
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 5420",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
214
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
215 216 217 218 219 220 221 222
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 5520",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
223
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
224 225 226 227 228 229 230 231
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 5720",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
232
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
233 234 235 236 237 238 239 240
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 7420",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
241
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
242 243 244 245 246 247 248 249
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 7520",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
250
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
251 252 253 254 255 256 257 258
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 7720",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
259
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
260 261 262
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
263
	{ }
264 265
};

266
static struct calling_interface_buffer *buffer;
267 268
static struct page *bufferpage;
static DEFINE_MUTEX(buffer_mutex);
269

270 271
static int hwswitch_state;

272 273 274 275 276 277 278 279 280 281 282
static void get_buffer(void)
{
	mutex_lock(&buffer_mutex);
	memset(buffer, 0, sizeof(struct calling_interface_buffer));
}

static void release_buffer(void)
{
	mutex_unlock(&buffer_mutex);
}

283
static void __init parse_da_table(const struct dmi_header *dm)
M
Matthew Garrett 已提交
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
{
	/* 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_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;

	da_command_address = table->cmdIOAddress;
	da_command_code = table->cmdIOCode;

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

	if (!da_tokens)
		return;

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

	da_num_tokens += tokens;
}

312
static void __init find_tokens(const struct dmi_header *dm, void *dummy)
M
Matthew Garrett 已提交
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
{
	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;
	}
}

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

	return -1;
}

static struct calling_interface_buffer *
dell_send_request(struct calling_interface_buffer *buffer, int class,
		  int select)
{
	struct smi_cmd command;

	command.magic = SMI_CMD_MAGIC;
	command.command_address = da_command_address;
	command.command_code = da_command_code;
	command.ebx = virt_to_phys(buffer);
	command.ecx = 0x42534931;

	buffer->class = class;
	buffer->select = select;

	dcdbas_smi_request(&command);

	return buffer;
}

K
Keng-Yu Lin 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
static struct dentry *dell_laptop_dir;

static int dell_debugfs_show(struct seq_file *s, void *data)
{
	int status;

	get_buffer();
	dell_send_request(buffer, 17, 11);
	status = buffer->output[1];
	release_buffer();

	seq_printf(s, "status:\t0x%X\n", status);
	seq_printf(s, "Bit 0 : Hardware switch supported:   %lu\n",
		   status & BIT(0));
	seq_printf(s, "Bit 1 : Wifi locator supported:      %lu\n",
		  (status & BIT(1)) >> 1);
	seq_printf(s, "Bit 2 : Wifi is supported:           %lu\n",
		  (status & BIT(2)) >> 2);
	seq_printf(s, "Bit 3 : Bluetooth is supported:      %lu\n",
		  (status & BIT(3)) >> 3);
	seq_printf(s, "Bit 4 : WWAN is supported:           %lu\n",
		  (status & BIT(4)) >> 4);
	seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
		  (status & BIT(5)) >> 5);
	seq_printf(s, "Bit 8 : Wifi is installed:           %lu\n",
		  (status & BIT(8)) >> 8);
	seq_printf(s, "Bit 9 : Bluetooth is installed:      %lu\n",
		  (status & BIT(9)) >> 9);
	seq_printf(s, "Bit 10: WWAN is installed:           %lu\n",
		  (status & BIT(10)) >> 10);
	seq_printf(s, "Bit 16: Hardware switch is on:       %lu\n",
		  (status & BIT(16)) >> 16);
	seq_printf(s, "Bit 17: Wifi is blocked:             %lu\n",
		  (status & BIT(17)) >> 17);
	seq_printf(s, "Bit 18: Bluetooth is blocked:        %lu\n",
		  (status & BIT(18)) >> 18);
	seq_printf(s, "Bit 19: WWAN is blocked:             %lu\n",
		  (status & BIT(19)) >> 19);

	seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
	seq_printf(s, "Bit 0 : Wifi controlled by switch:      %lu\n",
		   hwswitch_state & BIT(0));
	seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
		   (hwswitch_state & BIT(1)) >> 1);
	seq_printf(s, "Bit 2 : WWAN controlled by switch:      %lu\n",
		   (hwswitch_state & BIT(2)) >> 2);
	seq_printf(s, "Bit 7 : Wireless switch config locked:  %lu\n",
		   (hwswitch_state & BIT(7)) >> 7);
	seq_printf(s, "Bit 8 : Wifi locator enabled:           %lu\n",
		   (hwswitch_state & BIT(8)) >> 8);
	seq_printf(s, "Bit 15: Wifi locator setting locked:    %lu\n",
		   (hwswitch_state & BIT(15)) >> 15);

	return 0;
}

static int dell_debugfs_open(struct inode *inode, struct file *file)
{
	return single_open(file, dell_debugfs_show, inode->i_private);
}

static const struct file_operations dell_debugfs_fops = {
	.owner = THIS_MODULE,
	.open = dell_debugfs_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

M
Matthew Garrett 已提交
425 426
static int dell_send_intensity(struct backlight_device *bd)
{
427
	int ret = 0;
M
Matthew Garrett 已提交
428

429 430 431
	get_buffer();
	buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
	buffer->input[1] = bd->props.brightness;
M
Matthew Garrett 已提交
432

433 434 435 436
	if (buffer->input[0] == -1) {
		ret = -ENODEV;
		goto out;
	}
M
Matthew Garrett 已提交
437 438

	if (power_supply_is_system_supplied() > 0)
439
		dell_send_request(buffer, 1, 2);
M
Matthew Garrett 已提交
440
	else
441
		dell_send_request(buffer, 1, 1);
M
Matthew Garrett 已提交
442

443 444
out:
	release_buffer();
M
Matthew Garrett 已提交
445 446 447 448 449
	return 0;
}

static int dell_get_intensity(struct backlight_device *bd)
{
450
	int ret = 0;
M
Matthew Garrett 已提交
451

452 453
	get_buffer();
	buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
M
Matthew Garrett 已提交
454

455 456 457 458
	if (buffer->input[0] == -1) {
		ret = -ENODEV;
		goto out;
	}
M
Matthew Garrett 已提交
459 460

	if (power_supply_is_system_supplied() > 0)
461
		dell_send_request(buffer, 0, 2);
M
Matthew Garrett 已提交
462
	else
463
		dell_send_request(buffer, 0, 1);
M
Matthew Garrett 已提交
464

465 466
	ret = buffer->output[1];

467 468
out:
	release_buffer();
469
	return ret;
M
Matthew Garrett 已提交
470 471
}

L
Lionel Debroux 已提交
472
static const struct backlight_ops dell_ops = {
M
Matthew Garrett 已提交
473 474 475 476
	.get_brightness = dell_get_intensity,
	.update_status  = dell_send_intensity,
};

477
static void touchpad_led_on(void)
478 479 480 481 482 483
{
	int command = 0x97;
	char data = 1;
	i8042_command(&data, command | 1 << 12);
}

484
static void touchpad_led_off(void)
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
{
	int command = 0x97;
	char data = 2;
	i8042_command(&data, command | 1 << 12);
}

static void touchpad_led_set(struct led_classdev *led_cdev,
	enum led_brightness value)
{
	if (value > 0)
		touchpad_led_on();
	else
		touchpad_led_off();
}

static struct led_classdev touchpad_led = {
	.name = "dell-laptop::touchpad",
	.brightness_set = touchpad_led_set,
503
	.flags = LED_CORE_SUSPENDRESUME,
504 505 506 507 508 509 510 511 512 513 514 515
};

static int __devinit touchpad_led_init(struct device *dev)
{
	return led_classdev_register(dev, &touchpad_led);
}

static void touchpad_led_exit(void)
{
	led_classdev_unregister(&touchpad_led);
}

M
Matthew Garrett 已提交
516 517 518 519 520 521 522 523
static int __init dell_init(void)
{
	int max_intensity = 0;
	int ret;

	if (!dmi_check_system(dell_device_table))
		return -ENODEV;

524 525 526 527
	quirks = NULL;
	/* find if this machine support other functions */
	dmi_check_system(dell_quirks);

528
	dmi_walk(find_tokens, NULL);
M
Matthew Garrett 已提交
529 530

	if (!da_tokens)  {
531
		pr_info("Unable to find dmi tokens\n");
M
Matthew Garrett 已提交
532 533 534
		return -ENODEV;
	}

535 536 537 538 539 540 541 542 543 544 545 546
	ret = platform_driver_register(&platform_driver);
	if (ret)
		goto fail_platform_driver;
	platform_device = platform_device_alloc("dell-laptop", -1);
	if (!platform_device) {
		ret = -ENOMEM;
		goto fail_platform_device1;
	}
	ret = platform_device_add(platform_device);
	if (ret)
		goto fail_platform_device2;

547 548 549 550 551 552 553 554 555 556
	/*
	 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
	 * is passed to SMI handler.
	 */
	bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);

	if (!bufferpage)
		goto fail_buffer;
	buffer = page_address(bufferpage);

557 558 559
	if (quirks && quirks->touchpad_led)
		touchpad_led_init(&platform_device->dev);

K
Keng-Yu Lin 已提交
560 561
	dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);

M
Matthew Garrett 已提交
562 563 564 565 566 567 568 569
#ifdef CONFIG_ACPI
	/* In the event of an ACPI backlight being available, don't
	 * register the platform controller.
	 */
	if (acpi_video_backlight_support())
		return 0;
#endif

570 571 572 573 574
	get_buffer();
	buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
	if (buffer->input[0] != -1) {
		dell_send_request(buffer, 0, 2);
		max_intensity = buffer->output[3];
M
Matthew Garrett 已提交
575
	}
576
	release_buffer();
M
Matthew Garrett 已提交
577 578

	if (max_intensity) {
579 580
		struct backlight_properties props;
		memset(&props, 0, sizeof(struct backlight_properties));
M
Matthew Garrett 已提交
581
		props.type = BACKLIGHT_PLATFORM;
582 583 584 585 586 587
		props.max_brightness = max_intensity;
		dell_backlight_device = backlight_device_register("dell_backlight",
								  &platform_device->dev,
								  NULL,
								  &dell_ops,
								  &props);
M
Matthew Garrett 已提交
588 589 590 591

		if (IS_ERR(dell_backlight_device)) {
			ret = PTR_ERR(dell_backlight_device);
			dell_backlight_device = NULL;
592
			goto fail_backlight;
M
Matthew Garrett 已提交
593 594 595 596 597 598 599 600
		}

		dell_backlight_device->props.brightness =
			dell_get_intensity(dell_backlight_device);
		backlight_update_status(dell_backlight_device);
	}

	return 0;
601 602

fail_backlight:
603 604
	free_page((unsigned long)bufferpage);
fail_buffer:
605 606 607 608 609 610
	platform_device_del(platform_device);
fail_platform_device2:
	platform_device_put(platform_device);
fail_platform_device1:
	platform_driver_unregister(&platform_driver);
fail_platform_driver:
M
Matthew Garrett 已提交
611 612 613 614 615 616
	kfree(da_tokens);
	return ret;
}

static void __exit dell_exit(void)
{
K
Keng-Yu Lin 已提交
617
	debugfs_remove_recursive(dell_laptop_dir);
618 619
	if (quirks && quirks->touchpad_led)
		touchpad_led_exit();
M
Matthew Garrett 已提交
620
	backlight_device_unregister(dell_backlight_device);
621
	if (platform_device) {
622
		platform_device_unregister(platform_device);
623 624
		platform_driver_unregister(&platform_driver);
	}
625
	kfree(da_tokens);
626
	free_page((unsigned long)buffer);
M
Matthew Garrett 已提交
627 628 629 630 631 632 633 634
}

module_init(dell_init);
module_exit(dell_exit);

MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
MODULE_DESCRIPTION("Dell laptop driver");
MODULE_LICENSE("GPL");