samsung-laptop.c 29.6 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
/*
 * 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>
#include <linux/fb.h>
#include <linux/dmi.h>
#include <linux/platform_device.h>
#include <linux/rfkill.h>
24
#include <linux/acpi.h>
25 26
#include <linux/seq_file.h>
#include <linux/debugfs.h>
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/*
 * 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

47 48 49 50 51 52 53 54 55 56 57
/* Structure get/set data using sabi */
struct sabi_data {
	union {
		struct {
			u32 d0;
			u32 d1;
			u16 d2;
			u8  d3;
		};
		u8 data[11];
	};
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
};

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
	 */
74 75
	u16 get_brightness;
	u16 set_brightness;
76 77 78 79 80 81 82 83 84 85

	/*
	 * 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...
	 */
86 87
	u16 get_wireless_button;
	u16 set_wireless_button;
88 89

	/* 0 is off, 1 is on */
90 91
	u16 get_backlight;
	u16 set_backlight;
92 93 94 95 96

	/*
	 * 0x80 or 0x00 - no action
	 * 0x81 - recovery key pressed
	 */
97 98
	u16 get_recovery_mode;
	u16 set_recovery_mode;
99 100 101 102 103

	/*
	 * on seclinux: 0 is low, 1 is high,
	 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
	 */
104 105
	u16 get_performance_level;
	u16 set_performance_level;
106 107 108 109 110

	/*
	 * Tell the BIOS that Linux is running on this machine.
	 * 81 is on, 80 is off
	 */
111
	u16 set_linux;
112 113 114 115
};

struct sabi_performance_level {
	const char *name;
116
	u16 value;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 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
};

struct sabi_config {
	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[] = {
	{
		.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,

			.set_linux = 0x0a,
		},

		.performance_levels = {
			{
				.name = "silent",
				.value = 0,
			},
			{
				.name = "normal",
				.value = 1,
			},
			{ },
		},
		.min_brightness = 1,
		.max_brightness = 8,
	},
	{
		.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,

			.set_linux = 0xff,
		},

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

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/*
 * 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;
};

260 261
struct samsung_laptop {
	const struct sabi_config *config;
262

263 264 265 266 267 268
	void __iomem *sabi;
	void __iomem *sabi_iface;
	void __iomem *f0000_segment;

	struct mutex sabi_mutex;

269
	struct platform_device *platform_device;
270 271 272
	struct backlight_device *backlight_device;
	struct rfkill *rfk;

273 274
	struct samsung_laptop_debug debug;

275
	bool handle_backlight;
276 277 278
	bool has_stepping_quirk;
};

279

280

281
static bool force;
282 283 284 285
module_param(force, bool, 0);
MODULE_PARM_DESC(force,
		"Disable the DMI check and forces the driver to be loaded");

286
static bool debug;
287 288 289
module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");

290 291 292
static int sabi_command(struct samsung_laptop *samsung, u16 command,
			struct sabi_data *in,
			struct sabi_data *out)
293
{
294
	const struct sabi_config *config = samsung->config;
295
	int ret = 0;
296
	u16 port = readw(samsung->sabi + config->header_offsets.port);
297 298
	u8 complete, iface_data;

299
	mutex_lock(&samsung->sabi_mutex);
300

301 302 303 304 305 306 307 308
	if (debug) {
		if (in)
			pr_info("SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}",
				command, in->d0, in->d1, in->d2, in->d3);
		else
			pr_info("SABI 0x%04x", command);
	}

309
	/* enable memory to be able to write to it */
310
	outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
311 312

	/* write out the command */
313 314 315
	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);
316 317 318 319 320 321
	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);
	}
322
	outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
323 324

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

	/* see if the command actually succeeded */
328 329
	complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
	iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
330
	if (complete != 0xaa || iface_data == 0xff) {
331 332 333 334
		pr_warn("SABI command 0x%04x failed with"
			" completion flag 0x%02x and interface data 0x%02x",
			command, complete, iface_data);
		ret = -EINVAL;
335 336
		goto exit;
	}
337 338 339 340 341 342 343 344 345 346 347 348

	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) {
		pr_info("SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}",
			out->d0, out->d1, out->d2, out->d3);
	}
349 350

exit:
351
	mutex_unlock(&samsung->sabi_mutex);
352
	return ret;
353 354
}

355 356 357
/* simple wrappers usable with most commands */
static int sabi_set_commandb(struct samsung_laptop *samsung,
			     u16 command, u8 data)
358
{
359
	struct sabi_data in = { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 };
360

361 362
	in.data[0] = data;
	return sabi_command(samsung, command, &in, NULL);
363 364
}

365
static int read_brightness(struct samsung_laptop *samsung)
366
{
367 368
	const struct sabi_config *config = samsung->config;
	const struct sabi_commands *commands = &samsung->config->commands;
369
	struct sabi_data sretval;
370 371 372
	int user_brightness = 0;
	int retval;

373 374 375 376 377 378 379 380 381 382 383
	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;

384 385 386
	return user_brightness;
}

387
static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
388
{
389 390 391
	const struct sabi_config *config = samsung->config;
	const struct sabi_commands *commands = &samsung->config->commands;
	u8 user_level = user_brightness + config->min_brightness;
392

393
	if (samsung->has_stepping_quirk && user_level != 0) {
394 395 396 397
		/*
		 * short circuit if the specified level is what's already set
		 * to prevent the screen from flickering needlessly
		 */
398
		if (user_brightness == read_brightness(samsung))
399 400
			return;

401
		sabi_set_commandb(samsung, commands->set_brightness, 0);
402 403
	}

404
	sabi_set_commandb(samsung, commands->set_brightness, user_level);
405 406 407 408
}

static int get_brightness(struct backlight_device *bd)
{
409 410 411
	struct samsung_laptop *samsung = bl_get_data(bd);

	return read_brightness(samsung);
412 413
}

414
static void check_for_stepping_quirk(struct samsung_laptop *samsung)
415
{
416 417 418
	int initial_level;
	int check_level;
	int orig_level = read_brightness(samsung);
419 420 421 422 423 424 425 426

	/*
	 * 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.
	 */

427
	if (orig_level == 0)
428
		set_brightness(samsung, 1);
429

430
	initial_level = read_brightness(samsung);
431

432 433 434 435 436
	if (initial_level <= 2)
		check_level = initial_level + 2;
	else
		check_level = initial_level - 2;

437
	samsung->has_stepping_quirk = false;
438
	set_brightness(samsung, check_level);
439

440
	if (read_brightness(samsung) != check_level) {
441
		samsung->has_stepping_quirk = true;
442 443 444
		pr_info("enabled workaround for brightness stepping quirk\n");
	}

445
	set_brightness(samsung, orig_level);
446 447
}

448 449
static int update_status(struct backlight_device *bd)
{
450
	struct samsung_laptop *samsung = bl_get_data(bd);
451 452
	const struct sabi_commands *commands = &samsung->config->commands;

453
	set_brightness(samsung, bd->props.brightness);
454 455

	if (bd->props.power == FB_BLANK_UNBLANK)
456
		sabi_set_commandb(samsung, commands->set_backlight, 1);
457
	else
458
		sabi_set_commandb(samsung, commands->set_backlight, 0);
459

460 461 462 463 464 465 466 467 468 469
	return 0;
}

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

static int rfkill_set(void *data, bool blocked)
{
470
	struct samsung_laptop *samsung = data;
471 472
	const struct sabi_commands *commands = &samsung->config->commands;

473 474 475 476 477 478
	/* Do something with blocked...*/
	/*
	 * blocked == false is on
	 * blocked == true is off
	 */
	if (blocked)
479
		sabi_set_commandb(samsung, commands->set_wireless_button, 0);
480
	else
481
		sabi_set_commandb(samsung, commands->set_wireless_button, 1);
482 483 484 485 486 487 488 489 490 491 492

	return 0;
}

static struct rfkill_ops rfkill_ops = {
	.set_block = rfkill_set,
};

static ssize_t get_performance_level(struct device *dev,
				     struct device_attribute *attr, char *buf)
{
493
	struct samsung_laptop *samsung = dev_get_drvdata(dev);
494
	const struct sabi_config *config = samsung->config;
495
	const struct sabi_commands *commands = &config->commands;
496
	struct sabi_data sretval;
497 498 499 500
	int retval;
	int i;

	/* Read the state */
501 502
	retval = sabi_command(samsung, commands->get_performance_level,
			      NULL, &sretval);
503 504 505 506
	if (retval)
		return retval;

	/* The logic is backwards, yeah, lots of fun... */
507
	for (i = 0; config->performance_levels[i].name; ++i) {
508
		if (sretval.data[0] == config->performance_levels[i].value)
509
			return sprintf(buf, "%s\n", config->performance_levels[i].name);
510 511 512 513 514 515 516 517
	}
	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)
{
518
	struct samsung_laptop *samsung = dev_get_drvdata(dev);
519
	const struct sabi_config *config = samsung->config;
520 521
	const struct sabi_commands *commands = &config->commands;
	int i;
522

523 524 525 526 527 528 529
	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))) {
530 531 532
			sabi_set_commandb(samsung,
					  commands->set_performance_level,
					  level->value);
533
			break;
534 535
		}
	}
536 537 538 539

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

540 541
	return count;
}
542

543 544 545
static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
		   get_performance_level, set_performance_level);

546 547 548 549
static struct attribute *platform_attributes[] = {
	&dev_attr_performance_level.attr,
	NULL
};
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 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
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)
{
	if (samsung->rfk) {
		rfkill_unregister(samsung->rfk);
		rfkill_destroy(samsung->rfk);
		samsung->rfk = NULL;
	}
}

static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
{
	int retval;

	samsung->rfk = rfkill_alloc("samsung-wifi",
				    &samsung->platform_device->dev,
				    RFKILL_TYPE_WLAN,
				    &rfkill_ops, samsung);
	if (!samsung->rfk)
		return -ENOMEM;

	retval = rfkill_register(samsung->rfk);
	if (retval) {
		rfkill_destroy(samsung->rfk);
		samsung->rfk = NULL;
		return -ENODEV;
	}

	return 0;
}

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;

613 614 615
	if (!samsung->handle_backlight)
		return 0;

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	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;
}

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
static mode_t samsung_sysfs_is_visible(struct kobject *kobj,
				       struct attribute *attr, int idx)
{
	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;

	return ok ? attr->mode : 0;
}

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

655 656
static void samsung_sysfs_exit(struct samsung_laptop *samsung)
{
657 658 659
	struct platform_device *device = samsung->platform_device;

	sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
660 661 662 663
}

static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
{
664 665 666 667
	struct platform_device *device = samsung->platform_device;

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

668 669
}

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 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 771 772 773 774 775 776
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);

	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;

	return 0;

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

777 778 779 780 781 782
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)
783
		sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
784 785 786 787 788 789 790 791 792 793 794 795 796

	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 已提交
797 798
static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
				      unsigned int ifaceP)
799 800 801 802 803
{
	const struct sabi_config *config = samsung->config;

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

805 806 807 808 809 810 811 812 813 814 815 816 817 818
	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 已提交
819
	printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
}

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) {
		pr_err("Can't map the segment at 0xf0000\n");
		ret = -EINVAL;
		goto exit;
	}

	/* Try to find one of the signatures in memory to find the header */
	for (i = 0; sabi_configs[i].test_string != 0; ++i) {
		samsung->config = &sabi_configs[i];
		loca = find_signature(samsung->f0000_segment,
				      samsung->config->test_string);
		if (loca != 0xffff)
			break;
	}

	if (loca == 0xffff) {
		pr_err("This computer does not support SABI\n");
		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 已提交
863 864 865 866

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

867 868 869 870 871 872 873 874 875
	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) {
876 877
		int retval = sabi_set_commandb(samsung,
					       commands->set_linux, 0x81);
878 879 880 881 882 883 884 885
		if (retval) {
			pr_warn("Linux mode was not set!\n");
			ret = -ENODEV;
			goto exit;
		}
	}

	/* Check for stepping quirk */
886 887
	if (samsung->handle_backlight)
		check_for_stepping_quirk(samsung);
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

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

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

917 918
static int __init dmi_check_cb(const struct dmi_system_id *id)
{
919
	pr_info("found laptop model '%s'\n", id->ident);
920
	return 1;
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
}

static struct dmi_system_id __initdata samsung_dmi_table[] = {
	{
		.ident = "N128",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "N128"),
			DMI_MATCH(DMI_BOARD_NAME, "N128"),
		},
		.callback = dmi_check_cb,
	},
	{
		.ident = "N130",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "N130"),
			DMI_MATCH(DMI_BOARD_NAME, "N130"),
		},
		.callback = dmi_check_cb,
943 944 945 946 947 948 949 950 951 952
	},
	{
		.ident = "N510",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "N510"),
			DMI_MATCH(DMI_BOARD_NAME, "N510"),
		},
		.callback = dmi_check_cb,
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
	},
	{
		.ident = "X125",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "X125"),
			DMI_MATCH(DMI_BOARD_NAME, "X125"),
		},
		.callback = dmi_check_cb,
	},
	{
		.ident = "X120/X170",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "X120/X170"),
			DMI_MATCH(DMI_BOARD_NAME, "X120/X170"),
		},
		.callback = dmi_check_cb,
	},
	{
		.ident = "NC10",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "NC10"),
			DMI_MATCH(DMI_BOARD_NAME, "NC10"),
		},
		.callback = dmi_check_cb,
	},
		{
		.ident = "NP-Q45",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "SQ45S70S"),
			DMI_MATCH(DMI_BOARD_NAME, "SQ45S70S"),
		},
		.callback = dmi_check_cb,
		},
	{
		.ident = "X360",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
			DMI_MATCH(DMI_BOARD_NAME, "X360"),
		},
		.callback = dmi_check_cb,
	},
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
	{
		.ident = "R410 Plus",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "R410P"),
			DMI_MATCH(DMI_BOARD_NAME, "R460"),
		},
		.callback = dmi_check_cb,
	},
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	{
		.ident = "R518",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "R518"),
			DMI_MATCH(DMI_BOARD_NAME, "R518"),
		},
		.callback = dmi_check_cb,
	},
	{
		.ident = "R519/R719",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "R519/R719"),
			DMI_MATCH(DMI_BOARD_NAME, "R519/R719"),
		},
		.callback = dmi_check_cb,
	},
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	{
		.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"),
		},
		.callback = dmi_check_cb,
	},
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
	{
		.ident = "N220",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "N220"),
			DMI_MATCH(DMI_BOARD_NAME, "N220"),
		},
		.callback = dmi_check_cb,
	},
1054
	{
1055
		.ident = "N150/N210/N220/N230",
1056 1057 1058
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
1059 1060
			DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220/N230"),
			DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220/N230"),
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
		},
		.callback = dmi_check_cb,
	},
	{
		.ident = "N150P/N210P/N220P",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "N150P/N210P/N220P"),
			DMI_MATCH(DMI_BOARD_NAME, "N150P/N210P/N220P"),
		},
		.callback = dmi_check_cb,
	},
1074 1075 1076 1077 1078 1079 1080 1081 1082
	{
		.ident = "R700",
		.matches = {
		      DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		      DMI_MATCH(DMI_PRODUCT_NAME, "SR700"),
		      DMI_MATCH(DMI_BOARD_NAME, "SR700"),
		},
		.callback = dmi_check_cb,
	},
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
	{
		.ident = "R530/R730",
		.matches = {
		      DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
		      DMI_MATCH(DMI_PRODUCT_NAME, "R530/R730"),
		      DMI_MATCH(DMI_BOARD_NAME, "R530/R730"),
		},
		.callback = dmi_check_cb,
	},
	{
		.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"),
		},
		.callback = dmi_check_cb,
	},
	{
		.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"),
		},
		.callback = dmi_check_cb,
	},
	{
		.ident = "R70/R71",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR,
					"SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "R70/R71"),
			DMI_MATCH(DMI_BOARD_NAME, "R70/R71"),
		},
		.callback = dmi_check_cb,
	},
	{
		.ident = "P460",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "P460"),
			DMI_MATCH(DMI_BOARD_NAME, "P460"),
		},
		.callback = dmi_check_cb,
	},
1129 1130 1131 1132 1133 1134 1135 1136 1137
	{
		.ident = "R528/R728",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "R528/R728"),
			DMI_MATCH(DMI_BOARD_NAME, "R528/R728"),
		},
		.callback = dmi_check_cb,
	},
1138 1139 1140 1141 1142 1143 1144 1145
	{
		.ident = "NC210/NC110",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "NC210/NC110"),
			DMI_MATCH(DMI_BOARD_NAME, "NC210/NC110"),
		},
		.callback = dmi_check_cb,
1146 1147 1148 1149 1150 1151 1152 1153 1154
	},
		{
		.ident = "X520",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
			DMI_MATCH(DMI_PRODUCT_NAME, "X520"),
			DMI_MATCH(DMI_BOARD_NAME, "X520"),
		},
		.callback = dmi_check_cb,
1155
	},
1156 1157 1158 1159
	{ },
};
MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);

1160
static struct platform_device *samsung_platform_device;
1161 1162 1163

static int __init samsung_init(void)
{
1164 1165
	struct samsung_laptop *samsung;
	int ret;
1166 1167 1168 1169

	if (!force && !dmi_check_system(samsung_dmi_table))
		return -ENODEV;

1170 1171 1172 1173 1174
	samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
	if (!samsung)
		return -ENOMEM;

	mutex_init(&samsung->sabi_mutex);
1175 1176 1177 1178 1179 1180 1181 1182 1183
	samsung->handle_backlight = true;

#ifdef CONFIG_ACPI
	/* Don't handle backlight here if the acpi video already handle it */
	if (acpi_video_backlight_support()) {
		pr_info("Backlight controlled by ACPI video driver\n");
		samsung->handle_backlight = false;
	}
#endif
1184

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	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;

1205 1206 1207 1208
	ret = samsung_debugfs_init(samsung);
	if (ret)
		goto error_debugfs;

1209 1210 1211
	samsung_platform_device = samsung->platform_device;
	return ret;

1212 1213
error_debugfs:
	samsung_rfkill_exit(samsung);
1214 1215 1216 1217 1218 1219 1220 1221 1222
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:
1223
	kfree(samsung);
1224
	return ret;
1225 1226 1227 1228
}

static void __exit samsung_exit(void)
{
1229 1230 1231 1232
	struct samsung_laptop *samsung;

	samsung = platform_get_drvdata(samsung_platform_device);

1233
	samsung_debugfs_exit(samsung);
1234 1235 1236 1237 1238
	samsung_rfkill_exit(samsung);
	samsung_backlight_exit(samsung);
	samsung_sysfs_exit(samsung);
	samsung_sabi_exit(samsung);
	samsung_platform_exit(samsung);
1239 1240

	kfree(samsung);
1241
	samsung_platform_device = NULL;
1242 1243 1244 1245 1246 1247 1248 1249
}

module_init(samsung_init);
module_exit(samsung_exit);

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