dell-laptop.c 17.1 KB
Newer Older
M
Matthew Garrett 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *  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.
 */

#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/rfkill.h>
#include <linux/power_supply.h>
#include <linux/acpi.h>
25
#include <linux/mm.h>
26
#include <linux/i8042.h>
27
#include <linux/slab.h>
K
Keng-Yu Lin 已提交
28 29
#include <linux/debugfs.h>
#include <linux/seq_file.h>
30
#include "../../firmware/dcdbas.h"
M
Matthew Garrett 已提交
31 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 62 63 64 65

#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;

static int da_command_address;
static int da_command_code;
static int da_num_tokens;
static struct calling_interface_token *da_tokens;

66 67 68 69 70 71 72 73
static struct platform_driver platform_driver = {
	.driver = {
		.name = "dell-laptop",
		.owner = THIS_MODULE,
	}
};

static struct platform_device *platform_device;
M
Matthew Garrett 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
static struct backlight_device *dell_backlight_device;
static struct rfkill *wifi_rfkill;
static struct rfkill *bluetooth_rfkill;
static struct rfkill *wwan_rfkill;

static const struct dmi_system_id __initdata dell_device_table[] = {
	{
		.ident = "Dell laptop",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
		},
	},
87 88 89 90 91 92
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
		},
	},
93 94 95 96 97 98 99
	{
		.ident = "Dell Computer Corporation",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
		},
	},
M
Matthew Garrett 已提交
100 101 102
	{ }
};

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
static struct dmi_system_id __devinitdata dell_blacklist[] = {
	/* Supported by compal-laptop */
	{
		.ident = "Dell Mini 9",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
		},
	},
	{
		.ident = "Dell Mini 10",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
		},
	},
	{
		.ident = "Dell Mini 10v",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
		},
	},
126 127 128 129 130 131 132
	{
		.ident = "Dell Mini 1012",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
		},
	},
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	{
		.ident = "Dell Inspiron 11z",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
		},
	},
	{
		.ident = "Dell Mini 12",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
		},
	},
	{}
};

150
static struct calling_interface_buffer *buffer;
151 152
static struct page *bufferpage;
static DEFINE_MUTEX(buffer_mutex);
153

154 155
static int hwswitch_state;

156 157 158 159 160 161 162 163 164 165 166
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);
}

167
static void __init parse_da_table(const struct dmi_header *dm)
M
Matthew Garrett 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
{
	/* 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;
}

196
static void __init find_tokens(const struct dmi_header *dm, void *dummy)
M
Matthew Garrett 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 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
{
	switch (dm->type) {
	case 0xd4: /* Indexed IO */
		break;
	case 0xd5: /* Protected Area Type 1 */
		break;
	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;
}

/* Derived from information in DellWirelessCtl.cpp:
   Class 17, select 11 is radio control. It returns an array of 32-bit values.

245 246
   Input byte 0 = 0: Wireless information

M
Matthew Garrett 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
   result[0]: return code
   result[1]:
     Bit 0:      Hardware switch supported
     Bit 1:      Wifi locator supported
     Bit 2:      Wifi is supported
     Bit 3:      Bluetooth is supported
     Bit 4:      WWAN is supported
     Bit 5:      Wireless keyboard supported
     Bits 6-7:   Reserved
     Bit 8:      Wifi is installed
     Bit 9:      Bluetooth is installed
     Bit 10:     WWAN is installed
     Bits 11-15: Reserved
     Bit 16:     Hardware switch is on
     Bit 17:     Wifi is blocked
     Bit 18:     Bluetooth is blocked
     Bit 19:     WWAN is blocked
     Bits 20-31: Reserved
   result[2]: NVRAM size in bytes
   result[3]: NVRAM format version number
267 268 269 270 271 272 273 274 275 276 277 278 279

   Input byte 0 = 2: Wireless switch configuration
   result[0]: return code
   result[1]:
     Bit 0:      Wifi controlled by switch
     Bit 1:      Bluetooth controlled by switch
     Bit 2:      WWAN controlled by switch
     Bits 3-6:   Reserved
     Bit 7:      Wireless switch config locked
     Bit 8:      Wifi locator enabled
     Bits 9-14:  Reserved
     Bit 15:     Wifi locator setting locked
     Bits 16-31: Reserved
M
Matthew Garrett 已提交
280 281
*/

J
Johannes Berg 已提交
282
static int dell_rfkill_set(void *data, bool blocked)
M
Matthew Garrett 已提交
283
{
284
	int disable = blocked ? 1 : 0;
J
Johannes Berg 已提交
285
	unsigned long radio = (unsigned long)data;
286
	int hwswitch_bit = (unsigned long)data - 1;
287
	int ret = 0;
M
Matthew Garrett 已提交
288

289 290
	get_buffer();
	dell_send_request(buffer, 17, 11);
291

292 293 294 295
	/* If the hardware switch controls this radio, and the hardware
	   switch is disabled, don't allow changing the software state */
	if ((hwswitch_state & BIT(hwswitch_bit)) &&
	    !(buffer->output[1] & BIT(16))) {
296 297 298
		ret = -EINVAL;
		goto out;
	}
M
Matthew Garrett 已提交
299

300 301 302 303 304 305
	buffer->input[0] = (1 | (radio<<8) | (disable << 16));
	dell_send_request(buffer, 17, 11);

out:
	release_buffer();
	return ret;
M
Matthew Garrett 已提交
306 307
}

J
Johannes Berg 已提交
308
static void dell_rfkill_query(struct rfkill *rfkill, void *data)
M
Matthew Garrett 已提交
309 310
{
	int status;
J
Johannes Berg 已提交
311
	int bit = (unsigned long)data + 16;
312
	int hwswitch_bit = (unsigned long)data - 1;
M
Matthew Garrett 已提交
313

314 315 316 317
	get_buffer();
	dell_send_request(buffer, 17, 11);
	status = buffer->output[1];
	release_buffer();
M
Matthew Garrett 已提交
318

319
	rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
320 321 322

	if (hwswitch_state & (BIT(hwswitch_bit)))
		rfkill_set_hw_state(rfkill, !(status & BIT(16)));
M
Matthew Garrett 已提交
323 324
}

J
Johannes Berg 已提交
325 326 327 328
static const struct rfkill_ops dell_rfkill_ops = {
	.set_block = dell_rfkill_set,
	.query = dell_rfkill_query,
};
M
Matthew Garrett 已提交
329

K
Keng-Yu Lin 已提交
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 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
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,
};

399 400 401 402 403 404 405 406 407 408 409 410
static void dell_update_rfkill(struct work_struct *ignored)
{
	if (wifi_rfkill)
		dell_rfkill_query(wifi_rfkill, (void *)1);
	if (bluetooth_rfkill)
		dell_rfkill_query(bluetooth_rfkill, (void *)2);
	if (wwan_rfkill)
		dell_rfkill_query(wwan_rfkill, (void *)3);
}
static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);


411
static int __init dell_setup_rfkill(void)
M
Matthew Garrett 已提交
412 413 414 415
{
	int status;
	int ret;

416 417 418 419 420 421
	if (dmi_check_system(dell_blacklist)) {
		printk(KERN_INFO "dell-laptop: Blacklisted hardware detected - "
				"not enabling rfkill\n");
		return 0;
	}

422 423 424
	get_buffer();
	dell_send_request(buffer, 17, 11);
	status = buffer->output[1];
425 426 427
	buffer->input[0] = 0x2;
	dell_send_request(buffer, 17, 11);
	hwswitch_state = buffer->output[1];
428
	release_buffer();
M
Matthew Garrett 已提交
429 430

	if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
431 432
		wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
					   RFKILL_TYPE_WLAN,
J
Johannes Berg 已提交
433 434 435
					   &dell_rfkill_ops, (void *) 1);
		if (!wifi_rfkill) {
			ret = -ENOMEM;
M
Matthew Garrett 已提交
436
			goto err_wifi;
J
Johannes Berg 已提交
437
		}
M
Matthew Garrett 已提交
438 439 440 441 442 443
		ret = rfkill_register(wifi_rfkill);
		if (ret)
			goto err_wifi;
	}

	if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
444 445
		bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
						&platform_device->dev,
J
Johannes Berg 已提交
446 447 448 449
						RFKILL_TYPE_BLUETOOTH,
						&dell_rfkill_ops, (void *) 2);
		if (!bluetooth_rfkill) {
			ret = -ENOMEM;
M
Matthew Garrett 已提交
450
			goto err_bluetooth;
J
Johannes Berg 已提交
451
		}
M
Matthew Garrett 已提交
452 453 454 455 456 457
		ret = rfkill_register(bluetooth_rfkill);
		if (ret)
			goto err_bluetooth;
	}

	if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
458 459 460
		wwan_rfkill = rfkill_alloc("dell-wwan",
					   &platform_device->dev,
					   RFKILL_TYPE_WWAN,
J
Johannes Berg 已提交
461 462 463
					   &dell_rfkill_ops, (void *) 3);
		if (!wwan_rfkill) {
			ret = -ENOMEM;
M
Matthew Garrett 已提交
464
			goto err_wwan;
J
Johannes Berg 已提交
465
		}
M
Matthew Garrett 已提交
466 467 468 469 470 471 472
		ret = rfkill_register(wwan_rfkill);
		if (ret)
			goto err_wwan;
	}

	return 0;
err_wwan:
J
Johannes Berg 已提交
473 474
	rfkill_destroy(wwan_rfkill);
	if (bluetooth_rfkill)
M
Matthew Garrett 已提交
475 476
		rfkill_unregister(bluetooth_rfkill);
err_bluetooth:
J
Johannes Berg 已提交
477 478
	rfkill_destroy(bluetooth_rfkill);
	if (wifi_rfkill)
M
Matthew Garrett 已提交
479 480
		rfkill_unregister(wifi_rfkill);
err_wifi:
J
Johannes Berg 已提交
481
	rfkill_destroy(wifi_rfkill);
M
Matthew Garrett 已提交
482 483 484 485

	return ret;
}

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
static void dell_cleanup_rfkill(void)
{
	if (wifi_rfkill) {
		rfkill_unregister(wifi_rfkill);
		rfkill_destroy(wifi_rfkill);
	}
	if (bluetooth_rfkill) {
		rfkill_unregister(bluetooth_rfkill);
		rfkill_destroy(bluetooth_rfkill);
	}
	if (wwan_rfkill) {
		rfkill_unregister(wwan_rfkill);
		rfkill_destroy(wwan_rfkill);
	}
}

M
Matthew Garrett 已提交
502 503
static int dell_send_intensity(struct backlight_device *bd)
{
504
	int ret = 0;
M
Matthew Garrett 已提交
505

506 507 508
	get_buffer();
	buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
	buffer->input[1] = bd->props.brightness;
M
Matthew Garrett 已提交
509

510 511 512 513
	if (buffer->input[0] == -1) {
		ret = -ENODEV;
		goto out;
	}
M
Matthew Garrett 已提交
514 515

	if (power_supply_is_system_supplied() > 0)
516
		dell_send_request(buffer, 1, 2);
M
Matthew Garrett 已提交
517
	else
518
		dell_send_request(buffer, 1, 1);
M
Matthew Garrett 已提交
519

520 521
out:
	release_buffer();
M
Matthew Garrett 已提交
522 523 524 525 526
	return 0;
}

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

529 530
	get_buffer();
	buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
M
Matthew Garrett 已提交
531

532 533 534 535
	if (buffer->input[0] == -1) {
		ret = -ENODEV;
		goto out;
	}
M
Matthew Garrett 已提交
536 537

	if (power_supply_is_system_supplied() > 0)
538
		dell_send_request(buffer, 0, 2);
M
Matthew Garrett 已提交
539
	else
540
		dell_send_request(buffer, 0, 1);
M
Matthew Garrett 已提交
541

542 543 544 545 546
out:
	release_buffer();
	if (ret)
		return ret;
	return buffer->output[1];
M
Matthew Garrett 已提交
547 548 549 550 551 552 553
}

static struct backlight_ops dell_ops = {
	.get_brightness = dell_get_intensity,
	.update_status  = dell_send_intensity,
};

554
static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
			      struct serio *port)
{
	static bool extended;

	if (str & 0x20)
		return false;

	if (unlikely(data == 0xe0)) {
		extended = true;
		return false;
	} else if (unlikely(extended)) {
		switch (data) {
		case 0x8:
			schedule_delayed_work(&dell_rfkill_work,
					      round_jiffies_relative(HZ));
			break;
		}
		extended = false;
	}

	return false;
}

M
Matthew Garrett 已提交
578 579 580 581 582 583 584 585
static int __init dell_init(void)
{
	int max_intensity = 0;
	int ret;

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

586
	dmi_walk(find_tokens, NULL);
M
Matthew Garrett 已提交
587 588 589 590 591 592

	if (!da_tokens)  {
		printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
		return -ENODEV;
	}

593 594 595 596 597 598 599 600 601 602 603 604
	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;

605 606 607 608 609 610 611 612 613 614 615
	/*
	 * 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);
	mutex_init(&buffer_mutex);

M
Matthew Garrett 已提交
616 617 618 619
	ret = dell_setup_rfkill();

	if (ret) {
		printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
620
		goto fail_rfkill;
M
Matthew Garrett 已提交
621 622
	}

623 624 625 626 627 628 629
	ret = i8042_install_filter(dell_laptop_i8042_filter);
	if (ret) {
		printk(KERN_WARNING
		       "dell-laptop: Unable to install key filter\n");
		goto fail_filter;
	}

K
Keng-Yu Lin 已提交
630 631 632 633 634
	dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
	if (dell_laptop_dir != NULL)
		debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
				    &dell_debugfs_fops);

M
Matthew Garrett 已提交
635 636 637 638 639 640 641 642
#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

643 644 645 646 647
	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 已提交
648
	}
649
	release_buffer();
M
Matthew Garrett 已提交
650 651

	if (max_intensity) {
652 653 654 655 656 657 658 659
		struct backlight_properties props;
		memset(&props, 0, sizeof(struct backlight_properties));
		props.max_brightness = max_intensity;
		dell_backlight_device = backlight_device_register("dell_backlight",
								  &platform_device->dev,
								  NULL,
								  &dell_ops,
								  &props);
M
Matthew Garrett 已提交
660 661 662 663

		if (IS_ERR(dell_backlight_device)) {
			ret = PTR_ERR(dell_backlight_device);
			dell_backlight_device = NULL;
664
			goto fail_backlight;
M
Matthew Garrett 已提交
665 666 667 668 669 670 671 672
		}

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

	return 0;
673 674

fail_backlight:
675
	i8042_remove_filter(dell_laptop_i8042_filter);
676
	cancel_delayed_work_sync(&dell_rfkill_work);
677
fail_filter:
678
	dell_cleanup_rfkill();
679
fail_rfkill:
680 681
	free_page((unsigned long)bufferpage);
fail_buffer:
682 683 684 685 686 687
	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 已提交
688 689 690 691 692 693
	kfree(da_tokens);
	return ret;
}

static void __exit dell_exit(void)
{
K
Keng-Yu Lin 已提交
694
	debugfs_remove_recursive(dell_laptop_dir);
695
	i8042_remove_filter(dell_laptop_i8042_filter);
696
	cancel_delayed_work_sync(&dell_rfkill_work);
M
Matthew Garrett 已提交
697
	backlight_device_unregister(dell_backlight_device);
698
	dell_cleanup_rfkill();
699
	if (platform_device) {
700
		platform_device_unregister(platform_device);
701 702
		platform_driver_unregister(&platform_driver);
	}
703
	kfree(da_tokens);
704
	free_page((unsigned long)buffer);
M
Matthew Garrett 已提交
705 706 707 708 709 710 711 712 713
}

module_init(dell_init);
module_exit(dell_exit);

MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
MODULE_DESCRIPTION("Dell laptop driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");
714
MODULE_ALIAS("dmi:*svnDellInc.:*:ct9:*");
715
MODULE_ALIAS("dmi:*svnDellComputerCorporation.:*:ct8:*");