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