msi-laptop.c 23.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*-*-linux-c-*-*/

/*
  Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
 */

/*
 * msi-laptop.c - MSI S270 laptop support. This laptop is sold under
 * various brands, including "Cytron/TCM/Medion/Tchibo MD96100".
 *
26 27
 * Driver also supports S271, S420 models.
 *
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 * This driver exports a few files in /sys/devices/platform/msi-laptop-pf/:
 *
 *   lcd_level - Screen brightness: contains a single integer in the
 *   range 0..8. (rw)
 *
 *   auto_brightness - Enable automatic brightness control: contains
 *   either 0 or 1. If set to 1 the hardware adjusts the screen
 *   brightness automatically when the power cord is
 *   plugged/unplugged. (rw)
 *
 *   wlan - WLAN subsystem enabled: contains either 0 or 1. (ro)
 *
 *   bluetooth - Bluetooth subsystem enabled: contains either 0 or 1
 *   Please note that this file is constantly 0 if no Bluetooth
 *   hardware is available. (ro)
 *
 * In addition to these platform device attributes the driver
 * registers itself in the Linux backlight control subsystem and is
 * available to userspace under /sys/class/backlight/msi-laptop-bl/.
 *
 * This driver might work on other laptops produced by MSI. If you
 * want to try it you can pass force=1 as argument to the module which
 * will force it to load even when the DMI data doesn't identify the
 * laptop as MSI S270. YMMV.
 */

54 55
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

56 57 58 59 60 61 62
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/acpi.h>
#include <linux/dmi.h>
#include <linux/backlight.h>
#include <linux/platform_device.h>
63
#include <linux/rfkill.h>
64
#include <linux/i8042.h>
65 66
#include <linux/input.h>
#include <linux/input/sparse-keymap.h>
67 68 69 70 71 72 73 74

#define MSI_DRIVER_VERSION "0.5"

#define MSI_LCD_LEVEL_MAX 9

#define MSI_EC_COMMAND_WIRELESS 0x10
#define MSI_EC_COMMAND_LCD_LEVEL 0x11

75 76 77 78
#define MSI_STANDARD_EC_COMMAND_ADDRESS	0x2e
#define MSI_STANDARD_EC_BLUETOOTH_MASK	(1 << 0)
#define MSI_STANDARD_EC_WEBCAM_MASK	(1 << 1)
#define MSI_STANDARD_EC_WLAN_MASK	(1 << 3)
79
#define MSI_STANDARD_EC_3G_MASK		(1 << 4)
80

81 82 83 84
/* For set SCM load flag to disable BIOS fn key */
#define MSI_STANDARD_EC_SCM_LOAD_ADDRESS	0x2d
#define MSI_STANDARD_EC_SCM_LOAD_MASK		(1 << 0)

85 86 87
#define MSI_STANDARD_EC_TOUCHPAD_ADDRESS	0xe4
#define MSI_STANDARD_EC_TOUCHPAD_MASK		(1 << 4)

88 89
static int msi_laptop_resume(struct platform_device *device);

90 91
#define MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS	0x2f

92 93 94 95 96 97 98 99
static int force;
module_param(force, bool, 0);
MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");

static int auto_brightness;
module_param(auto_brightness, int, 0);
MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");

100 101 102 103 104 105 106 107
static const struct key_entry msi_laptop_keymap[] = {
	{KE_KEY, KEY_TOUCHPAD_ON, {KEY_TOUCHPAD_ON} },	/* Touch Pad On */
	{KE_KEY, KEY_TOUCHPAD_OFF, {KEY_TOUCHPAD_OFF} },/* Touch Pad On */
	{KE_END, 0}
};

static struct input_dev *msi_laptop_input_dev;

108
static bool old_ec_model;
109
static int wlan_s, bluetooth_s, threeg_s;
110
static int threeg_exists;
111

112 113 114 115 116 117 118 119 120 121 122 123 124
/* Some MSI 3G netbook only have one fn key to control Wlan/Bluetooth/3G,
 * those netbook will load the SCM (windows app) to disable the original
 * Wlan/Bluetooth control by BIOS when user press fn key, then control
 * Wlan/Bluetooth/3G by SCM (software control by OS). Without SCM, user
 * cann't on/off 3G module on those 3G netbook.
 * On Linux, msi-laptop driver will do the same thing to disable the
 * original BIOS control, then might need use HAL or other userland
 * application to do the software control that simulate with SCM.
 * e.g. MSI N034 netbook
 */
static bool load_scm_model;
static struct rfkill *rfk_wlan, *rfk_bluetooth, *rfk_threeg;

125 126 127 128 129 130 131 132 133 134 135 136
/* Hardware access */

static int set_lcd_level(int level)
{
	u8 buf[2];

	if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
		return -EINVAL;

	buf[0] = 0x80;
	buf[1] = (u8) (level*31);

137
	return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf),
T
Thomas Renninger 已提交
138
			      NULL, 0);
139 140 141 142 143 144 145
}

static int get_lcd_level(void)
{
	u8 wdata = 0, rdata;
	int result;

146
	result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1,
T
Thomas Renninger 已提交
147
				&rdata, 1);
148 149 150 151 152 153 154 155 156 157 158
	if (result < 0)
		return result;

	return (int) rdata / 31;
}

static int get_auto_brightness(void)
{
	u8 wdata = 4, rdata;
	int result;

159
	result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1,
T
Thomas Renninger 已提交
160
				&rdata, 1);
161 162 163 164 165 166 167 168 169 170 171 172 173
	if (result < 0)
		return result;

	return !!(rdata & 8);
}

static int set_auto_brightness(int enable)
{
	u8 wdata[2], rdata;
	int result;

	wdata[0] = 4;

174
	result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1,
T
Thomas Renninger 已提交
175
				&rdata, 1);
176 177 178 179 180 181
	if (result < 0)
		return result;

	wdata[0] = 0x84;
	wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);

182
	return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2,
T
Thomas Renninger 已提交
183
			      NULL, 0);
184 185
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
static ssize_t set_device_state(const char *buf, size_t count, u8 mask)
{
	int status;
	u8 wdata = 0, rdata;
	int result;

	if (sscanf(buf, "%i", &status) != 1 || (status < 0 || status > 1))
		return -EINVAL;

	/* read current device state */
	result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
	if (result < 0)
		return -EINVAL;

	if (!!(rdata & mask) != status) {
		/* reverse device bit */
		if (rdata & mask)
			wdata = rdata & ~mask;
		else
			wdata = rdata | mask;

		result = ec_write(MSI_STANDARD_EC_COMMAND_ADDRESS, wdata);
		if (result < 0)
			return -EINVAL;
	}

	return count;
}

215 216 217 218 219
static int get_wireless_state(int *wlan, int *bluetooth)
{
	u8 wdata = 0, rdata;
	int result;

T
Thomas Renninger 已提交
220
	result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1);
221 222 223 224 225 226 227 228 229 230 231 232
	if (result < 0)
		return -1;

	if (wlan)
		*wlan = !!(rdata & 8);

	if (bluetooth)
		*bluetooth = !!(rdata & 128);

	return 0;
}

233 234 235 236 237 238 239 240 241 242 243 244 245
static int get_wireless_state_ec_standard(void)
{
	u8 rdata;
	int result;

	result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
	if (result < 0)
		return -1;

	wlan_s = !!(rdata & MSI_STANDARD_EC_WLAN_MASK);

	bluetooth_s = !!(rdata & MSI_STANDARD_EC_BLUETOOTH_MASK);

246 247
	threeg_s = !!(rdata & MSI_STANDARD_EC_3G_MASK);

248 249 250
	return 0;
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264
static int get_threeg_exists(void)
{
	u8 rdata;
	int result;

	result = ec_read(MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS, &rdata);
	if (result < 0)
		return -1;

	threeg_exists = !!(rdata & MSI_STANDARD_EC_3G_MASK);

	return 0;
}

265 266 267 268 269 270 271 272 273 274
/* Backlight device stuff */

static int bl_get_brightness(struct backlight_device *b)
{
	return get_lcd_level();
}


static int bl_update_status(struct backlight_device *b)
{
275
	return set_lcd_level(b->props.brightness);
276 277
}

278
static const struct backlight_ops msibl_ops = {
279 280 281 282 283 284 285 286 287 288 289 290 291 292
	.get_brightness = bl_get_brightness,
	.update_status  = bl_update_status,
};

static struct backlight_device *msibl_device;

/* Platform device */

static ssize_t show_wlan(struct device *dev,
	struct device_attribute *attr, char *buf)
{

	int ret, enabled;

293 294 295 296 297 298
	if (old_ec_model) {
		ret = get_wireless_state(&enabled, NULL);
	} else {
		ret = get_wireless_state_ec_standard();
		enabled = wlan_s;
	}
299 300 301 302 303 304
	if (ret < 0)
		return ret;

	return sprintf(buf, "%i\n", enabled);
}

305 306 307 308 309 310
static ssize_t store_wlan(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	return set_device_state(buf, count, MSI_STANDARD_EC_WLAN_MASK);
}

311 312 313 314 315 316
static ssize_t show_bluetooth(struct device *dev,
	struct device_attribute *attr, char *buf)
{

	int ret, enabled;

317 318 319 320 321 322
	if (old_ec_model) {
		ret = get_wireless_state(NULL, &enabled);
	} else {
		ret = get_wireless_state_ec_standard();
		enabled = bluetooth_s;
	}
323 324 325 326 327 328
	if (ret < 0)
		return ret;

	return sprintf(buf, "%i\n", enabled);
}

329 330 331 332 333 334
static ssize_t store_bluetooth(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	return set_device_state(buf, count, MSI_STANDARD_EC_BLUETOOTH_MASK);
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
static ssize_t show_threeg(struct device *dev,
	struct device_attribute *attr, char *buf)
{

	int ret;

	/* old msi ec not support 3G */
	if (old_ec_model)
		return -1;

	ret = get_wireless_state_ec_standard();
	if (ret < 0)
		return ret;

	return sprintf(buf, "%i\n", threeg_s);
}

352 353 354 355 356 357
static ssize_t store_threeg(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	return set_device_state(buf, count, MSI_STANDARD_EC_3G_MASK);
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
static ssize_t show_lcd_level(struct device *dev,
	struct device_attribute *attr, char *buf)
{

	int ret;

	ret = get_lcd_level();
	if (ret < 0)
		return ret;

	return sprintf(buf, "%i\n", ret);
}

static ssize_t store_lcd_level(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{

	int level, ret;

377 378
	if (sscanf(buf, "%i", &level) != 1 ||
	    (level < 0 || level >= MSI_LCD_LEVEL_MAX))
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
		return -EINVAL;

	ret = set_lcd_level(level);
	if (ret < 0)
		return ret;

	return count;
}

static ssize_t show_auto_brightness(struct device *dev,
	struct device_attribute *attr, char *buf)
{

	int ret;

	ret = get_auto_brightness();
	if (ret < 0)
		return ret;

	return sprintf(buf, "%i\n", ret);
}

static ssize_t store_auto_brightness(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{

	int enable, ret;

	if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
		return -EINVAL;

	ret = set_auto_brightness(enable);
	if (ret < 0)
		return ret;

	return count;
}

static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
418 419
static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness,
		   store_auto_brightness);
420 421
static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
422
static DEVICE_ATTR(threeg, 0444, show_threeg, NULL);
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

static struct attribute *msipf_attributes[] = {
	&dev_attr_lcd_level.attr,
	&dev_attr_auto_brightness.attr,
	&dev_attr_bluetooth.attr,
	&dev_attr_wlan.attr,
	NULL
};

static struct attribute_group msipf_attribute_group = {
	.attrs = msipf_attributes
};

static struct platform_driver msipf_driver = {
	.driver = {
		.name = "msi-laptop-pf",
		.owner = THIS_MODULE,
440 441
	},
	.resume = msi_laptop_resume,
442 443 444 445 446 447
};

static struct platform_device *msipf_device;

/* Initialization */

448
static int dmi_check_cb(const struct dmi_system_id *id)
449
{
J
Joe Perches 已提交
450
	pr_info("Identified laptop model '%s'\n", id->ident);
451
	return 1;
452 453
}

454 455 456 457 458 459
static struct dmi_system_id __initdata msi_dmi_table[] = {
	{
		.ident = "MSI S270",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
			DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
460
			DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
461 462
			DMI_MATCH(DMI_CHASSIS_VENDOR,
				  "MICRO-STAR INT'L CO.,LTD")
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
		},
		.callback = dmi_check_cb
	},
	{
		.ident = "MSI S271",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
			DMI_MATCH(DMI_PRODUCT_NAME, "MS-1058"),
			DMI_MATCH(DMI_PRODUCT_VERSION, "0581"),
			DMI_MATCH(DMI_BOARD_NAME, "MS-1058")
		},
		.callback = dmi_check_cb
	},
	{
		.ident = "MSI S420",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
			DMI_MATCH(DMI_PRODUCT_NAME, "MS-1412"),
			DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
			DMI_MATCH(DMI_BOARD_NAME, "MS-1412")
		},
		.callback = dmi_check_cb
485 486 487 488 489 490
	},
	{
		.ident = "Medion MD96100",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
			DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
491
			DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
492 493
			DMI_MATCH(DMI_CHASSIS_VENDOR,
				  "MICRO-STAR INT'L CO.,LTD")
494 495
		},
		.callback = dmi_check_cb
496
	},
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	{ }
};

static struct dmi_system_id __initdata msi_load_scm_models_dmi_table[] = {
	{
		.ident = "MSI N034",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
				"MICRO-STAR INTERNATIONAL CO., LTD"),
			DMI_MATCH(DMI_PRODUCT_NAME, "MS-N034"),
			DMI_MATCH(DMI_CHASSIS_VENDOR,
			"MICRO-STAR INTERNATIONAL CO., LTD")
		},
		.callback = dmi_check_cb
	},
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	{
		.ident = "MSI N051",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
				"MICRO-STAR INTERNATIONAL CO., LTD"),
			DMI_MATCH(DMI_PRODUCT_NAME, "MS-N051"),
			DMI_MATCH(DMI_CHASSIS_VENDOR,
			"MICRO-STAR INTERNATIONAL CO., LTD")
		},
		.callback = dmi_check_cb
	},
	{
		.ident = "MSI N014",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
				"MICRO-STAR INTERNATIONAL CO., LTD"),
			DMI_MATCH(DMI_PRODUCT_NAME, "MS-N014"),
		},
		.callback = dmi_check_cb
	},
532 533 534 535 536 537 538 539 540
	{
		.ident = "MSI CR620",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
				"Micro-Star International"),
			DMI_MATCH(DMI_PRODUCT_NAME, "CR620"),
		},
		.callback = dmi_check_cb
	},
541 542 543 544 545 546 547 548 549
	{
		.ident = "MSI U270",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
				"Micro-Star International Co., Ltd."),
			DMI_MATCH(DMI_PRODUCT_NAME, "U270 series"),
		},
		.callback = dmi_check_cb
	},
550 551 552
	{ }
};

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
static int rfkill_bluetooth_set(void *data, bool blocked)
{
	/* Do something with blocked...*/
	/*
	 * blocked == false is on
	 * blocked == true is off
	 */
	if (blocked)
		set_device_state("0", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
	else
		set_device_state("1", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);

	return 0;
}

static int rfkill_wlan_set(void *data, bool blocked)
{
	if (blocked)
		set_device_state("0", 0, MSI_STANDARD_EC_WLAN_MASK);
	else
		set_device_state("1", 0, MSI_STANDARD_EC_WLAN_MASK);

	return 0;
}

static int rfkill_threeg_set(void *data, bool blocked)
{
	if (blocked)
		set_device_state("0", 0, MSI_STANDARD_EC_3G_MASK);
	else
		set_device_state("1", 0, MSI_STANDARD_EC_3G_MASK);

	return 0;
}

588
static const struct rfkill_ops rfkill_bluetooth_ops = {
589 590 591
	.set_block = rfkill_bluetooth_set
};

592
static const struct rfkill_ops rfkill_wlan_ops = {
593 594 595
	.set_block = rfkill_wlan_set
};

596
static const struct rfkill_ops rfkill_threeg_ops = {
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
	.set_block = rfkill_threeg_set
};

static void rfkill_cleanup(void)
{
	if (rfk_bluetooth) {
		rfkill_unregister(rfk_bluetooth);
		rfkill_destroy(rfk_bluetooth);
	}

	if (rfk_threeg) {
		rfkill_unregister(rfk_threeg);
		rfkill_destroy(rfk_threeg);
	}

	if (rfk_wlan) {
		rfkill_unregister(rfk_wlan);
		rfkill_destroy(rfk_wlan);
	}
}

618 619 620 621 622 623 624 625 626 627 628 629 630
static void msi_update_rfkill(struct work_struct *ignored)
{
	get_wireless_state_ec_standard();

	if (rfk_wlan)
		rfkill_set_sw_state(rfk_wlan, !wlan_s);
	if (rfk_bluetooth)
		rfkill_set_sw_state(rfk_bluetooth, !bluetooth_s);
	if (rfk_threeg)
		rfkill_set_sw_state(rfk_threeg, !threeg_s);
}
static DECLARE_DELAYED_WORK(msi_rfkill_work, msi_update_rfkill);

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
static void msi_send_touchpad_key(struct work_struct *ignored)
{
	u8 rdata;
	int result;

	result = ec_read(MSI_STANDARD_EC_TOUCHPAD_ADDRESS, &rdata);
	if (result < 0)
		return;

	sparse_keymap_report_event(msi_laptop_input_dev,
		(rdata & MSI_STANDARD_EC_TOUCHPAD_MASK) ?
		KEY_TOUCHPAD_ON : KEY_TOUCHPAD_OFF, 1, true);
}
static DECLARE_DELAYED_WORK(msi_touchpad_work, msi_send_touchpad_key);

646 647 648 649 650 651 652 653
static bool msi_laptop_i8042_filter(unsigned char data, unsigned char str,
				struct serio *port)
{
	static bool extended;

	if (str & 0x20)
		return false;

654
	/* 0x54 wwan, 0x62 bluetooth, 0x76 wlan, 0xE4 touchpad toggle*/
655 656 657 658
	if (unlikely(data == 0xe0)) {
		extended = true;
		return false;
	} else if (unlikely(extended)) {
659
		extended = false;
660
		switch (data) {
661 662 663 664
		case 0xE4:
			schedule_delayed_work(&msi_touchpad_work,
				round_jiffies_relative(0.5 * HZ));
			break;
665 666 667 668 669 670 671 672 673 674 675 676
		case 0x54:
		case 0x62:
		case 0x76:
			schedule_delayed_work(&msi_rfkill_work,
				round_jiffies_relative(0.5 * HZ));
			break;
		}
	}

	return false;
}

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
static void msi_init_rfkill(struct work_struct *ignored)
{
	if (rfk_wlan) {
		rfkill_set_sw_state(rfk_wlan, !wlan_s);
		rfkill_wlan_set(NULL, !wlan_s);
	}
	if (rfk_bluetooth) {
		rfkill_set_sw_state(rfk_bluetooth, !bluetooth_s);
		rfkill_bluetooth_set(NULL, !bluetooth_s);
	}
	if (rfk_threeg) {
		rfkill_set_sw_state(rfk_threeg, !threeg_s);
		rfkill_threeg_set(NULL, !threeg_s);
	}
}
static DECLARE_DELAYED_WORK(msi_rfkill_init, msi_init_rfkill);

694 695 696 697 698
static int rfkill_init(struct platform_device *sdev)
{
	/* add rfkill */
	int retval;

699 700 701
	/* keep the hardware wireless state */
	get_wireless_state_ec_standard();

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	rfk_bluetooth = rfkill_alloc("msi-bluetooth", &sdev->dev,
				RFKILL_TYPE_BLUETOOTH,
				&rfkill_bluetooth_ops, NULL);
	if (!rfk_bluetooth) {
		retval = -ENOMEM;
		goto err_bluetooth;
	}
	retval = rfkill_register(rfk_bluetooth);
	if (retval)
		goto err_bluetooth;

	rfk_wlan = rfkill_alloc("msi-wlan", &sdev->dev, RFKILL_TYPE_WLAN,
				&rfkill_wlan_ops, NULL);
	if (!rfk_wlan) {
		retval = -ENOMEM;
		goto err_wlan;
	}
	retval = rfkill_register(rfk_wlan);
	if (retval)
		goto err_wlan;

723 724 725 726 727 728 729 730 731 732
	if (threeg_exists) {
		rfk_threeg = rfkill_alloc("msi-threeg", &sdev->dev,
				RFKILL_TYPE_WWAN, &rfkill_threeg_ops, NULL);
		if (!rfk_threeg) {
			retval = -ENOMEM;
			goto err_threeg;
		}
		retval = rfkill_register(rfk_threeg);
		if (retval)
			goto err_threeg;
733 734
	}

735 736 737 738
	/* schedule to run rfkill state initial */
	schedule_delayed_work(&msi_rfkill_init,
				round_jiffies_relative(1 * HZ));

739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
	return 0;

err_threeg:
	rfkill_destroy(rfk_threeg);
	if (rfk_wlan)
		rfkill_unregister(rfk_wlan);
err_wlan:
	rfkill_destroy(rfk_wlan);
	if (rfk_bluetooth)
		rfkill_unregister(rfk_bluetooth);
err_bluetooth:
	rfkill_destroy(rfk_bluetooth);

	return retval;
}

755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
static int msi_laptop_resume(struct platform_device *device)
{
	u8 data;
	int result;

	if (!load_scm_model)
		return 0;

	/* set load SCM to disable hardware control by fn key */
	result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
	if (result < 0)
		return result;

	result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
		data | MSI_STANDARD_EC_SCM_LOAD_MASK);
	if (result < 0)
		return result;

	return 0;
}

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
static int __init msi_laptop_input_setup(void)
{
	int err;

	msi_laptop_input_dev = input_allocate_device();
	if (!msi_laptop_input_dev)
		return -ENOMEM;

	msi_laptop_input_dev->name = "MSI Laptop hotkeys";
	msi_laptop_input_dev->phys = "msi-laptop/input0";
	msi_laptop_input_dev->id.bustype = BUS_HOST;

	err = sparse_keymap_setup(msi_laptop_input_dev,
		msi_laptop_keymap, NULL);
	if (err)
		goto err_free_dev;

	err = input_register_device(msi_laptop_input_dev);
	if (err)
		goto err_free_keymap;

	return 0;

err_free_keymap:
	sparse_keymap_free(msi_laptop_input_dev);
err_free_dev:
	input_free_device(msi_laptop_input_dev);
	return err;
}

static void msi_laptop_input_destroy(void)
{
	sparse_keymap_free(msi_laptop_input_dev);
	input_unregister_device(msi_laptop_input_dev);
}

812
static int __init load_scm_model_init(struct platform_device *sdev)
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
{
	u8 data;
	int result;

	/* allow userland write sysfs file  */
	dev_attr_bluetooth.store = store_bluetooth;
	dev_attr_wlan.store = store_wlan;
	dev_attr_threeg.store = store_threeg;
	dev_attr_bluetooth.attr.mode |= S_IWUSR;
	dev_attr_wlan.attr.mode |= S_IWUSR;
	dev_attr_threeg.attr.mode |= S_IWUSR;

	/* disable hardware control by fn key */
	result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
	if (result < 0)
		return result;

	result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
		data | MSI_STANDARD_EC_SCM_LOAD_MASK);
	if (result < 0)
		return result;

	/* initial rfkill */
	result = rfkill_init(sdev);
	if (result < 0)
838 839
		goto fail_rfkill;

840 841 842 843 844
	/* setup input device */
	result = msi_laptop_input_setup();
	if (result)
		goto fail_input;

845 846
	result = i8042_install_filter(msi_laptop_i8042_filter);
	if (result) {
847
		pr_err("Unable to install key filter\n");
848 849
		goto fail_filter;
	}
850 851

	return 0;
852 853

fail_filter:
854 855 856
	msi_laptop_input_destroy();

fail_input:
857 858 859 860 861 862
	rfkill_cleanup();

fail_rfkill:

	return result;

863 864
}

865 866 867 868 869 870 871
static int __init msi_init(void)
{
	int ret;

	if (acpi_disabled)
		return -ENODEV;

872 873
	if (force || dmi_check_system(msi_dmi_table))
		old_ec_model = 1;
874

875 876 877
	if (!old_ec_model)
		get_threeg_exists();

878 879 880
	if (!old_ec_model && dmi_check_system(msi_load_scm_models_dmi_table))
		load_scm_model = 1;

881 882 883 884 885
	if (auto_brightness < 0 || auto_brightness > 2)
		return -EINVAL;

	/* Register backlight stuff */

886
	if (acpi_video_backlight_support()) {
J
Joe Perches 已提交
887
		pr_info("Brightness ignored, must be controlled by ACPI video driver\n");
888
	} else {
889 890
		struct backlight_properties props;
		memset(&props, 0, sizeof(struct backlight_properties));
M
Matthew Garrett 已提交
891
		props.type = BACKLIGHT_PLATFORM;
892
		props.max_brightness = MSI_LCD_LEVEL_MAX - 1;
893
		msibl_device = backlight_device_register("msi-laptop-bl", NULL,
894 895
							 NULL, &msibl_ops,
							 &props);
896 897 898
		if (IS_ERR(msibl_device))
			return PTR_ERR(msibl_device);
	}
899

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
	ret = platform_driver_register(&msipf_driver);
	if (ret)
		goto fail_backlight;

	/* Register platform stuff */

	msipf_device = platform_device_alloc("msi-laptop-pf", -1);
	if (!msipf_device) {
		ret = -ENOMEM;
		goto fail_platform_driver;
	}

	ret = platform_device_add(msipf_device);
	if (ret)
		goto fail_platform_device1;

916 917 918 919 920
	if (load_scm_model && (load_scm_model_init(msipf_device) < 0)) {
		ret = -EINVAL;
		goto fail_platform_device1;
	}

921 922
	ret = sysfs_create_group(&msipf_device->dev.kobj,
				 &msipf_attribute_group);
923 924 925
	if (ret)
		goto fail_platform_device2;

926
	if (!old_ec_model) {
927 928 929
		if (threeg_exists)
			ret = device_create_file(&msipf_device->dev,
						&dev_attr_threeg);
930 931 932 933
		if (ret)
			goto fail_platform_device2;
	}

934 935 936 937 938 939 940
	/* Disable automatic brightness control by default because
	 * this module was probably loaded to do brightness control in
	 * software. */

	if (auto_brightness != 2)
		set_auto_brightness(auto_brightness);

J
Joe Perches 已提交
941
	pr_info("driver " MSI_DRIVER_VERSION " successfully loaded\n");
942 943 944 945 946

	return 0;

fail_platform_device2:

947 948 949 950 951
	if (load_scm_model) {
		i8042_remove_filter(msi_laptop_i8042_filter);
		cancel_delayed_work_sync(&msi_rfkill_work);
		rfkill_cleanup();
	}
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
	platform_device_del(msipf_device);

fail_platform_device1:

	platform_device_put(msipf_device);

fail_platform_driver:

	platform_driver_unregister(&msipf_driver);

fail_backlight:

	backlight_device_unregister(msibl_device);

	return ret;
}

static void __exit msi_cleanup(void)
{
971 972
	if (load_scm_model) {
		i8042_remove_filter(msi_laptop_i8042_filter);
973
		msi_laptop_input_destroy();
974 975 976
		cancel_delayed_work_sync(&msi_rfkill_work);
		rfkill_cleanup();
	}
977 978

	sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
979
	if (!old_ec_model && threeg_exists)
980
		device_remove_file(&msipf_device->dev, &dev_attr_threeg);
981 982 983 984 985 986 987 988
	platform_device_unregister(msipf_device);
	platform_driver_unregister(&msipf_driver);
	backlight_device_unregister(msibl_device);

	/* Enable automatic brightness control again */
	if (auto_brightness != 2)
		set_auto_brightness(1);

J
Joe Perches 已提交
989
	pr_info("driver unloaded\n");
990 991 992 993 994 995 996 997 998
}

module_init(msi_init);
module_exit(msi_cleanup);

MODULE_AUTHOR("Lennart Poettering");
MODULE_DESCRIPTION("MSI Laptop Support");
MODULE_VERSION(MSI_DRIVER_VERSION);
MODULE_LICENSE("GPL");
999 1000 1001 1002 1003

MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
1004
MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");
1005 1006
MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N051:*");
MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N014:*");
1007
MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnCR620:*");
1008
MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnU270series:*");