samsung-laptop.c 43.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Samsung Laptop driver
 *
 * Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de)
 * Copyright (C) 2009,2011 Novell 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.
 *
 */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/backlight.h>
20
#include <linux/leds.h>
21 22 23 24
#include <linux/fb.h>
#include <linux/dmi.h>
#include <linux/platform_device.h>
#include <linux/rfkill.h>
25
#include <linux/acpi.h>
26 27
#include <linux/seq_file.h>
#include <linux/debugfs.h>
28
#include <linux/ctype.h>
29
#include <linux/efi.h>
30
#include <linux/suspend.h>
31
#include <acpi/video.h>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

/*
 * This driver is needed because a number of Samsung laptops do not hook
 * their control settings through ACPI.  So we have to poke around in the
 * BIOS to do things like brightness values, and "special" key controls.
 */

/*
 * We have 0 - 8 as valid brightness levels.  The specs say that level 0 should
 * be reserved by the BIOS (which really doesn't make much sense), we tell
 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
 */
#define MAX_BRIGHT	0x07


#define SABI_IFACE_MAIN			0x00
#define SABI_IFACE_SUB			0x02
#define SABI_IFACE_COMPLETE		0x04
#define SABI_IFACE_DATA			0x05

52 53 54
#define WL_STATUS_WLAN			0x0
#define WL_STATUS_BT			0x2

55 56 57 58 59 60 61 62 63 64 65
/* Structure get/set data using sabi */
struct sabi_data {
	union {
		struct {
			u32 d0;
			u32 d1;
			u16 d2;
			u8  d3;
		};
		u8 data[11];
	};
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
};

struct sabi_header_offsets {
	u8 port;
	u8 re_mem;
	u8 iface_func;
	u8 en_mem;
	u8 data_offset;
	u8 data_segment;
};

struct sabi_commands {
	/*
	 * Brightness is 0 - 8, as described above.
	 * Value 0 is for the BIOS to use
	 */
82 83
	u16 get_brightness;
	u16 set_brightness;
84 85 86 87 88 89 90 91 92 93

	/*
	 * first byte:
	 * 0x00 - wireless is off
	 * 0x01 - wireless is on
	 * second byte:
	 * 0x02 - 3G is off
	 * 0x03 - 3G is on
	 * TODO, verify 3G is correct, that doesn't seem right...
	 */
94 95
	u16 get_wireless_button;
	u16 set_wireless_button;
96 97

	/* 0 is off, 1 is on */
98 99
	u16 get_backlight;
	u16 set_backlight;
100 101 102 103 104

	/*
	 * 0x80 or 0x00 - no action
	 * 0x81 - recovery key pressed
	 */
105 106
	u16 get_recovery_mode;
	u16 set_recovery_mode;
107 108 109 110 111

	/*
	 * on seclinux: 0 is low, 1 is high,
	 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
	 */
112 113
	u16 get_performance_level;
	u16 set_performance_level;
114

115 116 117 118
	/* 0x80 is off, 0x81 is on */
	u16 get_battery_life_extender;
	u16 set_battery_life_extender;

119 120 121 122
	/* 0x80 is off, 0x81 is on */
	u16 get_usb_charge;
	u16 set_usb_charge;

123 124 125 126
	/* the first byte is for bluetooth and the third one is for wlan */
	u16 get_wireless_status;
	u16 set_wireless_status;

127 128 129 130
	/* 0x80 is off, 0x81 is on */
	u16 get_lid_handling;
	u16 set_lid_handling;

131 132 133
	/* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
	u16 kbd_backlight;

134 135 136 137
	/*
	 * Tell the BIOS that Linux is running on this machine.
	 * 81 is on, 80 is off
	 */
138
	u16 set_linux;
139 140 141 142
};

struct sabi_performance_level {
	const char *name;
143
	u16 value;
144 145 146
};

struct sabi_config {
147
	int sabi_version;
148 149 150 151 152 153 154 155 156 157 158
	const char *test_string;
	u16 main_function;
	const struct sabi_header_offsets header_offsets;
	const struct sabi_commands commands;
	const struct sabi_performance_level performance_levels[4];
	u8 min_brightness;
	u8 max_brightness;
};

static const struct sabi_config sabi_configs[] = {
	{
159 160 161 162
		/* I don't know if it is really 2, but it it is
		 * less than 3 anyway */
		.sabi_version = 2,

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		.test_string = "SECLINUX",

		.main_function = 0x4c49,

		.header_offsets = {
			.port = 0x00,
			.re_mem = 0x02,
			.iface_func = 0x03,
			.en_mem = 0x04,
			.data_offset = 0x05,
			.data_segment = 0x07,
		},

		.commands = {
			.get_brightness = 0x00,
			.set_brightness = 0x01,

			.get_wireless_button = 0x02,
			.set_wireless_button = 0x03,

			.get_backlight = 0x04,
			.set_backlight = 0x05,

			.get_recovery_mode = 0x06,
			.set_recovery_mode = 0x07,

			.get_performance_level = 0x08,
			.set_performance_level = 0x09,

192 193 194
			.get_battery_life_extender = 0xFFFF,
			.set_battery_life_extender = 0xFFFF,

195 196 197
			.get_usb_charge = 0xFFFF,
			.set_usb_charge = 0xFFFF,

198 199 200
			.get_wireless_status = 0xFFFF,
			.set_wireless_status = 0xFFFF,

201 202 203
			.get_lid_handling = 0xFFFF,
			.set_lid_handling = 0xFFFF,

204 205
			.kbd_backlight = 0xFFFF,

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
			.set_linux = 0x0a,
		},

		.performance_levels = {
			{
				.name = "silent",
				.value = 0,
			},
			{
				.name = "normal",
				.value = 1,
			},
			{ },
		},
		.min_brightness = 1,
		.max_brightness = 8,
	},
	{
224 225
		.sabi_version = 3,

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
		.test_string = "SwSmi@",

		.main_function = 0x5843,

		.header_offsets = {
			.port = 0x00,
			.re_mem = 0x04,
			.iface_func = 0x02,
			.en_mem = 0x03,
			.data_offset = 0x05,
			.data_segment = 0x07,
		},

		.commands = {
			.get_brightness = 0x10,
			.set_brightness = 0x11,

			.get_wireless_button = 0x12,
			.set_wireless_button = 0x13,

			.get_backlight = 0x2d,
			.set_backlight = 0x2e,

			.get_recovery_mode = 0xff,
			.set_recovery_mode = 0xff,

			.get_performance_level = 0x31,
			.set_performance_level = 0x32,

255 256 257
			.get_battery_life_extender = 0x65,
			.set_battery_life_extender = 0x66,

258 259 260
			.get_usb_charge = 0x67,
			.set_usb_charge = 0x68,

261 262 263
			.get_wireless_status = 0x69,
			.set_wireless_status = 0x6a,

264 265 266
			.get_lid_handling = 0x6d,
			.set_lid_handling = 0x6e,

267 268
			.kbd_backlight = 0x78,

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
			.set_linux = 0xff,
		},

		.performance_levels = {
			{
				.name = "normal",
				.value = 0,
			},
			{
				.name = "silent",
				.value = 1,
			},
			{
				.name = "overclock",
				.value = 2,
			},
			{ },
		},
		.min_brightness = 0,
		.max_brightness = 8,
	},
	{ },
};

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
/*
 * samsung-laptop/    - debugfs root directory
 *   f0000_segment    - dump f0000 segment
 *   command          - current command
 *   data             - current data
 *   d0, d1, d2, d3   - data fields
 *   call             - call SABI using command and data
 *
 * This allow to call arbitrary sabi commands wihout
 * modifying the driver at all.
 * For example, setting the keyboard backlight brightness to 5
 *
 *  echo 0x78 > command
 *  echo 0x0582 > d0
 *  echo 0 > d1
 *  echo 0 > d2
 *  echo 0 > d3
 *  cat call
 */

struct samsung_laptop_debug {
	struct dentry *root;
	struct sabi_data data;
	u16 command;

	struct debugfs_blob_wrapper f0000_wrapper;
	struct debugfs_blob_wrapper data_wrapper;
320
	struct debugfs_blob_wrapper sdiag_wrapper;
321 322
};

323 324 325 326 327 328 329 330
struct samsung_laptop;

struct samsung_rfkill {
	struct samsung_laptop *samsung;
	struct rfkill *rfkill;
	enum rfkill_type type;
};

331 332
struct samsung_laptop {
	const struct sabi_config *config;
333

334 335 336 337 338 339
	void __iomem *sabi;
	void __iomem *sabi_iface;
	void __iomem *f0000_segment;

	struct mutex sabi_mutex;

340
	struct platform_device *platform_device;
341
	struct backlight_device *backlight_device;
342 343 344

	struct samsung_rfkill wlan;
	struct samsung_rfkill bluetooth;
345

346 347 348 349 350
	struct led_classdev kbd_led;
	int kbd_led_wk;
	struct workqueue_struct *led_workqueue;
	struct work_struct kbd_led_work;

351
	struct samsung_laptop_debug debug;
352
	struct samsung_quirks *quirks;
353

354 355
	struct notifier_block pm_nb;

356
	bool handle_backlight;
357
	bool has_stepping_quirk;
358 359

	char sdiag[64];
360 361
};

362 363
struct samsung_quirks {
	bool broken_acpi_video;
364 365
	bool four_kbd_backlight_levels;
	bool enable_kbd_backlight;
366
	bool use_native_backlight;
367
	bool lid_handling;
368 369 370
};

static struct samsung_quirks samsung_unknown = {};
371

372 373 374
static struct samsung_quirks samsung_broken_acpi_video = {
	.broken_acpi_video = true,
};
375

376 377 378 379
static struct samsung_quirks samsung_use_native_backlight = {
	.use_native_backlight = true,
};

380 381 382 383 384
static struct samsung_quirks samsung_np740u3e = {
	.four_kbd_backlight_levels = true,
	.enable_kbd_backlight = true,
};

385 386 387 388
static struct samsung_quirks samsung_lid_handling = {
	.lid_handling = true,
};

389
static bool force;
390 391 392 393
module_param(force, bool, 0);
MODULE_PARM_DESC(force,
		"Disable the DMI check and forces the driver to be loaded");

394
static bool debug;
395 396 397
module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");

398 399 400
static int sabi_command(struct samsung_laptop *samsung, u16 command,
			struct sabi_data *in,
			struct sabi_data *out)
401
{
402
	const struct sabi_config *config = samsung->config;
403
	int ret = 0;
404
	u16 port = readw(samsung->sabi + config->header_offsets.port);
405 406
	u8 complete, iface_data;

407
	mutex_lock(&samsung->sabi_mutex);
408

409 410
	if (debug) {
		if (in)
C
Corentin Chary 已提交
411 412
			pr_info("SABI command:0x%04x "
				"data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
413 414
				command, in->d0, in->d1, in->d2, in->d3);
		else
C
Corentin Chary 已提交
415
			pr_info("SABI command:0x%04x", command);
416 417
	}

418
	/* enable memory to be able to write to it */
419
	outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
420 421

	/* write out the command */
422 423 424
	writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
	writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
	writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
425 426 427 428 429 430
	if (in) {
		writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
		writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
		writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
		writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
	}
431
	outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
432 433

	/* write protect memory to make it safe */
434
	outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
435 436

	/* see if the command actually succeeded */
437 438
	complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
	iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
C
Corentin Chary 已提交
439 440 441 442 443 444

	/* iface_data = 0xFF happens when a command is not known
	 * so we only add a warning in debug mode since we will
	 * probably issue some unknown command at startup to find
	 * out which features are supported */
	if (complete != 0xaa || (iface_data == 0xff && debug))
445 446 447
		pr_warn("SABI command 0x%04x failed with"
			" completion flag 0x%02x and interface data 0x%02x",
			command, complete, iface_data);
C
Corentin Chary 已提交
448 449

	if (complete != 0xaa || iface_data == 0xff) {
450
		ret = -EINVAL;
451 452
		goto exit;
	}
453 454 455 456 457 458 459 460 461

	if (out) {
		out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
		out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
		out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
		out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
	}

	if (debug && out) {
C
Corentin Chary 已提交
462
		pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
463 464
			out->d0, out->d1, out->d2, out->d3);
	}
465 466

exit:
467
	mutex_unlock(&samsung->sabi_mutex);
468
	return ret;
469 470
}

471 472 473
/* simple wrappers usable with most commands */
static int sabi_set_commandb(struct samsung_laptop *samsung,
			     u16 command, u8 data)
474
{
475
	struct sabi_data in = { { { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 } } };
476

477 478
	in.data[0] = data;
	return sabi_command(samsung, command, &in, NULL);
479 480
}

481
static int read_brightness(struct samsung_laptop *samsung)
482
{
483 484
	const struct sabi_config *config = samsung->config;
	const struct sabi_commands *commands = &samsung->config->commands;
485
	struct sabi_data sretval;
486 487 488
	int user_brightness = 0;
	int retval;

489 490 491 492 493 494 495 496 497 498 499
	retval = sabi_command(samsung, commands->get_brightness,
			      NULL, &sretval);
	if (retval)
		return retval;

	user_brightness = sretval.data[0];
	if (user_brightness > config->min_brightness)
		user_brightness -= config->min_brightness;
	else
		user_brightness = 0;

500 501 502
	return user_brightness;
}

503
static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
504
{
505 506 507
	const struct sabi_config *config = samsung->config;
	const struct sabi_commands *commands = &samsung->config->commands;
	u8 user_level = user_brightness + config->min_brightness;
508

509
	if (samsung->has_stepping_quirk && user_level != 0) {
510 511 512 513
		/*
		 * short circuit if the specified level is what's already set
		 * to prevent the screen from flickering needlessly
		 */
514
		if (user_brightness == read_brightness(samsung))
515 516
			return;

517
		sabi_set_commandb(samsung, commands->set_brightness, 0);
518 519
	}

520
	sabi_set_commandb(samsung, commands->set_brightness, user_level);
521 522 523 524
}

static int get_brightness(struct backlight_device *bd)
{
525 526 527
	struct samsung_laptop *samsung = bl_get_data(bd);

	return read_brightness(samsung);
528 529
}

530
static void check_for_stepping_quirk(struct samsung_laptop *samsung)
531
{
532 533 534
	int initial_level;
	int check_level;
	int orig_level = read_brightness(samsung);
535 536 537 538 539 540 541 542

	/*
	 * Some laptops exhibit the strange behaviour of stepping toward
	 * (rather than setting) the brightness except when changing to/from
	 * brightness level 0. This behaviour is checked for here and worked
	 * around in set_brightness.
	 */

543
	if (orig_level == 0)
544
		set_brightness(samsung, 1);
545

546
	initial_level = read_brightness(samsung);
547

548 549 550 551 552
	if (initial_level <= 2)
		check_level = initial_level + 2;
	else
		check_level = initial_level - 2;

553
	samsung->has_stepping_quirk = false;
554
	set_brightness(samsung, check_level);
555

556
	if (read_brightness(samsung) != check_level) {
557
		samsung->has_stepping_quirk = true;
558 559 560
		pr_info("enabled workaround for brightness stepping quirk\n");
	}

561
	set_brightness(samsung, orig_level);
562 563
}

564 565
static int update_status(struct backlight_device *bd)
{
566
	struct samsung_laptop *samsung = bl_get_data(bd);
567 568
	const struct sabi_commands *commands = &samsung->config->commands;

569
	set_brightness(samsung, bd->props.brightness);
570 571

	if (bd->props.power == FB_BLANK_UNBLANK)
572
		sabi_set_commandb(samsung, commands->set_backlight, 1);
573
	else
574
		sabi_set_commandb(samsung, commands->set_backlight, 0);
575

576 577 578 579 580 581 582 583
	return 0;
}

static const struct backlight_ops backlight_ops = {
	.get_brightness	= get_brightness,
	.update_status	= update_status,
};

584
static int seclinux_rfkill_set(void *data, bool blocked)
585
{
586 587
	struct samsung_rfkill *srfkill = data;
	struct samsung_laptop *samsung = srfkill->samsung;
588 589
	const struct sabi_commands *commands = &samsung->config->commands;

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
	return sabi_set_commandb(samsung, commands->set_wireless_button,
				 !blocked);
}

static struct rfkill_ops seclinux_rfkill_ops = {
	.set_block = seclinux_rfkill_set,
};

static int swsmi_wireless_status(struct samsung_laptop *samsung,
				 struct sabi_data *data)
{
	const struct sabi_commands *commands = &samsung->config->commands;

	return sabi_command(samsung, commands->get_wireless_status,
			    NULL, data);
}

static int swsmi_rfkill_set(void *priv, bool blocked)
{
	struct samsung_rfkill *srfkill = priv;
	struct samsung_laptop *samsung = srfkill->samsung;
	const struct sabi_commands *commands = &samsung->config->commands;
	struct sabi_data data;
	int ret, i;

	ret = swsmi_wireless_status(samsung, &data);
	if (ret)
		return ret;

	/* Don't set the state for non-present devices */
	for (i = 0; i < 4; i++)
		if (data.data[i] == 0x02)
			data.data[1] = 0;

	if (srfkill->type == RFKILL_TYPE_WLAN)
		data.data[WL_STATUS_WLAN] = !blocked;
	else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
		data.data[WL_STATUS_BT] = !blocked;

	return sabi_command(samsung, commands->set_wireless_status,
			    &data, &data);
}

static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
{
	struct samsung_rfkill *srfkill = priv;
	struct samsung_laptop *samsung = srfkill->samsung;
	struct sabi_data data;
	int ret;

	ret = swsmi_wireless_status(samsung, &data);
	if (ret)
		return ;

	if (srfkill->type == RFKILL_TYPE_WLAN)
		ret = data.data[WL_STATUS_WLAN];
	else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
		ret = data.data[WL_STATUS_BT];
648
	else
649
		return ;
650

651
	rfkill_set_sw_state(rfkill, !ret);
652 653
}

654 655 656
static struct rfkill_ops swsmi_rfkill_ops = {
	.set_block = swsmi_rfkill_set,
	.query = swsmi_rfkill_query,
657 658 659 660 661
};

static ssize_t get_performance_level(struct device *dev,
				     struct device_attribute *attr, char *buf)
{
662
	struct samsung_laptop *samsung = dev_get_drvdata(dev);
663
	const struct sabi_config *config = samsung->config;
664
	const struct sabi_commands *commands = &config->commands;
665
	struct sabi_data sretval;
666 667 668 669
	int retval;
	int i;

	/* Read the state */
670 671
	retval = sabi_command(samsung, commands->get_performance_level,
			      NULL, &sretval);
672 673 674 675
	if (retval)
		return retval;

	/* The logic is backwards, yeah, lots of fun... */
676
	for (i = 0; config->performance_levels[i].name; ++i) {
677
		if (sretval.data[0] == config->performance_levels[i].value)
678
			return sprintf(buf, "%s\n", config->performance_levels[i].name);
679 680 681 682 683 684 685 686
	}
	return sprintf(buf, "%s\n", "unknown");
}

static ssize_t set_performance_level(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t count)
{
687
	struct samsung_laptop *samsung = dev_get_drvdata(dev);
688
	const struct sabi_config *config = samsung->config;
689 690
	const struct sabi_commands *commands = &config->commands;
	int i;
691

692 693 694 695 696 697 698
	if (count < 1)
		return count;

	for (i = 0; config->performance_levels[i].name; ++i) {
		const struct sabi_performance_level *level =
			&config->performance_levels[i];
		if (!strncasecmp(level->name, buf, strlen(level->name))) {
699 700 701
			sabi_set_commandb(samsung,
					  commands->set_performance_level,
					  level->value);
702
			break;
703 704
		}
	}
705 706 707 708

	if (!config->performance_levels[i].name)
		return -EINVAL;

709 710
	return count;
}
711

712 713 714
static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
		   get_performance_level, set_performance_level);

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
static int read_battery_life_extender(struct samsung_laptop *samsung)
{
	const struct sabi_commands *commands = &samsung->config->commands;
	struct sabi_data data;
	int retval;

	if (commands->get_battery_life_extender == 0xFFFF)
		return -ENODEV;

	memset(&data, 0, sizeof(data));
	data.data[0] = 0x80;
	retval = sabi_command(samsung, commands->get_battery_life_extender,
			      &data, &data);

	if (retval)
		return retval;

	if (data.data[0] != 0 && data.data[0] != 1)
		return -ENODEV;

	return data.data[0];
}

static int write_battery_life_extender(struct samsung_laptop *samsung,
				       int enabled)
{
	const struct sabi_commands *commands = &samsung->config->commands;
	struct sabi_data data;

	memset(&data, 0, sizeof(data));
	data.data[0] = 0x80 | enabled;
	return sabi_command(samsung, commands->set_battery_life_extender,
			    &data, NULL);
}

static ssize_t get_battery_life_extender(struct device *dev,
					 struct device_attribute *attr,
					 char *buf)
{
	struct samsung_laptop *samsung = dev_get_drvdata(dev);
	int ret;

	ret = read_battery_life_extender(samsung);
	if (ret < 0)
		return ret;

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

static ssize_t set_battery_life_extender(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	struct samsung_laptop *samsung = dev_get_drvdata(dev);
	int ret, value;

771
	if (!count || kstrtoint(buf, 0, &value) != 0)
772 773 774 775 776 777 778 779 780 781 782 783
		return -EINVAL;

	ret = write_battery_life_extender(samsung, !!value);
	if (ret < 0)
		return ret;

	return count;
}

static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
		   get_battery_life_extender, set_battery_life_extender);

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 812 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 838 839
static int read_usb_charge(struct samsung_laptop *samsung)
{
	const struct sabi_commands *commands = &samsung->config->commands;
	struct sabi_data data;
	int retval;

	if (commands->get_usb_charge == 0xFFFF)
		return -ENODEV;

	memset(&data, 0, sizeof(data));
	data.data[0] = 0x80;
	retval = sabi_command(samsung, commands->get_usb_charge,
			      &data, &data);

	if (retval)
		return retval;

	if (data.data[0] != 0 && data.data[0] != 1)
		return -ENODEV;

	return data.data[0];
}

static int write_usb_charge(struct samsung_laptop *samsung,
			    int enabled)
{
	const struct sabi_commands *commands = &samsung->config->commands;
	struct sabi_data data;

	memset(&data, 0, sizeof(data));
	data.data[0] = 0x80 | enabled;
	return sabi_command(samsung, commands->set_usb_charge,
			    &data, NULL);
}

static ssize_t get_usb_charge(struct device *dev,
			      struct device_attribute *attr,
			      char *buf)
{
	struct samsung_laptop *samsung = dev_get_drvdata(dev);
	int ret;

	ret = read_usb_charge(samsung);
	if (ret < 0)
		return ret;

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

static ssize_t set_usb_charge(struct device *dev,
			      struct device_attribute *attr,
			      const char *buf, size_t count)
{
	struct samsung_laptop *samsung = dev_get_drvdata(dev);
	int ret, value;

840
	if (!count || kstrtoint(buf, 0, &value) != 0)
841 842 843 844 845 846 847 848 849 850 851 852
		return -EINVAL;

	ret = write_usb_charge(samsung, !!value);
	if (ret < 0)
		return ret;

	return count;
}

static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
		   get_usb_charge, set_usb_charge);

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
static int read_lid_handling(struct samsung_laptop *samsung)
{
	const struct sabi_commands *commands = &samsung->config->commands;
	struct sabi_data data;
	int retval;

	if (commands->get_lid_handling == 0xFFFF)
		return -ENODEV;

	memset(&data, 0, sizeof(data));
	retval = sabi_command(samsung, commands->get_lid_handling,
			      &data, &data);

	if (retval)
		return retval;

	return data.data[0] & 0x1;
}

static int write_lid_handling(struct samsung_laptop *samsung,
			      int enabled)
{
	const struct sabi_commands *commands = &samsung->config->commands;
	struct sabi_data data;

	memset(&data, 0, sizeof(data));
	data.data[0] = 0x80 | enabled;
	return sabi_command(samsung, commands->set_lid_handling,
			    &data, NULL);
}

static ssize_t get_lid_handling(struct device *dev,
				struct device_attribute *attr,
				char *buf)
{
	struct samsung_laptop *samsung = dev_get_drvdata(dev);
	int ret;

	ret = read_lid_handling(samsung);
	if (ret < 0)
		return ret;

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

static ssize_t set_lid_handling(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct samsung_laptop *samsung = dev_get_drvdata(dev);
	int ret, value;

	if (!count || kstrtoint(buf, 0, &value) != 0)
		return -EINVAL;

	ret = write_lid_handling(samsung, !!value);
	if (ret < 0)
		return ret;

	return count;
}

static DEVICE_ATTR(lid_handling, S_IWUSR | S_IRUGO,
		   get_lid_handling, set_lid_handling);

918 919
static struct attribute *platform_attributes[] = {
	&dev_attr_performance_level.attr,
920
	&dev_attr_battery_life_extender.attr,
921
	&dev_attr_usb_charge.attr,
922
	&dev_attr_lid_handling.attr,
923 924
	NULL
};
925

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
static int find_signature(void __iomem *memcheck, const char *testStr)
{
	int i = 0;
	int loca;

	for (loca = 0; loca < 0xffff; loca++) {
		char temp = readb(memcheck + loca);

		if (temp == testStr[i]) {
			if (i == strlen(testStr)-1)
				break;
			++i;
		} else {
			i = 0;
		}
	}
	return loca;
}

static void samsung_rfkill_exit(struct samsung_laptop *samsung)
{
947 948 949 950 951 952 953 954 955
	if (samsung->wlan.rfkill) {
		rfkill_unregister(samsung->wlan.rfkill);
		rfkill_destroy(samsung->wlan.rfkill);
		samsung->wlan.rfkill = NULL;
	}
	if (samsung->bluetooth.rfkill) {
		rfkill_unregister(samsung->bluetooth.rfkill);
		rfkill_destroy(samsung->bluetooth.rfkill);
		samsung->bluetooth.rfkill = NULL;
956 957 958
	}
}

959 960 961 962 963
static int samsung_new_rfkill(struct samsung_laptop *samsung,
			      struct samsung_rfkill *arfkill,
			      const char *name, enum rfkill_type type,
			      const struct rfkill_ops *ops,
			      int blocked)
964
{
965 966
	struct rfkill **rfkill = &arfkill->rfkill;
	int ret;
967

968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
	arfkill->type = type;
	arfkill->samsung = samsung;

	*rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
			       type, ops, arfkill);

	if (!*rfkill)
		return -EINVAL;

	if (blocked != -1)
		rfkill_init_sw_state(*rfkill, blocked);

	ret = rfkill_register(*rfkill);
	if (ret) {
		rfkill_destroy(*rfkill);
		*rfkill = NULL;
		return ret;
985
	}
986 987
	return 0;
}
988

989 990 991 992 993 994 995 996 997 998 999 1000
static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
{
	return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
				  RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
}

static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
{
	struct sabi_data data;
	int ret;

	ret = swsmi_wireless_status(samsung, &data);
1001 1002 1003 1004 1005
	if (ret) {
		/* Some swsmi laptops use the old seclinux way to control
		 * wireless devices */
		if (ret == -EINVAL)
			ret = samsung_rfkill_init_seclinux(samsung);
1006
		return ret;
1007
	}
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041

	/* 0x02 seems to mean that the device is no present/available */

	if (data.data[WL_STATUS_WLAN] != 0x02)
		ret = samsung_new_rfkill(samsung, &samsung->wlan,
					 "samsung-wlan",
					 RFKILL_TYPE_WLAN,
					 &swsmi_rfkill_ops,
					 !data.data[WL_STATUS_WLAN]);
	if (ret)
		goto exit;

	if (data.data[WL_STATUS_BT] != 0x02)
		ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
					 "samsung-bluetooth",
					 RFKILL_TYPE_BLUETOOTH,
					 &swsmi_rfkill_ops,
					 !data.data[WL_STATUS_BT]);
	if (ret)
		goto exit;

exit:
	if (ret)
		samsung_rfkill_exit(samsung);

	return ret;
}

static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
{
	if (samsung->config->sabi_version == 2)
		return samsung_rfkill_init_seclinux(samsung);
	if (samsung->config->sabi_version == 3)
		return samsung_rfkill_init_swsmi(samsung);
1042 1043 1044
	return 0;
}

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
static void samsung_lid_handling_exit(struct samsung_laptop *samsung)
{
	if (samsung->quirks->lid_handling)
		write_lid_handling(samsung, 0);
}

static int __init samsung_lid_handling_init(struct samsung_laptop *samsung)
{
	int retval = 0;

	if (samsung->quirks->lid_handling)
		retval = write_lid_handling(samsung, 1);

	return retval;
}

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
static int kbd_backlight_enable(struct samsung_laptop *samsung)
{
	const struct sabi_commands *commands = &samsung->config->commands;
	struct sabi_data data;
	int retval;

	if (commands->kbd_backlight == 0xFFFF)
		return -ENODEV;

	memset(&data, 0, sizeof(data));
	data.d0 = 0xaabb;
	retval = sabi_command(samsung, commands->kbd_backlight,
			      &data, &data);

	if (retval)
		return retval;

	if (data.d0 != 0xccdd)
		return -ENODEV;
	return 0;
}

static int kbd_backlight_read(struct samsung_laptop *samsung)
{
	const struct sabi_commands *commands = &samsung->config->commands;
	struct sabi_data data;
	int retval;

	memset(&data, 0, sizeof(data));
	data.data[0] = 0x81;
	retval = sabi_command(samsung, commands->kbd_backlight,
			      &data, &data);

	if (retval)
		return retval;

	return data.data[0];
}

static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
{
	const struct sabi_commands *commands = &samsung->config->commands;
	struct sabi_data data;

	memset(&data, 0, sizeof(data));
	data.d0 = 0x82 | ((brightness & 0xFF) << 8);
	return sabi_command(samsung, commands->kbd_backlight,
			    &data, NULL);
}

static void kbd_led_update(struct work_struct *work)
{
	struct samsung_laptop *samsung;

	samsung = container_of(work, struct samsung_laptop, kbd_led_work);
	kbd_backlight_write(samsung, samsung->kbd_led_wk);
}

static void kbd_led_set(struct led_classdev *led_cdev,
			enum led_brightness value)
{
	struct samsung_laptop *samsung;

	samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);

	if (value > samsung->kbd_led.max_brightness)
		value = samsung->kbd_led.max_brightness;
	else if (value < 0)
		value = 0;

	samsung->kbd_led_wk = value;
	queue_work(samsung->led_workqueue, &samsung->kbd_led_work);
}

static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
{
	struct samsung_laptop *samsung;

	samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
	return kbd_backlight_read(samsung);
}

static void samsung_leds_exit(struct samsung_laptop *samsung)
{
	if (!IS_ERR_OR_NULL(samsung->kbd_led.dev))
		led_classdev_unregister(&samsung->kbd_led);
	if (samsung->led_workqueue)
		destroy_workqueue(samsung->led_workqueue);
}

static int __init samsung_leds_init(struct samsung_laptop *samsung)
{
	int ret = 0;

	samsung->led_workqueue = create_singlethread_workqueue("led_workqueue");
	if (!samsung->led_workqueue)
		return -ENOMEM;

	if (kbd_backlight_enable(samsung) >= 0) {
		INIT_WORK(&samsung->kbd_led_work, kbd_led_update);

		samsung->kbd_led.name = "samsung::kbd_backlight";
		samsung->kbd_led.brightness_set = kbd_led_set;
		samsung->kbd_led.brightness_get = kbd_led_get;
		samsung->kbd_led.max_brightness = 8;
1166 1167
		if (samsung->quirks->four_kbd_backlight_levels)
			samsung->kbd_led.max_brightness = 4;
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178

		ret = led_classdev_register(&samsung->platform_device->dev,
					   &samsung->kbd_led);
	}

	if (ret)
		samsung_leds_exit(samsung);

	return ret;
}

1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
static void samsung_backlight_exit(struct samsung_laptop *samsung)
{
	if (samsung->backlight_device) {
		backlight_device_unregister(samsung->backlight_device);
		samsung->backlight_device = NULL;
	}
}

static int __init samsung_backlight_init(struct samsung_laptop *samsung)
{
	struct backlight_device *bd;
	struct backlight_properties props;

1192 1193 1194
	if (!samsung->handle_backlight)
		return 0;

1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	memset(&props, 0, sizeof(struct backlight_properties));
	props.type = BACKLIGHT_PLATFORM;
	props.max_brightness = samsung->config->max_brightness -
		samsung->config->min_brightness;

	bd = backlight_device_register("samsung",
				       &samsung->platform_device->dev,
				       samsung, &backlight_ops,
				       &props);
	if (IS_ERR(bd))
		return PTR_ERR(bd);

	samsung->backlight_device = bd;
	samsung->backlight_device->props.brightness = read_brightness(samsung);
	samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
	backlight_update_status(samsung->backlight_device);

	return 0;
}

1215
static umode_t samsung_sysfs_is_visible(struct kobject *kobj,
1216
					struct attribute *attr, int idx)
1217 1218 1219 1220 1221 1222 1223 1224
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct platform_device *pdev = to_platform_device(dev);
	struct samsung_laptop *samsung = platform_get_drvdata(pdev);
	bool ok = true;

	if (attr == &dev_attr_performance_level.attr)
		ok = !!samsung->config->performance_levels[0].name;
1225 1226
	if (attr == &dev_attr_battery_life_extender.attr)
		ok = !!(read_battery_life_extender(samsung) >= 0);
1227 1228
	if (attr == &dev_attr_usb_charge.attr)
		ok = !!(read_usb_charge(samsung) >= 0);
1229 1230
	if (attr == &dev_attr_lid_handling.attr)
		ok = !!(read_lid_handling(samsung) >= 0);
1231 1232 1233 1234 1235 1236 1237 1238 1239

	return ok ? attr->mode : 0;
}

static struct attribute_group platform_attribute_group = {
	.is_visible = samsung_sysfs_is_visible,
	.attrs = platform_attributes
};

1240 1241
static void samsung_sysfs_exit(struct samsung_laptop *samsung)
{
1242 1243 1244
	struct platform_device *device = samsung->platform_device;

	sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1245 1246 1247 1248
}

static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
{
1249 1250 1251 1252
	struct platform_device *device = samsung->platform_device;

	return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);

1253 1254
}

1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
static int show_call(struct seq_file *m, void *data)
{
	struct samsung_laptop *samsung = m->private;
	struct sabi_data *sdata = &samsung->debug.data;
	int ret;

	seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
		   samsung->debug.command,
		   sdata->d0, sdata->d1, sdata->d2, sdata->d3);

	ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);

	if (ret) {
		seq_printf(m, "SABI command 0x%04x failed\n",
			   samsung->debug.command);
		return ret;
	}

	seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
		   sdata->d0, sdata->d1, sdata->d2, sdata->d3);
	return 0;
}

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

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

static void samsung_debugfs_exit(struct samsung_laptop *samsung)
{
	debugfs_remove_recursive(samsung->debug.root);
}

static int samsung_debugfs_init(struct samsung_laptop *samsung)
{
	struct dentry *dent;

	samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL);
	if (!samsung->debug.root) {
		pr_err("failed to create debugfs directory");
		goto error_debugfs;
	}

	samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
	samsung->debug.f0000_wrapper.size = 0xffff;

	samsung->debug.data_wrapper.data = &samsung->debug.data;
	samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);

1312 1313 1314
	samsung->debug.sdiag_wrapper.data = samsung->sdiag;
	samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);

1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
	dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR,
				  samsung->debug.root, &samsung->debug.command);
	if (!dent)
		goto error_debugfs;

	dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root,
				  &samsung->debug.data.d0);
	if (!dent)
		goto error_debugfs;

	dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root,
				  &samsung->debug.data.d1);
	if (!dent)
		goto error_debugfs;

	dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root,
				  &samsung->debug.data.d2);
	if (!dent)
		goto error_debugfs;

	dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root,
				 &samsung->debug.data.d3);
	if (!dent)
		goto error_debugfs;

	dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR,
				   samsung->debug.root,
				   &samsung->debug.data_wrapper);
	if (!dent)
		goto error_debugfs;

	dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR,
				   samsung->debug.root,
				   &samsung->debug.f0000_wrapper);
	if (!dent)
		goto error_debugfs;

	dent = debugfs_create_file("call", S_IFREG | S_IRUGO,
				   samsung->debug.root, samsung,
				   &samsung_laptop_call_io_ops);
	if (!dent)
		goto error_debugfs;

1358 1359 1360 1361 1362 1363
	dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR,
				   samsung->debug.root,
				   &samsung->debug.sdiag_wrapper);
	if (!dent)
		goto error_debugfs;

1364 1365 1366 1367 1368 1369 1370
	return 0;

error_debugfs:
	samsung_debugfs_exit(samsung);
	return -ENOMEM;
}

1371 1372 1373 1374 1375 1376
static void samsung_sabi_exit(struct samsung_laptop *samsung)
{
	const struct sabi_config *config = samsung->config;

	/* Turn off "Linux" mode in the BIOS */
	if (config && config->commands.set_linux != 0xff)
1377
		sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390

	if (samsung->sabi_iface) {
		iounmap(samsung->sabi_iface);
		samsung->sabi_iface = NULL;
	}
	if (samsung->f0000_segment) {
		iounmap(samsung->f0000_segment);
		samsung->f0000_segment = NULL;
	}

	samsung->config = NULL;
}

C
Corentin Chary 已提交
1391 1392
static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
				      unsigned int ifaceP)
1393 1394 1395 1396 1397
{
	const struct sabi_config *config = samsung->config;

	printk(KERN_DEBUG "This computer supports SABI==%x\n",
	       loca + 0xf0000 - 6);
C
Corentin Chary 已提交
1398

1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
	printk(KERN_DEBUG "SABI header:\n");
	printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
	       readw(samsung->sabi + config->header_offsets.port));
	printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
	       readb(samsung->sabi + config->header_offsets.iface_func));
	printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
	       readb(samsung->sabi + config->header_offsets.en_mem));
	printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
	       readb(samsung->sabi + config->header_offsets.re_mem));
	printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
	       readw(samsung->sabi + config->header_offsets.data_offset));
	printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
	       readw(samsung->sabi + config->header_offsets.data_segment));

C
Corentin Chary 已提交
1413
	printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
1414 1415
}

1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
{
	int loca = find_signature(samsung->f0000_segment, "SDiaG@");
	int i;

	if (loca == 0xffff)
		return ;

	/* Example:
	 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
	 *
	 * Product name: 90X3A
	 * BIOS Version: 07HL
	 */
	loca += 1;
	for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
		char temp = readb(samsung->f0000_segment + loca);

		if (isalnum(temp) || temp == '/' || temp == '-')
			samsung->sdiag[i++] = temp;
		else
			break ;
	}

	if (debug && samsung->sdiag[0])
		pr_info("sdiag: %s", samsung->sdiag);
}

1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
static int __init samsung_sabi_init(struct samsung_laptop *samsung)
{
	const struct sabi_config *config = NULL;
	const struct sabi_commands *commands;
	unsigned int ifaceP;
	int ret = 0;
	int i;
	int loca;

	samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
	if (!samsung->f0000_segment) {
1455 1456
		if (debug || force)
			pr_err("Can't map the segment at 0xf0000\n");
1457 1458 1459 1460
		ret = -EINVAL;
		goto exit;
	}

1461 1462
	samsung_sabi_diag(samsung);

1463
	/* Try to find one of the signatures in memory to find the header */
1464
	for (i = 0; sabi_configs[i].test_string != NULL; ++i) {
1465 1466 1467 1468 1469 1470 1471 1472
		samsung->config = &sabi_configs[i];
		loca = find_signature(samsung->f0000_segment,
				      samsung->config->test_string);
		if (loca != 0xffff)
			break;
	}

	if (loca == 0xffff) {
1473 1474
		if (debug || force)
			pr_err("This computer does not support SABI\n");
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
		ret = -ENODEV;
		goto exit;
	}

	config = samsung->config;
	commands = &config->commands;

	/* point to the SMI port Number */
	loca += 1;
	samsung->sabi = (samsung->f0000_segment + loca);

	/* Get a pointer to the SABI Interface */
	ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
	ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
C
Corentin Chary 已提交
1489 1490 1491 1492

	if (debug)
		samsung_sabi_infos(samsung, loca, ifaceP);

1493 1494 1495 1496 1497 1498 1499 1500 1501
	samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
	if (!samsung->sabi_iface) {
		pr_err("Can't remap %x\n", ifaceP);
		ret = -EINVAL;
		goto exit;
	}

	/* Turn on "Linux" mode in the BIOS */
	if (commands->set_linux != 0xff) {
1502 1503
		int retval = sabi_set_commandb(samsung,
					       commands->set_linux, 0x81);
1504 1505 1506 1507 1508 1509 1510 1511
		if (retval) {
			pr_warn("Linux mode was not set!\n");
			ret = -ENODEV;
			goto exit;
		}
	}

	/* Check for stepping quirk */
1512 1513
	if (samsung->handle_backlight)
		check_for_stepping_quirk(samsung);
1514

C
Corentin Chary 已提交
1515 1516 1517
	pr_info("detected SABI interface: %s\n",
		samsung->config->test_string);

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
exit:
	if (ret)
		samsung_sabi_exit(samsung);

	return ret;
}

static void samsung_platform_exit(struct samsung_laptop *samsung)
{
	if (samsung->platform_device) {
		platform_device_unregister(samsung->platform_device);
		samsung->platform_device = NULL;
	}
}

1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
static int samsung_pm_notification(struct notifier_block *nb,
				   unsigned long val, void *ptr)
{
	struct samsung_laptop *samsung;

	samsung = container_of(nb, struct samsung_laptop, pm_nb);
	if (val == PM_POST_HIBERNATION &&
	    samsung->quirks->enable_kbd_backlight)
		kbd_backlight_enable(samsung);

1543 1544 1545
	if (val == PM_POST_HIBERNATION && samsung->quirks->lid_handling)
		write_lid_handling(samsung, 1);

1546 1547 1548
	return 0;
}

1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
static int __init samsung_platform_init(struct samsung_laptop *samsung)
{
	struct platform_device *pdev;

	pdev = platform_device_register_simple("samsung", -1, NULL, 0);
	if (IS_ERR(pdev))
		return PTR_ERR(pdev);

	samsung->platform_device = pdev;
	platform_set_drvdata(samsung->platform_device, samsung);
	return 0;
}

1562 1563 1564 1565 1566 1567 1568 1569
static struct samsung_quirks *quirks;

static int __init samsung_dmi_matched(const struct dmi_system_id *d)
{
	quirks = d->driver_data;
	return 0;
}

1570 1571 1572 1573 1574
static struct dmi_system_id __initdata samsung_dmi_table[] = {
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
1575
			DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
1576 1577 1578 1579 1580 1581
		},
	},
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
1582
			DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
1583 1584
		},
	},
1585 1586 1587 1588
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
1589
			DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
1590 1591
		},
	},
1592 1593 1594 1595
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
1596
			DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
1597
		},
1598
	},
C
Corentin Chary 已提交
1599 1600 1601 1602 1603 1604 1605 1606 1607
	/* DMI ids for laptops with bad Chassis Type */
	{
	  .ident = "R40/R41",
	  .matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		DMI_MATCH(DMI_PRODUCT_NAME, "R40/R41"),
		DMI_MATCH(DMI_BOARD_NAME, "R40/R41"),
		},
	},
1608 1609 1610 1611 1612 1613 1614 1615 1616
	/* Specific DMI ids for laptop with quirks */
	{
	 .callback = samsung_dmi_matched,
	 .ident = "N150P",
	 .matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		DMI_MATCH(DMI_PRODUCT_NAME, "N150P"),
		DMI_MATCH(DMI_BOARD_NAME, "N150P"),
		},
1617
	 .driver_data = &samsung_use_native_backlight,
1618 1619 1620 1621 1622 1623 1624 1625 1626
	},
	{
	 .callback = samsung_dmi_matched,
	 .ident = "N145P/N250P/N260P",
	 .matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		DMI_MATCH(DMI_PRODUCT_NAME, "N145P/N250P/N260P"),
		DMI_MATCH(DMI_BOARD_NAME, "N145P/N250P/N260P"),
		},
1627
	 .driver_data = &samsung_use_native_backlight,
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
	},
	{
	 .callback = samsung_dmi_matched,
	 .ident = "N150/N210/N220",
	 .matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220"),
		DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220"),
		},
	 .driver_data = &samsung_broken_acpi_video,
	},
	{
	 .callback = samsung_dmi_matched,
	 .ident = "NF110/NF210/NF310",
	 .matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		DMI_MATCH(DMI_PRODUCT_NAME, "NF110/NF210/NF310"),
		DMI_MATCH(DMI_BOARD_NAME, "NF110/NF210/NF310"),
		},
	 .driver_data = &samsung_broken_acpi_video,
	},
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
	{
	 .callback = samsung_dmi_matched,
	 .ident = "X360",
	 .matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
		DMI_MATCH(DMI_BOARD_NAME, "X360"),
		},
	 .driver_data = &samsung_broken_acpi_video,
	},
1659 1660 1661 1662 1663 1664 1665 1666
	{
	 .callback = samsung_dmi_matched,
	 .ident = "N250P",
	 .matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		DMI_MATCH(DMI_PRODUCT_NAME, "N250P"),
		DMI_MATCH(DMI_BOARD_NAME, "N250P"),
		},
1667
	 .driver_data = &samsung_use_native_backlight,
1668
	},
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
	{
	 .callback = samsung_dmi_matched,
	 .ident = "NC210",
	 .matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		DMI_MATCH(DMI_PRODUCT_NAME, "NC210/NC110"),
		DMI_MATCH(DMI_BOARD_NAME, "NC210/NC110"),
		},
	 .driver_data = &samsung_broken_acpi_video,
	},
1679 1680 1681 1682 1683 1684 1685 1686 1687
	{
	 .callback = samsung_dmi_matched,
	 .ident = "730U3E/740U3E",
	 .matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
		},
	 .driver_data = &samsung_np740u3e,
	},
1688 1689 1690 1691 1692 1693 1694 1695 1696
	{
	 .callback = samsung_dmi_matched,
	 .ident = "300V3Z/300V4Z/300V5Z",
	 .matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		DMI_MATCH(DMI_PRODUCT_NAME, "300V3Z/300V4Z/300V5Z"),
		},
	 .driver_data = &samsung_lid_handling,
	},
1697 1698 1699 1700
	{ },
};
MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);

1701
static struct platform_device *samsung_platform_device;
1702 1703 1704

static int __init samsung_init(void)
{
1705 1706
	struct samsung_laptop *samsung;
	int ret;
1707

1708 1709 1710
	if (efi_enabled(EFI_BOOT))
		return -ENODEV;

1711
	quirks = &samsung_unknown;
1712 1713 1714
	if (!force && !dmi_check_system(samsung_dmi_table))
		return -ENODEV;

1715 1716 1717 1718 1719
	samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
	if (!samsung)
		return -ENOMEM;

	mutex_init(&samsung->sabi_mutex);
1720
	samsung->handle_backlight = true;
1721
	samsung->quirks = quirks;
1722

1723 1724
#ifdef CONFIG_ACPI
	if (samsung->quirks->broken_acpi_video)
1725 1726 1727
		acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
	if (samsung->quirks->use_native_backlight)
		acpi_video_set_dmi_backlight_type(acpi_backlight_native);
1728

1729
	if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
1730
		samsung->handle_backlight = false;
1731
#endif
1732

1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
	ret = samsung_platform_init(samsung);
	if (ret)
		goto error_platform;

	ret = samsung_sabi_init(samsung);
	if (ret)
		goto error_sabi;

	ret = samsung_sysfs_init(samsung);
	if (ret)
		goto error_sysfs;

	ret = samsung_backlight_init(samsung);
	if (ret)
		goto error_backlight;

	ret = samsung_rfkill_init(samsung);
	if (ret)
		goto error_rfkill;

1753 1754 1755 1756
	ret = samsung_leds_init(samsung);
	if (ret)
		goto error_leds;

1757 1758 1759 1760
	ret = samsung_lid_handling_init(samsung);
	if (ret)
		goto error_lid_handling;

1761 1762 1763 1764
	ret = samsung_debugfs_init(samsung);
	if (ret)
		goto error_debugfs;

1765 1766 1767
	samsung->pm_nb.notifier_call = samsung_pm_notification;
	register_pm_notifier(&samsung->pm_nb);

1768 1769 1770
	samsung_platform_device = samsung->platform_device;
	return ret;

1771
error_debugfs:
1772 1773
	samsung_lid_handling_exit(samsung);
error_lid_handling:
1774 1775
	samsung_leds_exit(samsung);
error_leds:
1776
	samsung_rfkill_exit(samsung);
1777 1778 1779 1780 1781 1782 1783 1784 1785
error_rfkill:
	samsung_backlight_exit(samsung);
error_backlight:
	samsung_sysfs_exit(samsung);
error_sysfs:
	samsung_sabi_exit(samsung);
error_sabi:
	samsung_platform_exit(samsung);
error_platform:
1786
	kfree(samsung);
1787
	return ret;
1788 1789 1790 1791
}

static void __exit samsung_exit(void)
{
1792 1793 1794
	struct samsung_laptop *samsung;

	samsung = platform_get_drvdata(samsung_platform_device);
1795
	unregister_pm_notifier(&samsung->pm_nb);
1796

1797
	samsung_debugfs_exit(samsung);
1798
	samsung_lid_handling_exit(samsung);
1799
	samsung_leds_exit(samsung);
1800 1801 1802 1803 1804
	samsung_rfkill_exit(samsung);
	samsung_backlight_exit(samsung);
	samsung_sysfs_exit(samsung);
	samsung_sabi_exit(samsung);
	samsung_platform_exit(samsung);
1805 1806

	kfree(samsung);
1807
	samsung_platform_device = NULL;
1808 1809 1810 1811 1812 1813 1814 1815
}

module_init(samsung_init);
module_exit(samsung_exit);

MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
MODULE_DESCRIPTION("Samsung Backlight driver");
MODULE_LICENSE("GPL");