Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  Driver for Dell laptop extras
   4 *
   5 *  Copyright (c) Red Hat <mjg@redhat.com>
   6 *  Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
   7 *  Copyright (c) 2014 Pali Rohár <pali@kernel.org>
   8 *
   9 *  Based on documentation in the libsmbios package:
  10 *  Copyright (C) 2005-2014 Dell Inc.
 
 
 
 
  11 */
  12
  13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14
  15#include <linux/module.h>
  16#include <linux/kernel.h>
  17#include <linux/init.h>
  18#include <linux/platform_device.h>
  19#include <linux/backlight.h>
  20#include <linux/err.h>
  21#include <linux/dmi.h>
  22#include <linux/io.h>
  23#include <linux/rfkill.h>
  24#include <linux/power_supply.h>
  25#include <linux/acpi.h>
  26#include <linux/mm.h>
  27#include <linux/i8042.h>
  28#include <linux/debugfs.h>
  29#include <linux/seq_file.h>
  30#include <acpi/video.h>
  31#include "dell-rbtn.h"
  32#include "dell-smbios.h"
  33
 
 
 
 
 
 
 
 
 
  34struct quirk_entry {
  35	bool touchpad_led;
  36	bool kbd_led_not_present;
  37	bool kbd_led_levels_off_1;
  38	bool kbd_missing_ac_tag;
  39
  40	bool needs_kbd_timeouts;
  41	/*
  42	 * Ordered list of timeouts expressed in seconds.
  43	 * The list must end with -1
  44	 */
  45	int kbd_timeouts[];
  46};
  47
  48static struct quirk_entry *quirks;
  49
  50static struct quirk_entry quirk_dell_vostro_v130 = {
  51	.touchpad_led = true,
  52};
  53
  54static int __init dmi_matched(const struct dmi_system_id *dmi)
  55{
  56	quirks = dmi->driver_data;
  57	return 1;
  58}
  59
  60/*
  61 * These values come from Windows utility provided by Dell. If any other value
  62 * is used then BIOS silently set timeout to 0 without any error message.
  63 */
  64static struct quirk_entry quirk_dell_xps13_9333 = {
  65	.needs_kbd_timeouts = true,
  66	.kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
  67};
  68
  69static struct quirk_entry quirk_dell_xps13_9370 = {
  70	.kbd_missing_ac_tag = true,
  71};
  72
  73static struct quirk_entry quirk_dell_latitude_e6410 = {
  74	.kbd_led_levels_off_1 = true,
  75};
  76
  77static struct quirk_entry quirk_dell_inspiron_1012 = {
  78	.kbd_led_not_present = true,
  79};
  80
  81static struct platform_driver platform_driver = {
  82	.driver = {
  83		.name = "dell-laptop",
  84	}
  85};
  86
  87static struct platform_device *platform_device;
  88static struct backlight_device *dell_backlight_device;
  89static struct rfkill *wifi_rfkill;
  90static struct rfkill *bluetooth_rfkill;
  91static struct rfkill *wwan_rfkill;
  92static bool force_rfkill;
  93
  94module_param(force_rfkill, bool, 0444);
  95MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
  96
  97static const struct dmi_system_id dell_device_table[] __initconst = {
  98	{
  99		.ident = "Dell laptop",
 100		.matches = {
 101			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 102			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
 103		},
 104	},
 105	{
 106		.matches = {
 107			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 108			DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
 109		},
 110	},
 111	{
 112		.matches = {
 113			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 114			DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
 115		},
 116	},
 117	{
 118		.matches = {
 119			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 120			DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
 121		},
 122	},
 123	{
 124		.matches = {
 125			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 126			DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
 127		},
 128	},
 129	{
 130		.matches = {
 131			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 132			DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
 133		},
 134	},
 135	{
 136		.ident = "Dell Computer Corporation",
 137		.matches = {
 138			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
 139			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
 140		},
 141	},
 142	{ }
 143};
 144MODULE_DEVICE_TABLE(dmi, dell_device_table);
 145
 146static const struct dmi_system_id dell_quirks[] __initconst = {
 147	{
 148		.callback = dmi_matched,
 149		.ident = "Dell Vostro V130",
 150		.matches = {
 151			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 152			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
 153		},
 154		.driver_data = &quirk_dell_vostro_v130,
 155	},
 156	{
 157		.callback = dmi_matched,
 158		.ident = "Dell Vostro V131",
 159		.matches = {
 160			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 161			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
 162		},
 163		.driver_data = &quirk_dell_vostro_v130,
 164	},
 165	{
 166		.callback = dmi_matched,
 167		.ident = "Dell Vostro 3350",
 168		.matches = {
 169			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 170			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
 171		},
 172		.driver_data = &quirk_dell_vostro_v130,
 173	},
 174	{
 175		.callback = dmi_matched,
 176		.ident = "Dell Vostro 3555",
 177		.matches = {
 178			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 179			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
 180		},
 181		.driver_data = &quirk_dell_vostro_v130,
 182	},
 183	{
 184		.callback = dmi_matched,
 185		.ident = "Dell Inspiron N311z",
 186		.matches = {
 187			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 188			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
 189		},
 190		.driver_data = &quirk_dell_vostro_v130,
 191	},
 192	{
 193		.callback = dmi_matched,
 194		.ident = "Dell Inspiron M5110",
 195		.matches = {
 196			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 197			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
 198		},
 199		.driver_data = &quirk_dell_vostro_v130,
 200	},
 201	{
 202		.callback = dmi_matched,
 203		.ident = "Dell Vostro 3360",
 204		.matches = {
 205			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 206			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
 207		},
 208		.driver_data = &quirk_dell_vostro_v130,
 209	},
 210	{
 211		.callback = dmi_matched,
 212		.ident = "Dell Vostro 3460",
 213		.matches = {
 214			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 215			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
 216		},
 217		.driver_data = &quirk_dell_vostro_v130,
 218	},
 219	{
 220		.callback = dmi_matched,
 221		.ident = "Dell Vostro 3560",
 222		.matches = {
 223			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 224			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
 225		},
 226		.driver_data = &quirk_dell_vostro_v130,
 227	},
 228	{
 229		.callback = dmi_matched,
 230		.ident = "Dell Vostro 3450",
 231		.matches = {
 232			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 233			DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
 234		},
 235		.driver_data = &quirk_dell_vostro_v130,
 236	},
 237	{
 238		.callback = dmi_matched,
 239		.ident = "Dell Inspiron 5420",
 240		.matches = {
 241			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 242			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
 243		},
 244		.driver_data = &quirk_dell_vostro_v130,
 245	},
 246	{
 247		.callback = dmi_matched,
 248		.ident = "Dell Inspiron 5520",
 249		.matches = {
 250			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 251			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
 252		},
 253		.driver_data = &quirk_dell_vostro_v130,
 254	},
 255	{
 256		.callback = dmi_matched,
 257		.ident = "Dell Inspiron 5720",
 258		.matches = {
 259			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 260			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
 261		},
 262		.driver_data = &quirk_dell_vostro_v130,
 263	},
 264	{
 265		.callback = dmi_matched,
 266		.ident = "Dell Inspiron 7420",
 267		.matches = {
 268			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 269			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
 270		},
 271		.driver_data = &quirk_dell_vostro_v130,
 272	},
 273	{
 274		.callback = dmi_matched,
 275		.ident = "Dell Inspiron 7520",
 276		.matches = {
 277			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 278			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
 279		},
 280		.driver_data = &quirk_dell_vostro_v130,
 281	},
 282	{
 283		.callback = dmi_matched,
 284		.ident = "Dell Inspiron 7720",
 285		.matches = {
 286			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 287			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
 288		},
 289		.driver_data = &quirk_dell_vostro_v130,
 290	},
 291	{
 292		.callback = dmi_matched,
 293		.ident = "Dell XPS13 9333",
 294		.matches = {
 295			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 296			DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
 297		},
 298		.driver_data = &quirk_dell_xps13_9333,
 299	},
 300	{
 301		.callback = dmi_matched,
 302		.ident = "Dell XPS 13 9370",
 303		.matches = {
 304			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 305			DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
 306		},
 307		.driver_data = &quirk_dell_xps13_9370,
 308	},
 309	{
 310		.callback = dmi_matched,
 311		.ident = "Dell Latitude E6410",
 312		.matches = {
 313			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 314			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
 315		},
 316		.driver_data = &quirk_dell_latitude_e6410,
 317	},
 318	{
 319		.callback = dmi_matched,
 320		.ident = "Dell Inspiron 1012",
 321		.matches = {
 322			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 323			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
 324		},
 325		.driver_data = &quirk_dell_inspiron_1012,
 326	},
 327	{
 328		.callback = dmi_matched,
 329		.ident = "Dell Inspiron 1018",
 330		.matches = {
 331			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 332			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1018"),
 333		},
 334		.driver_data = &quirk_dell_inspiron_1012,
 335	},
 336	{ }
 337};
 338
 339static void dell_fill_request(struct calling_interface_buffer *buffer,
 340			       u32 arg0, u32 arg1, u32 arg2, u32 arg3)
 341{
 342	memset(buffer, 0, sizeof(struct calling_interface_buffer));
 343	buffer->input[0] = arg0;
 344	buffer->input[1] = arg1;
 345	buffer->input[2] = arg2;
 346	buffer->input[3] = arg3;
 347}
 348
 349static int dell_send_request(struct calling_interface_buffer *buffer,
 350			     u16 class, u16 select)
 351{
 352	int ret;
 353
 354	buffer->cmd_class = class;
 355	buffer->cmd_select = select;
 356	ret = dell_smbios_call(buffer);
 357	if (ret != 0)
 358		return ret;
 359	return dell_smbios_error(buffer->output[0]);
 360}
 361
 362/*
 363 * Derived from information in smbios-wireless-ctl:
 364 *
 365 * cbSelect 17, Value 11
 366 *
 367 * Return Wireless Info
 368 * cbArg1, byte0 = 0x00
 369 *
 370 *     cbRes1 Standard return codes (0, -1, -2)
 371 *     cbRes2 Info bit flags:
 372 *
 373 *     0 Hardware switch supported (1)
 374 *     1 WiFi locator supported (1)
 375 *     2 WLAN supported (1)
 376 *     3 Bluetooth (BT) supported (1)
 377 *     4 WWAN supported (1)
 378 *     5 Wireless KBD supported (1)
 379 *     6 Uw b supported (1)
 380 *     7 WiGig supported (1)
 381 *     8 WLAN installed (1)
 382 *     9 BT installed (1)
 383 *     10 WWAN installed (1)
 384 *     11 Uw b installed (1)
 385 *     12 WiGig installed (1)
 386 *     13-15 Reserved (0)
 387 *     16 Hardware (HW) switch is On (1)
 388 *     17 WLAN disabled (1)
 389 *     18 BT disabled (1)
 390 *     19 WWAN disabled (1)
 391 *     20 Uw b disabled (1)
 392 *     21 WiGig disabled (1)
 393 *     20-31 Reserved (0)
 394 *
 395 *     cbRes3 NVRAM size in bytes
 396 *     cbRes4, byte 0 NVRAM format version number
 397 *
 398 *
 399 * Set QuickSet Radio Disable Flag
 400 *     cbArg1, byte0 = 0x01
 401 *     cbArg1, byte1
 402 *     Radio ID     value:
 403 *     0        Radio Status
 404 *     1        WLAN ID
 405 *     2        BT ID
 406 *     3        WWAN ID
 407 *     4        UWB ID
 408 *     5        WIGIG ID
 409 *     cbArg1, byte2    Flag bits:
 410 *             0 QuickSet disables radio (1)
 411 *             1-7 Reserved (0)
 412 *
 413 *     cbRes1    Standard return codes (0, -1, -2)
 414 *     cbRes2    QuickSet (QS) radio disable bit map:
 415 *     0 QS disables WLAN
 416 *     1 QS disables BT
 417 *     2 QS disables WWAN
 418 *     3 QS disables UWB
 419 *     4 QS disables WIGIG
 420 *     5-31 Reserved (0)
 421 *
 422 * Wireless Switch Configuration
 423 *     cbArg1, byte0 = 0x02
 424 *
 425 *     cbArg1, byte1
 426 *     Subcommand:
 427 *     0 Get config
 428 *     1 Set config
 429 *     2 Set WiFi locator enable/disable
 430 *     cbArg1,byte2
 431 *     Switch settings (if byte 1==1):
 432 *     0 WLAN sw itch control (1)
 433 *     1 BT sw itch control (1)
 434 *     2 WWAN sw itch control (1)
 435 *     3 UWB sw itch control (1)
 436 *     4 WiGig sw itch control (1)
 437 *     5-7 Reserved (0)
 438 *    cbArg1, byte2 Enable bits (if byte 1==2):
 439 *     0 Enable WiFi locator (1)
 440 *
 441 *    cbRes1     Standard return codes (0, -1, -2)
 442 *    cbRes2 QuickSet radio disable bit map:
 443 *     0 WLAN controlled by sw itch (1)
 444 *     1 BT controlled by sw itch (1)
 445 *     2 WWAN controlled by sw itch (1)
 446 *     3 UWB controlled by sw itch (1)
 447 *     4 WiGig controlled by sw itch (1)
 448 *     5-6 Reserved (0)
 449 *     7 Wireless sw itch config locked (1)
 450 *     8 WiFi locator enabled (1)
 451 *     9-14 Reserved (0)
 452 *     15 WiFi locator setting locked (1)
 453 *     16-31 Reserved (0)
 454 *
 455 * Read Local Config Data (LCD)
 456 *     cbArg1, byte0 = 0x10
 457 *     cbArg1, byte1 NVRAM index low byte
 458 *     cbArg1, byte2 NVRAM index high byte
 459 *     cbRes1 Standard return codes (0, -1, -2)
 460 *     cbRes2 4 bytes read from LCD[index]
 461 *     cbRes3 4 bytes read from LCD[index+4]
 462 *     cbRes4 4 bytes read from LCD[index+8]
 463 *
 464 * Write Local Config Data (LCD)
 465 *     cbArg1, byte0 = 0x11
 466 *     cbArg1, byte1 NVRAM index low byte
 467 *     cbArg1, byte2 NVRAM index high byte
 468 *     cbArg2 4 bytes to w rite at LCD[index]
 469 *     cbArg3 4 bytes to w rite at LCD[index+4]
 470 *     cbArg4 4 bytes to w rite at LCD[index+8]
 471 *     cbRes1 Standard return codes (0, -1, -2)
 472 *
 473 * Populate Local Config Data from NVRAM
 474 *     cbArg1, byte0 = 0x12
 475 *     cbRes1 Standard return codes (0, -1, -2)
 476 *
 477 * Commit Local Config Data to NVRAM
 478 *     cbArg1, byte0 = 0x13
 479 *     cbRes1 Standard return codes (0, -1, -2)
 480 */
 481
 482static int dell_rfkill_set(void *data, bool blocked)
 483{
 
 484	int disable = blocked ? 1 : 0;
 485	unsigned long radio = (unsigned long)data;
 486	int hwswitch_bit = (unsigned long)data - 1;
 487	struct calling_interface_buffer buffer;
 488	int hwswitch;
 489	int status;
 490	int ret;
 491
 492	dell_fill_request(&buffer, 0, 0, 0, 0);
 493	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 494	if (ret)
 495		return ret;
 496	status = buffer.output[1];
 497
 498	dell_fill_request(&buffer, 0x2, 0, 0, 0);
 499	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 500	if (ret)
 501		return ret;
 502	hwswitch = buffer.output[1];
 
 
 
 
 
 
 
 
 503
 504	/* If the hardware switch controls this radio, and the hardware
 505	   switch is disabled, always disable the radio */
 506	if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
 507	    (status & BIT(0)) && !(status & BIT(16)))
 508		disable = 1;
 509
 510	dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
 511	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 512	return ret;
 
 
 
 
 
 
 513}
 514
 
 515static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
 516					int status)
 
 517{
 518	if (status & BIT(0)) {
 519		/* Has hw-switch, sync sw_state to BIOS */
 520		struct calling_interface_buffer buffer;
 521		int block = rfkill_blocked(rfkill);
 522		dell_fill_request(&buffer,
 523				   1 | (radio << 8) | (block << 16), 0, 0, 0);
 524		dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 525	} else {
 526		/* No hw-switch, sync BIOS state to sw_state */
 527		rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
 528	}
 529}
 530
 531static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
 532					int status, int hwswitch)
 533{
 534	if (hwswitch & (BIT(radio - 1)))
 535		rfkill_set_hw_state(rfkill, !(status & BIT(16)));
 536}
 537
 538static void dell_rfkill_query(struct rfkill *rfkill, void *data)
 539{
 
 540	int radio = ((unsigned long)data & 0xF);
 541	struct calling_interface_buffer buffer;
 542	int hwswitch;
 543	int status;
 544	int ret;
 545
 546	dell_fill_request(&buffer, 0, 0, 0, 0);
 547	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 548	status = buffer.output[1];
 
 
 549
 550	if (ret != 0 || !(status & BIT(0))) {
 
 551		return;
 552	}
 553
 554	dell_fill_request(&buffer, 0x2, 0, 0, 0);
 555	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 556	hwswitch = buffer.output[1];
 
 
 
 
 
 557
 558	if (ret != 0)
 559		return;
 560
 561	dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
 562}
 563
 564static const struct rfkill_ops dell_rfkill_ops = {
 565	.set_block = dell_rfkill_set,
 566	.query = dell_rfkill_query,
 567};
 568
 569static struct dentry *dell_laptop_dir;
 570
 571static int dell_debugfs_show(struct seq_file *s, void *data)
 572{
 573	struct calling_interface_buffer buffer;
 574	int hwswitch_state;
 575	int hwswitch_ret;
 576	int status;
 577	int ret;
 578
 579	dell_fill_request(&buffer, 0, 0, 0, 0);
 580	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 581	if (ret)
 582		return ret;
 583	status = buffer.output[1];
 
 
 
 
 
 
 
 584
 585	dell_fill_request(&buffer, 0x2, 0, 0, 0);
 586	hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 587	if (hwswitch_ret)
 588		return hwswitch_ret;
 589	hwswitch_state = buffer.output[1];
 590
 591	seq_printf(s, "return:\t%d\n", ret);
 592	seq_printf(s, "status:\t0x%X\n", status);
 593	seq_printf(s, "Bit 0 : Hardware switch supported:   %lu\n",
 594		   status & BIT(0));
 595	seq_printf(s, "Bit 1 : Wifi locator supported:      %lu\n",
 596		  (status & BIT(1)) >> 1);
 597	seq_printf(s, "Bit 2 : Wifi is supported:           %lu\n",
 598		  (status & BIT(2)) >> 2);
 599	seq_printf(s, "Bit 3 : Bluetooth is supported:      %lu\n",
 600		  (status & BIT(3)) >> 3);
 601	seq_printf(s, "Bit 4 : WWAN is supported:           %lu\n",
 602		  (status & BIT(4)) >> 4);
 603	seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
 604		  (status & BIT(5)) >> 5);
 605	seq_printf(s, "Bit 6 : UWB supported:               %lu\n",
 606		  (status & BIT(6)) >> 6);
 607	seq_printf(s, "Bit 7 : WiGig supported:             %lu\n",
 608		  (status & BIT(7)) >> 7);
 609	seq_printf(s, "Bit 8 : Wifi is installed:           %lu\n",
 610		  (status & BIT(8)) >> 8);
 611	seq_printf(s, "Bit 9 : Bluetooth is installed:      %lu\n",
 612		  (status & BIT(9)) >> 9);
 613	seq_printf(s, "Bit 10: WWAN is installed:           %lu\n",
 614		  (status & BIT(10)) >> 10);
 615	seq_printf(s, "Bit 11: UWB installed:               %lu\n",
 616		  (status & BIT(11)) >> 11);
 617	seq_printf(s, "Bit 12: WiGig installed:             %lu\n",
 618		  (status & BIT(12)) >> 12);
 619
 620	seq_printf(s, "Bit 16: Hardware switch is on:       %lu\n",
 621		  (status & BIT(16)) >> 16);
 622	seq_printf(s, "Bit 17: Wifi is blocked:             %lu\n",
 623		  (status & BIT(17)) >> 17);
 624	seq_printf(s, "Bit 18: Bluetooth is blocked:        %lu\n",
 625		  (status & BIT(18)) >> 18);
 626	seq_printf(s, "Bit 19: WWAN is blocked:             %lu\n",
 627		  (status & BIT(19)) >> 19);
 628	seq_printf(s, "Bit 20: UWB is blocked:              %lu\n",
 629		  (status & BIT(20)) >> 20);
 630	seq_printf(s, "Bit 21: WiGig is blocked:            %lu\n",
 631		  (status & BIT(21)) >> 21);
 632
 633	seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
 634	seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
 635	seq_printf(s, "Bit 0 : Wifi controlled by switch:      %lu\n",
 636		   hwswitch_state & BIT(0));
 637	seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
 638		   (hwswitch_state & BIT(1)) >> 1);
 639	seq_printf(s, "Bit 2 : WWAN controlled by switch:      %lu\n",
 640		   (hwswitch_state & BIT(2)) >> 2);
 641	seq_printf(s, "Bit 3 : UWB controlled by switch:       %lu\n",
 642		   (hwswitch_state & BIT(3)) >> 3);
 643	seq_printf(s, "Bit 4 : WiGig controlled by switch:     %lu\n",
 644		   (hwswitch_state & BIT(4)) >> 4);
 645	seq_printf(s, "Bit 7 : Wireless switch config locked:  %lu\n",
 646		   (hwswitch_state & BIT(7)) >> 7);
 647	seq_printf(s, "Bit 8 : Wifi locator enabled:           %lu\n",
 648		   (hwswitch_state & BIT(8)) >> 8);
 649	seq_printf(s, "Bit 15: Wifi locator setting locked:    %lu\n",
 650		   (hwswitch_state & BIT(15)) >> 15);
 651
 652	return 0;
 653}
 654DEFINE_SHOW_ATTRIBUTE(dell_debugfs);
 
 
 
 
 
 
 
 
 
 
 
 
 655
 656static void dell_update_rfkill(struct work_struct *ignored)
 657{
 658	struct calling_interface_buffer buffer;
 659	int hwswitch = 0;
 660	int status;
 661	int ret;
 662
 663	dell_fill_request(&buffer, 0, 0, 0, 0);
 664	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 665	status = buffer.output[1];
 
 
 666
 667	if (ret != 0)
 668		return;
 669
 670	dell_fill_request(&buffer, 0x2, 0, 0, 0);
 671	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 
 
 
 672
 673	if (ret == 0 && (status & BIT(0)))
 674		hwswitch = buffer.output[1];
 675
 676	if (wifi_rfkill) {
 677		dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
 678		dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
 679	}
 680	if (bluetooth_rfkill) {
 681		dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
 682					    hwswitch);
 683		dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
 
 684	}
 685	if (wwan_rfkill) {
 686		dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
 687		dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
 688	}
 
 
 
 689}
 690static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
 691
 692static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
 693			      struct serio *port)
 694{
 695	static bool extended;
 696
 697	if (str & I8042_STR_AUXDATA)
 698		return false;
 699
 700	if (unlikely(data == 0xe0)) {
 701		extended = true;
 702		return false;
 703	} else if (unlikely(extended)) {
 704		switch (data) {
 705		case 0x8:
 706			schedule_delayed_work(&dell_rfkill_work,
 707					      round_jiffies_relative(HZ / 4));
 708			break;
 709		}
 710		extended = false;
 711	}
 712
 713	return false;
 714}
 715
 716static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
 717static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
 718
 719static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
 720					  unsigned long action, void *data)
 721{
 722	schedule_delayed_work(&dell_rfkill_work, 0);
 723	return NOTIFY_OK;
 724}
 725
 726static struct notifier_block dell_laptop_rbtn_notifier = {
 727	.notifier_call = dell_laptop_rbtn_notifier_call,
 728};
 729
 730static int __init dell_setup_rfkill(void)
 731{
 732	struct calling_interface_buffer buffer;
 733	int status, ret, whitelisted;
 734	const char *product;
 735
 736	/*
 737	 * rfkill support causes trouble on various models, mostly Inspirons.
 738	 * So we whitelist certain series, and don't support rfkill on others.
 739	 */
 740	whitelisted = 0;
 741	product = dmi_get_system_info(DMI_PRODUCT_NAME);
 742	if (product &&  (strncmp(product, "Latitude", 8) == 0 ||
 743			 strncmp(product, "Precision", 9) == 0))
 744		whitelisted = 1;
 745	if (!force_rfkill && !whitelisted)
 746		return 0;
 747
 748	dell_fill_request(&buffer, 0, 0, 0, 0);
 749	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
 750	status = buffer.output[1];
 
 
 751
 752	/* dell wireless info smbios call is not supported */
 753	if (ret != 0)
 754		return 0;
 755
 756	/* rfkill is only tested on laptops with a hwswitch */
 757	if (!(status & BIT(0)) && !force_rfkill)
 758		return 0;
 759
 760	if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
 761		wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
 762					   RFKILL_TYPE_WLAN,
 763					   &dell_rfkill_ops, (void *) 1);
 764		if (!wifi_rfkill) {
 765			ret = -ENOMEM;
 766			goto err_wifi;
 767		}
 768		ret = rfkill_register(wifi_rfkill);
 769		if (ret)
 770			goto err_wifi;
 771	}
 772
 773	if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
 774		bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
 775						&platform_device->dev,
 776						RFKILL_TYPE_BLUETOOTH,
 777						&dell_rfkill_ops, (void *) 2);
 778		if (!bluetooth_rfkill) {
 779			ret = -ENOMEM;
 780			goto err_bluetooth;
 781		}
 782		ret = rfkill_register(bluetooth_rfkill);
 783		if (ret)
 784			goto err_bluetooth;
 785	}
 786
 787	if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
 788		wwan_rfkill = rfkill_alloc("dell-wwan",
 789					   &platform_device->dev,
 790					   RFKILL_TYPE_WWAN,
 791					   &dell_rfkill_ops, (void *) 3);
 792		if (!wwan_rfkill) {
 793			ret = -ENOMEM;
 794			goto err_wwan;
 795		}
 796		ret = rfkill_register(wwan_rfkill);
 797		if (ret)
 798			goto err_wwan;
 799	}
 800
 801	/*
 802	 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
 803	 * which can receive events from HW slider switch.
 804	 *
 805	 * Dell SMBIOS on whitelisted models supports controlling radio devices
 806	 * but does not support receiving HW button switch events. We can use
 807	 * i8042 filter hook function to receive keyboard data and handle
 808	 * keycode for HW button.
 809	 *
 810	 * So if it is possible we will use Dell Airplane Mode Switch ACPI
 811	 * driver for receiving HW events and Dell SMBIOS for setting rfkill
 812	 * states. If ACPI driver or device is not available we will fallback to
 813	 * i8042 filter hook function.
 814	 *
 815	 * To prevent duplicate rfkill devices which control and do same thing,
 816	 * dell-rbtn driver will automatically remove its own rfkill devices
 817	 * once function dell_rbtn_notifier_register() is called.
 818	 */
 819
 820	dell_rbtn_notifier_register_func =
 821		symbol_request(dell_rbtn_notifier_register);
 822	if (dell_rbtn_notifier_register_func) {
 823		dell_rbtn_notifier_unregister_func =
 824			symbol_request(dell_rbtn_notifier_unregister);
 825		if (!dell_rbtn_notifier_unregister_func) {
 826			symbol_put(dell_rbtn_notifier_register);
 827			dell_rbtn_notifier_register_func = NULL;
 828		}
 829	}
 830
 831	if (dell_rbtn_notifier_register_func) {
 832		ret = dell_rbtn_notifier_register_func(
 833			&dell_laptop_rbtn_notifier);
 834		symbol_put(dell_rbtn_notifier_register);
 835		dell_rbtn_notifier_register_func = NULL;
 836		if (ret != 0) {
 837			symbol_put(dell_rbtn_notifier_unregister);
 838			dell_rbtn_notifier_unregister_func = NULL;
 839		}
 840	} else {
 841		pr_info("Symbols from dell-rbtn acpi driver are not available\n");
 842		ret = -ENODEV;
 843	}
 844
 845	if (ret == 0) {
 846		pr_info("Using dell-rbtn acpi driver for receiving events\n");
 847	} else if (ret != -ENODEV) {
 848		pr_warn("Unable to register dell rbtn notifier\n");
 849		goto err_filter;
 850	} else {
 851		ret = i8042_install_filter(dell_laptop_i8042_filter);
 852		if (ret) {
 853			pr_warn("Unable to install key filter\n");
 854			goto err_filter;
 855		}
 856		pr_info("Using i8042 filter function for receiving events\n");
 857	}
 858
 859	return 0;
 860err_filter:
 861	if (wwan_rfkill)
 862		rfkill_unregister(wwan_rfkill);
 863err_wwan:
 864	rfkill_destroy(wwan_rfkill);
 865	if (bluetooth_rfkill)
 866		rfkill_unregister(bluetooth_rfkill);
 867err_bluetooth:
 868	rfkill_destroy(bluetooth_rfkill);
 869	if (wifi_rfkill)
 870		rfkill_unregister(wifi_rfkill);
 871err_wifi:
 872	rfkill_destroy(wifi_rfkill);
 873
 874	return ret;
 875}
 876
 877static void dell_cleanup_rfkill(void)
 878{
 879	if (dell_rbtn_notifier_unregister_func) {
 880		dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
 881		symbol_put(dell_rbtn_notifier_unregister);
 882		dell_rbtn_notifier_unregister_func = NULL;
 883	} else {
 884		i8042_remove_filter(dell_laptop_i8042_filter);
 885	}
 886	cancel_delayed_work_sync(&dell_rfkill_work);
 887	if (wifi_rfkill) {
 888		rfkill_unregister(wifi_rfkill);
 889		rfkill_destroy(wifi_rfkill);
 890	}
 891	if (bluetooth_rfkill) {
 892		rfkill_unregister(bluetooth_rfkill);
 893		rfkill_destroy(bluetooth_rfkill);
 894	}
 895	if (wwan_rfkill) {
 896		rfkill_unregister(wwan_rfkill);
 897		rfkill_destroy(wwan_rfkill);
 898	}
 899}
 900
 901static int dell_send_intensity(struct backlight_device *bd)
 902{
 903	struct calling_interface_buffer buffer;
 904	struct calling_interface_token *token;
 905	int ret;
 906
 907	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
 908	if (!token)
 909		return -ENODEV;
 910
 911	dell_fill_request(&buffer,
 912			   token->location, bd->props.brightness, 0, 0);
 
 
 913	if (power_supply_is_system_supplied() > 0)
 914		ret = dell_send_request(&buffer,
 915					CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
 916	else
 917		ret = dell_send_request(&buffer,
 918					CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
 919
 
 
 
 920	return ret;
 921}
 922
 923static int dell_get_intensity(struct backlight_device *bd)
 924{
 925	struct calling_interface_buffer buffer;
 926	struct calling_interface_token *token;
 927	int ret;
 928
 929	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
 930	if (!token)
 931		return -ENODEV;
 932
 933	dell_fill_request(&buffer, token->location, 0, 0, 0);
 
 
 934	if (power_supply_is_system_supplied() > 0)
 935		ret = dell_send_request(&buffer,
 936					CLASS_TOKEN_READ, SELECT_TOKEN_AC);
 937	else
 938		ret = dell_send_request(&buffer,
 939					CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
 940
 941	if (ret == 0)
 942		ret = buffer.output[1];
 
 
 943
 
 944	return ret;
 945}
 946
 947static const struct backlight_ops dell_ops = {
 948	.get_brightness = dell_get_intensity,
 949	.update_status  = dell_send_intensity,
 950};
 951
 952static void touchpad_led_on(void)
 953{
 954	int command = 0x97;
 955	char data = 1;
 956	i8042_command(&data, command | 1 << 12);
 957}
 958
 959static void touchpad_led_off(void)
 960{
 961	int command = 0x97;
 962	char data = 2;
 963	i8042_command(&data, command | 1 << 12);
 964}
 965
 966static void touchpad_led_set(struct led_classdev *led_cdev,
 967	enum led_brightness value)
 968{
 969	if (value > 0)
 970		touchpad_led_on();
 971	else
 972		touchpad_led_off();
 973}
 974
 975static struct led_classdev touchpad_led = {
 976	.name = "dell-laptop::touchpad",
 977	.brightness_set = touchpad_led_set,
 978	.flags = LED_CORE_SUSPENDRESUME,
 979};
 980
 981static int __init touchpad_led_init(struct device *dev)
 982{
 983	return led_classdev_register(dev, &touchpad_led);
 984}
 985
 986static void touchpad_led_exit(void)
 987{
 988	led_classdev_unregister(&touchpad_led);
 989}
 990
 991/*
 992 * Derived from information in smbios-keyboard-ctl:
 993 *
 994 * cbClass 4
 995 * cbSelect 11
 996 * Keyboard illumination
 997 * cbArg1 determines the function to be performed
 998 *
 999 * cbArg1 0x0 = Get Feature Information
1000 *  cbRES1         Standard return codes (0, -1, -2)
1001 *  cbRES2, word0  Bitmap of user-selectable modes
1002 *     bit 0     Always off (All systems)
1003 *     bit 1     Always on (Travis ATG, Siberia)
1004 *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1005 *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1006 *     bit 4     Auto: Input-activity-based On; input-activity based Off
1007 *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1008 *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1009 *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1010 *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1011 *     bits 9-15 Reserved for future use
1012 *  cbRES2, byte2  Reserved for future use
1013 *  cbRES2, byte3  Keyboard illumination type
1014 *     0         Reserved
1015 *     1         Tasklight
1016 *     2         Backlight
1017 *     3-255     Reserved for future use
1018 *  cbRES3, byte0  Supported auto keyboard illumination trigger bitmap.
1019 *     bit 0     Any keystroke
1020 *     bit 1     Touchpad activity
1021 *     bit 2     Pointing stick
1022 *     bit 3     Any mouse
1023 *     bits 4-7  Reserved for future use
1024 *  cbRES3, byte1  Supported timeout unit bitmap
1025 *     bit 0     Seconds
1026 *     bit 1     Minutes
1027 *     bit 2     Hours
1028 *     bit 3     Days
1029 *     bits 4-7  Reserved for future use
1030 *  cbRES3, byte2  Number of keyboard light brightness levels
1031 *  cbRES4, byte0  Maximum acceptable seconds value (0 if seconds not supported).
1032 *  cbRES4, byte1  Maximum acceptable minutes value (0 if minutes not supported).
1033 *  cbRES4, byte2  Maximum acceptable hours value (0 if hours not supported).
1034 *  cbRES4, byte3  Maximum acceptable days value (0 if days not supported)
1035 *
1036 * cbArg1 0x1 = Get Current State
1037 *  cbRES1         Standard return codes (0, -1, -2)
1038 *  cbRES2, word0  Bitmap of current mode state
1039 *     bit 0     Always off (All systems)
1040 *     bit 1     Always on (Travis ATG, Siberia)
1041 *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1042 *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1043 *     bit 4     Auto: Input-activity-based On; input-activity based Off
1044 *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1045 *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1046 *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1047 *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1048 *     bits 9-15 Reserved for future use
1049 *     Note: Only One bit can be set
1050 *  cbRES2, byte2  Currently active auto keyboard illumination triggers.
1051 *     bit 0     Any keystroke
1052 *     bit 1     Touchpad activity
1053 *     bit 2     Pointing stick
1054 *     bit 3     Any mouse
1055 *     bits 4-7  Reserved for future use
1056 *  cbRES2, byte3  Current Timeout on battery
1057 *     bits 7:6  Timeout units indicator:
1058 *     00b       Seconds
1059 *     01b       Minutes
1060 *     10b       Hours
1061 *     11b       Days
1062 *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1063 *     NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1064 *     are set upon return from the [Get feature information] call.
1065 *  cbRES3, byte0  Current setting of ALS value that turns the light on or off.
1066 *  cbRES3, byte1  Current ALS reading
1067 *  cbRES3, byte2  Current keyboard light level.
1068 *  cbRES3, byte3  Current timeout on AC Power
1069 *     bits 7:6  Timeout units indicator:
1070 *     00b       Seconds
1071 *     01b       Minutes
1072 *     10b       Hours
1073 *     11b       Days
1074 *     Bits 5:0  Timeout value (0-63) in sec/min/hr/day
1075 *     NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1076 *     are set upon return from the upon return from the [Get Feature information] call.
1077 *
1078 * cbArg1 0x2 = Set New State
1079 *  cbRES1         Standard return codes (0, -1, -2)
1080 *  cbArg2, word0  Bitmap of current mode state
1081 *     bit 0     Always off (All systems)
1082 *     bit 1     Always on (Travis ATG, Siberia)
1083 *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1084 *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1085 *     bit 4     Auto: Input-activity-based On; input-activity based Off
1086 *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1087 *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1088 *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1089 *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1090 *     bits 9-15 Reserved for future use
1091 *     Note: Only One bit can be set
1092 *  cbArg2, byte2  Desired auto keyboard illumination triggers. Must remain inactive to allow
1093 *                 keyboard to turn off automatically.
1094 *     bit 0     Any keystroke
1095 *     bit 1     Touchpad activity
1096 *     bit 2     Pointing stick
1097 *     bit 3     Any mouse
1098 *     bits 4-7  Reserved for future use
1099 *  cbArg2, byte3  Desired Timeout on battery
1100 *     bits 7:6  Timeout units indicator:
1101 *     00b       Seconds
1102 *     01b       Minutes
1103 *     10b       Hours
1104 *     11b       Days
1105 *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1106 *  cbArg3, byte0  Desired setting of ALS value that turns the light on or off.
1107 *  cbArg3, byte2  Desired keyboard light level.
1108 *  cbArg3, byte3  Desired Timeout on AC power
1109 *     bits 7:6  Timeout units indicator:
1110 *     00b       Seconds
1111 *     01b       Minutes
1112 *     10b       Hours
1113 *     11b       Days
1114 *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1115 */
1116
1117
1118enum kbd_timeout_unit {
1119	KBD_TIMEOUT_SECONDS = 0,
1120	KBD_TIMEOUT_MINUTES,
1121	KBD_TIMEOUT_HOURS,
1122	KBD_TIMEOUT_DAYS,
1123};
1124
1125enum kbd_mode_bit {
1126	KBD_MODE_BIT_OFF = 0,
1127	KBD_MODE_BIT_ON,
1128	KBD_MODE_BIT_ALS,
1129	KBD_MODE_BIT_TRIGGER_ALS,
1130	KBD_MODE_BIT_TRIGGER,
1131	KBD_MODE_BIT_TRIGGER_25,
1132	KBD_MODE_BIT_TRIGGER_50,
1133	KBD_MODE_BIT_TRIGGER_75,
1134	KBD_MODE_BIT_TRIGGER_100,
1135};
1136
1137#define kbd_is_als_mode_bit(bit) \
1138	((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1139#define kbd_is_trigger_mode_bit(bit) \
1140	((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1141#define kbd_is_level_mode_bit(bit) \
1142	((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1143
1144struct kbd_info {
1145	u16 modes;
1146	u8 type;
1147	u8 triggers;
1148	u8 levels;
1149	u8 seconds;
1150	u8 minutes;
1151	u8 hours;
1152	u8 days;
1153};
1154
1155struct kbd_state {
1156	u8 mode_bit;
1157	u8 triggers;
1158	u8 timeout_value;
1159	u8 timeout_unit;
1160	u8 timeout_value_ac;
1161	u8 timeout_unit_ac;
1162	u8 als_setting;
1163	u8 als_value;
1164	u8 level;
1165};
1166
1167static const int kbd_tokens[] = {
1168	KBD_LED_OFF_TOKEN,
1169	KBD_LED_AUTO_25_TOKEN,
1170	KBD_LED_AUTO_50_TOKEN,
1171	KBD_LED_AUTO_75_TOKEN,
1172	KBD_LED_AUTO_100_TOKEN,
1173	KBD_LED_ON_TOKEN,
1174};
1175
1176static u16 kbd_token_bits;
1177
1178static struct kbd_info kbd_info;
1179static bool kbd_als_supported;
1180static bool kbd_triggers_supported;
1181static bool kbd_timeout_ac_supported;
1182
1183static u8 kbd_mode_levels[16];
1184static int kbd_mode_levels_count;
1185
1186static u8 kbd_previous_level;
1187static u8 kbd_previous_mode_bit;
1188
1189static bool kbd_led_present;
1190static DEFINE_MUTEX(kbd_led_mutex);
1191static enum led_brightness kbd_led_level;
1192
1193/*
1194 * NOTE: there are three ways to set the keyboard backlight level.
1195 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1196 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1197 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1198 *
1199 * There are laptops which support only one of these methods. If we want to
1200 * support as many machines as possible we need to implement all three methods.
1201 * The first two methods use the kbd_state structure. The third uses SMBIOS
1202 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1203 * keyboard backlight level via kbd_state.level.
1204 */
1205
1206static int kbd_get_info(struct kbd_info *info)
1207{
1208	struct calling_interface_buffer buffer;
1209	u8 units;
1210	int ret;
1211
1212	dell_fill_request(&buffer, 0, 0, 0, 0);
1213	ret = dell_send_request(&buffer,
1214				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1215	if (ret)
1216		return ret;
1217
1218	info->modes = buffer.output[1] & 0xFFFF;
1219	info->type = (buffer.output[1] >> 24) & 0xFF;
1220	info->triggers = buffer.output[2] & 0xFF;
1221	units = (buffer.output[2] >> 8) & 0xFF;
1222	info->levels = (buffer.output[2] >> 16) & 0xFF;
 
 
 
1223
1224	if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1225		info->levels--;
 
 
 
1226
1227	if (units & BIT(0))
1228		info->seconds = (buffer.output[3] >> 0) & 0xFF;
1229	if (units & BIT(1))
1230		info->minutes = (buffer.output[3] >> 8) & 0xFF;
1231	if (units & BIT(2))
1232		info->hours = (buffer.output[3] >> 16) & 0xFF;
1233	if (units & BIT(3))
1234		info->days = (buffer.output[3] >> 24) & 0xFF;
1235
 
 
1236	return ret;
1237}
1238
1239static unsigned int kbd_get_max_level(void)
1240{
1241	if (kbd_info.levels != 0)
1242		return kbd_info.levels;
1243	if (kbd_mode_levels_count > 0)
1244		return kbd_mode_levels_count - 1;
1245	return 0;
1246}
1247
1248static int kbd_get_level(struct kbd_state *state)
1249{
1250	int i;
1251
1252	if (kbd_info.levels != 0)
1253		return state->level;
1254
1255	if (kbd_mode_levels_count > 0) {
1256		for (i = 0; i < kbd_mode_levels_count; ++i)
1257			if (kbd_mode_levels[i] == state->mode_bit)
1258				return i;
1259		return 0;
1260	}
1261
1262	return -EINVAL;
1263}
1264
1265static int kbd_set_level(struct kbd_state *state, u8 level)
1266{
1267	if (kbd_info.levels != 0) {
1268		if (level != 0)
1269			kbd_previous_level = level;
1270		if (state->level == level)
1271			return 0;
1272		state->level = level;
1273		if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1274			state->mode_bit = kbd_previous_mode_bit;
1275		else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1276			kbd_previous_mode_bit = state->mode_bit;
1277			state->mode_bit = KBD_MODE_BIT_OFF;
1278		}
1279		return 0;
1280	}
1281
1282	if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1283		if (level != 0)
1284			kbd_previous_level = level;
1285		state->mode_bit = kbd_mode_levels[level];
1286		return 0;
1287	}
1288
1289	return -EINVAL;
1290}
1291
1292static int kbd_get_state(struct kbd_state *state)
1293{
1294	struct calling_interface_buffer buffer;
1295	int ret;
1296
1297	dell_fill_request(&buffer, 0x1, 0, 0, 0);
1298	ret = dell_send_request(&buffer,
1299				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1300	if (ret)
1301		return ret;
1302
1303	state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
 
 
 
 
 
 
 
 
 
1304	if (state->mode_bit != 0)
1305		state->mode_bit--;
1306
1307	state->triggers = (buffer.output[1] >> 16) & 0xFF;
1308	state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1309	state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1310	state->als_setting = buffer.output[2] & 0xFF;
1311	state->als_value = (buffer.output[2] >> 8) & 0xFF;
1312	state->level = (buffer.output[2] >> 16) & 0xFF;
1313	state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1314	state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1315
 
 
1316	return ret;
1317}
1318
1319static int kbd_set_state(struct kbd_state *state)
1320{
1321	struct calling_interface_buffer buffer;
1322	int ret;
1323	u32 input1;
1324	u32 input2;
1325
1326	input1 = BIT(state->mode_bit) & 0xFFFF;
1327	input1 |= (state->triggers & 0xFF) << 16;
1328	input1 |= (state->timeout_value & 0x3F) << 24;
1329	input1 |= (state->timeout_unit & 0x3) << 30;
1330	input2 = state->als_setting & 0xFF;
1331	input2 |= (state->level & 0xFF) << 16;
1332	input2 |= (state->timeout_value_ac & 0x3F) << 24;
1333	input2 |= (state->timeout_unit_ac & 0x3) << 30;
1334	dell_fill_request(&buffer, 0x2, input1, input2, 0);
1335	ret = dell_send_request(&buffer,
1336				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1337
1338	return ret;
1339}
1340
1341static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1342{
1343	int ret;
1344
1345	ret = kbd_set_state(state);
1346	if (ret == 0)
1347		return 0;
1348
1349	/*
1350	 * When setting the new state fails,try to restore the previous one.
1351	 * This is needed on some machines where BIOS sets a default state when
1352	 * setting a new state fails. This default state could be all off.
1353	 */
1354
1355	if (kbd_set_state(old))
1356		pr_err("Setting old previous keyboard state failed\n");
1357
1358	return ret;
1359}
1360
1361static int kbd_set_token_bit(u8 bit)
1362{
1363	struct calling_interface_buffer buffer;
1364	struct calling_interface_token *token;
1365	int ret;
1366
1367	if (bit >= ARRAY_SIZE(kbd_tokens))
1368		return -EINVAL;
1369
1370	token = dell_smbios_find_token(kbd_tokens[bit]);
1371	if (!token)
1372		return -EINVAL;
1373
1374	dell_fill_request(&buffer, token->location, token->value, 0, 0);
1375	ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
 
 
 
 
1376
1377	return ret;
1378}
1379
1380static int kbd_get_token_bit(u8 bit)
1381{
1382	struct calling_interface_buffer buffer;
1383	struct calling_interface_token *token;
1384	int ret;
1385	int val;
1386
1387	if (bit >= ARRAY_SIZE(kbd_tokens))
1388		return -EINVAL;
1389
1390	token = dell_smbios_find_token(kbd_tokens[bit]);
1391	if (!token)
1392		return -EINVAL;
1393
1394	dell_fill_request(&buffer, token->location, 0, 0, 0);
1395	ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1396	val = buffer.output[1];
 
 
 
1397
1398	if (ret)
1399		return ret;
1400
1401	return (val == token->value);
1402}
1403
1404static int kbd_get_first_active_token_bit(void)
1405{
1406	int i;
1407	int ret;
1408
1409	for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1410		ret = kbd_get_token_bit(i);
1411		if (ret == 1)
1412			return i;
1413	}
1414
1415	return ret;
1416}
1417
1418static int kbd_get_valid_token_counts(void)
1419{
1420	return hweight16(kbd_token_bits);
1421}
1422
1423static inline int kbd_init_info(void)
1424{
1425	struct kbd_state state;
1426	int ret;
1427	int i;
1428
1429	ret = kbd_get_info(&kbd_info);
1430	if (ret)
1431		return ret;
1432
1433	/* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1434	 *       timeout value which is shared for both battery and AC power
1435	 *       settings. So do not try to set AC values on old models.
1436	 */
1437	if ((quirks && quirks->kbd_missing_ac_tag) ||
1438	    dell_smbios_find_token(KBD_LED_AC_TOKEN))
1439		kbd_timeout_ac_supported = true;
1440
1441	kbd_get_state(&state);
1442
1443	/* NOTE: timeout value is stored in 6 bits so max value is 63 */
1444	if (kbd_info.seconds > 63)
1445		kbd_info.seconds = 63;
1446	if (kbd_info.minutes > 63)
1447		kbd_info.minutes = 63;
1448	if (kbd_info.hours > 63)
1449		kbd_info.hours = 63;
1450	if (kbd_info.days > 63)
1451		kbd_info.days = 63;
1452
1453	/* NOTE: On tested machines ON mode did not work and caused
1454	 *       problems (turned backlight off) so do not use it
1455	 */
1456	kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1457
1458	kbd_previous_level = kbd_get_level(&state);
1459	kbd_previous_mode_bit = state.mode_bit;
1460
1461	if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1462		kbd_previous_level = 1;
1463
1464	if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1465		kbd_previous_mode_bit =
1466			ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1467		if (kbd_previous_mode_bit != 0)
1468			kbd_previous_mode_bit--;
1469	}
1470
1471	if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1472			      BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1473		kbd_als_supported = true;
1474
1475	if (kbd_info.modes & (
1476	    BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1477	    BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1478	    BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1479	   ))
1480		kbd_triggers_supported = true;
1481
1482	/* kbd_mode_levels[0] is reserved, see below */
1483	for (i = 0; i < 16; ++i)
1484		if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1485			kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1486
1487	/*
1488	 * Find the first supported mode and assign to kbd_mode_levels[0].
1489	 * This should be 0 (off), but we cannot depend on the BIOS to
1490	 * support 0.
1491	 */
1492	if (kbd_mode_levels_count > 0) {
1493		for (i = 0; i < 16; ++i) {
1494			if (BIT(i) & kbd_info.modes) {
1495				kbd_mode_levels[0] = i;
1496				break;
1497			}
1498		}
1499		kbd_mode_levels_count++;
1500	}
1501
1502	return 0;
1503
1504}
1505
1506static inline void kbd_init_tokens(void)
1507{
1508	int i;
1509
1510	for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1511		if (dell_smbios_find_token(kbd_tokens[i]))
1512			kbd_token_bits |= BIT(i);
1513}
1514
1515static void kbd_init(void)
1516{
1517	int ret;
1518
1519	if (quirks && quirks->kbd_led_not_present)
1520		return;
1521
1522	ret = kbd_init_info();
1523	kbd_init_tokens();
1524
1525	/*
1526	 * Only supports keyboard backlight when it has at least two modes.
1527	 */
1528	if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1529	    || kbd_get_valid_token_counts() >= 2)
1530		kbd_led_present = true;
1531}
1532
1533static ssize_t kbd_led_timeout_store(struct device *dev,
1534				     struct device_attribute *attr,
1535				     const char *buf, size_t count)
1536{
1537	struct kbd_state new_state;
1538	struct kbd_state state;
1539	bool convert;
1540	int value;
1541	int ret;
1542	char ch;
1543	u8 unit;
1544	int i;
1545
1546	ret = sscanf(buf, "%d %c", &value, &ch);
1547	if (ret < 1)
1548		return -EINVAL;
1549	else if (ret == 1)
1550		ch = 's';
1551
1552	if (value < 0)
1553		return -EINVAL;
1554
1555	convert = false;
1556
1557	switch (ch) {
1558	case 's':
1559		if (value > kbd_info.seconds)
1560			convert = true;
1561		unit = KBD_TIMEOUT_SECONDS;
1562		break;
1563	case 'm':
1564		if (value > kbd_info.minutes)
1565			convert = true;
1566		unit = KBD_TIMEOUT_MINUTES;
1567		break;
1568	case 'h':
1569		if (value > kbd_info.hours)
1570			convert = true;
1571		unit = KBD_TIMEOUT_HOURS;
1572		break;
1573	case 'd':
1574		if (value > kbd_info.days)
1575			convert = true;
1576		unit = KBD_TIMEOUT_DAYS;
1577		break;
1578	default:
1579		return -EINVAL;
1580	}
1581
1582	if (quirks && quirks->needs_kbd_timeouts)
1583		convert = true;
1584
1585	if (convert) {
1586		/* Convert value from current units to seconds */
1587		switch (unit) {
1588		case KBD_TIMEOUT_DAYS:
1589			value *= 24;
1590			fallthrough;
1591		case KBD_TIMEOUT_HOURS:
1592			value *= 60;
1593			fallthrough;
1594		case KBD_TIMEOUT_MINUTES:
1595			value *= 60;
1596			unit = KBD_TIMEOUT_SECONDS;
1597		}
1598
1599		if (quirks && quirks->needs_kbd_timeouts) {
1600			for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1601				if (value <= quirks->kbd_timeouts[i]) {
1602					value = quirks->kbd_timeouts[i];
1603					break;
1604				}
1605			}
1606		}
1607
1608		if (value <= kbd_info.seconds && kbd_info.seconds) {
1609			unit = KBD_TIMEOUT_SECONDS;
1610		} else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1611			value /= 60;
1612			unit = KBD_TIMEOUT_MINUTES;
1613		} else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1614			value /= (60 * 60);
1615			unit = KBD_TIMEOUT_HOURS;
1616		} else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1617			value /= (60 * 60 * 24);
1618			unit = KBD_TIMEOUT_DAYS;
1619		} else {
1620			return -EINVAL;
1621		}
1622	}
1623
1624	mutex_lock(&kbd_led_mutex);
1625
1626	ret = kbd_get_state(&state);
1627	if (ret)
1628		goto out;
1629
1630	new_state = state;
1631
1632	if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1633		new_state.timeout_value_ac = value;
1634		new_state.timeout_unit_ac = unit;
1635	} else {
1636		new_state.timeout_value = value;
1637		new_state.timeout_unit = unit;
1638	}
1639
1640	ret = kbd_set_state_safe(&new_state, &state);
1641	if (ret)
1642		goto out;
1643
1644	ret = count;
1645out:
1646	mutex_unlock(&kbd_led_mutex);
1647	return ret;
1648}
1649
1650static ssize_t kbd_led_timeout_show(struct device *dev,
1651				    struct device_attribute *attr, char *buf)
1652{
1653	struct kbd_state state;
1654	int value;
1655	int ret;
1656	int len;
1657	u8 unit;
1658
1659	ret = kbd_get_state(&state);
1660	if (ret)
1661		return ret;
1662
1663	if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1664		value = state.timeout_value_ac;
1665		unit = state.timeout_unit_ac;
1666	} else {
1667		value = state.timeout_value;
1668		unit = state.timeout_unit;
1669	}
1670
1671	len = sprintf(buf, "%d", value);
1672
1673	switch (unit) {
1674	case KBD_TIMEOUT_SECONDS:
1675		return len + sprintf(buf+len, "s\n");
1676	case KBD_TIMEOUT_MINUTES:
1677		return len + sprintf(buf+len, "m\n");
1678	case KBD_TIMEOUT_HOURS:
1679		return len + sprintf(buf+len, "h\n");
1680	case KBD_TIMEOUT_DAYS:
1681		return len + sprintf(buf+len, "d\n");
1682	default:
1683		return -EINVAL;
1684	}
1685
1686	return len;
1687}
1688
1689static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1690		   kbd_led_timeout_show, kbd_led_timeout_store);
1691
1692static const char * const kbd_led_triggers[] = {
1693	"keyboard",
1694	"touchpad",
1695	/*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1696	"mouse",
1697};
1698
1699static ssize_t kbd_led_triggers_store(struct device *dev,
1700				      struct device_attribute *attr,
1701				      const char *buf, size_t count)
1702{
1703	struct kbd_state new_state;
1704	struct kbd_state state;
1705	bool triggers_enabled = false;
1706	int trigger_bit = -1;
1707	char trigger[21];
1708	int i, ret;
1709
1710	ret = sscanf(buf, "%20s", trigger);
1711	if (ret != 1)
1712		return -EINVAL;
1713
1714	if (trigger[0] != '+' && trigger[0] != '-')
1715		return -EINVAL;
1716
1717	mutex_lock(&kbd_led_mutex);
1718
1719	ret = kbd_get_state(&state);
1720	if (ret)
1721		goto out;
1722
1723	if (kbd_triggers_supported)
1724		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1725
1726	if (kbd_triggers_supported) {
1727		for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1728			if (!(kbd_info.triggers & BIT(i)))
1729				continue;
1730			if (!kbd_led_triggers[i])
1731				continue;
1732			if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1733				continue;
1734			if (trigger[0] == '+' &&
1735			    triggers_enabled && (state.triggers & BIT(i))) {
1736				ret = count;
1737				goto out;
1738			}
1739			if (trigger[0] == '-' &&
1740			    (!triggers_enabled || !(state.triggers & BIT(i)))) {
1741				ret = count;
1742				goto out;
1743			}
1744			trigger_bit = i;
1745			break;
1746		}
1747	}
1748
1749	if (trigger_bit == -1) {
1750		ret = -EINVAL;
1751		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1752	}
1753
1754	new_state = state;
1755	if (trigger[0] == '+')
1756		new_state.triggers |= BIT(trigger_bit);
1757	else {
1758		new_state.triggers &= ~BIT(trigger_bit);
1759		/*
1760		 * NOTE: trackstick bit (2) must be disabled when
1761		 *       disabling touchpad bit (1), otherwise touchpad
1762		 *       bit (1) will not be disabled
1763		 */
1764		if (trigger_bit == 1)
1765			new_state.triggers &= ~BIT(2);
1766	}
1767	if ((kbd_info.triggers & new_state.triggers) !=
1768	    new_state.triggers) {
1769		ret = -EINVAL;
1770		goto out;
1771	}
1772	if (new_state.triggers && !triggers_enabled) {
1773		new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1774		kbd_set_level(&new_state, kbd_previous_level);
1775	} else if (new_state.triggers == 0) {
1776		kbd_set_level(&new_state, 0);
1777	}
1778	if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1779		ret = -EINVAL;
1780		goto out;
1781	}
1782	ret = kbd_set_state_safe(&new_state, &state);
1783	if (ret)
1784		goto out;
1785	if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1786		kbd_previous_mode_bit = new_state.mode_bit;
1787	ret = count;
1788out:
1789	mutex_unlock(&kbd_led_mutex);
1790	return ret;
1791}
1792
1793static ssize_t kbd_led_triggers_show(struct device *dev,
1794				     struct device_attribute *attr, char *buf)
1795{
1796	struct kbd_state state;
1797	bool triggers_enabled;
1798	int level, i, ret;
1799	int len = 0;
1800
1801	ret = kbd_get_state(&state);
1802	if (ret)
1803		return ret;
1804
1805	len = 0;
1806
1807	if (kbd_triggers_supported) {
1808		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1809		level = kbd_get_level(&state);
1810		for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1811			if (!(kbd_info.triggers & BIT(i)))
1812				continue;
1813			if (!kbd_led_triggers[i])
1814				continue;
1815			if ((triggers_enabled || level <= 0) &&
1816			    (state.triggers & BIT(i)))
1817				buf[len++] = '+';
1818			else
1819				buf[len++] = '-';
1820			len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1821		}
1822	}
1823
1824	if (len)
1825		buf[len - 1] = '\n';
1826
1827	return len;
1828}
1829
1830static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1831		   kbd_led_triggers_show, kbd_led_triggers_store);
1832
1833static ssize_t kbd_led_als_enabled_store(struct device *dev,
1834					 struct device_attribute *attr,
1835					 const char *buf, size_t count)
1836{
1837	struct kbd_state new_state;
1838	struct kbd_state state;
1839	bool triggers_enabled = false;
1840	int enable;
1841	int ret;
1842
1843	ret = kstrtoint(buf, 0, &enable);
1844	if (ret)
1845		return ret;
1846
1847	mutex_lock(&kbd_led_mutex);
1848
1849	ret = kbd_get_state(&state);
1850	if (ret)
1851		goto out;
1852
1853	if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1854		ret = count;
1855		goto out;
1856	}
1857
1858	new_state = state;
1859
1860	if (kbd_triggers_supported)
1861		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1862
1863	if (enable) {
1864		if (triggers_enabled)
1865			new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1866		else
1867			new_state.mode_bit = KBD_MODE_BIT_ALS;
1868	} else {
1869		if (triggers_enabled) {
1870			new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1871			kbd_set_level(&new_state, kbd_previous_level);
1872		} else {
1873			new_state.mode_bit = KBD_MODE_BIT_ON;
1874		}
1875	}
1876	if (!(kbd_info.modes & BIT(new_state.mode_bit)))  {
1877		ret = -EINVAL;
1878		goto out;
1879	}
1880
1881	ret = kbd_set_state_safe(&new_state, &state);
1882	if (ret)
1883		goto out;
1884	kbd_previous_mode_bit = new_state.mode_bit;
1885
1886	ret = count;
1887out:
1888	mutex_unlock(&kbd_led_mutex);
1889	return ret;
1890}
1891
1892static ssize_t kbd_led_als_enabled_show(struct device *dev,
1893					struct device_attribute *attr,
1894					char *buf)
1895{
1896	struct kbd_state state;
1897	bool enabled = false;
1898	int ret;
1899
1900	ret = kbd_get_state(&state);
1901	if (ret)
1902		return ret;
1903	enabled = kbd_is_als_mode_bit(state.mode_bit);
1904
1905	return sprintf(buf, "%d\n", enabled ? 1 : 0);
1906}
1907
1908static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1909		   kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1910
1911static ssize_t kbd_led_als_setting_store(struct device *dev,
1912					 struct device_attribute *attr,
1913					 const char *buf, size_t count)
1914{
1915	struct kbd_state state;
1916	struct kbd_state new_state;
1917	u8 setting;
1918	int ret;
1919
1920	ret = kstrtou8(buf, 10, &setting);
1921	if (ret)
1922		return ret;
1923
1924	mutex_lock(&kbd_led_mutex);
1925
1926	ret = kbd_get_state(&state);
1927	if (ret)
1928		goto out;
1929
1930	new_state = state;
1931	new_state.als_setting = setting;
1932
1933	ret = kbd_set_state_safe(&new_state, &state);
1934	if (ret)
1935		goto out;
1936
1937	ret = count;
1938out:
1939	mutex_unlock(&kbd_led_mutex);
1940	return ret;
1941}
1942
1943static ssize_t kbd_led_als_setting_show(struct device *dev,
1944					struct device_attribute *attr,
1945					char *buf)
1946{
1947	struct kbd_state state;
1948	int ret;
1949
1950	ret = kbd_get_state(&state);
1951	if (ret)
1952		return ret;
1953
1954	return sprintf(buf, "%d\n", state.als_setting);
1955}
1956
1957static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1958		   kbd_led_als_setting_show, kbd_led_als_setting_store);
1959
1960static struct attribute *kbd_led_attrs[] = {
1961	&dev_attr_stop_timeout.attr,
1962	&dev_attr_start_triggers.attr,
1963	NULL,
1964};
1965
1966static const struct attribute_group kbd_led_group = {
1967	.attrs = kbd_led_attrs,
1968};
1969
1970static struct attribute *kbd_led_als_attrs[] = {
1971	&dev_attr_als_enabled.attr,
1972	&dev_attr_als_setting.attr,
1973	NULL,
1974};
1975
1976static const struct attribute_group kbd_led_als_group = {
1977	.attrs = kbd_led_als_attrs,
1978};
1979
1980static const struct attribute_group *kbd_led_groups[] = {
1981	&kbd_led_group,
1982	&kbd_led_als_group,
1983	NULL,
1984};
1985
1986static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1987{
1988	int ret;
1989	u16 num;
1990	struct kbd_state state;
1991
1992	if (kbd_get_max_level()) {
1993		ret = kbd_get_state(&state);
1994		if (ret)
1995			return 0;
1996		ret = kbd_get_level(&state);
1997		if (ret < 0)
1998			return 0;
1999		return ret;
2000	}
2001
2002	if (kbd_get_valid_token_counts()) {
2003		ret = kbd_get_first_active_token_bit();
2004		if (ret < 0)
2005			return 0;
2006		for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
2007			num &= num - 1; /* clear the first bit set */
2008		if (num == 0)
2009			return 0;
2010		return ffs(num) - 1;
2011	}
2012
2013	pr_warn("Keyboard brightness level control not supported\n");
2014	return 0;
2015}
2016
2017static int kbd_led_level_set(struct led_classdev *led_cdev,
2018			     enum led_brightness value)
2019{
2020	enum led_brightness new_value = value;
2021	struct kbd_state state;
2022	struct kbd_state new_state;
2023	u16 num;
2024	int ret;
2025
2026	mutex_lock(&kbd_led_mutex);
2027
2028	if (kbd_get_max_level()) {
2029		ret = kbd_get_state(&state);
2030		if (ret)
2031			goto out;
2032		new_state = state;
2033		ret = kbd_set_level(&new_state, value);
2034		if (ret)
2035			goto out;
2036		ret = kbd_set_state_safe(&new_state, &state);
2037	} else if (kbd_get_valid_token_counts()) {
 
 
2038		for (num = kbd_token_bits; num != 0 && value > 0; --value)
2039			num &= num - 1; /* clear the first bit set */
2040		if (num == 0)
2041			ret = 0;
2042		else
2043			ret = kbd_set_token_bit(ffs(num) - 1);
2044	} else {
2045		pr_warn("Keyboard brightness level control not supported\n");
2046		ret = -ENXIO;
2047	}
2048
2049out:
2050	if (ret == 0)
2051		kbd_led_level = new_value;
2052
2053	mutex_unlock(&kbd_led_mutex);
2054	return ret;
2055}
2056
2057static struct led_classdev kbd_led = {
2058	.name           = "dell::kbd_backlight",
2059	.flags		= LED_BRIGHT_HW_CHANGED,
2060	.brightness_set_blocking = kbd_led_level_set,
2061	.brightness_get = kbd_led_level_get,
2062	.groups         = kbd_led_groups,
2063};
2064
2065static int __init kbd_led_init(struct device *dev)
2066{
2067	int ret;
2068
2069	kbd_init();
2070	if (!kbd_led_present)
2071		return -ENODEV;
2072	if (!kbd_als_supported)
2073		kbd_led_groups[1] = NULL;
2074	kbd_led.max_brightness = kbd_get_max_level();
2075	if (!kbd_led.max_brightness) {
2076		kbd_led.max_brightness = kbd_get_valid_token_counts();
2077		if (kbd_led.max_brightness)
2078			kbd_led.max_brightness--;
2079	}
2080
2081	kbd_led_level = kbd_led_level_get(NULL);
2082
2083	ret = led_classdev_register(dev, &kbd_led);
2084	if (ret)
2085		kbd_led_present = false;
2086
2087	return ret;
2088}
2089
2090static void brightness_set_exit(struct led_classdev *led_cdev,
2091				enum led_brightness value)
2092{
2093	/* Don't change backlight level on exit */
2094};
2095
2096static void kbd_led_exit(void)
2097{
2098	if (!kbd_led_present)
2099		return;
2100	kbd_led.brightness_set = brightness_set_exit;
2101	led_classdev_unregister(&kbd_led);
2102}
2103
2104static int dell_laptop_notifier_call(struct notifier_block *nb,
2105				     unsigned long action, void *data)
2106{
2107	bool changed = false;
2108	enum led_brightness new_kbd_led_level;
2109
2110	switch (action) {
2111	case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2112		if (!kbd_led_present)
2113			break;
2114
2115		mutex_lock(&kbd_led_mutex);
2116		new_kbd_led_level = kbd_led_level_get(&kbd_led);
2117		if (kbd_led_level != new_kbd_led_level) {
2118			kbd_led_level = new_kbd_led_level;
2119			changed = true;
2120		}
2121		mutex_unlock(&kbd_led_mutex);
2122
2123		if (changed)
2124			led_classdev_notify_brightness_hw_changed(&kbd_led,
2125								kbd_led_level);
2126		break;
2127	}
2128
2129	return NOTIFY_OK;
2130}
2131
2132static struct notifier_block dell_laptop_notifier = {
2133	.notifier_call = dell_laptop_notifier_call,
2134};
2135
2136static int micmute_led_set(struct led_classdev *led_cdev,
2137			   enum led_brightness brightness)
2138{
2139	struct calling_interface_buffer buffer;
2140	struct calling_interface_token *token;
2141	int state = brightness != LED_OFF;
2142
2143	if (state == 0)
2144		token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2145	else
2146		token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2147
2148	if (!token)
2149		return -ENODEV;
2150
2151	dell_fill_request(&buffer, token->location, token->value, 0, 0);
2152	dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2153
2154	return 0;
2155}
2156
2157static struct led_classdev micmute_led_cdev = {
2158	.name = "platform::micmute",
2159	.max_brightness = 1,
2160	.brightness_set_blocking = micmute_led_set,
2161	.default_trigger = "audio-micmute",
2162};
2163
2164static int __init dell_init(void)
2165{
 
2166	struct calling_interface_token *token;
2167	int max_intensity = 0;
2168	int ret;
2169
2170	if (!dmi_check_system(dell_device_table))
2171		return -ENODEV;
2172
2173	quirks = NULL;
2174	/* find if this machine support other functions */
2175	dmi_check_system(dell_quirks);
2176
2177	ret = platform_driver_register(&platform_driver);
2178	if (ret)
2179		goto fail_platform_driver;
2180	platform_device = platform_device_alloc("dell-laptop", -1);
2181	if (!platform_device) {
2182		ret = -ENOMEM;
2183		goto fail_platform_device1;
2184	}
2185	ret = platform_device_add(platform_device);
2186	if (ret)
2187		goto fail_platform_device2;
2188
2189	ret = dell_setup_rfkill();
2190
2191	if (ret) {
2192		pr_warn("Unable to setup rfkill\n");
2193		goto fail_rfkill;
2194	}
2195
2196	if (quirks && quirks->touchpad_led)
2197		touchpad_led_init(&platform_device->dev);
2198
2199	kbd_led_init(&platform_device->dev);
2200
2201	dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2202	debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2203			    &dell_debugfs_fops);
2204
2205	dell_laptop_register_notifier(&dell_laptop_notifier);
2206
2207	if (dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE) &&
2208	    dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE)) {
2209		micmute_led_cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
2210		ret = led_classdev_register(&platform_device->dev, &micmute_led_cdev);
2211		if (ret < 0)
2212			goto fail_led;
2213	}
2214
2215	if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2216		return 0;
2217
2218	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2219	if (token) {
2220		struct calling_interface_buffer buffer;
2221
2222		dell_fill_request(&buffer, token->location, 0, 0, 0);
2223		ret = dell_send_request(&buffer,
2224					CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2225		if (ret == 0)
2226			max_intensity = buffer.output[3];
2227	}
2228
2229	if (max_intensity) {
2230		struct backlight_properties props;
2231		memset(&props, 0, sizeof(struct backlight_properties));
2232		props.type = BACKLIGHT_PLATFORM;
2233		props.max_brightness = max_intensity;
2234		dell_backlight_device = backlight_device_register("dell_backlight",
2235								  &platform_device->dev,
2236								  NULL,
2237								  &dell_ops,
2238								  &props);
2239
2240		if (IS_ERR(dell_backlight_device)) {
2241			ret = PTR_ERR(dell_backlight_device);
2242			dell_backlight_device = NULL;
2243			goto fail_backlight;
2244		}
2245
2246		dell_backlight_device->props.brightness =
2247			dell_get_intensity(dell_backlight_device);
2248		if (dell_backlight_device->props.brightness < 0) {
2249			ret = dell_backlight_device->props.brightness;
2250			goto fail_get_brightness;
2251		}
2252		backlight_update_status(dell_backlight_device);
2253	}
2254
2255	return 0;
2256
2257fail_get_brightness:
2258	backlight_device_unregister(dell_backlight_device);
2259fail_backlight:
2260	led_classdev_unregister(&micmute_led_cdev);
2261fail_led:
2262	dell_cleanup_rfkill();
2263fail_rfkill:
2264	platform_device_del(platform_device);
2265fail_platform_device2:
2266	platform_device_put(platform_device);
2267fail_platform_device1:
2268	platform_driver_unregister(&platform_driver);
2269fail_platform_driver:
2270	return ret;
2271}
2272
2273static void __exit dell_exit(void)
2274{
2275	dell_laptop_unregister_notifier(&dell_laptop_notifier);
2276	debugfs_remove_recursive(dell_laptop_dir);
2277	if (quirks && quirks->touchpad_led)
2278		touchpad_led_exit();
2279	kbd_led_exit();
2280	backlight_device_unregister(dell_backlight_device);
2281	led_classdev_unregister(&micmute_led_cdev);
2282	dell_cleanup_rfkill();
2283	if (platform_device) {
2284		platform_device_unregister(platform_device);
2285		platform_driver_unregister(&platform_driver);
2286	}
2287}
2288
2289/* dell-rbtn.c driver export functions which will not work correctly (and could
2290 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2291 * not problem when dell-rbtn.c is compiled as external module. When both files
2292 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2293 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2294 * This can be achieved by late_initcall() instead module_init().
2295 */
2296late_initcall(dell_init);
2297module_exit(dell_exit);
2298
2299MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2300MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2301MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
2302MODULE_DESCRIPTION("Dell laptop driver");
2303MODULE_LICENSE("GPL");
v4.6
 
   1/*
   2 *  Driver for Dell laptop extras
   3 *
   4 *  Copyright (c) Red Hat <mjg@redhat.com>
   5 *  Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
   6 *  Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
   7 *
   8 *  Based on documentation in the libsmbios package:
   9 *  Copyright (C) 2005-2014 Dell Inc.
  10 *
  11 *  This program is free software; you can redistribute it and/or modify
  12 *  it under the terms of the GNU General Public License version 2 as
  13 *  published by the Free Software Foundation.
  14 */
  15
  16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17
  18#include <linux/module.h>
  19#include <linux/kernel.h>
  20#include <linux/init.h>
  21#include <linux/platform_device.h>
  22#include <linux/backlight.h>
  23#include <linux/err.h>
  24#include <linux/dmi.h>
  25#include <linux/io.h>
  26#include <linux/rfkill.h>
  27#include <linux/power_supply.h>
  28#include <linux/acpi.h>
  29#include <linux/mm.h>
  30#include <linux/i8042.h>
  31#include <linux/debugfs.h>
  32#include <linux/seq_file.h>
  33#include <acpi/video.h>
  34#include "dell-rbtn.h"
  35#include "dell-smbios.h"
  36
  37#define BRIGHTNESS_TOKEN 0x7d
  38#define KBD_LED_OFF_TOKEN 0x01E1
  39#define KBD_LED_ON_TOKEN 0x01E2
  40#define KBD_LED_AUTO_TOKEN 0x01E3
  41#define KBD_LED_AUTO_25_TOKEN 0x02EA
  42#define KBD_LED_AUTO_50_TOKEN 0x02EB
  43#define KBD_LED_AUTO_75_TOKEN 0x02EC
  44#define KBD_LED_AUTO_100_TOKEN 0x02F6
  45
  46struct quirk_entry {
  47	u8 touchpad_led;
 
 
 
  48
  49	int needs_kbd_timeouts;
  50	/*
  51	 * Ordered list of timeouts expressed in seconds.
  52	 * The list must end with -1
  53	 */
  54	int kbd_timeouts[];
  55};
  56
  57static struct quirk_entry *quirks;
  58
  59static struct quirk_entry quirk_dell_vostro_v130 = {
  60	.touchpad_led = 1,
  61};
  62
  63static int __init dmi_matched(const struct dmi_system_id *dmi)
  64{
  65	quirks = dmi->driver_data;
  66	return 1;
  67}
  68
  69/*
  70 * These values come from Windows utility provided by Dell. If any other value
  71 * is used then BIOS silently set timeout to 0 without any error message.
  72 */
  73static struct quirk_entry quirk_dell_xps13_9333 = {
  74	.needs_kbd_timeouts = 1,
  75	.kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
  76};
  77
 
 
 
 
 
 
 
 
 
 
 
 
  78static struct platform_driver platform_driver = {
  79	.driver = {
  80		.name = "dell-laptop",
  81	}
  82};
  83
  84static struct platform_device *platform_device;
  85static struct backlight_device *dell_backlight_device;
  86static struct rfkill *wifi_rfkill;
  87static struct rfkill *bluetooth_rfkill;
  88static struct rfkill *wwan_rfkill;
  89static bool force_rfkill;
  90
  91module_param(force_rfkill, bool, 0444);
  92MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
  93
  94static const struct dmi_system_id dell_device_table[] __initconst = {
  95	{
  96		.ident = "Dell laptop",
  97		.matches = {
  98			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  99			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
 100		},
 101	},
 102	{
 103		.matches = {
 104			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 105			DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
 106		},
 107	},
 108	{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 109		.ident = "Dell Computer Corporation",
 110		.matches = {
 111			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
 112			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
 113		},
 114	},
 115	{ }
 116};
 117MODULE_DEVICE_TABLE(dmi, dell_device_table);
 118
 119static const struct dmi_system_id dell_quirks[] __initconst = {
 120	{
 121		.callback = dmi_matched,
 122		.ident = "Dell Vostro V130",
 123		.matches = {
 124			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 125			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
 126		},
 127		.driver_data = &quirk_dell_vostro_v130,
 128	},
 129	{
 130		.callback = dmi_matched,
 131		.ident = "Dell Vostro V131",
 132		.matches = {
 133			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 134			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
 135		},
 136		.driver_data = &quirk_dell_vostro_v130,
 137	},
 138	{
 139		.callback = dmi_matched,
 140		.ident = "Dell Vostro 3350",
 141		.matches = {
 142			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 143			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
 144		},
 145		.driver_data = &quirk_dell_vostro_v130,
 146	},
 147	{
 148		.callback = dmi_matched,
 149		.ident = "Dell Vostro 3555",
 150		.matches = {
 151			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 152			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
 153		},
 154		.driver_data = &quirk_dell_vostro_v130,
 155	},
 156	{
 157		.callback = dmi_matched,
 158		.ident = "Dell Inspiron N311z",
 159		.matches = {
 160			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 161			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
 162		},
 163		.driver_data = &quirk_dell_vostro_v130,
 164	},
 165	{
 166		.callback = dmi_matched,
 167		.ident = "Dell Inspiron M5110",
 168		.matches = {
 169			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 170			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
 171		},
 172		.driver_data = &quirk_dell_vostro_v130,
 173	},
 174	{
 175		.callback = dmi_matched,
 176		.ident = "Dell Vostro 3360",
 177		.matches = {
 178			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 179			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
 180		},
 181		.driver_data = &quirk_dell_vostro_v130,
 182	},
 183	{
 184		.callback = dmi_matched,
 185		.ident = "Dell Vostro 3460",
 186		.matches = {
 187			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 188			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
 189		},
 190		.driver_data = &quirk_dell_vostro_v130,
 191	},
 192	{
 193		.callback = dmi_matched,
 194		.ident = "Dell Vostro 3560",
 195		.matches = {
 196			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 197			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
 198		},
 199		.driver_data = &quirk_dell_vostro_v130,
 200	},
 201	{
 202		.callback = dmi_matched,
 203		.ident = "Dell Vostro 3450",
 204		.matches = {
 205			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 206			DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
 207		},
 208		.driver_data = &quirk_dell_vostro_v130,
 209	},
 210	{
 211		.callback = dmi_matched,
 212		.ident = "Dell Inspiron 5420",
 213		.matches = {
 214			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 215			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
 216		},
 217		.driver_data = &quirk_dell_vostro_v130,
 218	},
 219	{
 220		.callback = dmi_matched,
 221		.ident = "Dell Inspiron 5520",
 222		.matches = {
 223			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 224			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
 225		},
 226		.driver_data = &quirk_dell_vostro_v130,
 227	},
 228	{
 229		.callback = dmi_matched,
 230		.ident = "Dell Inspiron 5720",
 231		.matches = {
 232			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 233			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
 234		},
 235		.driver_data = &quirk_dell_vostro_v130,
 236	},
 237	{
 238		.callback = dmi_matched,
 239		.ident = "Dell Inspiron 7420",
 240		.matches = {
 241			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 242			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
 243		},
 244		.driver_data = &quirk_dell_vostro_v130,
 245	},
 246	{
 247		.callback = dmi_matched,
 248		.ident = "Dell Inspiron 7520",
 249		.matches = {
 250			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 251			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
 252		},
 253		.driver_data = &quirk_dell_vostro_v130,
 254	},
 255	{
 256		.callback = dmi_matched,
 257		.ident = "Dell Inspiron 7720",
 258		.matches = {
 259			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 260			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
 261		},
 262		.driver_data = &quirk_dell_vostro_v130,
 263	},
 264	{
 265		.callback = dmi_matched,
 266		.ident = "Dell XPS13 9333",
 267		.matches = {
 268			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 269			DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
 270		},
 271		.driver_data = &quirk_dell_xps13_9333,
 272	},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 273	{ }
 274};
 275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 276/*
 277 * Derived from information in smbios-wireless-ctl:
 278 *
 279 * cbSelect 17, Value 11
 280 *
 281 * Return Wireless Info
 282 * cbArg1, byte0 = 0x00
 283 *
 284 *     cbRes1 Standard return codes (0, -1, -2)
 285 *     cbRes2 Info bit flags:
 286 *
 287 *     0 Hardware switch supported (1)
 288 *     1 WiFi locator supported (1)
 289 *     2 WLAN supported (1)
 290 *     3 Bluetooth (BT) supported (1)
 291 *     4 WWAN supported (1)
 292 *     5 Wireless KBD supported (1)
 293 *     6 Uw b supported (1)
 294 *     7 WiGig supported (1)
 295 *     8 WLAN installed (1)
 296 *     9 BT installed (1)
 297 *     10 WWAN installed (1)
 298 *     11 Uw b installed (1)
 299 *     12 WiGig installed (1)
 300 *     13-15 Reserved (0)
 301 *     16 Hardware (HW) switch is On (1)
 302 *     17 WLAN disabled (1)
 303 *     18 BT disabled (1)
 304 *     19 WWAN disabled (1)
 305 *     20 Uw b disabled (1)
 306 *     21 WiGig disabled (1)
 307 *     20-31 Reserved (0)
 308 *
 309 *     cbRes3 NVRAM size in bytes
 310 *     cbRes4, byte 0 NVRAM format version number
 311 *
 312 *
 313 * Set QuickSet Radio Disable Flag
 314 *     cbArg1, byte0 = 0x01
 315 *     cbArg1, byte1
 316 *     Radio ID     value:
 317 *     0        Radio Status
 318 *     1        WLAN ID
 319 *     2        BT ID
 320 *     3        WWAN ID
 321 *     4        UWB ID
 322 *     5        WIGIG ID
 323 *     cbArg1, byte2    Flag bits:
 324 *             0 QuickSet disables radio (1)
 325 *             1-7 Reserved (0)
 326 *
 327 *     cbRes1    Standard return codes (0, -1, -2)
 328 *     cbRes2    QuickSet (QS) radio disable bit map:
 329 *     0 QS disables WLAN
 330 *     1 QS disables BT
 331 *     2 QS disables WWAN
 332 *     3 QS disables UWB
 333 *     4 QS disables WIGIG
 334 *     5-31 Reserved (0)
 335 *
 336 * Wireless Switch Configuration
 337 *     cbArg1, byte0 = 0x02
 338 *
 339 *     cbArg1, byte1
 340 *     Subcommand:
 341 *     0 Get config
 342 *     1 Set config
 343 *     2 Set WiFi locator enable/disable
 344 *     cbArg1,byte2
 345 *     Switch settings (if byte 1==1):
 346 *     0 WLAN sw itch control (1)
 347 *     1 BT sw itch control (1)
 348 *     2 WWAN sw itch control (1)
 349 *     3 UWB sw itch control (1)
 350 *     4 WiGig sw itch control (1)
 351 *     5-7 Reserved (0)
 352 *    cbArg1, byte2 Enable bits (if byte 1==2):
 353 *     0 Enable WiFi locator (1)
 354 *
 355 *    cbRes1     Standard return codes (0, -1, -2)
 356 *    cbRes2 QuickSet radio disable bit map:
 357 *     0 WLAN controlled by sw itch (1)
 358 *     1 BT controlled by sw itch (1)
 359 *     2 WWAN controlled by sw itch (1)
 360 *     3 UWB controlled by sw itch (1)
 361 *     4 WiGig controlled by sw itch (1)
 362 *     5-6 Reserved (0)
 363 *     7 Wireless sw itch config locked (1)
 364 *     8 WiFi locator enabled (1)
 365 *     9-14 Reserved (0)
 366 *     15 WiFi locator setting locked (1)
 367 *     16-31 Reserved (0)
 368 *
 369 * Read Local Config Data (LCD)
 370 *     cbArg1, byte0 = 0x10
 371 *     cbArg1, byte1 NVRAM index low byte
 372 *     cbArg1, byte2 NVRAM index high byte
 373 *     cbRes1 Standard return codes (0, -1, -2)
 374 *     cbRes2 4 bytes read from LCD[index]
 375 *     cbRes3 4 bytes read from LCD[index+4]
 376 *     cbRes4 4 bytes read from LCD[index+8]
 377 *
 378 * Write Local Config Data (LCD)
 379 *     cbArg1, byte0 = 0x11
 380 *     cbArg1, byte1 NVRAM index low byte
 381 *     cbArg1, byte2 NVRAM index high byte
 382 *     cbArg2 4 bytes to w rite at LCD[index]
 383 *     cbArg3 4 bytes to w rite at LCD[index+4]
 384 *     cbArg4 4 bytes to w rite at LCD[index+8]
 385 *     cbRes1 Standard return codes (0, -1, -2)
 386 *
 387 * Populate Local Config Data from NVRAM
 388 *     cbArg1, byte0 = 0x12
 389 *     cbRes1 Standard return codes (0, -1, -2)
 390 *
 391 * Commit Local Config Data to NVRAM
 392 *     cbArg1, byte0 = 0x13
 393 *     cbRes1 Standard return codes (0, -1, -2)
 394 */
 395
 396static int dell_rfkill_set(void *data, bool blocked)
 397{
 398	struct calling_interface_buffer *buffer;
 399	int disable = blocked ? 1 : 0;
 400	unsigned long radio = (unsigned long)data;
 401	int hwswitch_bit = (unsigned long)data - 1;
 
 402	int hwswitch;
 403	int status;
 404	int ret;
 405
 406	buffer = dell_smbios_get_buffer();
 
 
 
 
 407
 408	dell_smbios_send_request(17, 11);
 409	ret = buffer->output[0];
 410	status = buffer->output[1];
 411
 412	if (ret != 0)
 413		goto out;
 414
 415	dell_smbios_clear_buffer();
 416
 417	buffer->input[0] = 0x2;
 418	dell_smbios_send_request(17, 11);
 419	ret = buffer->output[0];
 420	hwswitch = buffer->output[1];
 421
 422	/* If the hardware switch controls this radio, and the hardware
 423	   switch is disabled, always disable the radio */
 424	if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
 425	    (status & BIT(0)) && !(status & BIT(16)))
 426		disable = 1;
 427
 428	dell_smbios_clear_buffer();
 429
 430	buffer->input[0] = (1 | (radio<<8) | (disable << 16));
 431	dell_smbios_send_request(17, 11);
 432	ret = buffer->output[0];
 433
 434 out:
 435	dell_smbios_release_buffer();
 436	return dell_smbios_error(ret);
 437}
 438
 439/* Must be called with the buffer held */
 440static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
 441					int status,
 442					struct calling_interface_buffer *buffer)
 443{
 444	if (status & BIT(0)) {
 445		/* Has hw-switch, sync sw_state to BIOS */
 
 446		int block = rfkill_blocked(rfkill);
 447		dell_smbios_clear_buffer();
 448		buffer->input[0] = (1 | (radio << 8) | (block << 16));
 449		dell_smbios_send_request(17, 11);
 450	} else {
 451		/* No hw-switch, sync BIOS state to sw_state */
 452		rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
 453	}
 454}
 455
 456static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
 457					int status, int hwswitch)
 458{
 459	if (hwswitch & (BIT(radio - 1)))
 460		rfkill_set_hw_state(rfkill, !(status & BIT(16)));
 461}
 462
 463static void dell_rfkill_query(struct rfkill *rfkill, void *data)
 464{
 465	struct calling_interface_buffer *buffer;
 466	int radio = ((unsigned long)data & 0xF);
 
 467	int hwswitch;
 468	int status;
 469	int ret;
 470
 471	buffer = dell_smbios_get_buffer();
 472
 473	dell_smbios_send_request(17, 11);
 474	ret = buffer->output[0];
 475	status = buffer->output[1];
 476
 477	if (ret != 0 || !(status & BIT(0))) {
 478		dell_smbios_release_buffer();
 479		return;
 480	}
 481
 482	dell_smbios_clear_buffer();
 483
 484	buffer->input[0] = 0x2;
 485	dell_smbios_send_request(17, 11);
 486	ret = buffer->output[0];
 487	hwswitch = buffer->output[1];
 488
 489	dell_smbios_release_buffer();
 490
 491	if (ret != 0)
 492		return;
 493
 494	dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
 495}
 496
 497static const struct rfkill_ops dell_rfkill_ops = {
 498	.set_block = dell_rfkill_set,
 499	.query = dell_rfkill_query,
 500};
 501
 502static struct dentry *dell_laptop_dir;
 503
 504static int dell_debugfs_show(struct seq_file *s, void *data)
 505{
 506	struct calling_interface_buffer *buffer;
 507	int hwswitch_state;
 508	int hwswitch_ret;
 509	int status;
 510	int ret;
 511
 512	buffer = dell_smbios_get_buffer();
 513
 514	dell_smbios_send_request(17, 11);
 515	ret = buffer->output[0];
 516	status = buffer->output[1];
 517
 518	dell_smbios_clear_buffer();
 519
 520	buffer->input[0] = 0x2;
 521	dell_smbios_send_request(17, 11);
 522	hwswitch_ret = buffer->output[0];
 523	hwswitch_state = buffer->output[1];
 524
 525	dell_smbios_release_buffer();
 
 
 
 
 526
 527	seq_printf(s, "return:\t%d\n", ret);
 528	seq_printf(s, "status:\t0x%X\n", status);
 529	seq_printf(s, "Bit 0 : Hardware switch supported:   %lu\n",
 530		   status & BIT(0));
 531	seq_printf(s, "Bit 1 : Wifi locator supported:      %lu\n",
 532		  (status & BIT(1)) >> 1);
 533	seq_printf(s, "Bit 2 : Wifi is supported:           %lu\n",
 534		  (status & BIT(2)) >> 2);
 535	seq_printf(s, "Bit 3 : Bluetooth is supported:      %lu\n",
 536		  (status & BIT(3)) >> 3);
 537	seq_printf(s, "Bit 4 : WWAN is supported:           %lu\n",
 538		  (status & BIT(4)) >> 4);
 539	seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
 540		  (status & BIT(5)) >> 5);
 541	seq_printf(s, "Bit 6 : UWB supported:               %lu\n",
 542		  (status & BIT(6)) >> 6);
 543	seq_printf(s, "Bit 7 : WiGig supported:             %lu\n",
 544		  (status & BIT(7)) >> 7);
 545	seq_printf(s, "Bit 8 : Wifi is installed:           %lu\n",
 546		  (status & BIT(8)) >> 8);
 547	seq_printf(s, "Bit 9 : Bluetooth is installed:      %lu\n",
 548		  (status & BIT(9)) >> 9);
 549	seq_printf(s, "Bit 10: WWAN is installed:           %lu\n",
 550		  (status & BIT(10)) >> 10);
 551	seq_printf(s, "Bit 11: UWB installed:               %lu\n",
 552		  (status & BIT(11)) >> 11);
 553	seq_printf(s, "Bit 12: WiGig installed:             %lu\n",
 554		  (status & BIT(12)) >> 12);
 555
 556	seq_printf(s, "Bit 16: Hardware switch is on:       %lu\n",
 557		  (status & BIT(16)) >> 16);
 558	seq_printf(s, "Bit 17: Wifi is blocked:             %lu\n",
 559		  (status & BIT(17)) >> 17);
 560	seq_printf(s, "Bit 18: Bluetooth is blocked:        %lu\n",
 561		  (status & BIT(18)) >> 18);
 562	seq_printf(s, "Bit 19: WWAN is blocked:             %lu\n",
 563		  (status & BIT(19)) >> 19);
 564	seq_printf(s, "Bit 20: UWB is blocked:              %lu\n",
 565		  (status & BIT(20)) >> 20);
 566	seq_printf(s, "Bit 21: WiGig is blocked:            %lu\n",
 567		  (status & BIT(21)) >> 21);
 568
 569	seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
 570	seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
 571	seq_printf(s, "Bit 0 : Wifi controlled by switch:      %lu\n",
 572		   hwswitch_state & BIT(0));
 573	seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
 574		   (hwswitch_state & BIT(1)) >> 1);
 575	seq_printf(s, "Bit 2 : WWAN controlled by switch:      %lu\n",
 576		   (hwswitch_state & BIT(2)) >> 2);
 577	seq_printf(s, "Bit 3 : UWB controlled by switch:       %lu\n",
 578		   (hwswitch_state & BIT(3)) >> 3);
 579	seq_printf(s, "Bit 4 : WiGig controlled by switch:     %lu\n",
 580		   (hwswitch_state & BIT(4)) >> 4);
 581	seq_printf(s, "Bit 7 : Wireless switch config locked:  %lu\n",
 582		   (hwswitch_state & BIT(7)) >> 7);
 583	seq_printf(s, "Bit 8 : Wifi locator enabled:           %lu\n",
 584		   (hwswitch_state & BIT(8)) >> 8);
 585	seq_printf(s, "Bit 15: Wifi locator setting locked:    %lu\n",
 586		   (hwswitch_state & BIT(15)) >> 15);
 587
 588	return 0;
 589}
 590
 591static int dell_debugfs_open(struct inode *inode, struct file *file)
 592{
 593	return single_open(file, dell_debugfs_show, inode->i_private);
 594}
 595
 596static const struct file_operations dell_debugfs_fops = {
 597	.owner = THIS_MODULE,
 598	.open = dell_debugfs_open,
 599	.read = seq_read,
 600	.llseek = seq_lseek,
 601	.release = single_release,
 602};
 603
 604static void dell_update_rfkill(struct work_struct *ignored)
 605{
 606	struct calling_interface_buffer *buffer;
 607	int hwswitch = 0;
 608	int status;
 609	int ret;
 610
 611	buffer = dell_smbios_get_buffer();
 612
 613	dell_smbios_send_request(17, 11);
 614	ret = buffer->output[0];
 615	status = buffer->output[1];
 616
 617	if (ret != 0)
 618		goto out;
 619
 620	dell_smbios_clear_buffer();
 621
 622	buffer->input[0] = 0x2;
 623	dell_smbios_send_request(17, 11);
 624	ret = buffer->output[0];
 625
 626	if (ret == 0 && (status & BIT(0)))
 627		hwswitch = buffer->output[1];
 628
 629	if (wifi_rfkill) {
 630		dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
 631		dell_rfkill_update_sw_state(wifi_rfkill, 1, status, buffer);
 632	}
 633	if (bluetooth_rfkill) {
 634		dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
 635					    hwswitch);
 636		dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status,
 637					    buffer);
 638	}
 639	if (wwan_rfkill) {
 640		dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
 641		dell_rfkill_update_sw_state(wwan_rfkill, 3, status, buffer);
 642	}
 643
 644 out:
 645	dell_smbios_release_buffer();
 646}
 647static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
 648
 649static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
 650			      struct serio *port)
 651{
 652	static bool extended;
 653
 654	if (str & I8042_STR_AUXDATA)
 655		return false;
 656
 657	if (unlikely(data == 0xe0)) {
 658		extended = true;
 659		return false;
 660	} else if (unlikely(extended)) {
 661		switch (data) {
 662		case 0x8:
 663			schedule_delayed_work(&dell_rfkill_work,
 664					      round_jiffies_relative(HZ / 4));
 665			break;
 666		}
 667		extended = false;
 668	}
 669
 670	return false;
 671}
 672
 673static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
 674static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
 675
 676static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
 677					  unsigned long action, void *data)
 678{
 679	schedule_delayed_work(&dell_rfkill_work, 0);
 680	return NOTIFY_OK;
 681}
 682
 683static struct notifier_block dell_laptop_rbtn_notifier = {
 684	.notifier_call = dell_laptop_rbtn_notifier_call,
 685};
 686
 687static int __init dell_setup_rfkill(void)
 688{
 689	struct calling_interface_buffer *buffer;
 690	int status, ret, whitelisted;
 691	const char *product;
 692
 693	/*
 694	 * rfkill support causes trouble on various models, mostly Inspirons.
 695	 * So we whitelist certain series, and don't support rfkill on others.
 696	 */
 697	whitelisted = 0;
 698	product = dmi_get_system_info(DMI_PRODUCT_NAME);
 699	if (product &&  (strncmp(product, "Latitude", 8) == 0 ||
 700			 strncmp(product, "Precision", 9) == 0))
 701		whitelisted = 1;
 702	if (!force_rfkill && !whitelisted)
 703		return 0;
 704
 705	buffer = dell_smbios_get_buffer();
 706	dell_smbios_send_request(17, 11);
 707	ret = buffer->output[0];
 708	status = buffer->output[1];
 709	dell_smbios_release_buffer();
 710
 711	/* dell wireless info smbios call is not supported */
 712	if (ret != 0)
 713		return 0;
 714
 715	/* rfkill is only tested on laptops with a hwswitch */
 716	if (!(status & BIT(0)) && !force_rfkill)
 717		return 0;
 718
 719	if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
 720		wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
 721					   RFKILL_TYPE_WLAN,
 722					   &dell_rfkill_ops, (void *) 1);
 723		if (!wifi_rfkill) {
 724			ret = -ENOMEM;
 725			goto err_wifi;
 726		}
 727		ret = rfkill_register(wifi_rfkill);
 728		if (ret)
 729			goto err_wifi;
 730	}
 731
 732	if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
 733		bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
 734						&platform_device->dev,
 735						RFKILL_TYPE_BLUETOOTH,
 736						&dell_rfkill_ops, (void *) 2);
 737		if (!bluetooth_rfkill) {
 738			ret = -ENOMEM;
 739			goto err_bluetooth;
 740		}
 741		ret = rfkill_register(bluetooth_rfkill);
 742		if (ret)
 743			goto err_bluetooth;
 744	}
 745
 746	if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
 747		wwan_rfkill = rfkill_alloc("dell-wwan",
 748					   &platform_device->dev,
 749					   RFKILL_TYPE_WWAN,
 750					   &dell_rfkill_ops, (void *) 3);
 751		if (!wwan_rfkill) {
 752			ret = -ENOMEM;
 753			goto err_wwan;
 754		}
 755		ret = rfkill_register(wwan_rfkill);
 756		if (ret)
 757			goto err_wwan;
 758	}
 759
 760	/*
 761	 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
 762	 * which can receive events from HW slider switch.
 763	 *
 764	 * Dell SMBIOS on whitelisted models supports controlling radio devices
 765	 * but does not support receiving HW button switch events. We can use
 766	 * i8042 filter hook function to receive keyboard data and handle
 767	 * keycode for HW button.
 768	 *
 769	 * So if it is possible we will use Dell Airplane Mode Switch ACPI
 770	 * driver for receiving HW events and Dell SMBIOS for setting rfkill
 771	 * states. If ACPI driver or device is not available we will fallback to
 772	 * i8042 filter hook function.
 773	 *
 774	 * To prevent duplicate rfkill devices which control and do same thing,
 775	 * dell-rbtn driver will automatically remove its own rfkill devices
 776	 * once function dell_rbtn_notifier_register() is called.
 777	 */
 778
 779	dell_rbtn_notifier_register_func =
 780		symbol_request(dell_rbtn_notifier_register);
 781	if (dell_rbtn_notifier_register_func) {
 782		dell_rbtn_notifier_unregister_func =
 783			symbol_request(dell_rbtn_notifier_unregister);
 784		if (!dell_rbtn_notifier_unregister_func) {
 785			symbol_put(dell_rbtn_notifier_register);
 786			dell_rbtn_notifier_register_func = NULL;
 787		}
 788	}
 789
 790	if (dell_rbtn_notifier_register_func) {
 791		ret = dell_rbtn_notifier_register_func(
 792			&dell_laptop_rbtn_notifier);
 793		symbol_put(dell_rbtn_notifier_register);
 794		dell_rbtn_notifier_register_func = NULL;
 795		if (ret != 0) {
 796			symbol_put(dell_rbtn_notifier_unregister);
 797			dell_rbtn_notifier_unregister_func = NULL;
 798		}
 799	} else {
 800		pr_info("Symbols from dell-rbtn acpi driver are not available\n");
 801		ret = -ENODEV;
 802	}
 803
 804	if (ret == 0) {
 805		pr_info("Using dell-rbtn acpi driver for receiving events\n");
 806	} else if (ret != -ENODEV) {
 807		pr_warn("Unable to register dell rbtn notifier\n");
 808		goto err_filter;
 809	} else {
 810		ret = i8042_install_filter(dell_laptop_i8042_filter);
 811		if (ret) {
 812			pr_warn("Unable to install key filter\n");
 813			goto err_filter;
 814		}
 815		pr_info("Using i8042 filter function for receiving events\n");
 816	}
 817
 818	return 0;
 819err_filter:
 820	if (wwan_rfkill)
 821		rfkill_unregister(wwan_rfkill);
 822err_wwan:
 823	rfkill_destroy(wwan_rfkill);
 824	if (bluetooth_rfkill)
 825		rfkill_unregister(bluetooth_rfkill);
 826err_bluetooth:
 827	rfkill_destroy(bluetooth_rfkill);
 828	if (wifi_rfkill)
 829		rfkill_unregister(wifi_rfkill);
 830err_wifi:
 831	rfkill_destroy(wifi_rfkill);
 832
 833	return ret;
 834}
 835
 836static void dell_cleanup_rfkill(void)
 837{
 838	if (dell_rbtn_notifier_unregister_func) {
 839		dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
 840		symbol_put(dell_rbtn_notifier_unregister);
 841		dell_rbtn_notifier_unregister_func = NULL;
 842	} else {
 843		i8042_remove_filter(dell_laptop_i8042_filter);
 844	}
 845	cancel_delayed_work_sync(&dell_rfkill_work);
 846	if (wifi_rfkill) {
 847		rfkill_unregister(wifi_rfkill);
 848		rfkill_destroy(wifi_rfkill);
 849	}
 850	if (bluetooth_rfkill) {
 851		rfkill_unregister(bluetooth_rfkill);
 852		rfkill_destroy(bluetooth_rfkill);
 853	}
 854	if (wwan_rfkill) {
 855		rfkill_unregister(wwan_rfkill);
 856		rfkill_destroy(wwan_rfkill);
 857	}
 858}
 859
 860static int dell_send_intensity(struct backlight_device *bd)
 861{
 862	struct calling_interface_buffer *buffer;
 863	struct calling_interface_token *token;
 864	int ret;
 865
 866	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
 867	if (!token)
 868		return -ENODEV;
 869
 870	buffer = dell_smbios_get_buffer();
 871	buffer->input[0] = token->location;
 872	buffer->input[1] = bd->props.brightness;
 873
 874	if (power_supply_is_system_supplied() > 0)
 875		dell_smbios_send_request(1, 2);
 
 876	else
 877		dell_smbios_send_request(1, 1);
 
 878
 879	ret = dell_smbios_error(buffer->output[0]);
 880
 881	dell_smbios_release_buffer();
 882	return ret;
 883}
 884
 885static int dell_get_intensity(struct backlight_device *bd)
 886{
 887	struct calling_interface_buffer *buffer;
 888	struct calling_interface_token *token;
 889	int ret;
 890
 891	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
 892	if (!token)
 893		return -ENODEV;
 894
 895	buffer = dell_smbios_get_buffer();
 896	buffer->input[0] = token->location;
 897
 898	if (power_supply_is_system_supplied() > 0)
 899		dell_smbios_send_request(0, 2);
 
 900	else
 901		dell_smbios_send_request(0, 1);
 
 902
 903	if (buffer->output[0])
 904		ret = dell_smbios_error(buffer->output[0]);
 905	else
 906		ret = buffer->output[1];
 907
 908	dell_smbios_release_buffer();
 909	return ret;
 910}
 911
 912static const struct backlight_ops dell_ops = {
 913	.get_brightness = dell_get_intensity,
 914	.update_status  = dell_send_intensity,
 915};
 916
 917static void touchpad_led_on(void)
 918{
 919	int command = 0x97;
 920	char data = 1;
 921	i8042_command(&data, command | 1 << 12);
 922}
 923
 924static void touchpad_led_off(void)
 925{
 926	int command = 0x97;
 927	char data = 2;
 928	i8042_command(&data, command | 1 << 12);
 929}
 930
 931static void touchpad_led_set(struct led_classdev *led_cdev,
 932	enum led_brightness value)
 933{
 934	if (value > 0)
 935		touchpad_led_on();
 936	else
 937		touchpad_led_off();
 938}
 939
 940static struct led_classdev touchpad_led = {
 941	.name = "dell-laptop::touchpad",
 942	.brightness_set = touchpad_led_set,
 943	.flags = LED_CORE_SUSPENDRESUME,
 944};
 945
 946static int __init touchpad_led_init(struct device *dev)
 947{
 948	return led_classdev_register(dev, &touchpad_led);
 949}
 950
 951static void touchpad_led_exit(void)
 952{
 953	led_classdev_unregister(&touchpad_led);
 954}
 955
 956/*
 957 * Derived from information in smbios-keyboard-ctl:
 958 *
 959 * cbClass 4
 960 * cbSelect 11
 961 * Keyboard illumination
 962 * cbArg1 determines the function to be performed
 963 *
 964 * cbArg1 0x0 = Get Feature Information
 965 *  cbRES1         Standard return codes (0, -1, -2)
 966 *  cbRES2, word0  Bitmap of user-selectable modes
 967 *     bit 0     Always off (All systems)
 968 *     bit 1     Always on (Travis ATG, Siberia)
 969 *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
 970 *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
 971 *     bit 4     Auto: Input-activity-based On; input-activity based Off
 972 *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
 973 *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
 974 *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
 975 *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
 976 *     bits 9-15 Reserved for future use
 977 *  cbRES2, byte2  Reserved for future use
 978 *  cbRES2, byte3  Keyboard illumination type
 979 *     0         Reserved
 980 *     1         Tasklight
 981 *     2         Backlight
 982 *     3-255     Reserved for future use
 983 *  cbRES3, byte0  Supported auto keyboard illumination trigger bitmap.
 984 *     bit 0     Any keystroke
 985 *     bit 1     Touchpad activity
 986 *     bit 2     Pointing stick
 987 *     bit 3     Any mouse
 988 *     bits 4-7  Reserved for future use
 989 *  cbRES3, byte1  Supported timeout unit bitmap
 990 *     bit 0     Seconds
 991 *     bit 1     Minutes
 992 *     bit 2     Hours
 993 *     bit 3     Days
 994 *     bits 4-7  Reserved for future use
 995 *  cbRES3, byte2  Number of keyboard light brightness levels
 996 *  cbRES4, byte0  Maximum acceptable seconds value (0 if seconds not supported).
 997 *  cbRES4, byte1  Maximum acceptable minutes value (0 if minutes not supported).
 998 *  cbRES4, byte2  Maximum acceptable hours value (0 if hours not supported).
 999 *  cbRES4, byte3  Maximum acceptable days value (0 if days not supported)
1000 *
1001 * cbArg1 0x1 = Get Current State
1002 *  cbRES1         Standard return codes (0, -1, -2)
1003 *  cbRES2, word0  Bitmap of current mode state
1004 *     bit 0     Always off (All systems)
1005 *     bit 1     Always on (Travis ATG, Siberia)
1006 *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1007 *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1008 *     bit 4     Auto: Input-activity-based On; input-activity based Off
1009 *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1010 *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1011 *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1012 *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1013 *     bits 9-15 Reserved for future use
1014 *     Note: Only One bit can be set
1015 *  cbRES2, byte2  Currently active auto keyboard illumination triggers.
1016 *     bit 0     Any keystroke
1017 *     bit 1     Touchpad activity
1018 *     bit 2     Pointing stick
1019 *     bit 3     Any mouse
1020 *     bits 4-7  Reserved for future use
1021 *  cbRES2, byte3  Current Timeout
1022 *     bits 7:6  Timeout units indicator:
1023 *     00b       Seconds
1024 *     01b       Minutes
1025 *     10b       Hours
1026 *     11b       Days
1027 *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1028 *     NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1029 *     are set upon return from the [Get feature information] call.
1030 *  cbRES3, byte0  Current setting of ALS value that turns the light on or off.
1031 *  cbRES3, byte1  Current ALS reading
1032 *  cbRES3, byte2  Current keyboard light level.
 
 
 
 
 
 
 
 
 
1033 *
1034 * cbArg1 0x2 = Set New State
1035 *  cbRES1         Standard return codes (0, -1, -2)
1036 *  cbArg2, word0  Bitmap of current mode state
1037 *     bit 0     Always off (All systems)
1038 *     bit 1     Always on (Travis ATG, Siberia)
1039 *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1040 *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1041 *     bit 4     Auto: Input-activity-based On; input-activity based Off
1042 *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1043 *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1044 *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1045 *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1046 *     bits 9-15 Reserved for future use
1047 *     Note: Only One bit can be set
1048 *  cbArg2, byte2  Desired auto keyboard illumination triggers. Must remain inactive to allow
1049 *                 keyboard to turn off automatically.
1050 *     bit 0     Any keystroke
1051 *     bit 1     Touchpad activity
1052 *     bit 2     Pointing stick
1053 *     bit 3     Any mouse
1054 *     bits 4-7  Reserved for future use
1055 *  cbArg2, byte3  Desired Timeout
1056 *     bits 7:6  Timeout units indicator:
1057 *     00b       Seconds
1058 *     01b       Minutes
1059 *     10b       Hours
1060 *     11b       Days
1061 *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1062 *  cbArg3, byte0  Desired setting of ALS value that turns the light on or off.
1063 *  cbArg3, byte2  Desired keyboard light level.
 
 
 
 
 
 
 
1064 */
1065
1066
1067enum kbd_timeout_unit {
1068	KBD_TIMEOUT_SECONDS = 0,
1069	KBD_TIMEOUT_MINUTES,
1070	KBD_TIMEOUT_HOURS,
1071	KBD_TIMEOUT_DAYS,
1072};
1073
1074enum kbd_mode_bit {
1075	KBD_MODE_BIT_OFF = 0,
1076	KBD_MODE_BIT_ON,
1077	KBD_MODE_BIT_ALS,
1078	KBD_MODE_BIT_TRIGGER_ALS,
1079	KBD_MODE_BIT_TRIGGER,
1080	KBD_MODE_BIT_TRIGGER_25,
1081	KBD_MODE_BIT_TRIGGER_50,
1082	KBD_MODE_BIT_TRIGGER_75,
1083	KBD_MODE_BIT_TRIGGER_100,
1084};
1085
1086#define kbd_is_als_mode_bit(bit) \
1087	((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1088#define kbd_is_trigger_mode_bit(bit) \
1089	((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1090#define kbd_is_level_mode_bit(bit) \
1091	((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1092
1093struct kbd_info {
1094	u16 modes;
1095	u8 type;
1096	u8 triggers;
1097	u8 levels;
1098	u8 seconds;
1099	u8 minutes;
1100	u8 hours;
1101	u8 days;
1102};
1103
1104struct kbd_state {
1105	u8 mode_bit;
1106	u8 triggers;
1107	u8 timeout_value;
1108	u8 timeout_unit;
 
 
1109	u8 als_setting;
1110	u8 als_value;
1111	u8 level;
1112};
1113
1114static const int kbd_tokens[] = {
1115	KBD_LED_OFF_TOKEN,
1116	KBD_LED_AUTO_25_TOKEN,
1117	KBD_LED_AUTO_50_TOKEN,
1118	KBD_LED_AUTO_75_TOKEN,
1119	KBD_LED_AUTO_100_TOKEN,
1120	KBD_LED_ON_TOKEN,
1121};
1122
1123static u16 kbd_token_bits;
1124
1125static struct kbd_info kbd_info;
1126static bool kbd_als_supported;
1127static bool kbd_triggers_supported;
 
1128
1129static u8 kbd_mode_levels[16];
1130static int kbd_mode_levels_count;
1131
1132static u8 kbd_previous_level;
1133static u8 kbd_previous_mode_bit;
1134
1135static bool kbd_led_present;
 
 
1136
1137/*
1138 * NOTE: there are three ways to set the keyboard backlight level.
1139 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1140 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1141 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1142 *
1143 * There are laptops which support only one of these methods. If we want to
1144 * support as many machines as possible we need to implement all three methods.
1145 * The first two methods use the kbd_state structure. The third uses SMBIOS
1146 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1147 * keyboard backlight level via kbd_state.level.
1148 */
1149
1150static int kbd_get_info(struct kbd_info *info)
1151{
1152	struct calling_interface_buffer *buffer;
1153	u8 units;
1154	int ret;
1155
1156	buffer = dell_smbios_get_buffer();
 
 
 
 
1157
1158	buffer->input[0] = 0x0;
1159	dell_smbios_send_request(4, 11);
1160	ret = buffer->output[0];
1161
1162	if (ret) {
1163		ret = dell_smbios_error(ret);
1164		goto out;
1165	}
1166
1167	info->modes = buffer->output[1] & 0xFFFF;
1168	info->type = (buffer->output[1] >> 24) & 0xFF;
1169	info->triggers = buffer->output[2] & 0xFF;
1170	units = (buffer->output[2] >> 8) & 0xFF;
1171	info->levels = (buffer->output[2] >> 16) & 0xFF;
1172
1173	if (units & BIT(0))
1174		info->seconds = (buffer->output[3] >> 0) & 0xFF;
1175	if (units & BIT(1))
1176		info->minutes = (buffer->output[3] >> 8) & 0xFF;
1177	if (units & BIT(2))
1178		info->hours = (buffer->output[3] >> 16) & 0xFF;
1179	if (units & BIT(3))
1180		info->days = (buffer->output[3] >> 24) & 0xFF;
1181
1182 out:
1183	dell_smbios_release_buffer();
1184	return ret;
1185}
1186
1187static unsigned int kbd_get_max_level(void)
1188{
1189	if (kbd_info.levels != 0)
1190		return kbd_info.levels;
1191	if (kbd_mode_levels_count > 0)
1192		return kbd_mode_levels_count - 1;
1193	return 0;
1194}
1195
1196static int kbd_get_level(struct kbd_state *state)
1197{
1198	int i;
1199
1200	if (kbd_info.levels != 0)
1201		return state->level;
1202
1203	if (kbd_mode_levels_count > 0) {
1204		for (i = 0; i < kbd_mode_levels_count; ++i)
1205			if (kbd_mode_levels[i] == state->mode_bit)
1206				return i;
1207		return 0;
1208	}
1209
1210	return -EINVAL;
1211}
1212
1213static int kbd_set_level(struct kbd_state *state, u8 level)
1214{
1215	if (kbd_info.levels != 0) {
1216		if (level != 0)
1217			kbd_previous_level = level;
1218		if (state->level == level)
1219			return 0;
1220		state->level = level;
1221		if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1222			state->mode_bit = kbd_previous_mode_bit;
1223		else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1224			kbd_previous_mode_bit = state->mode_bit;
1225			state->mode_bit = KBD_MODE_BIT_OFF;
1226		}
1227		return 0;
1228	}
1229
1230	if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1231		if (level != 0)
1232			kbd_previous_level = level;
1233		state->mode_bit = kbd_mode_levels[level];
1234		return 0;
1235	}
1236
1237	return -EINVAL;
1238}
1239
1240static int kbd_get_state(struct kbd_state *state)
1241{
1242	struct calling_interface_buffer *buffer;
1243	int ret;
1244
1245	buffer = dell_smbios_get_buffer();
 
 
 
 
1246
1247	buffer->input[0] = 0x1;
1248	dell_smbios_send_request(4, 11);
1249	ret = buffer->output[0];
1250
1251	if (ret) {
1252		ret = dell_smbios_error(ret);
1253		goto out;
1254	}
1255
1256	state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1257	if (state->mode_bit != 0)
1258		state->mode_bit--;
1259
1260	state->triggers = (buffer->output[1] >> 16) & 0xFF;
1261	state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1262	state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1263	state->als_setting = buffer->output[2] & 0xFF;
1264	state->als_value = (buffer->output[2] >> 8) & 0xFF;
1265	state->level = (buffer->output[2] >> 16) & 0xFF;
 
 
1266
1267 out:
1268	dell_smbios_release_buffer();
1269	return ret;
1270}
1271
1272static int kbd_set_state(struct kbd_state *state)
1273{
1274	struct calling_interface_buffer *buffer;
1275	int ret;
 
 
1276
1277	buffer = dell_smbios_get_buffer();
1278	buffer->input[0] = 0x2;
1279	buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1280	buffer->input[1] |= (state->triggers & 0xFF) << 16;
1281	buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1282	buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1283	buffer->input[2] = state->als_setting & 0xFF;
1284	buffer->input[2] |= (state->level & 0xFF) << 16;
1285	dell_smbios_send_request(4, 11);
1286	ret = buffer->output[0];
1287	dell_smbios_release_buffer();
1288
1289	return dell_smbios_error(ret);
1290}
1291
1292static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1293{
1294	int ret;
1295
1296	ret = kbd_set_state(state);
1297	if (ret == 0)
1298		return 0;
1299
1300	/*
1301	 * When setting the new state fails,try to restore the previous one.
1302	 * This is needed on some machines where BIOS sets a default state when
1303	 * setting a new state fails. This default state could be all off.
1304	 */
1305
1306	if (kbd_set_state(old))
1307		pr_err("Setting old previous keyboard state failed\n");
1308
1309	return ret;
1310}
1311
1312static int kbd_set_token_bit(u8 bit)
1313{
1314	struct calling_interface_buffer *buffer;
1315	struct calling_interface_token *token;
1316	int ret;
1317
1318	if (bit >= ARRAY_SIZE(kbd_tokens))
1319		return -EINVAL;
1320
1321	token = dell_smbios_find_token(kbd_tokens[bit]);
1322	if (!token)
1323		return -EINVAL;
1324
1325	buffer = dell_smbios_get_buffer();
1326	buffer->input[0] = token->location;
1327	buffer->input[1] = token->value;
1328	dell_smbios_send_request(1, 0);
1329	ret = buffer->output[0];
1330	dell_smbios_release_buffer();
1331
1332	return dell_smbios_error(ret);
1333}
1334
1335static int kbd_get_token_bit(u8 bit)
1336{
1337	struct calling_interface_buffer *buffer;
1338	struct calling_interface_token *token;
1339	int ret;
1340	int val;
1341
1342	if (bit >= ARRAY_SIZE(kbd_tokens))
1343		return -EINVAL;
1344
1345	token = dell_smbios_find_token(kbd_tokens[bit]);
1346	if (!token)
1347		return -EINVAL;
1348
1349	buffer = dell_smbios_get_buffer();
1350	buffer->input[0] = token->location;
1351	dell_smbios_send_request(0, 0);
1352	ret = buffer->output[0];
1353	val = buffer->output[1];
1354	dell_smbios_release_buffer();
1355
1356	if (ret)
1357		return dell_smbios_error(ret);
1358
1359	return (val == token->value);
1360}
1361
1362static int kbd_get_first_active_token_bit(void)
1363{
1364	int i;
1365	int ret;
1366
1367	for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1368		ret = kbd_get_token_bit(i);
1369		if (ret == 1)
1370			return i;
1371	}
1372
1373	return ret;
1374}
1375
1376static int kbd_get_valid_token_counts(void)
1377{
1378	return hweight16(kbd_token_bits);
1379}
1380
1381static inline int kbd_init_info(void)
1382{
1383	struct kbd_state state;
1384	int ret;
1385	int i;
1386
1387	ret = kbd_get_info(&kbd_info);
1388	if (ret)
1389		return ret;
1390
 
 
 
 
 
 
 
 
1391	kbd_get_state(&state);
1392
1393	/* NOTE: timeout value is stored in 6 bits so max value is 63 */
1394	if (kbd_info.seconds > 63)
1395		kbd_info.seconds = 63;
1396	if (kbd_info.minutes > 63)
1397		kbd_info.minutes = 63;
1398	if (kbd_info.hours > 63)
1399		kbd_info.hours = 63;
1400	if (kbd_info.days > 63)
1401		kbd_info.days = 63;
1402
1403	/* NOTE: On tested machines ON mode did not work and caused
1404	 *       problems (turned backlight off) so do not use it
1405	 */
1406	kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1407
1408	kbd_previous_level = kbd_get_level(&state);
1409	kbd_previous_mode_bit = state.mode_bit;
1410
1411	if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1412		kbd_previous_level = 1;
1413
1414	if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1415		kbd_previous_mode_bit =
1416			ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1417		if (kbd_previous_mode_bit != 0)
1418			kbd_previous_mode_bit--;
1419	}
1420
1421	if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1422			      BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1423		kbd_als_supported = true;
1424
1425	if (kbd_info.modes & (
1426	    BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1427	    BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1428	    BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1429	   ))
1430		kbd_triggers_supported = true;
1431
1432	/* kbd_mode_levels[0] is reserved, see below */
1433	for (i = 0; i < 16; ++i)
1434		if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1435			kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1436
1437	/*
1438	 * Find the first supported mode and assign to kbd_mode_levels[0].
1439	 * This should be 0 (off), but we cannot depend on the BIOS to
1440	 * support 0.
1441	 */
1442	if (kbd_mode_levels_count > 0) {
1443		for (i = 0; i < 16; ++i) {
1444			if (BIT(i) & kbd_info.modes) {
1445				kbd_mode_levels[0] = i;
1446				break;
1447			}
1448		}
1449		kbd_mode_levels_count++;
1450	}
1451
1452	return 0;
1453
1454}
1455
1456static inline void kbd_init_tokens(void)
1457{
1458	int i;
1459
1460	for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1461		if (dell_smbios_find_token(kbd_tokens[i]))
1462			kbd_token_bits |= BIT(i);
1463}
1464
1465static void kbd_init(void)
1466{
1467	int ret;
1468
 
 
 
1469	ret = kbd_init_info();
1470	kbd_init_tokens();
1471
1472	if (kbd_token_bits != 0 || ret == 0)
 
 
 
 
1473		kbd_led_present = true;
1474}
1475
1476static ssize_t kbd_led_timeout_store(struct device *dev,
1477				     struct device_attribute *attr,
1478				     const char *buf, size_t count)
1479{
1480	struct kbd_state new_state;
1481	struct kbd_state state;
1482	bool convert;
1483	int value;
1484	int ret;
1485	char ch;
1486	u8 unit;
1487	int i;
1488
1489	ret = sscanf(buf, "%d %c", &value, &ch);
1490	if (ret < 1)
1491		return -EINVAL;
1492	else if (ret == 1)
1493		ch = 's';
1494
1495	if (value < 0)
1496		return -EINVAL;
1497
1498	convert = false;
1499
1500	switch (ch) {
1501	case 's':
1502		if (value > kbd_info.seconds)
1503			convert = true;
1504		unit = KBD_TIMEOUT_SECONDS;
1505		break;
1506	case 'm':
1507		if (value > kbd_info.minutes)
1508			convert = true;
1509		unit = KBD_TIMEOUT_MINUTES;
1510		break;
1511	case 'h':
1512		if (value > kbd_info.hours)
1513			convert = true;
1514		unit = KBD_TIMEOUT_HOURS;
1515		break;
1516	case 'd':
1517		if (value > kbd_info.days)
1518			convert = true;
1519		unit = KBD_TIMEOUT_DAYS;
1520		break;
1521	default:
1522		return -EINVAL;
1523	}
1524
1525	if (quirks && quirks->needs_kbd_timeouts)
1526		convert = true;
1527
1528	if (convert) {
1529		/* Convert value from current units to seconds */
1530		switch (unit) {
1531		case KBD_TIMEOUT_DAYS:
1532			value *= 24;
 
1533		case KBD_TIMEOUT_HOURS:
1534			value *= 60;
 
1535		case KBD_TIMEOUT_MINUTES:
1536			value *= 60;
1537			unit = KBD_TIMEOUT_SECONDS;
1538		}
1539
1540		if (quirks && quirks->needs_kbd_timeouts) {
1541			for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1542				if (value <= quirks->kbd_timeouts[i]) {
1543					value = quirks->kbd_timeouts[i];
1544					break;
1545				}
1546			}
1547		}
1548
1549		if (value <= kbd_info.seconds && kbd_info.seconds) {
1550			unit = KBD_TIMEOUT_SECONDS;
1551		} else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1552			value /= 60;
1553			unit = KBD_TIMEOUT_MINUTES;
1554		} else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1555			value /= (60 * 60);
1556			unit = KBD_TIMEOUT_HOURS;
1557		} else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1558			value /= (60 * 60 * 24);
1559			unit = KBD_TIMEOUT_DAYS;
1560		} else {
1561			return -EINVAL;
1562		}
1563	}
1564
 
 
1565	ret = kbd_get_state(&state);
1566	if (ret)
1567		return ret;
1568
1569	new_state = state;
1570	new_state.timeout_value = value;
1571	new_state.timeout_unit = unit;
 
 
 
 
 
 
1572
1573	ret = kbd_set_state_safe(&new_state, &state);
1574	if (ret)
1575		return ret;
1576
1577	return count;
 
 
 
1578}
1579
1580static ssize_t kbd_led_timeout_show(struct device *dev,
1581				    struct device_attribute *attr, char *buf)
1582{
1583	struct kbd_state state;
 
1584	int ret;
1585	int len;
 
1586
1587	ret = kbd_get_state(&state);
1588	if (ret)
1589		return ret;
1590
1591	len = sprintf(buf, "%d", state.timeout_value);
 
 
 
 
 
 
 
 
1592
1593	switch (state.timeout_unit) {
1594	case KBD_TIMEOUT_SECONDS:
1595		return len + sprintf(buf+len, "s\n");
1596	case KBD_TIMEOUT_MINUTES:
1597		return len + sprintf(buf+len, "m\n");
1598	case KBD_TIMEOUT_HOURS:
1599		return len + sprintf(buf+len, "h\n");
1600	case KBD_TIMEOUT_DAYS:
1601		return len + sprintf(buf+len, "d\n");
1602	default:
1603		return -EINVAL;
1604	}
1605
1606	return len;
1607}
1608
1609static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1610		   kbd_led_timeout_show, kbd_led_timeout_store);
1611
1612static const char * const kbd_led_triggers[] = {
1613	"keyboard",
1614	"touchpad",
1615	/*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1616	"mouse",
1617};
1618
1619static ssize_t kbd_led_triggers_store(struct device *dev,
1620				      struct device_attribute *attr,
1621				      const char *buf, size_t count)
1622{
1623	struct kbd_state new_state;
1624	struct kbd_state state;
1625	bool triggers_enabled = false;
1626	int trigger_bit = -1;
1627	char trigger[21];
1628	int i, ret;
1629
1630	ret = sscanf(buf, "%20s", trigger);
1631	if (ret != 1)
1632		return -EINVAL;
1633
1634	if (trigger[0] != '+' && trigger[0] != '-')
1635		return -EINVAL;
1636
 
 
1637	ret = kbd_get_state(&state);
1638	if (ret)
1639		return ret;
1640
1641	if (kbd_triggers_supported)
1642		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1643
1644	if (kbd_triggers_supported) {
1645		for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1646			if (!(kbd_info.triggers & BIT(i)))
1647				continue;
1648			if (!kbd_led_triggers[i])
1649				continue;
1650			if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1651				continue;
1652			if (trigger[0] == '+' &&
1653			    triggers_enabled && (state.triggers & BIT(i)))
1654				return count;
 
 
1655			if (trigger[0] == '-' &&
1656			    (!triggers_enabled || !(state.triggers & BIT(i))))
1657				return count;
 
 
1658			trigger_bit = i;
1659			break;
1660		}
1661	}
1662
1663	if (trigger_bit != -1) {
1664		new_state = state;
1665		if (trigger[0] == '+')
1666			new_state.triggers |= BIT(trigger_bit);
1667		else {
1668			new_state.triggers &= ~BIT(trigger_bit);
1669			/* NOTE: trackstick bit (2) must be disabled when
1670			 *       disabling touchpad bit (1), otherwise touchpad
1671			 *       bit (1) will not be disabled */
1672			if (trigger_bit == 1)
1673				new_state.triggers &= ~BIT(2);
1674		}
1675		if ((kbd_info.triggers & new_state.triggers) !=
1676		    new_state.triggers)
1677			return -EINVAL;
1678		if (new_state.triggers && !triggers_enabled) {
1679			new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1680			kbd_set_level(&new_state, kbd_previous_level);
1681		} else if (new_state.triggers == 0) {
1682			kbd_set_level(&new_state, 0);
1683		}
1684		if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1685			return -EINVAL;
1686		ret = kbd_set_state_safe(&new_state, &state);
1687		if (ret)
1688			return ret;
1689		if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1690			kbd_previous_mode_bit = new_state.mode_bit;
1691		return count;
1692	}
1693
1694	return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1695}
1696
1697static ssize_t kbd_led_triggers_show(struct device *dev,
1698				     struct device_attribute *attr, char *buf)
1699{
1700	struct kbd_state state;
1701	bool triggers_enabled;
1702	int level, i, ret;
1703	int len = 0;
1704
1705	ret = kbd_get_state(&state);
1706	if (ret)
1707		return ret;
1708
1709	len = 0;
1710
1711	if (kbd_triggers_supported) {
1712		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1713		level = kbd_get_level(&state);
1714		for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1715			if (!(kbd_info.triggers & BIT(i)))
1716				continue;
1717			if (!kbd_led_triggers[i])
1718				continue;
1719			if ((triggers_enabled || level <= 0) &&
1720			    (state.triggers & BIT(i)))
1721				buf[len++] = '+';
1722			else
1723				buf[len++] = '-';
1724			len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1725		}
1726	}
1727
1728	if (len)
1729		buf[len - 1] = '\n';
1730
1731	return len;
1732}
1733
1734static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1735		   kbd_led_triggers_show, kbd_led_triggers_store);
1736
1737static ssize_t kbd_led_als_enabled_store(struct device *dev,
1738					 struct device_attribute *attr,
1739					 const char *buf, size_t count)
1740{
1741	struct kbd_state new_state;
1742	struct kbd_state state;
1743	bool triggers_enabled = false;
1744	int enable;
1745	int ret;
1746
1747	ret = kstrtoint(buf, 0, &enable);
1748	if (ret)
1749		return ret;
1750
 
 
1751	ret = kbd_get_state(&state);
1752	if (ret)
1753		return ret;
1754
1755	if (enable == kbd_is_als_mode_bit(state.mode_bit))
1756		return count;
 
 
1757
1758	new_state = state;
1759
1760	if (kbd_triggers_supported)
1761		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1762
1763	if (enable) {
1764		if (triggers_enabled)
1765			new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1766		else
1767			new_state.mode_bit = KBD_MODE_BIT_ALS;
1768	} else {
1769		if (triggers_enabled) {
1770			new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1771			kbd_set_level(&new_state, kbd_previous_level);
1772		} else {
1773			new_state.mode_bit = KBD_MODE_BIT_ON;
1774		}
1775	}
1776	if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1777		return -EINVAL;
 
 
1778
1779	ret = kbd_set_state_safe(&new_state, &state);
1780	if (ret)
1781		return ret;
1782	kbd_previous_mode_bit = new_state.mode_bit;
1783
1784	return count;
 
 
 
1785}
1786
1787static ssize_t kbd_led_als_enabled_show(struct device *dev,
1788					struct device_attribute *attr,
1789					char *buf)
1790{
1791	struct kbd_state state;
1792	bool enabled = false;
1793	int ret;
1794
1795	ret = kbd_get_state(&state);
1796	if (ret)
1797		return ret;
1798	enabled = kbd_is_als_mode_bit(state.mode_bit);
1799
1800	return sprintf(buf, "%d\n", enabled ? 1 : 0);
1801}
1802
1803static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1804		   kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1805
1806static ssize_t kbd_led_als_setting_store(struct device *dev,
1807					 struct device_attribute *attr,
1808					 const char *buf, size_t count)
1809{
1810	struct kbd_state state;
1811	struct kbd_state new_state;
1812	u8 setting;
1813	int ret;
1814
1815	ret = kstrtou8(buf, 10, &setting);
1816	if (ret)
1817		return ret;
1818
 
 
1819	ret = kbd_get_state(&state);
1820	if (ret)
1821		return ret;
1822
1823	new_state = state;
1824	new_state.als_setting = setting;
1825
1826	ret = kbd_set_state_safe(&new_state, &state);
1827	if (ret)
1828		return ret;
1829
1830	return count;
 
 
 
1831}
1832
1833static ssize_t kbd_led_als_setting_show(struct device *dev,
1834					struct device_attribute *attr,
1835					char *buf)
1836{
1837	struct kbd_state state;
1838	int ret;
1839
1840	ret = kbd_get_state(&state);
1841	if (ret)
1842		return ret;
1843
1844	return sprintf(buf, "%d\n", state.als_setting);
1845}
1846
1847static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1848		   kbd_led_als_setting_show, kbd_led_als_setting_store);
1849
1850static struct attribute *kbd_led_attrs[] = {
1851	&dev_attr_stop_timeout.attr,
1852	&dev_attr_start_triggers.attr,
1853	NULL,
1854};
1855
1856static const struct attribute_group kbd_led_group = {
1857	.attrs = kbd_led_attrs,
1858};
1859
1860static struct attribute *kbd_led_als_attrs[] = {
1861	&dev_attr_als_enabled.attr,
1862	&dev_attr_als_setting.attr,
1863	NULL,
1864};
1865
1866static const struct attribute_group kbd_led_als_group = {
1867	.attrs = kbd_led_als_attrs,
1868};
1869
1870static const struct attribute_group *kbd_led_groups[] = {
1871	&kbd_led_group,
1872	&kbd_led_als_group,
1873	NULL,
1874};
1875
1876static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1877{
1878	int ret;
1879	u16 num;
1880	struct kbd_state state;
1881
1882	if (kbd_get_max_level()) {
1883		ret = kbd_get_state(&state);
1884		if (ret)
1885			return 0;
1886		ret = kbd_get_level(&state);
1887		if (ret < 0)
1888			return 0;
1889		return ret;
1890	}
1891
1892	if (kbd_get_valid_token_counts()) {
1893		ret = kbd_get_first_active_token_bit();
1894		if (ret < 0)
1895			return 0;
1896		for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1897			num &= num - 1; /* clear the first bit set */
1898		if (num == 0)
1899			return 0;
1900		return ffs(num) - 1;
1901	}
1902
1903	pr_warn("Keyboard brightness level control not supported\n");
1904	return 0;
1905}
1906
1907static void kbd_led_level_set(struct led_classdev *led_cdev,
1908			      enum led_brightness value)
1909{
 
1910	struct kbd_state state;
1911	struct kbd_state new_state;
1912	u16 num;
 
 
 
1913
1914	if (kbd_get_max_level()) {
1915		if (kbd_get_state(&state))
1916			return;
 
1917		new_state = state;
1918		if (kbd_set_level(&new_state, value))
1919			return;
1920		kbd_set_state_safe(&new_state, &state);
1921		return;
1922	}
1923
1924	if (kbd_get_valid_token_counts()) {
1925		for (num = kbd_token_bits; num != 0 && value > 0; --value)
1926			num &= num - 1; /* clear the first bit set */
1927		if (num == 0)
1928			return;
1929		kbd_set_token_bit(ffs(num) - 1);
1930		return;
 
 
 
1931	}
1932
1933	pr_warn("Keyboard brightness level control not supported\n");
 
 
 
 
 
1934}
1935
1936static struct led_classdev kbd_led = {
1937	.name           = "dell::kbd_backlight",
1938	.brightness_set = kbd_led_level_set,
 
1939	.brightness_get = kbd_led_level_get,
1940	.groups         = kbd_led_groups,
1941};
1942
1943static int __init kbd_led_init(struct device *dev)
1944{
 
 
1945	kbd_init();
1946	if (!kbd_led_present)
1947		return -ENODEV;
1948	if (!kbd_als_supported)
1949		kbd_led_groups[1] = NULL;
1950	kbd_led.max_brightness = kbd_get_max_level();
1951	if (!kbd_led.max_brightness) {
1952		kbd_led.max_brightness = kbd_get_valid_token_counts();
1953		if (kbd_led.max_brightness)
1954			kbd_led.max_brightness--;
1955	}
1956	return led_classdev_register(dev, &kbd_led);
 
 
 
 
 
 
 
1957}
1958
1959static void brightness_set_exit(struct led_classdev *led_cdev,
1960				enum led_brightness value)
1961{
1962	/* Don't change backlight level on exit */
1963};
1964
1965static void kbd_led_exit(void)
1966{
1967	if (!kbd_led_present)
1968		return;
1969	kbd_led.brightness_set = brightness_set_exit;
1970	led_classdev_unregister(&kbd_led);
1971}
1972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1973static int __init dell_init(void)
1974{
1975	struct calling_interface_buffer *buffer;
1976	struct calling_interface_token *token;
1977	int max_intensity = 0;
1978	int ret;
1979
1980	if (!dmi_check_system(dell_device_table))
1981		return -ENODEV;
1982
1983	quirks = NULL;
1984	/* find if this machine support other functions */
1985	dmi_check_system(dell_quirks);
1986
1987	ret = platform_driver_register(&platform_driver);
1988	if (ret)
1989		goto fail_platform_driver;
1990	platform_device = platform_device_alloc("dell-laptop", -1);
1991	if (!platform_device) {
1992		ret = -ENOMEM;
1993		goto fail_platform_device1;
1994	}
1995	ret = platform_device_add(platform_device);
1996	if (ret)
1997		goto fail_platform_device2;
1998
1999	ret = dell_setup_rfkill();
2000
2001	if (ret) {
2002		pr_warn("Unable to setup rfkill\n");
2003		goto fail_rfkill;
2004	}
2005
2006	if (quirks && quirks->touchpad_led)
2007		touchpad_led_init(&platform_device->dev);
2008
2009	kbd_led_init(&platform_device->dev);
2010
2011	dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2012	if (dell_laptop_dir != NULL)
2013		debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2014				    &dell_debugfs_fops);
 
 
 
 
 
 
 
 
 
2015
2016	if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2017		return 0;
2018
2019	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2020	if (token) {
2021		buffer = dell_smbios_get_buffer();
2022		buffer->input[0] = token->location;
2023		dell_smbios_send_request(0, 2);
2024		if (buffer->output[0] == 0)
2025			max_intensity = buffer->output[3];
2026		dell_smbios_release_buffer();
 
2027	}
2028
2029	if (max_intensity) {
2030		struct backlight_properties props;
2031		memset(&props, 0, sizeof(struct backlight_properties));
2032		props.type = BACKLIGHT_PLATFORM;
2033		props.max_brightness = max_intensity;
2034		dell_backlight_device = backlight_device_register("dell_backlight",
2035								  &platform_device->dev,
2036								  NULL,
2037								  &dell_ops,
2038								  &props);
2039
2040		if (IS_ERR(dell_backlight_device)) {
2041			ret = PTR_ERR(dell_backlight_device);
2042			dell_backlight_device = NULL;
2043			goto fail_backlight;
2044		}
2045
2046		dell_backlight_device->props.brightness =
2047			dell_get_intensity(dell_backlight_device);
 
 
 
 
2048		backlight_update_status(dell_backlight_device);
2049	}
2050
2051	return 0;
2052
 
 
2053fail_backlight:
 
 
2054	dell_cleanup_rfkill();
2055fail_rfkill:
2056	platform_device_del(platform_device);
2057fail_platform_device2:
2058	platform_device_put(platform_device);
2059fail_platform_device1:
2060	platform_driver_unregister(&platform_driver);
2061fail_platform_driver:
2062	return ret;
2063}
2064
2065static void __exit dell_exit(void)
2066{
 
2067	debugfs_remove_recursive(dell_laptop_dir);
2068	if (quirks && quirks->touchpad_led)
2069		touchpad_led_exit();
2070	kbd_led_exit();
2071	backlight_device_unregister(dell_backlight_device);
 
2072	dell_cleanup_rfkill();
2073	if (platform_device) {
2074		platform_device_unregister(platform_device);
2075		platform_driver_unregister(&platform_driver);
2076	}
2077}
2078
2079/* dell-rbtn.c driver export functions which will not work correctly (and could
2080 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2081 * not problem when dell-rbtn.c is compiled as external module. When both files
2082 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2083 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2084 * This can be achieved by late_initcall() instead module_init().
2085 */
2086late_initcall(dell_init);
2087module_exit(dell_exit);
2088
2089MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2090MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2091MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2092MODULE_DESCRIPTION("Dell laptop driver");
2093MODULE_LICENSE("GPL");