Linux Audio

Check our new training course

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