Thomas Gleixner | 1621633 | 2019-05-19 15:51:31 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Henrique de Moraes Holschuh | 643f12d | 2007-03-29 01:58:43 -0300 | [diff] [blame] | 3 | * thinkpad_acpi.c - ThinkPad ACPI Extras |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5 | * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net> |
Henrique de Moraes Holschuh | 1c762ca | 2009-04-04 04:25:42 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br> |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7 | */ |
| 8 | |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 11 | #define TPACPI_VERSION "0.26" |
Stanislav Fomichev | 6b99e35 | 2017-06-20 20:45:13 -0700 | [diff] [blame] | 12 | #define TPACPI_SYSFS_VERSION 0x030000 |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 13 | |
| 14 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Changelog: |
Henrique de Moraes Holschuh | 4b45cc0 | 2008-01-08 13:02:46 -0200 | [diff] [blame] | 16 | * 2007-10-20 changelog trimmed down |
| 17 | * |
Henrique de Moraes Holschuh | 643f12d | 2007-03-29 01:58:43 -0300 | [diff] [blame] | 18 | * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to |
| 19 | * drivers/misc. |
Henrique de Moraes Holschuh | f9ff43a | 2006-11-25 16:37:38 -0200 | [diff] [blame] | 20 | * |
| 21 | * 2006-11-22 0.13 new maintainer |
| 22 | * changelog now lives in git commit history, and will |
| 23 | * not be updated further in-file. |
Henrique de Moraes Holschuh | 837ca6d | 2007-03-23 17:33:54 -0300 | [diff] [blame] | 24 | * |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 25 | * 2005-03-17 0.11 support for 600e, 770x |
| 26 | * thanks to Jamie Lentin <lentinj@dial.pipex.com> |
Henrique de Moraes Holschuh | 4b45cc0 | 2008-01-08 13:02:46 -0200 | [diff] [blame] | 27 | * |
| 28 | * 2005-01-16 0.9 use MODULE_VERSION |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 29 | * thanks to Henrik Brix Andersen <brix@gentoo.org> |
| 30 | * fix parameter passing on module loading |
| 31 | * thanks to Rusty Russell <rusty@rustcorp.com.au> |
| 32 | * thanks to Jim Radford <radford@blackbean.org> |
| 33 | * 2004-11-08 0.8 fix init error case, don't return from a macro |
| 34 | * thanks to Chris Wright <chrisw@osdl.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
| 36 | |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 37 | #include <linux/kernel.h> |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/types.h> |
| 41 | #include <linux/string.h> |
| 42 | #include <linux/list.h> |
| 43 | #include <linux/mutex.h> |
Henrique de Moraes Holschuh | 73a94d8 | 2009-04-04 04:25:47 +0000 | [diff] [blame] | 44 | #include <linux/sched.h> |
Eric W. Biederman | 7a36094 | 2017-09-26 12:45:33 -0500 | [diff] [blame] | 45 | #include <linux/sched/signal.h> |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 46 | #include <linux/kthread.h> |
| 47 | #include <linux/freezer.h> |
| 48 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 49 | #include <linux/slab.h> |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 50 | #include <linux/nvram.h> |
| 51 | #include <linux/proc_fs.h> |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 52 | #include <linux/seq_file.h> |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 53 | #include <linux/sysfs.h> |
| 54 | #include <linux/backlight.h> |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 55 | #include <linux/bitops.h> |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 56 | #include <linux/fb.h> |
| 57 | #include <linux/platform_device.h> |
| 58 | #include <linux/hwmon.h> |
| 59 | #include <linux/hwmon-sysfs.h> |
| 60 | #include <linux/input.h> |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 61 | #include <linux/leds.h> |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 62 | #include <linux/rfkill.h> |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 63 | #include <linux/dmi.h> |
| 64 | #include <linux/jiffies.h> |
| 65 | #include <linux/workqueue.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 66 | #include <linux/acpi.h> |
Jiaxun Yang | f7db839 | 2019-03-07 17:37:16 +0800 | [diff] [blame] | 67 | #include <linux/pci.h> |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 68 | #include <linux/power_supply.h> |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 69 | #include <linux/platform_profile.h> |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 70 | #include <sound/core.h> |
| 71 | #include <sound/control.h> |
| 72 | #include <sound/initval.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 73 | #include <linux/uaccess.h> |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 74 | #include <acpi/battery.h> |
Hans de Goede | b33c6ce | 2015-06-16 16:28:10 +0200 | [diff] [blame] | 75 | #include <acpi/video.h> |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 76 | #include <drm/drm_privacy_screen_driver.h> |
Hans de Goede | 153cca9 | 2021-07-29 10:21:34 +0200 | [diff] [blame] | 77 | #include "dual_accel_detect.h" |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 78 | |
| 79 | /* ThinkPad CMOS commands */ |
| 80 | #define TP_CMOS_VOLUME_DOWN 0 |
| 81 | #define TP_CMOS_VOLUME_UP 1 |
| 82 | #define TP_CMOS_VOLUME_MUTE 2 |
| 83 | #define TP_CMOS_BRIGHTNESS_UP 4 |
| 84 | #define TP_CMOS_BRIGHTNESS_DOWN 5 |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 85 | #define TP_CMOS_THINKLIGHT_ON 12 |
| 86 | #define TP_CMOS_THINKLIGHT_OFF 13 |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 87 | |
| 88 | /* NVRAM Addresses */ |
| 89 | enum tp_nvram_addr { |
| 90 | TP_NVRAM_ADDR_HK2 = 0x57, |
| 91 | TP_NVRAM_ADDR_THINKLIGHT = 0x58, |
| 92 | TP_NVRAM_ADDR_VIDEO = 0x59, |
| 93 | TP_NVRAM_ADDR_BRIGHTNESS = 0x5e, |
| 94 | TP_NVRAM_ADDR_MIXER = 0x60, |
| 95 | }; |
| 96 | |
| 97 | /* NVRAM bit masks */ |
| 98 | enum { |
| 99 | TP_NVRAM_MASK_HKT_THINKPAD = 0x08, |
| 100 | TP_NVRAM_MASK_HKT_ZOOM = 0x20, |
| 101 | TP_NVRAM_MASK_HKT_DISPLAY = 0x40, |
| 102 | TP_NVRAM_MASK_HKT_HIBERNATE = 0x80, |
| 103 | TP_NVRAM_MASK_THINKLIGHT = 0x10, |
| 104 | TP_NVRAM_MASK_HKT_DISPEXPND = 0x30, |
| 105 | TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20, |
| 106 | TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f, |
| 107 | TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0, |
| 108 | TP_NVRAM_MASK_MUTE = 0x40, |
| 109 | TP_NVRAM_MASK_HKT_VOLUME = 0x80, |
| 110 | TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f, |
| 111 | TP_NVRAM_POS_LEVEL_VOLUME = 0, |
| 112 | }; |
| 113 | |
Henrique de Moraes Holschuh | 5d756db | 2010-05-16 19:45:28 -0300 | [diff] [blame] | 114 | /* Misc NVRAM-related */ |
| 115 | enum { |
| 116 | TP_NVRAM_LEVEL_VOLUME_MAX = 14, |
| 117 | }; |
| 118 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 119 | /* ACPI HIDs */ |
Manoj Iyer | 9fbdaeb | 2011-05-08 18:04:29 -0400 | [diff] [blame] | 120 | #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068" |
| 121 | #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068" |
Hui Wang | a3c42a4 | 2016-11-08 16:13:23 +0800 | [diff] [blame] | 122 | #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268" |
Henrique de Moraes Holschuh | 437e470 | 2010-05-16 19:45:43 -0300 | [diff] [blame] | 123 | #define TPACPI_ACPI_EC_HID "PNP0C09" |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 124 | |
| 125 | /* Input IDs */ |
| 126 | #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */ |
| 127 | #define TPACPI_HKEY_INPUT_VERSION 0x4101 |
| 128 | |
Henrique de Moraes Holschuh | 153f822 | 2009-01-11 03:01:01 -0200 | [diff] [blame] | 129 | /* ACPI \WGSV commands */ |
| 130 | enum { |
| 131 | TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */ |
| 132 | TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */ |
| 133 | TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */ |
| 134 | TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */ |
| 135 | }; |
| 136 | |
| 137 | /* TP_ACPI_WGSV_GET_STATE bits */ |
| 138 | enum { |
| 139 | TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */ |
| 140 | TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */ |
| 141 | TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */ |
| 142 | TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */ |
| 143 | TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */ |
| 144 | TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */ |
| 145 | TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */ |
| 146 | TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */ |
| 147 | TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */ |
| 148 | TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */ |
| 149 | }; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 150 | |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 151 | /* HKEY events */ |
| 152 | enum tpacpi_hkey_event_t { |
| 153 | /* Hotkey-related */ |
| 154 | TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */ |
| 155 | TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */ |
| 156 | TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */ |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 157 | TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */ |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 158 | TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */ |
| 159 | TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */ |
| 160 | TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */ |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 161 | TP_HKEY_EV_PRIVACYGUARD_TOGGLE = 0x130f, /* Toggle priv.guard on/off */ |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 162 | |
| 163 | /* Reasons for waking up from S3/S4 */ |
| 164 | TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */ |
| 165 | TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */ |
| 166 | TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */ |
| 167 | TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */ |
| 168 | TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */ |
| 169 | TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */ |
| 170 | |
| 171 | /* Auto-sleep after eject request */ |
| 172 | TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */ |
| 173 | TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */ |
| 174 | |
| 175 | /* Misc bay events */ |
| 176 | TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */ |
Henrique de Moraes Holschuh | a50245a | 2011-06-05 16:22:35 -0300 | [diff] [blame] | 177 | TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock |
| 178 | or port replicator */ |
| 179 | TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug |
| 180 | dock or port replicator */ |
Alexander Kobel | 0a053f0 | 2021-02-13 16:13:36 +0100 | [diff] [blame] | 181 | /* |
| 182 | * Thinkpad X1 Tablet series devices emit 0x4012 and 0x4013 |
| 183 | * when keyboard cover is attached, detached or folded onto the back |
| 184 | */ |
| 185 | TP_HKEY_EV_KBD_COVER_ATTACH = 0x4012, /* keyboard cover attached */ |
| 186 | TP_HKEY_EV_KBD_COVER_DETACH = 0x4013, /* keyboard cover detached or folded back */ |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 187 | |
| 188 | /* User-interface events */ |
| 189 | TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */ |
| 190 | TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */ |
| 191 | TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */ |
| 192 | TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */ |
Lyude | b03f4d4 | 2016-11-11 15:15:03 -0500 | [diff] [blame] | 193 | TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016): |
| 194 | * enter/leave tablet mode |
| 195 | */ |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 196 | TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */ |
| 197 | TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */ |
| 198 | TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */ |
| 199 | |
Henrique de Moraes Holschuh | 2d43f67 | 2011-06-05 16:22:34 -0300 | [diff] [blame] | 200 | /* Key-related user-interface events */ |
| 201 | TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */ |
| 202 | TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */ |
Xavier Naveira | 67ab624 | 2015-02-10 08:45:18 +0100 | [diff] [blame] | 203 | TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */ |
Henrique de Moraes Holschuh | 2d43f67 | 2011-06-05 16:22:34 -0300 | [diff] [blame] | 204 | |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 205 | /* Thermal events */ |
| 206 | TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */ |
| 207 | TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */ |
| 208 | TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */ |
| 209 | TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */ |
Henrique de Moraes Holschuh | 6e6bc5f6 | 2018-04-24 16:56:03 -0300 | [diff] [blame] | 210 | TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */ |
| 211 | TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set |
| 212 | * command completed. Related to |
| 213 | * AML DYTC */ |
| 214 | TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation |
| 215 | * changed. Related to AML GMTS */ |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 216 | |
Richard Hartmann | 6f62bc3 | 2012-12-29 22:51:49 +0100 | [diff] [blame] | 217 | /* AC-related events */ |
| 218 | TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */ |
Henrique de Moraes Holschuh | 2d43f67 | 2011-06-05 16:22:34 -0300 | [diff] [blame] | 219 | |
David Herrmann | 587d862 | 2018-01-12 12:04:45 +0100 | [diff] [blame] | 220 | /* Further user-interface events */ |
| 221 | TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */ |
| 222 | TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */ |
| 223 | |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 224 | /* Misc */ |
| 225 | TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */ |
| 226 | }; |
| 227 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 228 | /**************************************************************************** |
| 229 | * Main driver |
| 230 | */ |
| 231 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 232 | #define TPACPI_NAME "thinkpad" |
| 233 | #define TPACPI_DESC "ThinkPad ACPI Extras" |
| 234 | #define TPACPI_FILE TPACPI_NAME "_acpi" |
| 235 | #define TPACPI_URL "http://ibm-acpi.sf.net/" |
| 236 | #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net" |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 237 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 238 | #define TPACPI_PROC_DIR "ibm" |
| 239 | #define TPACPI_ACPI_EVENT_PREFIX "ibm" |
| 240 | #define TPACPI_DRVR_NAME TPACPI_FILE |
Henrique de Moraes Holschuh | 95e57ab | 2008-04-26 01:02:22 -0300 | [diff] [blame] | 241 | #define TPACPI_DRVR_SHORTNAME "tpacpi" |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 242 | #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon" |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 243 | |
Henrique de Moraes Holschuh | 95e57ab | 2008-04-26 01:02:22 -0300 | [diff] [blame] | 244 | #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd" |
Henrique de Moraes Holschuh | e0e3c06 | 2008-04-26 01:02:28 -0300 | [diff] [blame] | 245 | #define TPACPI_WORKQUEUE_NAME "ktpacpid" |
Henrique de Moraes Holschuh | 95e57ab | 2008-04-26 01:02:22 -0300 | [diff] [blame] | 246 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 247 | #define TPACPI_MAX_ACPI_ARGS 3 |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 248 | |
Henrique de Moraes Holschuh | 7ff8d62 | 2009-04-04 04:25:46 +0000 | [diff] [blame] | 249 | /* Debugging printk groups */ |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 250 | #define TPACPI_DBG_ALL 0xffff |
Henrique de Moraes Holschuh | 73a94d8 | 2009-04-04 04:25:47 +0000 | [diff] [blame] | 251 | #define TPACPI_DBG_DISCLOSETASK 0x8000 |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 252 | #define TPACPI_DBG_INIT 0x0001 |
| 253 | #define TPACPI_DBG_EXIT 0x0002 |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 254 | #define TPACPI_DBG_RFKILL 0x0004 |
Henrique de Moraes Holschuh | 56e2c20 | 2009-04-04 04:25:51 +0000 | [diff] [blame] | 255 | #define TPACPI_DBG_HKEY 0x0008 |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 256 | #define TPACPI_DBG_FAN 0x0010 |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 257 | #define TPACPI_DBG_BRGHT 0x0020 |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 258 | #define TPACPI_DBG_MIXER 0x0040 |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 259 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 260 | #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off") |
| 261 | #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled") |
| 262 | #define strlencmp(a, b) (strncmp((a), (b), strlen(b))) |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 263 | |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 264 | |
| 265 | /**************************************************************************** |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 266 | * Driver-wide structs and misc. variables |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 267 | */ |
| 268 | |
| 269 | struct ibm_struct; |
| 270 | |
| 271 | struct tp_acpi_drv_struct { |
| 272 | const struct acpi_device_id *hid; |
| 273 | struct acpi_driver *driver; |
| 274 | |
| 275 | void (*notify) (struct ibm_struct *, u32); |
| 276 | acpi_handle *handle; |
| 277 | u32 type; |
| 278 | struct acpi_device *device; |
| 279 | }; |
| 280 | |
| 281 | struct ibm_struct { |
| 282 | char *name; |
| 283 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 284 | int (*read) (struct seq_file *); |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 285 | int (*write) (char *); |
| 286 | void (*exit) (void); |
| 287 | void (*resume) (void); |
Rafael J. Wysocki | fd3c3a4 | 2012-06-27 23:18:44 +0200 | [diff] [blame] | 288 | void (*suspend) (void); |
Henrique de Moraes Holschuh | 90d9d3c | 2009-01-11 03:01:02 -0200 | [diff] [blame] | 289 | void (*shutdown) (void); |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 290 | |
| 291 | struct list_head all_drivers; |
| 292 | |
| 293 | struct tp_acpi_drv_struct *acpi; |
| 294 | |
| 295 | struct { |
| 296 | u8 acpi_driver_registered:1; |
| 297 | u8 acpi_notify_installed:1; |
| 298 | u8 proc_created:1; |
| 299 | u8 init_called:1; |
| 300 | u8 experimental:1; |
| 301 | } flags; |
| 302 | }; |
| 303 | |
| 304 | struct ibm_init_struct { |
| 305 | char param[32]; |
| 306 | |
| 307 | int (*init) (struct ibm_init_struct *); |
Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 308 | umode_t base_procfs_mode; |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 309 | struct ibm_struct *data; |
| 310 | }; |
| 311 | |
| 312 | static struct { |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 313 | u32 bluetooth:1; |
| 314 | u32 hotkey:1; |
| 315 | u32 hotkey_mask:1; |
| 316 | u32 hotkey_wlsw:1; |
Lyude | b318002 | 2016-11-11 15:15:02 -0500 | [diff] [blame] | 317 | enum { |
| 318 | TP_HOTKEY_TABLET_NONE = 0, |
| 319 | TP_HOTKEY_TABLET_USES_MHKG, |
Benjamin Berg | dda3ec0 | 2017-09-15 15:20:49 +0200 | [diff] [blame] | 320 | TP_HOTKEY_TABLET_USES_GMMS, |
Lyude | b318002 | 2016-11-11 15:15:02 -0500 | [diff] [blame] | 321 | } hotkey_tablet; |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 322 | u32 kbdlight:1; |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 323 | u32 light:1; |
| 324 | u32 light_status:1; |
Henrique de Moraes Holschuh | b597279 | 2008-04-26 01:02:17 -0300 | [diff] [blame] | 325 | u32 bright_acpimode:1; |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 326 | u32 bright_unkfw:1; |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 327 | u32 wan:1; |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 328 | u32 uwb:1; |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 329 | u32 fan_ctrl_status_undef:1; |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 330 | u32 second_fan:1; |
Lars Hofhansl | 14232c6 | 2020-04-23 14:57:09 -0700 | [diff] [blame] | 331 | u32 second_fan_ctl:1; |
Henrique de Moraes Holschuh | 6020173 | 2009-05-30 13:25:07 -0300 | [diff] [blame] | 332 | u32 beep_needs_two_args:1; |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 333 | u32 mixer_no_level_control:1; |
Jouke Witteveen | 1a32ebb | 2018-07-11 11:45:36 +0200 | [diff] [blame] | 334 | u32 battery_force_primary:1; |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 335 | u32 input_device_registered:1; |
| 336 | u32 platform_drv_registered:1; |
| 337 | u32 platform_drv_attrs_registered:1; |
| 338 | u32 sensors_pdrv_registered:1; |
| 339 | u32 sensors_pdrv_attrs_registered:1; |
| 340 | u32 sensors_pdev_attrs_registered:1; |
| 341 | u32 hotkey_poll_active:1; |
Bastien Nocera | f23a5bc | 2015-03-02 14:45:16 +0100 | [diff] [blame] | 342 | u32 has_adaptive_kbd:1; |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 343 | } tp_features; |
| 344 | |
Henrique de Moraes Holschuh | 9288902 | 2008-04-26 01:02:18 -0300 | [diff] [blame] | 345 | static struct { |
| 346 | u16 hotkey_mask_ff:1; |
Henrique de Moraes Holschuh | c7ac629 | 2009-12-15 21:51:10 -0200 | [diff] [blame] | 347 | u16 volume_ctrl_forbidden:1; |
Henrique de Moraes Holschuh | 9288902 | 2008-04-26 01:02:18 -0300 | [diff] [blame] | 348 | } tp_warned; |
| 349 | |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 350 | struct thinkpad_id_data { |
| 351 | unsigned int vendor; /* ThinkPad vendor: |
| 352 | * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */ |
| 353 | |
| 354 | char *bios_version_str; /* Something like 1ZET51WW (1.03z) */ |
| 355 | char *ec_version_str; /* Something like 1ZHT51WW-1.04a */ |
| 356 | |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 357 | u32 bios_model; /* 1Y = 0x3159, 0 = unknown */ |
| 358 | u32 ec_model; |
| 359 | u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */ |
Henrique de Moraes Holschuh | 050df10 | 2009-05-30 13:25:05 -0300 | [diff] [blame] | 360 | u16 ec_release; |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 361 | |
Henrique de Moraes Holschuh | 8c74adb | 2008-04-26 01:02:19 -0300 | [diff] [blame] | 362 | char *model_str; /* ThinkPad T43 */ |
| 363 | char *nummodel_str; /* 9384A9C for a 9384-A9C model */ |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 364 | }; |
Henrique de Moraes Holschuh | 0c78039 | 2008-01-08 13:02:43 -0200 | [diff] [blame] | 365 | static struct thinkpad_id_data thinkpad_id; |
| 366 | |
Henrique de Moraes Holschuh | 8fef502 | 2007-09-23 11:39:02 -0300 | [diff] [blame] | 367 | static enum { |
| 368 | TPACPI_LIFE_INIT = 0, |
| 369 | TPACPI_LIFE_RUNNING, |
| 370 | TPACPI_LIFE_EXITING, |
| 371 | } tpacpi_lifecycle; |
| 372 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 373 | static int experimental; |
| 374 | static u32 dbg_level; |
| 375 | |
Henrique de Moraes Holschuh | e0e3c06 | 2008-04-26 01:02:28 -0300 | [diff] [blame] | 376 | static struct workqueue_struct *tpacpi_wq; |
| 377 | |
Henrique de Moraes Holschuh | 75bd3bf | 2009-04-14 02:44:11 +0000 | [diff] [blame] | 378 | enum led_status_t { |
| 379 | TPACPI_LED_OFF = 0, |
| 380 | TPACPI_LED_ON, |
| 381 | TPACPI_LED_BLINK, |
| 382 | }; |
| 383 | |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 384 | /* tpacpi LED class */ |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 385 | struct tpacpi_led_classdev { |
| 386 | struct led_classdev led_classdev; |
Adam Lee | edf2d77 | 2013-06-08 16:51:15 +0800 | [diff] [blame] | 387 | int led; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 388 | }; |
| 389 | |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 390 | /* brightness level capabilities */ |
| 391 | static unsigned int bright_maxlvl; /* 0 = unknown */ |
| 392 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 393 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 394 | static int dbg_wlswemul; |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 395 | static bool tpacpi_wlsw_emulstate; |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 396 | static int dbg_bluetoothemul; |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 397 | static bool tpacpi_bluetooth_emulstate; |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 398 | static int dbg_wwanemul; |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 399 | static bool tpacpi_wwan_emulstate; |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 400 | static int dbg_uwbemul; |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 401 | static bool tpacpi_uwb_emulstate; |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 402 | #endif |
| 403 | |
| 404 | |
Henrique de Moraes Holschuh | 3dcc2c3 | 2009-04-04 04:25:45 +0000 | [diff] [blame] | 405 | /************************************************************************* |
| 406 | * Debugging helpers |
| 407 | */ |
| 408 | |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 409 | #define dbg_printk(a_dbg_level, format, arg...) \ |
| 410 | do { \ |
| 411 | if (dbg_level & (a_dbg_level)) \ |
| 412 | printk(KERN_DEBUG pr_fmt("%s: " format), \ |
| 413 | __func__, ##arg); \ |
| 414 | } while (0) |
Henrique de Moraes Holschuh | 3dcc2c3 | 2009-04-04 04:25:45 +0000 | [diff] [blame] | 415 | |
| 416 | #ifdef CONFIG_THINKPAD_ACPI_DEBUG |
| 417 | #define vdbg_printk dbg_printk |
| 418 | static const char *str_supported(int is_supported); |
| 419 | #else |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 420 | static inline const char *str_supported(int is_supported) { return ""; } |
| 421 | #define vdbg_printk(a_dbg_level, format, arg...) \ |
Joe Perches | efd85cf | 2015-08-26 11:13:38 -0700 | [diff] [blame] | 422 | do { if (0) no_printk(format, ##arg); } while (0) |
Henrique de Moraes Holschuh | 3dcc2c3 | 2009-04-04 04:25:45 +0000 | [diff] [blame] | 423 | #endif |
| 424 | |
Henrique de Moraes Holschuh | 73a94d8 | 2009-04-04 04:25:47 +0000 | [diff] [blame] | 425 | static void tpacpi_log_usertask(const char * const what) |
| 426 | { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 427 | printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"), |
| 428 | what, task_tgid_vnr(current)); |
Henrique de Moraes Holschuh | 73a94d8 | 2009-04-04 04:25:47 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 431 | #define tpacpi_disclose_usertask(what, format, arg...) \ |
| 432 | do { \ |
| 433 | if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \ |
| 434 | (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \ |
| 435 | printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \ |
| 436 | what, task_tgid_vnr(current), ## arg); \ |
| 437 | } \ |
| 438 | } while (0) |
Henrique de Moraes Holschuh | 3dcc2c3 | 2009-04-04 04:25:45 +0000 | [diff] [blame] | 439 | |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 440 | /* |
| 441 | * Quirk handling helpers |
| 442 | * |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 443 | * ThinkPad IDs and versions seen in the field so far are |
| 444 | * two or three characters from the set [0-9A-Z], i.e. base 36. |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 445 | * |
| 446 | * We use values well outside that range as specials. |
| 447 | */ |
| 448 | |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 449 | #define TPACPI_MATCH_ANY 0xffffffffU |
| 450 | #define TPACPI_MATCH_ANY_VERSION 0xffffU |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 451 | #define TPACPI_MATCH_UNKNOWN 0U |
| 452 | |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 453 | /* TPID('1', 'Y') == 0x3159 */ |
| 454 | #define TPID(__c1, __c2) (((__c1) << 8) | (__c2)) |
| 455 | #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3)) |
| 456 | #define TPVER TPID |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 457 | |
| 458 | #define TPACPI_Q_IBM(__id1, __id2, __quirk) \ |
| 459 | { .vendor = PCI_VENDOR_ID_IBM, \ |
| 460 | .bios = TPID(__id1, __id2), \ |
| 461 | .ec = TPACPI_MATCH_ANY, \ |
| 462 | .quirks = (__quirk) } |
| 463 | |
| 464 | #define TPACPI_Q_LNV(__id1, __id2, __quirk) \ |
| 465 | { .vendor = PCI_VENDOR_ID_LENOVO, \ |
| 466 | .bios = TPID(__id1, __id2), \ |
| 467 | .ec = TPACPI_MATCH_ANY, \ |
| 468 | .quirks = (__quirk) } |
| 469 | |
Jouke Witteveen | 1a32ebb | 2018-07-11 11:45:36 +0200 | [diff] [blame] | 470 | #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \ |
| 471 | { .vendor = PCI_VENDOR_ID_LENOVO, \ |
| 472 | .bios = TPID3(__id1, __id2, __id3), \ |
| 473 | .ec = TPACPI_MATCH_ANY, \ |
| 474 | .quirks = (__quirk) } |
| 475 | |
Jouke Witteveen | 599eeff | 2018-12-05 12:17:52 +0100 | [diff] [blame] | 476 | #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \ |
| 477 | { .vendor = PCI_VENDOR_ID_IBM, \ |
| 478 | .bios = TPACPI_MATCH_ANY, \ |
| 479 | .ec = TPID(__id1, __id2), \ |
| 480 | .quirks = (__quirk) } |
| 481 | |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 482 | #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \ |
| 483 | { .vendor = PCI_VENDOR_ID_LENOVO, \ |
| 484 | .bios = TPACPI_MATCH_ANY, \ |
| 485 | .ec = TPID(__id1, __id2), \ |
| 486 | .quirks = (__quirk) } |
| 487 | |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 488 | struct tpacpi_quirk { |
| 489 | unsigned int vendor; |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 490 | u32 bios; |
| 491 | u32 ec; |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 492 | unsigned long quirks; |
| 493 | }; |
| 494 | |
| 495 | /** |
| 496 | * tpacpi_check_quirks() - search BIOS/EC version on a list |
| 497 | * @qlist: array of &struct tpacpi_quirk |
| 498 | * @qlist_size: number of elements in @qlist |
| 499 | * |
| 500 | * Iterates over a quirks list until one is found that matches the |
| 501 | * ThinkPad's vendor, BIOS and EC model. |
| 502 | * |
| 503 | * Returns 0 if nothing matches, otherwise returns the quirks field of |
| 504 | * the matching &struct tpacpi_quirk entry. |
| 505 | * |
| 506 | * The match criteria is: vendor, ec and bios much match. |
| 507 | */ |
| 508 | static unsigned long __init tpacpi_check_quirks( |
| 509 | const struct tpacpi_quirk *qlist, |
| 510 | unsigned int qlist_size) |
| 511 | { |
| 512 | while (qlist_size) { |
| 513 | if ((qlist->vendor == thinkpad_id.vendor || |
| 514 | qlist->vendor == TPACPI_MATCH_ANY) && |
| 515 | (qlist->bios == thinkpad_id.bios_model || |
| 516 | qlist->bios == TPACPI_MATCH_ANY) && |
| 517 | (qlist->ec == thinkpad_id.ec_model || |
| 518 | qlist->ec == TPACPI_MATCH_ANY)) |
| 519 | return qlist->quirks; |
| 520 | |
| 521 | qlist_size--; |
| 522 | qlist++; |
| 523 | } |
| 524 | return 0; |
| 525 | } |
| 526 | |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 527 | static inline bool __pure __init tpacpi_is_lenovo(void) |
| 528 | { |
| 529 | return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO; |
| 530 | } |
| 531 | |
| 532 | static inline bool __pure __init tpacpi_is_ibm(void) |
| 533 | { |
| 534 | return thinkpad_id.vendor == PCI_VENDOR_ID_IBM; |
| 535 | } |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 536 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 537 | /**************************************************************************** |
| 538 | **************************************************************************** |
| 539 | * |
| 540 | * ACPI Helpers and device model |
| 541 | * |
| 542 | **************************************************************************** |
| 543 | ****************************************************************************/ |
| 544 | |
| 545 | /************************************************************************* |
| 546 | * ACPI basic handles |
| 547 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Henrique de Moraes Holschuh | 94954cc6 | 2007-07-18 23:45:27 -0300 | [diff] [blame] | 549 | static acpi_handle root_handle; |
Henrique de Moraes Holschuh | 437e470 | 2010-05-16 19:45:43 -0300 | [diff] [blame] | 550 | static acpi_handle ec_handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 552 | #define TPACPI_HANDLE(object, parent, paths...) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | static acpi_handle object##_handle; \ |
Andi Kleen | 3bd0189 | 2012-10-04 17:12:01 -0700 | [diff] [blame] | 554 | static const acpi_handle * const object##_parent __initconst = \ |
Henrique de Moraes Holschuh | ef07a5ab | 2010-05-16 19:45:54 -0300 | [diff] [blame] | 555 | &parent##_handle; \ |
| 556 | static char *object##_paths[] __initdata = { paths } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 558 | TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */ |
| 559 | TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 561 | TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */ |
| 562 | /* T4x, X31, X40 */ |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 563 | "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */ |
| 564 | "\\CMS", /* R40, R40e */ |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 565 | ); /* all others */ |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 566 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 567 | TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */ |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 568 | "^HKEY", /* R30, R31 */ |
| 569 | "HKEY", /* all others */ |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 570 | ); /* 570 */ |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 571 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 572 | /************************************************************************* |
| 573 | * ACPI helpers |
Henrique de Moraes Holschuh | a8b7a66 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 574 | */ |
| 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | static int acpi_evalf(acpi_handle handle, |
Dan Carpenter | eceeb43 | 2012-09-01 12:54:07 -0700 | [diff] [blame] | 577 | int *res, char *method, char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
| 579 | char *fmt0 = fmt; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 580 | struct acpi_object_list params; |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 581 | union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS]; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 582 | struct acpi_buffer result, *resultp; |
| 583 | union acpi_object out_obj; |
| 584 | acpi_status status; |
| 585 | va_list ap; |
| 586 | char res_type; |
| 587 | int success; |
| 588 | int quiet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | if (!*fmt) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 591 | pr_err("acpi_evalf() called with empty format\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | if (*fmt == 'q') { |
| 596 | quiet = 1; |
| 597 | fmt++; |
| 598 | } else |
| 599 | quiet = 0; |
| 600 | |
| 601 | res_type = *(fmt++); |
| 602 | |
| 603 | params.count = 0; |
| 604 | params.pointer = &in_objs[0]; |
| 605 | |
| 606 | va_start(ap, fmt); |
| 607 | while (*fmt) { |
| 608 | char c = *(fmt++); |
| 609 | switch (c) { |
| 610 | case 'd': /* int */ |
| 611 | in_objs[params.count].integer.value = va_arg(ap, int); |
| 612 | in_objs[params.count++].type = ACPI_TYPE_INTEGER; |
| 613 | break; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 614 | /* add more types as needed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | default: |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 616 | pr_err("acpi_evalf() called with invalid format character '%c'\n", |
| 617 | c); |
Jesper Juhl | 2136585 | 2010-12-24 19:56:28 +0100 | [diff] [blame] | 618 | va_end(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | return 0; |
| 620 | } |
| 621 | } |
| 622 | va_end(ap); |
| 623 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 624 | if (res_type != 'v') { |
| 625 | result.length = sizeof(out_obj); |
| 626 | result.pointer = &out_obj; |
| 627 | resultp = &result; |
| 628 | } else |
| 629 | resultp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 631 | status = acpi_evaluate_object(handle, method, ¶ms, resultp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
| 633 | switch (res_type) { |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 634 | case 'd': /* int */ |
Henrique de Moraes Holschuh | 263f4a3 | 2010-05-16 19:45:45 -0300 | [diff] [blame] | 635 | success = (status == AE_OK && |
| 636 | out_obj.type == ACPI_TYPE_INTEGER); |
| 637 | if (success && res) |
Dan Carpenter | eceeb43 | 2012-09-01 12:54:07 -0700 | [diff] [blame] | 638 | *res = out_obj.integer.value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | break; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 640 | case 'v': /* void */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | success = status == AE_OK; |
| 642 | break; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 643 | /* add more types as needed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | default: |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 645 | pr_err("acpi_evalf() called with invalid format character '%c'\n", |
| 646 | res_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | if (!success && !quiet) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 651 | pr_err("acpi_evalf(%s, %s, ...) failed: %s\n", |
Henrique de Moraes Holschuh | 263f4a3 | 2010-05-16 19:45:45 -0300 | [diff] [blame] | 652 | method, fmt0, acpi_format_exception(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | return success; |
| 655 | } |
| 656 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 657 | static int acpi_ec_read(int i, u8 *p) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 658 | { |
| 659 | int v; |
| 660 | |
| 661 | if (ecrd_handle) { |
| 662 | if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i)) |
| 663 | return 0; |
| 664 | *p = v; |
| 665 | } else { |
| 666 | if (ec_read(i, p) < 0) |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | return 1; |
| 671 | } |
| 672 | |
| 673 | static int acpi_ec_write(int i, u8 v) |
| 674 | { |
| 675 | if (ecwr_handle) { |
| 676 | if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v)) |
| 677 | return 0; |
| 678 | } else { |
| 679 | if (ec_write(i, v) < 0) |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | return 1; |
| 684 | } |
| 685 | |
Henrique de Moraes Holschuh | c9bea99 | 2007-04-21 11:08:42 -0300 | [diff] [blame] | 686 | static int issue_thinkpad_cmos_command(int cmos_cmd) |
| 687 | { |
| 688 | if (!cmos_handle) |
| 689 | return -ENXIO; |
| 690 | |
| 691 | if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd)) |
| 692 | return -EIO; |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 697 | /************************************************************************* |
| 698 | * ACPI device model |
| 699 | */ |
| 700 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 701 | #define TPACPI_ACPIHANDLE_INIT(object) \ |
| 702 | drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \ |
Henrique de Moraes Holschuh | ef07a5ab | 2010-05-16 19:45:54 -0300 | [diff] [blame] | 703 | object##_paths, ARRAY_SIZE(object##_paths)) |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 704 | |
Henrique de Moraes Holschuh | ef07a5ab | 2010-05-16 19:45:54 -0300 | [diff] [blame] | 705 | static void __init drv_acpi_handle_init(const char *name, |
| 706 | acpi_handle *handle, const acpi_handle parent, |
| 707 | char **paths, const int num_paths) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 708 | { |
| 709 | int i; |
| 710 | acpi_status status; |
| 711 | |
Henrique de Moraes Holschuh | 5ae930e | 2007-04-27 22:00:14 -0300 | [diff] [blame] | 712 | vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n", |
| 713 | name); |
| 714 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 715 | for (i = 0; i < num_paths; i++) { |
| 716 | status = acpi_get_handle(parent, paths[i], handle); |
| 717 | if (ACPI_SUCCESS(status)) { |
Henrique de Moraes Holschuh | 5ae930e | 2007-04-27 22:00:14 -0300 | [diff] [blame] | 718 | dbg_printk(TPACPI_DBG_INIT, |
| 719 | "Found ACPI handle %s for %s\n", |
Henrique de Moraes Holschuh | ef07a5ab | 2010-05-16 19:45:54 -0300 | [diff] [blame] | 720 | paths[i], name); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 721 | return; |
| 722 | } |
| 723 | } |
| 724 | |
Henrique de Moraes Holschuh | 5ae930e | 2007-04-27 22:00:14 -0300 | [diff] [blame] | 725 | vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n", |
| 726 | name); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 727 | *handle = NULL; |
| 728 | } |
| 729 | |
Henrique de Moraes Holschuh | 437e470 | 2010-05-16 19:45:43 -0300 | [diff] [blame] | 730 | static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle, |
| 731 | u32 level, void *context, void **return_value) |
| 732 | { |
Aaron Lu | 46445b6 | 2013-10-11 21:27:46 +0800 | [diff] [blame] | 733 | struct acpi_device *dev; |
| 734 | if (!strcmp(context, "video")) { |
| 735 | if (acpi_bus_get_device(handle, &dev)) |
| 736 | return AE_OK; |
| 737 | if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev))) |
| 738 | return AE_OK; |
| 739 | } |
| 740 | |
Henrique de Moraes Holschuh | 437e470 | 2010-05-16 19:45:43 -0300 | [diff] [blame] | 741 | *(acpi_handle *)return_value = handle; |
| 742 | |
| 743 | return AE_CTRL_TERMINATE; |
| 744 | } |
| 745 | |
| 746 | static void __init tpacpi_acpi_handle_locate(const char *name, |
| 747 | const char *hid, |
| 748 | acpi_handle *handle) |
| 749 | { |
| 750 | acpi_status status; |
| 751 | acpi_handle device_found; |
| 752 | |
Aaron Lu | 46445b6 | 2013-10-11 21:27:46 +0800 | [diff] [blame] | 753 | BUG_ON(!name || !handle); |
Henrique de Moraes Holschuh | 437e470 | 2010-05-16 19:45:43 -0300 | [diff] [blame] | 754 | vdbg_printk(TPACPI_DBG_INIT, |
| 755 | "trying to locate ACPI handle for %s, using HID %s\n", |
Aaron Lu | 46445b6 | 2013-10-11 21:27:46 +0800 | [diff] [blame] | 756 | name, hid ? hid : "NULL"); |
Henrique de Moraes Holschuh | 437e470 | 2010-05-16 19:45:43 -0300 | [diff] [blame] | 757 | |
| 758 | memset(&device_found, 0, sizeof(device_found)); |
| 759 | status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback, |
| 760 | (void *)name, &device_found); |
| 761 | |
| 762 | *handle = NULL; |
| 763 | |
| 764 | if (ACPI_SUCCESS(status)) { |
| 765 | *handle = device_found; |
| 766 | dbg_printk(TPACPI_DBG_INIT, |
| 767 | "Found ACPI handle for %s\n", name); |
| 768 | } else { |
| 769 | vdbg_printk(TPACPI_DBG_INIT, |
| 770 | "Could not locate an ACPI handle for %s: %s\n", |
| 771 | name, acpi_format_exception(status)); |
| 772 | } |
| 773 | } |
| 774 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 775 | static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 776 | { |
| 777 | struct ibm_struct *ibm = data; |
| 778 | |
Henrique de Moraes Holschuh | 8fef502 | 2007-09-23 11:39:02 -0300 | [diff] [blame] | 779 | if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING) |
| 780 | return; |
| 781 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 782 | if (!ibm || !ibm->acpi || !ibm->acpi->notify) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 783 | return; |
| 784 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 785 | ibm->acpi->notify(ibm, event); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 786 | } |
| 787 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 788 | static int __init setup_acpi_notify(struct ibm_struct *ibm) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 789 | { |
| 790 | acpi_status status; |
Henrique de Moraes Holschuh | 5ae930e | 2007-04-27 22:00:14 -0300 | [diff] [blame] | 791 | int rc; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 792 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 793 | BUG_ON(!ibm->acpi); |
| 794 | |
| 795 | if (!*ibm->acpi->handle) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 796 | return 0; |
| 797 | |
Henrique de Moraes Holschuh | 5ae930e | 2007-04-27 22:00:14 -0300 | [diff] [blame] | 798 | vdbg_printk(TPACPI_DBG_INIT, |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 799 | "setting up ACPI notify for %s\n", ibm->name); |
| 800 | |
Henrique de Moraes Holschuh | 5ae930e | 2007-04-27 22:00:14 -0300 | [diff] [blame] | 801 | rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device); |
| 802 | if (rc < 0) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 803 | pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 804 | return -ENODEV; |
| 805 | } |
| 806 | |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 807 | ibm->acpi->device->driver_data = ibm; |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 808 | sprintf(acpi_device_class(ibm->acpi->device), "%s/%s", |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 809 | TPACPI_ACPI_EVENT_PREFIX, |
Henrique de Moraes Holschuh | 643f12d | 2007-03-29 01:58:43 -0300 | [diff] [blame] | 810 | ibm->name); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 811 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 812 | status = acpi_install_notify_handler(*ibm->acpi->handle, |
| 813 | ibm->acpi->type, dispatch_acpi_notify, ibm); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 814 | if (ACPI_FAILURE(status)) { |
| 815 | if (status == AE_ALREADY_EXISTS) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 816 | pr_notice("another device driver is already handling %s events\n", |
| 817 | ibm->name); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 818 | } else { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 819 | pr_err("acpi_install_notify_handler(%s) failed: %s\n", |
Henrique de Moraes Holschuh | 72f1992 | 2010-05-16 19:45:48 -0300 | [diff] [blame] | 820 | ibm->name, acpi_format_exception(status)); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 821 | } |
| 822 | return -ENODEV; |
| 823 | } |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 824 | ibm->flags.acpi_notify_installed = 1; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 825 | return 0; |
| 826 | } |
| 827 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 828 | static int __init tpacpi_device_add(struct acpi_device *device) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 829 | { |
| 830 | return 0; |
| 831 | } |
| 832 | |
Henrique de Moraes Holschuh | 6700121 | 2007-04-21 11:08:25 -0300 | [diff] [blame] | 833 | static int __init register_tpacpi_subdriver(struct ibm_struct *ibm) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 834 | { |
Henrique de Moraes Holschuh | 5ae930e | 2007-04-27 22:00:14 -0300 | [diff] [blame] | 835 | int rc; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 836 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 837 | dbg_printk(TPACPI_DBG_INIT, |
| 838 | "registering %s as an ACPI driver\n", ibm->name); |
| 839 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 840 | BUG_ON(!ibm->acpi); |
| 841 | |
| 842 | ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL); |
| 843 | if (!ibm->acpi->driver) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 844 | pr_err("failed to allocate memory for ibm->acpi->driver\n"); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 845 | return -ENOMEM; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 846 | } |
| 847 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 848 | sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name); |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 849 | ibm->acpi->driver->ids = ibm->acpi->hid; |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 850 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 851 | ibm->acpi->driver->ops.add = &tpacpi_device_add; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 852 | |
Henrique de Moraes Holschuh | 5ae930e | 2007-04-27 22:00:14 -0300 | [diff] [blame] | 853 | rc = acpi_bus_register_driver(ibm->acpi->driver); |
| 854 | if (rc < 0) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 855 | pr_err("acpi_bus_register_driver(%s) failed: %d\n", |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 856 | ibm->name, rc); |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 857 | kfree(ibm->acpi->driver); |
| 858 | ibm->acpi->driver = NULL; |
Henrique de Moraes Holschuh | 5ae930e | 2007-04-27 22:00:14 -0300 | [diff] [blame] | 859 | } else if (!rc) |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 860 | ibm->flags.acpi_driver_registered = 1; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 861 | |
Henrique de Moraes Holschuh | 5ae930e | 2007-04-27 22:00:14 -0300 | [diff] [blame] | 862 | return rc; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | |
| 866 | /**************************************************************************** |
| 867 | **************************************************************************** |
| 868 | * |
| 869 | * Procfs Helpers |
| 870 | * |
| 871 | **************************************************************************** |
| 872 | ****************************************************************************/ |
| 873 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 874 | static int dispatch_proc_show(struct seq_file *m, void *v) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 875 | { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 876 | struct ibm_struct *ibm = m->private; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 877 | |
| 878 | if (!ibm || !ibm->read) |
| 879 | return -EINVAL; |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 880 | return ibm->read(m); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 881 | } |
| 882 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 883 | static int dispatch_proc_open(struct inode *inode, struct file *file) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 884 | { |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 885 | return single_open(file, dispatch_proc_show, PDE_DATA(inode)); |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | static ssize_t dispatch_proc_write(struct file *file, |
| 889 | const char __user *userbuf, |
| 890 | size_t count, loff_t *pos) |
| 891 | { |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 892 | struct ibm_struct *ibm = PDE_DATA(file_inode(file)); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 893 | char *kernbuf; |
| 894 | int ret; |
| 895 | |
| 896 | if (!ibm || !ibm->write) |
| 897 | return -EINVAL; |
Andy Shevchenko | df11f6c | 2020-07-15 12:40:07 +0300 | [diff] [blame] | 898 | if (count > PAGE_SIZE - 1) |
| 899 | return -EINVAL; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 900 | |
Andy Shevchenko | df11f6c | 2020-07-15 12:40:07 +0300 | [diff] [blame] | 901 | kernbuf = kmalloc(count + 1, GFP_KERNEL); |
| 902 | if (!kernbuf) |
| 903 | return -ENOMEM; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 904 | |
Andy Shevchenko | df11f6c | 2020-07-15 12:40:07 +0300 | [diff] [blame] | 905 | if (copy_from_user(kernbuf, userbuf, count)) { |
| 906 | kfree(kernbuf); |
| 907 | return -EFAULT; |
| 908 | } |
| 909 | |
| 910 | kernbuf[count] = 0; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 911 | ret = ibm->write(kernbuf); |
| 912 | if (ret == 0) |
| 913 | ret = count; |
| 914 | |
| 915 | kfree(kernbuf); |
| 916 | |
| 917 | return ret; |
| 918 | } |
| 919 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 920 | static const struct proc_ops dispatch_proc_ops = { |
| 921 | .proc_open = dispatch_proc_open, |
| 922 | .proc_read = seq_read, |
| 923 | .proc_lseek = seq_lseek, |
| 924 | .proc_release = single_release, |
| 925 | .proc_write = dispatch_proc_write, |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 926 | }; |
| 927 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 928 | /**************************************************************************** |
| 929 | **************************************************************************** |
| 930 | * |
Henrique de Moraes Holschuh | 7f5d1cd | 2007-07-18 23:45:34 -0300 | [diff] [blame] | 931 | * Device model: input, hwmon and platform |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 932 | * |
| 933 | **************************************************************************** |
| 934 | ****************************************************************************/ |
| 935 | |
Henrique de Moraes Holschuh | 94954cc6 | 2007-07-18 23:45:27 -0300 | [diff] [blame] | 936 | static struct platform_device *tpacpi_pdev; |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 937 | static struct platform_device *tpacpi_sensors_pdev; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 938 | static struct device *tpacpi_hwmon; |
Henrique de Moraes Holschuh | 7f5d1cd | 2007-07-18 23:45:34 -0300 | [diff] [blame] | 939 | static struct input_dev *tpacpi_inputdev; |
Henrique de Moraes Holschuh | 8523ed6 | 2007-09-23 11:39:01 -0300 | [diff] [blame] | 940 | static struct mutex tpacpi_inputdev_send_mutex; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 941 | static LIST_HEAD(tpacpi_all_drivers); |
Henrique de Moraes Holschuh | e295e85 | 2007-07-18 23:45:37 -0300 | [diff] [blame] | 942 | |
Rafael J. Wysocki | 3567a4e2 | 2012-08-09 23:00:13 +0200 | [diff] [blame] | 943 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | 4959a78 | 2012-06-27 23:19:01 +0200 | [diff] [blame] | 944 | static int tpacpi_suspend_handler(struct device *dev) |
Henrique de Moraes Holschuh | 083f176 | 2008-01-08 13:02:50 -0200 | [diff] [blame] | 945 | { |
| 946 | struct ibm_struct *ibm, *itmp; |
| 947 | |
| 948 | list_for_each_entry_safe(ibm, itmp, |
| 949 | &tpacpi_all_drivers, |
| 950 | all_drivers) { |
| 951 | if (ibm->suspend) |
Rafael J. Wysocki | fd3c3a4 | 2012-06-27 23:18:44 +0200 | [diff] [blame] | 952 | (ibm->suspend)(); |
Henrique de Moraes Holschuh | 083f176 | 2008-01-08 13:02:50 -0200 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
Rafael J. Wysocki | 4959a78 | 2012-06-27 23:19:01 +0200 | [diff] [blame] | 958 | static int tpacpi_resume_handler(struct device *dev) |
Henrique de Moraes Holschuh | e295e85 | 2007-07-18 23:45:37 -0300 | [diff] [blame] | 959 | { |
| 960 | struct ibm_struct *ibm, *itmp; |
| 961 | |
| 962 | list_for_each_entry_safe(ibm, itmp, |
| 963 | &tpacpi_all_drivers, |
| 964 | all_drivers) { |
| 965 | if (ibm->resume) |
| 966 | (ibm->resume)(); |
| 967 | } |
| 968 | |
| 969 | return 0; |
| 970 | } |
Rafael J. Wysocki | 3567a4e2 | 2012-08-09 23:00:13 +0200 | [diff] [blame] | 971 | #endif |
Henrique de Moraes Holschuh | e295e85 | 2007-07-18 23:45:37 -0300 | [diff] [blame] | 972 | |
Rafael J. Wysocki | 4959a78 | 2012-06-27 23:19:01 +0200 | [diff] [blame] | 973 | static SIMPLE_DEV_PM_OPS(tpacpi_pm, |
| 974 | tpacpi_suspend_handler, tpacpi_resume_handler); |
| 975 | |
Henrique de Moraes Holschuh | 90d9d3c | 2009-01-11 03:01:02 -0200 | [diff] [blame] | 976 | static void tpacpi_shutdown_handler(struct platform_device *pdev) |
| 977 | { |
| 978 | struct ibm_struct *ibm, *itmp; |
| 979 | |
| 980 | list_for_each_entry_safe(ibm, itmp, |
| 981 | &tpacpi_all_drivers, |
| 982 | all_drivers) { |
| 983 | if (ibm->shutdown) |
| 984 | (ibm->shutdown)(); |
| 985 | } |
| 986 | } |
| 987 | |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 988 | static struct platform_driver tpacpi_pdriver = { |
| 989 | .driver = { |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 990 | .name = TPACPI_DRVR_NAME, |
Rafael J. Wysocki | 4959a78 | 2012-06-27 23:19:01 +0200 | [diff] [blame] | 991 | .pm = &tpacpi_pm, |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 992 | }, |
Henrique de Moraes Holschuh | 90d9d3c | 2009-01-11 03:01:02 -0200 | [diff] [blame] | 993 | .shutdown = tpacpi_shutdown_handler, |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 994 | }; |
| 995 | |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 996 | static struct platform_driver tpacpi_hwmon_pdriver = { |
| 997 | .driver = { |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 998 | .name = TPACPI_HWMON_DRVR_NAME, |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 999 | }, |
| 1000 | }; |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 1001 | |
Henrique de Moraes Holschuh | 176750d | 2007-04-24 11:48:13 -0300 | [diff] [blame] | 1002 | /************************************************************************* |
Henrique de Moraes Holschuh | 7252374 | 2007-04-24 11:48:14 -0300 | [diff] [blame] | 1003 | * sysfs support helpers |
| 1004 | */ |
| 1005 | |
Henrique de Moraes Holschuh | 7252374 | 2007-04-24 11:48:14 -0300 | [diff] [blame] | 1006 | static int parse_strtoul(const char *buf, |
| 1007 | unsigned long max, unsigned long *value) |
| 1008 | { |
| 1009 | char *endp; |
| 1010 | |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 1011 | *value = simple_strtoul(skip_spaces(buf), &endp, 0); |
| 1012 | endp = skip_spaces(endp); |
Henrique de Moraes Holschuh | 7252374 | 2007-04-24 11:48:14 -0300 | [diff] [blame] | 1013 | if (*endp || *value > max) |
| 1014 | return -EINVAL; |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
Henrique de Moraes Holschuh | d64c81c4 | 2008-10-18 14:23:55 -0300 | [diff] [blame] | 1019 | static void tpacpi_disable_brightness_delay(void) |
| 1020 | { |
| 1021 | if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0)) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 1022 | pr_notice("ACPI backlight control delay disabled\n"); |
Henrique de Moraes Holschuh | d64c81c4 | 2008-10-18 14:23:55 -0300 | [diff] [blame] | 1023 | } |
| 1024 | |
Henrique de Moraes Holschuh | 73a94d8 | 2009-04-04 04:25:47 +0000 | [diff] [blame] | 1025 | static void printk_deprecated_attribute(const char * const what, |
| 1026 | const char * const details) |
| 1027 | { |
| 1028 | tpacpi_log_usertask("deprecated sysfs attribute"); |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 1029 | pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n", |
Henrique de Moraes Holschuh | 73a94d8 | 2009-04-04 04:25:47 +0000 | [diff] [blame] | 1030 | what, details); |
| 1031 | } |
| 1032 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1033 | /************************************************************************* |
| 1034 | * rfkill and radio control support helpers |
| 1035 | */ |
| 1036 | |
| 1037 | /* |
| 1038 | * ThinkPad-ACPI firmware handling model: |
| 1039 | * |
| 1040 | * WLSW (master wireless switch) is event-driven, and is common to all |
| 1041 | * firmware-controlled radios. It cannot be controlled, just monitored, |
| 1042 | * as expected. It overrides all radio state in firmware |
| 1043 | * |
| 1044 | * The kernel, a masked-off hotkey, and WLSW can change the radio state |
| 1045 | * (TODO: verify how WLSW interacts with the returned radio state). |
| 1046 | * |
| 1047 | * The only time there are shadow radio state changes, is when |
| 1048 | * masked-off hotkeys are used. |
| 1049 | */ |
| 1050 | |
| 1051 | /* |
| 1052 | * Internal driver API for radio state: |
| 1053 | * |
| 1054 | * int: < 0 = error, otherwise enum tpacpi_rfkill_state |
| 1055 | * bool: true means radio blocked (off) |
| 1056 | */ |
| 1057 | enum tpacpi_rfkill_state { |
| 1058 | TPACPI_RFK_RADIO_OFF = 0, |
| 1059 | TPACPI_RFK_RADIO_ON |
| 1060 | }; |
| 1061 | |
| 1062 | /* rfkill switches */ |
| 1063 | enum tpacpi_rfk_id { |
| 1064 | TPACPI_RFK_BLUETOOTH_SW_ID = 0, |
| 1065 | TPACPI_RFK_WWAN_SW_ID, |
| 1066 | TPACPI_RFK_UWB_SW_ID, |
| 1067 | TPACPI_RFK_SW_MAX |
| 1068 | }; |
| 1069 | |
| 1070 | static const char *tpacpi_rfkill_names[] = { |
| 1071 | [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth", |
| 1072 | [TPACPI_RFK_WWAN_SW_ID] = "wwan", |
| 1073 | [TPACPI_RFK_UWB_SW_ID] = "uwb", |
| 1074 | [TPACPI_RFK_SW_MAX] = NULL |
| 1075 | }; |
| 1076 | |
| 1077 | /* ThinkPad-ACPI rfkill subdriver */ |
| 1078 | struct tpacpi_rfk { |
| 1079 | struct rfkill *rfkill; |
| 1080 | enum tpacpi_rfk_id id; |
| 1081 | const struct tpacpi_rfk_ops *ops; |
| 1082 | }; |
| 1083 | |
| 1084 | struct tpacpi_rfk_ops { |
| 1085 | /* firmware interface */ |
| 1086 | int (*get_status)(void); |
| 1087 | int (*set_status)(const enum tpacpi_rfkill_state); |
| 1088 | }; |
| 1089 | |
| 1090 | static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX]; |
| 1091 | |
| 1092 | /* Query FW and update rfkill sw state for a given rfkill switch */ |
| 1093 | static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk) |
| 1094 | { |
| 1095 | int status; |
| 1096 | |
| 1097 | if (!tp_rfk) |
| 1098 | return -ENODEV; |
| 1099 | |
| 1100 | status = (tp_rfk->ops->get_status)(); |
| 1101 | if (status < 0) |
| 1102 | return status; |
| 1103 | |
| 1104 | rfkill_set_sw_state(tp_rfk->rfkill, |
| 1105 | (status == TPACPI_RFK_RADIO_OFF)); |
| 1106 | |
| 1107 | return status; |
| 1108 | } |
| 1109 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1110 | /* |
| 1111 | * Sync the HW-blocking state of all rfkill switches, |
| 1112 | * do notice it causes the rfkill core to schedule uevents |
| 1113 | */ |
| 1114 | static void tpacpi_rfk_update_hwblock_state(bool blocked) |
| 1115 | { |
| 1116 | unsigned int i; |
| 1117 | struct tpacpi_rfk *tp_rfk; |
| 1118 | |
| 1119 | for (i = 0; i < TPACPI_RFK_SW_MAX; i++) { |
| 1120 | tp_rfk = tpacpi_rfkill_switches[i]; |
| 1121 | if (tp_rfk) { |
| 1122 | if (rfkill_set_hw_state(tp_rfk->rfkill, |
| 1123 | blocked)) { |
| 1124 | /* ignore -- we track sw block */ |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | /* Call to get the WLSW state from the firmware */ |
| 1131 | static int hotkey_get_wlsw(void); |
| 1132 | |
| 1133 | /* Call to query WLSW state and update all rfkill switches */ |
| 1134 | static bool tpacpi_rfk_check_hwblock_state(void) |
| 1135 | { |
| 1136 | int res = hotkey_get_wlsw(); |
| 1137 | int hw_blocked; |
| 1138 | |
| 1139 | /* When unknown or unsupported, we have to assume it is unblocked */ |
| 1140 | if (res < 0) |
| 1141 | return false; |
| 1142 | |
| 1143 | hw_blocked = (res == TPACPI_RFK_RADIO_OFF); |
| 1144 | tpacpi_rfk_update_hwblock_state(hw_blocked); |
| 1145 | |
| 1146 | return hw_blocked; |
| 1147 | } |
| 1148 | |
| 1149 | static int tpacpi_rfk_hook_set_block(void *data, bool blocked) |
| 1150 | { |
| 1151 | struct tpacpi_rfk *tp_rfk = data; |
| 1152 | int res; |
| 1153 | |
| 1154 | dbg_printk(TPACPI_DBG_RFKILL, |
| 1155 | "request to change radio state to %s\n", |
| 1156 | blocked ? "blocked" : "unblocked"); |
| 1157 | |
| 1158 | /* try to set radio state */ |
| 1159 | res = (tp_rfk->ops->set_status)(blocked ? |
| 1160 | TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON); |
| 1161 | |
| 1162 | /* and update the rfkill core with whatever the FW really did */ |
| 1163 | tpacpi_rfk_update_swstate(tp_rfk); |
| 1164 | |
| 1165 | return (res < 0) ? res : 0; |
| 1166 | } |
| 1167 | |
| 1168 | static const struct rfkill_ops tpacpi_rfk_rfkill_ops = { |
| 1169 | .set_block = tpacpi_rfk_hook_set_block, |
| 1170 | }; |
| 1171 | |
| 1172 | static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id, |
| 1173 | const struct tpacpi_rfk_ops *tp_rfkops, |
| 1174 | const enum rfkill_type rfktype, |
| 1175 | const char *name, |
| 1176 | const bool set_default) |
| 1177 | { |
| 1178 | struct tpacpi_rfk *atp_rfk; |
| 1179 | int res; |
Alan Jenkins | 06d5caf | 2009-06-16 15:39:51 +0100 | [diff] [blame] | 1180 | bool sw_state = false; |
Henrique de Moraes Holschuh | 5451a92 | 2009-12-15 21:51:07 -0200 | [diff] [blame] | 1181 | bool hw_state; |
Alan Jenkins | 06d5caf | 2009-06-16 15:39:51 +0100 | [diff] [blame] | 1182 | int sw_status; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1183 | |
| 1184 | BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]); |
| 1185 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1186 | atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL); |
| 1187 | if (atp_rfk) |
| 1188 | atp_rfk->rfkill = rfkill_alloc(name, |
| 1189 | &tpacpi_pdev->dev, |
| 1190 | rfktype, |
| 1191 | &tpacpi_rfk_rfkill_ops, |
| 1192 | atp_rfk); |
| 1193 | if (!atp_rfk || !atp_rfk->rfkill) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 1194 | pr_err("failed to allocate memory for rfkill class\n"); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1195 | kfree(atp_rfk); |
| 1196 | return -ENOMEM; |
| 1197 | } |
| 1198 | |
| 1199 | atp_rfk->id = id; |
| 1200 | atp_rfk->ops = tp_rfkops; |
| 1201 | |
Alan Jenkins | 06d5caf | 2009-06-16 15:39:51 +0100 | [diff] [blame] | 1202 | sw_status = (tp_rfkops->get_status)(); |
| 1203 | if (sw_status < 0) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 1204 | pr_err("failed to read initial state for %s, error %d\n", |
| 1205 | name, sw_status); |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 1206 | } else { |
Alan Jenkins | 06d5caf | 2009-06-16 15:39:51 +0100 | [diff] [blame] | 1207 | sw_state = (sw_status == TPACPI_RFK_RADIO_OFF); |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 1208 | if (set_default) { |
| 1209 | /* try to keep the initial state, since we ask the |
| 1210 | * firmware to preserve it across S5 in NVRAM */ |
Alan Jenkins | 06d5caf | 2009-06-16 15:39:51 +0100 | [diff] [blame] | 1211 | rfkill_init_sw_state(atp_rfk->rfkill, sw_state); |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 1212 | } |
| 1213 | } |
Henrique de Moraes Holschuh | 5451a92 | 2009-12-15 21:51:07 -0200 | [diff] [blame] | 1214 | hw_state = tpacpi_rfk_check_hwblock_state(); |
| 1215 | rfkill_set_hw_state(atp_rfk->rfkill, hw_state); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1216 | |
| 1217 | res = rfkill_register(atp_rfk->rfkill); |
| 1218 | if (res < 0) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 1219 | pr_err("failed to register %s rfkill switch: %d\n", name, res); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1220 | rfkill_destroy(atp_rfk->rfkill); |
| 1221 | kfree(atp_rfk); |
| 1222 | return res; |
| 1223 | } |
| 1224 | |
| 1225 | tpacpi_rfkill_switches[id] = atp_rfk; |
Henrique de Moraes Holschuh | 5451a92 | 2009-12-15 21:51:07 -0200 | [diff] [blame] | 1226 | |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 1227 | pr_info("rfkill switch %s: radio is %sblocked\n", |
Henrique de Moraes Holschuh | 5451a92 | 2009-12-15 21:51:07 -0200 | [diff] [blame] | 1228 | name, (sw_state || hw_state) ? "" : "un"); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id) |
| 1233 | { |
| 1234 | struct tpacpi_rfk *tp_rfk; |
| 1235 | |
| 1236 | BUG_ON(id >= TPACPI_RFK_SW_MAX); |
| 1237 | |
| 1238 | tp_rfk = tpacpi_rfkill_switches[id]; |
| 1239 | if (tp_rfk) { |
| 1240 | rfkill_unregister(tp_rfk->rfkill); |
Corentin Chary | 5f0dadb | 2009-09-14 12:43:52 +0200 | [diff] [blame] | 1241 | rfkill_destroy(tp_rfk->rfkill); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1242 | tpacpi_rfkill_switches[id] = NULL; |
| 1243 | kfree(tp_rfk); |
| 1244 | } |
| 1245 | } |
| 1246 | |
Henrique de Moraes Holschuh | 73a94d8 | 2009-04-04 04:25:47 +0000 | [diff] [blame] | 1247 | static void printk_deprecated_rfkill_attribute(const char * const what) |
| 1248 | { |
| 1249 | printk_deprecated_attribute(what, |
| 1250 | "Please switch to generic rfkill before year 2010"); |
| 1251 | } |
| 1252 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1253 | /* sysfs <radio> enable ------------------------------------------------ */ |
| 1254 | static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id, |
| 1255 | struct device_attribute *attr, |
| 1256 | char *buf) |
| 1257 | { |
| 1258 | int status; |
| 1259 | |
| 1260 | printk_deprecated_rfkill_attribute(attr->attr.name); |
| 1261 | |
| 1262 | /* This is in the ABI... */ |
| 1263 | if (tpacpi_rfk_check_hwblock_state()) { |
| 1264 | status = TPACPI_RFK_RADIO_OFF; |
| 1265 | } else { |
| 1266 | status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]); |
| 1267 | if (status < 0) |
| 1268 | return status; |
| 1269 | } |
| 1270 | |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 1271 | return sysfs_emit(buf, "%d\n", |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1272 | (status == TPACPI_RFK_RADIO_ON) ? 1 : 0); |
| 1273 | } |
| 1274 | |
| 1275 | static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id, |
| 1276 | struct device_attribute *attr, |
| 1277 | const char *buf, size_t count) |
| 1278 | { |
| 1279 | unsigned long t; |
| 1280 | int res; |
| 1281 | |
| 1282 | printk_deprecated_rfkill_attribute(attr->attr.name); |
| 1283 | |
| 1284 | if (parse_strtoul(buf, 1, &t)) |
| 1285 | return -EINVAL; |
| 1286 | |
| 1287 | tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t); |
| 1288 | |
| 1289 | /* This is in the ABI... */ |
| 1290 | if (tpacpi_rfk_check_hwblock_state() && !!t) |
| 1291 | return -EPERM; |
| 1292 | |
| 1293 | res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ? |
| 1294 | TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF); |
| 1295 | tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]); |
| 1296 | |
| 1297 | return (res < 0) ? res : count; |
| 1298 | } |
| 1299 | |
| 1300 | /* procfs -------------------------------------------------------------- */ |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 1301 | static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1302 | { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1303 | if (id >= TPACPI_RFK_SW_MAX) |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 1304 | seq_printf(m, "status:\t\tnot supported\n"); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1305 | else { |
| 1306 | int status; |
| 1307 | |
| 1308 | /* This is in the ABI... */ |
| 1309 | if (tpacpi_rfk_check_hwblock_state()) { |
| 1310 | status = TPACPI_RFK_RADIO_OFF; |
| 1311 | } else { |
| 1312 | status = tpacpi_rfk_update_swstate( |
| 1313 | tpacpi_rfkill_switches[id]); |
| 1314 | if (status < 0) |
| 1315 | return status; |
| 1316 | } |
| 1317 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 1318 | seq_printf(m, "status:\t\t%s\n", |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1319 | (status == TPACPI_RFK_RADIO_ON) ? |
| 1320 | "enabled" : "disabled"); |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 1321 | seq_printf(m, "commands:\tenable, disable\n"); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1322 | } |
| 1323 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 1324 | return 0; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf) |
| 1328 | { |
| 1329 | char *cmd; |
| 1330 | int status = -1; |
| 1331 | int res = 0; |
| 1332 | |
| 1333 | if (id >= TPACPI_RFK_SW_MAX) |
| 1334 | return -ENODEV; |
| 1335 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 1336 | while ((cmd = strsep(&buf, ","))) { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1337 | if (strlencmp(cmd, "enable") == 0) |
| 1338 | status = TPACPI_RFK_RADIO_ON; |
| 1339 | else if (strlencmp(cmd, "disable") == 0) |
| 1340 | status = TPACPI_RFK_RADIO_OFF; |
| 1341 | else |
| 1342 | return -EINVAL; |
| 1343 | } |
| 1344 | |
| 1345 | if (status != -1) { |
| 1346 | tpacpi_disclose_usertask("procfs", "attempt to %s %s\n", |
| 1347 | (status == TPACPI_RFK_RADIO_ON) ? |
| 1348 | "enable" : "disable", |
| 1349 | tpacpi_rfkill_names[id]); |
| 1350 | res = (tpacpi_rfkill_switches[id]->ops->set_status)(status); |
| 1351 | tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]); |
| 1352 | } |
| 1353 | |
| 1354 | return res; |
| 1355 | } |
| 1356 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1357 | /************************************************************************* |
| 1358 | * thinkpad-acpi driver attributes |
| 1359 | */ |
| 1360 | |
| 1361 | /* interface_version --------------------------------------------------- */ |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1362 | static ssize_t interface_version_show(struct device_driver *drv, char *buf) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1363 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 1364 | return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1365 | } |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1366 | static DRIVER_ATTR_RO(interface_version); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1367 | |
| 1368 | /* debug_level --------------------------------------------------------- */ |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1369 | static ssize_t debug_level_show(struct device_driver *drv, char *buf) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1370 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 1371 | return sysfs_emit(buf, "0x%04x\n", dbg_level); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1374 | static ssize_t debug_level_store(struct device_driver *drv, const char *buf, |
| 1375 | size_t count) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1376 | { |
| 1377 | unsigned long t; |
| 1378 | |
| 1379 | if (parse_strtoul(buf, 0xffff, &t)) |
| 1380 | return -EINVAL; |
| 1381 | |
| 1382 | dbg_level = t; |
| 1383 | |
| 1384 | return count; |
| 1385 | } |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1386 | static DRIVER_ATTR_RW(debug_level); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1387 | |
| 1388 | /* version ------------------------------------------------------------- */ |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1389 | static ssize_t version_show(struct device_driver *drv, char *buf) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1390 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 1391 | return sysfs_emit(buf, "%s v%s\n", |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 1392 | TPACPI_DESC, TPACPI_VERSION); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1393 | } |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1394 | static DRIVER_ATTR_RO(version); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1395 | |
| 1396 | /* --------------------------------------------------------------------- */ |
| 1397 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1398 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 1399 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1400 | /* wlsw_emulstate ------------------------------------------------------ */ |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1401 | static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf) |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1402 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 1403 | return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1404 | } |
| 1405 | |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1406 | static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf, |
| 1407 | size_t count) |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1408 | { |
| 1409 | unsigned long t; |
| 1410 | |
| 1411 | if (parse_strtoul(buf, 1, &t)) |
| 1412 | return -EINVAL; |
| 1413 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1414 | if (tpacpi_wlsw_emulstate != !!t) { |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1415 | tpacpi_wlsw_emulstate = !!t; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1416 | tpacpi_rfk_update_hwblock_state(!t); /* negative logic */ |
| 1417 | } |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1418 | |
| 1419 | return count; |
| 1420 | } |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1421 | static DRIVER_ATTR_RW(wlsw_emulstate); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1422 | |
| 1423 | /* bluetooth_emulstate ------------------------------------------------- */ |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1424 | static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf) |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1425 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 1426 | return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1427 | } |
| 1428 | |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1429 | static ssize_t bluetooth_emulstate_store(struct device_driver *drv, |
| 1430 | const char *buf, size_t count) |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1431 | { |
| 1432 | unsigned long t; |
| 1433 | |
| 1434 | if (parse_strtoul(buf, 1, &t)) |
| 1435 | return -EINVAL; |
| 1436 | |
| 1437 | tpacpi_bluetooth_emulstate = !!t; |
| 1438 | |
| 1439 | return count; |
| 1440 | } |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1441 | static DRIVER_ATTR_RW(bluetooth_emulstate); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1442 | |
| 1443 | /* wwan_emulstate ------------------------------------------------- */ |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1444 | static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf) |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1445 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 1446 | return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1447 | } |
| 1448 | |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1449 | static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf, |
| 1450 | size_t count) |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1451 | { |
| 1452 | unsigned long t; |
| 1453 | |
| 1454 | if (parse_strtoul(buf, 1, &t)) |
| 1455 | return -EINVAL; |
| 1456 | |
| 1457 | tpacpi_wwan_emulstate = !!t; |
| 1458 | |
| 1459 | return count; |
| 1460 | } |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1461 | static DRIVER_ATTR_RW(wwan_emulstate); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1462 | |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 1463 | /* uwb_emulstate ------------------------------------------------- */ |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1464 | static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf) |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 1465 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 1466 | return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate); |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 1467 | } |
| 1468 | |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1469 | static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf, |
| 1470 | size_t count) |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 1471 | { |
| 1472 | unsigned long t; |
| 1473 | |
| 1474 | if (parse_strtoul(buf, 1, &t)) |
| 1475 | return -EINVAL; |
| 1476 | |
| 1477 | tpacpi_uwb_emulstate = !!t; |
| 1478 | |
| 1479 | return count; |
| 1480 | } |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 1481 | static DRIVER_ATTR_RW(uwb_emulstate); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1482 | #endif |
| 1483 | |
| 1484 | /* --------------------------------------------------------------------- */ |
| 1485 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 1486 | static struct driver_attribute *tpacpi_driver_attributes[] = { |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1487 | &driver_attr_debug_level, &driver_attr_version, |
| 1488 | &driver_attr_interface_version, |
| 1489 | }; |
| 1490 | |
| 1491 | static int __init tpacpi_create_driver_attributes(struct device_driver *drv) |
| 1492 | { |
| 1493 | int i, res; |
| 1494 | |
| 1495 | i = 0; |
| 1496 | res = 0; |
| 1497 | while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) { |
| 1498 | res = driver_create_file(drv, tpacpi_driver_attributes[i]); |
| 1499 | i++; |
| 1500 | } |
| 1501 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1502 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 1503 | if (!res && dbg_wlswemul) |
| 1504 | res = driver_create_file(drv, &driver_attr_wlsw_emulstate); |
| 1505 | if (!res && dbg_bluetoothemul) |
| 1506 | res = driver_create_file(drv, &driver_attr_bluetooth_emulstate); |
| 1507 | if (!res && dbg_wwanemul) |
| 1508 | res = driver_create_file(drv, &driver_attr_wwan_emulstate); |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 1509 | if (!res && dbg_uwbemul) |
| 1510 | res = driver_create_file(drv, &driver_attr_uwb_emulstate); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1511 | #endif |
| 1512 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1513 | return res; |
| 1514 | } |
| 1515 | |
| 1516 | static void tpacpi_remove_driver_attributes(struct device_driver *drv) |
| 1517 | { |
| 1518 | int i; |
| 1519 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 1520 | for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1521 | driver_remove_file(drv, tpacpi_driver_attributes[i]); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1522 | |
| 1523 | #ifdef THINKPAD_ACPI_DEBUGFACILITIES |
| 1524 | driver_remove_file(drv, &driver_attr_wlsw_emulstate); |
| 1525 | driver_remove_file(drv, &driver_attr_bluetooth_emulstate); |
| 1526 | driver_remove_file(drv, &driver_attr_wwan_emulstate); |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 1527 | driver_remove_file(drv, &driver_attr_uwb_emulstate); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 1528 | #endif |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 1529 | } |
| 1530 | |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1531 | /************************************************************************* |
| 1532 | * Firmware Data |
| 1533 | */ |
| 1534 | |
| 1535 | /* |
| 1536 | * Table of recommended minimum BIOS versions |
| 1537 | * |
| 1538 | * Reasons for listing: |
Uwe Kleine-König | 9ddc5b6 | 2010-01-20 17:02:24 +0100 | [diff] [blame] | 1539 | * 1. Stable BIOS, listed because the unknown amount of |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1540 | * bugs and bad ACPI behaviour on older versions |
| 1541 | * |
| 1542 | * 2. BIOS or EC fw with known bugs that trigger on Linux |
| 1543 | * |
| 1544 | * 3. BIOS with known reduced functionality in older versions |
| 1545 | * |
| 1546 | * We recommend the latest BIOS and EC version. |
| 1547 | * We only support the latest BIOS and EC fw version as a rule. |
| 1548 | * |
| 1549 | * Sources: IBM ThinkPad Public Web Documents (update changelogs), |
| 1550 | * Information from users in ThinkWiki |
Henrique de Moraes Holschuh | e675aba | 2009-09-12 15:22:13 -0300 | [diff] [blame] | 1551 | * |
| 1552 | * WARNING: we use this table also to detect that the machine is |
| 1553 | * a ThinkPad in some cases, so don't remove entries lightly. |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1554 | */ |
| 1555 | |
| 1556 | #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \ |
| 1557 | { .vendor = (__v), \ |
| 1558 | .bios = TPID(__id1, __id2), \ |
| 1559 | .ec = TPACPI_MATCH_ANY, \ |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 1560 | .quirks = TPACPI_MATCH_ANY_VERSION << 16 \ |
| 1561 | | TPVER(__bv1, __bv2) } |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1562 | |
| 1563 | #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \ |
Henrique de Moraes Holschuh | 275014a | 2009-11-17 14:07:22 -0800 | [diff] [blame] | 1564 | __eid, __ev1, __ev2) \ |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1565 | { .vendor = (__v), \ |
| 1566 | .bios = TPID(__bid1, __bid2), \ |
Henrique de Moraes Holschuh | 275014a | 2009-11-17 14:07:22 -0800 | [diff] [blame] | 1567 | .ec = __eid, \ |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 1568 | .quirks = TPVER(__ev1, __ev2) << 16 \ |
| 1569 | | TPVER(__bv1, __bv2) } |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1570 | |
| 1571 | #define TPV_QI0(__id1, __id2, __bv1, __bv2) \ |
| 1572 | TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2) |
| 1573 | |
Henrique de Moraes Holschuh | 275014a | 2009-11-17 14:07:22 -0800 | [diff] [blame] | 1574 | /* Outdated IBM BIOSes often lack the EC id string */ |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1575 | #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \ |
| 1576 | TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \ |
Henrique de Moraes Holschuh | 275014a | 2009-11-17 14:07:22 -0800 | [diff] [blame] | 1577 | __bv1, __bv2, TPID(__id1, __id2), \ |
| 1578 | __ev1, __ev2), \ |
| 1579 | TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \ |
| 1580 | __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \ |
| 1581 | __ev1, __ev2) |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1582 | |
Henrique de Moraes Holschuh | 275014a | 2009-11-17 14:07:22 -0800 | [diff] [blame] | 1583 | /* Outdated IBM BIOSes often lack the EC id string */ |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1584 | #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \ |
| 1585 | __eid1, __eid2, __ev1, __ev2) \ |
| 1586 | TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \ |
Henrique de Moraes Holschuh | 275014a | 2009-11-17 14:07:22 -0800 | [diff] [blame] | 1587 | __bv1, __bv2, TPID(__eid1, __eid2), \ |
| 1588 | __ev1, __ev2), \ |
| 1589 | TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \ |
| 1590 | __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \ |
| 1591 | __ev1, __ev2) |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1592 | |
| 1593 | #define TPV_QL0(__id1, __id2, __bv1, __bv2) \ |
| 1594 | TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2) |
| 1595 | |
| 1596 | #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \ |
| 1597 | TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \ |
Henrique de Moraes Holschuh | 275014a | 2009-11-17 14:07:22 -0800 | [diff] [blame] | 1598 | __bv1, __bv2, TPID(__id1, __id2), \ |
| 1599 | __ev1, __ev2) |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1600 | |
| 1601 | #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \ |
| 1602 | __eid1, __eid2, __ev1, __ev2) \ |
| 1603 | TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \ |
Henrique de Moraes Holschuh | 275014a | 2009-11-17 14:07:22 -0800 | [diff] [blame] | 1604 | __bv1, __bv2, TPID(__eid1, __eid2), \ |
| 1605 | __ev1, __ev2) |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1606 | |
| 1607 | static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = { |
| 1608 | /* Numeric models ------------------ */ |
| 1609 | /* FW MODEL BIOS VERS */ |
| 1610 | TPV_QI0('I', 'M', '6', '5'), /* 570 */ |
| 1611 | TPV_QI0('I', 'U', '2', '6'), /* 570E */ |
| 1612 | TPV_QI0('I', 'B', '5', '4'), /* 600 */ |
| 1613 | TPV_QI0('I', 'H', '4', '7'), /* 600E */ |
| 1614 | TPV_QI0('I', 'N', '3', '6'), /* 600E */ |
| 1615 | TPV_QI0('I', 'T', '5', '5'), /* 600X */ |
| 1616 | TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */ |
| 1617 | TPV_QI0('I', 'I', '4', '2'), /* 770X */ |
| 1618 | TPV_QI0('I', 'O', '2', '3'), /* 770Z */ |
| 1619 | |
| 1620 | /* A-series ------------------------- */ |
| 1621 | /* FW MODEL BIOS VERS EC VERS */ |
| 1622 | TPV_QI0('I', 'W', '5', '9'), /* A20m */ |
| 1623 | TPV_QI0('I', 'V', '6', '9'), /* A20p */ |
| 1624 | TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */ |
| 1625 | TPV_QI0('K', 'U', '3', '6'), /* A21e */ |
| 1626 | TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */ |
| 1627 | TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */ |
| 1628 | TPV_QI0('1', 'B', '1', '7'), /* A22e */ |
| 1629 | TPV_QI0('1', '3', '2', '0'), /* A22m */ |
| 1630 | TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */ |
| 1631 | TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */ |
| 1632 | TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */ |
| 1633 | |
| 1634 | /* G-series ------------------------- */ |
| 1635 | /* FW MODEL BIOS VERS */ |
| 1636 | TPV_QI0('1', 'T', 'A', '6'), /* G40 */ |
| 1637 | TPV_QI0('1', 'X', '5', '7'), /* G41 */ |
| 1638 | |
| 1639 | /* R-series, T-series --------------- */ |
| 1640 | /* FW MODEL BIOS VERS EC VERS */ |
| 1641 | TPV_QI0('1', 'C', 'F', '0'), /* R30 */ |
| 1642 | TPV_QI0('1', 'F', 'F', '1'), /* R31 */ |
| 1643 | TPV_QI0('1', 'M', '9', '7'), /* R32 */ |
| 1644 | TPV_QI0('1', 'O', '6', '1'), /* R40 */ |
| 1645 | TPV_QI0('1', 'P', '6', '5'), /* R40 */ |
| 1646 | TPV_QI0('1', 'S', '7', '0'), /* R40e */ |
| 1647 | TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51, |
| 1648 | T40/p, T41/p, T42/p (1) */ |
| 1649 | TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */ |
| 1650 | TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */ |
| 1651 | TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */ |
| 1652 | TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */ |
| 1653 | |
| 1654 | TPV_QI0('I', 'Y', '6', '1'), /* T20 */ |
| 1655 | TPV_QI0('K', 'Z', '3', '4'), /* T21 */ |
| 1656 | TPV_QI0('1', '6', '3', '2'), /* T22 */ |
| 1657 | TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */ |
| 1658 | TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */ |
| 1659 | TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */ |
| 1660 | |
| 1661 | TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */ |
| 1662 | TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */ |
Henrique de Moraes Holschuh | 90765c6 | 2009-12-09 01:36:23 +0000 | [diff] [blame] | 1663 | TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */ |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1664 | |
| 1665 | /* BIOS FW BIOS VERS EC FW EC VERS */ |
| 1666 | TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */ |
| 1667 | TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */ |
| 1668 | |
| 1669 | /* X-series ------------------------- */ |
| 1670 | /* FW MODEL BIOS VERS EC VERS */ |
| 1671 | TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */ |
| 1672 | TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */ |
| 1673 | TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */ |
| 1674 | TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */ |
| 1675 | TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */ |
| 1676 | TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */ |
| 1677 | TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */ |
| 1678 | |
Henrique de Moraes Holschuh | 90765c6 | 2009-12-09 01:36:23 +0000 | [diff] [blame] | 1679 | TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */ |
| 1680 | TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */ |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1681 | |
| 1682 | /* (0) - older versions lack DMI EC fw string and functionality */ |
| 1683 | /* (1) - older versions known to lack functionality */ |
| 1684 | }; |
| 1685 | |
| 1686 | #undef TPV_QL1 |
| 1687 | #undef TPV_QL0 |
| 1688 | #undef TPV_QI2 |
| 1689 | #undef TPV_QI1 |
| 1690 | #undef TPV_QI0 |
| 1691 | #undef TPV_Q_X |
| 1692 | #undef TPV_Q |
| 1693 | |
| 1694 | static void __init tpacpi_check_outdated_fw(void) |
| 1695 | { |
| 1696 | unsigned long fwvers; |
| 1697 | u16 ec_version, bios_version; |
| 1698 | |
| 1699 | fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable, |
| 1700 | ARRAY_SIZE(tpacpi_bios_version_qtable)); |
| 1701 | |
| 1702 | if (!fwvers) |
| 1703 | return; |
| 1704 | |
| 1705 | bios_version = fwvers & 0xffffU; |
| 1706 | ec_version = (fwvers >> 16) & 0xffffU; |
| 1707 | |
| 1708 | /* note that unknown versions are set to 0x0000 and we use that */ |
| 1709 | if ((bios_version > thinkpad_id.bios_release) || |
| 1710 | (ec_version > thinkpad_id.ec_release && |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 1711 | ec_version != TPACPI_MATCH_ANY_VERSION)) { |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1712 | /* |
| 1713 | * The changelogs would let us track down the exact |
| 1714 | * reason, but it is just too much of a pain to track |
| 1715 | * it. We only list BIOSes that are either really |
| 1716 | * broken, or really stable to begin with, so it is |
| 1717 | * best if the user upgrades the firmware anyway. |
| 1718 | */ |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 1719 | pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n"); |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 1720 | pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n"); |
Henrique de Moraes Holschuh | 600a99f | 2009-09-12 15:22:12 -0300 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | |
Henrique de Moraes Holschuh | e675aba | 2009-09-12 15:22:13 -0300 | [diff] [blame] | 1724 | static bool __init tpacpi_is_fw_known(void) |
| 1725 | { |
| 1726 | return tpacpi_check_quirks(tpacpi_bios_version_qtable, |
| 1727 | ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0; |
| 1728 | } |
| 1729 | |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 1730 | /**************************************************************************** |
| 1731 | **************************************************************************** |
| 1732 | * |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 1733 | * Subdrivers |
| 1734 | * |
| 1735 | **************************************************************************** |
| 1736 | ****************************************************************************/ |
| 1737 | |
| 1738 | /************************************************************************* |
Henrique de Moraes Holschuh | 7a43f78 | 2010-05-16 19:45:31 -0300 | [diff] [blame] | 1739 | * thinkpad-acpi metadata subdriver |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 1740 | */ |
| 1741 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 1742 | static int thinkpad_acpi_driver_read(struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 1744 | seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC); |
| 1745 | seq_printf(m, "version:\t%s\n", TPACPI_VERSION); |
| 1746 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 1749 | static struct ibm_struct thinkpad_acpi_driver_data = { |
| 1750 | .name = "driver", |
| 1751 | .read = thinkpad_acpi_driver_read, |
| 1752 | }; |
| 1753 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 1754 | /************************************************************************* |
| 1755 | * Hotkey subdriver |
| 1756 | */ |
| 1757 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 1758 | /* |
| 1759 | * ThinkPad firmware event model |
| 1760 | * |
| 1761 | * The ThinkPad firmware has two main event interfaces: normal ACPI |
| 1762 | * notifications (which follow the ACPI standard), and a private event |
| 1763 | * interface. |
| 1764 | * |
| 1765 | * The private event interface also issues events for the hotkeys. As |
| 1766 | * the driver gained features, the event handling code ended up being |
| 1767 | * built around the hotkey subdriver. This will need to be refactored |
| 1768 | * to a more formal event API eventually. |
| 1769 | * |
| 1770 | * Some "hotkeys" are actually supposed to be used as event reports, |
| 1771 | * such as "brightness has changed", "volume has changed", depending on |
| 1772 | * the ThinkPad model and how the firmware is operating. |
| 1773 | * |
| 1774 | * Unlike other classes, hotkey-class events have mask/unmask control on |
| 1775 | * non-ancient firmware. However, how it behaves changes a lot with the |
| 1776 | * firmware model and version. |
| 1777 | */ |
| 1778 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 1779 | enum { /* hot key scan codes (derived from ACPI DSDT) */ |
| 1780 | TP_ACPI_HOTKEYSCAN_FNF1 = 0, |
| 1781 | TP_ACPI_HOTKEYSCAN_FNF2, |
| 1782 | TP_ACPI_HOTKEYSCAN_FNF3, |
| 1783 | TP_ACPI_HOTKEYSCAN_FNF4, |
| 1784 | TP_ACPI_HOTKEYSCAN_FNF5, |
| 1785 | TP_ACPI_HOTKEYSCAN_FNF6, |
| 1786 | TP_ACPI_HOTKEYSCAN_FNF7, |
| 1787 | TP_ACPI_HOTKEYSCAN_FNF8, |
| 1788 | TP_ACPI_HOTKEYSCAN_FNF9, |
| 1789 | TP_ACPI_HOTKEYSCAN_FNF10, |
| 1790 | TP_ACPI_HOTKEYSCAN_FNF11, |
| 1791 | TP_ACPI_HOTKEYSCAN_FNF12, |
| 1792 | TP_ACPI_HOTKEYSCAN_FNBACKSPACE, |
| 1793 | TP_ACPI_HOTKEYSCAN_FNINSERT, |
| 1794 | TP_ACPI_HOTKEYSCAN_FNDELETE, |
| 1795 | TP_ACPI_HOTKEYSCAN_FNHOME, |
| 1796 | TP_ACPI_HOTKEYSCAN_FNEND, |
| 1797 | TP_ACPI_HOTKEYSCAN_FNPAGEUP, |
| 1798 | TP_ACPI_HOTKEYSCAN_FNPAGEDOWN, |
| 1799 | TP_ACPI_HOTKEYSCAN_FNSPACE, |
| 1800 | TP_ACPI_HOTKEYSCAN_VOLUMEUP, |
| 1801 | TP_ACPI_HOTKEYSCAN_VOLUMEDOWN, |
| 1802 | TP_ACPI_HOTKEYSCAN_MUTE, |
| 1803 | TP_ACPI_HOTKEYSCAN_THINKPAD, |
Henrique de Moraes Holschuh | 34a656d2 | 2010-08-09 23:48:20 -0300 | [diff] [blame] | 1804 | TP_ACPI_HOTKEYSCAN_UNK1, |
| 1805 | TP_ACPI_HOTKEYSCAN_UNK2, |
| 1806 | TP_ACPI_HOTKEYSCAN_UNK3, |
| 1807 | TP_ACPI_HOTKEYSCAN_UNK4, |
| 1808 | TP_ACPI_HOTKEYSCAN_UNK5, |
| 1809 | TP_ACPI_HOTKEYSCAN_UNK6, |
| 1810 | TP_ACPI_HOTKEYSCAN_UNK7, |
| 1811 | TP_ACPI_HOTKEYSCAN_UNK8, |
| 1812 | |
Christian Kellner | 149c8c7 | 2017-02-28 17:10:56 +0100 | [diff] [blame] | 1813 | /* Adaptive keyboard keycodes */ |
| 1814 | TP_ACPI_HOTKEYSCAN_ADAPTIVE_START, |
| 1815 | TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START, |
Bastien Nocera | 6a68d85 | 2015-03-02 14:45:31 +0100 | [diff] [blame] | 1816 | TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO, |
| 1817 | TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL, |
| 1818 | TP_ACPI_HOTKEYSCAN_CLOUD, |
| 1819 | TP_ACPI_HOTKEYSCAN_UNK9, |
| 1820 | TP_ACPI_HOTKEYSCAN_VOICE, |
| 1821 | TP_ACPI_HOTKEYSCAN_UNK10, |
| 1822 | TP_ACPI_HOTKEYSCAN_GESTURES, |
| 1823 | TP_ACPI_HOTKEYSCAN_UNK11, |
| 1824 | TP_ACPI_HOTKEYSCAN_UNK12, |
| 1825 | TP_ACPI_HOTKEYSCAN_UNK13, |
| 1826 | TP_ACPI_HOTKEYSCAN_CONFIG, |
| 1827 | TP_ACPI_HOTKEYSCAN_NEW_TAB, |
| 1828 | TP_ACPI_HOTKEYSCAN_RELOAD, |
| 1829 | TP_ACPI_HOTKEYSCAN_BACK, |
| 1830 | TP_ACPI_HOTKEYSCAN_MIC_DOWN, |
| 1831 | TP_ACPI_HOTKEYSCAN_MIC_UP, |
| 1832 | TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION, |
| 1833 | TP_ACPI_HOTKEYSCAN_CAMERA_MODE, |
| 1834 | TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY, |
| 1835 | |
Christian Kellner | 696c6523 | 2017-02-28 17:10:57 +0100 | [diff] [blame] | 1836 | /* Lenovo extended keymap, starting at 0x1300 */ |
| 1837 | TP_ACPI_HOTKEYSCAN_EXTENDED_START, |
| 1838 | /* first new observed key (star, favorites) is 0x1311 */ |
| 1839 | TP_ACPI_HOTKEYSCAN_STAR = 69, |
| 1840 | TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2, |
Benjamin Berg | cb5c197 | 2018-06-20 15:49:27 +0200 | [diff] [blame] | 1841 | TP_ACPI_HOTKEYSCAN_CALCULATOR, |
Christian Kellner | 696c6523 | 2017-02-28 17:10:57 +0100 | [diff] [blame] | 1842 | TP_ACPI_HOTKEYSCAN_BLUETOOTH, |
| 1843 | TP_ACPI_HOTKEYSCAN_KEYBOARD, |
Hans de Goede | 7ed7748 | 2020-09-08 12:21:11 -0700 | [diff] [blame] | 1844 | TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */ |
| 1845 | TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER, |
| 1846 | TP_ACPI_HOTKEYSCAN_PICKUP_PHONE, |
| 1847 | TP_ACPI_HOTKEYSCAN_HANGUP_PHONE, |
Christian Kellner | 696c6523 | 2017-02-28 17:10:57 +0100 | [diff] [blame] | 1848 | |
Henrique de Moraes Holschuh | 34a656d2 | 2010-08-09 23:48:20 -0300 | [diff] [blame] | 1849 | /* Hotkey keymap size */ |
| 1850 | TPACPI_HOTKEY_MAP_LEN |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 1851 | }; |
| 1852 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 1853 | enum { /* Keys/events available through NVRAM polling */ |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 1854 | TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U, |
| 1855 | TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U, |
| 1856 | }; |
| 1857 | |
| 1858 | enum { /* Positions of some of the keys in hotkey masks */ |
| 1859 | TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7, |
| 1860 | TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8, |
| 1861 | TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12, |
| 1862 | TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME, |
| 1863 | TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND, |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 1864 | TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP, |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 1865 | TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE, |
| 1866 | TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP, |
| 1867 | TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN, |
| 1868 | TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE, |
| 1869 | TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD, |
| 1870 | }; |
| 1871 | |
| 1872 | enum { /* NVRAM to ACPI HKEY group map */ |
| 1873 | TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK | |
| 1874 | TP_ACPI_HKEY_ZOOM_MASK | |
| 1875 | TP_ACPI_HKEY_DISPSWTCH_MASK | |
| 1876 | TP_ACPI_HKEY_HIBERNATE_MASK, |
| 1877 | TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK | |
| 1878 | TP_ACPI_HKEY_BRGHTDWN_MASK, |
| 1879 | TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK | |
| 1880 | TP_ACPI_HKEY_VOLDWN_MASK | |
| 1881 | TP_ACPI_HKEY_MUTE_MASK, |
| 1882 | }; |
| 1883 | |
| 1884 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
| 1885 | struct tp_nvram_state { |
| 1886 | u16 thinkpad_toggle:1; |
| 1887 | u16 zoom_toggle:1; |
| 1888 | u16 display_toggle:1; |
| 1889 | u16 thinklight_toggle:1; |
| 1890 | u16 hibernate_toggle:1; |
| 1891 | u16 displayexp_toggle:1; |
| 1892 | u16 display_state:1; |
| 1893 | u16 brightness_toggle:1; |
| 1894 | u16 volume_toggle:1; |
| 1895 | u16 mute:1; |
| 1896 | |
| 1897 | u8 brightness_level; |
| 1898 | u8 volume_level; |
| 1899 | }; |
| 1900 | |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 1901 | /* kthread for the hotkey poller */ |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 1902 | static struct task_struct *tpacpi_hotkey_task; |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 1903 | |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 1904 | /* |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 1905 | * Acquire mutex to write poller control variables as an |
| 1906 | * atomic block. |
| 1907 | * |
| 1908 | * Increment hotkey_config_change when changing them if you |
| 1909 | * want the kthread to forget old state. |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 1910 | * |
| 1911 | * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END |
| 1912 | */ |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 1913 | static struct mutex hotkey_thread_data_mutex; |
| 1914 | static unsigned int hotkey_config_change; |
| 1915 | |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 1916 | /* |
| 1917 | * hotkey poller control variables |
| 1918 | * |
| 1919 | * Must be atomic or readers will also need to acquire mutex |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 1920 | * |
| 1921 | * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END |
| 1922 | * should be used only when the changes need to be taken as |
| 1923 | * a block, OR when one needs to force the kthread to forget |
| 1924 | * old state. |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 1925 | */ |
| 1926 | static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */ |
| 1927 | static unsigned int hotkey_poll_freq = 10; /* Hz */ |
| 1928 | |
| 1929 | #define HOTKEY_CONFIG_CRITICAL_START \ |
| 1930 | do { \ |
| 1931 | mutex_lock(&hotkey_thread_data_mutex); \ |
| 1932 | hotkey_config_change++; \ |
| 1933 | } while (0); |
| 1934 | #define HOTKEY_CONFIG_CRITICAL_END \ |
| 1935 | mutex_unlock(&hotkey_thread_data_mutex); |
| 1936 | |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 1937 | #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ |
| 1938 | |
| 1939 | #define hotkey_source_mask 0U |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 1940 | #define HOTKEY_CONFIG_CRITICAL_START |
| 1941 | #define HOTKEY_CONFIG_CRITICAL_END |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 1942 | |
| 1943 | #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ |
| 1944 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 1945 | static struct mutex hotkey_mutex; |
| 1946 | |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 1947 | static enum { /* Reasons for waking up */ |
| 1948 | TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */ |
| 1949 | TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */ |
| 1950 | TP_ACPI_WAKEUP_UNDOCK, /* Undock request */ |
| 1951 | } hotkey_wakeup_reason; |
| 1952 | |
| 1953 | static int hotkey_autosleep_ack; |
| 1954 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 1955 | static u32 hotkey_orig_mask; /* events the BIOS had enabled */ |
| 1956 | static u32 hotkey_all_mask; /* all events supported in fw */ |
Dennis Wassenberg | 0118c2d | 2016-06-08 10:54:25 -0400 | [diff] [blame] | 1957 | static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */ |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 1958 | static u32 hotkey_reserved_mask; /* events better left disabled */ |
| 1959 | static u32 hotkey_driver_mask; /* events needed by the driver */ |
| 1960 | static u32 hotkey_user_mask; /* events visible to userspace */ |
| 1961 | static u32 hotkey_acpi_mask; /* events enabled in firmware */ |
Henrique de Moraes Holschuh | 6a38abb | 2007-07-18 23:45:35 -0300 | [diff] [blame] | 1962 | |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 1963 | static u16 *hotkey_keycode_map; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 1964 | |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 1965 | static void tpacpi_driver_event(const unsigned int hkey_event); |
| 1966 | static void hotkey_driver_event(const unsigned int scancode); |
Henrique de Moraes Holschuh | 7f0cf71 | 2010-02-25 21:29:00 -0300 | [diff] [blame] | 1967 | static void hotkey_poll_setup(const bool may_warn); |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 1968 | |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 1969 | /* HKEY.MHKG() return bits */ |
| 1970 | #define TP_HOTKEY_TABLET_MASK (1 << 3) |
Benjamin Berg | dda3ec0 | 2017-09-15 15:20:49 +0200 | [diff] [blame] | 1971 | enum { |
| 1972 | TP_ACPI_MULTI_MODE_INVALID = 0, |
| 1973 | TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0, |
| 1974 | TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1, |
| 1975 | TP_ACPI_MULTI_MODE_TABLET = 1 << 2, |
| 1976 | TP_ACPI_MULTI_MODE_FLAT = 1 << 3, |
| 1977 | TP_ACPI_MULTI_MODE_STAND = 1 << 4, |
| 1978 | TP_ACPI_MULTI_MODE_TENT = 1 << 5, |
| 1979 | TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6, |
| 1980 | }; |
| 1981 | |
| 1982 | enum { |
| 1983 | /* The following modes are considered tablet mode for the purpose of |
| 1984 | * reporting the status to userspace. i.e. in all these modes it makes |
| 1985 | * sense to disable the laptop input devices such as touchpad and |
| 1986 | * keyboard. |
| 1987 | */ |
| 1988 | TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET | |
| 1989 | TP_ACPI_MULTI_MODE_STAND | |
| 1990 | TP_ACPI_MULTI_MODE_TENT | |
| 1991 | TP_ACPI_MULTI_MODE_STAND_TENT, |
| 1992 | }; |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 1993 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1994 | static int hotkey_get_wlsw(void) |
Henrique de Moraes Holschuh | 74941a6 | 2007-07-18 23:45:31 -0300 | [diff] [blame] | 1995 | { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1996 | int status; |
| 1997 | |
| 1998 | if (!tp_features.hotkey_wlsw) |
| 1999 | return -ENODEV; |
| 2000 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 2001 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 2002 | if (dbg_wlswemul) |
| 2003 | return (tpacpi_wlsw_emulstate) ? |
| 2004 | TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 2005 | #endif |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 2006 | |
| 2007 | if (!acpi_evalf(hkey_handle, &status, "WLSW", "d")) |
Henrique de Moraes Holschuh | 74941a6 | 2007-07-18 23:45:31 -0300 | [diff] [blame] | 2008 | return -EIO; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 2009 | |
| 2010 | return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; |
Henrique de Moraes Holschuh | 74941a6 | 2007-07-18 23:45:31 -0300 | [diff] [blame] | 2011 | } |
| 2012 | |
Benjamin Berg | dda3ec0 | 2017-09-15 15:20:49 +0200 | [diff] [blame] | 2013 | static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode) |
| 2014 | { |
| 2015 | int type = (s >> 16) & 0xffff; |
| 2016 | int value = s & 0xffff; |
| 2017 | int mode = TP_ACPI_MULTI_MODE_INVALID; |
| 2018 | int valid_modes = 0; |
| 2019 | |
| 2020 | if (has_tablet_mode) |
| 2021 | *has_tablet_mode = 0; |
| 2022 | |
| 2023 | switch (type) { |
| 2024 | case 1: |
| 2025 | valid_modes = TP_ACPI_MULTI_MODE_LAPTOP | |
| 2026 | TP_ACPI_MULTI_MODE_TABLET | |
| 2027 | TP_ACPI_MULTI_MODE_STAND_TENT; |
| 2028 | break; |
| 2029 | case 2: |
| 2030 | valid_modes = TP_ACPI_MULTI_MODE_LAPTOP | |
| 2031 | TP_ACPI_MULTI_MODE_FLAT | |
| 2032 | TP_ACPI_MULTI_MODE_TABLET | |
| 2033 | TP_ACPI_MULTI_MODE_STAND | |
| 2034 | TP_ACPI_MULTI_MODE_TENT; |
| 2035 | break; |
| 2036 | case 3: |
| 2037 | valid_modes = TP_ACPI_MULTI_MODE_LAPTOP | |
| 2038 | TP_ACPI_MULTI_MODE_FLAT; |
| 2039 | break; |
| 2040 | case 4: |
Benjamin Berg | dda3ec0 | 2017-09-15 15:20:49 +0200 | [diff] [blame] | 2041 | case 5: |
Benjamin Berg | 26befef | 2017-11-14 17:14:14 +0100 | [diff] [blame] | 2042 | /* In mode 4, FLAT is not specified as a valid mode. However, |
| 2043 | * it can be seen at least on the X1 Yoga 2nd Generation. |
| 2044 | */ |
Benjamin Berg | dda3ec0 | 2017-09-15 15:20:49 +0200 | [diff] [blame] | 2045 | valid_modes = TP_ACPI_MULTI_MODE_LAPTOP | |
| 2046 | TP_ACPI_MULTI_MODE_FLAT | |
| 2047 | TP_ACPI_MULTI_MODE_TABLET | |
| 2048 | TP_ACPI_MULTI_MODE_STAND | |
| 2049 | TP_ACPI_MULTI_MODE_TENT; |
| 2050 | break; |
| 2051 | default: |
| 2052 | pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n", |
| 2053 | type, value, TPACPI_MAIL); |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
| 2057 | if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE)) |
| 2058 | *has_tablet_mode = 1; |
| 2059 | |
| 2060 | switch (value) { |
| 2061 | case 1: |
| 2062 | mode = TP_ACPI_MULTI_MODE_LAPTOP; |
| 2063 | break; |
| 2064 | case 2: |
| 2065 | mode = TP_ACPI_MULTI_MODE_FLAT; |
| 2066 | break; |
| 2067 | case 3: |
| 2068 | mode = TP_ACPI_MULTI_MODE_TABLET; |
| 2069 | break; |
| 2070 | case 4: |
| 2071 | if (type == 1) |
| 2072 | mode = TP_ACPI_MULTI_MODE_STAND_TENT; |
| 2073 | else |
| 2074 | mode = TP_ACPI_MULTI_MODE_STAND; |
| 2075 | break; |
| 2076 | case 5: |
| 2077 | mode = TP_ACPI_MULTI_MODE_TENT; |
| 2078 | break; |
| 2079 | default: |
| 2080 | if (type == 5 && value == 0xffff) { |
| 2081 | pr_warn("Multi mode status is undetected, assuming laptop\n"); |
| 2082 | return 0; |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | if (!(mode & valid_modes)) { |
| 2087 | pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n", |
| 2088 | value, type, TPACPI_MAIL); |
| 2089 | return 0; |
| 2090 | } |
| 2091 | |
| 2092 | return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE); |
| 2093 | } |
| 2094 | |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 2095 | static int hotkey_get_tablet_mode(int *status) |
| 2096 | { |
| 2097 | int s; |
| 2098 | |
Lyude | b03f4d4 | 2016-11-11 15:15:03 -0500 | [diff] [blame] | 2099 | switch (tp_features.hotkey_tablet) { |
| 2100 | case TP_HOTKEY_TABLET_USES_MHKG: |
| 2101 | if (!acpi_evalf(hkey_handle, &s, "MHKG", "d")) |
| 2102 | return -EIO; |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 2103 | |
Lyude | b03f4d4 | 2016-11-11 15:15:03 -0500 | [diff] [blame] | 2104 | *status = ((s & TP_HOTKEY_TABLET_MASK) != 0); |
| 2105 | break; |
Benjamin Berg | dda3ec0 | 2017-09-15 15:20:49 +0200 | [diff] [blame] | 2106 | case TP_HOTKEY_TABLET_USES_GMMS: |
| 2107 | if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0)) |
Lyude | b03f4d4 | 2016-11-11 15:15:03 -0500 | [diff] [blame] | 2108 | return -EIO; |
| 2109 | |
Benjamin Berg | dda3ec0 | 2017-09-15 15:20:49 +0200 | [diff] [blame] | 2110 | *status = hotkey_gmms_get_tablet_mode(s, NULL); |
Lyude | b03f4d4 | 2016-11-11 15:15:03 -0500 | [diff] [blame] | 2111 | break; |
| 2112 | default: |
| 2113 | break; |
| 2114 | } |
| 2115 | |
Henrique de Moraes Holschuh | cee47f5 | 2008-03-04 14:29:21 -0800 | [diff] [blame] | 2116 | return 0; |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 2117 | } |
| 2118 | |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2119 | /* |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2120 | * Reads current event mask from firmware, and updates |
| 2121 | * hotkey_acpi_mask accordingly. Also resets any bits |
| 2122 | * from hotkey_user_mask that are unavailable to be |
| 2123 | * delivered (shadow requirement of the userspace ABI). |
| 2124 | * |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2125 | * Call with hotkey_mutex held |
| 2126 | */ |
| 2127 | static int hotkey_mask_get(void) |
| 2128 | { |
| 2129 | if (tp_features.hotkey_mask) { |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2130 | u32 m = 0; |
| 2131 | |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2132 | if (!acpi_evalf(hkey_handle, &m, "DHKN", "d")) |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2133 | return -EIO; |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2134 | |
| 2135 | hotkey_acpi_mask = m; |
| 2136 | } else { |
| 2137 | /* no mask support doesn't mean no event support... */ |
| 2138 | hotkey_acpi_mask = hotkey_all_mask; |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2139 | } |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2140 | |
| 2141 | /* sync userspace-visible mask */ |
| 2142 | hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask); |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2143 | |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
Jean Delvare | df6dd1b | 2015-04-27 09:45:06 +0200 | [diff] [blame] | 2147 | static void hotkey_mask_warn_incomplete_mask(void) |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2148 | { |
| 2149 | /* log only what the user can fix... */ |
| 2150 | const u32 wantedmask = hotkey_driver_mask & |
| 2151 | ~(hotkey_acpi_mask | hotkey_source_mask) & |
| 2152 | (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK); |
| 2153 | |
| 2154 | if (wantedmask) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 2155 | pr_notice("required events 0x%08x not enabled!\n", wantedmask); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2156 | } |
| 2157 | |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2158 | /* |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2159 | * Set the firmware mask when supported |
| 2160 | * |
| 2161 | * Also calls hotkey_mask_get to update hotkey_acpi_mask. |
| 2162 | * |
| 2163 | * NOTE: does not set bits in hotkey_user_mask, but may reset them. |
| 2164 | * |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2165 | * Call with hotkey_mutex held |
| 2166 | */ |
| 2167 | static int hotkey_mask_set(u32 mask) |
| 2168 | { |
| 2169 | int i; |
| 2170 | int rc = 0; |
| 2171 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2172 | const u32 fwmask = mask & ~hotkey_source_mask; |
Henrique de Moraes Holschuh | 9288902 | 2008-04-26 01:02:18 -0300 | [diff] [blame] | 2173 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2174 | if (tp_features.hotkey_mask) { |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2175 | for (i = 0; i < 32; i++) { |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2176 | if (!acpi_evalf(hkey_handle, |
| 2177 | NULL, "MHKM", "vdd", i + 1, |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2178 | !!(mask & (1 << i)))) { |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2179 | rc = -EIO; |
| 2180 | break; |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2181 | } |
| 2182 | } |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2183 | } |
| 2184 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2185 | /* |
| 2186 | * We *must* make an inconditional call to hotkey_mask_get to |
| 2187 | * refresh hotkey_acpi_mask and update hotkey_user_mask |
| 2188 | * |
| 2189 | * Take the opportunity to also log when we cannot _enable_ |
| 2190 | * a given event. |
| 2191 | */ |
| 2192 | if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 2193 | pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n", |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 2194 | fwmask, hotkey_acpi_mask); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2195 | } |
| 2196 | |
Henrique de Moraes Holschuh | 6b30eb7 | 2009-12-09 01:36:25 +0000 | [diff] [blame] | 2197 | if (tpacpi_lifecycle != TPACPI_LIFE_EXITING) |
| 2198 | hotkey_mask_warn_incomplete_mask(); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2199 | |
| 2200 | return rc; |
| 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * Sets hotkey_user_mask and tries to set the firmware mask |
| 2205 | * |
| 2206 | * Call with hotkey_mutex held |
| 2207 | */ |
| 2208 | static int hotkey_user_mask_set(const u32 mask) |
| 2209 | { |
| 2210 | int rc; |
| 2211 | |
| 2212 | /* Give people a chance to notice they are doing something that |
| 2213 | * is bound to go boom on their users sooner or later */ |
| 2214 | if (!tp_warned.hotkey_mask_ff && |
| 2215 | (mask == 0xffff || mask == 0xffffff || |
| 2216 | mask == 0xffffffff)) { |
| 2217 | tp_warned.hotkey_mask_ff = 1; |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 2218 | pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n", |
| 2219 | mask); |
| 2220 | pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n"); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | /* Try to enable what the user asked for, plus whatever we need. |
| 2224 | * this syncs everything but won't enable bits in hotkey_user_mask */ |
| 2225 | rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask); |
| 2226 | |
| 2227 | /* Enable the available bits in hotkey_user_mask */ |
| 2228 | hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask); |
| 2229 | |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2230 | return rc; |
| 2231 | } |
| 2232 | |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 2233 | /* |
| 2234 | * Sets the driver hotkey mask. |
| 2235 | * |
| 2236 | * Can be called even if the hotkey subdriver is inactive |
| 2237 | */ |
| 2238 | static int tpacpi_hotkey_driver_mask_set(const u32 mask) |
| 2239 | { |
| 2240 | int rc; |
| 2241 | |
| 2242 | /* Do the right thing if hotkey_init has not been called yet */ |
| 2243 | if (!tp_features.hotkey) { |
| 2244 | hotkey_driver_mask = mask; |
| 2245 | return 0; |
| 2246 | } |
| 2247 | |
| 2248 | mutex_lock(&hotkey_mutex); |
| 2249 | |
| 2250 | HOTKEY_CONFIG_CRITICAL_START |
| 2251 | hotkey_driver_mask = mask; |
Henrique de Moraes Holschuh | b684a36 | 2009-09-26 21:42:49 -0300 | [diff] [blame] | 2252 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 2253 | hotkey_source_mask |= (mask & ~hotkey_all_mask); |
Henrique de Moraes Holschuh | b684a36 | 2009-09-26 21:42:49 -0300 | [diff] [blame] | 2254 | #endif |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 2255 | HOTKEY_CONFIG_CRITICAL_END |
| 2256 | |
| 2257 | rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) & |
| 2258 | ~hotkey_source_mask); |
Henrique de Moraes Holschuh | 7f0cf71 | 2010-02-25 21:29:00 -0300 | [diff] [blame] | 2259 | hotkey_poll_setup(true); |
| 2260 | |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 2261 | mutex_unlock(&hotkey_mutex); |
| 2262 | |
| 2263 | return rc; |
| 2264 | } |
| 2265 | |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2266 | static int hotkey_status_get(int *status) |
| 2267 | { |
| 2268 | if (!acpi_evalf(hkey_handle, status, "DHKC", "d")) |
| 2269 | return -EIO; |
| 2270 | |
| 2271 | return 0; |
| 2272 | } |
| 2273 | |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 2274 | static int hotkey_status_set(bool enable) |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2275 | { |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 2276 | if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0)) |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2277 | return -EIO; |
| 2278 | |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 2282 | static void tpacpi_input_send_tabletsw(void) |
Henrique de Moraes Holschuh | b3ec6f9 | 2008-02-16 02:17:55 -0200 | [diff] [blame] | 2283 | { |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 2284 | int state; |
Henrique de Moraes Holschuh | b3ec6f9 | 2008-02-16 02:17:55 -0200 | [diff] [blame] | 2285 | |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 2286 | if (tp_features.hotkey_tablet && |
| 2287 | !hotkey_get_tablet_mode(&state)) { |
| 2288 | mutex_lock(&tpacpi_inputdev_send_mutex); |
Henrique de Moraes Holschuh | b3ec6f9 | 2008-02-16 02:17:55 -0200 | [diff] [blame] | 2289 | |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 2290 | input_report_switch(tpacpi_inputdev, |
| 2291 | SW_TABLET_MODE, !!state); |
| 2292 | input_sync(tpacpi_inputdev); |
| 2293 | |
| 2294 | mutex_unlock(&tpacpi_inputdev_send_mutex); |
| 2295 | } |
Henrique de Moraes Holschuh | b3ec6f9 | 2008-02-16 02:17:55 -0200 | [diff] [blame] | 2296 | } |
| 2297 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2298 | /* Do NOT call without validating scancode first */ |
| 2299 | static void tpacpi_input_send_key(const unsigned int scancode) |
Henrique de Moraes Holschuh | b7c8c20 | 2008-01-08 13:02:40 -0200 | [diff] [blame] | 2300 | { |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2301 | const unsigned int keycode = hotkey_keycode_map[scancode]; |
Henrique de Moraes Holschuh | b7c8c20 | 2008-01-08 13:02:40 -0200 | [diff] [blame] | 2302 | |
| 2303 | if (keycode != KEY_RESERVED) { |
| 2304 | mutex_lock(&tpacpi_inputdev_send_mutex); |
| 2305 | |
Seth Forshee | 5ffba7e | 2011-01-14 15:54:39 -0600 | [diff] [blame] | 2306 | input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode); |
Henrique de Moraes Holschuh | b7c8c20 | 2008-01-08 13:02:40 -0200 | [diff] [blame] | 2307 | input_report_key(tpacpi_inputdev, keycode, 1); |
Henrique de Moraes Holschuh | b7c8c20 | 2008-01-08 13:02:40 -0200 | [diff] [blame] | 2308 | input_sync(tpacpi_inputdev); |
| 2309 | |
Seth Forshee | 5ffba7e | 2011-01-14 15:54:39 -0600 | [diff] [blame] | 2310 | input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode); |
Henrique de Moraes Holschuh | b7c8c20 | 2008-01-08 13:02:40 -0200 | [diff] [blame] | 2311 | input_report_key(tpacpi_inputdev, keycode, 0); |
Henrique de Moraes Holschuh | b7c8c20 | 2008-01-08 13:02:40 -0200 | [diff] [blame] | 2312 | input_sync(tpacpi_inputdev); |
| 2313 | |
| 2314 | mutex_unlock(&tpacpi_inputdev_send_mutex); |
| 2315 | } |
| 2316 | } |
| 2317 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2318 | /* Do NOT call without validating scancode first */ |
| 2319 | static void tpacpi_input_send_key_masked(const unsigned int scancode) |
| 2320 | { |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 2321 | hotkey_driver_event(scancode); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2322 | if (hotkey_user_mask & (1 << scancode)) |
| 2323 | tpacpi_input_send_key(scancode); |
| 2324 | } |
| 2325 | |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2326 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
| 2327 | static struct tp_acpi_drv_struct ibm_hotkey_acpidriver; |
| 2328 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2329 | /* Do NOT call without validating scancode first */ |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2330 | static void tpacpi_hotkey_send_key(unsigned int scancode) |
| 2331 | { |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2332 | tpacpi_input_send_key_masked(scancode); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2333 | } |
| 2334 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2335 | static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m) |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2336 | { |
| 2337 | u8 d; |
| 2338 | |
| 2339 | if (m & TP_NVRAM_HKEY_GROUP_HK2) { |
| 2340 | d = nvram_read_byte(TP_NVRAM_ADDR_HK2); |
| 2341 | n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD); |
| 2342 | n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM); |
| 2343 | n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY); |
| 2344 | n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE); |
| 2345 | } |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 2346 | if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) { |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2347 | d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT); |
| 2348 | n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT); |
| 2349 | } |
| 2350 | if (m & TP_ACPI_HKEY_DISPXPAND_MASK) { |
| 2351 | d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO); |
| 2352 | n->displayexp_toggle = |
| 2353 | !!(d & TP_NVRAM_MASK_HKT_DISPEXPND); |
| 2354 | } |
| 2355 | if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) { |
| 2356 | d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS); |
| 2357 | n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) |
| 2358 | >> TP_NVRAM_POS_LEVEL_BRIGHTNESS; |
| 2359 | n->brightness_toggle = |
| 2360 | !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS); |
| 2361 | } |
| 2362 | if (m & TP_NVRAM_HKEY_GROUP_VOLUME) { |
| 2363 | d = nvram_read_byte(TP_NVRAM_ADDR_MIXER); |
| 2364 | n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME) |
| 2365 | >> TP_NVRAM_POS_LEVEL_VOLUME; |
| 2366 | n->mute = !!(d & TP_NVRAM_MASK_MUTE); |
| 2367 | n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME); |
| 2368 | } |
| 2369 | } |
| 2370 | |
Behan Webster | a4d44ba | 2014-02-12 21:58:46 +0100 | [diff] [blame] | 2371 | #define TPACPI_COMPARE_KEY(__scancode, __member) \ |
| 2372 | do { \ |
| 2373 | if ((event_mask & (1 << __scancode)) && \ |
| 2374 | oldn->__member != newn->__member) \ |
| 2375 | tpacpi_hotkey_send_key(__scancode); \ |
| 2376 | } while (0) |
| 2377 | |
| 2378 | #define TPACPI_MAY_SEND_KEY(__scancode) \ |
| 2379 | do { \ |
| 2380 | if (event_mask & (1 << __scancode)) \ |
| 2381 | tpacpi_hotkey_send_key(__scancode); \ |
| 2382 | } while (0) |
| 2383 | |
| 2384 | static void issue_volchange(const unsigned int oldvol, |
| 2385 | const unsigned int newvol, |
| 2386 | const u32 event_mask) |
| 2387 | { |
| 2388 | unsigned int i = oldvol; |
| 2389 | |
| 2390 | while (i > newvol) { |
| 2391 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN); |
| 2392 | i--; |
| 2393 | } |
| 2394 | while (i < newvol) { |
| 2395 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP); |
| 2396 | i++; |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | static void issue_brightnesschange(const unsigned int oldbrt, |
| 2401 | const unsigned int newbrt, |
| 2402 | const u32 event_mask) |
| 2403 | { |
| 2404 | unsigned int i = oldbrt; |
| 2405 | |
| 2406 | while (i > newbrt) { |
| 2407 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND); |
| 2408 | i--; |
| 2409 | } |
| 2410 | while (i < newbrt) { |
| 2411 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME); |
| 2412 | i++; |
| 2413 | } |
| 2414 | } |
| 2415 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2416 | static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn, |
| 2417 | struct tp_nvram_state *newn, |
| 2418 | const u32 event_mask) |
| 2419 | { |
| 2420 | |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2421 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle); |
| 2422 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle); |
| 2423 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle); |
| 2424 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle); |
| 2425 | |
| 2426 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle); |
| 2427 | |
| 2428 | TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle); |
| 2429 | |
Henrique de Moraes Holschuh | 5d756db | 2010-05-16 19:45:28 -0300 | [diff] [blame] | 2430 | /* |
| 2431 | * Handle volume |
| 2432 | * |
| 2433 | * This code is supposed to duplicate the IBM firmware behaviour: |
| 2434 | * - Pressing MUTE issues mute hotkey message, even when already mute |
| 2435 | * - Pressing Volume up/down issues volume up/down hotkey messages, |
Lucas De Marchi | c844033 | 2011-03-17 17:18:22 -0300 | [diff] [blame] | 2436 | * even when already at maximum or minimum volume |
Henrique de Moraes Holschuh | 5d756db | 2010-05-16 19:45:28 -0300 | [diff] [blame] | 2437 | * - The act of unmuting issues volume up/down notification, |
| 2438 | * depending which key was used to unmute |
| 2439 | * |
| 2440 | * We are constrained to what the NVRAM can tell us, which is not much |
| 2441 | * and certainly not enough if more than one volume hotkey was pressed |
| 2442 | * since the last poll cycle. |
| 2443 | * |
| 2444 | * Just to make our life interesting, some newer Lenovo ThinkPads have |
| 2445 | * bugs in the BIOS and may fail to update volume_toggle properly. |
| 2446 | */ |
| 2447 | if (newn->mute) { |
| 2448 | /* muted */ |
| 2449 | if (!oldn->mute || |
| 2450 | oldn->volume_toggle != newn->volume_toggle || |
| 2451 | oldn->volume_level != newn->volume_level) { |
| 2452 | /* recently muted, or repeated mute keypress, or |
| 2453 | * multiple presses ending in mute */ |
Behan Webster | a4d44ba | 2014-02-12 21:58:46 +0100 | [diff] [blame] | 2454 | issue_volchange(oldn->volume_level, newn->volume_level, |
| 2455 | event_mask); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2456 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE); |
| 2457 | } |
Henrique de Moraes Holschuh | 5d756db | 2010-05-16 19:45:28 -0300 | [diff] [blame] | 2458 | } else { |
| 2459 | /* unmute */ |
| 2460 | if (oldn->mute) { |
| 2461 | /* recently unmuted, issue 'unmute' keypress */ |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2462 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP); |
Henrique de Moraes Holschuh | 5d756db | 2010-05-16 19:45:28 -0300 | [diff] [blame] | 2463 | } |
| 2464 | if (oldn->volume_level != newn->volume_level) { |
Behan Webster | a4d44ba | 2014-02-12 21:58:46 +0100 | [diff] [blame] | 2465 | issue_volchange(oldn->volume_level, newn->volume_level, |
| 2466 | event_mask); |
Henrique de Moraes Holschuh | 5d756db | 2010-05-16 19:45:28 -0300 | [diff] [blame] | 2467 | } else if (oldn->volume_toggle != newn->volume_toggle) { |
| 2468 | /* repeated vol up/down keypress at end of scale ? */ |
| 2469 | if (newn->volume_level == 0) |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2470 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN); |
Henrique de Moraes Holschuh | 5d756db | 2010-05-16 19:45:28 -0300 | [diff] [blame] | 2471 | else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX) |
| 2472 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | /* handle brightness */ |
Henrique de Moraes Holschuh | 2899902 | 2010-05-16 19:45:36 -0300 | [diff] [blame] | 2477 | if (oldn->brightness_level != newn->brightness_level) { |
| 2478 | issue_brightnesschange(oldn->brightness_level, |
Behan Webster | a4d44ba | 2014-02-12 21:58:46 +0100 | [diff] [blame] | 2479 | newn->brightness_level, event_mask); |
Henrique de Moraes Holschuh | 2899902 | 2010-05-16 19:45:36 -0300 | [diff] [blame] | 2480 | } else if (oldn->brightness_toggle != newn->brightness_toggle) { |
| 2481 | /* repeated key presses that didn't change state */ |
| 2482 | if (newn->brightness_level == 0) |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2483 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND); |
Henrique de Moraes Holschuh | 2899902 | 2010-05-16 19:45:36 -0300 | [diff] [blame] | 2484 | else if (newn->brightness_level >= bright_maxlvl |
| 2485 | && !tp_features.bright_unkfw) |
| 2486 | TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2487 | } |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2488 | |
| 2489 | #undef TPACPI_COMPARE_KEY |
| 2490 | #undef TPACPI_MAY_SEND_KEY |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2491 | } |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2492 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2493 | /* |
| 2494 | * Polling driver |
| 2495 | * |
| 2496 | * We track all events in hotkey_source_mask all the time, since |
| 2497 | * most of them are edge-based. We only issue those requested by |
| 2498 | * hotkey_user_mask or hotkey_driver_mask, though. |
| 2499 | */ |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2500 | static int hotkey_kthread(void *data) |
| 2501 | { |
Tom Rix | 5f38b06 | 2020-09-13 12:02:03 -0700 | [diff] [blame] | 2502 | struct tp_nvram_state s[2] = { 0 }; |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2503 | u32 poll_mask, event_mask; |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2504 | unsigned int si, so; |
| 2505 | unsigned long t; |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 2506 | unsigned int change_detector; |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2507 | unsigned int poll_freq; |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 2508 | bool was_frozen; |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2509 | |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2510 | if (tpacpi_lifecycle == TPACPI_LIFE_EXITING) |
| 2511 | goto exit; |
| 2512 | |
| 2513 | set_freezable(); |
| 2514 | |
| 2515 | so = 0; |
| 2516 | si = 1; |
| 2517 | t = 0; |
| 2518 | |
| 2519 | /* Initial state for compares */ |
| 2520 | mutex_lock(&hotkey_thread_data_mutex); |
| 2521 | change_detector = hotkey_config_change; |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2522 | poll_mask = hotkey_source_mask; |
| 2523 | event_mask = hotkey_source_mask & |
| 2524 | (hotkey_driver_mask | hotkey_user_mask); |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2525 | poll_freq = hotkey_poll_freq; |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2526 | mutex_unlock(&hotkey_thread_data_mutex); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2527 | hotkey_read_nvram(&s[so], poll_mask); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2528 | |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2529 | while (!kthread_should_stop()) { |
| 2530 | if (t == 0) { |
| 2531 | if (likely(poll_freq)) |
| 2532 | t = 1000/poll_freq; |
| 2533 | else |
| 2534 | t = 100; /* should never happen... */ |
| 2535 | } |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2536 | t = msleep_interruptible(t); |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 2537 | if (unlikely(kthread_freezable_should_stop(&was_frozen))) |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2538 | break; |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 2539 | |
| 2540 | if (t > 0 && !was_frozen) |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2541 | continue; |
| 2542 | |
| 2543 | mutex_lock(&hotkey_thread_data_mutex); |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 2544 | if (was_frozen || hotkey_config_change != change_detector) { |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2545 | /* forget old state on thaw or config change */ |
| 2546 | si = so; |
| 2547 | t = 0; |
| 2548 | change_detector = hotkey_config_change; |
| 2549 | } |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2550 | poll_mask = hotkey_source_mask; |
| 2551 | event_mask = hotkey_source_mask & |
| 2552 | (hotkey_driver_mask | hotkey_user_mask); |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2553 | poll_freq = hotkey_poll_freq; |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2554 | mutex_unlock(&hotkey_thread_data_mutex); |
| 2555 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2556 | if (likely(poll_mask)) { |
| 2557 | hotkey_read_nvram(&s[si], poll_mask); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2558 | if (likely(si != so)) { |
| 2559 | hotkey_compare_and_issue_event(&s[so], &s[si], |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2560 | event_mask); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | so = si; |
| 2565 | si ^= 1; |
| 2566 | } |
| 2567 | |
| 2568 | exit: |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2569 | return 0; |
| 2570 | } |
| 2571 | |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2572 | /* call with hotkey_mutex held */ |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2573 | static void hotkey_poll_stop_sync(void) |
| 2574 | { |
| 2575 | if (tpacpi_hotkey_task) { |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2576 | kthread_stop(tpacpi_hotkey_task); |
| 2577 | tpacpi_hotkey_task = NULL; |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | /* call with hotkey_mutex held */ |
Henrique de Moraes Holschuh | 7f0cf71 | 2010-02-25 21:29:00 -0300 | [diff] [blame] | 2582 | static void hotkey_poll_setup(const bool may_warn) |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2583 | { |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2584 | const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask; |
| 2585 | const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask; |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2586 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2587 | if (hotkey_poll_freq > 0 && |
| 2588 | (poll_driver_mask || |
| 2589 | (poll_user_mask && tpacpi_inputdev->users > 0))) { |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2590 | if (!tpacpi_hotkey_task) { |
| 2591 | tpacpi_hotkey_task = kthread_run(hotkey_kthread, |
Henrique de Moraes Holschuh | 95e57ab | 2008-04-26 01:02:22 -0300 | [diff] [blame] | 2592 | NULL, TPACPI_NVRAM_KTHREAD_NAME); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2593 | if (IS_ERR(tpacpi_hotkey_task)) { |
| 2594 | tpacpi_hotkey_task = NULL; |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 2595 | pr_err("could not create kernel thread for hotkey polling\n"); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | } else { |
| 2599 | hotkey_poll_stop_sync(); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2600 | if (may_warn && (poll_driver_mask || poll_user_mask) && |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2601 | hotkey_poll_freq == 0) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 2602 | pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n", |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 2603 | poll_user_mask, poll_driver_mask); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | } |
| 2607 | |
Henrique de Moraes Holschuh | 7f0cf71 | 2010-02-25 21:29:00 -0300 | [diff] [blame] | 2608 | static void hotkey_poll_setup_safe(const bool may_warn) |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2609 | { |
| 2610 | mutex_lock(&hotkey_mutex); |
| 2611 | hotkey_poll_setup(may_warn); |
| 2612 | mutex_unlock(&hotkey_mutex); |
| 2613 | } |
| 2614 | |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2615 | /* call with hotkey_mutex held */ |
| 2616 | static void hotkey_poll_set_freq(unsigned int freq) |
| 2617 | { |
| 2618 | if (!freq) |
| 2619 | hotkey_poll_stop_sync(); |
| 2620 | |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2621 | hotkey_poll_freq = freq; |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2622 | } |
| 2623 | |
Henrique de Moraes Holschuh | 1bc6b9c | 2008-02-16 02:17:52 -0200 | [diff] [blame] | 2624 | #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ |
| 2625 | |
Henrique de Moraes Holschuh | 7f0cf71 | 2010-02-25 21:29:00 -0300 | [diff] [blame] | 2626 | static void hotkey_poll_setup(const bool __unused) |
| 2627 | { |
| 2628 | } |
| 2629 | |
| 2630 | static void hotkey_poll_setup_safe(const bool __unused) |
Henrique de Moraes Holschuh | 1bc6b9c | 2008-02-16 02:17:52 -0200 | [diff] [blame] | 2631 | { |
| 2632 | } |
| 2633 | |
| 2634 | #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ |
| 2635 | |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2636 | static int hotkey_inputdev_open(struct input_dev *dev) |
| 2637 | { |
| 2638 | switch (tpacpi_lifecycle) { |
| 2639 | case TPACPI_LIFE_INIT: |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2640 | case TPACPI_LIFE_RUNNING: |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2641 | hotkey_poll_setup_safe(false); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2642 | return 0; |
Henrique de Moraes Holschuh | b589ea4 | 2010-02-25 21:28:58 -0300 | [diff] [blame] | 2643 | case TPACPI_LIFE_EXITING: |
| 2644 | return -EBUSY; |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | /* Should only happen if tpacpi_lifecycle is corrupt */ |
| 2648 | BUG(); |
| 2649 | return -EBUSY; |
| 2650 | } |
| 2651 | |
| 2652 | static void hotkey_inputdev_close(struct input_dev *dev) |
| 2653 | { |
| 2654 | /* disable hotkey polling when possible */ |
Henrique de Moraes Holschuh | b589ea4 | 2010-02-25 21:28:58 -0300 | [diff] [blame] | 2655 | if (tpacpi_lifecycle != TPACPI_LIFE_EXITING && |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2656 | !(hotkey_source_mask & hotkey_driver_mask)) |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2657 | hotkey_poll_setup_safe(false); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2658 | } |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2659 | |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2660 | /* sysfs hotkey enable ------------------------------------------------- */ |
| 2661 | static ssize_t hotkey_enable_show(struct device *dev, |
| 2662 | struct device_attribute *attr, |
| 2663 | char *buf) |
| 2664 | { |
Henrique de Moraes Holschuh | ae92bd1 | 2007-07-18 23:45:29 -0300 | [diff] [blame] | 2665 | int res, status; |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2666 | |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 2667 | printk_deprecated_attribute("hotkey_enable", |
| 2668 | "Hotkey reporting is always enabled"); |
| 2669 | |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2670 | res = hotkey_status_get(&status); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2671 | if (res) |
| 2672 | return res; |
| 2673 | |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2674 | return sysfs_emit(buf, "%d\n", status); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | static ssize_t hotkey_enable_store(struct device *dev, |
| 2678 | struct device_attribute *attr, |
| 2679 | const char *buf, size_t count) |
| 2680 | { |
| 2681 | unsigned long t; |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 2682 | |
| 2683 | printk_deprecated_attribute("hotkey_enable", |
| 2684 | "Hotkeys can be disabled through hotkey_mask"); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2685 | |
| 2686 | if (parse_strtoul(buf, 1, &t)) |
| 2687 | return -EINVAL; |
| 2688 | |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 2689 | if (t == 0) |
| 2690 | return -EPERM; |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2691 | |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 2692 | return count; |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2693 | } |
| 2694 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 2695 | static DEVICE_ATTR_RW(hotkey_enable); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2696 | |
| 2697 | /* sysfs hotkey mask --------------------------------------------------- */ |
| 2698 | static ssize_t hotkey_mask_show(struct device *dev, |
| 2699 | struct device_attribute *attr, |
| 2700 | char *buf) |
| 2701 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2702 | return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | static ssize_t hotkey_mask_store(struct device *dev, |
| 2706 | struct device_attribute *attr, |
| 2707 | const char *buf, size_t count) |
| 2708 | { |
| 2709 | unsigned long t; |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2710 | int res; |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2711 | |
Henrique de Moraes Holschuh | ae92bd1 | 2007-07-18 23:45:29 -0300 | [diff] [blame] | 2712 | if (parse_strtoul(buf, 0xffffffffUL, &t)) |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2713 | return -EINVAL; |
| 2714 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 2715 | if (mutex_lock_killable(&hotkey_mutex)) |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2716 | return -ERESTARTSYS; |
| 2717 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2718 | res = hotkey_user_mask_set(t); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2719 | |
| 2720 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2721 | hotkey_poll_setup(true); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2722 | #endif |
| 2723 | |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 2724 | mutex_unlock(&hotkey_mutex); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2725 | |
Henrique de Moraes Holschuh | 56e2c20 | 2009-04-04 04:25:51 +0000 | [diff] [blame] | 2726 | tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t); |
| 2727 | |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2728 | return (res) ? res : count; |
| 2729 | } |
| 2730 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 2731 | static DEVICE_ATTR_RW(hotkey_mask); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2732 | |
| 2733 | /* sysfs hotkey bios_enabled ------------------------------------------- */ |
| 2734 | static ssize_t hotkey_bios_enabled_show(struct device *dev, |
| 2735 | struct device_attribute *attr, |
| 2736 | char *buf) |
| 2737 | { |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 2738 | return sprintf(buf, "0\n"); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2739 | } |
| 2740 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 2741 | static DEVICE_ATTR_RO(hotkey_bios_enabled); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2742 | |
| 2743 | /* sysfs hotkey bios_mask ---------------------------------------------- */ |
| 2744 | static ssize_t hotkey_bios_mask_show(struct device *dev, |
| 2745 | struct device_attribute *attr, |
| 2746 | char *buf) |
| 2747 | { |
Henrique de Moraes Holschuh | 06777be | 2009-09-12 15:22:15 -0300 | [diff] [blame] | 2748 | printk_deprecated_attribute("hotkey_bios_mask", |
| 2749 | "This attribute is useless."); |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2750 | return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2751 | } |
| 2752 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 2753 | static DEVICE_ATTR_RO(hotkey_bios_mask); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 2754 | |
Henrique de Moraes Holschuh | 9b010de | 2007-07-18 23:45:30 -0300 | [diff] [blame] | 2755 | /* sysfs hotkey all_mask ----------------------------------------------- */ |
| 2756 | static ssize_t hotkey_all_mask_show(struct device *dev, |
| 2757 | struct device_attribute *attr, |
| 2758 | char *buf) |
| 2759 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2760 | return sysfs_emit(buf, "0x%08x\n", |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2761 | hotkey_all_mask | hotkey_source_mask); |
Henrique de Moraes Holschuh | 9b010de | 2007-07-18 23:45:30 -0300 | [diff] [blame] | 2762 | } |
| 2763 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 2764 | static DEVICE_ATTR_RO(hotkey_all_mask); |
Henrique de Moraes Holschuh | 9b010de | 2007-07-18 23:45:30 -0300 | [diff] [blame] | 2765 | |
Dennis Wassenberg | 0118c2d | 2016-06-08 10:54:25 -0400 | [diff] [blame] | 2766 | /* sysfs hotkey all_mask ----------------------------------------------- */ |
| 2767 | static ssize_t hotkey_adaptive_all_mask_show(struct device *dev, |
| 2768 | struct device_attribute *attr, |
| 2769 | char *buf) |
| 2770 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2771 | return sysfs_emit(buf, "0x%08x\n", |
Dennis Wassenberg | 0118c2d | 2016-06-08 10:54:25 -0400 | [diff] [blame] | 2772 | hotkey_adaptive_all_mask | hotkey_source_mask); |
| 2773 | } |
| 2774 | |
| 2775 | static DEVICE_ATTR_RO(hotkey_adaptive_all_mask); |
| 2776 | |
Henrique de Moraes Holschuh | 9b010de | 2007-07-18 23:45:30 -0300 | [diff] [blame] | 2777 | /* sysfs hotkey recommended_mask --------------------------------------- */ |
| 2778 | static ssize_t hotkey_recommended_mask_show(struct device *dev, |
| 2779 | struct device_attribute *attr, |
| 2780 | char *buf) |
| 2781 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2782 | return sysfs_emit(buf, "0x%08x\n", |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2783 | (hotkey_all_mask | hotkey_source_mask) |
| 2784 | & ~hotkey_reserved_mask); |
Henrique de Moraes Holschuh | 9b010de | 2007-07-18 23:45:30 -0300 | [diff] [blame] | 2785 | } |
| 2786 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 2787 | static DEVICE_ATTR_RO(hotkey_recommended_mask); |
Henrique de Moraes Holschuh | 9b010de | 2007-07-18 23:45:30 -0300 | [diff] [blame] | 2788 | |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2789 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
| 2790 | |
| 2791 | /* sysfs hotkey hotkey_source_mask ------------------------------------- */ |
| 2792 | static ssize_t hotkey_source_mask_show(struct device *dev, |
| 2793 | struct device_attribute *attr, |
| 2794 | char *buf) |
| 2795 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2796 | return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | static ssize_t hotkey_source_mask_store(struct device *dev, |
| 2800 | struct device_attribute *attr, |
| 2801 | const char *buf, size_t count) |
| 2802 | { |
| 2803 | unsigned long t; |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2804 | u32 r_ev; |
| 2805 | int rc; |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2806 | |
| 2807 | if (parse_strtoul(buf, 0xffffffffUL, &t) || |
| 2808 | ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0)) |
| 2809 | return -EINVAL; |
| 2810 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 2811 | if (mutex_lock_killable(&hotkey_mutex)) |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2812 | return -ERESTARTSYS; |
| 2813 | |
| 2814 | HOTKEY_CONFIG_CRITICAL_START |
| 2815 | hotkey_source_mask = t; |
| 2816 | HOTKEY_CONFIG_CRITICAL_END |
| 2817 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2818 | rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) & |
| 2819 | ~hotkey_source_mask); |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2820 | hotkey_poll_setup(true); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2821 | |
| 2822 | /* check if events needed by the driver got disabled */ |
| 2823 | r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask) |
| 2824 | & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK; |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2825 | |
| 2826 | mutex_unlock(&hotkey_mutex); |
| 2827 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2828 | if (rc < 0) |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 2829 | pr_err("hotkey_source_mask: failed to update the firmware event mask!\n"); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2830 | |
| 2831 | if (r_ev) |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 2832 | pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n", |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 2833 | r_ev); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2834 | |
Henrique de Moraes Holschuh | 56e2c20 | 2009-04-04 04:25:51 +0000 | [diff] [blame] | 2835 | tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t); |
| 2836 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2837 | return (rc < 0) ? rc : count; |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2838 | } |
| 2839 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 2840 | static DEVICE_ATTR_RW(hotkey_source_mask); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2841 | |
| 2842 | /* sysfs hotkey hotkey_poll_freq --------------------------------------- */ |
| 2843 | static ssize_t hotkey_poll_freq_show(struct device *dev, |
| 2844 | struct device_attribute *attr, |
| 2845 | char *buf) |
| 2846 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2847 | return sysfs_emit(buf, "%d\n", hotkey_poll_freq); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | static ssize_t hotkey_poll_freq_store(struct device *dev, |
| 2851 | struct device_attribute *attr, |
| 2852 | const char *buf, size_t count) |
| 2853 | { |
| 2854 | unsigned long t; |
| 2855 | |
| 2856 | if (parse_strtoul(buf, 25, &t)) |
| 2857 | return -EINVAL; |
| 2858 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 2859 | if (mutex_lock_killable(&hotkey_mutex)) |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2860 | return -ERESTARTSYS; |
| 2861 | |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 2862 | hotkey_poll_set_freq(t); |
| 2863 | hotkey_poll_setup(true); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2864 | |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2865 | mutex_unlock(&hotkey_mutex); |
| 2866 | |
Henrique de Moraes Holschuh | 56e2c20 | 2009-04-04 04:25:51 +0000 | [diff] [blame] | 2867 | tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t); |
| 2868 | |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2869 | return count; |
| 2870 | } |
| 2871 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 2872 | static DEVICE_ATTR_RW(hotkey_poll_freq); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 2873 | |
| 2874 | #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ |
| 2875 | |
Henrique de Moraes Holschuh | 50ebec0 | 2008-01-08 13:02:55 -0200 | [diff] [blame] | 2876 | /* sysfs hotkey radio_sw (pollable) ------------------------------------ */ |
Henrique de Moraes Holschuh | 74941a6 | 2007-07-18 23:45:31 -0300 | [diff] [blame] | 2877 | static ssize_t hotkey_radio_sw_show(struct device *dev, |
| 2878 | struct device_attribute *attr, |
| 2879 | char *buf) |
| 2880 | { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 2881 | int res; |
| 2882 | res = hotkey_get_wlsw(); |
Henrique de Moraes Holschuh | 74941a6 | 2007-07-18 23:45:31 -0300 | [diff] [blame] | 2883 | if (res < 0) |
| 2884 | return res; |
| 2885 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 2886 | /* Opportunistic update */ |
| 2887 | tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF)); |
| 2888 | |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2889 | return sysfs_emit(buf, "%d\n", |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 2890 | (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1); |
Henrique de Moraes Holschuh | 74941a6 | 2007-07-18 23:45:31 -0300 | [diff] [blame] | 2891 | } |
| 2892 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 2893 | static DEVICE_ATTR_RO(hotkey_radio_sw); |
Henrique de Moraes Holschuh | 74941a6 | 2007-07-18 23:45:31 -0300 | [diff] [blame] | 2894 | |
Henrique de Moraes Holschuh | 50ebec0 | 2008-01-08 13:02:55 -0200 | [diff] [blame] | 2895 | static void hotkey_radio_sw_notify_change(void) |
| 2896 | { |
| 2897 | if (tp_features.hotkey_wlsw) |
| 2898 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, |
| 2899 | "hotkey_radio_sw"); |
| 2900 | } |
| 2901 | |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 2902 | /* sysfs hotkey tablet mode (pollable) --------------------------------- */ |
| 2903 | static ssize_t hotkey_tablet_mode_show(struct device *dev, |
| 2904 | struct device_attribute *attr, |
| 2905 | char *buf) |
| 2906 | { |
| 2907 | int res, s; |
| 2908 | res = hotkey_get_tablet_mode(&s); |
| 2909 | if (res < 0) |
| 2910 | return res; |
| 2911 | |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2912 | return sysfs_emit(buf, "%d\n", !!s); |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 2913 | } |
| 2914 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 2915 | static DEVICE_ATTR_RO(hotkey_tablet_mode); |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 2916 | |
| 2917 | static void hotkey_tablet_mode_notify_change(void) |
| 2918 | { |
| 2919 | if (tp_features.hotkey_tablet) |
| 2920 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, |
| 2921 | "hotkey_tablet_mode"); |
| 2922 | } |
| 2923 | |
Henrique de Moraes Holschuh | 50ebec0 | 2008-01-08 13:02:55 -0200 | [diff] [blame] | 2924 | /* sysfs wakeup reason (pollable) -------------------------------------- */ |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 2925 | static ssize_t hotkey_wakeup_reason_show(struct device *dev, |
| 2926 | struct device_attribute *attr, |
| 2927 | char *buf) |
| 2928 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2929 | return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason); |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 2930 | } |
| 2931 | |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 2932 | static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL); |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 2933 | |
Adrian Bunk | 1d5a2b5 | 2008-02-13 23:30:06 +0200 | [diff] [blame] | 2934 | static void hotkey_wakeup_reason_notify_change(void) |
Henrique de Moraes Holschuh | 50ebec0 | 2008-01-08 13:02:55 -0200 | [diff] [blame] | 2935 | { |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2936 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, |
| 2937 | "wakeup_reason"); |
Henrique de Moraes Holschuh | 50ebec0 | 2008-01-08 13:02:55 -0200 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */ |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 2941 | static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev, |
| 2942 | struct device_attribute *attr, |
| 2943 | char *buf) |
| 2944 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2945 | return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack); |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 2946 | } |
| 2947 | |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 2948 | static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO, |
| 2949 | hotkey_wakeup_hotunplug_complete_show, NULL); |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 2950 | |
Adrian Bunk | 1d5a2b5 | 2008-02-13 23:30:06 +0200 | [diff] [blame] | 2951 | static void hotkey_wakeup_hotunplug_complete_notify_change(void) |
Henrique de Moraes Holschuh | 50ebec0 | 2008-01-08 13:02:55 -0200 | [diff] [blame] | 2952 | { |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 2953 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, |
| 2954 | "wakeup_hotunplug_complete"); |
Henrique de Moraes Holschuh | 50ebec0 | 2008-01-08 13:02:55 -0200 | [diff] [blame] | 2955 | } |
| 2956 | |
Bastien Nocera | b790cee | 2015-03-02 14:45:27 +0100 | [diff] [blame] | 2957 | /* sysfs adaptive kbd mode --------------------------------------------- */ |
| 2958 | |
| 2959 | static int adaptive_keyboard_get_mode(void); |
| 2960 | static int adaptive_keyboard_set_mode(int new_mode); |
| 2961 | |
| 2962 | enum ADAPTIVE_KEY_MODE { |
| 2963 | HOME_MODE, |
| 2964 | WEB_BROWSER_MODE, |
| 2965 | WEB_CONFERENCE_MODE, |
| 2966 | FUNCTION_MODE, |
| 2967 | LAYFLAT_MODE |
| 2968 | }; |
| 2969 | |
| 2970 | static ssize_t adaptive_kbd_mode_show(struct device *dev, |
| 2971 | struct device_attribute *attr, |
| 2972 | char *buf) |
| 2973 | { |
Dan Carpenter | abf9dc0 | 2015-03-11 12:34:50 +0300 | [diff] [blame] | 2974 | int current_mode; |
Bastien Nocera | b790cee | 2015-03-02 14:45:27 +0100 | [diff] [blame] | 2975 | |
| 2976 | current_mode = adaptive_keyboard_get_mode(); |
| 2977 | if (current_mode < 0) |
| 2978 | return current_mode; |
| 2979 | |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 2980 | return sysfs_emit(buf, "%d\n", current_mode); |
Bastien Nocera | b790cee | 2015-03-02 14:45:27 +0100 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | static ssize_t adaptive_kbd_mode_store(struct device *dev, |
| 2984 | struct device_attribute *attr, |
| 2985 | const char *buf, size_t count) |
| 2986 | { |
| 2987 | unsigned long t; |
| 2988 | int res; |
| 2989 | |
| 2990 | if (parse_strtoul(buf, LAYFLAT_MODE, &t)) |
| 2991 | return -EINVAL; |
| 2992 | |
| 2993 | res = adaptive_keyboard_set_mode(t); |
| 2994 | return (res < 0) ? res : count; |
| 2995 | } |
| 2996 | |
| 2997 | static DEVICE_ATTR_RW(adaptive_kbd_mode); |
| 2998 | |
| 2999 | static struct attribute *adaptive_kbd_attributes[] = { |
| 3000 | &dev_attr_adaptive_kbd_mode.attr, |
| 3001 | NULL |
| 3002 | }; |
| 3003 | |
| 3004 | static const struct attribute_group adaptive_kbd_attr_group = { |
| 3005 | .attrs = adaptive_kbd_attributes, |
| 3006 | }; |
| 3007 | |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 3008 | /* --------------------------------------------------------------------- */ |
| 3009 | |
Len Baker | c99ca78 | 2021-09-26 13:19:08 +0200 | [diff] [blame] | 3010 | static struct attribute *hotkey_attributes[] = { |
Henrique de Moraes Holschuh | ff80f13 | 2007-09-04 11:13:15 -0300 | [diff] [blame] | 3011 | &dev_attr_hotkey_enable.attr, |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 3012 | &dev_attr_hotkey_bios_enabled.attr, |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3013 | &dev_attr_hotkey_bios_mask.attr, |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 3014 | &dev_attr_wakeup_reason.attr, |
| 3015 | &dev_attr_wakeup_hotunplug_complete.attr, |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 3016 | &dev_attr_hotkey_mask.attr, |
| 3017 | &dev_attr_hotkey_all_mask.attr, |
Dennis Wassenberg | 0118c2d | 2016-06-08 10:54:25 -0400 | [diff] [blame] | 3018 | &dev_attr_hotkey_adaptive_all_mask.attr, |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 3019 | &dev_attr_hotkey_recommended_mask.attr, |
Hans de Goede | b68f8a1 | 2021-11-23 22:04:19 +0100 | [diff] [blame] | 3020 | &dev_attr_hotkey_tablet_mode.attr, |
| 3021 | &dev_attr_hotkey_radio_sw.attr, |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3022 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 3023 | &dev_attr_hotkey_source_mask.attr, |
| 3024 | &dev_attr_hotkey_poll_freq.attr, |
| 3025 | #endif |
Len Baker | c99ca78 | 2021-09-26 13:19:08 +0200 | [diff] [blame] | 3026 | NULL |
| 3027 | }; |
| 3028 | |
| 3029 | static umode_t hotkey_attr_is_visible(struct kobject *kobj, |
| 3030 | struct attribute *attr, int n) |
| 3031 | { |
| 3032 | if (attr == &dev_attr_hotkey_tablet_mode.attr) { |
| 3033 | if (!tp_features.hotkey_tablet) |
| 3034 | return 0; |
| 3035 | } else if (attr == &dev_attr_hotkey_radio_sw.attr) { |
| 3036 | if (!tp_features.hotkey_wlsw) |
| 3037 | return 0; |
| 3038 | } |
| 3039 | |
| 3040 | return attr->mode; |
| 3041 | } |
| 3042 | |
| 3043 | static const struct attribute_group hotkey_attr_group = { |
| 3044 | .is_visible = hotkey_attr_is_visible, |
| 3045 | .attrs = hotkey_attributes, |
Henrique de Moraes Holschuh | ff80f13 | 2007-09-04 11:13:15 -0300 | [diff] [blame] | 3046 | }; |
| 3047 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 3048 | /* |
| 3049 | * Sync both the hw and sw blocking state of all switches |
| 3050 | */ |
Henrique de Moraes Holschuh | 733e27c | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 3051 | static void tpacpi_send_radiosw_update(void) |
| 3052 | { |
| 3053 | int wlsw; |
| 3054 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 3055 | /* |
| 3056 | * We must sync all rfkill controllers *before* issuing any |
| 3057 | * rfkill input events, or we will race the rfkill core input |
| 3058 | * handler. |
| 3059 | * |
Lucas De Marchi | c844033 | 2011-03-17 17:18:22 -0300 | [diff] [blame] | 3060 | * tpacpi_inputdev_send_mutex works as a synchronization point |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 3061 | * for the above. |
| 3062 | * |
| 3063 | * We optimize to avoid numerous calls to hotkey_get_wlsw. |
| 3064 | */ |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 3065 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 3066 | wlsw = hotkey_get_wlsw(); |
| 3067 | |
| 3068 | /* Sync hw blocking state first if it is hw-blocked */ |
| 3069 | if (wlsw == TPACPI_RFK_RADIO_OFF) |
| 3070 | tpacpi_rfk_update_hwblock_state(true); |
| 3071 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 3072 | /* Sync hw blocking state last if it is hw-unblocked */ |
| 3073 | if (wlsw == TPACPI_RFK_RADIO_ON) |
| 3074 | tpacpi_rfk_update_hwblock_state(false); |
| 3075 | |
| 3076 | /* Issue rfkill input event for WLSW switch */ |
| 3077 | if (!(wlsw < 0)) { |
Henrique de Moraes Holschuh | 733e27c | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 3078 | mutex_lock(&tpacpi_inputdev_send_mutex); |
| 3079 | |
| 3080 | input_report_switch(tpacpi_inputdev, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 3081 | SW_RFKILL_ALL, (wlsw > 0)); |
Henrique de Moraes Holschuh | 733e27c | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 3082 | input_sync(tpacpi_inputdev); |
| 3083 | |
| 3084 | mutex_unlock(&tpacpi_inputdev_send_mutex); |
| 3085 | } |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 3086 | |
| 3087 | /* |
| 3088 | * this can be unconditional, as we will poll state again |
| 3089 | * if userspace uses the notify to read data |
| 3090 | */ |
Henrique de Moraes Holschuh | 733e27c | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 3091 | hotkey_radio_sw_notify_change(); |
| 3092 | } |
| 3093 | |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3094 | static void hotkey_exit(void) |
| 3095 | { |
| 3096 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 3097 | mutex_lock(&hotkey_mutex); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3098 | hotkey_poll_stop_sync(); |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 3099 | mutex_unlock(&hotkey_mutex); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3100 | #endif |
Len Baker | c99ca78 | 2021-09-26 13:19:08 +0200 | [diff] [blame] | 3101 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, &hotkey_attr_group); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3102 | |
Henrique de Moraes Holschuh | 4be7300 | 2009-09-20 14:09:23 -0300 | [diff] [blame] | 3103 | dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY, |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3104 | "restoring original HKEY status and mask\n"); |
| 3105 | /* yes, there is a bitwise or below, we want the |
| 3106 | * functions to be called even if one of them fail */ |
| 3107 | if (((tp_features.hotkey_mask && |
| 3108 | hotkey_mask_set(hotkey_orig_mask)) | |
| 3109 | hotkey_status_set(false)) != 0) |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 3110 | pr_err("failed to restore hot key mask to BIOS defaults\n"); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3111 | } |
| 3112 | |
Henrique de Moraes Holschuh | de4c8cc | 2009-09-12 15:22:18 -0300 | [diff] [blame] | 3113 | static void __init hotkey_unmap(const unsigned int scancode) |
| 3114 | { |
| 3115 | if (hotkey_keycode_map[scancode] != KEY_RESERVED) { |
| 3116 | clear_bit(hotkey_keycode_map[scancode], |
| 3117 | tpacpi_inputdev->keybit); |
| 3118 | hotkey_keycode_map[scancode] = KEY_RESERVED; |
| 3119 | } |
| 3120 | } |
| 3121 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3122 | /* |
| 3123 | * HKEY quirks: |
| 3124 | * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12 |
| 3125 | */ |
| 3126 | |
| 3127 | #define TPACPI_HK_Q_INIMASK 0x0001 |
| 3128 | |
| 3129 | static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = { |
| 3130 | TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */ |
| 3131 | TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */ |
| 3132 | TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */ |
| 3133 | TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */ |
| 3134 | TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */ |
| 3135 | TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */ |
| 3136 | TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */ |
| 3137 | TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */ |
| 3138 | TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */ |
| 3139 | TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */ |
| 3140 | TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */ |
| 3141 | TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */ |
| 3142 | TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */ |
| 3143 | TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */ |
| 3144 | TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */ |
| 3145 | TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */ |
| 3146 | TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */ |
| 3147 | TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */ |
| 3148 | TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */ |
| 3149 | }; |
| 3150 | |
Henrique de Moraes Holschuh | fc6e756 | 2010-09-17 21:53:41 -0300 | [diff] [blame] | 3151 | typedef u16 tpacpi_keymap_entry_t; |
| 3152 | typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN]; |
Henrique de Moraes Holschuh | d1e14dc | 2010-08-09 23:48:21 -0300 | [diff] [blame] | 3153 | |
Lyude | b318002 | 2016-11-11 15:15:02 -0500 | [diff] [blame] | 3154 | static int hotkey_init_tablet_mode(void) |
| 3155 | { |
Darren Hart | cb2bf25 | 2016-12-14 14:45:48 -0800 | [diff] [blame] | 3156 | int in_tablet_mode = 0, res; |
| 3157 | char *type = NULL; |
Lyude | b318002 | 2016-11-11 15:15:02 -0500 | [diff] [blame] | 3158 | |
Benjamin Berg | dda3ec0 | 2017-09-15 15:20:49 +0200 | [diff] [blame] | 3159 | if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) { |
| 3160 | int has_tablet_mode; |
| 3161 | |
| 3162 | in_tablet_mode = hotkey_gmms_get_tablet_mode(res, |
| 3163 | &has_tablet_mode); |
Hans de Goede | 685489a | 2020-11-06 15:01:30 +0100 | [diff] [blame] | 3164 | /* |
| 3165 | * The Yoga 11e series has 2 accelerometers described by a |
| 3166 | * BOSC0200 ACPI node. This setup relies on a Windows service |
| 3167 | * which calls special ACPI methods on this node to report |
| 3168 | * the laptop/tent/tablet mode to the EC. The bmc150 iio driver |
| 3169 | * does not support this, so skip the hotkey on these models. |
| 3170 | */ |
Hans de Goede | 153cca9 | 2021-07-29 10:21:34 +0200 | [diff] [blame] | 3171 | if (has_tablet_mode && !dual_accel_detect()) |
Benjamin Berg | dda3ec0 | 2017-09-15 15:20:49 +0200 | [diff] [blame] | 3172 | tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS; |
| 3173 | type = "GMMS"; |
| 3174 | } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) { |
Lyude | b03f4d4 | 2016-11-11 15:15:03 -0500 | [diff] [blame] | 3175 | /* For X41t, X60t, X61t Tablets... */ |
Lyude | b318002 | 2016-11-11 15:15:02 -0500 | [diff] [blame] | 3176 | tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG; |
| 3177 | in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK); |
| 3178 | type = "MHKG"; |
| 3179 | } |
| 3180 | |
| 3181 | if (!tp_features.hotkey_tablet) |
| 3182 | return 0; |
| 3183 | |
| 3184 | pr_info("Tablet mode switch found (type: %s), currently in %s mode\n", |
| 3185 | type, in_tablet_mode ? "tablet" : "laptop"); |
| 3186 | |
Lyude | b318002 | 2016-11-11 15:15:02 -0500 | [diff] [blame] | 3187 | return in_tablet_mode; |
| 3188 | } |
| 3189 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 3190 | static int __init hotkey_init(struct ibm_init_struct *iibm) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 3191 | { |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3192 | /* Requirements for changing the default keymaps: |
| 3193 | * |
| 3194 | * 1. Many of the keys are mapped to KEY_RESERVED for very |
| 3195 | * good reasons. Do not change them unless you have deep |
| 3196 | * knowledge on the IBM and Lenovo ThinkPad firmware for |
| 3197 | * the various ThinkPad models. The driver behaves |
| 3198 | * differently for KEY_RESERVED: such keys have their |
| 3199 | * hot key mask *unset* in mask_recommended, and also |
| 3200 | * in the initial hot key mask programmed into the |
| 3201 | * firmware at driver load time, which means the firm- |
| 3202 | * ware may react very differently if you change them to |
| 3203 | * something else; |
| 3204 | * |
| 3205 | * 2. You must be subscribed to the linux-thinkpad and |
| 3206 | * ibm-acpi-devel mailing lists, and you should read the |
| 3207 | * list archives since 2007 if you want to change the |
| 3208 | * keymaps. This requirement exists so that you will |
| 3209 | * know the past history of problems with the thinkpad- |
| 3210 | * acpi driver keymaps, and also that you will be |
| 3211 | * listening to any bug reports; |
| 3212 | * |
| 3213 | * 3. Do not send thinkpad-acpi specific patches directly to |
| 3214 | * for merging, *ever*. Send them to the linux-acpi |
| 3215 | * mailinglist for comments. Merging is to be done only |
| 3216 | * through acpi-test and the ACPI maintainer. |
| 3217 | * |
| 3218 | * If the above is too much to ask, don't change the keymap. |
| 3219 | * Ask the thinkpad-acpi maintainer to do it, instead. |
| 3220 | */ |
Henrique de Moraes Holschuh | d1e14dc | 2010-08-09 23:48:21 -0300 | [diff] [blame] | 3221 | |
| 3222 | enum keymap_index { |
| 3223 | TPACPI_KEYMAP_IBM_GENERIC = 0, |
| 3224 | TPACPI_KEYMAP_LENOVO_GENERIC, |
| 3225 | }; |
| 3226 | |
| 3227 | static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = { |
| 3228 | /* Generic keymap for IBM ThinkPads */ |
| 3229 | [TPACPI_KEYMAP_IBM_GENERIC] = { |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3230 | /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */ |
Henrique de Moraes Holschuh | d1e14dc | 2010-08-09 23:48:21 -0300 | [diff] [blame] | 3231 | KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP, |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3232 | KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8, |
| 3233 | KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND, |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3234 | |
| 3235 | /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */ |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3236 | KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */ |
| 3237 | KEY_UNKNOWN, /* 0x0D: FN+INSERT */ |
| 3238 | KEY_UNKNOWN, /* 0x0E: FN+DELETE */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3239 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3240 | /* brightness: firmware always reacts to them */ |
Henrique de Moraes Holschuh | e927c08 | 2007-10-30 17:46:19 -0200 | [diff] [blame] | 3241 | KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */ |
Henrique de Moraes Holschuh | e927c08 | 2007-10-30 17:46:19 -0200 | [diff] [blame] | 3242 | KEY_RESERVED, /* 0x10: FN+END (brightness down) */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3243 | |
| 3244 | /* Thinklight: firmware always react to it */ |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3245 | KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3246 | |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3247 | KEY_UNKNOWN, /* 0x12: FN+PGDOWN */ |
| 3248 | KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3249 | |
| 3250 | /* Volume: firmware always react to it and reprograms |
| 3251 | * the built-in *extra* mixer. Never map it to control |
| 3252 | * another mixer by default. */ |
Henrique de Moraes Holschuh | e927c08 | 2007-10-30 17:46:19 -0200 | [diff] [blame] | 3253 | KEY_RESERVED, /* 0x14: VOLUME UP */ |
| 3254 | KEY_RESERVED, /* 0x15: VOLUME DOWN */ |
| 3255 | KEY_RESERVED, /* 0x16: MUTE */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3256 | |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3257 | KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3258 | |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3259 | /* (assignments unknown, please report if found) */ |
| 3260 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3261 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
Bastien Nocera | 6a68d85 | 2015-03-02 14:45:31 +0100 | [diff] [blame] | 3262 | |
| 3263 | /* No assignments, only used for Adaptive keyboards. */ |
| 3264 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3265 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3266 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3267 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3268 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
Christian Kellner | 696c6523 | 2017-02-28 17:10:57 +0100 | [diff] [blame] | 3269 | |
| 3270 | /* No assignment, used for newer Lenovo models */ |
| 3271 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3272 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3273 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3274 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3275 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3276 | KEY_UNKNOWN, KEY_UNKNOWN |
| 3277 | |
Henrique de Moraes Holschuh | d1e14dc | 2010-08-09 23:48:21 -0300 | [diff] [blame] | 3278 | }, |
| 3279 | |
| 3280 | /* Generic keymap for Lenovo ThinkPads */ |
| 3281 | [TPACPI_KEYMAP_LENOVO_GENERIC] = { |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3282 | /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */ |
| 3283 | KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP, |
Jens Taprogge | 2b75426 | 2010-08-09 23:48:22 -0300 | [diff] [blame] | 3284 | KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8, |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3285 | KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND, |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3286 | |
| 3287 | /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */ |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3288 | KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */ |
| 3289 | KEY_UNKNOWN, /* 0x0D: FN+INSERT */ |
| 3290 | KEY_UNKNOWN, /* 0x0E: FN+DELETE */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3291 | |
Henrique de Moraes Holschuh | de4c8cc | 2009-09-12 15:22:18 -0300 | [diff] [blame] | 3292 | /* These should be enabled --only-- when ACPI video |
| 3293 | * is disabled (i.e. in "vendor" mode), and are handled |
| 3294 | * in a special way by the init code */ |
| 3295 | KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */ |
| 3296 | KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3297 | |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3298 | KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3299 | |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3300 | KEY_UNKNOWN, /* 0x12: FN+PGDOWN */ |
| 3301 | KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3302 | |
| 3303 | /* Volume: z60/z61, T60 (BIOS version?): firmware always |
| 3304 | * react to it and reprograms the built-in *extra* mixer. |
| 3305 | * Never map it to control another mixer by default. |
| 3306 | * |
| 3307 | * T60?, T61, R60?, R61: firmware and EC tries to send |
| 3308 | * these over the regular keyboard, so these are no-ops, |
| 3309 | * but there are still weird bugs re. MUTE, so do not |
| 3310 | * change unless you get test reports from all Lenovo |
| 3311 | * models. May cause the BIOS to interfere with the |
| 3312 | * HDA mixer. |
| 3313 | */ |
Henrique de Moraes Holschuh | e927c08 | 2007-10-30 17:46:19 -0200 | [diff] [blame] | 3314 | KEY_RESERVED, /* 0x14: VOLUME UP */ |
| 3315 | KEY_RESERVED, /* 0x15: VOLUME DOWN */ |
| 3316 | KEY_RESERVED, /* 0x16: MUTE */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3317 | |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3318 | KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */ |
Henrique de Moraes Holschuh | 0f08914 | 2008-01-08 13:02:38 -0200 | [diff] [blame] | 3319 | |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3320 | /* (assignments unknown, please report if found) */ |
Andy Lutomirski | 3300955 | 2011-05-24 15:16:43 -0400 | [diff] [blame] | 3321 | KEY_UNKNOWN, KEY_UNKNOWN, |
| 3322 | |
| 3323 | /* |
| 3324 | * The mic mute button only sends 0x1a. It does not |
| 3325 | * automatically mute the mic or change the mute light. |
| 3326 | */ |
| 3327 | KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */ |
| 3328 | |
| 3329 | /* (assignments unknown, please report if found) */ |
Andy Lutomirski | 3300955 | 2011-05-24 15:16:43 -0400 | [diff] [blame] | 3330 | KEY_UNKNOWN, |
Hans de Goede | 8b9dd4f | 2014-04-09 11:46:46 +0200 | [diff] [blame] | 3331 | |
| 3332 | /* Extra keys in use since the X240 / T440 / T540 */ |
Hans de Goede | 4beb81d | 2014-06-23 13:38:23 +0200 | [diff] [blame] | 3333 | KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE, |
Bastien Nocera | 6a68d85 | 2015-03-02 14:45:31 +0100 | [diff] [blame] | 3334 | |
| 3335 | /* |
| 3336 | * These are the adaptive keyboard keycodes for Carbon X1 2014. |
| 3337 | * The first item in this list is the Mute button which is |
| 3338 | * emitted with 0x103 through |
| 3339 | * adaptive_keyboard_hotkey_notify_hotkey() when the sound |
| 3340 | * symbol is held. |
| 3341 | * We'll need to offset those by 0x20. |
| 3342 | */ |
| 3343 | KEY_RESERVED, /* Mute held, 0x103 */ |
| 3344 | KEY_BRIGHTNESS_MIN, /* Backlight off */ |
| 3345 | KEY_RESERVED, /* Clipping tool */ |
| 3346 | KEY_RESERVED, /* Cloud */ |
| 3347 | KEY_RESERVED, |
| 3348 | KEY_VOICECOMMAND, /* Voice */ |
| 3349 | KEY_RESERVED, |
| 3350 | KEY_RESERVED, /* Gestures */ |
| 3351 | KEY_RESERVED, |
| 3352 | KEY_RESERVED, |
| 3353 | KEY_RESERVED, |
| 3354 | KEY_CONFIG, /* Settings */ |
| 3355 | KEY_RESERVED, /* New tab */ |
| 3356 | KEY_REFRESH, /* Reload */ |
| 3357 | KEY_BACK, /* Back */ |
| 3358 | KEY_RESERVED, /* Microphone down */ |
| 3359 | KEY_RESERVED, /* Microphone up */ |
| 3360 | KEY_RESERVED, /* Microphone cancellation */ |
| 3361 | KEY_RESERVED, /* Camera mode */ |
| 3362 | KEY_RESERVED, /* Rotate display, 0x116 */ |
Christian Kellner | 696c6523 | 2017-02-28 17:10:57 +0100 | [diff] [blame] | 3363 | |
| 3364 | /* |
| 3365 | * These are found in 2017 models (e.g. T470s, X270). |
| 3366 | * The lowest known value is 0x311, which according to |
| 3367 | * the manual should launch a user defined favorite |
| 3368 | * application. |
| 3369 | * |
| 3370 | * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START, |
| 3371 | * corresponding to 0x34. |
| 3372 | */ |
| 3373 | |
| 3374 | /* (assignments unknown, please report if found) */ |
| 3375 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3376 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3377 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3378 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, |
| 3379 | KEY_UNKNOWN, |
| 3380 | |
Hans de Goede | 7ed7748 | 2020-09-08 12:21:11 -0700 | [diff] [blame] | 3381 | KEY_BOOKMARKS, /* Favorite app, 0x311 */ |
Hans de Goede | e2c8c4e | 2020-09-08 12:21:50 -0700 | [diff] [blame] | 3382 | KEY_SELECTIVE_SCREENSHOT, /* Clipping tool */ |
Hans de Goede | 7ed7748 | 2020-09-08 12:21:11 -0700 | [diff] [blame] | 3383 | KEY_CALC, /* Calculator (above numpad, P52) */ |
| 3384 | KEY_BLUETOOTH, /* Bluetooth */ |
| 3385 | KEY_KEYBOARD, /* Keyboard, 0x315 */ |
| 3386 | KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */ |
| 3387 | KEY_NOTIFICATION_CENTER, /* Notification Center */ |
| 3388 | KEY_PICKUP_PHONE, /* Answer incoming call */ |
| 3389 | KEY_HANGUP_PHONE, /* Decline incoming call */ |
Henrique de Moraes Holschuh | d1e14dc | 2010-08-09 23:48:21 -0300 | [diff] [blame] | 3390 | }, |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3391 | }; |
| 3392 | |
Henrique de Moraes Holschuh | d1e14dc | 2010-08-09 23:48:21 -0300 | [diff] [blame] | 3393 | static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = { |
| 3394 | /* Generic maps (fallback) */ |
| 3395 | { |
| 3396 | .vendor = PCI_VENDOR_ID_IBM, |
| 3397 | .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY, |
| 3398 | .quirks = TPACPI_KEYMAP_IBM_GENERIC, |
| 3399 | }, |
| 3400 | { |
| 3401 | .vendor = PCI_VENDOR_ID_LENOVO, |
| 3402 | .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY, |
| 3403 | .quirks = TPACPI_KEYMAP_LENOVO_GENERIC, |
| 3404 | }, |
| 3405 | }; |
| 3406 | |
| 3407 | #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t) |
Henrique de Moraes Holschuh | fc6e756 | 2010-09-17 21:53:41 -0300 | [diff] [blame] | 3408 | #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t) |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3409 | |
Henrique de Moraes Holschuh | 6a38abb | 2007-07-18 23:45:35 -0300 | [diff] [blame] | 3410 | int res, i; |
Henrique de Moraes Holschuh | 74941a6 | 2007-07-18 23:45:31 -0300 | [diff] [blame] | 3411 | int status; |
Henrique de Moraes Holschuh | 1b6521d | 2007-09-23 11:39:03 -0300 | [diff] [blame] | 3412 | int hkeyv; |
Henrique de Moraes Holschuh | d89a727 | 2009-12-15 21:51:06 -0200 | [diff] [blame] | 3413 | bool radiosw_state = false; |
| 3414 | bool tabletsw_state = false; |
Henrique de Moraes Holschuh | b86c472 | 2007-04-21 11:08:39 -0300 | [diff] [blame] | 3415 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3416 | unsigned long quirks; |
Henrique de Moraes Holschuh | d1e14dc | 2010-08-09 23:48:21 -0300 | [diff] [blame] | 3417 | unsigned long keymap_id; |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3418 | |
Henrique de Moraes Holschuh | 56e2c20 | 2009-04-04 04:25:51 +0000 | [diff] [blame] | 3419 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, |
| 3420 | "initializing hotkey subdriver\n"); |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 3421 | |
Henrique de Moraes Holschuh | 6a38abb | 2007-07-18 23:45:35 -0300 | [diff] [blame] | 3422 | BUG_ON(!tpacpi_inputdev); |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 3423 | BUG_ON(tpacpi_inputdev->open != NULL || |
| 3424 | tpacpi_inputdev->close != NULL); |
Henrique de Moraes Holschuh | 6a38abb | 2007-07-18 23:45:35 -0300 | [diff] [blame] | 3425 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 3426 | TPACPI_ACPIHANDLE_INIT(hkey); |
Henrique de Moraes Holschuh | 40ca9fd | 2007-04-24 11:48:15 -0300 | [diff] [blame] | 3427 | mutex_init(&hotkey_mutex); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 3428 | |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 3429 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
Henrique de Moraes Holschuh | 01e88f2 | 2008-01-08 13:02:41 -0200 | [diff] [blame] | 3430 | mutex_init(&hotkey_thread_data_mutex); |
| 3431 | #endif |
| 3432 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 3433 | /* hotkey not supported on 570 */ |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 3434 | tp_features.hotkey = hkey_handle != NULL; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 3435 | |
Henrique de Moraes Holschuh | 56e2c20 | 2009-04-04 04:25:51 +0000 | [diff] [blame] | 3436 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, |
| 3437 | "hotkeys are %s\n", |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 3438 | str_supported(tp_features.hotkey)); |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 3439 | |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3440 | if (!tp_features.hotkey) |
| 3441 | return 1; |
| 3442 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3443 | quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable, |
| 3444 | ARRAY_SIZE(tpacpi_hotkey_qtable)); |
| 3445 | |
Henrique de Moraes Holschuh | d64c81c4 | 2008-10-18 14:23:55 -0300 | [diff] [blame] | 3446 | tpacpi_disable_brightness_delay(); |
| 3447 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3448 | /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p, |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3449 | A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking |
| 3450 | for HKEY interface version 0x100 */ |
| 3451 | if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) { |
Dennis Wassenberg | 0118c2d | 2016-06-08 10:54:25 -0400 | [diff] [blame] | 3452 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, |
| 3453 | "firmware HKEY interface version: 0x%x\n", |
| 3454 | hkeyv); |
| 3455 | |
| 3456 | switch (hkeyv >> 8) { |
| 3457 | case 1: |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3458 | /* |
| 3459 | * MHKV 0x100 in A31, R40, R40e, |
| 3460 | * T4x, X31, and later |
| 3461 | */ |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3462 | |
| 3463 | /* Paranoia check AND init hotkey_all_mask */ |
| 3464 | if (!acpi_evalf(hkey_handle, &hotkey_all_mask, |
| 3465 | "MHKA", "qd")) { |
Dennis Wassenberg | 0118c2d | 2016-06-08 10:54:25 -0400 | [diff] [blame] | 3466 | pr_err("missing MHKA handler, please report this to %s\n", |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3467 | TPACPI_MAIL); |
| 3468 | /* Fallback: pre-init for FN+F3,F4,F12 */ |
| 3469 | hotkey_all_mask = 0x080cU; |
| 3470 | } else { |
| 3471 | tp_features.hotkey_mask = 1; |
| 3472 | } |
Dennis Wassenberg | 0118c2d | 2016-06-08 10:54:25 -0400 | [diff] [blame] | 3473 | break; |
| 3474 | |
| 3475 | case 2: |
| 3476 | /* |
| 3477 | * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016) |
| 3478 | */ |
| 3479 | |
| 3480 | /* Paranoia check AND init hotkey_all_mask */ |
| 3481 | if (!acpi_evalf(hkey_handle, &hotkey_all_mask, |
| 3482 | "MHKA", "dd", 1)) { |
| 3483 | pr_err("missing MHKA handler, please report this to %s\n", |
| 3484 | TPACPI_MAIL); |
| 3485 | /* Fallback: pre-init for FN+F3,F4,F12 */ |
| 3486 | hotkey_all_mask = 0x080cU; |
| 3487 | } else { |
| 3488 | tp_features.hotkey_mask = 1; |
| 3489 | } |
| 3490 | |
| 3491 | /* |
| 3492 | * Check if we have an adaptive keyboard, like on the |
| 3493 | * Lenovo Carbon X1 2014 (2nd Gen). |
| 3494 | */ |
| 3495 | if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask, |
| 3496 | "MHKA", "dd", 2)) { |
| 3497 | if (hotkey_adaptive_all_mask != 0) { |
| 3498 | tp_features.has_adaptive_kbd = true; |
| 3499 | res = sysfs_create_group( |
| 3500 | &tpacpi_pdev->dev.kobj, |
| 3501 | &adaptive_kbd_attr_group); |
| 3502 | if (res) |
| 3503 | goto err_exit; |
| 3504 | } |
| 3505 | } else { |
| 3506 | tp_features.has_adaptive_kbd = false; |
| 3507 | hotkey_adaptive_all_mask = 0x0U; |
| 3508 | } |
| 3509 | break; |
| 3510 | |
| 3511 | default: |
| 3512 | pr_err("unknown version of the HKEY interface: 0x%x\n", |
| 3513 | hkeyv); |
| 3514 | pr_err("please report this to %s\n", TPACPI_MAIL); |
| 3515 | break; |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3516 | } |
| 3517 | } |
| 3518 | |
Henrique de Moraes Holschuh | 56e2c20 | 2009-04-04 04:25:51 +0000 | [diff] [blame] | 3519 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, |
| 3520 | "hotkey masks are %s\n", |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3521 | str_supported(tp_features.hotkey_mask)); |
| 3522 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3523 | /* Init hotkey_all_mask if not initialized yet */ |
| 3524 | if (!tp_features.hotkey_mask && !hotkey_all_mask && |
| 3525 | (quirks & TPACPI_HK_Q_INIMASK)) |
| 3526 | hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */ |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3527 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3528 | /* Init hotkey_acpi_mask and hotkey_orig_mask */ |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3529 | if (tp_features.hotkey_mask) { |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3530 | /* hotkey_source_mask *must* be zero for |
| 3531 | * the first hotkey_mask_get to return hotkey_orig_mask */ |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3532 | res = hotkey_mask_get(); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 3533 | if (res) |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3534 | goto err_exit; |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 3535 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3536 | hotkey_orig_mask = hotkey_acpi_mask; |
| 3537 | } else { |
| 3538 | hotkey_orig_mask = hotkey_all_mask; |
| 3539 | hotkey_acpi_mask = hotkey_all_mask; |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3540 | } |
Henrique de Moraes Holschuh | 74941a6 | 2007-07-18 23:45:31 -0300 | [diff] [blame] | 3541 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 3542 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 3543 | if (dbg_wlswemul) { |
| 3544 | tp_features.hotkey_wlsw = 1; |
Henrique de Moraes Holschuh | d89a727 | 2009-12-15 21:51:06 -0200 | [diff] [blame] | 3545 | radiosw_state = !!tpacpi_wlsw_emulstate; |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 3546 | pr_info("radio switch emulation enabled\n"); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 3547 | } else |
| 3548 | #endif |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3549 | /* Not all thinkpads have a hardware radio switch */ |
| 3550 | if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) { |
| 3551 | tp_features.hotkey_wlsw = 1; |
Henrique de Moraes Holschuh | d89a727 | 2009-12-15 21:51:06 -0200 | [diff] [blame] | 3552 | radiosw_state = !!status; |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 3553 | pr_info("radio switch found; radios are %s\n", |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3554 | enabled(status, 0)); |
Henrique de Moraes Holschuh | 3a87208 | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 3555 | } |
Henrique de Moraes Holschuh | 74941a6 | 2007-07-18 23:45:31 -0300 | [diff] [blame] | 3556 | |
Len Baker | c99ca78 | 2021-09-26 13:19:08 +0200 | [diff] [blame] | 3557 | tabletsw_state = hotkey_init_tablet_mode(); |
| 3558 | res = sysfs_create_group(&tpacpi_pdev->dev.kobj, &hotkey_attr_group); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3559 | if (res) |
| 3560 | goto err_exit; |
Henrique de Moraes Holschuh | 6a38abb | 2007-07-18 23:45:35 -0300 | [diff] [blame] | 3561 | |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3562 | /* Set up key map */ |
Henrique de Moraes Holschuh | d1e14dc | 2010-08-09 23:48:21 -0300 | [diff] [blame] | 3563 | keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable, |
| 3564 | ARRAY_SIZE(tpacpi_keymap_qtable)); |
| 3565 | BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps)); |
| 3566 | dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, |
| 3567 | "using keymap number %lu\n", keymap_id); |
| 3568 | |
Fuqian Huang | 35b7c80 | 2019-07-04 00:30:00 +0800 | [diff] [blame] | 3569 | hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id], |
| 3570 | TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL); |
| 3571 | if (!hotkey_keycode_map) { |
| 3572 | pr_err("failed to allocate memory for key map\n"); |
| 3573 | res = -ENOMEM; |
| 3574 | goto err_exit; |
| 3575 | } |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3576 | |
Henrique de Moraes Holschuh | 792979c | 2009-12-09 01:36:29 +0000 | [diff] [blame] | 3577 | input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3578 | tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE; |
| 3579 | tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN; |
| 3580 | tpacpi_inputdev->keycode = hotkey_keycode_map; |
| 3581 | for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) { |
| 3582 | if (hotkey_keycode_map[i] != KEY_RESERVED) { |
Henrique de Moraes Holschuh | 792979c | 2009-12-09 01:36:29 +0000 | [diff] [blame] | 3583 | input_set_capability(tpacpi_inputdev, EV_KEY, |
| 3584 | hotkey_keycode_map[i]); |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3585 | } else { |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3586 | if (i < sizeof(hotkey_reserved_mask)*8) |
| 3587 | hotkey_reserved_mask |= 1 << i; |
Henrique de Moraes Holschuh | edf0e0e | 2007-07-18 23:45:44 -0300 | [diff] [blame] | 3588 | } |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 3589 | } |
| 3590 | |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3591 | if (tp_features.hotkey_wlsw) { |
Henrique de Moraes Holschuh | 792979c | 2009-12-09 01:36:29 +0000 | [diff] [blame] | 3592 | input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL); |
Henrique de Moraes Holschuh | d89a727 | 2009-12-15 21:51:06 -0200 | [diff] [blame] | 3593 | input_report_switch(tpacpi_inputdev, |
| 3594 | SW_RFKILL_ALL, radiosw_state); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3595 | } |
| 3596 | if (tp_features.hotkey_tablet) { |
Henrique de Moraes Holschuh | 792979c | 2009-12-09 01:36:29 +0000 | [diff] [blame] | 3597 | input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE); |
Henrique de Moraes Holschuh | d89a727 | 2009-12-15 21:51:06 -0200 | [diff] [blame] | 3598 | input_report_switch(tpacpi_inputdev, |
| 3599 | SW_TABLET_MODE, tabletsw_state); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 3600 | } |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 3601 | |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3602 | /* Do not issue duplicate brightness change events to |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 3603 | * userspace. tpacpi_detect_brightness_capabilities() must have |
| 3604 | * been called before this point */ |
Hans de Goede | b33c6ce | 2015-06-16 16:28:10 +0200 | [diff] [blame] | 3605 | if (acpi_video_get_backlight_type() != acpi_backlight_vendor) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 3606 | pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n"); |
| 3607 | pr_notice("Disabling thinkpad-acpi brightness events by default...\n"); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3608 | |
Henrique de Moraes Holschuh | de4c8cc | 2009-09-12 15:22:18 -0300 | [diff] [blame] | 3609 | /* Disable brightness up/down on Lenovo thinkpads when |
| 3610 | * ACPI is handling them, otherwise it is plain impossible |
| 3611 | * for userspace to do something even remotely sane */ |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3612 | hotkey_reserved_mask |= |
| 3613 | (1 << TP_ACPI_HOTKEYSCAN_FNHOME) |
| 3614 | | (1 << TP_ACPI_HOTKEYSCAN_FNEND); |
Henrique de Moraes Holschuh | de4c8cc | 2009-09-12 15:22:18 -0300 | [diff] [blame] | 3615 | hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME); |
| 3616 | hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND); |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 3617 | } |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3618 | |
Henrique de Moraes Holschuh | 230d8cf25 | 2009-09-12 15:22:17 -0300 | [diff] [blame] | 3619 | #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3620 | hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK |
| 3621 | & ~hotkey_all_mask |
| 3622 | & ~hotkey_reserved_mask; |
Henrique de Moraes Holschuh | 230d8cf25 | 2009-09-12 15:22:17 -0300 | [diff] [blame] | 3623 | |
| 3624 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, |
| 3625 | "hotkey source mask 0x%08x, polling freq %u\n", |
| 3626 | hotkey_source_mask, hotkey_poll_freq); |
| 3627 | #endif |
| 3628 | |
Henrique de Moraes Holschuh | 56e2c20 | 2009-04-04 04:25:51 +0000 | [diff] [blame] | 3629 | dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, |
| 3630 | "enabling firmware HKEY event interface...\n"); |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 3631 | res = hotkey_status_set(true); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3632 | if (res) { |
| 3633 | hotkey_exit(); |
| 3634 | return res; |
| 3635 | } |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3636 | res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask) |
| 3637 | | hotkey_driver_mask) |
| 3638 | & ~hotkey_source_mask); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3639 | if (res < 0 && res != -ENXIO) { |
| 3640 | hotkey_exit(); |
| 3641 | return res; |
| 3642 | } |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 3643 | hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask) |
| 3644 | & ~hotkey_reserved_mask; |
| 3645 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, |
| 3646 | "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n", |
| 3647 | hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3648 | |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3649 | tpacpi_inputdev->open = &hotkey_inputdev_open; |
| 3650 | tpacpi_inputdev->close = &hotkey_inputdev_close; |
| 3651 | |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 3652 | hotkey_poll_setup_safe(true); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3653 | |
| 3654 | return 0; |
| 3655 | |
| 3656 | err_exit: |
Len Baker | c99ca78 | 2021-09-26 13:19:08 +0200 | [diff] [blame] | 3657 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, &hotkey_attr_group); |
| 3658 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, &adaptive_kbd_attr_group); |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 3659 | |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 3660 | return (res < 0) ? res : 1; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 3661 | } |
| 3662 | |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3663 | /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser |
| 3664 | * mode, Web conference mode, Function mode and Lay-flat mode. |
| 3665 | * We support Home mode and Function mode currently. |
| 3666 | * |
| 3667 | * Will consider support rest of modes in future. |
| 3668 | * |
| 3669 | */ |
Lad, Prabhakar | b201a47 | 2015-02-05 14:45:38 +0000 | [diff] [blame] | 3670 | static const int adaptive_keyboard_modes[] = { |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3671 | HOME_MODE, |
| 3672 | /* WEB_BROWSER_MODE = 2, |
| 3673 | WEB_CONFERENCE_MODE = 3, */ |
| 3674 | FUNCTION_MODE |
| 3675 | }; |
| 3676 | |
| 3677 | #define DFR_CHANGE_ROW 0x101 |
| 3678 | #define DFR_SHOW_QUICKVIEW_ROW 0x102 |
Bastien Nocera | 6a68d85 | 2015-03-02 14:45:31 +0100 | [diff] [blame] | 3679 | #define FIRST_ADAPTIVE_KEY 0x103 |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3680 | |
| 3681 | /* press Fn key a while second, it will switch to Function Mode. Then |
| 3682 | * release Fn key, previous mode be restored. |
| 3683 | */ |
| 3684 | static bool adaptive_keyboard_mode_is_saved; |
| 3685 | static int adaptive_keyboard_prev_mode; |
| 3686 | |
Bastien Nocera | f74587f | 2015-03-02 14:45:22 +0100 | [diff] [blame] | 3687 | static int adaptive_keyboard_get_mode(void) |
| 3688 | { |
| 3689 | int mode = 0; |
| 3690 | |
| 3691 | if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) { |
| 3692 | pr_err("Cannot read adaptive keyboard mode\n"); |
| 3693 | return -EIO; |
| 3694 | } |
| 3695 | |
| 3696 | return mode; |
| 3697 | } |
| 3698 | |
| 3699 | static int adaptive_keyboard_set_mode(int new_mode) |
| 3700 | { |
| 3701 | if (new_mode < 0 || |
| 3702 | new_mode > LAYFLAT_MODE) |
| 3703 | return -EINVAL; |
| 3704 | |
| 3705 | if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) { |
| 3706 | pr_err("Cannot set adaptive keyboard mode\n"); |
| 3707 | return -EIO; |
| 3708 | } |
| 3709 | |
| 3710 | return 0; |
| 3711 | } |
| 3712 | |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3713 | static int adaptive_keyboard_get_next_mode(int mode) |
| 3714 | { |
| 3715 | size_t i; |
| 3716 | size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1; |
| 3717 | |
| 3718 | for (i = 0; i <= max_mode; i++) { |
| 3719 | if (adaptive_keyboard_modes[i] == mode) |
| 3720 | break; |
| 3721 | } |
| 3722 | |
| 3723 | if (i >= max_mode) |
| 3724 | i = 0; |
| 3725 | else |
| 3726 | i++; |
| 3727 | |
| 3728 | return adaptive_keyboard_modes[i]; |
| 3729 | } |
| 3730 | |
| 3731 | static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode) |
| 3732 | { |
Dan Carpenter | abf9dc0 | 2015-03-11 12:34:50 +0300 | [diff] [blame] | 3733 | int current_mode = 0; |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3734 | int new_mode = 0; |
Bastien Nocera | 6a68d85 | 2015-03-02 14:45:31 +0100 | [diff] [blame] | 3735 | int keycode; |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3736 | |
| 3737 | switch (scancode) { |
| 3738 | case DFR_CHANGE_ROW: |
| 3739 | if (adaptive_keyboard_mode_is_saved) { |
| 3740 | new_mode = adaptive_keyboard_prev_mode; |
| 3741 | adaptive_keyboard_mode_is_saved = false; |
| 3742 | } else { |
Bastien Nocera | f74587f | 2015-03-02 14:45:22 +0100 | [diff] [blame] | 3743 | current_mode = adaptive_keyboard_get_mode(); |
| 3744 | if (current_mode < 0) |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3745 | return false; |
Bastien Nocera | f74587f | 2015-03-02 14:45:22 +0100 | [diff] [blame] | 3746 | new_mode = adaptive_keyboard_get_next_mode( |
| 3747 | current_mode); |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3748 | } |
| 3749 | |
Bastien Nocera | f74587f | 2015-03-02 14:45:22 +0100 | [diff] [blame] | 3750 | if (adaptive_keyboard_set_mode(new_mode) < 0) |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3751 | return false; |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3752 | |
| 3753 | return true; |
| 3754 | |
| 3755 | case DFR_SHOW_QUICKVIEW_ROW: |
Bastien Nocera | f74587f | 2015-03-02 14:45:22 +0100 | [diff] [blame] | 3756 | current_mode = adaptive_keyboard_get_mode(); |
| 3757 | if (current_mode < 0) |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3758 | return false; |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3759 | |
Bastien Nocera | f74587f | 2015-03-02 14:45:22 +0100 | [diff] [blame] | 3760 | adaptive_keyboard_prev_mode = current_mode; |
| 3761 | adaptive_keyboard_mode_is_saved = true; |
| 3762 | |
| 3763 | if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0) |
| 3764 | return false; |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3765 | return true; |
| 3766 | |
| 3767 | default: |
Dan Carpenter | 741d98c | 2015-03-11 12:36:07 +0300 | [diff] [blame] | 3768 | if (scancode < FIRST_ADAPTIVE_KEY || |
Christian Kellner | 696c6523 | 2017-02-28 17:10:57 +0100 | [diff] [blame] | 3769 | scancode >= FIRST_ADAPTIVE_KEY + |
| 3770 | TP_ACPI_HOTKEYSCAN_EXTENDED_START - |
| 3771 | TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) { |
Bastien Nocera | 6a68d85 | 2015-03-02 14:45:31 +0100 | [diff] [blame] | 3772 | pr_info("Unhandled adaptive keyboard key: 0x%x\n", |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 3773 | scancode); |
Bastien Nocera | 6a68d85 | 2015-03-02 14:45:31 +0100 | [diff] [blame] | 3774 | return false; |
| 3775 | } |
Christian Kellner | 149c8c7 | 2017-02-28 17:10:56 +0100 | [diff] [blame] | 3776 | keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY + |
| 3777 | TP_ACPI_HOTKEYSCAN_ADAPTIVE_START]; |
Bastien Nocera | 6a68d85 | 2015-03-02 14:45:31 +0100 | [diff] [blame] | 3778 | if (keycode != KEY_RESERVED) { |
| 3779 | mutex_lock(&tpacpi_inputdev_send_mutex); |
| 3780 | |
| 3781 | input_report_key(tpacpi_inputdev, keycode, 1); |
| 3782 | input_sync(tpacpi_inputdev); |
| 3783 | |
| 3784 | input_report_key(tpacpi_inputdev, keycode, 0); |
| 3785 | input_sync(tpacpi_inputdev); |
| 3786 | |
| 3787 | mutex_unlock(&tpacpi_inputdev_send_mutex); |
| 3788 | } |
| 3789 | return true; |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3790 | } |
| 3791 | } |
| 3792 | |
Hans de Goede | 1b8101d | 2021-10-05 22:23:18 +0200 | [diff] [blame] | 3793 | static bool hotkey_notify_extended_hotkey(const u32 hkey) |
| 3794 | { |
| 3795 | unsigned int scancode; |
| 3796 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 3797 | switch (hkey) { |
| 3798 | case TP_HKEY_EV_PRIVACYGUARD_TOGGLE: |
| 3799 | tpacpi_driver_event(hkey); |
| 3800 | return true; |
| 3801 | } |
| 3802 | |
Hans de Goede | 1b8101d | 2021-10-05 22:23:18 +0200 | [diff] [blame] | 3803 | /* Extended keycodes start at 0x300 and our offset into the map |
| 3804 | * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode |
| 3805 | * will be positive, but might not be in the correct range. |
| 3806 | */ |
| 3807 | scancode = (hkey & 0xfff) - (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START); |
| 3808 | if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START && |
| 3809 | scancode < TPACPI_HOTKEY_MAP_LEN) { |
| 3810 | tpacpi_input_send_key(scancode); |
| 3811 | return true; |
| 3812 | } |
| 3813 | |
| 3814 | return false; |
| 3815 | } |
| 3816 | |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3817 | static bool hotkey_notify_hotkey(const u32 hkey, |
| 3818 | bool *send_acpi_ev, |
| 3819 | bool *ignore_acpi_ev) |
| 3820 | { |
| 3821 | /* 0x1000-0x1FFF: key presses */ |
| 3822 | unsigned int scancode = hkey & 0xfff; |
| 3823 | *send_acpi_ev = true; |
| 3824 | *ignore_acpi_ev = false; |
| 3825 | |
Christian Kellner | 696c6523 | 2017-02-28 17:10:57 +0100 | [diff] [blame] | 3826 | /* |
| 3827 | * Original events are in the 0x10XX range, the adaptive keyboard |
| 3828 | * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017 |
| 3829 | * models, additional keys are emitted through 0x13XX. |
| 3830 | */ |
| 3831 | switch ((hkey >> 8) & 0xf) { |
| 3832 | case 0: |
| 3833 | if (scancode > 0 && |
| 3834 | scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) { |
| 3835 | /* HKEY event 0x1001 is scancode 0x00 */ |
| 3836 | scancode--; |
| 3837 | if (!(hotkey_source_mask & (1 << scancode))) { |
| 3838 | tpacpi_input_send_key_masked(scancode); |
| 3839 | *send_acpi_ev = false; |
| 3840 | } else { |
| 3841 | *ignore_acpi_ev = true; |
| 3842 | } |
| 3843 | return true; |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3844 | } |
Christian Kellner | 696c6523 | 2017-02-28 17:10:57 +0100 | [diff] [blame] | 3845 | break; |
| 3846 | |
| 3847 | case 1: |
Shuduo Sang | 3a9d20b | 2014-03-06 18:20:46 +0800 | [diff] [blame] | 3848 | return adaptive_keyboard_hotkey_notify_hotkey(scancode); |
Christian Kellner | 696c6523 | 2017-02-28 17:10:57 +0100 | [diff] [blame] | 3849 | |
| 3850 | case 3: |
Hans de Goede | 1b8101d | 2021-10-05 22:23:18 +0200 | [diff] [blame] | 3851 | return hotkey_notify_extended_hotkey(hkey); |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3852 | } |
Christian Kellner | 696c6523 | 2017-02-28 17:10:57 +0100 | [diff] [blame] | 3853 | |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3854 | return false; |
| 3855 | } |
| 3856 | |
| 3857 | static bool hotkey_notify_wakeup(const u32 hkey, |
| 3858 | bool *send_acpi_ev, |
| 3859 | bool *ignore_acpi_ev) |
| 3860 | { |
| 3861 | /* 0x2000-0x2FFF: Wakeup reason */ |
| 3862 | *send_acpi_ev = true; |
| 3863 | *ignore_acpi_ev = false; |
| 3864 | |
| 3865 | switch (hkey) { |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 3866 | case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */ |
| 3867 | case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */ |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3868 | hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK; |
| 3869 | *ignore_acpi_ev = true; |
| 3870 | break; |
| 3871 | |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 3872 | case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */ |
| 3873 | case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */ |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3874 | hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ; |
| 3875 | *ignore_acpi_ev = true; |
| 3876 | break; |
| 3877 | |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 3878 | case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */ |
| 3879 | case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */ |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 3880 | pr_alert("EMERGENCY WAKEUP: battery almost empty\n"); |
Henrique de Moraes Holschuh | 106b4e6 | 2009-01-11 03:01:06 -0200 | [diff] [blame] | 3881 | /* how to auto-heal: */ |
| 3882 | /* 2313: woke up from S3, go to S4/S5 */ |
| 3883 | /* 2413: woke up from S4, go to S5 */ |
| 3884 | break; |
| 3885 | |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3886 | default: |
| 3887 | return false; |
| 3888 | } |
| 3889 | |
| 3890 | if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 3891 | pr_info("woke up due to a hot-unplug request...\n"); |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3892 | hotkey_wakeup_reason_notify_change(); |
| 3893 | } |
| 3894 | return true; |
| 3895 | } |
| 3896 | |
Henrique de Moraes Holschuh | a50245a | 2011-06-05 16:22:35 -0300 | [diff] [blame] | 3897 | static bool hotkey_notify_dockevent(const u32 hkey, |
| 3898 | bool *send_acpi_ev, |
| 3899 | bool *ignore_acpi_ev) |
| 3900 | { |
| 3901 | /* 0x4000-0x4FFF: dock-related events */ |
| 3902 | *send_acpi_ev = true; |
| 3903 | *ignore_acpi_ev = false; |
| 3904 | |
| 3905 | switch (hkey) { |
| 3906 | case TP_HKEY_EV_UNDOCK_ACK: |
| 3907 | /* ACPI undock operation completed after wakeup */ |
| 3908 | hotkey_autosleep_ack = 1; |
| 3909 | pr_info("undocked\n"); |
| 3910 | hotkey_wakeup_hotunplug_complete_notify_change(); |
| 3911 | return true; |
| 3912 | |
| 3913 | case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */ |
| 3914 | pr_info("docked into hotplug port replicator\n"); |
| 3915 | return true; |
| 3916 | case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */ |
| 3917 | pr_info("undocked from hotplug port replicator\n"); |
| 3918 | return true; |
| 3919 | |
Alexander Kobel | 0a053f0 | 2021-02-13 16:13:36 +0100 | [diff] [blame] | 3920 | /* |
| 3921 | * Deliberately ignore attaching and detaching the keybord cover to avoid |
| 3922 | * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events |
| 3923 | * to userspace. |
| 3924 | * |
| 3925 | * Please refer to the following thread for more information and a preliminary |
| 3926 | * implementation using the GTOP ("Get Tablet OPtions") interface that could be |
| 3927 | * extended to other attachment options of the ThinkPad X1 Tablet series, such as |
| 3928 | * the Pico cartridge dock module: |
| 3929 | * https://lore.kernel.org/platform-driver-x86/38cb8265-1e30-d547-9e12-b4ae290be737@a-kobel.de/ |
| 3930 | */ |
| 3931 | case TP_HKEY_EV_KBD_COVER_ATTACH: |
| 3932 | case TP_HKEY_EV_KBD_COVER_DETACH: |
| 3933 | *send_acpi_ev = false; |
| 3934 | *ignore_acpi_ev = true; |
| 3935 | return true; |
| 3936 | |
Henrique de Moraes Holschuh | a50245a | 2011-06-05 16:22:35 -0300 | [diff] [blame] | 3937 | default: |
| 3938 | return false; |
| 3939 | } |
| 3940 | } |
| 3941 | |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3942 | static bool hotkey_notify_usrevent(const u32 hkey, |
| 3943 | bool *send_acpi_ev, |
| 3944 | bool *ignore_acpi_ev) |
| 3945 | { |
| 3946 | /* 0x5000-0x5FFF: human interface helpers */ |
| 3947 | *send_acpi_ev = true; |
| 3948 | *ignore_acpi_ev = false; |
| 3949 | |
| 3950 | switch (hkey) { |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 3951 | case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */ |
| 3952 | case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */ |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3953 | return true; |
| 3954 | |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 3955 | case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */ |
| 3956 | case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */ |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3957 | tpacpi_input_send_tabletsw(); |
| 3958 | hotkey_tablet_mode_notify_change(); |
| 3959 | *send_acpi_ev = false; |
| 3960 | return true; |
| 3961 | |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 3962 | case TP_HKEY_EV_LID_CLOSE: /* Lid closed */ |
| 3963 | case TP_HKEY_EV_LID_OPEN: /* Lid opened */ |
| 3964 | case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */ |
Henrique de Moraes Holschuh | 176dd98 | 2009-09-20 14:09:24 -0300 | [diff] [blame] | 3965 | /* do not propagate these events */ |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 3966 | *ignore_acpi_ev = true; |
| 3967 | return true; |
| 3968 | |
| 3969 | default: |
| 3970 | return false; |
| 3971 | } |
| 3972 | } |
| 3973 | |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 3974 | static void thermal_dump_all_sensors(void); |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 3975 | static void palmsensor_refresh(void); |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 3976 | |
Henrique de Moraes Holschuh | 2d43f67 | 2011-06-05 16:22:34 -0300 | [diff] [blame] | 3977 | static bool hotkey_notify_6xxx(const u32 hkey, |
Henrique de Moraes Holschuh | 106b4e6 | 2009-01-11 03:01:06 -0200 | [diff] [blame] | 3978 | bool *send_acpi_ev, |
| 3979 | bool *ignore_acpi_ev) |
| 3980 | { |
Henrique de Moraes Holschuh | 2d43f67 | 2011-06-05 16:22:34 -0300 | [diff] [blame] | 3981 | /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */ |
Henrique de Moraes Holschuh | 106b4e6 | 2009-01-11 03:01:06 -0200 | [diff] [blame] | 3982 | *send_acpi_ev = true; |
| 3983 | *ignore_acpi_ev = false; |
| 3984 | |
| 3985 | switch (hkey) { |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 3986 | case TP_HKEY_EV_THM_TABLE_CHANGED: |
Henrique de Moraes Holschuh | 6e6bc5f6 | 2018-04-24 16:56:03 -0300 | [diff] [blame] | 3987 | pr_debug("EC reports: Thermal Table has changed\n"); |
| 3988 | /* recommended action: do nothing, we don't have |
| 3989 | * Lenovo ATM information */ |
| 3990 | return true; |
| 3991 | case TP_HKEY_EV_THM_CSM_COMPLETED: |
| 3992 | pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n"); |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 3993 | /* Thermal event - pass on to event handler */ |
| 3994 | tpacpi_driver_event(hkey); |
Henrique de Moraes Holschuh | 6e6bc5f6 | 2018-04-24 16:56:03 -0300 | [diff] [blame] | 3995 | return true; |
| 3996 | case TP_HKEY_EV_THM_TRANSFM_CHANGED: |
| 3997 | pr_debug("EC reports: Thermal Transformation changed (GMTS)\n"); |
Henrique de Moraes Holschuh | 54926ce | 2009-01-11 03:01:09 -0200 | [diff] [blame] | 3998 | /* recommended action: do nothing, we don't have |
| 3999 | * Lenovo ATM information */ |
| 4000 | return true; |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 4001 | case TP_HKEY_EV_ALARM_BAT_HOT: |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4002 | pr_crit("THERMAL ALARM: battery is too hot!\n"); |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 4003 | /* recommended action: warn user through gui */ |
| 4004 | break; |
| 4005 | case TP_HKEY_EV_ALARM_BAT_XHOT: |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4006 | pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n"); |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 4007 | /* recommended action: immediate sleep/hibernate */ |
| 4008 | break; |
| 4009 | case TP_HKEY_EV_ALARM_SENSOR_HOT: |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 4010 | pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n"); |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 4011 | /* recommended action: warn user through gui, that */ |
| 4012 | /* some internal component is too hot */ |
| 4013 | break; |
| 4014 | case TP_HKEY_EV_ALARM_SENSOR_XHOT: |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 4015 | pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n"); |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 4016 | /* recommended action: immediate sleep/hibernate */ |
| 4017 | break; |
Richard Hartmann | 6f62bc3 | 2012-12-29 22:51:49 +0100 | [diff] [blame] | 4018 | case TP_HKEY_EV_AC_CHANGED: |
| 4019 | /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520: |
| 4020 | * AC status changed; can be triggered by plugging or |
| 4021 | * unplugging AC adapter, docking or undocking. */ |
| 4022 | |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4023 | fallthrough; |
Henrique de Moraes Holschuh | 2d43f67 | 2011-06-05 16:22:34 -0300 | [diff] [blame] | 4024 | |
| 4025 | case TP_HKEY_EV_KEY_NUMLOCK: |
| 4026 | case TP_HKEY_EV_KEY_FN: |
| 4027 | /* key press events, we just ignore them as long as the EC |
| 4028 | * is still reporting them in the normal keyboard stream */ |
| 4029 | *send_acpi_ev = false; |
| 4030 | *ignore_acpi_ev = true; |
| 4031 | return true; |
| 4032 | |
Esteve Varela Colominas | 12dde4d | 2021-03-15 20:58:24 +0100 | [diff] [blame] | 4033 | case TP_HKEY_EV_KEY_FN_ESC: |
Esteve Varela Colominas | 49702ba | 2021-03-21 19:35:13 +0100 | [diff] [blame] | 4034 | /* Get the media key status to force the status LED to update */ |
Esteve Varela Colominas | 12dde4d | 2021-03-15 20:58:24 +0100 | [diff] [blame] | 4035 | acpi_evalf(hkey_handle, NULL, "GMKS", "v"); |
| 4036 | *send_acpi_ev = false; |
| 4037 | *ignore_acpi_ev = true; |
| 4038 | return true; |
| 4039 | |
Lyude | b03f4d4 | 2016-11-11 15:15:03 -0500 | [diff] [blame] | 4040 | case TP_HKEY_EV_TABLET_CHANGED: |
| 4041 | tpacpi_input_send_tabletsw(); |
| 4042 | hotkey_tablet_mode_notify_change(); |
| 4043 | *send_acpi_ev = false; |
Henrique de Moraes Holschuh | edd1ed7 | 2018-04-24 16:56:04 -0300 | [diff] [blame] | 4044 | return true; |
Lyude | b03f4d4 | 2016-11-11 15:15:03 -0500 | [diff] [blame] | 4045 | |
David Herrmann | 587d862 | 2018-01-12 12:04:45 +0100 | [diff] [blame] | 4046 | case TP_HKEY_EV_PALM_DETECTED: |
| 4047 | case TP_HKEY_EV_PALM_UNDETECTED: |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 4048 | /* palm detected - pass on to event handler */ |
| 4049 | palmsensor_refresh(); |
David Herrmann | 587d862 | 2018-01-12 12:04:45 +0100 | [diff] [blame] | 4050 | return true; |
| 4051 | |
Henrique de Moraes Holschuh | 106b4e6 | 2009-01-11 03:01:06 -0200 | [diff] [blame] | 4052 | default: |
Henrique de Moraes Holschuh | fd75ba2 | 2018-04-24 16:56:05 -0300 | [diff] [blame] | 4053 | /* report simply as unknown, no sensor dump */ |
| 4054 | return false; |
Henrique de Moraes Holschuh | 106b4e6 | 2009-01-11 03:01:06 -0200 | [diff] [blame] | 4055 | } |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 4056 | |
| 4057 | thermal_dump_all_sensors(); |
Henrique de Moraes Holschuh | fd75ba2 | 2018-04-24 16:56:05 -0300 | [diff] [blame] | 4058 | return true; |
Henrique de Moraes Holschuh | 106b4e6 | 2009-01-11 03:01:06 -0200 | [diff] [blame] | 4059 | } |
| 4060 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 4061 | static void hotkey_notify(struct ibm_struct *ibm, u32 event) |
| 4062 | { |
Henrique de Moraes Holschuh | 6a38abb | 2007-07-18 23:45:35 -0300 | [diff] [blame] | 4063 | u32 hkey; |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4064 | bool send_acpi_ev; |
| 4065 | bool ignore_acpi_ev; |
| 4066 | bool known_ev; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 4067 | |
Henrique de Moraes Holschuh | 3eea123 | 2007-09-23 11:39:04 -0300 | [diff] [blame] | 4068 | if (event != 0x80) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4069 | pr_err("unknown HKEY notification event %d\n", event); |
Henrique de Moraes Holschuh | 3eea123 | 2007-09-23 11:39:04 -0300 | [diff] [blame] | 4070 | /* forward it to userspace, maybe it knows how to handle it */ |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 4071 | acpi_bus_generate_netlink_event( |
| 4072 | ibm->acpi->device->pnp.device_class, |
Kay Sievers | e0b36fc | 2009-01-11 03:00:59 -0200 | [diff] [blame] | 4073 | dev_name(&ibm->acpi->device->dev), |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 4074 | event, 0); |
Henrique de Moraes Holschuh | 3eea123 | 2007-09-23 11:39:04 -0300 | [diff] [blame] | 4075 | return; |
| 4076 | } |
| 4077 | |
| 4078 | while (1) { |
| 4079 | if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4080 | pr_err("failed to retrieve HKEY event\n"); |
Henrique de Moraes Holschuh | 3eea123 | 2007-09-23 11:39:04 -0300 | [diff] [blame] | 4081 | return; |
| 4082 | } |
| 4083 | |
| 4084 | if (hkey == 0) { |
| 4085 | /* queue empty */ |
| 4086 | return; |
| 4087 | } |
| 4088 | |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4089 | send_acpi_ev = true; |
| 4090 | ignore_acpi_ev = false; |
Henrique de Moraes Holschuh | 3eea123 | 2007-09-23 11:39:04 -0300 | [diff] [blame] | 4091 | |
Henrique de Moraes Holschuh | ff80f13 | 2007-09-04 11:13:15 -0300 | [diff] [blame] | 4092 | switch (hkey >> 12) { |
| 4093 | case 1: |
| 4094 | /* 0x1000-0x1FFF: key presses */ |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4095 | known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev, |
| 4096 | &ignore_acpi_ev); |
Henrique de Moraes Holschuh | ff80f13 | 2007-09-04 11:13:15 -0300 | [diff] [blame] | 4097 | break; |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 4098 | case 2: |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4099 | /* 0x2000-0x2FFF: Wakeup reason */ |
| 4100 | known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev, |
| 4101 | &ignore_acpi_ev); |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 4102 | break; |
| 4103 | case 3: |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4104 | /* 0x3000-0x3FFF: bay-related wakeups */ |
Henrique de Moraes Holschuh | bf8b29c | 2010-02-25 21:28:56 -0300 | [diff] [blame] | 4105 | switch (hkey) { |
| 4106 | case TP_HKEY_EV_BAYEJ_ACK: |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 4107 | hotkey_autosleep_ack = 1; |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4108 | pr_info("bay ejected\n"); |
Henrique de Moraes Holschuh | 50ebec0 | 2008-01-08 13:02:55 -0200 | [diff] [blame] | 4109 | hotkey_wakeup_hotunplug_complete_notify_change(); |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4110 | known_ev = true; |
Henrique de Moraes Holschuh | bf8b29c | 2010-02-25 21:28:56 -0300 | [diff] [blame] | 4111 | break; |
| 4112 | case TP_HKEY_EV_OPTDRV_EJ: |
| 4113 | /* FIXME: kick libata if SATA link offline */ |
| 4114 | known_ev = true; |
| 4115 | break; |
| 4116 | default: |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4117 | known_ev = false; |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 4118 | } |
| 4119 | break; |
| 4120 | case 4: |
Henrique de Moraes Holschuh | a50245a | 2011-06-05 16:22:35 -0300 | [diff] [blame] | 4121 | /* 0x4000-0x4FFF: dock-related events */ |
| 4122 | known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev, |
| 4123 | &ignore_acpi_ev); |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 4124 | break; |
Henrique de Moraes Holschuh | ff80f13 | 2007-09-04 11:13:15 -0300 | [diff] [blame] | 4125 | case 5: |
Henrique de Moraes Holschuh | d1edb2b | 2008-01-08 13:02:53 -0200 | [diff] [blame] | 4126 | /* 0x5000-0x5FFF: human interface helpers */ |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4127 | known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev, |
| 4128 | &ignore_acpi_ev); |
Henrique de Moraes Holschuh | ff80f13 | 2007-09-04 11:13:15 -0300 | [diff] [blame] | 4129 | break; |
Henrique de Moraes Holschuh | 106b4e6 | 2009-01-11 03:01:06 -0200 | [diff] [blame] | 4130 | case 6: |
Henrique de Moraes Holschuh | 2d43f67 | 2011-06-05 16:22:34 -0300 | [diff] [blame] | 4131 | /* 0x6000-0x6FFF: thermal alarms/notices and |
| 4132 | * keyboard events */ |
| 4133 | known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev, |
Henrique de Moraes Holschuh | 106b4e6 | 2009-01-11 03:01:06 -0200 | [diff] [blame] | 4134 | &ignore_acpi_ev); |
| 4135 | break; |
Henrique de Moraes Holschuh | ff80f13 | 2007-09-04 11:13:15 -0300 | [diff] [blame] | 4136 | case 7: |
| 4137 | /* 0x7000-0x7FFF: misc */ |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 4138 | if (tp_features.hotkey_wlsw && |
| 4139 | hkey == TP_HKEY_EV_RFKILL_CHANGED) { |
Henrique de Moraes Holschuh | 733e27c | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 4140 | tpacpi_send_radiosw_update(); |
Henrique de Moraes Holschuh | 3b64b51 | 2008-01-08 13:02:51 -0200 | [diff] [blame] | 4141 | send_acpi_ev = 0; |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4142 | known_ev = true; |
Henrique de Moraes Holschuh | ff80f13 | 2007-09-04 11:13:15 -0300 | [diff] [blame] | 4143 | break; |
| 4144 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4145 | fallthrough; /* to default */ |
Henrique de Moraes Holschuh | ff80f13 | 2007-09-04 11:13:15 -0300 | [diff] [blame] | 4146 | default: |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4147 | known_ev = false; |
Henrique de Moraes Holschuh | 3b64b51 | 2008-01-08 13:02:51 -0200 | [diff] [blame] | 4148 | } |
Henrique de Moraes Holschuh | 3827e7a | 2009-01-11 03:01:05 -0200 | [diff] [blame] | 4149 | if (!known_ev) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4150 | pr_notice("unhandled HKEY event 0x%04x\n", hkey); |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 4151 | pr_notice("please report the conditions when this event happened to %s\n", |
| 4152 | TPACPI_MAIL); |
Henrique de Moraes Holschuh | 6a38abb | 2007-07-18 23:45:35 -0300 | [diff] [blame] | 4153 | } |
Henrique de Moraes Holschuh | ff80f13 | 2007-09-04 11:13:15 -0300 | [diff] [blame] | 4154 | |
Henrique de Moraes Holschuh | 3eea123 | 2007-09-23 11:39:04 -0300 | [diff] [blame] | 4155 | /* netlink events */ |
Henrique de Moraes Holschuh | 3e5ce91 | 2007-09-23 11:39:05 -0300 | [diff] [blame] | 4156 | if (!ignore_acpi_ev && send_acpi_ev) { |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 4157 | acpi_bus_generate_netlink_event( |
| 4158 | ibm->acpi->device->pnp.device_class, |
Kay Sievers | e0b36fc | 2009-01-11 03:00:59 -0200 | [diff] [blame] | 4159 | dev_name(&ibm->acpi->device->dev), |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 4160 | event, hkey); |
Henrique de Moraes Holschuh | 3eea123 | 2007-09-23 11:39:04 -0300 | [diff] [blame] | 4161 | } |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 4162 | } |
| 4163 | } |
| 4164 | |
Rafael J. Wysocki | fd3c3a4 | 2012-06-27 23:18:44 +0200 | [diff] [blame] | 4165 | static void hotkey_suspend(void) |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 4166 | { |
| 4167 | /* Do these on suspend, we get the events on early resume! */ |
| 4168 | hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE; |
| 4169 | hotkey_autosleep_ack = 0; |
Shuduo Sang | 330947b | 2014-03-27 18:06:25 +0800 | [diff] [blame] | 4170 | |
| 4171 | /* save previous mode of adaptive keyboard of X1 Carbon */ |
Bastien Nocera | f23a5bc | 2015-03-02 14:45:16 +0100 | [diff] [blame] | 4172 | if (tp_features.has_adaptive_kbd) { |
| 4173 | if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode, |
| 4174 | "GTRW", "dd", 0)) { |
| 4175 | pr_err("Cannot read adaptive keyboard mode.\n"); |
Shuduo Sang | 330947b | 2014-03-27 18:06:25 +0800 | [diff] [blame] | 4176 | } |
| 4177 | } |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 4178 | } |
| 4179 | |
Henrique de Moraes Holschuh | 5c29d58 | 2007-07-18 23:45:38 -0300 | [diff] [blame] | 4180 | static void hotkey_resume(void) |
| 4181 | { |
Henrique de Moraes Holschuh | d64c81c4 | 2008-10-18 14:23:55 -0300 | [diff] [blame] | 4182 | tpacpi_disable_brightness_delay(); |
| 4183 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 4184 | if (hotkey_status_set(true) < 0 || |
| 4185 | hotkey_mask_set(hotkey_acpi_mask) < 0) |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 4186 | pr_err("error while attempting to reset the event firmware interface\n"); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 4187 | |
Henrique de Moraes Holschuh | 733e27c | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 4188 | tpacpi_send_radiosw_update(); |
Benjamin Berg | 96adb41 | 2020-11-23 14:21:57 +0100 | [diff] [blame] | 4189 | tpacpi_input_send_tabletsw(); |
Henrique de Moraes Holschuh | 6c231bd | 2008-02-16 02:17:58 -0200 | [diff] [blame] | 4190 | hotkey_tablet_mode_notify_change(); |
Henrique de Moraes Holschuh | 50ebec0 | 2008-01-08 13:02:55 -0200 | [diff] [blame] | 4191 | hotkey_wakeup_reason_notify_change(); |
| 4192 | hotkey_wakeup_hotunplug_complete_notify_change(); |
Henrique de Moraes Holschuh | db25f16 | 2009-09-12 15:22:14 -0300 | [diff] [blame] | 4193 | hotkey_poll_setup_safe(false); |
Shuduo Sang | 330947b | 2014-03-27 18:06:25 +0800 | [diff] [blame] | 4194 | |
| 4195 | /* restore previous mode of adapive keyboard of X1 Carbon */ |
Bastien Nocera | f23a5bc | 2015-03-02 14:45:16 +0100 | [diff] [blame] | 4196 | if (tp_features.has_adaptive_kbd) { |
| 4197 | if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", |
| 4198 | adaptive_keyboard_prev_mode)) { |
| 4199 | pr_err("Cannot set adaptive keyboard mode.\n"); |
Shuduo Sang | 330947b | 2014-03-27 18:06:25 +0800 | [diff] [blame] | 4200 | } |
| 4201 | } |
Henrique de Moraes Holschuh | 5c29d58 | 2007-07-18 23:45:38 -0300 | [diff] [blame] | 4202 | } |
| 4203 | |
Henrique de Moraes Holschuh | a041642 | 2007-04-27 22:00:16 -0300 | [diff] [blame] | 4204 | /* procfs -------------------------------------------------------------- */ |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 4205 | static int hotkey_read(struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | { |
Henrique de Moraes Holschuh | ae92bd1 | 2007-07-18 23:45:29 -0300 | [diff] [blame] | 4207 | int res, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 4209 | if (!tp_features.hotkey) { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 4210 | seq_printf(m, "status:\t\tnot supported\n"); |
| 4211 | return 0; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4212 | } |
| 4213 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 4214 | if (mutex_lock_killable(&hotkey_mutex)) |
Henrique de Moraes Holschuh | fc589a3 | 2007-10-30 17:46:24 -0200 | [diff] [blame] | 4215 | return -ERESTARTSYS; |
Henrique de Moraes Holschuh | b2c985e | 2008-01-08 13:02:39 -0200 | [diff] [blame] | 4216 | res = hotkey_status_get(&status); |
| 4217 | if (!res) |
| 4218 | res = hotkey_mask_get(); |
Henrique de Moraes Holschuh | 40ca9fd | 2007-04-24 11:48:15 -0300 | [diff] [blame] | 4219 | mutex_unlock(&hotkey_mutex); |
Henrique de Moraes Holschuh | b86c472 | 2007-04-21 11:08:39 -0300 | [diff] [blame] | 4220 | if (res) |
| 4221 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4222 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 4223 | seq_printf(m, "status:\t\t%s\n", enabled(status, 0)); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 4224 | if (hotkey_all_mask) { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 4225 | seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask); |
| 4226 | seq_printf(m, "commands:\tenable, disable, reset, <mask>\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | } else { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 4228 | seq_printf(m, "mask:\t\tnot supported\n"); |
| 4229 | seq_printf(m, "commands:\tenable, disable, reset\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | } |
| 4231 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 4232 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | } |
| 4234 | |
Henrique de Moraes Holschuh | 406e988 | 2009-04-14 02:44:10 +0000 | [diff] [blame] | 4235 | static void hotkey_enabledisable_warn(bool enable) |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 4236 | { |
| 4237 | tpacpi_log_usertask("procfs hotkey enable/disable"); |
Henrique de Moraes Holschuh | 406e988 | 2009-04-14 02:44:10 +0000 | [diff] [blame] | 4238 | if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable), |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 4239 | pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n"))) |
| 4240 | pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n"); |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4243 | static int hotkey_write(char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | { |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 4245 | int res; |
Henrique de Moraes Holschuh | ae92bd1 | 2007-07-18 23:45:29 -0300 | [diff] [blame] | 4246 | u32 mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | char *cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 4249 | if (!tp_features.hotkey) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | return -ENODEV; |
| 4251 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 4252 | if (mutex_lock_killable(&hotkey_mutex)) |
Henrique de Moraes Holschuh | fc589a3 | 2007-10-30 17:46:24 -0200 | [diff] [blame] | 4253 | return -ERESTARTSYS; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4254 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 4255 | mask = hotkey_user_mask; |
Henrique de Moraes Holschuh | 40ca9fd | 2007-04-24 11:48:15 -0300 | [diff] [blame] | 4256 | |
| 4257 | res = 0; |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 4258 | while ((cmd = strsep(&buf, ","))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | if (strlencmp(cmd, "enable") == 0) { |
Henrique de Moraes Holschuh | 406e988 | 2009-04-14 02:44:10 +0000 | [diff] [blame] | 4260 | hotkey_enabledisable_warn(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | } else if (strlencmp(cmd, "disable") == 0) { |
Henrique de Moraes Holschuh | 406e988 | 2009-04-14 02:44:10 +0000 | [diff] [blame] | 4262 | hotkey_enabledisable_warn(0); |
Henrique de Moraes Holschuh | 2586d56 | 2009-04-04 04:25:48 +0000 | [diff] [blame] | 4263 | res = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | } else if (strlencmp(cmd, "reset") == 0) { |
Henrique de Moraes Holschuh | 20c9aa4 | 2009-09-12 15:22:16 -0300 | [diff] [blame] | 4265 | mask = (hotkey_all_mask | hotkey_source_mask) |
| 4266 | & ~hotkey_reserved_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | } else if (sscanf(cmd, "0x%x", &mask) == 1) { |
| 4268 | /* mask set */ |
| 4269 | } else if (sscanf(cmd, "%x", &mask) == 1) { |
| 4270 | /* mask set */ |
Henrique de Moraes Holschuh | 40ca9fd | 2007-04-24 11:48:15 -0300 | [diff] [blame] | 4271 | } else { |
| 4272 | res = -EINVAL; |
| 4273 | goto errexit; |
| 4274 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | } |
Henrique de Moraes Holschuh | 56e2c20 | 2009-04-04 04:25:51 +0000 | [diff] [blame] | 4276 | |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 4277 | if (!res) { |
Henrique de Moraes Holschuh | 56e2c20 | 2009-04-04 04:25:51 +0000 | [diff] [blame] | 4278 | tpacpi_disclose_usertask("procfs hotkey", |
| 4279 | "set mask to 0x%08x\n", mask); |
Henrique de Moraes Holschuh | 0d922e3 | 2009-09-20 14:09:25 -0300 | [diff] [blame] | 4280 | res = hotkey_user_mask_set(mask); |
| 4281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | |
Henrique de Moraes Holschuh | 40ca9fd | 2007-04-24 11:48:15 -0300 | [diff] [blame] | 4283 | errexit: |
| 4284 | mutex_unlock(&hotkey_mutex); |
| 4285 | return res; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 4288 | static const struct acpi_device_id ibm_htk_device_ids[] = { |
Manoj Iyer | 9fbdaeb | 2011-05-08 18:04:29 -0400 | [diff] [blame] | 4289 | {TPACPI_ACPI_IBM_HKEY_HID, 0}, |
| 4290 | {TPACPI_ACPI_LENOVO_HKEY_HID, 0}, |
Hui Wang | a3c42a4 | 2016-11-08 16:13:23 +0800 | [diff] [blame] | 4291 | {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0}, |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 4292 | {"", 0}, |
| 4293 | }; |
| 4294 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 4295 | static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = { |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 4296 | .hid = ibm_htk_device_ids, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 4297 | .notify = hotkey_notify, |
| 4298 | .handle = &hkey_handle, |
| 4299 | .type = ACPI_DEVICE_NOTIFY, |
| 4300 | }; |
| 4301 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 4302 | static struct ibm_struct hotkey_driver_data = { |
| 4303 | .name = "hotkey", |
| 4304 | .read = hotkey_read, |
| 4305 | .write = hotkey_write, |
| 4306 | .exit = hotkey_exit, |
Henrique de Moraes Holschuh | 5c29d58 | 2007-07-18 23:45:38 -0300 | [diff] [blame] | 4307 | .resume = hotkey_resume, |
Henrique de Moraes Holschuh | a713b4d | 2008-01-08 13:02:52 -0200 | [diff] [blame] | 4308 | .suspend = hotkey_suspend, |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 4309 | .acpi = &ibm_hotkey_acpidriver, |
| 4310 | }; |
| 4311 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 4312 | /************************************************************************* |
| 4313 | * Bluetooth subdriver |
| 4314 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 4316 | enum { |
| 4317 | /* ACPI GBDC/SBDC bits */ |
| 4318 | TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */ |
| 4319 | TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */ |
Henrique de Moraes Holschuh | 153f822 | 2009-01-11 03:01:01 -0200 | [diff] [blame] | 4320 | TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume: |
Henrique de Moraes Holschuh | 08fedfc | 2010-02-25 22:22:07 -0300 | [diff] [blame] | 4321 | 0 = disable, 1 = enable */ |
Henrique de Moraes Holschuh | 153f822 | 2009-01-11 03:01:01 -0200 | [diff] [blame] | 4322 | }; |
| 4323 | |
| 4324 | enum { |
| 4325 | /* ACPI \BLTH commands */ |
| 4326 | TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */ |
| 4327 | TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */ |
| 4328 | TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */ |
| 4329 | TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */ |
| 4330 | TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */ |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 4331 | }; |
| 4332 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4333 | #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw" |
| 4334 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4335 | static int bluetooth_get_status(void) |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4336 | { |
| 4337 | int status; |
| 4338 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4339 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 4340 | if (dbg_bluetoothemul) |
| 4341 | return (tpacpi_bluetooth_emulstate) ? |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4342 | TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4343 | #endif |
| 4344 | |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4345 | if (!acpi_evalf(hkey_handle, &status, "GBDC", "d")) |
| 4346 | return -EIO; |
| 4347 | |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4348 | return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ? |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4349 | TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4350 | } |
| 4351 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4352 | static int bluetooth_set_status(enum tpacpi_rfkill_state state) |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4353 | { |
| 4354 | int status; |
| 4355 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4356 | vdbg_printk(TPACPI_DBG_RFKILL, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4357 | "will attempt to %s bluetooth\n", |
| 4358 | (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable"); |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4359 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4360 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 4361 | if (dbg_bluetoothemul) { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4362 | tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4363 | return 0; |
| 4364 | } |
| 4365 | #endif |
| 4366 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4367 | if (state == TPACPI_RFK_RADIO_ON) |
Henrique de Moraes Holschuh | 08fedfc | 2010-02-25 22:22:07 -0300 | [diff] [blame] | 4368 | status = TP_ACPI_BLUETOOTH_RADIOSSW |
| 4369 | | TP_ACPI_BLUETOOTH_RESUMECTRL; |
| 4370 | else |
| 4371 | status = 0; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4372 | |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4373 | if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status)) |
| 4374 | return -EIO; |
| 4375 | |
| 4376 | return 0; |
| 4377 | } |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 4378 | |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4379 | /* sysfs bluetooth enable ---------------------------------------------- */ |
| 4380 | static ssize_t bluetooth_enable_show(struct device *dev, |
| 4381 | struct device_attribute *attr, |
| 4382 | char *buf) |
| 4383 | { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4384 | return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID, |
| 4385 | attr, buf); |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4386 | } |
| 4387 | |
| 4388 | static ssize_t bluetooth_enable_store(struct device *dev, |
| 4389 | struct device_attribute *attr, |
| 4390 | const char *buf, size_t count) |
| 4391 | { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4392 | return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID, |
| 4393 | attr, buf, count); |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4394 | } |
| 4395 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 4396 | static DEVICE_ATTR_RW(bluetooth_enable); |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4397 | |
| 4398 | /* --------------------------------------------------------------------- */ |
| 4399 | |
| 4400 | static struct attribute *bluetooth_attributes[] = { |
| 4401 | &dev_attr_bluetooth_enable.attr, |
| 4402 | NULL |
| 4403 | }; |
| 4404 | |
| 4405 | static const struct attribute_group bluetooth_attr_group = { |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4406 | .attrs = bluetooth_attributes, |
| 4407 | }; |
| 4408 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4409 | static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = { |
| 4410 | .get_status = bluetooth_get_status, |
| 4411 | .set_status = bluetooth_set_status, |
| 4412 | }; |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4413 | |
Henrique de Moraes Holschuh | 90d9d3c | 2009-01-11 03:01:02 -0200 | [diff] [blame] | 4414 | static void bluetooth_shutdown(void) |
| 4415 | { |
| 4416 | /* Order firmware to save current state to NVRAM */ |
| 4417 | if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd", |
| 4418 | TP_ACPI_BLTH_SAVE_STATE)) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4419 | pr_notice("failed to save bluetooth state to NVRAM\n"); |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4420 | else |
| 4421 | vdbg_printk(TPACPI_DBG_RFKILL, |
Paul Bolle | 1f01358 | 2011-10-06 22:57:56 +0200 | [diff] [blame] | 4422 | "bluetooth state saved to NVRAM\n"); |
Henrique de Moraes Holschuh | 90d9d3c | 2009-01-11 03:01:02 -0200 | [diff] [blame] | 4423 | } |
| 4424 | |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4425 | static void bluetooth_exit(void) |
| 4426 | { |
| 4427 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, |
| 4428 | &bluetooth_attr_group); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4429 | |
| 4430 | tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID); |
| 4431 | |
| 4432 | bluetooth_shutdown(); |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4433 | } |
| 4434 | |
Jiaxun Yang | f7db839 | 2019-03-07 17:37:16 +0800 | [diff] [blame] | 4435 | static const struct dmi_system_id bt_fwbug_list[] __initconst = { |
| 4436 | { |
| 4437 | .ident = "ThinkPad E485", |
| 4438 | .matches = { |
| 4439 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), |
| 4440 | DMI_MATCH(DMI_BOARD_NAME, "20KU"), |
| 4441 | }, |
| 4442 | }, |
| 4443 | { |
| 4444 | .ident = "ThinkPad E585", |
| 4445 | .matches = { |
| 4446 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), |
| 4447 | DMI_MATCH(DMI_BOARD_NAME, "20KV"), |
| 4448 | }, |
| 4449 | }, |
| 4450 | { |
| 4451 | .ident = "ThinkPad A285 - 20MW", |
| 4452 | .matches = { |
| 4453 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), |
| 4454 | DMI_MATCH(DMI_BOARD_NAME, "20MW"), |
| 4455 | }, |
| 4456 | }, |
| 4457 | { |
| 4458 | .ident = "ThinkPad A285 - 20MX", |
| 4459 | .matches = { |
| 4460 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), |
| 4461 | DMI_MATCH(DMI_BOARD_NAME, "20MX"), |
| 4462 | }, |
| 4463 | }, |
| 4464 | { |
| 4465 | .ident = "ThinkPad A485 - 20MU", |
| 4466 | .matches = { |
| 4467 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), |
| 4468 | DMI_MATCH(DMI_BOARD_NAME, "20MU"), |
| 4469 | }, |
| 4470 | }, |
| 4471 | { |
| 4472 | .ident = "ThinkPad A485 - 20MV", |
| 4473 | .matches = { |
| 4474 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), |
| 4475 | DMI_MATCH(DMI_BOARD_NAME, "20MV"), |
| 4476 | }, |
| 4477 | }, |
| 4478 | {} |
| 4479 | }; |
| 4480 | |
| 4481 | static const struct pci_device_id fwbug_cards_ids[] __initconst = { |
| 4482 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) }, |
| 4483 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) }, |
| 4484 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) }, |
| 4485 | {} |
| 4486 | }; |
| 4487 | |
| 4488 | |
| 4489 | static int __init have_bt_fwbug(void) |
| 4490 | { |
| 4491 | /* |
| 4492 | * Some AMD based ThinkPads have a firmware bug that calling |
| 4493 | * "GBDC" will cause bluetooth on Intel wireless cards blocked |
| 4494 | */ |
| 4495 | if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) { |
| 4496 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, |
| 4497 | FW_BUG "disable bluetooth subdriver for Intel cards\n"); |
| 4498 | return 1; |
| 4499 | } else |
| 4500 | return 0; |
| 4501 | } |
| 4502 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 4503 | static int __init bluetooth_init(struct ibm_init_struct *iibm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | { |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4505 | int res; |
Henrique de Moraes Holschuh | d6fdd1e | 2007-04-21 11:08:40 -0300 | [diff] [blame] | 4506 | int status = 0; |
| 4507 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4508 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, |
| 4509 | "initializing bluetooth subdriver\n"); |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 4510 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 4511 | TPACPI_ACPIHANDLE_INIT(hkey); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 4512 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4513 | /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, |
| 4514 | G4x, R30, R31, R40e, R50e, T20-22, X20-21 */ |
Jiaxun Yang | f7db839 | 2019-03-07 17:37:16 +0800 | [diff] [blame] | 4515 | tp_features.bluetooth = !have_bt_fwbug() && hkey_handle && |
Henrique de Moraes Holschuh | d6fdd1e | 2007-04-21 11:08:40 -0300 | [diff] [blame] | 4516 | acpi_evalf(hkey_handle, &status, "GBDC", "qd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4518 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, |
| 4519 | "bluetooth is %s, status 0x%02x\n", |
Henrique de Moraes Holschuh | d6fdd1e | 2007-04-21 11:08:40 -0300 | [diff] [blame] | 4520 | str_supported(tp_features.bluetooth), |
| 4521 | status); |
| 4522 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4523 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 4524 | if (dbg_bluetoothemul) { |
| 4525 | tp_features.bluetooth = 1; |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4526 | pr_info("bluetooth switch emulation enabled\n"); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4527 | } else |
| 4528 | #endif |
Henrique de Moraes Holschuh | 3a87208 | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 4529 | if (tp_features.bluetooth && |
| 4530 | !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) { |
| 4531 | /* no bluetooth hardware present in system */ |
| 4532 | tp_features.bluetooth = 0; |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4533 | dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, |
Henrique de Moraes Holschuh | 3a87208 | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 4534 | "bluetooth hardware not installed\n"); |
| 4535 | } |
| 4536 | |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4537 | if (!tp_features.bluetooth) |
| 4538 | return 1; |
| 4539 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4540 | res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID, |
| 4541 | &bluetooth_tprfk_ops, |
| 4542 | RFKILL_TYPE_BLUETOOTH, |
| 4543 | TPACPI_RFK_BLUETOOTH_SW_NAME, |
| 4544 | true); |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4545 | if (res) |
| 4546 | return res; |
| 4547 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4548 | res = sysfs_create_group(&tpacpi_pdev->dev.kobj, |
| 4549 | &bluetooth_attr_group); |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4550 | if (res) { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4551 | tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID); |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4552 | return res; |
Henrique de Moraes Holschuh | d6fdd1e | 2007-04-21 11:08:40 -0300 | [diff] [blame] | 4553 | } |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 4554 | |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4555 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | } |
| 4557 | |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4558 | /* procfs -------------------------------------------------------------- */ |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 4559 | static int bluetooth_read(struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 4561 | return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | } |
| 4563 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4564 | static int bluetooth_write(char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4566 | return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | } |
| 4568 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 4569 | static struct ibm_struct bluetooth_driver_data = { |
| 4570 | .name = "bluetooth", |
| 4571 | .read = bluetooth_read, |
| 4572 | .write = bluetooth_write, |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4573 | .exit = bluetooth_exit, |
Henrique de Moraes Holschuh | 90d9d3c | 2009-01-11 03:01:02 -0200 | [diff] [blame] | 4574 | .shutdown = bluetooth_shutdown, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 4575 | }; |
| 4576 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 4577 | /************************************************************************* |
| 4578 | * Wan subdriver |
| 4579 | */ |
| 4580 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 4581 | enum { |
| 4582 | /* ACPI GWAN/SWAN bits */ |
| 4583 | TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */ |
| 4584 | TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */ |
Henrique de Moraes Holschuh | 153f822 | 2009-01-11 03:01:01 -0200 | [diff] [blame] | 4585 | TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume: |
Henrique de Moraes Holschuh | 08fedfc | 2010-02-25 22:22:07 -0300 | [diff] [blame] | 4586 | 0 = disable, 1 = enable */ |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 4587 | }; |
| 4588 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4589 | #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw" |
| 4590 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4591 | static int wan_get_status(void) |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4592 | { |
| 4593 | int status; |
| 4594 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4595 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 4596 | if (dbg_wwanemul) |
| 4597 | return (tpacpi_wwan_emulstate) ? |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4598 | TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4599 | #endif |
| 4600 | |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4601 | if (!acpi_evalf(hkey_handle, &status, "GWAN", "d")) |
| 4602 | return -EIO; |
| 4603 | |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4604 | return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ? |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4605 | TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4606 | } |
| 4607 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4608 | static int wan_set_status(enum tpacpi_rfkill_state state) |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4609 | { |
| 4610 | int status; |
| 4611 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4612 | vdbg_printk(TPACPI_DBG_RFKILL, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4613 | "will attempt to %s wwan\n", |
| 4614 | (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable"); |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4615 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4616 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 4617 | if (dbg_wwanemul) { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4618 | tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4619 | return 0; |
| 4620 | } |
| 4621 | #endif |
| 4622 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4623 | if (state == TPACPI_RFK_RADIO_ON) |
Henrique de Moraes Holschuh | 08fedfc | 2010-02-25 22:22:07 -0300 | [diff] [blame] | 4624 | status = TP_ACPI_WANCARD_RADIOSSW |
| 4625 | | TP_ACPI_WANCARD_RESUMECTRL; |
| 4626 | else |
| 4627 | status = 0; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4628 | |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4629 | if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status)) |
| 4630 | return -EIO; |
| 4631 | |
| 4632 | return 0; |
| 4633 | } |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 4634 | |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4635 | /* sysfs wan enable ---------------------------------------------------- */ |
| 4636 | static ssize_t wan_enable_show(struct device *dev, |
| 4637 | struct device_attribute *attr, |
| 4638 | char *buf) |
| 4639 | { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4640 | return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID, |
| 4641 | attr, buf); |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | static ssize_t wan_enable_store(struct device *dev, |
| 4645 | struct device_attribute *attr, |
| 4646 | const char *buf, size_t count) |
| 4647 | { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4648 | return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID, |
| 4649 | attr, buf, count); |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4650 | } |
| 4651 | |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 4652 | static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO, |
| 4653 | wan_enable_show, wan_enable_store); |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4654 | |
| 4655 | /* --------------------------------------------------------------------- */ |
| 4656 | |
| 4657 | static struct attribute *wan_attributes[] = { |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 4658 | &dev_attr_wwan_enable.attr, |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4659 | NULL |
| 4660 | }; |
| 4661 | |
| 4662 | static const struct attribute_group wan_attr_group = { |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4663 | .attrs = wan_attributes, |
| 4664 | }; |
| 4665 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4666 | static const struct tpacpi_rfk_ops wan_tprfk_ops = { |
| 4667 | .get_status = wan_get_status, |
| 4668 | .set_status = wan_set_status, |
| 4669 | }; |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4670 | |
Henrique de Moraes Holschuh | 90d9d3c | 2009-01-11 03:01:02 -0200 | [diff] [blame] | 4671 | static void wan_shutdown(void) |
| 4672 | { |
| 4673 | /* Order firmware to save current state to NVRAM */ |
| 4674 | if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd", |
| 4675 | TP_ACPI_WGSV_SAVE_STATE)) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4676 | pr_notice("failed to save WWAN state to NVRAM\n"); |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4677 | else |
| 4678 | vdbg_printk(TPACPI_DBG_RFKILL, |
| 4679 | "WWAN state saved to NVRAM\n"); |
Henrique de Moraes Holschuh | 90d9d3c | 2009-01-11 03:01:02 -0200 | [diff] [blame] | 4680 | } |
| 4681 | |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4682 | static void wan_exit(void) |
| 4683 | { |
| 4684 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, |
| 4685 | &wan_attr_group); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4686 | |
| 4687 | tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID); |
| 4688 | |
| 4689 | wan_shutdown(); |
Henrique de Moraes Holschuh | 07431ec | 2008-07-21 09:15:50 -0300 | [diff] [blame] | 4690 | } |
| 4691 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 4692 | static int __init wan_init(struct ibm_init_struct *iibm) |
Jeremy Fitzhardinge | 42adb53 | 2006-06-01 17:41:00 -0400 | [diff] [blame] | 4693 | { |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4694 | int res; |
Henrique de Moraes Holschuh | d6fdd1e | 2007-04-21 11:08:40 -0300 | [diff] [blame] | 4695 | int status = 0; |
| 4696 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4697 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, |
| 4698 | "initializing wan subdriver\n"); |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 4699 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 4700 | TPACPI_ACPIHANDLE_INIT(hkey); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 4701 | |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 4702 | tp_features.wan = hkey_handle && |
Henrique de Moraes Holschuh | d6fdd1e | 2007-04-21 11:08:40 -0300 | [diff] [blame] | 4703 | acpi_evalf(hkey_handle, &status, "GWAN", "qd"); |
Jeremy Fitzhardinge | 42adb53 | 2006-06-01 17:41:00 -0400 | [diff] [blame] | 4704 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4705 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, |
| 4706 | "wan is %s, status 0x%02x\n", |
Henrique de Moraes Holschuh | d6fdd1e | 2007-04-21 11:08:40 -0300 | [diff] [blame] | 4707 | str_supported(tp_features.wan), |
| 4708 | status); |
| 4709 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4710 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 4711 | if (dbg_wwanemul) { |
| 4712 | tp_features.wan = 1; |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4713 | pr_info("wwan switch emulation enabled\n"); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 4714 | } else |
| 4715 | #endif |
Henrique de Moraes Holschuh | 3a87208 | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 4716 | if (tp_features.wan && |
| 4717 | !(status & TP_ACPI_WANCARD_HWPRESENT)) { |
| 4718 | /* no wan hardware present in system */ |
| 4719 | tp_features.wan = 0; |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4720 | dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, |
Henrique de Moraes Holschuh | 3a87208 | 2008-07-21 09:15:49 -0300 | [diff] [blame] | 4721 | "wan hardware not installed\n"); |
| 4722 | } |
| 4723 | |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4724 | if (!tp_features.wan) |
| 4725 | return 1; |
| 4726 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4727 | res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID, |
| 4728 | &wan_tprfk_ops, |
| 4729 | RFKILL_TYPE_WWAN, |
| 4730 | TPACPI_RFK_WWAN_SW_NAME, |
| 4731 | true); |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4732 | if (res) |
| 4733 | return res; |
| 4734 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4735 | res = sysfs_create_group(&tpacpi_pdev->dev.kobj, |
| 4736 | &wan_attr_group); |
| 4737 | |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4738 | if (res) { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4739 | tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID); |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4740 | return res; |
Henrique de Moraes Holschuh | d6fdd1e | 2007-04-21 11:08:40 -0300 | [diff] [blame] | 4741 | } |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 4742 | |
Henrique de Moraes Holschuh | 0e74dc2 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 4743 | return 0; |
Jeremy Fitzhardinge | 42adb53 | 2006-06-01 17:41:00 -0400 | [diff] [blame] | 4744 | } |
| 4745 | |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4746 | /* procfs -------------------------------------------------------------- */ |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 4747 | static int wan_read(struct seq_file *m) |
Jeremy Fitzhardinge | 42adb53 | 2006-06-01 17:41:00 -0400 | [diff] [blame] | 4748 | { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 4749 | return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m); |
Jeremy Fitzhardinge | 42adb53 | 2006-06-01 17:41:00 -0400 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | static int wan_write(char *buf) |
| 4753 | { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4754 | return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf); |
Jeremy Fitzhardinge | 42adb53 | 2006-06-01 17:41:00 -0400 | [diff] [blame] | 4755 | } |
| 4756 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 4757 | static struct ibm_struct wan_driver_data = { |
| 4758 | .name = "wan", |
| 4759 | .read = wan_read, |
| 4760 | .write = wan_write, |
Henrique de Moraes Holschuh | d3a6ade | 2007-04-27 22:00:17 -0300 | [diff] [blame] | 4761 | .exit = wan_exit, |
Henrique de Moraes Holschuh | 90d9d3c | 2009-01-11 03:01:02 -0200 | [diff] [blame] | 4762 | .shutdown = wan_shutdown, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 4763 | }; |
| 4764 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 4765 | /************************************************************************* |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4766 | * UWB subdriver |
| 4767 | */ |
| 4768 | |
| 4769 | enum { |
| 4770 | /* ACPI GUWB/SUWB bits */ |
| 4771 | TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */ |
| 4772 | TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */ |
| 4773 | }; |
| 4774 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4775 | #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw" |
| 4776 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4777 | static int uwb_get_status(void) |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4778 | { |
| 4779 | int status; |
| 4780 | |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4781 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 4782 | if (dbg_uwbemul) |
| 4783 | return (tpacpi_uwb_emulstate) ? |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4784 | TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4785 | #endif |
| 4786 | |
| 4787 | if (!acpi_evalf(hkey_handle, &status, "GUWB", "d")) |
| 4788 | return -EIO; |
| 4789 | |
| 4790 | return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ? |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4791 | TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4792 | } |
| 4793 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4794 | static int uwb_set_status(enum tpacpi_rfkill_state state) |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4795 | { |
| 4796 | int status; |
| 4797 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4798 | vdbg_printk(TPACPI_DBG_RFKILL, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4799 | "will attempt to %s UWB\n", |
| 4800 | (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable"); |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4801 | |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4802 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 4803 | if (dbg_uwbemul) { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4804 | tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON); |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4805 | return 0; |
| 4806 | } |
| 4807 | #endif |
| 4808 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4809 | if (state == TPACPI_RFK_RADIO_ON) |
| 4810 | status = TP_ACPI_UWB_RADIOSSW; |
| 4811 | else |
| 4812 | status = 0; |
| 4813 | |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4814 | if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status)) |
| 4815 | return -EIO; |
| 4816 | |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4817 | return 0; |
| 4818 | } |
| 4819 | |
| 4820 | /* --------------------------------------------------------------------- */ |
| 4821 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4822 | static const struct tpacpi_rfk_ops uwb_tprfk_ops = { |
| 4823 | .get_status = uwb_get_status, |
| 4824 | .set_status = uwb_set_status, |
| 4825 | }; |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4826 | |
| 4827 | static void uwb_exit(void) |
| 4828 | { |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4829 | tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID); |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | static int __init uwb_init(struct ibm_init_struct *iibm) |
| 4833 | { |
| 4834 | int res; |
| 4835 | int status = 0; |
| 4836 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4837 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, |
| 4838 | "initializing uwb subdriver\n"); |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4839 | |
| 4840 | TPACPI_ACPIHANDLE_INIT(hkey); |
| 4841 | |
| 4842 | tp_features.uwb = hkey_handle && |
| 4843 | acpi_evalf(hkey_handle, &status, "GUWB", "qd"); |
| 4844 | |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4845 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, |
| 4846 | "uwb is %s, status 0x%02x\n", |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4847 | str_supported(tp_features.uwb), |
| 4848 | status); |
| 4849 | |
| 4850 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
| 4851 | if (dbg_uwbemul) { |
| 4852 | tp_features.uwb = 1; |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 4853 | pr_info("uwb switch emulation enabled\n"); |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4854 | } else |
| 4855 | #endif |
| 4856 | if (tp_features.uwb && |
| 4857 | !(status & TP_ACPI_UWB_HWPRESENT)) { |
| 4858 | /* no uwb hardware present in system */ |
| 4859 | tp_features.uwb = 0; |
| 4860 | dbg_printk(TPACPI_DBG_INIT, |
| 4861 | "uwb hardware not installed\n"); |
| 4862 | } |
| 4863 | |
| 4864 | if (!tp_features.uwb) |
| 4865 | return 1; |
| 4866 | |
| 4867 | res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4868 | &uwb_tprfk_ops, |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4869 | RFKILL_TYPE_UWB, |
Henrique de Moraes Holschuh | bee4cd9b | 2009-04-04 04:25:50 +0000 | [diff] [blame] | 4870 | TPACPI_RFK_UWB_SW_NAME, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 4871 | false); |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 4872 | return res; |
| 4873 | } |
| 4874 | |
| 4875 | static struct ibm_struct uwb_driver_data = { |
| 4876 | .name = "uwb", |
| 4877 | .exit = uwb_exit, |
| 4878 | .flags.experimental = 1, |
| 4879 | }; |
| 4880 | |
| 4881 | /************************************************************************* |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 4882 | * Video subdriver |
| 4883 | */ |
| 4884 | |
Henrique de Moraes Holschuh | d7c1d17 | 2008-02-16 02:17:54 -0200 | [diff] [blame] | 4885 | #ifdef CONFIG_THINKPAD_ACPI_VIDEO |
| 4886 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 4887 | enum video_access_mode { |
| 4888 | TPACPI_VIDEO_NONE = 0, |
| 4889 | TPACPI_VIDEO_570, /* 570 */ |
| 4890 | TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */ |
| 4891 | TPACPI_VIDEO_NEW, /* all others */ |
| 4892 | }; |
| 4893 | |
| 4894 | enum { /* video status flags, based on VIDEO_570 */ |
| 4895 | TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */ |
| 4896 | TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */ |
| 4897 | TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */ |
| 4898 | }; |
| 4899 | |
| 4900 | enum { /* TPACPI_VIDEO_570 constants */ |
| 4901 | TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */ |
| 4902 | TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to |
| 4903 | * video_status_flags */ |
| 4904 | TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */ |
| 4905 | TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */ |
| 4906 | }; |
| 4907 | |
Henrique de Moraes Holschuh | 9a8e173 | 2006-11-25 16:36:00 -0200 | [diff] [blame] | 4908 | static enum video_access_mode video_supported; |
| 4909 | static int video_orig_autosw; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4910 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 4911 | static int video_autosw_get(void); |
| 4912 | static int video_autosw_set(int enable); |
| 4913 | |
Joe Perches | 112a6ee | 2011-04-04 10:06:24 -0700 | [diff] [blame] | 4914 | TPACPI_HANDLE(vid, root, |
| 4915 | "\\_SB.PCI.AGP.VGA", /* 570 */ |
| 4916 | "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */ |
| 4917 | "\\_SB.PCI0.VID0", /* 770e */ |
| 4918 | "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */ |
| 4919 | "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */ |
| 4920 | "\\_SB.PCI0.AGP.VID", /* all others */ |
| 4921 | ); /* R30, R31 */ |
| 4922 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 4923 | TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */ |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 4924 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 4925 | static int __init video_init(struct ibm_init_struct *iibm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | { |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4927 | int ivga; |
| 4928 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 4929 | vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n"); |
| 4930 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 4931 | TPACPI_ACPIHANDLE_INIT(vid); |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 4932 | if (tpacpi_is_ibm()) |
| 4933 | TPACPI_ACPIHANDLE_INIT(vid2); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 4934 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4935 | if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga) |
| 4936 | /* G41, assume IVGA doesn't change */ |
| 4937 | vid_handle = vid2_handle; |
| 4938 | |
| 4939 | if (!vid_handle) |
| 4940 | /* video switching not supported on R30, R31 */ |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 4941 | video_supported = TPACPI_VIDEO_NONE; |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 4942 | else if (tpacpi_is_ibm() && |
| 4943 | acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd")) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4944 | /* 570 */ |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 4945 | video_supported = TPACPI_VIDEO_570; |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 4946 | else if (tpacpi_is_ibm() && |
| 4947 | acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd")) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4948 | /* 600e/x, 770e, 770x */ |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 4949 | video_supported = TPACPI_VIDEO_770; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 4950 | else |
| 4951 | /* all others */ |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 4952 | video_supported = TPACPI_VIDEO_NEW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 4954 | vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n", |
| 4955 | str_supported(video_supported != TPACPI_VIDEO_NONE), |
| 4956 | video_supported); |
| 4957 | |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 4958 | return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4959 | } |
| 4960 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 4961 | static void video_exit(void) |
| 4962 | { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 4963 | dbg_printk(TPACPI_DBG_EXIT, |
| 4964 | "restoring original video autoswitch mode\n"); |
| 4965 | if (video_autosw_set(video_orig_autosw)) |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 4966 | pr_err("error while trying to restore original video autoswitch mode\n"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 4967 | } |
| 4968 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 4969 | static int video_outputsw_get(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4970 | { |
| 4971 | int status = 0; |
| 4972 | int i; |
| 4973 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 4974 | switch (video_supported) { |
| 4975 | case TPACPI_VIDEO_570: |
| 4976 | if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", |
| 4977 | TP_ACPI_VIDEO_570_PHSCMD)) |
| 4978 | return -EIO; |
| 4979 | status = i & TP_ACPI_VIDEO_570_PHSMASK; |
| 4980 | break; |
| 4981 | case TPACPI_VIDEO_770: |
| 4982 | if (!acpi_evalf(NULL, &i, "\\VCDL", "d")) |
| 4983 | return -EIO; |
| 4984 | if (i) |
| 4985 | status |= TP_ACPI_VIDEO_S_LCD; |
| 4986 | if (!acpi_evalf(NULL, &i, "\\VCDC", "d")) |
| 4987 | return -EIO; |
| 4988 | if (i) |
| 4989 | status |= TP_ACPI_VIDEO_S_CRT; |
| 4990 | break; |
| 4991 | case TPACPI_VIDEO_NEW: |
| 4992 | if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) || |
| 4993 | !acpi_evalf(NULL, &i, "\\VCDC", "d")) |
| 4994 | return -EIO; |
| 4995 | if (i) |
| 4996 | status |= TP_ACPI_VIDEO_S_CRT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 4998 | if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) || |
| 4999 | !acpi_evalf(NULL, &i, "\\VCDL", "d")) |
| 5000 | return -EIO; |
| 5001 | if (i) |
| 5002 | status |= TP_ACPI_VIDEO_S_LCD; |
| 5003 | if (!acpi_evalf(NULL, &i, "\\VCDD", "d")) |
| 5004 | return -EIO; |
| 5005 | if (i) |
| 5006 | status |= TP_ACPI_VIDEO_S_DVI; |
| 5007 | break; |
| 5008 | default: |
| 5009 | return -ENOSYS; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5010 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5011 | |
| 5012 | return status; |
| 5013 | } |
| 5014 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5015 | static int video_outputsw_set(int status) |
| 5016 | { |
| 5017 | int autosw; |
| 5018 | int res = 0; |
| 5019 | |
| 5020 | switch (video_supported) { |
| 5021 | case TPACPI_VIDEO_570: |
| 5022 | res = acpi_evalf(NULL, NULL, |
| 5023 | "\\_SB.PHS2", "vdd", |
| 5024 | TP_ACPI_VIDEO_570_PHS2CMD, |
| 5025 | status | TP_ACPI_VIDEO_570_PHS2SET); |
| 5026 | break; |
| 5027 | case TPACPI_VIDEO_770: |
| 5028 | autosw = video_autosw_get(); |
| 5029 | if (autosw < 0) |
| 5030 | return autosw; |
| 5031 | |
| 5032 | res = video_autosw_set(1); |
| 5033 | if (res) |
| 5034 | return res; |
| 5035 | res = acpi_evalf(vid_handle, NULL, |
| 5036 | "ASWT", "vdd", status * 0x100, 0); |
| 5037 | if (!autosw && video_autosw_set(autosw)) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 5038 | pr_err("video auto-switch left enabled due to error\n"); |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5039 | return -EIO; |
| 5040 | } |
| 5041 | break; |
| 5042 | case TPACPI_VIDEO_NEW: |
| 5043 | res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) && |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 5044 | acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1); |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5045 | break; |
| 5046 | default: |
| 5047 | return -ENOSYS; |
| 5048 | } |
| 5049 | |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5050 | return (res) ? 0 : -EIO; |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | static int video_autosw_get(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | { |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5055 | int autosw = 0; |
| 5056 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5057 | switch (video_supported) { |
| 5058 | case TPACPI_VIDEO_570: |
| 5059 | if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d")) |
| 5060 | return -EIO; |
| 5061 | break; |
| 5062 | case TPACPI_VIDEO_770: |
| 5063 | case TPACPI_VIDEO_NEW: |
| 5064 | if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d")) |
| 5065 | return -EIO; |
| 5066 | break; |
| 5067 | default: |
| 5068 | return -ENOSYS; |
| 5069 | } |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5070 | |
| 5071 | return autosw & 1; |
| 5072 | } |
| 5073 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5074 | static int video_autosw_set(int enable) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5075 | { |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5076 | if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0)) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5077 | return -EIO; |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5078 | return 0; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5079 | } |
| 5080 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5081 | static int video_outputsw_cycle(void) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5082 | { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5083 | int autosw = video_autosw_get(); |
| 5084 | int res; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5085 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5086 | if (autosw < 0) |
| 5087 | return autosw; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5088 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5089 | switch (video_supported) { |
| 5090 | case TPACPI_VIDEO_570: |
| 5091 | res = video_autosw_set(1); |
| 5092 | if (res) |
| 5093 | return res; |
| 5094 | res = acpi_evalf(ec_handle, NULL, "_Q16", "v"); |
| 5095 | break; |
| 5096 | case TPACPI_VIDEO_770: |
| 5097 | case TPACPI_VIDEO_NEW: |
| 5098 | res = video_autosw_set(1); |
| 5099 | if (res) |
| 5100 | return res; |
| 5101 | res = acpi_evalf(vid_handle, NULL, "VSWT", "v"); |
| 5102 | break; |
| 5103 | default: |
| 5104 | return -ENOSYS; |
| 5105 | } |
| 5106 | if (!autosw && video_autosw_set(autosw)) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 5107 | pr_err("video auto-switch left enabled due to error\n"); |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5108 | return -EIO; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5109 | } |
| 5110 | |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5111 | return (res) ? 0 : -EIO; |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5112 | } |
| 5113 | |
| 5114 | static int video_expand_toggle(void) |
| 5115 | { |
| 5116 | switch (video_supported) { |
| 5117 | case TPACPI_VIDEO_570: |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5118 | return acpi_evalf(ec_handle, NULL, "_Q17", "v") ? |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5119 | 0 : -EIO; |
| 5120 | case TPACPI_VIDEO_770: |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5121 | return acpi_evalf(vid_handle, NULL, "VEXP", "v") ? |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5122 | 0 : -EIO; |
| 5123 | case TPACPI_VIDEO_NEW: |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5124 | return acpi_evalf(NULL, NULL, "\\VEXP", "v") ? |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5125 | 0 : -EIO; |
| 5126 | default: |
| 5127 | return -ENOSYS; |
| 5128 | } |
| 5129 | /* not reached */ |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5130 | } |
| 5131 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5132 | static int video_read(struct seq_file *m) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5133 | { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5134 | int status, autosw; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5135 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5136 | if (video_supported == TPACPI_VIDEO_NONE) { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5137 | seq_printf(m, "status:\t\tnot supported\n"); |
| 5138 | return 0; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5139 | } |
| 5140 | |
Henrique de Moraes Holschuh | b525c06 | 2010-02-25 22:22:22 -0300 | [diff] [blame] | 5141 | /* Even reads can crash X.org, so... */ |
| 5142 | if (!capable(CAP_SYS_ADMIN)) |
| 5143 | return -EPERM; |
| 5144 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5145 | status = video_outputsw_get(); |
| 5146 | if (status < 0) |
| 5147 | return status; |
| 5148 | |
| 5149 | autosw = video_autosw_get(); |
| 5150 | if (autosw < 0) |
| 5151 | return autosw; |
| 5152 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5153 | seq_printf(m, "status:\t\tsupported\n"); |
| 5154 | seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0)); |
| 5155 | seq_printf(m, "crt:\t\t%s\n", enabled(status, 1)); |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 5156 | if (video_supported == TPACPI_VIDEO_NEW) |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5157 | seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3)); |
| 5158 | seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0)); |
| 5159 | seq_printf(m, "commands:\tlcd_enable, lcd_disable\n"); |
| 5160 | seq_printf(m, "commands:\tcrt_enable, crt_disable\n"); |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 5161 | if (video_supported == TPACPI_VIDEO_NEW) |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5162 | seq_printf(m, "commands:\tdvi_enable, dvi_disable\n"); |
| 5163 | seq_printf(m, "commands:\tauto_enable, auto_disable\n"); |
| 5164 | seq_printf(m, "commands:\tvideo_switch, expand_toggle\n"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5165 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5166 | return 0; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5167 | } |
| 5168 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5169 | static int video_write(char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | { |
| 5171 | char *cmd; |
| 5172 | int enable, disable, status; |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5173 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5175 | if (video_supported == TPACPI_VIDEO_NONE) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5176 | return -ENODEV; |
| 5177 | |
Henrique de Moraes Holschuh | b525c06 | 2010-02-25 22:22:22 -0300 | [diff] [blame] | 5178 | /* Even reads can crash X.org, let alone writes... */ |
| 5179 | if (!capable(CAP_SYS_ADMIN)) |
| 5180 | return -EPERM; |
| 5181 | |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5182 | enable = 0; |
| 5183 | disable = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 5185 | while ((cmd = strsep(&buf, ","))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | if (strlencmp(cmd, "lcd_enable") == 0) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5187 | enable |= TP_ACPI_VIDEO_S_LCD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5188 | } else if (strlencmp(cmd, "lcd_disable") == 0) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5189 | disable |= TP_ACPI_VIDEO_S_LCD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | } else if (strlencmp(cmd, "crt_enable") == 0) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5191 | enable |= TP_ACPI_VIDEO_S_CRT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | } else if (strlencmp(cmd, "crt_disable") == 0) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5193 | disable |= TP_ACPI_VIDEO_S_CRT; |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 5194 | } else if (video_supported == TPACPI_VIDEO_NEW && |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5195 | strlencmp(cmd, "dvi_enable") == 0) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5196 | enable |= TP_ACPI_VIDEO_S_DVI; |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 5197 | } else if (video_supported == TPACPI_VIDEO_NEW && |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5198 | strlencmp(cmd, "dvi_disable") == 0) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5199 | disable |= TP_ACPI_VIDEO_S_DVI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | } else if (strlencmp(cmd, "auto_enable") == 0) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5201 | res = video_autosw_set(1); |
| 5202 | if (res) |
| 5203 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | } else if (strlencmp(cmd, "auto_disable") == 0) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5205 | res = video_autosw_set(0); |
| 5206 | if (res) |
| 5207 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5208 | } else if (strlencmp(cmd, "video_switch") == 0) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5209 | res = video_outputsw_cycle(); |
| 5210 | if (res) |
| 5211 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | } else if (strlencmp(cmd, "expand_toggle") == 0) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5213 | res = video_expand_toggle(); |
| 5214 | if (res) |
| 5215 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | } else |
| 5217 | return -EINVAL; |
| 5218 | } |
| 5219 | |
| 5220 | if (enable || disable) { |
Henrique de Moraes Holschuh | 83f3472 | 2007-04-21 11:08:41 -0300 | [diff] [blame] | 5221 | status = video_outputsw_get(); |
| 5222 | if (status < 0) |
| 5223 | return status; |
| 5224 | res = video_outputsw_set((status & ~disable) | enable); |
| 5225 | if (res) |
| 5226 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | return 0; |
| 5230 | } |
| 5231 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 5232 | static struct ibm_struct video_driver_data = { |
| 5233 | .name = "video", |
| 5234 | .read = video_read, |
| 5235 | .write = video_write, |
| 5236 | .exit = video_exit, |
| 5237 | }; |
| 5238 | |
Henrique de Moraes Holschuh | d7c1d17 | 2008-02-16 02:17:54 -0200 | [diff] [blame] | 5239 | #endif /* CONFIG_THINKPAD_ACPI_VIDEO */ |
| 5240 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5241 | /************************************************************************* |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5242 | * Keyboard backlight subdriver |
| 5243 | */ |
| 5244 | |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 5245 | static enum led_brightness kbdlight_brightness; |
| 5246 | static DEFINE_MUTEX(kbdlight_mutex); |
| 5247 | |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5248 | static int kbdlight_set_level(int level) |
| 5249 | { |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 5250 | int ret = 0; |
| 5251 | |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5252 | if (!hkey_handle) |
| 5253 | return -ENXIO; |
| 5254 | |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 5255 | mutex_lock(&kbdlight_mutex); |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5256 | |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 5257 | if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level)) |
| 5258 | ret = -EIO; |
| 5259 | else |
| 5260 | kbdlight_brightness = level; |
| 5261 | |
| 5262 | mutex_unlock(&kbdlight_mutex); |
| 5263 | |
| 5264 | return ret; |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5265 | } |
| 5266 | |
| 5267 | static int kbdlight_get_level(void) |
| 5268 | { |
| 5269 | int status = 0; |
| 5270 | |
| 5271 | if (!hkey_handle) |
| 5272 | return -ENXIO; |
| 5273 | |
| 5274 | if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0)) |
| 5275 | return -EIO; |
| 5276 | |
| 5277 | if (status < 0) |
| 5278 | return status; |
| 5279 | |
| 5280 | return status & 0x3; |
| 5281 | } |
| 5282 | |
| 5283 | static bool kbdlight_is_supported(void) |
| 5284 | { |
| 5285 | int status = 0; |
| 5286 | |
| 5287 | if (!hkey_handle) |
| 5288 | return false; |
| 5289 | |
| 5290 | if (!acpi_has_method(hkey_handle, "MLCG")) { |
| 5291 | vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n"); |
| 5292 | return false; |
| 5293 | } |
| 5294 | |
| 5295 | if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) { |
| 5296 | vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n"); |
| 5297 | return false; |
| 5298 | } |
| 5299 | |
| 5300 | if (status < 0) { |
| 5301 | vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status); |
| 5302 | return false; |
| 5303 | } |
| 5304 | |
| 5305 | vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status); |
| 5306 | /* |
| 5307 | * Guessed test for keyboard backlight: |
| 5308 | * |
| 5309 | * Machines with backlight keyboard return: |
| 5310 | * b010100000010000000XX - ThinkPad X1 Carbon 3rd |
| 5311 | * b110100010010000000XX - ThinkPad x230 |
| 5312 | * b010100000010000000XX - ThinkPad x240 |
| 5313 | * b010100000010000000XX - ThinkPad W541 |
| 5314 | * (XX is current backlight level) |
| 5315 | * |
| 5316 | * Machines without backlight keyboard return: |
| 5317 | * b10100001000000000000 - ThinkPad x230 |
| 5318 | * b10110001000000000000 - ThinkPad E430 |
| 5319 | * b00000000000000000000 - ThinkPad E450 |
| 5320 | * |
| 5321 | * Candidate BITs for detection test (XOR): |
| 5322 | * b01000000001000000000 |
| 5323 | * ^ |
| 5324 | */ |
| 5325 | return status & BIT(9); |
| 5326 | } |
| 5327 | |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5328 | static int kbdlight_sysfs_set(struct led_classdev *led_cdev, |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5329 | enum led_brightness brightness) |
| 5330 | { |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5331 | return kbdlight_set_level(brightness); |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5332 | } |
| 5333 | |
| 5334 | static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev) |
| 5335 | { |
| 5336 | int level; |
| 5337 | |
| 5338 | level = kbdlight_get_level(); |
| 5339 | if (level < 0) |
| 5340 | return 0; |
| 5341 | |
| 5342 | return level; |
| 5343 | } |
| 5344 | |
| 5345 | static struct tpacpi_led_classdev tpacpi_led_kbdlight = { |
| 5346 | .led_classdev = { |
| 5347 | .name = "tpacpi::kbd_backlight", |
| 5348 | .max_brightness = 2, |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 5349 | .flags = LED_BRIGHT_HW_CHANGED, |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5350 | .brightness_set_blocking = &kbdlight_sysfs_set, |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5351 | .brightness_get = &kbdlight_sysfs_get, |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5352 | } |
| 5353 | }; |
| 5354 | |
| 5355 | static int __init kbdlight_init(struct ibm_init_struct *iibm) |
| 5356 | { |
| 5357 | int rc; |
| 5358 | |
| 5359 | vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n"); |
| 5360 | |
| 5361 | TPACPI_ACPIHANDLE_INIT(hkey); |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5362 | |
| 5363 | if (!kbdlight_is_supported()) { |
| 5364 | tp_features.kbdlight = 0; |
| 5365 | vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n"); |
| 5366 | return 1; |
| 5367 | } |
| 5368 | |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 5369 | kbdlight_brightness = kbdlight_sysfs_get(NULL); |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5370 | tp_features.kbdlight = 1; |
| 5371 | |
| 5372 | rc = led_classdev_register(&tpacpi_pdev->dev, |
| 5373 | &tpacpi_led_kbdlight.led_classdev); |
| 5374 | if (rc < 0) { |
| 5375 | tp_features.kbdlight = 0; |
| 5376 | return rc; |
| 5377 | } |
| 5378 | |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 5379 | tpacpi_hotkey_driver_mask_set(hotkey_driver_mask | |
| 5380 | TP_ACPI_HKEY_KBD_LIGHT_MASK); |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5381 | return 0; |
| 5382 | } |
| 5383 | |
| 5384 | static void kbdlight_exit(void) |
| 5385 | { |
Andy Shevchenko | 41f8004 | 2020-05-07 19:37:23 +0300 | [diff] [blame] | 5386 | led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev); |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5387 | } |
| 5388 | |
Marco Trevisan (Treviño) | afcedeb | 2016-05-24 00:39:51 +0200 | [diff] [blame] | 5389 | static int kbdlight_set_level_and_update(int level) |
| 5390 | { |
| 5391 | int ret; |
| 5392 | struct led_classdev *led_cdev; |
| 5393 | |
| 5394 | ret = kbdlight_set_level(level); |
| 5395 | led_cdev = &tpacpi_led_kbdlight.led_classdev; |
| 5396 | |
| 5397 | if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED)) |
| 5398 | led_cdev->brightness = level; |
| 5399 | |
| 5400 | return ret; |
| 5401 | } |
| 5402 | |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5403 | static int kbdlight_read(struct seq_file *m) |
| 5404 | { |
| 5405 | int level; |
| 5406 | |
| 5407 | if (!tp_features.kbdlight) { |
| 5408 | seq_printf(m, "status:\t\tnot supported\n"); |
| 5409 | } else { |
| 5410 | level = kbdlight_get_level(); |
| 5411 | if (level < 0) |
| 5412 | seq_printf(m, "status:\t\terror %d\n", level); |
| 5413 | else |
| 5414 | seq_printf(m, "status:\t\t%d\n", level); |
| 5415 | seq_printf(m, "commands:\t0, 1, 2\n"); |
| 5416 | } |
| 5417 | |
| 5418 | return 0; |
| 5419 | } |
| 5420 | |
| 5421 | static int kbdlight_write(char *buf) |
| 5422 | { |
| 5423 | char *cmd; |
Andy Shevchenko | 466f469 | 2020-05-11 15:54:14 +0300 | [diff] [blame] | 5424 | int res, level = -EINVAL; |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5425 | |
| 5426 | if (!tp_features.kbdlight) |
| 5427 | return -ENODEV; |
| 5428 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 5429 | while ((cmd = strsep(&buf, ","))) { |
Andy Shevchenko | 466f469 | 2020-05-11 15:54:14 +0300 | [diff] [blame] | 5430 | res = kstrtoint(cmd, 10, &level); |
| 5431 | if (res < 0) |
| 5432 | return res; |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5433 | } |
| 5434 | |
Andy Shevchenko | 466f469 | 2020-05-11 15:54:14 +0300 | [diff] [blame] | 5435 | if (level >= 3 || level < 0) |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5436 | return -EINVAL; |
| 5437 | |
Marco Trevisan (Treviño) | afcedeb | 2016-05-24 00:39:51 +0200 | [diff] [blame] | 5438 | return kbdlight_set_level_and_update(level); |
| 5439 | } |
| 5440 | |
| 5441 | static void kbdlight_suspend(void) |
| 5442 | { |
| 5443 | struct led_classdev *led_cdev; |
| 5444 | |
| 5445 | if (!tp_features.kbdlight) |
| 5446 | return; |
| 5447 | |
| 5448 | led_cdev = &tpacpi_led_kbdlight.led_classdev; |
| 5449 | led_update_brightness(led_cdev); |
| 5450 | led_classdev_suspend(led_cdev); |
| 5451 | } |
| 5452 | |
| 5453 | static void kbdlight_resume(void) |
| 5454 | { |
| 5455 | if (!tp_features.kbdlight) |
| 5456 | return; |
| 5457 | |
| 5458 | led_classdev_resume(&tpacpi_led_kbdlight.led_classdev); |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | static struct ibm_struct kbdlight_driver_data = { |
| 5462 | .name = "kbdlight", |
| 5463 | .read = kbdlight_read, |
| 5464 | .write = kbdlight_write, |
Marco Trevisan (Treviño) | afcedeb | 2016-05-24 00:39:51 +0200 | [diff] [blame] | 5465 | .suspend = kbdlight_suspend, |
| 5466 | .resume = kbdlight_resume, |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 5467 | .exit = kbdlight_exit, |
| 5468 | }; |
| 5469 | |
| 5470 | /************************************************************************* |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5471 | * Light (thinklight) subdriver |
| 5472 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 5474 | TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */ |
| 5475 | TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */ |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5476 | |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5477 | static int light_get_status(void) |
| 5478 | { |
| 5479 | int status = 0; |
| 5480 | |
| 5481 | if (tp_features.light_status) { |
| 5482 | if (!acpi_evalf(ec_handle, &status, "KBLT", "d")) |
| 5483 | return -EIO; |
| 5484 | return (!!status); |
| 5485 | } |
| 5486 | |
| 5487 | return -ENXIO; |
| 5488 | } |
| 5489 | |
| 5490 | static int light_set_status(int status) |
| 5491 | { |
| 5492 | int rc; |
| 5493 | |
| 5494 | if (tp_features.light) { |
| 5495 | if (cmos_handle) { |
| 5496 | rc = acpi_evalf(cmos_handle, NULL, NULL, "vd", |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5497 | (status) ? |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5498 | TP_CMOS_THINKLIGHT_ON : |
| 5499 | TP_CMOS_THINKLIGHT_OFF); |
| 5500 | } else { |
| 5501 | rc = acpi_evalf(lght_handle, NULL, NULL, "vd", |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5502 | (status) ? 1 : 0); |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5503 | } |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5504 | return (rc) ? 0 : -EIO; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5505 | } |
| 5506 | |
| 5507 | return -ENXIO; |
| 5508 | } |
| 5509 | |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5510 | static int light_sysfs_set(struct led_classdev *led_cdev, |
Henrique de Moraes Holschuh | e306501 | 2008-04-26 01:02:24 -0300 | [diff] [blame] | 5511 | enum led_brightness brightness) |
| 5512 | { |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5513 | return light_set_status((brightness != LED_OFF) ? |
| 5514 | TPACPI_LED_ON : TPACPI_LED_OFF); |
Henrique de Moraes Holschuh | e306501 | 2008-04-26 01:02:24 -0300 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev) |
| 5518 | { |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5519 | return (light_get_status() == 1) ? LED_FULL : LED_OFF; |
Henrique de Moraes Holschuh | e306501 | 2008-04-26 01:02:24 -0300 | [diff] [blame] | 5520 | } |
| 5521 | |
| 5522 | static struct tpacpi_led_classdev tpacpi_led_thinklight = { |
| 5523 | .led_classdev = { |
| 5524 | .name = "tpacpi::thinklight", |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5525 | .brightness_set_blocking = &light_sysfs_set, |
Henrique de Moraes Holschuh | e306501 | 2008-04-26 01:02:24 -0300 | [diff] [blame] | 5526 | .brightness_get = &light_sysfs_get, |
| 5527 | } |
| 5528 | }; |
| 5529 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 5530 | static int __init light_init(struct ibm_init_struct *iibm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5531 | { |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 5532 | int rc; |
Henrique de Moraes Holschuh | e306501 | 2008-04-26 01:02:24 -0300 | [diff] [blame] | 5533 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 5534 | vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n"); |
| 5535 | |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 5536 | if (tpacpi_is_ibm()) { |
| 5537 | TPACPI_ACPIHANDLE_INIT(ledb); |
| 5538 | TPACPI_ACPIHANDLE_INIT(lght); |
| 5539 | } |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 5540 | TPACPI_ACPIHANDLE_INIT(cmos); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 5541 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5542 | /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */ |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 5543 | tp_features.light = (cmos_handle || lght_handle) && !ledb_handle; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5544 | |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 5545 | if (tp_features.light) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5546 | /* light status not supported on |
| 5547 | 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */ |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 5548 | tp_features.light_status = |
| 5549 | acpi_evalf(ec_handle, NULL, "KBLT", "qv"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 5551 | vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n", |
| 5552 | str_supported(tp_features.light), |
| 5553 | str_supported(tp_features.light_status)); |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 5554 | |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 5555 | if (!tp_features.light) |
| 5556 | return 1; |
| 5557 | |
| 5558 | rc = led_classdev_register(&tpacpi_pdev->dev, |
| 5559 | &tpacpi_led_thinklight.led_classdev); |
Henrique de Moraes Holschuh | e306501 | 2008-04-26 01:02:24 -0300 | [diff] [blame] | 5560 | |
| 5561 | if (rc < 0) { |
| 5562 | tp_features.light = 0; |
| 5563 | tp_features.light_status = 0; |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 5564 | } else { |
| 5565 | rc = 0; |
Henrique de Moraes Holschuh | e306501 | 2008-04-26 01:02:24 -0300 | [diff] [blame] | 5566 | } |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 5567 | |
Henrique de Moraes Holschuh | e306501 | 2008-04-26 01:02:24 -0300 | [diff] [blame] | 5568 | return rc; |
| 5569 | } |
| 5570 | |
| 5571 | static void light_exit(void) |
| 5572 | { |
| 5573 | led_classdev_unregister(&tpacpi_led_thinklight.led_classdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | } |
| 5575 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5576 | static int light_read(struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | { |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5578 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 5580 | if (!tp_features.light) { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5581 | seq_printf(m, "status:\t\tnot supported\n"); |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 5582 | } else if (!tp_features.light_status) { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5583 | seq_printf(m, "status:\t\tunknown\n"); |
| 5584 | seq_printf(m, "commands:\ton, off\n"); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5585 | } else { |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5586 | status = light_get_status(); |
| 5587 | if (status < 0) |
| 5588 | return status; |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5589 | seq_printf(m, "status:\t\t%s\n", onoff(status, 0)); |
| 5590 | seq_printf(m, "commands:\ton, off\n"); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5593 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | } |
| 5595 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5596 | static int light_write(char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | char *cmd; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5599 | int newstatus = 0; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5600 | |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 5601 | if (!tp_features.light) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5602 | return -ENODEV; |
| 5603 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 5604 | while ((cmd = strsep(&buf, ","))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | if (strlencmp(cmd, "on") == 0) { |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5606 | newstatus = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | } else if (strlencmp(cmd, "off") == 0) { |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5608 | newstatus = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | } else |
| 5610 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | } |
| 5612 | |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5613 | return light_set_status(newstatus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5614 | } |
| 5615 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 5616 | static struct ibm_struct light_driver_data = { |
| 5617 | .name = "light", |
| 5618 | .read = light_read, |
| 5619 | .write = light_write, |
Henrique de Moraes Holschuh | e306501 | 2008-04-26 01:02:24 -0300 | [diff] [blame] | 5620 | .exit = light_exit, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 5621 | }; |
| 5622 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5623 | /************************************************************************* |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5624 | * CMOS subdriver |
| 5625 | */ |
| 5626 | |
Henrique de Moraes Holschuh | b616004 | 2007-04-24 11:48:19 -0300 | [diff] [blame] | 5627 | /* sysfs cmos_command -------------------------------------------------- */ |
| 5628 | static ssize_t cmos_command_store(struct device *dev, |
| 5629 | struct device_attribute *attr, |
| 5630 | const char *buf, size_t count) |
| 5631 | { |
| 5632 | unsigned long cmos_cmd; |
| 5633 | int res; |
| 5634 | |
| 5635 | if (parse_strtoul(buf, 21, &cmos_cmd)) |
| 5636 | return -EINVAL; |
| 5637 | |
| 5638 | res = issue_thinkpad_cmos_command(cmos_cmd); |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5639 | return (res) ? res : count; |
Henrique de Moraes Holschuh | b616004 | 2007-04-24 11:48:19 -0300 | [diff] [blame] | 5640 | } |
| 5641 | |
Bastien Nocera | b4dd04ac | 2015-03-02 18:40:50 +0100 | [diff] [blame] | 5642 | static DEVICE_ATTR_WO(cmos_command); |
Henrique de Moraes Holschuh | b616004 | 2007-04-24 11:48:19 -0300 | [diff] [blame] | 5643 | |
| 5644 | /* --------------------------------------------------------------------- */ |
| 5645 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 5646 | static int __init cmos_init(struct ibm_init_struct *iibm) |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 5647 | { |
Henrique de Moraes Holschuh | b616004 | 2007-04-24 11:48:19 -0300 | [diff] [blame] | 5648 | int res; |
| 5649 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 5650 | vdbg_printk(TPACPI_DBG_INIT, |
| 5651 | "initializing cmos commands subdriver\n"); |
| 5652 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 5653 | TPACPI_ACPIHANDLE_INIT(cmos); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 5654 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 5655 | vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n", |
| 5656 | str_supported(cmos_handle != NULL)); |
Henrique de Moraes Holschuh | b616004 | 2007-04-24 11:48:19 -0300 | [diff] [blame] | 5657 | |
| 5658 | res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command); |
| 5659 | if (res) |
| 5660 | return res; |
| 5661 | |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5662 | return (cmos_handle) ? 0 : 1; |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 5663 | } |
| 5664 | |
Henrique de Moraes Holschuh | b616004 | 2007-04-24 11:48:19 -0300 | [diff] [blame] | 5665 | static void cmos_exit(void) |
| 5666 | { |
| 5667 | device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command); |
| 5668 | } |
| 5669 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5670 | static int cmos_read(struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | { |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5672 | /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, |
| 5673 | R30, R31, T20-22, X20-21 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | if (!cmos_handle) |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5675 | seq_printf(m, "status:\t\tnot supported\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | else { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5677 | seq_printf(m, "status:\t\tsupported\n"); |
| 5678 | seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | } |
| 5680 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 5681 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | } |
| 5683 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5684 | static int cmos_write(char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | { |
| 5686 | char *cmd; |
Henrique de Moraes Holschuh | c9bea99 | 2007-04-21 11:08:42 -0300 | [diff] [blame] | 5687 | int cmos_cmd, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 5689 | while ((cmd = strsep(&buf, ","))) { |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5690 | if (sscanf(cmd, "%u", &cmos_cmd) == 1 && |
| 5691 | cmos_cmd >= 0 && cmos_cmd <= 21) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | /* cmos_cmd set */ |
| 5693 | } else |
| 5694 | return -EINVAL; |
| 5695 | |
Henrique de Moraes Holschuh | c9bea99 | 2007-04-21 11:08:42 -0300 | [diff] [blame] | 5696 | res = issue_thinkpad_cmos_command(cmos_cmd); |
| 5697 | if (res) |
| 5698 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | return 0; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5702 | } |
| 5703 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 5704 | static struct ibm_struct cmos_driver_data = { |
| 5705 | .name = "cmos", |
| 5706 | .read = cmos_read, |
| 5707 | .write = cmos_write, |
Henrique de Moraes Holschuh | b616004 | 2007-04-24 11:48:19 -0300 | [diff] [blame] | 5708 | .exit = cmos_exit, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 5709 | }; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5710 | |
| 5711 | /************************************************************************* |
| 5712 | * LED subdriver |
| 5713 | */ |
| 5714 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 5715 | enum led_access_mode { |
| 5716 | TPACPI_LED_NONE = 0, |
| 5717 | TPACPI_LED_570, /* 570 */ |
| 5718 | TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */ |
| 5719 | TPACPI_LED_NEW, /* all others */ |
| 5720 | }; |
| 5721 | |
| 5722 | enum { /* For TPACPI_LED_OLD */ |
| 5723 | TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */ |
| 5724 | TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */ |
| 5725 | TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */ |
| 5726 | }; |
| 5727 | |
Henrique de Moraes Holschuh | 9a8e173 | 2006-11-25 16:36:00 -0200 | [diff] [blame] | 5728 | static enum led_access_mode led_supported; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5729 | |
Henrique de Moraes Holschuh | 2cbb5c8 | 2010-05-16 19:45:50 -0300 | [diff] [blame] | 5730 | static acpi_handle led_handle; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 5731 | |
Henrique de Moraes Holschuh | f21179a | 2009-05-30 13:25:08 -0300 | [diff] [blame] | 5732 | #define TPACPI_LED_NUMLEDS 16 |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5733 | static struct tpacpi_led_classdev *tpacpi_leds; |
| 5734 | static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS]; |
Harvey Harrison | e3aa51f | 2008-05-29 17:51:57 -0700 | [diff] [blame] | 5735 | static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = { |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5736 | /* there's a limit of 19 chars + NULL before 2.6.26 */ |
| 5737 | "tpacpi::power", |
| 5738 | "tpacpi:orange:batt", |
| 5739 | "tpacpi:green:batt", |
| 5740 | "tpacpi::dock_active", |
| 5741 | "tpacpi::bay_active", |
| 5742 | "tpacpi::dock_batt", |
| 5743 | "tpacpi::unknown_led", |
| 5744 | "tpacpi::standby", |
Henrique de Moraes Holschuh | f21179a | 2009-05-30 13:25:08 -0300 | [diff] [blame] | 5745 | "tpacpi::dock_status1", |
| 5746 | "tpacpi::dock_status2", |
Hans de Goede | be892e9 | 2021-11-23 22:05:24 +0100 | [diff] [blame] | 5747 | "tpacpi::lid_logo_dot", |
Henrique de Moraes Holschuh | f21179a | 2009-05-30 13:25:08 -0300 | [diff] [blame] | 5748 | "tpacpi::unknown_led3", |
| 5749 | "tpacpi::thinkvantage", |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5750 | }; |
Hans de Goede | be892e9 | 2021-11-23 22:05:24 +0100 | [diff] [blame] | 5751 | #define TPACPI_SAFE_LEDS 0x1481U |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5752 | |
| 5753 | static inline bool tpacpi_is_led_restricted(const unsigned int led) |
| 5754 | { |
| 5755 | #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS |
| 5756 | return false; |
| 5757 | #else |
Henrique de Moraes Holschuh | f21179a | 2009-05-30 13:25:08 -0300 | [diff] [blame] | 5758 | return (1U & (TPACPI_SAFE_LEDS >> led)) == 0; |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5759 | #endif |
| 5760 | } |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5761 | |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5762 | static int led_get_status(const unsigned int led) |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5763 | { |
| 5764 | int status; |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5765 | enum led_status_t led_s; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5766 | |
| 5767 | switch (led_supported) { |
| 5768 | case TPACPI_LED_570: |
| 5769 | if (!acpi_evalf(ec_handle, |
| 5770 | &status, "GLED", "dd", 1 << led)) |
| 5771 | return -EIO; |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5772 | led_s = (status == 0) ? |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5773 | TPACPI_LED_OFF : |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 5774 | ((status == 1) ? |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5775 | TPACPI_LED_ON : |
| 5776 | TPACPI_LED_BLINK); |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5777 | tpacpi_led_state_cache[led] = led_s; |
| 5778 | return led_s; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5779 | default: |
| 5780 | return -ENXIO; |
| 5781 | } |
| 5782 | |
| 5783 | /* not reached */ |
| 5784 | } |
| 5785 | |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5786 | static int led_set_status(const unsigned int led, |
| 5787 | const enum led_status_t ledstatus) |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5788 | { |
| 5789 | /* off, on, blink. Index is led_status_t */ |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5790 | static const unsigned int led_sled_arg1[] = { 0, 1, 3 }; |
| 5791 | static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 }; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5792 | |
| 5793 | int rc = 0; |
| 5794 | |
| 5795 | switch (led_supported) { |
| 5796 | case TPACPI_LED_570: |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5797 | /* 570 */ |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5798 | if (unlikely(led > 7)) |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5799 | return -EINVAL; |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5800 | if (unlikely(tpacpi_is_led_restricted(led))) |
| 5801 | return -EPERM; |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5802 | if (!acpi_evalf(led_handle, NULL, NULL, "vdd", |
| 5803 | (1 << led), led_sled_arg1[ledstatus])) |
Pavel Machek | aac3311 | 2019-04-29 17:21:48 +0200 | [diff] [blame] | 5804 | return -EIO; |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5805 | break; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5806 | case TPACPI_LED_OLD: |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5807 | /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */ |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5808 | if (unlikely(led > 7)) |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5809 | return -EINVAL; |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5810 | if (unlikely(tpacpi_is_led_restricted(led))) |
| 5811 | return -EPERM; |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5812 | rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led)); |
| 5813 | if (rc >= 0) |
| 5814 | rc = ec_write(TPACPI_LED_EC_HLBL, |
| 5815 | (ledstatus == TPACPI_LED_BLINK) << led); |
| 5816 | if (rc >= 0) |
| 5817 | rc = ec_write(TPACPI_LED_EC_HLCL, |
| 5818 | (ledstatus != TPACPI_LED_OFF) << led); |
| 5819 | break; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5820 | case TPACPI_LED_NEW: |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5821 | /* all others */ |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5822 | if (unlikely(led >= TPACPI_LED_NUMLEDS)) |
| 5823 | return -EINVAL; |
| 5824 | if (unlikely(tpacpi_is_led_restricted(led))) |
| 5825 | return -EPERM; |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5826 | if (!acpi_evalf(led_handle, NULL, NULL, "vdd", |
| 5827 | led, led_led_arg1[ledstatus])) |
Pavel Machek | aac3311 | 2019-04-29 17:21:48 +0200 | [diff] [blame] | 5828 | return -EIO; |
Henrique de Moraes Holschuh | 24e45bb | 2008-06-03 23:36:11 -0300 | [diff] [blame] | 5829 | break; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5830 | default: |
Pavel Machek | aac3311 | 2019-04-29 17:21:48 +0200 | [diff] [blame] | 5831 | return -ENXIO; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5832 | } |
| 5833 | |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5834 | if (!rc) |
| 5835 | tpacpi_led_state_cache[led] = ledstatus; |
| 5836 | |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 5837 | return rc; |
| 5838 | } |
| 5839 | |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5840 | static int led_sysfs_set(struct led_classdev *led_cdev, |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5841 | enum led_brightness brightness) |
| 5842 | { |
| 5843 | struct tpacpi_led_classdev *data = container_of(led_cdev, |
| 5844 | struct tpacpi_led_classdev, led_classdev); |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5845 | enum led_status_t new_state; |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5846 | |
Henrique de Moraes Holschuh | 75bd3bf | 2009-04-14 02:44:11 +0000 | [diff] [blame] | 5847 | if (brightness == LED_OFF) |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5848 | new_state = TPACPI_LED_OFF; |
Henrique de Moraes Holschuh | 75bd3bf | 2009-04-14 02:44:11 +0000 | [diff] [blame] | 5849 | else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK) |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5850 | new_state = TPACPI_LED_ON; |
Henrique de Moraes Holschuh | 75bd3bf | 2009-04-14 02:44:11 +0000 | [diff] [blame] | 5851 | else |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5852 | new_state = TPACPI_LED_BLINK; |
Henrique de Moraes Holschuh | 75bd3bf | 2009-04-14 02:44:11 +0000 | [diff] [blame] | 5853 | |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5854 | return led_set_status(data->led, new_state); |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5855 | } |
| 5856 | |
| 5857 | static int led_sysfs_blink_set(struct led_classdev *led_cdev, |
| 5858 | unsigned long *delay_on, unsigned long *delay_off) |
| 5859 | { |
| 5860 | struct tpacpi_led_classdev *data = container_of(led_cdev, |
| 5861 | struct tpacpi_led_classdev, led_classdev); |
| 5862 | |
| 5863 | /* Can we choose the flash rate? */ |
| 5864 | if (*delay_on == 0 && *delay_off == 0) { |
| 5865 | /* yes. set them to the hardware blink rate (1 Hz) */ |
| 5866 | *delay_on = 500; /* ms */ |
| 5867 | *delay_off = 500; /* ms */ |
| 5868 | } else if ((*delay_on != 500) || (*delay_off != 500)) |
| 5869 | return -EINVAL; |
| 5870 | |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5871 | return led_set_status(data->led, TPACPI_LED_BLINK); |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5872 | } |
| 5873 | |
| 5874 | static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev) |
| 5875 | { |
| 5876 | int rc; |
| 5877 | |
| 5878 | struct tpacpi_led_classdev *data = container_of(led_cdev, |
| 5879 | struct tpacpi_led_classdev, led_classdev); |
| 5880 | |
| 5881 | rc = led_get_status(data->led); |
| 5882 | |
| 5883 | if (rc == TPACPI_LED_OFF || rc < 0) |
| 5884 | rc = LED_OFF; /* no error handling in led class :( */ |
| 5885 | else |
| 5886 | rc = LED_FULL; |
| 5887 | |
| 5888 | return rc; |
| 5889 | } |
| 5890 | |
| 5891 | static void led_exit(void) |
| 5892 | { |
| 5893 | unsigned int i; |
| 5894 | |
Andy Shevchenko | 41f8004 | 2020-05-07 19:37:23 +0300 | [diff] [blame] | 5895 | for (i = 0; i < TPACPI_LED_NUMLEDS; i++) |
| 5896 | led_classdev_unregister(&tpacpi_leds[i].led_classdev); |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5897 | |
| 5898 | kfree(tpacpi_leds); |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5899 | } |
| 5900 | |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5901 | static int __init tpacpi_init_led(unsigned int led) |
| 5902 | { |
Henrique de Moraes Holschuh | f21179a | 2009-05-30 13:25:08 -0300 | [diff] [blame] | 5903 | /* LEDs with no name don't get registered */ |
| 5904 | if (!tpacpi_led_names[led]) |
| 5905 | return 0; |
| 5906 | |
Hans de Goede | 86ec0c2 | 2017-02-09 16:44:12 +0100 | [diff] [blame] | 5907 | tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set; |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5908 | tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set; |
| 5909 | if (led_supported == TPACPI_LED_570) |
Andy Shevchenko | 41f8004 | 2020-05-07 19:37:23 +0300 | [diff] [blame] | 5910 | tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get; |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5911 | |
| 5912 | tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led]; |
Andy Shevchenko | 41f8004 | 2020-05-07 19:37:23 +0300 | [diff] [blame] | 5913 | tpacpi_leds[led].led = led; |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5914 | |
Andy Shevchenko | 41f8004 | 2020-05-07 19:37:23 +0300 | [diff] [blame] | 5915 | return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev); |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 5916 | } |
| 5917 | |
Henrique de Moraes Holschuh | f21179a | 2009-05-30 13:25:08 -0300 | [diff] [blame] | 5918 | static const struct tpacpi_quirk led_useful_qtable[] __initconst = { |
| 5919 | TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */ |
| 5920 | TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */ |
| 5921 | TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */ |
| 5922 | |
| 5923 | TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */ |
| 5924 | TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */ |
| 5925 | TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */ |
| 5926 | TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */ |
| 5927 | TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */ |
| 5928 | TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */ |
| 5929 | TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */ |
| 5930 | TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */ |
| 5931 | |
| 5932 | TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */ |
| 5933 | TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */ |
| 5934 | TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */ |
| 5935 | TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */ |
| 5936 | TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */ |
| 5937 | |
| 5938 | TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */ |
| 5939 | TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */ |
| 5940 | TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */ |
| 5941 | TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */ |
| 5942 | |
| 5943 | /* (1) - may have excess leds enabled on MSB */ |
| 5944 | |
| 5945 | /* Defaults (order matters, keep last, don't reorder!) */ |
| 5946 | { /* Lenovo */ |
| 5947 | .vendor = PCI_VENDOR_ID_LENOVO, |
| 5948 | .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY, |
| 5949 | .quirks = 0x1fffU, |
| 5950 | }, |
| 5951 | { /* IBM ThinkPads with no EC version string */ |
| 5952 | .vendor = PCI_VENDOR_ID_IBM, |
| 5953 | .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN, |
| 5954 | .quirks = 0x00ffU, |
| 5955 | }, |
| 5956 | { /* IBM ThinkPads with EC version string */ |
| 5957 | .vendor = PCI_VENDOR_ID_IBM, |
| 5958 | .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY, |
| 5959 | .quirks = 0x00bfU, |
| 5960 | }, |
| 5961 | }; |
| 5962 | |
Henrique de Moraes Holschuh | 2cbb5c8 | 2010-05-16 19:45:50 -0300 | [diff] [blame] | 5963 | static enum led_access_mode __init led_init_detect_mode(void) |
| 5964 | { |
| 5965 | acpi_status status; |
| 5966 | |
| 5967 | if (tpacpi_is_ibm()) { |
| 5968 | /* 570 */ |
| 5969 | status = acpi_get_handle(ec_handle, "SLED", &led_handle); |
| 5970 | if (ACPI_SUCCESS(status)) |
| 5971 | return TPACPI_LED_570; |
| 5972 | |
| 5973 | /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */ |
| 5974 | status = acpi_get_handle(ec_handle, "SYSL", &led_handle); |
| 5975 | if (ACPI_SUCCESS(status)) |
| 5976 | return TPACPI_LED_OLD; |
| 5977 | } |
| 5978 | |
| 5979 | /* most others */ |
| 5980 | status = acpi_get_handle(ec_handle, "LED", &led_handle); |
| 5981 | if (ACPI_SUCCESS(status)) |
| 5982 | return TPACPI_LED_NEW; |
| 5983 | |
| 5984 | /* R30, R31, and unknown firmwares */ |
| 5985 | led_handle = NULL; |
| 5986 | return TPACPI_LED_NONE; |
| 5987 | } |
| 5988 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 5989 | static int __init led_init(struct ibm_init_struct *iibm) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5990 | { |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5991 | unsigned int i; |
| 5992 | int rc; |
Henrique de Moraes Holschuh | f21179a | 2009-05-30 13:25:08 -0300 | [diff] [blame] | 5993 | unsigned long useful_leds; |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 5994 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 5995 | vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n"); |
| 5996 | |
Henrique de Moraes Holschuh | 2cbb5c8 | 2010-05-16 19:45:50 -0300 | [diff] [blame] | 5997 | led_supported = led_init_detect_mode(); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 5998 | |
Adam Lee | fcb44e1 | 2013-06-07 16:20:08 +0800 | [diff] [blame] | 5999 | if (led_supported != TPACPI_LED_NONE) { |
| 6000 | useful_leds = tpacpi_check_quirks(led_useful_qtable, |
| 6001 | ARRAY_SIZE(led_useful_qtable)); |
| 6002 | |
| 6003 | if (!useful_leds) { |
| 6004 | led_handle = NULL; |
| 6005 | led_supported = TPACPI_LED_NONE; |
| 6006 | } |
| 6007 | } |
| 6008 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 6009 | vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n", |
| 6010 | str_supported(led_supported), led_supported); |
| 6011 | |
Henrique de Moraes Holschuh | f21179a | 2009-05-30 13:25:08 -0300 | [diff] [blame] | 6012 | if (led_supported == TPACPI_LED_NONE) |
| 6013 | return 1; |
| 6014 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 6015 | tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds), |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 6016 | GFP_KERNEL); |
| 6017 | if (!tpacpi_leds) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 6018 | pr_err("Out of memory for LED data\n"); |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 6019 | return -ENOMEM; |
| 6020 | } |
| 6021 | |
| 6022 | for (i = 0; i < TPACPI_LED_NUMLEDS; i++) { |
Adam Lee | edf2d77 | 2013-06-08 16:51:15 +0800 | [diff] [blame] | 6023 | tpacpi_leds[i].led = -1; |
| 6024 | |
Andy Shevchenko | 41f8004 | 2020-05-07 19:37:23 +0300 | [diff] [blame] | 6025 | if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) { |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 6026 | rc = tpacpi_init_led(i); |
| 6027 | if (rc < 0) { |
| 6028 | led_exit(); |
| 6029 | return rc; |
| 6030 | } |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 6031 | } |
| 6032 | } |
| 6033 | |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 6034 | #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 6035 | pr_notice("warning: userspace override of important firmware LEDs is enabled\n"); |
Henrique de Moraes Holschuh | a4d5eff | 2009-04-04 04:25:49 +0000 | [diff] [blame] | 6036 | #endif |
Henrique de Moraes Holschuh | f21179a | 2009-05-30 13:25:08 -0300 | [diff] [blame] | 6037 | return 0; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6038 | } |
| 6039 | |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 6040 | #define str_led_status(s) \ |
| 6041 | ((s) == TPACPI_LED_OFF ? "off" : \ |
| 6042 | ((s) == TPACPI_LED_ON ? "on" : "blinking")) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6043 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6044 | static int led_read(struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | { |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6046 | if (!led_supported) { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6047 | seq_printf(m, "status:\t\tnot supported\n"); |
| 6048 | return 0; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6049 | } |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6050 | seq_printf(m, "status:\t\tsupported\n"); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6051 | |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 6052 | if (led_supported == TPACPI_LED_570) { |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6053 | /* 570 */ |
| 6054 | int i, status; |
| 6055 | for (i = 0; i < 8; i++) { |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 6056 | status = led_get_status(i); |
| 6057 | if (status < 0) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6058 | return -EIO; |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6059 | seq_printf(m, "%d:\t\t%s\n", |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 6060 | i, str_led_status(status)); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6061 | } |
| 6062 | } |
| 6063 | |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 6064 | seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6066 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | } |
| 6068 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6069 | static int led_write(char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | { |
| 6071 | char *cmd; |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 6072 | int led, rc; |
| 6073 | enum led_status_t s; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6074 | |
| 6075 | if (!led_supported) |
| 6076 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 6078 | while ((cmd = strsep(&buf, ","))) { |
Adam Lee | edf2d77 | 2013-06-08 16:51:15 +0800 | [diff] [blame] | 6079 | if (sscanf(cmd, "%d", &led) != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | return -EINVAL; |
| 6081 | |
Andy Shevchenko | 41f8004 | 2020-05-07 19:37:23 +0300 | [diff] [blame] | 6082 | if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1)) |
| 6083 | return -ENODEV; |
| 6084 | |
| 6085 | if (tpacpi_leds[led].led < 0) |
Adam Lee | edf2d77 | 2013-06-08 16:51:15 +0800 | [diff] [blame] | 6086 | return -ENODEV; |
| 6087 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6088 | if (strstr(cmd, "off")) { |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 6089 | s = TPACPI_LED_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6090 | } else if (strstr(cmd, "on")) { |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 6091 | s = TPACPI_LED_ON; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6092 | } else if (strstr(cmd, "blink")) { |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 6093 | s = TPACPI_LED_BLINK; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6094 | } else { |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 6095 | return -EINVAL; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6096 | } |
Henrique de Moraes Holschuh | 4fa6811 | 2008-04-26 01:02:23 -0300 | [diff] [blame] | 6097 | |
| 6098 | rc = led_set_status(led, s); |
| 6099 | if (rc < 0) |
| 6100 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6101 | } |
| 6102 | |
| 6103 | return 0; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6104 | } |
| 6105 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 6106 | static struct ibm_struct led_driver_data = { |
| 6107 | .name = "led", |
| 6108 | .read = led_read, |
| 6109 | .write = led_write, |
Henrique de Moraes Holschuh | af11610 | 2008-04-26 01:02:25 -0300 | [diff] [blame] | 6110 | .exit = led_exit, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 6111 | }; |
| 6112 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 6113 | /************************************************************************* |
| 6114 | * Beep subdriver |
| 6115 | */ |
| 6116 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 6117 | TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */ |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 6118 | |
Henrique de Moraes Holschuh | 6020173 | 2009-05-30 13:25:07 -0300 | [diff] [blame] | 6119 | #define TPACPI_BEEP_Q1 0x0001 |
| 6120 | |
| 6121 | static const struct tpacpi_quirk beep_quirk_table[] __initconst = { |
| 6122 | TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */ |
| 6123 | TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */ |
| 6124 | }; |
| 6125 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 6126 | static int __init beep_init(struct ibm_init_struct *iibm) |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 6127 | { |
Henrique de Moraes Holschuh | 6020173 | 2009-05-30 13:25:07 -0300 | [diff] [blame] | 6128 | unsigned long quirks; |
| 6129 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 6130 | vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n"); |
| 6131 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 6132 | TPACPI_ACPIHANDLE_INIT(beep); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 6133 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 6134 | vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n", |
| 6135 | str_supported(beep_handle != NULL)); |
| 6136 | |
Henrique de Moraes Holschuh | 6020173 | 2009-05-30 13:25:07 -0300 | [diff] [blame] | 6137 | quirks = tpacpi_check_quirks(beep_quirk_table, |
| 6138 | ARRAY_SIZE(beep_quirk_table)); |
| 6139 | |
| 6140 | tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1); |
| 6141 | |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 6142 | return (beep_handle) ? 0 : 1; |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 6143 | } |
| 6144 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6145 | static int beep_read(struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | { |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6147 | if (!beep_handle) |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6148 | seq_printf(m, "status:\t\tnot supported\n"); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6149 | else { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6150 | seq_printf(m, "status:\t\tsupported\n"); |
| 6151 | seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n"); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6153 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6154 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6155 | } |
| 6156 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6157 | static int beep_write(char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | { |
| 6159 | char *cmd; |
| 6160 | int beep_cmd; |
| 6161 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6162 | if (!beep_handle) |
| 6163 | return -ENODEV; |
| 6164 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 6165 | while ((cmd = strsep(&buf, ","))) { |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6166 | if (sscanf(cmd, "%u", &beep_cmd) == 1 && |
| 6167 | beep_cmd >= 0 && beep_cmd <= 17) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | /* beep_cmd set */ |
| 6169 | } else |
| 6170 | return -EINVAL; |
Henrique de Moraes Holschuh | 6020173 | 2009-05-30 13:25:07 -0300 | [diff] [blame] | 6171 | if (tp_features.beep_needs_two_args) { |
| 6172 | if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", |
| 6173 | beep_cmd, 0)) |
| 6174 | return -EIO; |
| 6175 | } else { |
| 6176 | if (!acpi_evalf(beep_handle, NULL, NULL, "vd", |
| 6177 | beep_cmd)) |
| 6178 | return -EIO; |
| 6179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6180 | } |
| 6181 | |
| 6182 | return 0; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6183 | } |
| 6184 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 6185 | static struct ibm_struct beep_driver_data = { |
| 6186 | .name = "beep", |
| 6187 | .read = beep_read, |
| 6188 | .write = beep_write, |
| 6189 | }; |
| 6190 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 6191 | /************************************************************************* |
| 6192 | * Thermal subdriver |
| 6193 | */ |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6194 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 6195 | enum thermal_access_mode { |
| 6196 | TPACPI_THERMAL_NONE = 0, /* No thermal support */ |
| 6197 | TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */ |
| 6198 | TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */ |
| 6199 | TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */ |
| 6200 | TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */ |
| 6201 | }; |
| 6202 | |
| 6203 | enum { /* TPACPI_THERMAL_TPEC_* */ |
| 6204 | TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */ |
| 6205 | TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */ |
Mark Pearson | 6759e18 | 2021-04-07 17:20:15 -0400 | [diff] [blame] | 6206 | TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */ |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 6207 | TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */ |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 6208 | |
| 6209 | TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */ |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 6210 | }; |
| 6211 | |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 6212 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 6213 | #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */ |
| 6214 | struct ibm_thermal_sensors_struct { |
| 6215 | s32 temp[TPACPI_MAX_THERMAL_SENSORS]; |
| 6216 | }; |
| 6217 | |
Henrique de Moraes Holschuh | a26f878 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6218 | static enum thermal_access_mode thermal_read_mode; |
Hans de Goede | 00cc426 | 2021-04-13 09:21:12 +0200 | [diff] [blame] | 6219 | static const struct attribute_group *thermal_attr_group; |
| 6220 | static bool thermal_use_labels; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6221 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6222 | /* idx is zero-based */ |
| 6223 | static int thermal_get_sensor(int idx, s32 *value) |
| 6224 | { |
| 6225 | int t; |
| 6226 | s8 tmp; |
| 6227 | char tmpi[5]; |
| 6228 | |
| 6229 | t = TP_EC_THERMAL_TMP0; |
| 6230 | |
| 6231 | switch (thermal_read_mode) { |
| 6232 | #if TPACPI_MAX_THERMAL_SENSORS >= 16 |
| 6233 | case TPACPI_THERMAL_TPEC_16: |
| 6234 | if (idx >= 8 && idx <= 15) { |
| 6235 | t = TP_EC_THERMAL_TMP8; |
| 6236 | idx -= 8; |
| 6237 | } |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6238 | #endif |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 6239 | fallthrough; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6240 | case TPACPI_THERMAL_TPEC_8: |
| 6241 | if (idx <= 7) { |
| 6242 | if (!acpi_ec_read(t + idx, &tmp)) |
| 6243 | return -EIO; |
| 6244 | *value = tmp * 1000; |
| 6245 | return 0; |
| 6246 | } |
| 6247 | break; |
| 6248 | |
| 6249 | case TPACPI_THERMAL_ACPI_UPDT: |
| 6250 | if (idx <= 7) { |
| 6251 | snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx); |
| 6252 | if (!acpi_evalf(ec_handle, NULL, "UPDT", "v")) |
| 6253 | return -EIO; |
| 6254 | if (!acpi_evalf(ec_handle, &t, tmpi, "d")) |
| 6255 | return -EIO; |
| 6256 | *value = (t - 2732) * 100; |
| 6257 | return 0; |
| 6258 | } |
| 6259 | break; |
| 6260 | |
| 6261 | case TPACPI_THERMAL_ACPI_TMP07: |
| 6262 | if (idx <= 7) { |
| 6263 | snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx); |
| 6264 | if (!acpi_evalf(ec_handle, &t, tmpi, "d")) |
| 6265 | return -EIO; |
| 6266 | if (t > 127 || t < -127) |
| 6267 | t = TP_EC_THERMAL_TMP_NA; |
| 6268 | *value = t * 1000; |
| 6269 | return 0; |
| 6270 | } |
| 6271 | break; |
| 6272 | |
| 6273 | case TPACPI_THERMAL_NONE: |
| 6274 | default: |
| 6275 | return -ENOSYS; |
| 6276 | } |
| 6277 | |
| 6278 | return -EINVAL; |
| 6279 | } |
| 6280 | |
| 6281 | static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s) |
| 6282 | { |
| 6283 | int res, i; |
| 6284 | int n; |
| 6285 | |
| 6286 | n = 8; |
| 6287 | i = 0; |
| 6288 | |
| 6289 | if (!s) |
| 6290 | return -EINVAL; |
| 6291 | |
| 6292 | if (thermal_read_mode == TPACPI_THERMAL_TPEC_16) |
| 6293 | n = 16; |
| 6294 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 6295 | for (i = 0 ; i < n; i++) { |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6296 | res = thermal_get_sensor(i, &s->temp[i]); |
| 6297 | if (res) |
| 6298 | return res; |
| 6299 | } |
| 6300 | |
| 6301 | return n; |
| 6302 | } |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 6303 | |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 6304 | static void thermal_dump_all_sensors(void) |
| 6305 | { |
| 6306 | int n, i; |
| 6307 | struct ibm_thermal_sensors_struct t; |
| 6308 | |
| 6309 | n = thermal_get_sensors(&t); |
| 6310 | if (n <= 0) |
| 6311 | return; |
| 6312 | |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 6313 | pr_notice("temperatures (Celsius):"); |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 6314 | |
| 6315 | for (i = 0; i < n; i++) { |
| 6316 | if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 6317 | pr_cont(" %d", (int)(t.temp[i] / 1000)); |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 6318 | else |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 6319 | pr_cont(" N/A"); |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 6320 | } |
| 6321 | |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 6322 | pr_cont("\n"); |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 6323 | } |
| 6324 | |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6325 | /* sysfs temp##_input -------------------------------------------------- */ |
| 6326 | |
| 6327 | static ssize_t thermal_temp_input_show(struct device *dev, |
| 6328 | struct device_attribute *attr, |
| 6329 | char *buf) |
| 6330 | { |
| 6331 | struct sensor_device_attribute *sensor_attr = |
| 6332 | to_sensor_dev_attr(attr); |
| 6333 | int idx = sensor_attr->index; |
| 6334 | s32 value; |
| 6335 | int res; |
| 6336 | |
| 6337 | res = thermal_get_sensor(idx, &value); |
| 6338 | if (res) |
| 6339 | return res; |
Henrique de Moraes Holschuh | 9ebd9e8 | 2009-12-09 01:36:28 +0000 | [diff] [blame] | 6340 | if (value == TPACPI_THERMAL_SENSOR_NA) |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6341 | return -ENXIO; |
| 6342 | |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 6343 | return sysfs_emit(buf, "%d\n", value); |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6344 | } |
| 6345 | |
| 6346 | #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \ |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 6347 | SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \ |
| 6348 | thermal_temp_input_show, NULL, _idxB) |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6349 | |
| 6350 | static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = { |
| 6351 | THERMAL_SENSOR_ATTR_TEMP(1, 0), |
| 6352 | THERMAL_SENSOR_ATTR_TEMP(2, 1), |
| 6353 | THERMAL_SENSOR_ATTR_TEMP(3, 2), |
| 6354 | THERMAL_SENSOR_ATTR_TEMP(4, 3), |
| 6355 | THERMAL_SENSOR_ATTR_TEMP(5, 4), |
| 6356 | THERMAL_SENSOR_ATTR_TEMP(6, 5), |
| 6357 | THERMAL_SENSOR_ATTR_TEMP(7, 6), |
| 6358 | THERMAL_SENSOR_ATTR_TEMP(8, 7), |
| 6359 | THERMAL_SENSOR_ATTR_TEMP(9, 8), |
| 6360 | THERMAL_SENSOR_ATTR_TEMP(10, 9), |
| 6361 | THERMAL_SENSOR_ATTR_TEMP(11, 10), |
| 6362 | THERMAL_SENSOR_ATTR_TEMP(12, 11), |
| 6363 | THERMAL_SENSOR_ATTR_TEMP(13, 12), |
| 6364 | THERMAL_SENSOR_ATTR_TEMP(14, 13), |
| 6365 | THERMAL_SENSOR_ATTR_TEMP(15, 14), |
| 6366 | THERMAL_SENSOR_ATTR_TEMP(16, 15), |
| 6367 | }; |
| 6368 | |
| 6369 | #define THERMAL_ATTRS(X) \ |
| 6370 | &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr |
| 6371 | |
| 6372 | static struct attribute *thermal_temp_input_attr[] = { |
| 6373 | THERMAL_ATTRS(8), |
| 6374 | THERMAL_ATTRS(9), |
| 6375 | THERMAL_ATTRS(10), |
| 6376 | THERMAL_ATTRS(11), |
| 6377 | THERMAL_ATTRS(12), |
| 6378 | THERMAL_ATTRS(13), |
| 6379 | THERMAL_ATTRS(14), |
| 6380 | THERMAL_ATTRS(15), |
| 6381 | THERMAL_ATTRS(0), |
| 6382 | THERMAL_ATTRS(1), |
| 6383 | THERMAL_ATTRS(2), |
| 6384 | THERMAL_ATTRS(3), |
| 6385 | THERMAL_ATTRS(4), |
| 6386 | THERMAL_ATTRS(5), |
| 6387 | THERMAL_ATTRS(6), |
| 6388 | THERMAL_ATTRS(7), |
| 6389 | NULL |
| 6390 | }; |
| 6391 | |
| 6392 | static const struct attribute_group thermal_temp_input16_group = { |
| 6393 | .attrs = thermal_temp_input_attr |
| 6394 | }; |
| 6395 | |
| 6396 | static const struct attribute_group thermal_temp_input8_group = { |
| 6397 | .attrs = &thermal_temp_input_attr[8] |
| 6398 | }; |
| 6399 | |
| 6400 | #undef THERMAL_SENSOR_ATTR_TEMP |
| 6401 | #undef THERMAL_ATTRS |
| 6402 | |
Hans de Goede | 00cc426 | 2021-04-13 09:21:12 +0200 | [diff] [blame] | 6403 | static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 6404 | { |
| 6405 | return sysfs_emit(buf, "CPU\n"); |
| 6406 | } |
| 6407 | static DEVICE_ATTR_RO(temp1_label); |
| 6408 | |
| 6409 | static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 6410 | { |
| 6411 | return sysfs_emit(buf, "GPU\n"); |
| 6412 | } |
| 6413 | static DEVICE_ATTR_RO(temp2_label); |
| 6414 | |
| 6415 | static struct attribute *temp_label_attributes[] = { |
| 6416 | &dev_attr_temp1_label.attr, |
| 6417 | &dev_attr_temp2_label.attr, |
| 6418 | NULL |
| 6419 | }; |
| 6420 | |
| 6421 | static const struct attribute_group temp_label_attr_group = { |
| 6422 | .attrs = temp_label_attributes, |
| 6423 | }; |
| 6424 | |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6425 | /* --------------------------------------------------------------------- */ |
| 6426 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 6427 | static int __init thermal_init(struct ibm_init_struct *iibm) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6428 | { |
Mark Pearson | 6759e18 | 2021-04-07 17:20:15 -0400 | [diff] [blame] | 6429 | u8 t, ta1, ta2, ver = 0; |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6430 | int i; |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 6431 | int acpi_tmp7; |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6432 | int res; |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 6433 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 6434 | vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n"); |
| 6435 | |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 6436 | acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv"); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6437 | |
Henrique de Moraes Holschuh | 3d6f99c | 2007-07-18 23:45:46 -0300 | [diff] [blame] | 6438 | if (thinkpad_id.ec_model) { |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6439 | /* |
| 6440 | * Direct EC access mode: sensors at registers |
| 6441 | * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for |
| 6442 | * non-implemented, thermal sensors return 0x80 when |
| 6443 | * not available |
Mark Pearson | 6759e18 | 2021-04-07 17:20:15 -0400 | [diff] [blame] | 6444 | * The above rule is unfortunately flawed. This has been seen with |
| 6445 | * 0xC2 (power supply ID) causing thermal control problems. |
| 6446 | * The EC version can be determined by offset 0xEF and at least for |
| 6447 | * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7 |
| 6448 | * are not thermal registers. |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6449 | */ |
Mark Pearson | 6759e18 | 2021-04-07 17:20:15 -0400 | [diff] [blame] | 6450 | if (!acpi_ec_read(TP_EC_FUNCREV, &ver)) |
| 6451 | pr_warn("Thinkpad ACPI EC unable to access EC version\n"); |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6452 | |
| 6453 | ta1 = ta2 = 0; |
| 6454 | for (i = 0; i < 8; i++) { |
Henrique de Moraes Holschuh | 04cc862 | 2007-04-21 11:08:43 -0300 | [diff] [blame] | 6455 | if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) { |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6456 | ta1 |= t; |
| 6457 | } else { |
| 6458 | ta1 = 0; |
| 6459 | break; |
| 6460 | } |
Mark Pearson | 6759e18 | 2021-04-07 17:20:15 -0400 | [diff] [blame] | 6461 | if (ver < 3) { |
| 6462 | if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) { |
| 6463 | ta2 |= t; |
| 6464 | } else { |
| 6465 | ta1 = 0; |
| 6466 | break; |
| 6467 | } |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6468 | } |
| 6469 | } |
| 6470 | if (ta1 == 0) { |
| 6471 | /* This is sheer paranoia, but we handle it anyway */ |
| 6472 | if (acpi_tmp7) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 6473 | pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n"); |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 6474 | thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07; |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6475 | } else { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 6476 | pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n"); |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 6477 | thermal_read_mode = TPACPI_THERMAL_NONE; |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6478 | } |
| 6479 | } else { |
Hans de Goede | 00cc426 | 2021-04-13 09:21:12 +0200 | [diff] [blame] | 6480 | if (ver >= 3) { |
Mark Pearson | 6759e18 | 2021-04-07 17:20:15 -0400 | [diff] [blame] | 6481 | thermal_read_mode = TPACPI_THERMAL_TPEC_8; |
Hans de Goede | 00cc426 | 2021-04-13 09:21:12 +0200 | [diff] [blame] | 6482 | thermal_use_labels = true; |
| 6483 | } else { |
Mark Pearson | 6759e18 | 2021-04-07 17:20:15 -0400 | [diff] [blame] | 6484 | thermal_read_mode = |
| 6485 | (ta2 != 0) ? |
| 6486 | TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8; |
Hans de Goede | 00cc426 | 2021-04-13 09:21:12 +0200 | [diff] [blame] | 6487 | } |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6488 | } |
| 6489 | } else if (acpi_tmp7) { |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 6490 | if (tpacpi_is_ibm() && |
| 6491 | acpi_evalf(ec_handle, NULL, "UPDT", "qv")) { |
Henrique de Moraes Holschuh | a26f878 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6492 | /* 600e/x, 770e, 770x */ |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 6493 | thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT; |
Henrique de Moraes Holschuh | a26f878 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6494 | } else { |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 6495 | /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */ |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 6496 | thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07; |
Henrique de Moraes Holschuh | a26f878 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6497 | } |
| 6498 | } else { |
| 6499 | /* temperatures not supported on 570, G4x, R30, R31, R32 */ |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 6500 | thermal_read_mode = TPACPI_THERMAL_NONE; |
Henrique de Moraes Holschuh | a26f878 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6501 | } |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6502 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 6503 | vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n", |
| 6504 | str_supported(thermal_read_mode != TPACPI_THERMAL_NONE), |
| 6505 | thermal_read_mode); |
| 6506 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 6507 | switch (thermal_read_mode) { |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6508 | case TPACPI_THERMAL_TPEC_16: |
Hans de Goede | 00cc426 | 2021-04-13 09:21:12 +0200 | [diff] [blame] | 6509 | thermal_attr_group = &thermal_temp_input16_group; |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6510 | break; |
| 6511 | case TPACPI_THERMAL_TPEC_8: |
| 6512 | case TPACPI_THERMAL_ACPI_TMP07: |
| 6513 | case TPACPI_THERMAL_ACPI_UPDT: |
Hans de Goede | 00cc426 | 2021-04-13 09:21:12 +0200 | [diff] [blame] | 6514 | thermal_attr_group = &thermal_temp_input8_group; |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6515 | break; |
| 6516 | case TPACPI_THERMAL_NONE: |
| 6517 | default: |
| 6518 | return 1; |
| 6519 | } |
| 6520 | |
Hans de Goede | 00cc426 | 2021-04-13 09:21:12 +0200 | [diff] [blame] | 6521 | res = sysfs_create_group(&tpacpi_hwmon->kobj, thermal_attr_group); |
| 6522 | if (res) |
| 6523 | return res; |
| 6524 | |
| 6525 | if (thermal_use_labels) { |
| 6526 | res = sysfs_create_group(&tpacpi_hwmon->kobj, &temp_label_attr_group); |
| 6527 | if (res) { |
| 6528 | sysfs_remove_group(&tpacpi_hwmon->kobj, thermal_attr_group); |
| 6529 | return res; |
| 6530 | } |
| 6531 | } |
| 6532 | |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6533 | return 0; |
| 6534 | } |
| 6535 | |
| 6536 | static void thermal_exit(void) |
| 6537 | { |
Hans de Goede | 00cc426 | 2021-04-13 09:21:12 +0200 | [diff] [blame] | 6538 | if (thermal_attr_group) |
| 6539 | sysfs_remove_group(&tpacpi_hwmon->kobj, thermal_attr_group); |
| 6540 | |
| 6541 | if (thermal_use_labels) |
| 6542 | sysfs_remove_group(&tpacpi_hwmon->kobj, &temp_label_attr_group); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6543 | } |
| 6544 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6545 | static int thermal_read(struct seq_file *m) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6546 | { |
Henrique de Moraes Holschuh | a26f878 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6547 | int n, i; |
| 6548 | struct ibm_thermal_sensors_struct t; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6549 | |
Henrique de Moraes Holschuh | a26f878 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6550 | n = thermal_get_sensors(&t); |
| 6551 | if (unlikely(n < 0)) |
| 6552 | return n; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6553 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6554 | seq_printf(m, "temperatures:\t"); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6555 | |
Henrique de Moraes Holschuh | a26f878 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6556 | if (n > 0) { |
| 6557 | for (i = 0; i < (n - 1); i++) |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6558 | seq_printf(m, "%d ", t.temp[i] / 1000); |
| 6559 | seq_printf(m, "%d\n", t.temp[i] / 1000); |
Henrique de Moraes Holschuh | a26f878 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 6560 | } else |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6561 | seq_printf(m, "not supported\n"); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6562 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 6563 | return 0; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 6564 | } |
| 6565 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 6566 | static struct ibm_struct thermal_driver_data = { |
| 6567 | .name = "thermal", |
| 6568 | .read = thermal_read, |
Henrique de Moraes Holschuh | 2c37aa4 | 2007-04-24 11:48:16 -0300 | [diff] [blame] | 6569 | .exit = thermal_exit, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 6570 | }; |
| 6571 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 6572 | /************************************************************************* |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 6573 | * Backlight/brightness subdriver |
| 6574 | */ |
Holger Macht | 8acb025 | 2006-10-20 14:30:28 -0700 | [diff] [blame] | 6575 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 6576 | #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen" |
| 6577 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6578 | /* |
| 6579 | * ThinkPads can read brightness from two places: EC HBRV (0x31), or |
| 6580 | * CMOS NVRAM byte 0x5E, bits 0-3. |
| 6581 | * |
| 6582 | * EC HBRV (0x31) has the following layout |
| 6583 | * Bit 7: unknown function |
| 6584 | * Bit 6: unknown function |
| 6585 | * Bit 5: Z: honour scale changes, NZ: ignore scale changes |
| 6586 | * Bit 4: must be set to zero to avoid problems |
| 6587 | * Bit 3-0: backlight brightness level |
| 6588 | * |
| 6589 | * brightness_get_raw returns status data in the HBRV layout |
Henrique de Moraes Holschuh | d7880f1 | 2009-06-18 00:40:16 -0300 | [diff] [blame] | 6590 | * |
| 6591 | * WARNING: The X61 has been verified to use HBRV for something else, so |
| 6592 | * this should be used _only_ on IBM ThinkPads, and maybe with some careful |
| 6593 | * testing on the very early *60 Lenovo models... |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6594 | */ |
| 6595 | |
Henrique de Moraes Holschuh | e11aecf | 2008-04-26 01:02:21 -0300 | [diff] [blame] | 6596 | enum { |
| 6597 | TP_EC_BACKLIGHT = 0x31, |
| 6598 | |
| 6599 | /* TP_EC_BACKLIGHT bitmasks */ |
| 6600 | TP_EC_BACKLIGHT_LVLMSK = 0x1F, |
| 6601 | TP_EC_BACKLIGHT_CMDMSK = 0xE0, |
| 6602 | TP_EC_BACKLIGHT_MAPSW = 0x20, |
| 6603 | }; |
| 6604 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6605 | enum tpacpi_brightness_access_mode { |
| 6606 | TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */ |
| 6607 | TPACPI_BRGHT_MODE_EC, /* EC control */ |
| 6608 | TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */ |
| 6609 | TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */ |
| 6610 | TPACPI_BRGHT_MODE_MAX |
| 6611 | }; |
| 6612 | |
Henrique de Moraes Holschuh | 94954cc6 | 2007-07-18 23:45:27 -0300 | [diff] [blame] | 6613 | static struct backlight_device *ibm_backlight_device; |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6614 | |
| 6615 | static enum tpacpi_brightness_access_mode brightness_mode = |
| 6616 | TPACPI_BRGHT_MODE_MAX; |
| 6617 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 6618 | static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */ |
| 6619 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6620 | static struct mutex brightness_mutex; |
| 6621 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6622 | /* NVRAM brightness access, |
| 6623 | * call with brightness_mutex held! */ |
| 6624 | static unsigned int tpacpi_brightness_nvram_get(void) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6625 | { |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6626 | u8 lnvram; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6627 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6628 | lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS) |
| 6629 | & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) |
| 6630 | >> TP_NVRAM_POS_LEVEL_BRIGHTNESS; |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6631 | lnvram &= bright_maxlvl; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6632 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6633 | return lnvram; |
| 6634 | } |
| 6635 | |
| 6636 | static void tpacpi_brightness_checkpoint_nvram(void) |
| 6637 | { |
| 6638 | u8 lec = 0; |
| 6639 | u8 b_nvram; |
| 6640 | |
| 6641 | if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM) |
| 6642 | return; |
| 6643 | |
| 6644 | vdbg_printk(TPACPI_DBG_BRGHT, |
| 6645 | "trying to checkpoint backlight level to NVRAM...\n"); |
| 6646 | |
| 6647 | if (mutex_lock_killable(&brightness_mutex) < 0) |
| 6648 | return; |
| 6649 | |
| 6650 | if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec))) |
| 6651 | goto unlock; |
| 6652 | lec &= TP_EC_BACKLIGHT_LVLMSK; |
| 6653 | b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS); |
| 6654 | |
| 6655 | if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) |
| 6656 | >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) { |
| 6657 | /* NVRAM needs update */ |
| 6658 | b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS << |
| 6659 | TP_NVRAM_POS_LEVEL_BRIGHTNESS); |
| 6660 | b_nvram |= lec; |
| 6661 | nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS); |
| 6662 | dbg_printk(TPACPI_DBG_BRGHT, |
| 6663 | "updated NVRAM backlight level to %u (0x%02x)\n", |
| 6664 | (unsigned int) lec, (unsigned int) b_nvram); |
| 6665 | } else |
| 6666 | vdbg_printk(TPACPI_DBG_BRGHT, |
| 6667 | "NVRAM backlight level already is %u (0x%02x)\n", |
| 6668 | (unsigned int) lec, (unsigned int) b_nvram); |
| 6669 | |
| 6670 | unlock: |
| 6671 | mutex_unlock(&brightness_mutex); |
| 6672 | } |
| 6673 | |
| 6674 | |
| 6675 | /* call with brightness_mutex held! */ |
| 6676 | static int tpacpi_brightness_get_raw(int *status) |
| 6677 | { |
| 6678 | u8 lec = 0; |
| 6679 | |
| 6680 | switch (brightness_mode) { |
| 6681 | case TPACPI_BRGHT_MODE_UCMS_STEP: |
| 6682 | *status = tpacpi_brightness_nvram_get(); |
| 6683 | return 0; |
| 6684 | case TPACPI_BRGHT_MODE_EC: |
| 6685 | case TPACPI_BRGHT_MODE_ECNVRAM: |
| 6686 | if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec))) |
Henrique de Moraes Holschuh | 2d5e94d | 2008-04-26 01:02:20 -0300 | [diff] [blame] | 6687 | return -EIO; |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6688 | *status = lec; |
| 6689 | return 0; |
| 6690 | default: |
| 6691 | return -ENXIO; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6692 | } |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
| 6695 | /* call with brightness_mutex held! */ |
| 6696 | /* do NOT call with illegal backlight level value */ |
| 6697 | static int tpacpi_brightness_set_ec(unsigned int value) |
| 6698 | { |
| 6699 | u8 lec = 0; |
| 6700 | |
| 6701 | if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec))) |
| 6702 | return -EIO; |
| 6703 | |
| 6704 | if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT, |
| 6705 | (lec & TP_EC_BACKLIGHT_CMDMSK) | |
| 6706 | (value & TP_EC_BACKLIGHT_LVLMSK)))) |
| 6707 | return -EIO; |
| 6708 | |
| 6709 | return 0; |
| 6710 | } |
| 6711 | |
| 6712 | /* call with brightness_mutex held! */ |
| 6713 | static int tpacpi_brightness_set_ucmsstep(unsigned int value) |
| 6714 | { |
| 6715 | int cmos_cmd, inc; |
| 6716 | unsigned int current_value, i; |
| 6717 | |
| 6718 | current_value = tpacpi_brightness_nvram_get(); |
| 6719 | |
| 6720 | if (value == current_value) |
| 6721 | return 0; |
| 6722 | |
| 6723 | cmos_cmd = (value > current_value) ? |
| 6724 | TP_CMOS_BRIGHTNESS_UP : |
| 6725 | TP_CMOS_BRIGHTNESS_DOWN; |
| 6726 | inc = (value > current_value) ? 1 : -1; |
| 6727 | |
| 6728 | for (i = current_value; i != value; i += inc) |
| 6729 | if (issue_thinkpad_cmos_command(cmos_cmd)) |
| 6730 | return -EIO; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6731 | |
Henrique de Moraes Holschuh | e11aecf | 2008-04-26 01:02:21 -0300 | [diff] [blame] | 6732 | return 0; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6733 | } |
| 6734 | |
| 6735 | /* May return EINTR which can always be mapped to ERESTARTSYS */ |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6736 | static int brightness_set(unsigned int value) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6737 | { |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6738 | int res; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6739 | |
Andrey Utkin | bd3c7b9e | 2014-07-15 01:56:21 +0300 | [diff] [blame] | 6740 | if (value > bright_maxlvl) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6741 | return -EINVAL; |
| 6742 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6743 | vdbg_printk(TPACPI_DBG_BRGHT, |
| 6744 | "set backlight level to %d\n", value); |
| 6745 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 6746 | res = mutex_lock_killable(&brightness_mutex); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6747 | if (res < 0) |
| 6748 | return res; |
| 6749 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6750 | switch (brightness_mode) { |
| 6751 | case TPACPI_BRGHT_MODE_EC: |
| 6752 | case TPACPI_BRGHT_MODE_ECNVRAM: |
| 6753 | res = tpacpi_brightness_set_ec(value); |
| 6754 | break; |
| 6755 | case TPACPI_BRGHT_MODE_UCMS_STEP: |
| 6756 | res = tpacpi_brightness_set_ucmsstep(value); |
| 6757 | break; |
| 6758 | default: |
| 6759 | res = -ENXIO; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6760 | } |
| 6761 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6762 | mutex_unlock(&brightness_mutex); |
| 6763 | return res; |
| 6764 | } |
| 6765 | |
| 6766 | /* sysfs backlight class ----------------------------------------------- */ |
| 6767 | |
| 6768 | static int brightness_update_status(struct backlight_device *bd) |
| 6769 | { |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6770 | unsigned int level = |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6771 | (bd->props.fb_blank == FB_BLANK_UNBLANK && |
| 6772 | bd->props.power == FB_BLANK_UNBLANK) ? |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6773 | bd->props.brightness : 0; |
| 6774 | |
| 6775 | dbg_printk(TPACPI_DBG_BRGHT, |
| 6776 | "backlight: attempt to set level to %d\n", |
| 6777 | level); |
| 6778 | |
| 6779 | /* it is the backlight class's job (caller) to handle |
| 6780 | * EINTR and other errors properly */ |
| 6781 | return brightness_set(level); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6782 | } |
Holger Macht | 8acb025 | 2006-10-20 14:30:28 -0700 | [diff] [blame] | 6783 | |
Henrique de Moraes Holschuh | e11aecf | 2008-04-26 01:02:21 -0300 | [diff] [blame] | 6784 | static int brightness_get(struct backlight_device *bd) |
| 6785 | { |
| 6786 | int status, res; |
| 6787 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6788 | res = mutex_lock_killable(&brightness_mutex); |
Henrique de Moraes Holschuh | e11aecf | 2008-04-26 01:02:21 -0300 | [diff] [blame] | 6789 | if (res < 0) |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6790 | return 0; |
| 6791 | |
| 6792 | res = tpacpi_brightness_get_raw(&status); |
| 6793 | |
| 6794 | mutex_unlock(&brightness_mutex); |
| 6795 | |
| 6796 | if (res < 0) |
| 6797 | return 0; |
Henrique de Moraes Holschuh | e11aecf | 2008-04-26 01:02:21 -0300 | [diff] [blame] | 6798 | |
| 6799 | return status & TP_EC_BACKLIGHT_LVLMSK; |
| 6800 | } |
| 6801 | |
Henrique de Moraes Holschuh | 347a268 | 2009-12-09 01:36:24 +0000 | [diff] [blame] | 6802 | static void tpacpi_brightness_notify_change(void) |
| 6803 | { |
| 6804 | backlight_force_update(ibm_backlight_device, |
| 6805 | BACKLIGHT_UPDATE_HOTKEY); |
| 6806 | } |
| 6807 | |
Lionel Debroux | acc2472 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 6808 | static const struct backlight_ops ibm_backlight_data = { |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 6809 | .get_brightness = brightness_get, |
| 6810 | .update_status = brightness_update_status, |
Henrique de Moraes Holschuh | fb87a81 | 2006-11-25 16:35:09 -0200 | [diff] [blame] | 6811 | }; |
| 6812 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 6813 | /* --------------------------------------------------------------------- */ |
Henrique de Moraes Holschuh | f432255 | 2007-07-18 23:45:48 -0300 | [diff] [blame] | 6814 | |
Henrique de Moraes Holschuh | 122f267 | 2010-08-09 23:48:18 -0300 | [diff] [blame] | 6815 | /* |
| 6816 | * Call _BCL method of video device. On some ThinkPads this will |
| 6817 | * switch the firmware to the ACPI brightness control mode. |
| 6818 | */ |
| 6819 | |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6820 | static int __init tpacpi_query_bcl_levels(acpi_handle handle) |
| 6821 | { |
| 6822 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 6823 | union acpi_object *obj; |
Aaron Lu | 46445b6 | 2013-10-11 21:27:46 +0800 | [diff] [blame] | 6824 | struct acpi_device *device, *child; |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6825 | int rc; |
| 6826 | |
Aaron Lu | 46445b6 | 2013-10-11 21:27:46 +0800 | [diff] [blame] | 6827 | if (acpi_bus_get_device(handle, &device)) |
| 6828 | return 0; |
| 6829 | |
| 6830 | rc = 0; |
| 6831 | list_for_each_entry(child, &device->children, node) { |
| 6832 | acpi_status status = acpi_evaluate_object(child->handle, "_BCL", |
| 6833 | NULL, &buffer); |
Aaron Ma | 720ef73 | 2020-10-03 01:09:16 +0800 | [diff] [blame] | 6834 | if (ACPI_FAILURE(status)) { |
| 6835 | buffer.length = ACPI_ALLOCATE_BUFFER; |
Aaron Lu | 46445b6 | 2013-10-11 21:27:46 +0800 | [diff] [blame] | 6836 | continue; |
Aaron Ma | 720ef73 | 2020-10-03 01:09:16 +0800 | [diff] [blame] | 6837 | } |
Aaron Lu | 46445b6 | 2013-10-11 21:27:46 +0800 | [diff] [blame] | 6838 | |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6839 | obj = (union acpi_object *)buffer.pointer; |
| 6840 | if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 6841 | pr_err("Unknown _BCL data, please report this to %s\n", |
Aaron Lu | 46445b6 | 2013-10-11 21:27:46 +0800 | [diff] [blame] | 6842 | TPACPI_MAIL); |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6843 | rc = 0; |
| 6844 | } else { |
| 6845 | rc = obj->package.count; |
| 6846 | } |
Aaron Lu | 46445b6 | 2013-10-11 21:27:46 +0800 | [diff] [blame] | 6847 | break; |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6848 | } |
| 6849 | |
| 6850 | kfree(buffer.pointer); |
| 6851 | return rc; |
| 6852 | } |
| 6853 | |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6854 | |
| 6855 | /* |
| 6856 | * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map |
| 6857 | */ |
| 6858 | static unsigned int __init tpacpi_check_std_acpi_brightness_support(void) |
| 6859 | { |
Henrique de Moraes Holschuh | 122f267 | 2010-08-09 23:48:18 -0300 | [diff] [blame] | 6860 | acpi_handle video_device; |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6861 | int bcl_levels = 0; |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6862 | |
Aaron Lu | 46445b6 | 2013-10-11 21:27:46 +0800 | [diff] [blame] | 6863 | tpacpi_acpi_handle_locate("video", NULL, &video_device); |
Henrique de Moraes Holschuh | 122f267 | 2010-08-09 23:48:18 -0300 | [diff] [blame] | 6864 | if (video_device) |
| 6865 | bcl_levels = tpacpi_query_bcl_levels(video_device); |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6866 | |
Henrique de Moraes Holschuh | 122f267 | 2010-08-09 23:48:18 -0300 | [diff] [blame] | 6867 | tp_features.bright_acpimode = (bcl_levels > 0); |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6868 | |
Henrique de Moraes Holschuh | 122f267 | 2010-08-09 23:48:18 -0300 | [diff] [blame] | 6869 | return (bcl_levels > 2) ? (bcl_levels - 2) : 0; |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6870 | } |
| 6871 | |
Henrique de Moraes Holschuh | 59fe4fe | 2009-08-01 12:04:20 -0300 | [diff] [blame] | 6872 | /* |
| 6873 | * These are only useful for models that have only one possibility |
| 6874 | * of GPU. If the BIOS model handles both ATI and Intel, don't use |
| 6875 | * these quirks. |
| 6876 | */ |
| 6877 | #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */ |
| 6878 | #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */ |
| 6879 | #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */ |
| 6880 | |
| 6881 | static const struct tpacpi_quirk brightness_quirk_table[] __initconst = { |
| 6882 | /* Models with ATI GPUs known to require ECNVRAM mode */ |
| 6883 | TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */ |
| 6884 | |
Henrique de Moraes Holschuh | 6da25bf | 2009-09-12 15:22:11 -0300 | [diff] [blame] | 6885 | /* Models with ATI GPUs that can use ECNVRAM */ |
Henrique de Moraes Holschuh | 7d1894d | 2010-02-25 21:28:56 -0300 | [diff] [blame] | 6886 | TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */ |
Henrique de Moraes Holschuh | 59fe4fe | 2009-08-01 12:04:20 -0300 | [diff] [blame] | 6887 | TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), |
Henrique de Moraes Holschuh | 7d1894d | 2010-02-25 21:28:56 -0300 | [diff] [blame] | 6888 | TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */ |
Henrique de Moraes Holschuh | 59fe4fe | 2009-08-01 12:04:20 -0300 | [diff] [blame] | 6889 | TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), |
| 6890 | |
Henrique de Moraes Holschuh | 6da25bf | 2009-09-12 15:22:11 -0300 | [diff] [blame] | 6891 | /* Models with Intel Extreme Graphics 2 */ |
Henrique de Moraes Holschuh | 7d1894d | 2010-02-25 21:28:56 -0300 | [diff] [blame] | 6892 | TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */ |
Henrique de Moraes Holschuh | a9f8eac | 2009-12-09 01:36:21 +0000 | [diff] [blame] | 6893 | TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), |
| 6894 | TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), |
Henrique de Moraes Holschuh | 59fe4fe | 2009-08-01 12:04:20 -0300 | [diff] [blame] | 6895 | |
| 6896 | /* Models with Intel GMA900 */ |
| 6897 | TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */ |
| 6898 | TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */ |
| 6899 | TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */ |
| 6900 | }; |
| 6901 | |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6902 | /* |
| 6903 | * Returns < 0 for error, otherwise sets tp_features.bright_* |
| 6904 | * and bright_maxlvl. |
| 6905 | */ |
| 6906 | static void __init tpacpi_detect_brightness_capabilities(void) |
| 6907 | { |
| 6908 | unsigned int b; |
| 6909 | |
| 6910 | vdbg_printk(TPACPI_DBG_INIT, |
| 6911 | "detecting firmware brightness interface capabilities\n"); |
| 6912 | |
| 6913 | /* we could run a quirks check here (same table used by |
| 6914 | * brightness_init) if needed */ |
| 6915 | |
| 6916 | /* |
| 6917 | * We always attempt to detect acpi support, so as to switch |
| 6918 | * Lenovo Vista BIOS to ACPI brightness mode even if we are not |
| 6919 | * going to publish a backlight interface |
| 6920 | */ |
| 6921 | b = tpacpi_check_std_acpi_brightness_support(); |
| 6922 | switch (b) { |
| 6923 | case 16: |
| 6924 | bright_maxlvl = 15; |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6925 | break; |
| 6926 | case 8: |
| 6927 | case 0: |
| 6928 | bright_maxlvl = 7; |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6929 | break; |
| 6930 | default: |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6931 | tp_features.bright_unkfw = 1; |
| 6932 | bright_maxlvl = b - 1; |
| 6933 | } |
Eric Curtin | 15c7562 | 2016-01-30 16:55:59 +0000 | [diff] [blame] | 6934 | pr_debug("detected %u brightness levels\n", bright_maxlvl + 1); |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6935 | } |
| 6936 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 6937 | static int __init brightness_init(struct ibm_init_struct *iibm) |
Henrique de Moraes Holschuh | fb87a81 | 2006-11-25 16:35:09 -0200 | [diff] [blame] | 6938 | { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 6939 | struct backlight_properties props; |
Henrique de Moraes Holschuh | adb0058 | 2007-02-22 16:04:55 -0200 | [diff] [blame] | 6940 | int b; |
Henrique de Moraes Holschuh | 59fe4fe | 2009-08-01 12:04:20 -0300 | [diff] [blame] | 6941 | unsigned long quirks; |
Henrique de Moraes Holschuh | adb0058 | 2007-02-22 16:04:55 -0200 | [diff] [blame] | 6942 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 6943 | vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n"); |
| 6944 | |
Henrique de Moraes Holschuh | f432255 | 2007-07-18 23:45:48 -0300 | [diff] [blame] | 6945 | mutex_init(&brightness_mutex); |
| 6946 | |
Henrique de Moraes Holschuh | 59fe4fe | 2009-08-01 12:04:20 -0300 | [diff] [blame] | 6947 | quirks = tpacpi_check_quirks(brightness_quirk_table, |
| 6948 | ARRAY_SIZE(brightness_quirk_table)); |
| 6949 | |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6950 | /* tpacpi_detect_brightness_capabilities() must have run already */ |
Thomas Renninger | 2dba1b5 | 2008-08-01 17:38:03 +0200 | [diff] [blame] | 6951 | |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 6952 | /* if it is unknown, we don't handle it: it wouldn't be safe */ |
| 6953 | if (tp_features.bright_unkfw) |
| 6954 | return 1; |
| 6955 | |
Henrique de Moraes Holschuh | b597279 | 2008-04-26 01:02:17 -0300 | [diff] [blame] | 6956 | if (!brightness_enable) { |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6957 | dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT, |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 6958 | "brightness support disabled by module parameter\n"); |
Henrique de Moraes Holschuh | b597279 | 2008-04-26 01:02:17 -0300 | [diff] [blame] | 6959 | return 1; |
| 6960 | } |
| 6961 | |
Hans de Goede | b33c6ce | 2015-06-16 16:28:10 +0200 | [diff] [blame] | 6962 | if (acpi_video_get_backlight_type() != acpi_backlight_vendor) { |
Henrique de Moraes Holschuh | 217f096 | 2010-08-09 23:48:19 -0300 | [diff] [blame] | 6963 | if (brightness_enable > 1) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 6964 | pr_info("Standard ACPI backlight interface available, not loading native one\n"); |
Henrique de Moraes Holschuh | 217f096 | 2010-08-09 23:48:19 -0300 | [diff] [blame] | 6965 | return 1; |
| 6966 | } else if (brightness_enable == 1) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 6967 | pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n"); |
Henrique de Moraes Holschuh | 217f096 | 2010-08-09 23:48:19 -0300 | [diff] [blame] | 6968 | return 1; |
| 6969 | } |
Aaron Ma | 46713ae | 2020-07-10 09:56:14 +0800 | [diff] [blame] | 6970 | } else if (!tp_features.bright_acpimode) { |
| 6971 | pr_notice("ACPI backlight interface not available\n"); |
| 6972 | return 1; |
Henrique de Moraes Holschuh | 217f096 | 2010-08-09 23:48:19 -0300 | [diff] [blame] | 6973 | } |
| 6974 | |
Aaron Ma | 46713ae | 2020-07-10 09:56:14 +0800 | [diff] [blame] | 6975 | pr_notice("ACPI native brightness control enabled\n"); |
| 6976 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6977 | /* |
| 6978 | * Check for module parameter bogosity, note that we |
| 6979 | * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be |
| 6980 | * able to detect "unspecified" |
| 6981 | */ |
| 6982 | if (brightness_mode > TPACPI_BRGHT_MODE_MAX) |
Henrique de Moraes Holschuh | 24d3b77 | 2007-07-18 23:45:43 -0300 | [diff] [blame] | 6983 | return -EINVAL; |
| 6984 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6985 | /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */ |
| 6986 | if (brightness_mode == TPACPI_BRGHT_MODE_AUTO || |
| 6987 | brightness_mode == TPACPI_BRGHT_MODE_MAX) { |
Henrique de Moraes Holschuh | 59fe4fe | 2009-08-01 12:04:20 -0300 | [diff] [blame] | 6988 | if (quirks & TPACPI_BRGHT_Q_EC) |
| 6989 | brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM; |
| 6990 | else |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6991 | brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP; |
| 6992 | |
| 6993 | dbg_printk(TPACPI_DBG_BRGHT, |
Henrique de Moraes Holschuh | 59fe4fe | 2009-08-01 12:04:20 -0300 | [diff] [blame] | 6994 | "driver auto-selected brightness_mode=%d\n", |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 6995 | brightness_mode); |
| 6996 | } |
| 6997 | |
Henrique de Moraes Holschuh | d7880f1 | 2009-06-18 00:40:16 -0300 | [diff] [blame] | 6998 | /* Safety */ |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 6999 | if (!tpacpi_is_ibm() && |
Henrique de Moraes Holschuh | d7880f1 | 2009-06-18 00:40:16 -0300 | [diff] [blame] | 7000 | (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM || |
| 7001 | brightness_mode == TPACPI_BRGHT_MODE_EC)) |
| 7002 | return -EINVAL; |
| 7003 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 7004 | if (tpacpi_brightness_get_raw(&b) < 0) |
Henrique de Moraes Holschuh | 24d3b77 | 2007-07-18 23:45:43 -0300 | [diff] [blame] | 7005 | return 1; |
Henrique de Moraes Holschuh | adb0058 | 2007-02-22 16:04:55 -0200 | [diff] [blame] | 7006 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 7007 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 7008 | props.type = BACKLIGHT_PLATFORM; |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 7009 | props.max_brightness = bright_maxlvl; |
| 7010 | props.brightness = b & TP_EC_BACKLIGHT_LVLMSK; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 7011 | ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME, |
| 7012 | NULL, NULL, |
| 7013 | &ibm_backlight_data, |
| 7014 | &props); |
Henrique de Moraes Holschuh | fb87a81 | 2006-11-25 16:35:09 -0200 | [diff] [blame] | 7015 | if (IS_ERR(ibm_backlight_device)) { |
Henrique de Moraes Holschuh | 435c47e | 2009-09-20 14:09:22 -0300 | [diff] [blame] | 7016 | int rc = PTR_ERR(ibm_backlight_device); |
| 7017 | ibm_backlight_device = NULL; |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 7018 | pr_err("Could not register backlight device\n"); |
Henrique de Moraes Holschuh | 435c47e | 2009-09-20 14:09:22 -0300 | [diff] [blame] | 7019 | return rc; |
Henrique de Moraes Holschuh | fb87a81 | 2006-11-25 16:35:09 -0200 | [diff] [blame] | 7020 | } |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 7021 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT, |
| 7022 | "brightness is supported\n"); |
Henrique de Moraes Holschuh | fb87a81 | 2006-11-25 16:35:09 -0200 | [diff] [blame] | 7023 | |
Henrique de Moraes Holschuh | 59fe4fe | 2009-08-01 12:04:20 -0300 | [diff] [blame] | 7024 | if (quirks & TPACPI_BRGHT_Q_ASK) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 7025 | pr_notice("brightness: will use unverified default: brightness_mode=%d\n", |
| 7026 | brightness_mode); |
| 7027 | pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n", |
| 7028 | TPACPI_MAIL); |
Henrique de Moraes Holschuh | 59fe4fe | 2009-08-01 12:04:20 -0300 | [diff] [blame] | 7029 | } |
| 7030 | |
Henrique de Moraes Holschuh | 7d9745c | 2010-05-16 19:45:57 -0300 | [diff] [blame] | 7031 | /* Added by mistake in early 2007. Probably useless, but it could |
| 7032 | * be working around some unknown firmware problem where the value |
| 7033 | * read at startup doesn't match the real hardware state... so leave |
| 7034 | * it in place just in case */ |
Henrique de Moraes Holschuh | adb0058 | 2007-02-22 16:04:55 -0200 | [diff] [blame] | 7035 | backlight_update_status(ibm_backlight_device); |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 7036 | |
Henrique de Moraes Holschuh | 347a268 | 2009-12-09 01:36:24 +0000 | [diff] [blame] | 7037 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT, |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 7038 | "brightness: registering brightness hotkeys as change notification\n"); |
Henrique de Moraes Holschuh | 347a268 | 2009-12-09 01:36:24 +0000 | [diff] [blame] | 7039 | tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
| 7040 | | TP_ACPI_HKEY_BRGHTUP_MASK |
Joe Perches | 3098064 | 2010-11-14 19:04:38 -0800 | [diff] [blame] | 7041 | | TP_ACPI_HKEY_BRGHTDWN_MASK); |
Henrique de Moraes Holschuh | fb87a81 | 2006-11-25 16:35:09 -0200 | [diff] [blame] | 7042 | return 0; |
| 7043 | } |
| 7044 | |
Rafael J. Wysocki | fd3c3a4 | 2012-06-27 23:18:44 +0200 | [diff] [blame] | 7045 | static void brightness_suspend(void) |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 7046 | { |
| 7047 | tpacpi_brightness_checkpoint_nvram(); |
| 7048 | } |
| 7049 | |
| 7050 | static void brightness_shutdown(void) |
| 7051 | { |
| 7052 | tpacpi_brightness_checkpoint_nvram(); |
| 7053 | } |
| 7054 | |
Henrique de Moraes Holschuh | fb87a81 | 2006-11-25 16:35:09 -0200 | [diff] [blame] | 7055 | static void brightness_exit(void) |
| 7056 | { |
| 7057 | if (ibm_backlight_device) { |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 7058 | vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT, |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 7059 | "calling backlight_device_unregister()\n"); |
Henrique de Moraes Holschuh | fb87a81 | 2006-11-25 16:35:09 -0200 | [diff] [blame] | 7060 | backlight_device_unregister(ibm_backlight_device); |
Henrique de Moraes Holschuh | fb87a81 | 2006-11-25 16:35:09 -0200 | [diff] [blame] | 7061 | } |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 7062 | |
| 7063 | tpacpi_brightness_checkpoint_nvram(); |
Henrique de Moraes Holschuh | fb87a81 | 2006-11-25 16:35:09 -0200 | [diff] [blame] | 7064 | } |
| 7065 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 7066 | static int brightness_read(struct seq_file *m) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7067 | { |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7068 | int level; |
| 7069 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 7070 | level = brightness_get(NULL); |
| 7071 | if (level < 0) { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 7072 | seq_printf(m, "level:\t\tunreadable\n"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7073 | } else { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 7074 | seq_printf(m, "level:\t\t%d\n", level); |
| 7075 | seq_printf(m, "commands:\tup, down\n"); |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 7076 | seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n", |
| 7077 | bright_maxlvl); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7078 | } |
| 7079 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 7080 | return 0; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7081 | } |
| 7082 | |
| 7083 | static int brightness_write(char *buf) |
| 7084 | { |
| 7085 | int level; |
Henrique de Moraes Holschuh | 4273af8 | 2007-10-30 17:46:25 -0200 | [diff] [blame] | 7086 | int rc; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7087 | char *cmd; |
| 7088 | |
Henrique de Moraes Holschuh | 4273af8 | 2007-10-30 17:46:25 -0200 | [diff] [blame] | 7089 | level = brightness_get(NULL); |
| 7090 | if (level < 0) |
| 7091 | return level; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7092 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 7093 | while ((cmd = strsep(&buf, ","))) { |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7094 | if (strlencmp(cmd, "up") == 0) { |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 7095 | if (level < bright_maxlvl) |
Henrique de Moraes Holschuh | 4273af8 | 2007-10-30 17:46:25 -0200 | [diff] [blame] | 7096 | level++; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7097 | } else if (strlencmp(cmd, "down") == 0) { |
Henrique de Moraes Holschuh | 4273af8 | 2007-10-30 17:46:25 -0200 | [diff] [blame] | 7098 | if (level > 0) |
| 7099 | level--; |
| 7100 | } else if (sscanf(cmd, "level %d", &level) == 1 && |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 7101 | level >= 0 && level <= bright_maxlvl) { |
Henrique de Moraes Holschuh | 4273af8 | 2007-10-30 17:46:25 -0200 | [diff] [blame] | 7102 | /* new level set */ |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7103 | } else |
| 7104 | return -EINVAL; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7105 | } |
| 7106 | |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 7107 | tpacpi_disclose_usertask("procfs brightness", |
| 7108 | "set level to %d\n", level); |
| 7109 | |
Henrique de Moraes Holschuh | 4273af8 | 2007-10-30 17:46:25 -0200 | [diff] [blame] | 7110 | /* |
| 7111 | * Now we know what the final level should be, so we try to set it. |
| 7112 | * Doing it this way makes the syscall restartable in case of EINTR |
| 7113 | */ |
| 7114 | rc = brightness_set(level); |
Henrique de Moraes Holschuh | 347a268 | 2009-12-09 01:36:24 +0000 | [diff] [blame] | 7115 | if (!rc && ibm_backlight_device) |
| 7116 | backlight_force_update(ibm_backlight_device, |
| 7117 | BACKLIGHT_UPDATE_SYSFS); |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 7118 | return (rc == -EINTR) ? -ERESTARTSYS : rc; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7119 | } |
| 7120 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 7121 | static struct ibm_struct brightness_driver_data = { |
| 7122 | .name = "brightness", |
| 7123 | .read = brightness_read, |
| 7124 | .write = brightness_write, |
| 7125 | .exit = brightness_exit, |
Henrique de Moraes Holschuh | 0e50183 | 2009-04-04 04:25:53 +0000 | [diff] [blame] | 7126 | .suspend = brightness_suspend, |
| 7127 | .shutdown = brightness_shutdown, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 7128 | }; |
| 7129 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7130 | /************************************************************************* |
| 7131 | * Volume subdriver |
| 7132 | */ |
| 7133 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7134 | /* |
| 7135 | * IBM ThinkPads have a simple volume controller with MUTE gating. |
| 7136 | * Very early Lenovo ThinkPads follow the IBM ThinkPad spec. |
| 7137 | * |
| 7138 | * Since the *61 series (and probably also the later *60 series), Lenovo |
| 7139 | * ThinkPads only implement the MUTE gate. |
| 7140 | * |
| 7141 | * EC register 0x30 |
| 7142 | * Bit 6: MUTE (1 mutes sound) |
| 7143 | * Bit 3-0: Volume |
| 7144 | * Other bits should be zero as far as we know. |
| 7145 | * |
| 7146 | * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and |
| 7147 | * bits 3-0 (volume). Other bits in NVRAM may have other functions, |
| 7148 | * such as bit 7 which is used to detect repeated presses of MUTE, |
| 7149 | * and we leave them unchanged. |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 7150 | * |
| 7151 | * On newer Lenovo ThinkPads, the EC can automatically change the volume |
| 7152 | * in response to user input. Unfortunately, this rarely works well. |
| 7153 | * The laptop changes the state of its internal MUTE gate and, on some |
| 7154 | * models, sends KEY_MUTE, causing any user code that responds to the |
| 7155 | * mute button to get confused. The hardware MUTE gate is also |
| 7156 | * unnecessary, since user code can handle the mute button without |
| 7157 | * kernel or EC help. |
| 7158 | * |
| 7159 | * To avoid confusing userspace, we simply disable all EC-based mute |
| 7160 | * and volume controls when possible. |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7161 | */ |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 7162 | |
Henrique de Moraes Holschuh | ff850c3 | 2009-12-26 22:52:15 -0200 | [diff] [blame] | 7163 | #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT |
| 7164 | |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7165 | #define TPACPI_ALSA_DRVNAME "ThinkPad EC" |
| 7166 | #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control" |
| 7167 | #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME |
| 7168 | |
Takashi Iwai | cab6661 | 2013-10-24 16:06:32 +0200 | [diff] [blame] | 7169 | #if SNDRV_CARDS <= 32 |
| 7170 | #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1) |
| 7171 | #else |
| 7172 | #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1) |
| 7173 | #endif |
| 7174 | static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */ |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7175 | static char *alsa_id = "ThinkPadEC"; |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 7176 | static bool alsa_enable = SNDRV_DEFAULT_ENABLE1; |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7177 | |
| 7178 | struct tpacpi_alsa_data { |
| 7179 | struct snd_card *card; |
| 7180 | struct snd_ctl_elem_id *ctl_mute_id; |
| 7181 | struct snd_ctl_elem_id *ctl_vol_id; |
| 7182 | }; |
| 7183 | |
| 7184 | static struct snd_card *alsa_card; |
| 7185 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7186 | enum { |
| 7187 | TP_EC_AUDIO = 0x30, |
| 7188 | |
| 7189 | /* TP_EC_AUDIO bits */ |
| 7190 | TP_EC_AUDIO_MUTESW = 6, |
| 7191 | |
| 7192 | /* TP_EC_AUDIO bitmasks */ |
| 7193 | TP_EC_AUDIO_LVL_MSK = 0x0F, |
| 7194 | TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW), |
| 7195 | |
| 7196 | /* Maximum volume */ |
| 7197 | TP_EC_VOLUME_MAX = 14, |
| 7198 | }; |
| 7199 | |
| 7200 | enum tpacpi_volume_access_mode { |
| 7201 | TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */ |
| 7202 | TPACPI_VOL_MODE_EC, /* Pure EC control */ |
| 7203 | TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */ |
| 7204 | TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */ |
| 7205 | TPACPI_VOL_MODE_MAX |
| 7206 | }; |
| 7207 | |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7208 | enum tpacpi_volume_capabilities { |
| 7209 | TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */ |
| 7210 | TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */ |
| 7211 | TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */ |
| 7212 | TPACPI_VOL_CAP_MAX |
| 7213 | }; |
| 7214 | |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 7215 | enum tpacpi_mute_btn_mode { |
| 7216 | TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */ |
| 7217 | /* We don't know what mode 1 is. */ |
| 7218 | TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */ |
| 7219 | TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */ |
| 7220 | }; |
| 7221 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7222 | static enum tpacpi_volume_access_mode volume_mode = |
| 7223 | TPACPI_VOL_MODE_MAX; |
| 7224 | |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7225 | static enum tpacpi_volume_capabilities volume_capabilities; |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 7226 | static bool volume_control_allowed; |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 7227 | static bool software_mute_requested = true; |
| 7228 | static bool software_mute_active; |
| 7229 | static int software_mute_orig_mode; |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7230 | |
| 7231 | /* |
| 7232 | * Used to syncronize writers to TP_EC_AUDIO and |
| 7233 | * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write |
| 7234 | */ |
| 7235 | static struct mutex volume_mutex; |
| 7236 | |
| 7237 | static void tpacpi_volume_checkpoint_nvram(void) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7238 | { |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7239 | u8 lec = 0; |
| 7240 | u8 b_nvram; |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7241 | u8 ec_mask; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7242 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7243 | if (volume_mode != TPACPI_VOL_MODE_ECNVRAM) |
| 7244 | return; |
Henrique de Moraes Holschuh | c7ac629 | 2009-12-15 21:51:10 -0200 | [diff] [blame] | 7245 | if (!volume_control_allowed) |
| 7246 | return; |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 7247 | if (software_mute_active) |
| 7248 | return; |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7249 | |
| 7250 | vdbg_printk(TPACPI_DBG_MIXER, |
| 7251 | "trying to checkpoint mixer state to NVRAM...\n"); |
| 7252 | |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7253 | if (tp_features.mixer_no_level_control) |
| 7254 | ec_mask = TP_EC_AUDIO_MUTESW_MSK; |
| 7255 | else |
| 7256 | ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK; |
| 7257 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7258 | if (mutex_lock_killable(&volume_mutex) < 0) |
| 7259 | return; |
| 7260 | |
| 7261 | if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec))) |
| 7262 | goto unlock; |
| 7263 | lec &= ec_mask; |
| 7264 | b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER); |
| 7265 | |
| 7266 | if (lec != (b_nvram & ec_mask)) { |
| 7267 | /* NVRAM needs update */ |
| 7268 | b_nvram &= ~ec_mask; |
| 7269 | b_nvram |= lec; |
| 7270 | nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER); |
| 7271 | dbg_printk(TPACPI_DBG_MIXER, |
| 7272 | "updated NVRAM mixer status to 0x%02x (0x%02x)\n", |
| 7273 | (unsigned int) lec, (unsigned int) b_nvram); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7274 | } else { |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7275 | vdbg_printk(TPACPI_DBG_MIXER, |
| 7276 | "NVRAM mixer status already is 0x%02x (0x%02x)\n", |
| 7277 | (unsigned int) lec, (unsigned int) b_nvram); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7278 | } |
| 7279 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7280 | unlock: |
| 7281 | mutex_unlock(&volume_mutex); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7282 | } |
| 7283 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7284 | static int volume_get_status_ec(u8 *status) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7285 | { |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7286 | u8 s; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7287 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7288 | if (!acpi_ec_read(TP_EC_AUDIO, &s)) |
| 7289 | return -EIO; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7290 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7291 | *status = s; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7292 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7293 | dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s); |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 7294 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7295 | return 0; |
| 7296 | } |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7297 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7298 | static int volume_get_status(u8 *status) |
| 7299 | { |
| 7300 | return volume_get_status_ec(status); |
| 7301 | } |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7302 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7303 | static int volume_set_status_ec(const u8 status) |
| 7304 | { |
| 7305 | if (!acpi_ec_write(TP_EC_AUDIO, status)) |
| 7306 | return -EIO; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7307 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7308 | dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status); |
| 7309 | |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 7310 | /* |
| 7311 | * On X200s, and possibly on others, it can take a while for |
| 7312 | * reads to become correct. |
| 7313 | */ |
| 7314 | msleep(1); |
| 7315 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7316 | return 0; |
| 7317 | } |
| 7318 | |
| 7319 | static int volume_set_status(const u8 status) |
| 7320 | { |
| 7321 | return volume_set_status_ec(status); |
| 7322 | } |
| 7323 | |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7324 | /* returns < 0 on error, 0 on no change, 1 on change */ |
| 7325 | static int __volume_set_mute_ec(const bool mute) |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7326 | { |
| 7327 | int rc; |
| 7328 | u8 s, n; |
| 7329 | |
| 7330 | if (mutex_lock_killable(&volume_mutex) < 0) |
| 7331 | return -EINTR; |
| 7332 | |
| 7333 | rc = volume_get_status_ec(&s); |
| 7334 | if (rc) |
| 7335 | goto unlock; |
| 7336 | |
| 7337 | n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK : |
| 7338 | s & ~TP_EC_AUDIO_MUTESW_MSK; |
| 7339 | |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7340 | if (n != s) { |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7341 | rc = volume_set_status_ec(n); |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7342 | if (!rc) |
| 7343 | rc = 1; |
| 7344 | } |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7345 | |
| 7346 | unlock: |
| 7347 | mutex_unlock(&volume_mutex); |
| 7348 | return rc; |
| 7349 | } |
| 7350 | |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7351 | static int volume_alsa_set_mute(const bool mute) |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7352 | { |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7353 | dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n", |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7354 | (mute) ? "" : "un"); |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7355 | return __volume_set_mute_ec(mute); |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7356 | } |
| 7357 | |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7358 | static int volume_set_mute(const bool mute) |
| 7359 | { |
| 7360 | int rc; |
| 7361 | |
| 7362 | dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n", |
| 7363 | (mute) ? "" : "un"); |
| 7364 | |
| 7365 | rc = __volume_set_mute_ec(mute); |
| 7366 | return (rc < 0) ? rc : 0; |
| 7367 | } |
| 7368 | |
| 7369 | /* returns < 0 on error, 0 on no change, 1 on change */ |
| 7370 | static int __volume_set_volume_ec(const u8 vol) |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7371 | { |
| 7372 | int rc; |
| 7373 | u8 s, n; |
| 7374 | |
| 7375 | if (vol > TP_EC_VOLUME_MAX) |
| 7376 | return -EINVAL; |
| 7377 | |
| 7378 | if (mutex_lock_killable(&volume_mutex) < 0) |
| 7379 | return -EINTR; |
| 7380 | |
| 7381 | rc = volume_get_status_ec(&s); |
| 7382 | if (rc) |
| 7383 | goto unlock; |
| 7384 | |
| 7385 | n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol; |
| 7386 | |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7387 | if (n != s) { |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7388 | rc = volume_set_status_ec(n); |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7389 | if (!rc) |
| 7390 | rc = 1; |
| 7391 | } |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7392 | |
| 7393 | unlock: |
| 7394 | mutex_unlock(&volume_mutex); |
| 7395 | return rc; |
| 7396 | } |
| 7397 | |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 7398 | static int volume_set_software_mute(bool startup) |
| 7399 | { |
| 7400 | int result; |
| 7401 | |
| 7402 | if (!tpacpi_is_lenovo()) |
| 7403 | return -ENODEV; |
| 7404 | |
| 7405 | if (startup) { |
| 7406 | if (!acpi_evalf(ec_handle, &software_mute_orig_mode, |
| 7407 | "HAUM", "qd")) |
| 7408 | return -EIO; |
| 7409 | |
| 7410 | dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, |
| 7411 | "Initial HAUM setting was %d\n", |
| 7412 | software_mute_orig_mode); |
| 7413 | } |
| 7414 | |
| 7415 | if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd", |
| 7416 | (int)TP_EC_MUTE_BTN_NONE)) |
| 7417 | return -EIO; |
| 7418 | |
| 7419 | if (result != TP_EC_MUTE_BTN_NONE) |
| 7420 | pr_warn("Unexpected SAUM result %d\n", |
| 7421 | result); |
| 7422 | |
| 7423 | /* |
| 7424 | * In software mute mode, the standard codec controls take |
| 7425 | * precendence, so we unmute the ThinkPad HW switch at |
| 7426 | * startup. Just on case there are SAUM-capable ThinkPads |
| 7427 | * with level controls, set max HW volume as well. |
| 7428 | */ |
| 7429 | if (tp_features.mixer_no_level_control) |
| 7430 | result = volume_set_mute(false); |
| 7431 | else |
| 7432 | result = volume_set_status(TP_EC_VOLUME_MAX); |
| 7433 | |
| 7434 | if (result != 0) |
| 7435 | pr_warn("Failed to unmute the HW mute switch\n"); |
| 7436 | |
| 7437 | return 0; |
| 7438 | } |
| 7439 | |
| 7440 | static void volume_exit_software_mute(void) |
| 7441 | { |
| 7442 | int r; |
| 7443 | |
| 7444 | if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode) |
| 7445 | || r != software_mute_orig_mode) |
| 7446 | pr_warn("Failed to restore mute mode\n"); |
| 7447 | } |
| 7448 | |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7449 | static int volume_alsa_set_volume(const u8 vol) |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7450 | { |
| 7451 | dbg_printk(TPACPI_DBG_MIXER, |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7452 | "ALSA: trying to set volume level to %hu\n", vol); |
| 7453 | return __volume_set_volume_ec(vol); |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7454 | } |
| 7455 | |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7456 | static void volume_alsa_notify_change(void) |
| 7457 | { |
| 7458 | struct tpacpi_alsa_data *d; |
| 7459 | |
| 7460 | if (alsa_card && alsa_card->private_data) { |
| 7461 | d = alsa_card->private_data; |
| 7462 | if (d->ctl_mute_id) |
| 7463 | snd_ctl_notify(alsa_card, |
| 7464 | SNDRV_CTL_EVENT_MASK_VALUE, |
| 7465 | d->ctl_mute_id); |
| 7466 | if (d->ctl_vol_id) |
| 7467 | snd_ctl_notify(alsa_card, |
| 7468 | SNDRV_CTL_EVENT_MASK_VALUE, |
| 7469 | d->ctl_vol_id); |
| 7470 | } |
| 7471 | } |
| 7472 | |
| 7473 | static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol, |
| 7474 | struct snd_ctl_elem_info *uinfo) |
| 7475 | { |
| 7476 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 7477 | uinfo->count = 1; |
| 7478 | uinfo->value.integer.min = 0; |
| 7479 | uinfo->value.integer.max = TP_EC_VOLUME_MAX; |
| 7480 | return 0; |
| 7481 | } |
| 7482 | |
| 7483 | static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol, |
| 7484 | struct snd_ctl_elem_value *ucontrol) |
| 7485 | { |
| 7486 | u8 s; |
| 7487 | int rc; |
| 7488 | |
| 7489 | rc = volume_get_status(&s); |
| 7490 | if (rc < 0) |
| 7491 | return rc; |
| 7492 | |
| 7493 | ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK; |
| 7494 | return 0; |
| 7495 | } |
| 7496 | |
| 7497 | static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol, |
| 7498 | struct snd_ctl_elem_value *ucontrol) |
| 7499 | { |
Henrique de Moraes Holschuh | 38e11cd | 2010-05-16 19:45:40 -0300 | [diff] [blame] | 7500 | tpacpi_disclose_usertask("ALSA", "set volume to %ld\n", |
| 7501 | ucontrol->value.integer.value[0]); |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7502 | return volume_alsa_set_volume(ucontrol->value.integer.value[0]); |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7503 | } |
| 7504 | |
| 7505 | #define volume_alsa_mute_info snd_ctl_boolean_mono_info |
| 7506 | |
| 7507 | static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol, |
| 7508 | struct snd_ctl_elem_value *ucontrol) |
| 7509 | { |
| 7510 | u8 s; |
| 7511 | int rc; |
| 7512 | |
| 7513 | rc = volume_get_status(&s); |
| 7514 | if (rc < 0) |
| 7515 | return rc; |
| 7516 | |
| 7517 | ucontrol->value.integer.value[0] = |
| 7518 | (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1; |
| 7519 | return 0; |
| 7520 | } |
| 7521 | |
| 7522 | static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol, |
| 7523 | struct snd_ctl_elem_value *ucontrol) |
| 7524 | { |
Henrique de Moraes Holschuh | 38e11cd | 2010-05-16 19:45:40 -0300 | [diff] [blame] | 7525 | tpacpi_disclose_usertask("ALSA", "%smute\n", |
| 7526 | ucontrol->value.integer.value[0] ? |
| 7527 | "un" : ""); |
Henrique de Moraes Holschuh | 88cc837 | 2010-02-27 18:45:29 -0300 | [diff] [blame] | 7528 | return volume_alsa_set_mute(!ucontrol->value.integer.value[0]); |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7529 | } |
| 7530 | |
Mathias Krause | c3aa472 | 2014-07-16 19:43:17 +0200 | [diff] [blame] | 7531 | static struct snd_kcontrol_new volume_alsa_control_vol __initdata = { |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7532 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 7533 | .name = "Console Playback Volume", |
| 7534 | .index = 0, |
| 7535 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 7536 | .info = volume_alsa_vol_info, |
| 7537 | .get = volume_alsa_vol_get, |
| 7538 | }; |
| 7539 | |
Mathias Krause | c3aa472 | 2014-07-16 19:43:17 +0200 | [diff] [blame] | 7540 | static struct snd_kcontrol_new volume_alsa_control_mute __initdata = { |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7541 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 7542 | .name = "Console Playback Switch", |
| 7543 | .index = 0, |
| 7544 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 7545 | .info = volume_alsa_mute_info, |
| 7546 | .get = volume_alsa_mute_get, |
| 7547 | }; |
| 7548 | |
Rafael J. Wysocki | fd3c3a4 | 2012-06-27 23:18:44 +0200 | [diff] [blame] | 7549 | static void volume_suspend(void) |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7550 | { |
| 7551 | tpacpi_volume_checkpoint_nvram(); |
| 7552 | } |
| 7553 | |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7554 | static void volume_resume(void) |
| 7555 | { |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 7556 | if (software_mute_active) { |
| 7557 | if (volume_set_software_mute(false) < 0) |
| 7558 | pr_warn("Failed to restore software mute\n"); |
| 7559 | } else { |
| 7560 | volume_alsa_notify_change(); |
| 7561 | } |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7562 | } |
| 7563 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7564 | static void volume_shutdown(void) |
| 7565 | { |
| 7566 | tpacpi_volume_checkpoint_nvram(); |
| 7567 | } |
| 7568 | |
| 7569 | static void volume_exit(void) |
| 7570 | { |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7571 | if (alsa_card) { |
| 7572 | snd_card_free(alsa_card); |
| 7573 | alsa_card = NULL; |
| 7574 | } |
| 7575 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7576 | tpacpi_volume_checkpoint_nvram(); |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 7577 | |
| 7578 | if (software_mute_active) |
| 7579 | volume_exit_software_mute(); |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7580 | } |
| 7581 | |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7582 | static int __init volume_create_alsa_mixer(void) |
| 7583 | { |
| 7584 | struct snd_card *card; |
| 7585 | struct tpacpi_alsa_data *data; |
| 7586 | struct snd_kcontrol *ctl_vol; |
| 7587 | struct snd_kcontrol *ctl_mute; |
| 7588 | int rc; |
| 7589 | |
Takashi Iwai | 89235e5 | 2014-01-29 15:01:27 +0100 | [diff] [blame] | 7590 | rc = snd_card_new(&tpacpi_pdev->dev, |
| 7591 | alsa_index, alsa_id, THIS_MODULE, |
| 7592 | sizeof(struct tpacpi_alsa_data), &card); |
Henrique de Moraes Holschuh | 74c75c1 | 2009-12-26 22:52:14 -0200 | [diff] [blame] | 7593 | if (rc < 0 || !card) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 7594 | pr_err("Failed to create ALSA card structures: %d\n", rc); |
Henrique de Moraes Holschuh | 74c75c1 | 2009-12-26 22:52:14 -0200 | [diff] [blame] | 7595 | return 1; |
| 7596 | } |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7597 | |
| 7598 | BUG_ON(!card->private_data); |
| 7599 | data = card->private_data; |
| 7600 | data->card = card; |
| 7601 | |
| 7602 | strlcpy(card->driver, TPACPI_ALSA_DRVNAME, |
| 7603 | sizeof(card->driver)); |
| 7604 | strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME, |
| 7605 | sizeof(card->shortname)); |
| 7606 | snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s", |
| 7607 | (thinkpad_id.ec_version_str) ? |
| 7608 | thinkpad_id.ec_version_str : "(unknown)"); |
| 7609 | snprintf(card->longname, sizeof(card->longname), |
| 7610 | "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO, |
| 7611 | (thinkpad_id.ec_version_str) ? |
| 7612 | thinkpad_id.ec_version_str : "unknown"); |
| 7613 | |
| 7614 | if (volume_control_allowed) { |
| 7615 | volume_alsa_control_vol.put = volume_alsa_vol_put; |
| 7616 | volume_alsa_control_vol.access = |
| 7617 | SNDRV_CTL_ELEM_ACCESS_READWRITE; |
| 7618 | |
| 7619 | volume_alsa_control_mute.put = volume_alsa_mute_put; |
| 7620 | volume_alsa_control_mute.access = |
| 7621 | SNDRV_CTL_ELEM_ACCESS_READWRITE; |
| 7622 | } |
| 7623 | |
| 7624 | if (!tp_features.mixer_no_level_control) { |
| 7625 | ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL); |
| 7626 | rc = snd_ctl_add(card, ctl_vol); |
| 7627 | if (rc < 0) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 7628 | pr_err("Failed to create ALSA volume control: %d\n", |
| 7629 | rc); |
Henrique de Moraes Holschuh | 74c75c1 | 2009-12-26 22:52:14 -0200 | [diff] [blame] | 7630 | goto err_exit; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7631 | } |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7632 | data->ctl_vol_id = &ctl_vol->id; |
| 7633 | } |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7634 | |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7635 | ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL); |
| 7636 | rc = snd_ctl_add(card, ctl_mute); |
| 7637 | if (rc < 0) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 7638 | pr_err("Failed to create ALSA mute control: %d\n", rc); |
Henrique de Moraes Holschuh | 74c75c1 | 2009-12-26 22:52:14 -0200 | [diff] [blame] | 7639 | goto err_exit; |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7640 | } |
| 7641 | data->ctl_mute_id = &ctl_mute->id; |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 7642 | |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7643 | rc = snd_card_register(card); |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7644 | if (rc < 0) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 7645 | pr_err("Failed to register ALSA card: %d\n", rc); |
Henrique de Moraes Holschuh | 74c75c1 | 2009-12-26 22:52:14 -0200 | [diff] [blame] | 7646 | goto err_exit; |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7647 | } |
| 7648 | |
| 7649 | alsa_card = card; |
Henrique de Moraes Holschuh | 74c75c1 | 2009-12-26 22:52:14 -0200 | [diff] [blame] | 7650 | return 0; |
| 7651 | |
| 7652 | err_exit: |
| 7653 | snd_card_free(card); |
| 7654 | return 1; |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7655 | } |
| 7656 | |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7657 | #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */ |
| 7658 | #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */ |
| 7659 | |
| 7660 | static const struct tpacpi_quirk volume_quirk_table[] __initconst = { |
| 7661 | /* Whitelist volume level on all IBM by default */ |
| 7662 | { .vendor = PCI_VENDOR_ID_IBM, |
| 7663 | .bios = TPACPI_MATCH_ANY, |
| 7664 | .ec = TPACPI_MATCH_ANY, |
| 7665 | .quirks = TPACPI_VOL_Q_LEVEL }, |
| 7666 | |
| 7667 | /* Lenovo models with volume control (needs confirmation) */ |
| 7668 | TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */ |
| 7669 | TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */ |
| 7670 | TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */ |
| 7671 | TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */ |
| 7672 | TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */ |
| 7673 | TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */ |
| 7674 | TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */ |
| 7675 | |
| 7676 | /* Whitelist mute-only on all Lenovo by default */ |
| 7677 | { .vendor = PCI_VENDOR_ID_LENOVO, |
| 7678 | .bios = TPACPI_MATCH_ANY, |
| 7679 | .ec = TPACPI_MATCH_ANY, |
| 7680 | .quirks = TPACPI_VOL_Q_MUTEONLY } |
| 7681 | }; |
| 7682 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7683 | static int __init volume_init(struct ibm_init_struct *iibm) |
| 7684 | { |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7685 | unsigned long quirks; |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7686 | int rc; |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7687 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7688 | vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n"); |
| 7689 | |
| 7690 | mutex_init(&volume_mutex); |
| 7691 | |
| 7692 | /* |
| 7693 | * Check for module parameter bogosity, note that we |
| 7694 | * init volume_mode to TPACPI_VOL_MODE_MAX in order to be |
| 7695 | * able to detect "unspecified" |
| 7696 | */ |
| 7697 | if (volume_mode > TPACPI_VOL_MODE_MAX) |
| 7698 | return -EINVAL; |
| 7699 | |
| 7700 | if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 7701 | pr_err("UCMS step volume mode not implemented, please contact %s\n", |
| 7702 | TPACPI_MAIL); |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7703 | return 1; |
| 7704 | } |
| 7705 | |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7706 | if (volume_capabilities >= TPACPI_VOL_CAP_MAX) |
| 7707 | return -EINVAL; |
| 7708 | |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7709 | /* |
| 7710 | * The ALSA mixer is our primary interface. |
| 7711 | * When disabled, don't install the subdriver at all |
| 7712 | */ |
| 7713 | if (!alsa_enable) { |
| 7714 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 7715 | "ALSA mixer disabled by parameter, not loading volume subdriver...\n"); |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7716 | return 1; |
| 7717 | } |
| 7718 | |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7719 | quirks = tpacpi_check_quirks(volume_quirk_table, |
| 7720 | ARRAY_SIZE(volume_quirk_table)); |
| 7721 | |
| 7722 | switch (volume_capabilities) { |
| 7723 | case TPACPI_VOL_CAP_AUTO: |
| 7724 | if (quirks & TPACPI_VOL_Q_MUTEONLY) |
| 7725 | tp_features.mixer_no_level_control = 1; |
| 7726 | else if (quirks & TPACPI_VOL_Q_LEVEL) |
| 7727 | tp_features.mixer_no_level_control = 0; |
| 7728 | else |
| 7729 | return 1; /* no mixer */ |
| 7730 | break; |
| 7731 | case TPACPI_VOL_CAP_VOLMUTE: |
| 7732 | tp_features.mixer_no_level_control = 0; |
| 7733 | break; |
| 7734 | case TPACPI_VOL_CAP_MUTEONLY: |
| 7735 | tp_features.mixer_no_level_control = 1; |
| 7736 | break; |
| 7737 | default: |
| 7738 | return 1; |
| 7739 | } |
| 7740 | |
| 7741 | if (volume_capabilities != TPACPI_VOL_CAP_AUTO) |
| 7742 | dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, |
| 7743 | "using user-supplied volume_capabilities=%d\n", |
| 7744 | volume_capabilities); |
| 7745 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7746 | if (volume_mode == TPACPI_VOL_MODE_AUTO || |
| 7747 | volume_mode == TPACPI_VOL_MODE_MAX) { |
| 7748 | volume_mode = TPACPI_VOL_MODE_ECNVRAM; |
| 7749 | |
| 7750 | dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, |
| 7751 | "driver auto-selected volume_mode=%d\n", |
| 7752 | volume_mode); |
| 7753 | } else { |
| 7754 | dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, |
| 7755 | "using user-supplied volume_mode=%d\n", |
| 7756 | volume_mode); |
| 7757 | } |
| 7758 | |
| 7759 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7760 | "mute is supported, volume control is %s\n", |
| 7761 | str_supported(!tp_features.mixer_no_level_control)); |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7762 | |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 7763 | if (software_mute_requested && volume_set_software_mute(true) == 0) { |
| 7764 | software_mute_active = true; |
| 7765 | } else { |
| 7766 | rc = volume_create_alsa_mixer(); |
| 7767 | if (rc) { |
| 7768 | pr_err("Could not create the ALSA mixer interface\n"); |
| 7769 | return rc; |
| 7770 | } |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7771 | |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 7772 | pr_info("Console audio control enabled, mode: %s\n", |
| 7773 | (volume_control_allowed) ? |
| 7774 | "override (read/write)" : |
| 7775 | "monitor (read only)"); |
| 7776 | } |
Henrique de Moraes Holschuh | c7ac629 | 2009-12-15 21:51:10 -0200 | [diff] [blame] | 7777 | |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7778 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, |
| 7779 | "registering volume hotkeys as change notification\n"); |
| 7780 | tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
| 7781 | | TP_ACPI_HKEY_VOLUP_MASK |
| 7782 | | TP_ACPI_HKEY_VOLDWN_MASK |
| 7783 | | TP_ACPI_HKEY_MUTE_MASK); |
| 7784 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7785 | return 0; |
| 7786 | } |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7787 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 7788 | static int volume_read(struct seq_file *m) |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7789 | { |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7790 | u8 status; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7791 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7792 | if (volume_get_status(&status) < 0) { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 7793 | seq_printf(m, "level:\t\tunreadable\n"); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7794 | } else { |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7795 | if (tp_features.mixer_no_level_control) |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 7796 | seq_printf(m, "level:\t\tunsupported\n"); |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7797 | else |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 7798 | seq_printf(m, "level:\t\t%d\n", |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7799 | status & TP_EC_AUDIO_LVL_MSK); |
| 7800 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 7801 | seq_printf(m, "mute:\t\t%s\n", |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7802 | onoff(status, TP_EC_AUDIO_MUTESW)); |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7803 | |
Henrique de Moraes Holschuh | c7ac629 | 2009-12-15 21:51:10 -0200 | [diff] [blame] | 7804 | if (volume_control_allowed) { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 7805 | seq_printf(m, "commands:\tunmute, mute\n"); |
Henrique de Moraes Holschuh | c7ac629 | 2009-12-15 21:51:10 -0200 | [diff] [blame] | 7806 | if (!tp_features.mixer_no_level_control) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 7807 | seq_printf(m, "commands:\tup, down\n"); |
| 7808 | seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n", |
| 7809 | TP_EC_VOLUME_MAX); |
Henrique de Moraes Holschuh | c7ac629 | 2009-12-15 21:51:10 -0200 | [diff] [blame] | 7810 | } |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7811 | } |
| 7812 | } |
| 7813 | |
| 7814 | return 0; |
| 7815 | } |
| 7816 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7817 | static int volume_write(char *buf) |
| 7818 | { |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7819 | u8 s; |
| 7820 | u8 new_level, new_mute; |
| 7821 | int l; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7822 | char *cmd; |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7823 | int rc; |
| 7824 | |
Henrique de Moraes Holschuh | c7ac629 | 2009-12-15 21:51:10 -0200 | [diff] [blame] | 7825 | /* |
| 7826 | * We do allow volume control at driver startup, so that the |
| 7827 | * user can set initial state through the volume=... parameter hack. |
| 7828 | */ |
| 7829 | if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) { |
| 7830 | if (unlikely(!tp_warned.volume_ctrl_forbidden)) { |
| 7831 | tp_warned.volume_ctrl_forbidden = 1; |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 7832 | pr_notice("Console audio control in monitor mode, changes are not allowed\n"); |
| 7833 | pr_notice("Use the volume_control=1 module parameter to enable volume control\n"); |
Henrique de Moraes Holschuh | c7ac629 | 2009-12-15 21:51:10 -0200 | [diff] [blame] | 7834 | } |
| 7835 | return -EPERM; |
| 7836 | } |
| 7837 | |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7838 | rc = volume_get_status(&s); |
| 7839 | if (rc < 0) |
| 7840 | return rc; |
| 7841 | |
| 7842 | new_level = s & TP_EC_AUDIO_LVL_MSK; |
| 7843 | new_mute = s & TP_EC_AUDIO_MUTESW_MSK; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7844 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 7845 | while ((cmd = strsep(&buf, ","))) { |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7846 | if (!tp_features.mixer_no_level_control) { |
| 7847 | if (strlencmp(cmd, "up") == 0) { |
| 7848 | if (new_mute) |
| 7849 | new_mute = 0; |
| 7850 | else if (new_level < TP_EC_VOLUME_MAX) |
| 7851 | new_level++; |
| 7852 | continue; |
| 7853 | } else if (strlencmp(cmd, "down") == 0) { |
| 7854 | if (new_mute) |
| 7855 | new_mute = 0; |
| 7856 | else if (new_level > 0) |
| 7857 | new_level--; |
| 7858 | continue; |
| 7859 | } else if (sscanf(cmd, "level %u", &l) == 1 && |
| 7860 | l >= 0 && l <= TP_EC_VOLUME_MAX) { |
Jiapeng Chong | 3fca4b1 | 2021-05-24 14:23:58 +0800 | [diff] [blame] | 7861 | new_level = l; |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7862 | continue; |
| 7863 | } |
| 7864 | } |
| 7865 | if (strlencmp(cmd, "mute") == 0) |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7866 | new_mute = TP_EC_AUDIO_MUTESW_MSK; |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7867 | else if (strlencmp(cmd, "unmute") == 0) |
| 7868 | new_mute = 0; |
| 7869 | else |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7870 | return -EINVAL; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7871 | } |
| 7872 | |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 7873 | if (tp_features.mixer_no_level_control) { |
| 7874 | tpacpi_disclose_usertask("procfs volume", "%smute\n", |
| 7875 | new_mute ? "" : "un"); |
| 7876 | rc = volume_set_mute(!!new_mute); |
| 7877 | } else { |
| 7878 | tpacpi_disclose_usertask("procfs volume", |
| 7879 | "%smute and set level to %d\n", |
| 7880 | new_mute ? "" : "un", new_level); |
| 7881 | rc = volume_set_status(new_mute | new_level); |
| 7882 | } |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7883 | volume_alsa_notify_change(); |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7884 | |
| 7885 | return (rc == -EINTR) ? -ERESTARTSYS : rc; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 7886 | } |
| 7887 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 7888 | static struct ibm_struct volume_driver_data = { |
| 7889 | .name = "volume", |
| 7890 | .read = volume_read, |
| 7891 | .write = volume_write, |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7892 | .exit = volume_exit, |
| 7893 | .suspend = volume_suspend, |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 7894 | .resume = volume_resume, |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 7895 | .shutdown = volume_shutdown, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 7896 | }; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7897 | |
Henrique de Moraes Holschuh | ff850c3 | 2009-12-26 22:52:15 -0200 | [diff] [blame] | 7898 | #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */ |
| 7899 | |
| 7900 | #define alsa_card NULL |
| 7901 | |
Tobias Klauser | bb55a2e | 2016-11-25 14:18:07 +0100 | [diff] [blame] | 7902 | static inline void volume_alsa_notify_change(void) |
Henrique de Moraes Holschuh | ff850c3 | 2009-12-26 22:52:15 -0200 | [diff] [blame] | 7903 | { |
| 7904 | } |
| 7905 | |
| 7906 | static int __init volume_init(struct ibm_init_struct *iibm) |
| 7907 | { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 7908 | pr_info("volume: disabled as there is no ALSA support in this kernel\n"); |
Henrique de Moraes Holschuh | ff850c3 | 2009-12-26 22:52:15 -0200 | [diff] [blame] | 7909 | |
| 7910 | return 1; |
| 7911 | } |
| 7912 | |
| 7913 | static struct ibm_struct volume_driver_data = { |
| 7914 | .name = "volume", |
| 7915 | }; |
| 7916 | |
| 7917 | #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */ |
| 7918 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7919 | /************************************************************************* |
| 7920 | * Fan subdriver |
| 7921 | */ |
| 7922 | |
| 7923 | /* |
| 7924 | * FAN ACCESS MODES |
| 7925 | * |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 7926 | * TPACPI_FAN_RD_ACPI_GFAN: |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7927 | * ACPI GFAN method: returns fan level |
| 7928 | * |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 7929 | * see TPACPI_FAN_WR_ACPI_SFAN |
Henrique de Moraes Holschuh | f51d1a3 | 2007-04-21 11:08:29 -0300 | [diff] [blame] | 7930 | * EC 0x2f (HFSP) not available if GFAN exists |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7931 | * |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 7932 | * TPACPI_FAN_WR_ACPI_SFAN: |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7933 | * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max) |
| 7934 | * |
Henrique de Moraes Holschuh | f51d1a3 | 2007-04-21 11:08:29 -0300 | [diff] [blame] | 7935 | * EC 0x2f (HFSP) might be available *for reading*, but do not use |
| 7936 | * it for writing. |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7937 | * |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 7938 | * TPACPI_FAN_WR_TPEC: |
Henrique de Moraes Holschuh | f51d1a3 | 2007-04-21 11:08:29 -0300 | [diff] [blame] | 7939 | * ThinkPad EC register 0x2f (HFSP): fan control loop mode |
| 7940 | * Supported on almost all ThinkPads |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7941 | * |
| 7942 | * Fan speed changes of any sort (including those caused by the |
| 7943 | * disengaged mode) are usually done slowly by the firmware as the |
Uwe Kleine-König | 9ddc5b6 | 2010-01-20 17:02:24 +0100 | [diff] [blame] | 7944 | * maximum amount of fan duty cycle change per second seems to be |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7945 | * limited. |
| 7946 | * |
| 7947 | * Reading is not available if GFAN exists. |
| 7948 | * Writing is not available if SFAN exists. |
| 7949 | * |
| 7950 | * Bits |
| 7951 | * 7 automatic mode engaged; |
| 7952 | * (default operation mode of the ThinkPad) |
| 7953 | * fan level is ignored in this mode. |
Henrique de Moraes Holschuh | f51d1a3 | 2007-04-21 11:08:29 -0300 | [diff] [blame] | 7954 | * 6 full speed mode (takes precedence over bit 7); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7955 | * not available on all thinkpads. May disable |
Henrique de Moraes Holschuh | f51d1a3 | 2007-04-21 11:08:29 -0300 | [diff] [blame] | 7956 | * the tachometer while the fan controller ramps up |
| 7957 | * the speed (which can take up to a few *minutes*). |
| 7958 | * Speeds up fan to 100% duty-cycle, which is far above |
| 7959 | * the standard RPM levels. It is not impossible that |
| 7960 | * it could cause hardware damage. |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7961 | * 5-3 unused in some models. Extra bits for fan level |
| 7962 | * in others, but still useless as all values above |
| 7963 | * 7 map to the same speed as level 7 in these models. |
| 7964 | * 2-0 fan level (0..7 usually) |
| 7965 | * 0x00 = stop |
| 7966 | * 0x07 = max (set when temperatures critical) |
| 7967 | * Some ThinkPads may have other levels, see |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 7968 | * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7969 | * |
| 7970 | * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 7971 | * boot. Apparently the EC does not initialize it, so unless ACPI DSDT |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7972 | * does so, its initial value is meaningless (0x07). |
| 7973 | * |
| 7974 | * For firmware bugs, refer to: |
Alexander A. Klimov | 19206a1 | 2020-07-13 21:06:28 +0200 | [diff] [blame] | 7975 | * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7976 | * |
| 7977 | * ---- |
| 7978 | * |
| 7979 | * ThinkPad EC register 0x84 (LSB), 0x85 (MSB): |
| 7980 | * Main fan tachometer reading (in RPM) |
| 7981 | * |
| 7982 | * This register is present on all ThinkPads with a new-style EC, and |
| 7983 | * it is known not to be present on the A21m/e, and T22, as there is |
| 7984 | * something else in offset 0x84 according to the ACPI DSDT. Other |
| 7985 | * ThinkPads from this same time period (and earlier) probably lack the |
| 7986 | * tachometer as well. |
| 7987 | * |
Nick Andrew | 877d031 | 2009-01-26 11:06:57 +0100 | [diff] [blame] | 7988 | * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7989 | * was never fixed by IBM to report the EC firmware version string |
| 7990 | * probably support the tachometer (like the early X models), so |
| 7991 | * detecting it is quite hard. We need more data to know for sure. |
| 7992 | * |
| 7993 | * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings |
| 7994 | * might result. |
| 7995 | * |
Henrique de Moraes Holschuh | f51d1a3 | 2007-04-21 11:08:29 -0300 | [diff] [blame] | 7996 | * FIRMWARE BUG: may go stale while the EC is switching to full speed |
| 7997 | * mode. |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 7998 | * |
| 7999 | * For firmware bugs, refer to: |
Alexander A. Klimov | 19206a1 | 2020-07-13 21:06:28 +0200 | [diff] [blame] | 8000 | * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8001 | * |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8002 | * ---- |
| 8003 | * |
| 8004 | * ThinkPad EC register 0x31 bit 0 (only on select models) |
| 8005 | * |
| 8006 | * When bit 0 of EC register 0x31 is zero, the tachometer registers |
| 8007 | * show the speed of the main fan. When bit 0 of EC register 0x31 |
| 8008 | * is one, the tachometer registers show the speed of the auxiliary |
| 8009 | * fan. |
| 8010 | * |
| 8011 | * Fan control seems to affect both fans, regardless of the state |
| 8012 | * of this bit. |
| 8013 | * |
| 8014 | * So far, only the firmware for the X60/X61 non-tablet versions |
| 8015 | * seem to support this (firmware TP-7M). |
| 8016 | * |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8017 | * TPACPI_FAN_WR_ACPI_FANS: |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8018 | * ThinkPad X31, X40, X41. Not available in the X60. |
| 8019 | * |
| 8020 | * FANS ACPI handle: takes three arguments: low speed, medium speed, |
| 8021 | * high speed. ACPI DSDT seems to map these three speeds to levels |
| 8022 | * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH |
| 8023 | * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3") |
| 8024 | * |
| 8025 | * The speeds are stored on handles |
| 8026 | * (FANA:FAN9), (FANC:FANB), (FANE:FAND). |
| 8027 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 8028 | * There are three default speed sets, accessible as handles: |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8029 | * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H |
| 8030 | * |
| 8031 | * ACPI DSDT switches which set is in use depending on various |
| 8032 | * factors. |
| 8033 | * |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8034 | * TPACPI_FAN_WR_TPEC is also available and should be used to |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8035 | * command the fan. The X31/X40/X41 seems to have 8 fan levels, |
| 8036 | * but the ACPI tables just mention level 7. |
| 8037 | */ |
| 8038 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 8039 | enum { /* Fan control constants */ |
| 8040 | fan_status_offset = 0x2f, /* EC register 0x2f */ |
| 8041 | fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM) |
| 8042 | * 0x84 must be read before 0x85 */ |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8043 | fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M) |
| 8044 | bit 0 selects which fan is active */ |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 8045 | |
| 8046 | TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */ |
| 8047 | TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */ |
| 8048 | |
| 8049 | TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */ |
| 8050 | }; |
| 8051 | |
| 8052 | enum fan_status_access_mode { |
| 8053 | TPACPI_FAN_NONE = 0, /* No fan status or control */ |
| 8054 | TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */ |
| 8055 | TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */ |
| 8056 | }; |
| 8057 | |
| 8058 | enum fan_control_access_mode { |
| 8059 | TPACPI_FAN_WR_NONE = 0, /* No fan control */ |
| 8060 | TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */ |
| 8061 | TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */ |
| 8062 | TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */ |
| 8063 | }; |
| 8064 | |
| 8065 | enum fan_control_commands { |
| 8066 | TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */ |
| 8067 | TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */ |
| 8068 | TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd, |
| 8069 | * and also watchdog cmd */ |
| 8070 | }; |
| 8071 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 8072 | static bool fan_control_allowed; |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 8073 | |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8074 | static enum fan_status_access_mode fan_status_access_mode; |
| 8075 | static enum fan_control_access_mode fan_control_access_mode; |
| 8076 | static enum fan_control_commands fan_control_commands; |
| 8077 | |
Henrique de Moraes Holschuh | 778b4d7 | 2006-11-24 11:47:14 -0200 | [diff] [blame] | 8078 | static u8 fan_control_initial_status; |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8079 | static u8 fan_control_desired_level; |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8080 | static u8 fan_control_resume_level; |
Henrique de Moraes Holschuh | 16663a8 | 2006-11-24 11:47:14 -0200 | [diff] [blame] | 8081 | static int fan_watchdog_maxinterval; |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 8082 | |
| 8083 | static struct mutex fan_mutex; |
| 8084 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 8085 | static void fan_watchdog_fire(struct work_struct *ignored); |
Len Brown | 25c68a3 | 2006-12-08 04:43:41 -0500 | [diff] [blame] | 8086 | static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire); |
Henrique de Moraes Holschuh | 16663a8 | 2006-11-24 11:47:14 -0200 | [diff] [blame] | 8087 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 8088 | TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */ |
| 8089 | TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */ |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8090 | "\\FSPD", /* 600e/x, 770e, 770x */ |
| 8091 | ); /* all others */ |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 8092 | TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */ |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8093 | "JFNS", /* 770x-JL */ |
| 8094 | ); /* all others */ |
| 8095 | |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8096 | /* |
Henrique de Moraes Holschuh | 1c2ece7 | 2009-01-11 03:01:08 -0200 | [diff] [blame] | 8097 | * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the |
| 8098 | * HFSP register at boot, so it contains 0x07 but the Thinkpad could |
| 8099 | * be in auto mode (0x80). |
| 8100 | * |
| 8101 | * This is corrected by any write to HFSP either by the driver, or |
| 8102 | * by the firmware. |
| 8103 | * |
| 8104 | * We assume 0x07 really means auto mode while this quirk is active, |
| 8105 | * as this is far more likely than the ThinkPad being in level 7, |
| 8106 | * which is only used by the firmware during thermal emergencies. |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 8107 | * |
| 8108 | * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52), |
| 8109 | * TP-70 (T43, R52), which are known to be buggy. |
Henrique de Moraes Holschuh | 1c2ece7 | 2009-01-11 03:01:08 -0200 | [diff] [blame] | 8110 | */ |
| 8111 | |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 8112 | static void fan_quirk1_setup(void) |
Henrique de Moraes Holschuh | 1c2ece7 | 2009-01-11 03:01:08 -0200 | [diff] [blame] | 8113 | { |
Henrique de Moraes Holschuh | 1c2ece7 | 2009-01-11 03:01:08 -0200 | [diff] [blame] | 8114 | if (fan_control_initial_status == 0x07) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 8115 | pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n"); |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 8116 | tp_features.fan_ctrl_status_undef = 1; |
Henrique de Moraes Holschuh | 1c2ece7 | 2009-01-11 03:01:08 -0200 | [diff] [blame] | 8117 | } |
| 8118 | } |
| 8119 | |
| 8120 | static void fan_quirk1_handle(u8 *fan_status) |
| 8121 | { |
| 8122 | if (unlikely(tp_features.fan_ctrl_status_undef)) { |
| 8123 | if (*fan_status != fan_control_initial_status) { |
| 8124 | /* something changed the HFSP regisnter since |
| 8125 | * driver init time, so it is not undefined |
| 8126 | * anymore */ |
| 8127 | tp_features.fan_ctrl_status_undef = 0; |
| 8128 | } else { |
| 8129 | /* Return most likely status. In fact, it |
| 8130 | * might be the only possible status */ |
| 8131 | *fan_status = TP_EC_FAN_AUTO; |
| 8132 | } |
| 8133 | } |
| 8134 | } |
| 8135 | |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8136 | /* Select main fan on X60/X61, NOOP on others */ |
| 8137 | static bool fan_select_fan1(void) |
| 8138 | { |
| 8139 | if (tp_features.second_fan) { |
| 8140 | u8 val; |
| 8141 | |
| 8142 | if (ec_read(fan_select_offset, &val) < 0) |
| 8143 | return false; |
| 8144 | val &= 0xFEU; |
| 8145 | if (ec_write(fan_select_offset, val) < 0) |
| 8146 | return false; |
| 8147 | } |
| 8148 | return true; |
| 8149 | } |
| 8150 | |
| 8151 | /* Select secondary fan on X60/X61 */ |
| 8152 | static bool fan_select_fan2(void) |
| 8153 | { |
| 8154 | u8 val; |
| 8155 | |
| 8156 | if (!tp_features.second_fan) |
| 8157 | return false; |
| 8158 | |
| 8159 | if (ec_read(fan_select_offset, &val) < 0) |
| 8160 | return false; |
| 8161 | val |= 0x01U; |
| 8162 | if (ec_write(fan_select_offset, val) < 0) |
| 8163 | return false; |
| 8164 | |
| 8165 | return true; |
| 8166 | } |
| 8167 | |
Henrique de Moraes Holschuh | 1c2ece7 | 2009-01-11 03:01:08 -0200 | [diff] [blame] | 8168 | /* |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8169 | * Call with fan_mutex held |
| 8170 | */ |
| 8171 | static void fan_update_desired_level(u8 status) |
| 8172 | { |
| 8173 | if ((status & |
| 8174 | (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) { |
| 8175 | if (status > 7) |
| 8176 | fan_control_desired_level = 7; |
| 8177 | else |
| 8178 | fan_control_desired_level = status; |
| 8179 | } |
| 8180 | } |
| 8181 | |
| 8182 | static int fan_get_status(u8 *status) |
| 8183 | { |
| 8184 | u8 s; |
| 8185 | |
| 8186 | /* TODO: |
| 8187 | * Add TPACPI_FAN_RD_ACPI_FANS ? */ |
| 8188 | |
| 8189 | switch (fan_status_access_mode) { |
Dan Carpenter | eceeb43 | 2012-09-01 12:54:07 -0700 | [diff] [blame] | 8190 | case TPACPI_FAN_RD_ACPI_GFAN: { |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8191 | /* 570, 600e/x, 770e, 770x */ |
Dan Carpenter | eceeb43 | 2012-09-01 12:54:07 -0700 | [diff] [blame] | 8192 | int res; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8193 | |
Dan Carpenter | eceeb43 | 2012-09-01 12:54:07 -0700 | [diff] [blame] | 8194 | if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d"))) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8195 | return -EIO; |
| 8196 | |
| 8197 | if (likely(status)) |
Dan Carpenter | eceeb43 | 2012-09-01 12:54:07 -0700 | [diff] [blame] | 8198 | *status = res & 0x07; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8199 | |
| 8200 | break; |
Dan Carpenter | eceeb43 | 2012-09-01 12:54:07 -0700 | [diff] [blame] | 8201 | } |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8202 | case TPACPI_FAN_RD_TPEC: |
| 8203 | /* all except 570, 600e/x, 770e, 770x */ |
| 8204 | if (unlikely(!acpi_ec_read(fan_status_offset, &s))) |
| 8205 | return -EIO; |
| 8206 | |
Henrique de Moraes Holschuh | 1c2ece7 | 2009-01-11 03:01:08 -0200 | [diff] [blame] | 8207 | if (likely(status)) { |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8208 | *status = s; |
Henrique de Moraes Holschuh | 1c2ece7 | 2009-01-11 03:01:08 -0200 | [diff] [blame] | 8209 | fan_quirk1_handle(status); |
| 8210 | } |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8211 | |
| 8212 | break; |
| 8213 | |
| 8214 | default: |
| 8215 | return -ENXIO; |
| 8216 | } |
| 8217 | |
| 8218 | return 0; |
| 8219 | } |
| 8220 | |
| 8221 | static int fan_get_status_safe(u8 *status) |
| 8222 | { |
| 8223 | int rc; |
| 8224 | u8 s; |
| 8225 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 8226 | if (mutex_lock_killable(&fan_mutex)) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8227 | return -ERESTARTSYS; |
| 8228 | rc = fan_get_status(&s); |
| 8229 | if (!rc) |
| 8230 | fan_update_desired_level(s); |
| 8231 | mutex_unlock(&fan_mutex); |
| 8232 | |
Dan Carpenter | a771836 | 2016-04-15 17:46:34 +0300 | [diff] [blame] | 8233 | if (rc) |
| 8234 | return rc; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8235 | if (status) |
| 8236 | *status = s; |
| 8237 | |
Dan Carpenter | a771836 | 2016-04-15 17:46:34 +0300 | [diff] [blame] | 8238 | return 0; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8239 | } |
| 8240 | |
| 8241 | static int fan_get_speed(unsigned int *speed) |
| 8242 | { |
| 8243 | u8 hi, lo; |
| 8244 | |
| 8245 | switch (fan_status_access_mode) { |
| 8246 | case TPACPI_FAN_RD_TPEC: |
| 8247 | /* all except 570, 600e/x, 770e, 770x */ |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8248 | if (unlikely(!fan_select_fan1())) |
| 8249 | return -EIO; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8250 | if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) || |
| 8251 | !acpi_ec_read(fan_rpm_offset + 1, &hi))) |
| 8252 | return -EIO; |
| 8253 | |
| 8254 | if (likely(speed)) |
| 8255 | *speed = (hi << 8) | lo; |
| 8256 | |
| 8257 | break; |
| 8258 | |
| 8259 | default: |
| 8260 | return -ENXIO; |
| 8261 | } |
| 8262 | |
| 8263 | return 0; |
| 8264 | } |
| 8265 | |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8266 | static int fan2_get_speed(unsigned int *speed) |
| 8267 | { |
| 8268 | u8 hi, lo; |
| 8269 | bool rc; |
| 8270 | |
| 8271 | switch (fan_status_access_mode) { |
| 8272 | case TPACPI_FAN_RD_TPEC: |
| 8273 | /* all except 570, 600e/x, 770e, 770x */ |
| 8274 | if (unlikely(!fan_select_fan2())) |
| 8275 | return -EIO; |
| 8276 | rc = !acpi_ec_read(fan_rpm_offset, &lo) || |
| 8277 | !acpi_ec_read(fan_rpm_offset + 1, &hi); |
| 8278 | fan_select_fan1(); /* play it safe */ |
| 8279 | if (rc) |
| 8280 | return -EIO; |
| 8281 | |
| 8282 | if (likely(speed)) |
| 8283 | *speed = (hi << 8) | lo; |
| 8284 | |
| 8285 | break; |
| 8286 | |
| 8287 | default: |
| 8288 | return -ENXIO; |
| 8289 | } |
| 8290 | |
| 8291 | return 0; |
| 8292 | } |
| 8293 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8294 | static int fan_set_level(int level) |
| 8295 | { |
| 8296 | if (!fan_control_allowed) |
| 8297 | return -EPERM; |
| 8298 | |
| 8299 | switch (fan_control_access_mode) { |
| 8300 | case TPACPI_FAN_WR_ACPI_SFAN: |
Lars Hofhansl | 14232c6 | 2020-04-23 14:57:09 -0700 | [diff] [blame] | 8301 | if ((level < 0) || (level > 7)) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8302 | return -EINVAL; |
Lars Hofhansl | 14232c6 | 2020-04-23 14:57:09 -0700 | [diff] [blame] | 8303 | |
| 8304 | if (tp_features.second_fan_ctl) { |
| 8305 | if (!fan_select_fan2() || |
| 8306 | !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) { |
| 8307 | pr_warn("Couldn't set 2nd fan level, disabling support\n"); |
| 8308 | tp_features.second_fan_ctl = 0; |
| 8309 | } |
| 8310 | fan_select_fan1(); |
| 8311 | } |
| 8312 | if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) |
| 8313 | return -EIO; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8314 | break; |
| 8315 | |
| 8316 | case TPACPI_FAN_WR_ACPI_FANS: |
| 8317 | case TPACPI_FAN_WR_TPEC: |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8318 | if (!(level & TP_EC_FAN_AUTO) && |
| 8319 | !(level & TP_EC_FAN_FULLSPEED) && |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8320 | ((level < 0) || (level > 7))) |
| 8321 | return -EINVAL; |
| 8322 | |
| 8323 | /* safety net should the EC not support AUTO |
| 8324 | * or FULLSPEED mode bits and just ignore them */ |
| 8325 | if (level & TP_EC_FAN_FULLSPEED) |
| 8326 | level |= 7; /* safety min speed 7 */ |
Roel Kluin | 547266e | 2008-02-05 00:24:56 +0100 | [diff] [blame] | 8327 | else if (level & TP_EC_FAN_AUTO) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8328 | level |= 4; /* safety min speed 4 */ |
| 8329 | |
Lars Hofhansl | 14232c6 | 2020-04-23 14:57:09 -0700 | [diff] [blame] | 8330 | if (tp_features.second_fan_ctl) { |
| 8331 | if (!fan_select_fan2() || |
| 8332 | !acpi_ec_write(fan_status_offset, level)) { |
| 8333 | pr_warn("Couldn't set 2nd fan level, disabling support\n"); |
| 8334 | tp_features.second_fan_ctl = 0; |
| 8335 | } |
| 8336 | fan_select_fan1(); |
| 8337 | |
| 8338 | } |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8339 | if (!acpi_ec_write(fan_status_offset, level)) |
| 8340 | return -EIO; |
| 8341 | else |
| 8342 | tp_features.fan_ctrl_status_undef = 0; |
| 8343 | break; |
| 8344 | |
| 8345 | default: |
| 8346 | return -ENXIO; |
| 8347 | } |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 8348 | |
| 8349 | vdbg_printk(TPACPI_DBG_FAN, |
| 8350 | "fan control: set fan control register to 0x%02x\n", level); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8351 | return 0; |
| 8352 | } |
| 8353 | |
| 8354 | static int fan_set_level_safe(int level) |
| 8355 | { |
| 8356 | int rc; |
| 8357 | |
| 8358 | if (!fan_control_allowed) |
| 8359 | return -EPERM; |
| 8360 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 8361 | if (mutex_lock_killable(&fan_mutex)) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8362 | return -ERESTARTSYS; |
| 8363 | |
| 8364 | if (level == TPACPI_FAN_LAST_LEVEL) |
| 8365 | level = fan_control_desired_level; |
| 8366 | |
| 8367 | rc = fan_set_level(level); |
| 8368 | if (!rc) |
| 8369 | fan_update_desired_level(level); |
| 8370 | |
| 8371 | mutex_unlock(&fan_mutex); |
| 8372 | return rc; |
| 8373 | } |
| 8374 | |
| 8375 | static int fan_set_enable(void) |
| 8376 | { |
| 8377 | u8 s; |
| 8378 | int rc; |
| 8379 | |
| 8380 | if (!fan_control_allowed) |
| 8381 | return -EPERM; |
| 8382 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 8383 | if (mutex_lock_killable(&fan_mutex)) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8384 | return -ERESTARTSYS; |
| 8385 | |
| 8386 | switch (fan_control_access_mode) { |
| 8387 | case TPACPI_FAN_WR_ACPI_FANS: |
| 8388 | case TPACPI_FAN_WR_TPEC: |
| 8389 | rc = fan_get_status(&s); |
| 8390 | if (rc < 0) |
| 8391 | break; |
| 8392 | |
| 8393 | /* Don't go out of emergency fan mode */ |
| 8394 | if (s != 7) { |
| 8395 | s &= 0x07; |
| 8396 | s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */ |
| 8397 | } |
| 8398 | |
| 8399 | if (!acpi_ec_write(fan_status_offset, s)) |
| 8400 | rc = -EIO; |
| 8401 | else { |
| 8402 | tp_features.fan_ctrl_status_undef = 0; |
| 8403 | rc = 0; |
| 8404 | } |
| 8405 | break; |
| 8406 | |
| 8407 | case TPACPI_FAN_WR_ACPI_SFAN: |
| 8408 | rc = fan_get_status(&s); |
| 8409 | if (rc < 0) |
| 8410 | break; |
| 8411 | |
| 8412 | s &= 0x07; |
| 8413 | |
| 8414 | /* Set fan to at least level 4 */ |
| 8415 | s |= 4; |
| 8416 | |
| 8417 | if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s)) |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 8418 | rc = -EIO; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8419 | else |
| 8420 | rc = 0; |
| 8421 | break; |
| 8422 | |
| 8423 | default: |
| 8424 | rc = -ENXIO; |
| 8425 | } |
| 8426 | |
| 8427 | mutex_unlock(&fan_mutex); |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 8428 | |
| 8429 | if (!rc) |
| 8430 | vdbg_printk(TPACPI_DBG_FAN, |
| 8431 | "fan control: set fan control register to 0x%02x\n", |
| 8432 | s); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8433 | return rc; |
| 8434 | } |
| 8435 | |
| 8436 | static int fan_set_disable(void) |
| 8437 | { |
| 8438 | int rc; |
| 8439 | |
| 8440 | if (!fan_control_allowed) |
| 8441 | return -EPERM; |
| 8442 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 8443 | if (mutex_lock_killable(&fan_mutex)) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8444 | return -ERESTARTSYS; |
| 8445 | |
| 8446 | rc = 0; |
| 8447 | switch (fan_control_access_mode) { |
| 8448 | case TPACPI_FAN_WR_ACPI_FANS: |
| 8449 | case TPACPI_FAN_WR_TPEC: |
| 8450 | if (!acpi_ec_write(fan_status_offset, 0x00)) |
| 8451 | rc = -EIO; |
| 8452 | else { |
| 8453 | fan_control_desired_level = 0; |
| 8454 | tp_features.fan_ctrl_status_undef = 0; |
| 8455 | } |
| 8456 | break; |
| 8457 | |
| 8458 | case TPACPI_FAN_WR_ACPI_SFAN: |
| 8459 | if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00)) |
| 8460 | rc = -EIO; |
| 8461 | else |
| 8462 | fan_control_desired_level = 0; |
| 8463 | break; |
| 8464 | |
| 8465 | default: |
| 8466 | rc = -ENXIO; |
| 8467 | } |
| 8468 | |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 8469 | if (!rc) |
| 8470 | vdbg_printk(TPACPI_DBG_FAN, |
| 8471 | "fan control: set fan control register to 0\n"); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8472 | |
| 8473 | mutex_unlock(&fan_mutex); |
| 8474 | return rc; |
| 8475 | } |
| 8476 | |
| 8477 | static int fan_set_speed(int speed) |
| 8478 | { |
| 8479 | int rc; |
| 8480 | |
| 8481 | if (!fan_control_allowed) |
| 8482 | return -EPERM; |
| 8483 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 8484 | if (mutex_lock_killable(&fan_mutex)) |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8485 | return -ERESTARTSYS; |
| 8486 | |
| 8487 | rc = 0; |
| 8488 | switch (fan_control_access_mode) { |
| 8489 | case TPACPI_FAN_WR_ACPI_FANS: |
| 8490 | if (speed >= 0 && speed <= 65535) { |
| 8491 | if (!acpi_evalf(fans_handle, NULL, NULL, "vddd", |
| 8492 | speed, speed, speed)) |
| 8493 | rc = -EIO; |
| 8494 | } else |
| 8495 | rc = -EINVAL; |
| 8496 | break; |
| 8497 | |
| 8498 | default: |
| 8499 | rc = -ENXIO; |
| 8500 | } |
| 8501 | |
| 8502 | mutex_unlock(&fan_mutex); |
| 8503 | return rc; |
| 8504 | } |
| 8505 | |
| 8506 | static void fan_watchdog_reset(void) |
| 8507 | { |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8508 | if (fan_control_access_mode == TPACPI_FAN_WR_NONE) |
| 8509 | return; |
| 8510 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8511 | if (fan_watchdog_maxinterval > 0 && |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 8512 | tpacpi_lifecycle != TPACPI_LIFE_EXITING) |
| 8513 | mod_delayed_work(tpacpi_wq, &fan_watchdog_task, |
| 8514 | msecs_to_jiffies(fan_watchdog_maxinterval * 1000)); |
| 8515 | else |
| 8516 | cancel_delayed_work(&fan_watchdog_task); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8517 | } |
| 8518 | |
| 8519 | static void fan_watchdog_fire(struct work_struct *ignored) |
| 8520 | { |
| 8521 | int rc; |
| 8522 | |
| 8523 | if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING) |
| 8524 | return; |
| 8525 | |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 8526 | pr_notice("fan watchdog: enabling fan\n"); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8527 | rc = fan_set_enable(); |
| 8528 | if (rc < 0) { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 8529 | pr_err("fan watchdog: error %d while enabling fan, will try again later...\n", |
| 8530 | rc); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 8531 | /* reschedule for later */ |
| 8532 | fan_watchdog_reset(); |
| 8533 | } |
| 8534 | } |
| 8535 | |
| 8536 | /* |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8537 | * SYSFS fan layout: hwmon compatible (device) |
| 8538 | * |
| 8539 | * pwm*_enable: |
| 8540 | * 0: "disengaged" mode |
| 8541 | * 1: manual mode |
| 8542 | * 2: native EC "auto" mode (recommended, hardware default) |
| 8543 | * |
| 8544 | * pwm*: set speed in manual mode, ignored otherwise. |
| 8545 | * 0 is level 0; 255 is level 7. Intermediate points done with linear |
| 8546 | * interpolation. |
| 8547 | * |
| 8548 | * fan*_input: tachometer reading, RPM |
| 8549 | * |
| 8550 | * |
| 8551 | * SYSFS fan layout: extensions |
| 8552 | * |
| 8553 | * fan_watchdog (driver): |
| 8554 | * fan watchdog interval in seconds, 0 disables (default), max 120 |
| 8555 | */ |
| 8556 | |
| 8557 | /* sysfs fan pwm1_enable ----------------------------------------------- */ |
| 8558 | static ssize_t fan_pwm1_enable_show(struct device *dev, |
| 8559 | struct device_attribute *attr, |
| 8560 | char *buf) |
| 8561 | { |
| 8562 | int res, mode; |
| 8563 | u8 status; |
| 8564 | |
| 8565 | res = fan_get_status_safe(&status); |
| 8566 | if (res) |
| 8567 | return res; |
| 8568 | |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8569 | if (status & TP_EC_FAN_FULLSPEED) { |
| 8570 | mode = 0; |
| 8571 | } else if (status & TP_EC_FAN_AUTO) { |
| 8572 | mode = 2; |
| 8573 | } else |
| 8574 | mode = 1; |
| 8575 | |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 8576 | return sysfs_emit(buf, "%d\n", mode); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8577 | } |
| 8578 | |
| 8579 | static ssize_t fan_pwm1_enable_store(struct device *dev, |
| 8580 | struct device_attribute *attr, |
| 8581 | const char *buf, size_t count) |
| 8582 | { |
| 8583 | unsigned long t; |
| 8584 | int res, level; |
| 8585 | |
| 8586 | if (parse_strtoul(buf, 2, &t)) |
| 8587 | return -EINVAL; |
| 8588 | |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 8589 | tpacpi_disclose_usertask("hwmon pwm1_enable", |
| 8590 | "set fan mode to %lu\n", t); |
| 8591 | |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8592 | switch (t) { |
| 8593 | case 0: |
| 8594 | level = TP_EC_FAN_FULLSPEED; |
| 8595 | break; |
| 8596 | case 1: |
| 8597 | level = TPACPI_FAN_LAST_LEVEL; |
| 8598 | break; |
| 8599 | case 2: |
| 8600 | level = TP_EC_FAN_AUTO; |
| 8601 | break; |
| 8602 | case 3: |
| 8603 | /* reserved for software-controlled auto mode */ |
| 8604 | return -ENOSYS; |
| 8605 | default: |
| 8606 | return -EINVAL; |
| 8607 | } |
| 8608 | |
| 8609 | res = fan_set_level_safe(level); |
Henrique de Moraes Holschuh | c573ddb | 2007-04-27 22:00:12 -0300 | [diff] [blame] | 8610 | if (res == -ENXIO) |
| 8611 | return -EINVAL; |
| 8612 | else if (res < 0) |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8613 | return res; |
| 8614 | |
| 8615 | fan_watchdog_reset(); |
| 8616 | |
| 8617 | return count; |
| 8618 | } |
| 8619 | |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 8620 | static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, |
| 8621 | fan_pwm1_enable_show, fan_pwm1_enable_store); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8622 | |
| 8623 | /* sysfs fan pwm1 ------------------------------------------------------ */ |
| 8624 | static ssize_t fan_pwm1_show(struct device *dev, |
| 8625 | struct device_attribute *attr, |
| 8626 | char *buf) |
| 8627 | { |
| 8628 | int res; |
| 8629 | u8 status; |
| 8630 | |
| 8631 | res = fan_get_status_safe(&status); |
| 8632 | if (res) |
| 8633 | return res; |
| 8634 | |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8635 | if ((status & |
| 8636 | (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0) |
| 8637 | status = fan_control_desired_level; |
| 8638 | |
| 8639 | if (status > 7) |
| 8640 | status = 7; |
| 8641 | |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 8642 | return sysfs_emit(buf, "%u\n", (status * 255) / 7); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8643 | } |
| 8644 | |
| 8645 | static ssize_t fan_pwm1_store(struct device *dev, |
| 8646 | struct device_attribute *attr, |
| 8647 | const char *buf, size_t count) |
| 8648 | { |
| 8649 | unsigned long s; |
| 8650 | int rc; |
| 8651 | u8 status, newlevel; |
| 8652 | |
| 8653 | if (parse_strtoul(buf, 255, &s)) |
| 8654 | return -EINVAL; |
| 8655 | |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 8656 | tpacpi_disclose_usertask("hwmon pwm1", |
| 8657 | "set fan speed to %lu\n", s); |
| 8658 | |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8659 | /* scale down from 0-255 to 0-7 */ |
| 8660 | newlevel = (s >> 5) & 0x07; |
| 8661 | |
Henrique de Moraes Holschuh | 7646ea8 | 2009-01-11 03:01:04 -0200 | [diff] [blame] | 8662 | if (mutex_lock_killable(&fan_mutex)) |
Henrique de Moraes Holschuh | fc589a3 | 2007-10-30 17:46:24 -0200 | [diff] [blame] | 8663 | return -ERESTARTSYS; |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8664 | |
| 8665 | rc = fan_get_status(&status); |
| 8666 | if (!rc && (status & |
| 8667 | (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) { |
| 8668 | rc = fan_set_level(newlevel); |
Henrique de Moraes Holschuh | c573ddb | 2007-04-27 22:00:12 -0300 | [diff] [blame] | 8669 | if (rc == -ENXIO) |
| 8670 | rc = -EINVAL; |
| 8671 | else if (!rc) { |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8672 | fan_update_desired_level(newlevel); |
Henrique de Moraes Holschuh | ca4ac2f | 2007-04-27 22:00:11 -0300 | [diff] [blame] | 8673 | fan_watchdog_reset(); |
| 8674 | } |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8675 | } |
| 8676 | |
| 8677 | mutex_unlock(&fan_mutex); |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 8678 | return (rc) ? rc : count; |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8679 | } |
| 8680 | |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 8681 | static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8682 | |
| 8683 | /* sysfs fan fan1_input ------------------------------------------------ */ |
| 8684 | static ssize_t fan_fan1_input_show(struct device *dev, |
| 8685 | struct device_attribute *attr, |
| 8686 | char *buf) |
| 8687 | { |
| 8688 | int res; |
| 8689 | unsigned int speed; |
| 8690 | |
| 8691 | res = fan_get_speed(&speed); |
| 8692 | if (res < 0) |
| 8693 | return res; |
| 8694 | |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 8695 | return sysfs_emit(buf, "%u\n", speed); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8696 | } |
| 8697 | |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 8698 | static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8699 | |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8700 | /* sysfs fan fan2_input ------------------------------------------------ */ |
| 8701 | static ssize_t fan_fan2_input_show(struct device *dev, |
| 8702 | struct device_attribute *attr, |
| 8703 | char *buf) |
| 8704 | { |
| 8705 | int res; |
| 8706 | unsigned int speed; |
| 8707 | |
| 8708 | res = fan2_get_speed(&speed); |
| 8709 | if (res < 0) |
| 8710 | return res; |
| 8711 | |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 8712 | return sysfs_emit(buf, "%u\n", speed); |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8713 | } |
| 8714 | |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 8715 | static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL); |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8716 | |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 8717 | /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */ |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 8718 | static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf) |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8719 | { |
Ye Guojin | 33ce79b | 2021-10-18 09:17:50 +0000 | [diff] [blame] | 8720 | return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8721 | } |
| 8722 | |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 8723 | static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf, |
| 8724 | size_t count) |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8725 | { |
| 8726 | unsigned long t; |
| 8727 | |
| 8728 | if (parse_strtoul(buf, 120, &t)) |
| 8729 | return -EINVAL; |
| 8730 | |
Henrique de Moraes Holschuh | ecf2a80 | 2007-04-27 22:00:09 -0300 | [diff] [blame] | 8731 | if (!fan_control_allowed) |
| 8732 | return -EPERM; |
| 8733 | |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8734 | fan_watchdog_maxinterval = t; |
| 8735 | fan_watchdog_reset(); |
| 8736 | |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 8737 | tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t); |
| 8738 | |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8739 | return count; |
| 8740 | } |
Greg Kroah-Hartman | ac3054c | 2017-06-09 11:03:12 +0200 | [diff] [blame] | 8741 | static DRIVER_ATTR_RW(fan_watchdog); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8742 | |
| 8743 | /* --------------------------------------------------------------------- */ |
| 8744 | static struct attribute *fan_attributes[] = { |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 8745 | &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr, |
| 8746 | &dev_attr_fan1_input.attr, |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8747 | NULL, /* for fan2_input */ |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8748 | NULL |
| 8749 | }; |
| 8750 | |
| 8751 | static const struct attribute_group fan_attr_group = { |
| 8752 | .attrs = fan_attributes, |
| 8753 | }; |
| 8754 | |
Jouke Witteveen | 599eeff | 2018-12-05 12:17:52 +0100 | [diff] [blame] | 8755 | #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */ |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8756 | #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */ |
Lars Hofhansl | 14232c6 | 2020-04-23 14:57:09 -0700 | [diff] [blame] | 8757 | #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */ |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 8758 | |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 8759 | static const struct tpacpi_quirk fan_quirk_table[] __initconst = { |
Jouke Witteveen | 599eeff | 2018-12-05 12:17:52 +0100 | [diff] [blame] | 8760 | TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1), |
| 8761 | TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1), |
| 8762 | TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1), |
| 8763 | TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1), |
| 8764 | TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN), |
| 8765 | TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN), |
Lars Hofhansl | 14232c6 | 2020-04-23 14:57:09 -0700 | [diff] [blame] | 8766 | TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */ |
| 8767 | TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */ |
| 8768 | TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */ |
| 8769 | TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */ |
| 8770 | TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */ |
Jeannie Stevenson | c47c042 | 2021-01-15 16:06:30 +0000 | [diff] [blame] | 8771 | TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */ |
Lars Hofhansl | 14232c6 | 2020-04-23 14:57:09 -0700 | [diff] [blame] | 8772 | TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */ |
| 8773 | TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */ |
Iakov 'Jake' Kirilenko | c758be8 | 2020-11-05 18:25:56 +0300 | [diff] [blame] | 8774 | TPACPI_Q_LNV3('N', '2', 'V', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (3nd gen) */ |
Jimmy Wang | 1f33895 | 2021-11-05 17:05:28 +0800 | [diff] [blame] | 8775 | TPACPI_Q_LNV3('N', '4', '0', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (4nd gen) */ |
Matthias Maier | e8637df | 2020-11-25 18:04:16 -0600 | [diff] [blame] | 8776 | TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */ |
Til Jasper Ullrich | 25acf21 | 2021-05-25 17:09:52 +0200 | [diff] [blame] | 8777 | TPACPI_Q_LNV3('N', '3', '2', TPACPI_FAN_2CTL), /* X1 Carbon (9th gen) */ |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 8778 | }; |
| 8779 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 8780 | static int __init fan_init(struct ibm_init_struct *iibm) |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8781 | { |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8782 | int rc; |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 8783 | unsigned long quirks; |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8784 | |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 8785 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN, |
| 8786 | "initializing fan subdriver\n"); |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 8787 | |
Henrique de Moraes Holschuh | 40ca9fd | 2007-04-24 11:48:15 -0300 | [diff] [blame] | 8788 | mutex_init(&fan_mutex); |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8789 | fan_status_access_mode = TPACPI_FAN_NONE; |
| 8790 | fan_control_access_mode = TPACPI_FAN_WR_NONE; |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8791 | fan_control_commands = 0; |
Henrique de Moraes Holschuh | 16663a8 | 2006-11-24 11:47:14 -0200 | [diff] [blame] | 8792 | fan_watchdog_maxinterval = 0; |
Henrique de Moraes Holschuh | d8fd94d | 2007-04-21 11:08:36 -0300 | [diff] [blame] | 8793 | tp_features.fan_ctrl_status_undef = 0; |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8794 | tp_features.second_fan = 0; |
Lars Hofhansl | 14232c6 | 2020-04-23 14:57:09 -0700 | [diff] [blame] | 8795 | tp_features.second_fan_ctl = 0; |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8796 | fan_control_desired_level = 7; |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8797 | |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 8798 | if (tpacpi_is_ibm()) { |
| 8799 | TPACPI_ACPIHANDLE_INIT(fans); |
| 8800 | TPACPI_ACPIHANDLE_INIT(gfan); |
| 8801 | TPACPI_ACPIHANDLE_INIT(sfan); |
| 8802 | } |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 8803 | |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 8804 | quirks = tpacpi_check_quirks(fan_quirk_table, |
| 8805 | ARRAY_SIZE(fan_quirk_table)); |
| 8806 | |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8807 | if (gfan_handle) { |
| 8808 | /* 570, 600e/x, 770e, 770x */ |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8809 | fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN; |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8810 | } else { |
| 8811 | /* all other ThinkPads: note that even old-style |
| 8812 | * ThinkPad ECs supports the fan control register */ |
Henrique de Moraes Holschuh | 778b4d7 | 2006-11-24 11:47:14 -0200 | [diff] [blame] | 8813 | if (likely(acpi_ec_read(fan_status_offset, |
| 8814 | &fan_control_initial_status))) { |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8815 | fan_status_access_mode = TPACPI_FAN_RD_TPEC; |
Henrique de Moraes Holschuh | 7d95a3d | 2009-05-30 13:25:06 -0300 | [diff] [blame] | 8816 | if (quirks & TPACPI_FAN_Q1) |
| 8817 | fan_quirk1_setup(); |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8818 | if (quirks & TPACPI_FAN_2FAN) { |
| 8819 | tp_features.second_fan = 1; |
Lars Hofhansl | 14232c6 | 2020-04-23 14:57:09 -0700 | [diff] [blame] | 8820 | pr_info("secondary fan support enabled\n"); |
| 8821 | } |
| 8822 | if (quirks & TPACPI_FAN_2CTL) { |
| 8823 | tp_features.second_fan = 1; |
| 8824 | tp_features.second_fan_ctl = 1; |
| 8825 | pr_info("secondary fan control enabled\n"); |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8826 | } |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8827 | } else { |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 8828 | pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n"); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 8829 | return 1; |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8830 | } |
| 8831 | } |
| 8832 | |
| 8833 | if (sfan_handle) { |
| 8834 | /* 570, 770x-JL */ |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8835 | fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN; |
Henrique de Moraes Holschuh | 1c6a334 | 2006-11-24 11:47:12 -0200 | [diff] [blame] | 8836 | fan_control_commands |= |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8837 | TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE; |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8838 | } else { |
| 8839 | if (!gfan_handle) { |
| 8840 | /* gfan without sfan means no fan control */ |
| 8841 | /* all other models implement TP EC 0x2f control */ |
| 8842 | |
| 8843 | if (fans_handle) { |
Henrique de Moraes Holschuh | a8b7a66 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 8844 | /* X31, X40, X41 */ |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8845 | fan_control_access_mode = |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8846 | TPACPI_FAN_WR_ACPI_FANS; |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8847 | fan_control_commands |= |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8848 | TPACPI_FAN_CMD_SPEED | |
| 8849 | TPACPI_FAN_CMD_LEVEL | |
| 8850 | TPACPI_FAN_CMD_ENABLE; |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8851 | } else { |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8852 | fan_control_access_mode = TPACPI_FAN_WR_TPEC; |
Henrique de Moraes Holschuh | a12095c | 2006-11-24 11:47:13 -0200 | [diff] [blame] | 8853 | fan_control_commands |= |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8854 | TPACPI_FAN_CMD_LEVEL | |
| 8855 | TPACPI_FAN_CMD_ENABLE; |
Henrique de Moraes Holschuh | 69ba91c | 2006-11-24 11:47:09 -0200 | [diff] [blame] | 8856 | } |
| 8857 | } |
| 8858 | } |
| 8859 | |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 8860 | vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN, |
| 8861 | "fan is %s, modes %d, %d\n", |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 8862 | str_supported(fan_status_access_mode != TPACPI_FAN_NONE || |
| 8863 | fan_control_access_mode != TPACPI_FAN_WR_NONE), |
| 8864 | fan_status_access_mode, fan_control_access_mode); |
| 8865 | |
Henrique de Moraes Holschuh | ecf2a80 | 2007-04-27 22:00:09 -0300 | [diff] [blame] | 8866 | /* fan control master switch */ |
| 8867 | if (!fan_control_allowed) { |
| 8868 | fan_control_access_mode = TPACPI_FAN_WR_NONE; |
| 8869 | fan_control_commands = 0; |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 8870 | dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN, |
Henrique de Moraes Holschuh | ecf2a80 | 2007-04-27 22:00:09 -0300 | [diff] [blame] | 8871 | "fan control features disabled by parameter\n"); |
| 8872 | } |
| 8873 | |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8874 | /* update fan_control_desired_level */ |
| 8875 | if (fan_status_access_mode != TPACPI_FAN_NONE) |
| 8876 | fan_get_status_safe(NULL); |
| 8877 | |
| 8878 | if (fan_status_access_mode != TPACPI_FAN_NONE || |
| 8879 | fan_control_access_mode != TPACPI_FAN_WR_NONE) { |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8880 | if (tp_features.second_fan) { |
| 8881 | /* attach second fan tachometer */ |
| 8882 | fan_attributes[ARRAY_SIZE(fan_attributes)-2] = |
Bjørn Mork | 5fb73bc | 2015-05-19 19:45:03 +0200 | [diff] [blame] | 8883 | &dev_attr_fan2_input.attr; |
Henrique de Moraes Holschuh | d737724 | 2009-06-18 00:40:17 -0300 | [diff] [blame] | 8884 | } |
Stanislav Fomichev | 6b99e35 | 2017-06-20 20:45:13 -0700 | [diff] [blame] | 8885 | rc = sysfs_create_group(&tpacpi_hwmon->kobj, |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8886 | &fan_attr_group); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8887 | if (rc < 0) |
| 8888 | return rc; |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 8889 | |
| 8890 | rc = driver_create_file(&tpacpi_hwmon_pdriver.driver, |
| 8891 | &driver_attr_fan_watchdog); |
| 8892 | if (rc < 0) { |
Stanislav Fomichev | 6b99e35 | 2017-06-20 20:45:13 -0700 | [diff] [blame] | 8893 | sysfs_remove_group(&tpacpi_hwmon->kobj, |
Henrique de Moraes Holschuh | 9c0a76e | 2008-06-03 23:36:10 -0300 | [diff] [blame] | 8894 | &fan_attr_group); |
| 8895 | return rc; |
| 8896 | } |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8897 | return 0; |
| 8898 | } else |
| 8899 | return 1; |
| 8900 | } |
| 8901 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8902 | static void fan_exit(void) |
| 8903 | { |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 8904 | vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN, |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 8905 | "cancelling any pending fan watchdog tasks\n"); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8906 | |
| 8907 | /* FIXME: can we really do this unconditionally? */ |
Stanislav Fomichev | 6b99e35 | 2017-06-20 20:45:13 -0700 | [diff] [blame] | 8908 | sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group); |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 8909 | driver_remove_file(&tpacpi_hwmon_pdriver.driver, |
| 8910 | &driver_attr_fan_watchdog); |
Henrique de Moraes Holschuh | fe98a52 | 2007-04-24 11:48:17 -0300 | [diff] [blame] | 8911 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8912 | cancel_delayed_work(&fan_watchdog_task); |
Henrique de Moraes Holschuh | e0e3c06 | 2008-04-26 01:02:28 -0300 | [diff] [blame] | 8913 | flush_workqueue(tpacpi_wq); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8914 | } |
| 8915 | |
Rafael J. Wysocki | fd3c3a4 | 2012-06-27 23:18:44 +0200 | [diff] [blame] | 8916 | static void fan_suspend(void) |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8917 | { |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8918 | int rc; |
| 8919 | |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8920 | if (!fan_control_allowed) |
| 8921 | return; |
| 8922 | |
| 8923 | /* Store fan status in cache */ |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8924 | fan_control_resume_level = 0; |
| 8925 | rc = fan_get_status_safe(&fan_control_resume_level); |
| 8926 | if (rc < 0) |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 8927 | pr_notice("failed to read fan level for later restore during resume: %d\n", |
| 8928 | rc); |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8929 | |
| 8930 | /* if it is undefined, don't attempt to restore it. |
| 8931 | * KEEP THIS LAST */ |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8932 | if (tp_features.fan_ctrl_status_undef) |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8933 | fan_control_resume_level = 0; |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8934 | } |
| 8935 | |
| 8936 | static void fan_resume(void) |
| 8937 | { |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8938 | u8 current_level = 7; |
| 8939 | bool do_set = false; |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8940 | int rc; |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8941 | |
| 8942 | /* DSDT *always* updates status on resume */ |
| 8943 | tp_features.fan_ctrl_status_undef = 0; |
| 8944 | |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8945 | if (!fan_control_allowed || |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8946 | !fan_control_resume_level || |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8947 | (fan_get_status_safe(¤t_level) < 0)) |
| 8948 | return; |
| 8949 | |
| 8950 | switch (fan_control_access_mode) { |
| 8951 | case TPACPI_FAN_WR_ACPI_SFAN: |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8952 | /* never decrease fan level */ |
| 8953 | do_set = (fan_control_resume_level > current_level); |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8954 | break; |
| 8955 | case TPACPI_FAN_WR_ACPI_FANS: |
| 8956 | case TPACPI_FAN_WR_TPEC: |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8957 | /* never decrease fan level, scale is: |
| 8958 | * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO |
| 8959 | * |
| 8960 | * We expect the firmware to set either 7 or AUTO, but we |
| 8961 | * handle FULLSPEED out of paranoia. |
| 8962 | * |
| 8963 | * So, we can safely only restore FULLSPEED or 7, anything |
| 8964 | * else could slow the fan. Restoring AUTO is useless, at |
| 8965 | * best that's exactly what the DSDT already set (it is the |
| 8966 | * slower it uses). |
| 8967 | * |
| 8968 | * Always keep in mind that the DSDT *will* have set the |
| 8969 | * fans to what the vendor supposes is the best level. We |
| 8970 | * muck with it only to speed the fan up. |
| 8971 | */ |
| 8972 | if (fan_control_resume_level != 7 && |
| 8973 | !(fan_control_resume_level & TP_EC_FAN_FULLSPEED)) |
| 8974 | return; |
| 8975 | else |
| 8976 | do_set = !(current_level & TP_EC_FAN_FULLSPEED) && |
| 8977 | (current_level != fan_control_resume_level); |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8978 | break; |
| 8979 | default: |
| 8980 | return; |
| 8981 | } |
| 8982 | if (do_set) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 8983 | pr_notice("restoring fan level to 0x%02x\n", |
| 8984 | fan_control_resume_level); |
Henrique de Moraes Holschuh | 0081b16 | 2008-11-09 10:54:02 -0200 | [diff] [blame] | 8985 | rc = fan_set_level_safe(fan_control_resume_level); |
| 8986 | if (rc < 0) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 8987 | pr_notice("failed to restore fan level: %d\n", rc); |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 8988 | } |
| 8989 | } |
| 8990 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 8991 | static int fan_read(struct seq_file *m) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8992 | { |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8993 | int rc; |
| 8994 | u8 status; |
| 8995 | unsigned int speed = 0; |
| 8996 | |
| 8997 | switch (fan_status_access_mode) { |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 8998 | case TPACPI_FAN_RD_ACPI_GFAN: |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 8999 | /* 570, 600e/x, 770e, 770x */ |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 9000 | rc = fan_get_status_safe(&status); |
| 9001 | if (rc < 0) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9002 | return rc; |
| 9003 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9004 | seq_printf(m, "status:\t\t%s\n" |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9005 | "level:\t\t%d\n", |
| 9006 | (status != 0) ? "enabled" : "disabled", status); |
| 9007 | break; |
| 9008 | |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9009 | case TPACPI_FAN_RD_TPEC: |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9010 | /* all except 570, 600e/x, 770e, 770x */ |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 9011 | rc = fan_get_status_safe(&status); |
| 9012 | if (rc < 0) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9013 | return rc; |
| 9014 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9015 | seq_printf(m, "status:\t\t%s\n", |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9016 | (status != 0) ? "enabled" : "disabled"); |
| 9017 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 9018 | rc = fan_get_speed(&speed); |
| 9019 | if (rc < 0) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9020 | return rc; |
| 9021 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9022 | seq_printf(m, "speed:\t\t%d\n", speed); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9023 | |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9024 | if (status & TP_EC_FAN_FULLSPEED) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9025 | /* Disengaged mode takes precedence */ |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9026 | seq_printf(m, "level:\t\tdisengaged\n"); |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9027 | else if (status & TP_EC_FAN_AUTO) |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9028 | seq_printf(m, "level:\t\tauto\n"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9029 | else |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9030 | seq_printf(m, "level:\t\t%d\n", status); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9031 | break; |
| 9032 | |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9033 | case TPACPI_FAN_NONE: |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9034 | default: |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9035 | seq_printf(m, "status:\t\tnot supported\n"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9036 | } |
| 9037 | |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9038 | if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) { |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9039 | seq_printf(m, "commands:\tlevel <level>"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9040 | |
| 9041 | switch (fan_control_access_mode) { |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9042 | case TPACPI_FAN_WR_ACPI_SFAN: |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9043 | seq_printf(m, " (<level> is 0-7)\n"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9044 | break; |
| 9045 | |
| 9046 | default: |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 9047 | seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9048 | break; |
| 9049 | } |
| 9050 | } |
| 9051 | |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9052 | if (fan_control_commands & TPACPI_FAN_CMD_ENABLE) |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9053 | seq_printf(m, "commands:\tenable, disable\n" |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 9054 | "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9055 | |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9056 | if (fan_control_commands & TPACPI_FAN_CMD_SPEED) |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 9057 | seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9058 | |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 9059 | return 0; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 9060 | } |
| 9061 | |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9062 | static int fan_write_cmd_level(const char *cmd, int *rc) |
| 9063 | { |
| 9064 | int level; |
| 9065 | |
Henrique de Moraes Holschuh | a12095c | 2006-11-24 11:47:13 -0200 | [diff] [blame] | 9066 | if (strlencmp(cmd, "level auto") == 0) |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9067 | level = TP_EC_FAN_AUTO; |
Nathan Chancellor | fd96e35 | 2021-10-18 11:25:37 -0700 | [diff] [blame] | 9068 | else if ((strlencmp(cmd, "level disengaged") == 0) || |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 9069 | (strlencmp(cmd, "level full-speed") == 0)) |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9070 | level = TP_EC_FAN_FULLSPEED; |
Henrique de Moraes Holschuh | a12095c | 2006-11-24 11:47:13 -0200 | [diff] [blame] | 9071 | else if (sscanf(cmd, "level %d", &level) != 1) |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9072 | return 0; |
| 9073 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 9074 | *rc = fan_set_level_safe(level); |
| 9075 | if (*rc == -ENXIO) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 9076 | pr_err("level command accepted for unsupported access mode %d\n", |
| 9077 | fan_control_access_mode); |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 9078 | else if (!*rc) |
| 9079 | tpacpi_disclose_usertask("procfs fan", |
| 9080 | "set level to %d\n", level); |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9081 | |
| 9082 | return 1; |
| 9083 | } |
| 9084 | |
| 9085 | static int fan_write_cmd_enable(const char *cmd, int *rc) |
| 9086 | { |
| 9087 | if (strlencmp(cmd, "enable") != 0) |
| 9088 | return 0; |
| 9089 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 9090 | *rc = fan_set_enable(); |
| 9091 | if (*rc == -ENXIO) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 9092 | pr_err("enable command accepted for unsupported access mode %d\n", |
| 9093 | fan_control_access_mode); |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 9094 | else if (!*rc) |
| 9095 | tpacpi_disclose_usertask("procfs fan", "enable\n"); |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9096 | |
| 9097 | return 1; |
| 9098 | } |
| 9099 | |
| 9100 | static int fan_write_cmd_disable(const char *cmd, int *rc) |
| 9101 | { |
| 9102 | if (strlencmp(cmd, "disable") != 0) |
| 9103 | return 0; |
| 9104 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 9105 | *rc = fan_set_disable(); |
| 9106 | if (*rc == -ENXIO) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 9107 | pr_err("disable command accepted for unsupported access mode %d\n", |
| 9108 | fan_control_access_mode); |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 9109 | else if (!*rc) |
| 9110 | tpacpi_disclose_usertask("procfs fan", "disable\n"); |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9111 | |
| 9112 | return 1; |
| 9113 | } |
| 9114 | |
| 9115 | static int fan_write_cmd_speed(const char *cmd, int *rc) |
| 9116 | { |
| 9117 | int speed; |
| 9118 | |
Henrique de Moraes Holschuh | a8b7a66 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9119 | /* TODO: |
| 9120 | * Support speed <low> <medium> <high> ? */ |
| 9121 | |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9122 | if (sscanf(cmd, "speed %d", &speed) != 1) |
| 9123 | return 0; |
| 9124 | |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 9125 | *rc = fan_set_speed(speed); |
| 9126 | if (*rc == -ENXIO) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 9127 | pr_err("speed command accepted for unsupported access mode %d\n", |
| 9128 | fan_control_access_mode); |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 9129 | else if (!*rc) |
| 9130 | tpacpi_disclose_usertask("procfs fan", |
| 9131 | "set speed to %d\n", speed); |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9132 | |
| 9133 | return 1; |
| 9134 | } |
| 9135 | |
Henrique de Moraes Holschuh | 16663a8 | 2006-11-24 11:47:14 -0200 | [diff] [blame] | 9136 | static int fan_write_cmd_watchdog(const char *cmd, int *rc) |
| 9137 | { |
| 9138 | int interval; |
| 9139 | |
| 9140 | if (sscanf(cmd, "watchdog %d", &interval) != 1) |
| 9141 | return 0; |
| 9142 | |
| 9143 | if (interval < 0 || interval > 120) |
| 9144 | *rc = -EINVAL; |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 9145 | else { |
Henrique de Moraes Holschuh | 16663a8 | 2006-11-24 11:47:14 -0200 | [diff] [blame] | 9146 | fan_watchdog_maxinterval = interval; |
Henrique de Moraes Holschuh | 74a60c0 | 2009-04-04 04:25:52 +0000 | [diff] [blame] | 9147 | tpacpi_disclose_usertask("procfs fan", |
| 9148 | "set watchdog timer to %d\n", |
| 9149 | interval); |
| 9150 | } |
Henrique de Moraes Holschuh | 16663a8 | 2006-11-24 11:47:14 -0200 | [diff] [blame] | 9151 | |
| 9152 | return 1; |
| 9153 | } |
| 9154 | |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9155 | static int fan_write(char *buf) |
| 9156 | { |
| 9157 | char *cmd; |
| 9158 | int rc = 0; |
| 9159 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 9160 | while (!rc && (cmd = strsep(&buf, ","))) { |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9161 | if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) && |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9162 | fan_write_cmd_level(cmd, &rc)) && |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9163 | !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) && |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9164 | (fan_write_cmd_enable(cmd, &rc) || |
Henrique de Moraes Holschuh | 16663a8 | 2006-11-24 11:47:14 -0200 | [diff] [blame] | 9165 | fan_write_cmd_disable(cmd, &rc) || |
| 9166 | fan_write_cmd_watchdog(cmd, &rc))) && |
Henrique de Moraes Holschuh | efa2714 | 2007-04-21 11:08:28 -0300 | [diff] [blame] | 9167 | !((fan_control_commands & TPACPI_FAN_CMD_SPEED) && |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9168 | fan_write_cmd_speed(cmd, &rc)) |
| 9169 | ) |
| 9170 | rc = -EINVAL; |
Henrique de Moraes Holschuh | 16663a8 | 2006-11-24 11:47:14 -0200 | [diff] [blame] | 9171 | else if (!rc) |
| 9172 | fan_watchdog_reset(); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 9173 | } |
| 9174 | |
Henrique de Moraes Holschuh | 18ad799 | 2006-11-24 11:47:11 -0200 | [diff] [blame] | 9175 | return rc; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 9176 | } |
| 9177 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 9178 | static struct ibm_struct fan_driver_data = { |
| 9179 | .name = "fan", |
| 9180 | .read = fan_read, |
| 9181 | .write = fan_write, |
| 9182 | .exit = fan_exit, |
Henrique de Moraes Holschuh | 75700e5 | 2008-10-18 14:23:52 -0300 | [diff] [blame] | 9183 | .suspend = fan_suspend, |
| 9184 | .resume = fan_resume, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 9185 | }; |
| 9186 | |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9187 | /************************************************************************* |
| 9188 | * Mute LED subdriver |
| 9189 | */ |
| 9190 | |
Takashi Iwai | 9e908a1 | 2018-11-26 17:47:47 +0100 | [diff] [blame] | 9191 | #define TPACPI_LED_MAX 2 |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9192 | |
| 9193 | struct tp_led_table { |
| 9194 | acpi_string name; |
| 9195 | int on_value; |
| 9196 | int off_value; |
| 9197 | int state; |
| 9198 | }; |
| 9199 | |
Takashi Iwai | 9e908a1 | 2018-11-26 17:47:47 +0100 | [diff] [blame] | 9200 | static struct tp_led_table led_tables[TPACPI_LED_MAX] = { |
| 9201 | [LED_AUDIO_MUTE] = { |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9202 | .name = "SSMS", |
| 9203 | .on_value = 1, |
| 9204 | .off_value = 0, |
| 9205 | }, |
Takashi Iwai | 9e908a1 | 2018-11-26 17:47:47 +0100 | [diff] [blame] | 9206 | [LED_AUDIO_MICMUTE] = { |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9207 | .name = "MMTS", |
| 9208 | .on_value = 2, |
| 9209 | .off_value = 0, |
| 9210 | }, |
| 9211 | }; |
| 9212 | |
| 9213 | static int mute_led_on_off(struct tp_led_table *t, bool state) |
| 9214 | { |
| 9215 | acpi_handle temp; |
| 9216 | int output; |
| 9217 | |
Axel Lin | 3526eca | 2016-09-19 09:33:51 +0800 | [diff] [blame] | 9218 | if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) { |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9219 | pr_warn("Thinkpad ACPI has no %s interface.\n", t->name); |
| 9220 | return -EIO; |
| 9221 | } |
| 9222 | |
| 9223 | if (!acpi_evalf(hkey_handle, &output, t->name, "dd", |
| 9224 | state ? t->on_value : t->off_value)) |
| 9225 | return -EIO; |
| 9226 | |
| 9227 | t->state = state; |
| 9228 | return state; |
| 9229 | } |
| 9230 | |
Takashi Iwai | 9e908a1 | 2018-11-26 17:47:47 +0100 | [diff] [blame] | 9231 | static int tpacpi_led_set(int whichled, bool on) |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9232 | { |
| 9233 | struct tp_led_table *t; |
| 9234 | |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9235 | t = &led_tables[whichled]; |
| 9236 | if (t->state < 0 || t->state == on) |
| 9237 | return t->state; |
| 9238 | return mute_led_on_off(t, on); |
| 9239 | } |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9240 | |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9241 | static int tpacpi_led_mute_set(struct led_classdev *led_cdev, |
| 9242 | enum led_brightness brightness) |
| 9243 | { |
Takashi Iwai | 9e908a1 | 2018-11-26 17:47:47 +0100 | [diff] [blame] | 9244 | return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF); |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9245 | } |
| 9246 | |
| 9247 | static int tpacpi_led_micmute_set(struct led_classdev *led_cdev, |
| 9248 | enum led_brightness brightness) |
| 9249 | { |
Takashi Iwai | 9e908a1 | 2018-11-26 17:47:47 +0100 | [diff] [blame] | 9250 | return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF); |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9251 | } |
| 9252 | |
Takashi Iwai | 9e908a1 | 2018-11-26 17:47:47 +0100 | [diff] [blame] | 9253 | static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = { |
| 9254 | [LED_AUDIO_MUTE] = { |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9255 | .name = "platform::mute", |
| 9256 | .max_brightness = 1, |
| 9257 | .brightness_set_blocking = tpacpi_led_mute_set, |
| 9258 | .default_trigger = "audio-mute", |
| 9259 | }, |
Takashi Iwai | 9e908a1 | 2018-11-26 17:47:47 +0100 | [diff] [blame] | 9260 | [LED_AUDIO_MICMUTE] = { |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9261 | .name = "platform::micmute", |
| 9262 | .max_brightness = 1, |
| 9263 | .brightness_set_blocking = tpacpi_led_micmute_set, |
| 9264 | .default_trigger = "audio-micmute", |
| 9265 | }, |
| 9266 | }; |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9267 | |
| 9268 | static int mute_led_init(struct ibm_init_struct *iibm) |
| 9269 | { |
| 9270 | acpi_handle temp; |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9271 | int i, err; |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9272 | |
| 9273 | for (i = 0; i < TPACPI_LED_MAX; i++) { |
| 9274 | struct tp_led_table *t = &led_tables[i]; |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9275 | if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) { |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9276 | t->state = -ENODEV; |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9277 | continue; |
| 9278 | } |
| 9279 | |
Takashi Iwai | 9e908a1 | 2018-11-26 17:47:47 +0100 | [diff] [blame] | 9280 | mute_led_cdev[i].brightness = ledtrig_audio_get(i); |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9281 | err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]); |
| 9282 | if (err < 0) { |
Andy Shevchenko | 41f8004 | 2020-05-07 19:37:23 +0300 | [diff] [blame] | 9283 | while (i--) |
| 9284 | led_classdev_unregister(&mute_led_cdev[i]); |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9285 | return err; |
| 9286 | } |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9287 | } |
| 9288 | return 0; |
| 9289 | } |
| 9290 | |
| 9291 | static void mute_led_exit(void) |
| 9292 | { |
| 9293 | int i; |
| 9294 | |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9295 | for (i = 0; i < TPACPI_LED_MAX; i++) { |
Andy Shevchenko | 41f8004 | 2020-05-07 19:37:23 +0300 | [diff] [blame] | 9296 | led_classdev_unregister(&mute_led_cdev[i]); |
| 9297 | tpacpi_led_set(i, false); |
Takashi Iwai | 4e1d092 | 2018-11-26 17:47:45 +0100 | [diff] [blame] | 9298 | } |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9299 | } |
| 9300 | |
Takashi Iwai | 119f449 | 2014-02-12 16:32:45 +0100 | [diff] [blame] | 9301 | static void mute_led_resume(void) |
| 9302 | { |
| 9303 | int i; |
| 9304 | |
| 9305 | for (i = 0; i < TPACPI_LED_MAX; i++) { |
| 9306 | struct tp_led_table *t = &led_tables[i]; |
| 9307 | if (t->state >= 0) |
| 9308 | mute_led_on_off(t, t->state); |
| 9309 | } |
| 9310 | } |
| 9311 | |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9312 | static struct ibm_struct mute_led_driver_data = { |
| 9313 | .name = "mute_led", |
| 9314 | .exit = mute_led_exit, |
Takashi Iwai | 119f449 | 2014-02-12 16:32:45 +0100 | [diff] [blame] | 9315 | .resume = mute_led_resume, |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 9316 | }; |
| 9317 | |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9318 | /* |
| 9319 | * Battery Wear Control Driver |
| 9320 | * Contact: Ognjen Galic <smclt30p@gmail.com> |
| 9321 | */ |
| 9322 | |
| 9323 | /* Metadata */ |
| 9324 | |
| 9325 | #define GET_START "BCTG" |
| 9326 | #define SET_START "BCCS" |
| 9327 | #define GET_STOP "BCSG" |
| 9328 | #define SET_STOP "BCSS" |
| 9329 | |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9330 | enum { |
| 9331 | BAT_ANY = 0, |
| 9332 | BAT_PRIMARY = 1, |
| 9333 | BAT_SECONDARY = 2 |
| 9334 | }; |
| 9335 | |
| 9336 | enum { |
| 9337 | /* Error condition bit */ |
| 9338 | METHOD_ERR = BIT(31), |
| 9339 | }; |
| 9340 | |
| 9341 | enum { |
| 9342 | /* This is used in the get/set helpers */ |
| 9343 | THRESHOLD_START, |
| 9344 | THRESHOLD_STOP, |
| 9345 | }; |
| 9346 | |
| 9347 | struct tpacpi_battery_data { |
| 9348 | int charge_start; |
| 9349 | int start_support; |
| 9350 | int charge_stop; |
| 9351 | int stop_support; |
| 9352 | }; |
| 9353 | |
| 9354 | struct tpacpi_battery_driver_data { |
| 9355 | struct tpacpi_battery_data batteries[3]; |
| 9356 | int individual_addressing; |
| 9357 | }; |
| 9358 | |
| 9359 | static struct tpacpi_battery_driver_data battery_info; |
| 9360 | |
| 9361 | /* ACPI helpers/functions/probes */ |
| 9362 | |
| 9363 | /** |
| 9364 | * This evaluates a ACPI method call specific to the battery |
| 9365 | * ACPI extension. The specifics are that an error is marked |
| 9366 | * in the 32rd bit of the response, so we just check that here. |
| 9367 | */ |
| 9368 | static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param) |
| 9369 | { |
| 9370 | int response; |
| 9371 | |
| 9372 | if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) { |
| 9373 | acpi_handle_err(hkey_handle, "%s: evaluate failed", method); |
| 9374 | return AE_ERROR; |
| 9375 | } |
| 9376 | if (response & METHOD_ERR) { |
| 9377 | acpi_handle_err(hkey_handle, |
| 9378 | "%s evaluated but flagged as error", method); |
| 9379 | return AE_ERROR; |
| 9380 | } |
| 9381 | *ret = response; |
| 9382 | return AE_OK; |
| 9383 | } |
| 9384 | |
| 9385 | static int tpacpi_battery_get(int what, int battery, int *ret) |
| 9386 | { |
| 9387 | switch (what) { |
| 9388 | case THRESHOLD_START: |
| 9389 | if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery)) |
| 9390 | return -ENODEV; |
| 9391 | |
| 9392 | /* The value is in the low 8 bits of the response */ |
| 9393 | *ret = *ret & 0xFF; |
| 9394 | return 0; |
| 9395 | case THRESHOLD_STOP: |
| 9396 | if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery)) |
| 9397 | return -ENODEV; |
| 9398 | /* Value is in lower 8 bits */ |
| 9399 | *ret = *ret & 0xFF; |
| 9400 | /* |
| 9401 | * On the stop value, if we return 0 that |
| 9402 | * does not make any sense. 0 means Default, which |
| 9403 | * means that charging stops at 100%, so we return |
| 9404 | * that. |
| 9405 | */ |
| 9406 | if (*ret == 0) |
| 9407 | *ret = 100; |
| 9408 | return 0; |
| 9409 | default: |
| 9410 | pr_crit("wrong parameter: %d", what); |
| 9411 | return -EINVAL; |
| 9412 | } |
| 9413 | } |
| 9414 | |
| 9415 | static int tpacpi_battery_set(int what, int battery, int value) |
| 9416 | { |
| 9417 | int param, ret; |
| 9418 | /* The first 8 bits are the value of the threshold */ |
| 9419 | param = value; |
| 9420 | /* The battery ID is in bits 8-9, 2 bits */ |
| 9421 | param |= battery << 8; |
| 9422 | |
| 9423 | switch (what) { |
| 9424 | case THRESHOLD_START: |
| 9425 | if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) { |
| 9426 | pr_err("failed to set charge threshold on battery %d", |
| 9427 | battery); |
| 9428 | return -ENODEV; |
| 9429 | } |
| 9430 | return 0; |
| 9431 | case THRESHOLD_STOP: |
| 9432 | if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) { |
| 9433 | pr_err("failed to set stop threshold: %d", battery); |
| 9434 | return -ENODEV; |
| 9435 | } |
| 9436 | return 0; |
| 9437 | default: |
| 9438 | pr_crit("wrong parameter: %d", what); |
| 9439 | return -EINVAL; |
| 9440 | } |
| 9441 | } |
| 9442 | |
| 9443 | static int tpacpi_battery_probe(int battery) |
| 9444 | { |
| 9445 | int ret = 0; |
| 9446 | |
Thomas Weißschuh | d22296d | 2018-08-02 00:24:18 +0200 | [diff] [blame] | 9447 | memset(&battery_info.batteries[battery], 0, |
| 9448 | sizeof(battery_info.batteries[battery])); |
| 9449 | |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9450 | /* |
| 9451 | * 1) Get the current start threshold |
| 9452 | * 2) Check for support |
| 9453 | * 3) Get the current stop threshold |
| 9454 | * 4) Check for support |
| 9455 | */ |
| 9456 | if (acpi_has_method(hkey_handle, GET_START)) { |
| 9457 | if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) { |
| 9458 | pr_err("Error probing battery %d\n", battery); |
| 9459 | return -ENODEV; |
| 9460 | } |
| 9461 | /* Individual addressing is in bit 9 */ |
| 9462 | if (ret & BIT(9)) |
| 9463 | battery_info.individual_addressing = true; |
| 9464 | /* Support is marked in bit 8 */ |
| 9465 | if (ret & BIT(8)) |
| 9466 | battery_info.batteries[battery].start_support = 1; |
| 9467 | else |
| 9468 | return -ENODEV; |
| 9469 | if (tpacpi_battery_get(THRESHOLD_START, battery, |
| 9470 | &battery_info.batteries[battery].charge_start)) { |
| 9471 | pr_err("Error probing battery %d\n", battery); |
| 9472 | return -ENODEV; |
| 9473 | } |
| 9474 | } |
| 9475 | if (acpi_has_method(hkey_handle, GET_STOP)) { |
| 9476 | if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) { |
| 9477 | pr_err("Error probing battery stop; %d\n", battery); |
| 9478 | return -ENODEV; |
| 9479 | } |
| 9480 | /* Support is marked in bit 8 */ |
| 9481 | if (ret & BIT(8)) |
| 9482 | battery_info.batteries[battery].stop_support = 1; |
| 9483 | else |
| 9484 | return -ENODEV; |
| 9485 | if (tpacpi_battery_get(THRESHOLD_STOP, battery, |
| 9486 | &battery_info.batteries[battery].charge_stop)) { |
| 9487 | pr_err("Error probing battery stop: %d\n", battery); |
| 9488 | return -ENODEV; |
| 9489 | } |
| 9490 | } |
| 9491 | pr_info("battery %d registered (start %d, stop %d)", |
| 9492 | battery, |
| 9493 | battery_info.batteries[battery].charge_start, |
| 9494 | battery_info.batteries[battery].charge_stop); |
| 9495 | |
| 9496 | return 0; |
| 9497 | } |
| 9498 | |
| 9499 | /* General helper functions */ |
| 9500 | |
| 9501 | static int tpacpi_battery_get_id(const char *battery_name) |
| 9502 | { |
| 9503 | |
Jouke Witteveen | 1a32ebb | 2018-07-11 11:45:36 +0200 | [diff] [blame] | 9504 | if (strcmp(battery_name, "BAT0") == 0 || |
| 9505 | tp_features.battery_force_primary) |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9506 | return BAT_PRIMARY; |
| 9507 | if (strcmp(battery_name, "BAT1") == 0) |
| 9508 | return BAT_SECONDARY; |
| 9509 | /* |
| 9510 | * If for some reason the battery is not BAT0 nor is it |
| 9511 | * BAT1, we will assume it's the default, first battery, |
| 9512 | * AKA primary. |
| 9513 | */ |
| 9514 | pr_warn("unknown battery %s, assuming primary", battery_name); |
| 9515 | return BAT_PRIMARY; |
| 9516 | } |
| 9517 | |
| 9518 | /* sysfs interface */ |
| 9519 | |
| 9520 | static ssize_t tpacpi_battery_store(int what, |
| 9521 | struct device *dev, |
| 9522 | const char *buf, size_t count) |
| 9523 | { |
| 9524 | struct power_supply *supply = to_power_supply(dev); |
| 9525 | unsigned long value; |
| 9526 | int battery, rval; |
| 9527 | /* |
| 9528 | * Some systems have support for more than |
| 9529 | * one battery. If that is the case, |
| 9530 | * tpacpi_battery_probe marked that addressing |
| 9531 | * them individually is supported, so we do that |
| 9532 | * based on the device struct. |
| 9533 | * |
| 9534 | * On systems that are not supported, we assume |
| 9535 | * the primary as most of the ACPI calls fail |
| 9536 | * with "Any Battery" as the parameter. |
| 9537 | */ |
| 9538 | if (battery_info.individual_addressing) |
| 9539 | /* BAT_PRIMARY or BAT_SECONDARY */ |
| 9540 | battery = tpacpi_battery_get_id(supply->desc->name); |
| 9541 | else |
| 9542 | battery = BAT_PRIMARY; |
| 9543 | |
| 9544 | rval = kstrtoul(buf, 10, &value); |
| 9545 | if (rval) |
| 9546 | return rval; |
| 9547 | |
| 9548 | switch (what) { |
| 9549 | case THRESHOLD_START: |
| 9550 | if (!battery_info.batteries[battery].start_support) |
| 9551 | return -ENODEV; |
| 9552 | /* valid values are [0, 99] */ |
Xiongfeng Wang | b991178 | 2020-04-29 16:59:40 +0800 | [diff] [blame] | 9553 | if (value > 99) |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9554 | return -EINVAL; |
| 9555 | if (value > battery_info.batteries[battery].charge_stop) |
| 9556 | return -EINVAL; |
| 9557 | if (tpacpi_battery_set(THRESHOLD_START, battery, value)) |
| 9558 | return -ENODEV; |
| 9559 | battery_info.batteries[battery].charge_start = value; |
| 9560 | return count; |
| 9561 | |
| 9562 | case THRESHOLD_STOP: |
| 9563 | if (!battery_info.batteries[battery].stop_support) |
| 9564 | return -ENODEV; |
| 9565 | /* valid values are [1, 100] */ |
| 9566 | if (value < 1 || value > 100) |
| 9567 | return -EINVAL; |
| 9568 | if (value < battery_info.batteries[battery].charge_start) |
| 9569 | return -EINVAL; |
| 9570 | battery_info.batteries[battery].charge_stop = value; |
| 9571 | /* |
| 9572 | * When 100 is passed to stop, we need to flip |
| 9573 | * it to 0 as that the EC understands that as |
| 9574 | * "Default", which will charge to 100% |
| 9575 | */ |
| 9576 | if (value == 100) |
| 9577 | value = 0; |
| 9578 | if (tpacpi_battery_set(THRESHOLD_STOP, battery, value)) |
| 9579 | return -EINVAL; |
| 9580 | return count; |
| 9581 | default: |
| 9582 | pr_crit("Wrong parameter: %d", what); |
| 9583 | return -EINVAL; |
| 9584 | } |
| 9585 | return count; |
| 9586 | } |
| 9587 | |
| 9588 | static ssize_t tpacpi_battery_show(int what, |
| 9589 | struct device *dev, |
| 9590 | char *buf) |
| 9591 | { |
| 9592 | struct power_supply *supply = to_power_supply(dev); |
| 9593 | int ret, battery; |
| 9594 | /* |
| 9595 | * Some systems have support for more than |
| 9596 | * one battery. If that is the case, |
| 9597 | * tpacpi_battery_probe marked that addressing |
| 9598 | * them individually is supported, so we; |
| 9599 | * based on the device struct. |
| 9600 | * |
| 9601 | * On systems that are not supported, we assume |
| 9602 | * the primary as most of the ACPI calls fail |
| 9603 | * with "Any Battery" as the parameter. |
| 9604 | */ |
| 9605 | if (battery_info.individual_addressing) |
| 9606 | /* BAT_PRIMARY or BAT_SECONDARY */ |
| 9607 | battery = tpacpi_battery_get_id(supply->desc->name); |
| 9608 | else |
| 9609 | battery = BAT_PRIMARY; |
| 9610 | if (tpacpi_battery_get(what, battery, &ret)) |
| 9611 | return -ENODEV; |
| 9612 | return sprintf(buf, "%d\n", ret); |
| 9613 | } |
| 9614 | |
Thomas Weißschuh | e339295 | 2020-02-03 13:01:32 +0100 | [diff] [blame] | 9615 | static ssize_t charge_control_start_threshold_show(struct device *device, |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9616 | struct device_attribute *attr, |
| 9617 | char *buf) |
| 9618 | { |
| 9619 | return tpacpi_battery_show(THRESHOLD_START, device, buf); |
| 9620 | } |
| 9621 | |
Thomas Weißschuh | e339295 | 2020-02-03 13:01:32 +0100 | [diff] [blame] | 9622 | static ssize_t charge_control_end_threshold_show(struct device *device, |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9623 | struct device_attribute *attr, |
| 9624 | char *buf) |
| 9625 | { |
| 9626 | return tpacpi_battery_show(THRESHOLD_STOP, device, buf); |
| 9627 | } |
| 9628 | |
Thomas Weißschuh | e339295 | 2020-02-03 13:01:32 +0100 | [diff] [blame] | 9629 | static ssize_t charge_control_start_threshold_store(struct device *dev, |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9630 | struct device_attribute *attr, |
| 9631 | const char *buf, size_t count) |
| 9632 | { |
| 9633 | return tpacpi_battery_store(THRESHOLD_START, dev, buf, count); |
| 9634 | } |
| 9635 | |
Thomas Weißschuh | e339295 | 2020-02-03 13:01:32 +0100 | [diff] [blame] | 9636 | static ssize_t charge_control_end_threshold_store(struct device *dev, |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9637 | struct device_attribute *attr, |
| 9638 | const char *buf, size_t count) |
| 9639 | { |
| 9640 | return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count); |
| 9641 | } |
| 9642 | |
Thomas Weißschuh | e339295 | 2020-02-03 13:01:32 +0100 | [diff] [blame] | 9643 | static DEVICE_ATTR_RW(charge_control_start_threshold); |
| 9644 | static DEVICE_ATTR_RW(charge_control_end_threshold); |
Wei Yongjun | 066d7f3 | 2020-07-29 23:59:14 +0800 | [diff] [blame] | 9645 | static struct device_attribute dev_attr_charge_start_threshold = __ATTR( |
Thomas Weißschuh | e339295 | 2020-02-03 13:01:32 +0100 | [diff] [blame] | 9646 | charge_start_threshold, |
| 9647 | 0644, |
| 9648 | charge_control_start_threshold_show, |
| 9649 | charge_control_start_threshold_store |
| 9650 | ); |
Wei Yongjun | 066d7f3 | 2020-07-29 23:59:14 +0800 | [diff] [blame] | 9651 | static struct device_attribute dev_attr_charge_stop_threshold = __ATTR( |
Thomas Weißschuh | e339295 | 2020-02-03 13:01:32 +0100 | [diff] [blame] | 9652 | charge_stop_threshold, |
| 9653 | 0644, |
| 9654 | charge_control_end_threshold_show, |
| 9655 | charge_control_end_threshold_store |
| 9656 | ); |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9657 | |
| 9658 | static struct attribute *tpacpi_battery_attrs[] = { |
Thomas Weißschuh | e339295 | 2020-02-03 13:01:32 +0100 | [diff] [blame] | 9659 | &dev_attr_charge_control_start_threshold.attr, |
| 9660 | &dev_attr_charge_control_end_threshold.attr, |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9661 | &dev_attr_charge_start_threshold.attr, |
| 9662 | &dev_attr_charge_stop_threshold.attr, |
| 9663 | NULL, |
| 9664 | }; |
| 9665 | |
| 9666 | ATTRIBUTE_GROUPS(tpacpi_battery); |
| 9667 | |
| 9668 | /* ACPI battery hooking */ |
| 9669 | |
| 9670 | static int tpacpi_battery_add(struct power_supply *battery) |
| 9671 | { |
| 9672 | int batteryid = tpacpi_battery_get_id(battery->desc->name); |
| 9673 | |
| 9674 | if (tpacpi_battery_probe(batteryid)) |
| 9675 | return -ENODEV; |
| 9676 | if (device_add_groups(&battery->dev, tpacpi_battery_groups)) |
| 9677 | return -ENODEV; |
| 9678 | return 0; |
| 9679 | } |
| 9680 | |
| 9681 | static int tpacpi_battery_remove(struct power_supply *battery) |
| 9682 | { |
| 9683 | device_remove_groups(&battery->dev, tpacpi_battery_groups); |
| 9684 | return 0; |
| 9685 | } |
| 9686 | |
| 9687 | static struct acpi_battery_hook battery_hook = { |
| 9688 | .add_battery = tpacpi_battery_add, |
| 9689 | .remove_battery = tpacpi_battery_remove, |
| 9690 | .name = "ThinkPad Battery Extension", |
| 9691 | }; |
| 9692 | |
| 9693 | /* Subdriver init/exit */ |
| 9694 | |
Jouke Witteveen | 1a32ebb | 2018-07-11 11:45:36 +0200 | [diff] [blame] | 9695 | static const struct tpacpi_quirk battery_quirk_table[] __initconst = { |
| 9696 | /* |
| 9697 | * Individual addressing is broken on models that expose the |
| 9698 | * primary battery as BAT1. |
| 9699 | */ |
Jouke Witteveen | 6640ee6 | 2018-08-02 22:28:49 +0200 | [diff] [blame] | 9700 | TPACPI_Q_LNV('J', '7', true), /* B5400 */ |
| 9701 | TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */ |
| 9702 | TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */ |
Jouke Witteveen | 1a32ebb | 2018-07-11 11:45:36 +0200 | [diff] [blame] | 9703 | TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */ |
Jouke Witteveen | 6640ee6 | 2018-08-02 22:28:49 +0200 | [diff] [blame] | 9704 | TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */ |
Hans de Goede | 3cd420b | 2020-11-09 11:35:50 +0100 | [diff] [blame] | 9705 | TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */ |
Jouke Witteveen | 1a32ebb | 2018-07-11 11:45:36 +0200 | [diff] [blame] | 9706 | }; |
| 9707 | |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9708 | static int __init tpacpi_battery_init(struct ibm_init_struct *ibm) |
| 9709 | { |
Thomas Weißschuh | d22296d | 2018-08-02 00:24:18 +0200 | [diff] [blame] | 9710 | memset(&battery_info, 0, sizeof(battery_info)); |
| 9711 | |
Jouke Witteveen | 1a32ebb | 2018-07-11 11:45:36 +0200 | [diff] [blame] | 9712 | tp_features.battery_force_primary = tpacpi_check_quirks( |
| 9713 | battery_quirk_table, |
| 9714 | ARRAY_SIZE(battery_quirk_table)); |
| 9715 | |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 9716 | battery_hook_register(&battery_hook); |
| 9717 | return 0; |
| 9718 | } |
| 9719 | |
| 9720 | static void tpacpi_battery_exit(void) |
| 9721 | { |
| 9722 | battery_hook_unregister(&battery_hook); |
| 9723 | } |
| 9724 | |
| 9725 | static struct ibm_struct battery_driver_data = { |
| 9726 | .name = "battery", |
| 9727 | .exit = tpacpi_battery_exit, |
| 9728 | }; |
| 9729 | |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9730 | /************************************************************************* |
| 9731 | * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature |
| 9732 | */ |
| 9733 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9734 | static struct drm_privacy_screen *lcdshadow_dev; |
Hans de Goede | e8b7eb6 | 2021-10-05 22:23:19 +0200 | [diff] [blame] | 9735 | static acpi_handle lcdshadow_get_handle; |
| 9736 | static acpi_handle lcdshadow_set_handle; |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9737 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9738 | static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv, |
| 9739 | enum drm_privacy_screen_status state) |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9740 | { |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9741 | int output; |
| 9742 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9743 | if (WARN_ON(!mutex_is_locked(&priv->lock))) |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9744 | return -EIO; |
| 9745 | |
Hans de Goede | e8b7eb6 | 2021-10-05 22:23:19 +0200 | [diff] [blame] | 9746 | if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state)) |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9747 | return -EIO; |
| 9748 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9749 | priv->hw_state = priv->sw_state = state; |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9750 | return 0; |
| 9751 | } |
| 9752 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9753 | static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv) |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9754 | { |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9755 | int output; |
| 9756 | |
| 9757 | if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0)) |
| 9758 | return; |
| 9759 | |
| 9760 | priv->hw_state = priv->sw_state = output & 0x1; |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9761 | } |
| 9762 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9763 | static const struct drm_privacy_screen_ops lcdshadow_ops = { |
| 9764 | .set_sw_state = lcdshadow_set_sw_state, |
| 9765 | .get_hw_state = lcdshadow_get_hw_state, |
| 9766 | }; |
| 9767 | |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9768 | static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm) |
| 9769 | { |
Hans de Goede | e8b7eb6 | 2021-10-05 22:23:19 +0200 | [diff] [blame] | 9770 | acpi_status status1, status2; |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9771 | int output; |
| 9772 | |
Hans de Goede | e8b7eb6 | 2021-10-05 22:23:19 +0200 | [diff] [blame] | 9773 | status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle); |
| 9774 | status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle); |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9775 | if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2)) |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9776 | return 0; |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9777 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9778 | if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0)) |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9779 | return -EIO; |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9780 | |
| 9781 | if (!(output & 0x10000)) |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9782 | return 0; |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9783 | |
| 9784 | lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev, |
| 9785 | &lcdshadow_ops); |
| 9786 | if (IS_ERR(lcdshadow_dev)) |
| 9787 | return PTR_ERR(lcdshadow_dev); |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9788 | |
| 9789 | return 0; |
| 9790 | } |
| 9791 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9792 | static void lcdshadow_exit(void) |
| 9793 | { |
| 9794 | drm_privacy_screen_unregister(lcdshadow_dev); |
| 9795 | } |
| 9796 | |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9797 | static void lcdshadow_resume(void) |
| 9798 | { |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9799 | if (!lcdshadow_dev) |
| 9800 | return; |
| 9801 | |
| 9802 | mutex_lock(&lcdshadow_dev->lock); |
| 9803 | lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state); |
| 9804 | mutex_unlock(&lcdshadow_dev->lock); |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9805 | } |
| 9806 | |
| 9807 | static int lcdshadow_read(struct seq_file *m) |
| 9808 | { |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9809 | if (!lcdshadow_dev) { |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9810 | seq_puts(m, "status:\t\tnot supported\n"); |
| 9811 | } else { |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9812 | seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state); |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9813 | seq_puts(m, "commands:\t0, 1\n"); |
| 9814 | } |
| 9815 | |
| 9816 | return 0; |
| 9817 | } |
| 9818 | |
| 9819 | static int lcdshadow_write(char *buf) |
| 9820 | { |
| 9821 | char *cmd; |
Andy Shevchenko | 466f469 | 2020-05-11 15:54:14 +0300 | [diff] [blame] | 9822 | int res, state = -EINVAL; |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9823 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9824 | if (!lcdshadow_dev) |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9825 | return -ENODEV; |
| 9826 | |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 9827 | while ((cmd = strsep(&buf, ","))) { |
Andy Shevchenko | 466f469 | 2020-05-11 15:54:14 +0300 | [diff] [blame] | 9828 | res = kstrtoint(cmd, 10, &state); |
| 9829 | if (res < 0) |
| 9830 | return res; |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9831 | } |
| 9832 | |
Andy Shevchenko | 466f469 | 2020-05-11 15:54:14 +0300 | [diff] [blame] | 9833 | if (state >= 2 || state < 0) |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9834 | return -EINVAL; |
| 9835 | |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9836 | mutex_lock(&lcdshadow_dev->lock); |
| 9837 | res = lcdshadow_set_sw_state(lcdshadow_dev, state); |
| 9838 | mutex_unlock(&lcdshadow_dev->lock); |
| 9839 | |
| 9840 | drm_privacy_screen_call_notifier_chain(lcdshadow_dev); |
| 9841 | |
| 9842 | return res; |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9843 | } |
| 9844 | |
| 9845 | static struct ibm_struct lcdshadow_driver_data = { |
| 9846 | .name = "lcdshadow", |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 9847 | .exit = lcdshadow_exit, |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 9848 | .resume = lcdshadow_resume, |
| 9849 | .read = lcdshadow_read, |
| 9850 | .write = lcdshadow_write, |
| 9851 | }; |
| 9852 | |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9853 | /************************************************************************* |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9854 | * Thinkpad sensor interfaces |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9855 | */ |
| 9856 | |
Mark Pearson | 0b4f5c9 | 2021-03-11 12:48:43 -0500 | [diff] [blame] | 9857 | #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */ |
| 9858 | #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */ |
| 9859 | #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */ |
| 9860 | #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */ |
| 9861 | |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9862 | #define DYTC_CMD_GET 2 /* To get current IC function and mode */ |
| 9863 | #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */ |
| 9864 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9865 | #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */ |
| 9866 | #define PALMSENSOR_ON_BIT 1 /* psensor status */ |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9867 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9868 | static bool has_palmsensor; |
| 9869 | static bool has_lapsensor; |
| 9870 | static bool palm_state; |
| 9871 | static bool lap_state; |
Mark Pearson | 0b4f5c9 | 2021-03-11 12:48:43 -0500 | [diff] [blame] | 9872 | static int dytc_version; |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9873 | |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 9874 | static int dytc_command(int command, int *output) |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9875 | { |
| 9876 | acpi_handle dytc_handle; |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 9877 | |
| 9878 | if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) { |
| 9879 | /* Platform doesn't support DYTC */ |
| 9880 | return -ENODEV; |
| 9881 | } |
| 9882 | if (!acpi_evalf(dytc_handle, output, NULL, "dd", command)) |
| 9883 | return -EIO; |
| 9884 | return 0; |
| 9885 | } |
| 9886 | |
Mark Pearson | 0b4f5c9 | 2021-03-11 12:48:43 -0500 | [diff] [blame] | 9887 | static int dytc_get_version(void) |
| 9888 | { |
| 9889 | int err, output; |
| 9890 | |
| 9891 | /* Check if we've been called before - and just return cached value */ |
| 9892 | if (dytc_version) |
| 9893 | return dytc_version; |
| 9894 | |
| 9895 | /* Otherwise query DYTC and extract version information */ |
| 9896 | err = dytc_command(DYTC_CMD_QUERY, &output); |
| 9897 | /* |
| 9898 | * If support isn't available (ENODEV) then don't return an error |
| 9899 | * and don't create the sysfs group |
| 9900 | */ |
| 9901 | if (err == -ENODEV) |
| 9902 | return 0; |
| 9903 | /* For all other errors we can flag the failure */ |
| 9904 | if (err) |
| 9905 | return err; |
| 9906 | |
| 9907 | /* Check DYTC is enabled and supports mode setting */ |
| 9908 | if (output & BIT(DYTC_QUERY_ENABLE_BIT)) |
| 9909 | dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF; |
| 9910 | |
| 9911 | return 0; |
| 9912 | } |
| 9913 | |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 9914 | static int lapsensor_get(bool *present, bool *state) |
| 9915 | { |
| 9916 | int output, err; |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9917 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9918 | *present = false; |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 9919 | err = dytc_command(DYTC_CMD_GET, &output); |
| 9920 | if (err) |
| 9921 | return err; |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9922 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9923 | *present = true; /*If we get his far, we have lapmode support*/ |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9924 | *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false; |
| 9925 | return 0; |
| 9926 | } |
| 9927 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9928 | static int palmsensor_get(bool *present, bool *state) |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9929 | { |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9930 | acpi_handle psensor_handle; |
| 9931 | int output; |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9932 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9933 | *present = false; |
| 9934 | if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle))) |
| 9935 | return -ENODEV; |
| 9936 | if (!acpi_evalf(psensor_handle, &output, NULL, "d")) |
| 9937 | return -EIO; |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9938 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9939 | *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false; |
| 9940 | *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false; |
| 9941 | return 0; |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9942 | } |
| 9943 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9944 | static void lapsensor_refresh(void) |
| 9945 | { |
| 9946 | bool state; |
| 9947 | int err; |
| 9948 | |
| 9949 | if (has_lapsensor) { |
| 9950 | err = lapsensor_get(&has_lapsensor, &state); |
| 9951 | if (err) |
| 9952 | return; |
| 9953 | if (lap_state != state) { |
| 9954 | lap_state = state; |
| 9955 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode"); |
| 9956 | } |
| 9957 | } |
| 9958 | } |
| 9959 | |
| 9960 | static void palmsensor_refresh(void) |
| 9961 | { |
| 9962 | bool state; |
| 9963 | int err; |
| 9964 | |
| 9965 | if (has_palmsensor) { |
| 9966 | err = palmsensor_get(&has_palmsensor, &state); |
| 9967 | if (err) |
| 9968 | return; |
| 9969 | if (palm_state != state) { |
| 9970 | palm_state = state; |
| 9971 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor"); |
| 9972 | } |
| 9973 | } |
| 9974 | } |
| 9975 | |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9976 | static ssize_t dytc_lapmode_show(struct device *dev, |
| 9977 | struct device_attribute *attr, |
| 9978 | char *buf) |
| 9979 | { |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9980 | if (has_lapsensor) |
| 9981 | return sysfs_emit(buf, "%d\n", lap_state); |
| 9982 | return sysfs_emit(buf, "\n"); |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9983 | } |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9984 | static DEVICE_ATTR_RO(dytc_lapmode); |
| 9985 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9986 | static ssize_t palmsensor_show(struct device *dev, |
| 9987 | struct device_attribute *attr, |
| 9988 | char *buf) |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9989 | { |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9990 | if (has_palmsensor) |
| 9991 | return sysfs_emit(buf, "%d\n", palm_state); |
| 9992 | return sysfs_emit(buf, "\n"); |
| 9993 | } |
| 9994 | static DEVICE_ATTR_RO(palmsensor); |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 9995 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 9996 | static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm) |
| 9997 | { |
| 9998 | int palm_err, lap_err, err; |
| 9999 | |
| 10000 | palm_err = palmsensor_get(&has_palmsensor, &palm_state); |
| 10001 | lap_err = lapsensor_get(&has_lapsensor, &lap_state); |
| 10002 | /* |
| 10003 | * If support isn't available (ENODEV) for both devices then quit, but |
| 10004 | * don't return an error. |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 10005 | */ |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 10006 | if ((palm_err == -ENODEV) && (lap_err == -ENODEV)) |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 10007 | return 0; |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 10008 | /* Otherwise, if there was an error return it */ |
Mark Pearson | aa44afa | 2020-12-29 21:47:26 -0500 | [diff] [blame] | 10009 | if (palm_err && (palm_err != -ENODEV)) |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 10010 | return palm_err; |
Mark Pearson | aa44afa | 2020-12-29 21:47:26 -0500 | [diff] [blame] | 10011 | if (lap_err && (lap_err != -ENODEV)) |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 10012 | return lap_err; |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 10013 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 10014 | if (has_palmsensor) { |
| 10015 | err = sysfs_create_file(&tpacpi_pdev->dev.kobj, &dev_attr_palmsensor.attr); |
| 10016 | if (err) |
| 10017 | return err; |
| 10018 | } |
Mark Pearson | 0b4f5c9 | 2021-03-11 12:48:43 -0500 | [diff] [blame] | 10019 | |
| 10020 | /* Check if we know the DYTC version, if we don't then get it */ |
| 10021 | if (!dytc_version) { |
| 10022 | err = dytc_get_version(); |
| 10023 | if (err) |
| 10024 | return err; |
| 10025 | } |
| 10026 | /* |
| 10027 | * Platforms before DYTC version 5 claim to have a lap sensor, but it doesn't work, so we |
| 10028 | * ignore them |
| 10029 | */ |
| 10030 | if (has_lapsensor && (dytc_version >= 5)) { |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 10031 | err = sysfs_create_file(&tpacpi_pdev->dev.kobj, &dev_attr_dytc_lapmode.attr); |
| 10032 | if (err) |
| 10033 | return err; |
| 10034 | } |
| 10035 | return 0; |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 10036 | } |
| 10037 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 10038 | static void proxsensor_exit(void) |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 10039 | { |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 10040 | if (has_lapsensor) |
| 10041 | sysfs_remove_file(&tpacpi_pdev->dev.kobj, &dev_attr_dytc_lapmode.attr); |
| 10042 | if (has_palmsensor) |
| 10043 | sysfs_remove_file(&tpacpi_pdev->dev.kobj, &dev_attr_palmsensor.attr); |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 10044 | } |
| 10045 | |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 10046 | static struct ibm_struct proxsensor_driver_data = { |
| 10047 | .name = "proximity-sensor", |
| 10048 | .exit = proxsensor_exit, |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 10049 | }; |
| 10050 | |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10051 | /************************************************************************* |
| 10052 | * DYTC Platform Profile interface |
| 10053 | */ |
| 10054 | |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10055 | #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */ |
Mark Pearson | 7a47f86 | 2021-04-06 19:32:03 -0400 | [diff] [blame] | 10056 | #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */ |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10057 | #define DYTC_CMD_RESET 0x1ff /* To reset back to default */ |
| 10058 | |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10059 | #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */ |
| 10060 | #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */ |
| 10061 | |
| 10062 | #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */ |
| 10063 | #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */ |
| 10064 | #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */ |
| 10065 | |
| 10066 | #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */ |
| 10067 | #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */ |
| 10068 | #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */ |
| 10069 | |
| 10070 | #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */ |
| 10071 | #define DYTC_MODE_LOWPOWER 3 /* Low power mode */ |
| 10072 | #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */ |
Mark Pearson | 7a47f86 | 2021-04-06 19:32:03 -0400 | [diff] [blame] | 10073 | #define DYTC_MODE_MMC_BALANCE 0 /* Default mode from MMC_GET, aka balanced */ |
| 10074 | |
| 10075 | #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */ |
| 10076 | #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */ |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10077 | |
| 10078 | #define DYTC_SET_COMMAND(function, mode, on) \ |
| 10079 | (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \ |
| 10080 | (mode) << DYTC_SET_MODE_BIT | \ |
| 10081 | (on) << DYTC_SET_VALID_BIT) |
| 10082 | |
| 10083 | #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0) |
| 10084 | |
| 10085 | #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1) |
| 10086 | |
| 10087 | static bool dytc_profile_available; |
| 10088 | static enum platform_profile_option dytc_current_profile; |
| 10089 | static atomic_t dytc_ignore_event = ATOMIC_INIT(0); |
| 10090 | static DEFINE_MUTEX(dytc_mutex); |
Mark Pearson | 7a47f86 | 2021-04-06 19:32:03 -0400 | [diff] [blame] | 10091 | static bool dytc_mmc_get_available; |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10092 | |
| 10093 | static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile) |
| 10094 | { |
| 10095 | switch (dytcmode) { |
| 10096 | case DYTC_MODE_LOWPOWER: |
| 10097 | *profile = PLATFORM_PROFILE_LOW_POWER; |
| 10098 | break; |
| 10099 | case DYTC_MODE_BALANCE: |
Mark Pearson | 7a47f86 | 2021-04-06 19:32:03 -0400 | [diff] [blame] | 10100 | case DYTC_MODE_MMC_BALANCE: |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10101 | *profile = PLATFORM_PROFILE_BALANCED; |
| 10102 | break; |
| 10103 | case DYTC_MODE_PERFORM: |
| 10104 | *profile = PLATFORM_PROFILE_PERFORMANCE; |
| 10105 | break; |
| 10106 | default: /* Unknown mode */ |
| 10107 | return -EINVAL; |
| 10108 | } |
| 10109 | return 0; |
| 10110 | } |
| 10111 | |
| 10112 | static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode) |
| 10113 | { |
| 10114 | switch (profile) { |
| 10115 | case PLATFORM_PROFILE_LOW_POWER: |
| 10116 | *perfmode = DYTC_MODE_LOWPOWER; |
| 10117 | break; |
| 10118 | case PLATFORM_PROFILE_BALANCED: |
| 10119 | *perfmode = DYTC_MODE_BALANCE; |
| 10120 | break; |
| 10121 | case PLATFORM_PROFILE_PERFORMANCE: |
| 10122 | *perfmode = DYTC_MODE_PERFORM; |
| 10123 | break; |
| 10124 | default: /* Unknown profile */ |
| 10125 | return -EOPNOTSUPP; |
| 10126 | } |
| 10127 | return 0; |
| 10128 | } |
| 10129 | |
| 10130 | /* |
| 10131 | * dytc_profile_get: Function to register with platform_profile |
| 10132 | * handler. Returns current platform profile. |
| 10133 | */ |
Hans de Goede | d2386d7 | 2021-02-04 15:01:57 +0100 | [diff] [blame] | 10134 | static int dytc_profile_get(struct platform_profile_handler *pprof, |
| 10135 | enum platform_profile_option *profile) |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10136 | { |
| 10137 | *profile = dytc_current_profile; |
| 10138 | return 0; |
| 10139 | } |
| 10140 | |
| 10141 | /* |
| 10142 | * Helper function - check if we are in CQL mode and if we are |
| 10143 | * - disable CQL, |
| 10144 | * - run the command |
| 10145 | * - enable CQL |
| 10146 | * If not in CQL mode, just run the command |
| 10147 | */ |
Hans de Goede | d2386d7 | 2021-02-04 15:01:57 +0100 | [diff] [blame] | 10148 | static int dytc_cql_command(int command, int *output) |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10149 | { |
| 10150 | int err, cmd_err, dummy; |
| 10151 | int cur_funcmode; |
| 10152 | |
| 10153 | /* Determine if we are in CQL mode. This alters the commands we do */ |
| 10154 | err = dytc_command(DYTC_CMD_GET, output); |
| 10155 | if (err) |
| 10156 | return err; |
| 10157 | |
| 10158 | cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF; |
| 10159 | /* Check if we're OK to return immediately */ |
| 10160 | if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL)) |
| 10161 | return 0; |
| 10162 | |
| 10163 | if (cur_funcmode == DYTC_FUNCTION_CQL) { |
| 10164 | atomic_inc(&dytc_ignore_event); |
| 10165 | err = dytc_command(DYTC_DISABLE_CQL, &dummy); |
| 10166 | if (err) |
| 10167 | return err; |
| 10168 | } |
| 10169 | |
| 10170 | cmd_err = dytc_command(command, output); |
| 10171 | /* Check return condition after we've restored CQL state */ |
| 10172 | |
| 10173 | if (cur_funcmode == DYTC_FUNCTION_CQL) { |
| 10174 | err = dytc_command(DYTC_ENABLE_CQL, &dummy); |
| 10175 | if (err) |
| 10176 | return err; |
| 10177 | } |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10178 | return cmd_err; |
| 10179 | } |
| 10180 | |
| 10181 | /* |
| 10182 | * dytc_profile_set: Function to register with platform_profile |
| 10183 | * handler. Sets current platform profile. |
| 10184 | */ |
Hans de Goede | d2386d7 | 2021-02-04 15:01:57 +0100 | [diff] [blame] | 10185 | static int dytc_profile_set(struct platform_profile_handler *pprof, |
| 10186 | enum platform_profile_option profile) |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10187 | { |
| 10188 | int output; |
| 10189 | int err; |
| 10190 | |
| 10191 | if (!dytc_profile_available) |
| 10192 | return -ENODEV; |
| 10193 | |
| 10194 | err = mutex_lock_interruptible(&dytc_mutex); |
| 10195 | if (err) |
| 10196 | return err; |
| 10197 | |
| 10198 | if (profile == PLATFORM_PROFILE_BALANCED) { |
Hans de Goede | e157c95 | 2021-03-21 12:31:08 +0100 | [diff] [blame] | 10199 | /* |
| 10200 | * To get back to balanced mode we need to issue a reset command. |
| 10201 | * Note we still need to disable CQL mode before hand and re-enable |
| 10202 | * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays |
| 10203 | * stuck at 0 for aprox. 30 minutes. |
| 10204 | */ |
| 10205 | err = dytc_cql_command(DYTC_CMD_RESET, &output); |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10206 | if (err) |
| 10207 | goto unlock; |
| 10208 | } else { |
| 10209 | int perfmode; |
| 10210 | |
| 10211 | err = convert_profile_to_dytc(profile, &perfmode); |
| 10212 | if (err) |
| 10213 | goto unlock; |
| 10214 | |
| 10215 | /* Determine if we are in CQL mode. This alters the commands we do */ |
| 10216 | err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1), &output); |
| 10217 | if (err) |
| 10218 | goto unlock; |
| 10219 | } |
| 10220 | /* Success - update current profile */ |
| 10221 | dytc_current_profile = profile; |
| 10222 | unlock: |
| 10223 | mutex_unlock(&dytc_mutex); |
| 10224 | return err; |
| 10225 | } |
| 10226 | |
| 10227 | static void dytc_profile_refresh(void) |
| 10228 | { |
| 10229 | enum platform_profile_option profile; |
| 10230 | int output, err; |
| 10231 | int perfmode; |
| 10232 | |
| 10233 | mutex_lock(&dytc_mutex); |
Mark Pearson | 7a47f86 | 2021-04-06 19:32:03 -0400 | [diff] [blame] | 10234 | if (dytc_mmc_get_available) |
| 10235 | err = dytc_command(DYTC_CMD_MMC_GET, &output); |
| 10236 | else |
| 10237 | err = dytc_cql_command(DYTC_CMD_GET, &output); |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10238 | mutex_unlock(&dytc_mutex); |
| 10239 | if (err) |
| 10240 | return; |
| 10241 | |
| 10242 | perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF; |
| 10243 | convert_dytc_to_profile(perfmode, &profile); |
| 10244 | if (profile != dytc_current_profile) { |
| 10245 | dytc_current_profile = profile; |
| 10246 | platform_profile_notify(); |
| 10247 | } |
| 10248 | } |
| 10249 | |
| 10250 | static struct platform_profile_handler dytc_profile = { |
| 10251 | .profile_get = dytc_profile_get, |
| 10252 | .profile_set = dytc_profile_set, |
| 10253 | }; |
| 10254 | |
| 10255 | static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm) |
| 10256 | { |
| 10257 | int err, output; |
| 10258 | |
| 10259 | /* Setup supported modes */ |
| 10260 | set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices); |
| 10261 | set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices); |
| 10262 | set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices); |
| 10263 | |
| 10264 | dytc_profile_available = false; |
| 10265 | err = dytc_command(DYTC_CMD_QUERY, &output); |
| 10266 | /* |
| 10267 | * If support isn't available (ENODEV) then don't return an error |
| 10268 | * and don't create the sysfs group |
| 10269 | */ |
| 10270 | if (err == -ENODEV) |
| 10271 | return 0; |
| 10272 | /* For all other errors we can flag the failure */ |
| 10273 | if (err) |
| 10274 | return err; |
| 10275 | |
Mark Pearson | 0b4f5c9 | 2021-03-11 12:48:43 -0500 | [diff] [blame] | 10276 | /* Check if we know the DYTC version, if we don't then get it */ |
| 10277 | if (!dytc_version) { |
| 10278 | err = dytc_get_version(); |
| 10279 | if (err) |
| 10280 | return err; |
| 10281 | } |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10282 | /* Check DYTC is enabled and supports mode setting */ |
Mark Pearson | 0b4f5c9 | 2021-03-11 12:48:43 -0500 | [diff] [blame] | 10283 | if (dytc_version >= 5) { |
| 10284 | dbg_printk(TPACPI_DBG_INIT, |
| 10285 | "DYTC version %d: thermal mode available\n", dytc_version); |
Mark Pearson | 7a47f86 | 2021-04-06 19:32:03 -0400 | [diff] [blame] | 10286 | /* |
| 10287 | * Check if MMC_GET functionality available |
| 10288 | * Version > 6 and return success from MMC_GET command |
| 10289 | */ |
| 10290 | dytc_mmc_get_available = false; |
| 10291 | if (dytc_version >= 6) { |
| 10292 | err = dytc_command(DYTC_CMD_MMC_GET, &output); |
| 10293 | if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS)) |
| 10294 | dytc_mmc_get_available = true; |
| 10295 | } |
Mark Pearson | 0b4f5c9 | 2021-03-11 12:48:43 -0500 | [diff] [blame] | 10296 | /* Create platform_profile structure and register */ |
| 10297 | err = platform_profile_register(&dytc_profile); |
| 10298 | /* |
| 10299 | * If for some reason platform_profiles aren't enabled |
| 10300 | * don't quit terminally. |
| 10301 | */ |
| 10302 | if (err) |
| 10303 | return 0; |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10304 | |
Mark Pearson | 0b4f5c9 | 2021-03-11 12:48:43 -0500 | [diff] [blame] | 10305 | dytc_profile_available = true; |
| 10306 | /* Ensure initial values are correct */ |
| 10307 | dytc_profile_refresh(); |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10308 | } |
| 10309 | return 0; |
| 10310 | } |
| 10311 | |
| 10312 | static void dytc_profile_exit(void) |
| 10313 | { |
| 10314 | if (dytc_profile_available) { |
| 10315 | dytc_profile_available = false; |
| 10316 | platform_profile_remove(); |
| 10317 | } |
| 10318 | } |
| 10319 | |
| 10320 | static struct ibm_struct dytc_profile_driver_data = { |
| 10321 | .name = "dytc-profile", |
| 10322 | .exit = dytc_profile_exit, |
| 10323 | }; |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10324 | |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10325 | /************************************************************************* |
| 10326 | * Keyboard language interface |
| 10327 | */ |
| 10328 | |
| 10329 | struct keyboard_lang_data { |
| 10330 | const char *lang_str; |
| 10331 | int lang_code; |
| 10332 | }; |
| 10333 | |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10334 | static const struct keyboard_lang_data keyboard_lang_data[] = { |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10335 | {"be", 0x080c}, |
| 10336 | {"cz", 0x0405}, |
| 10337 | {"da", 0x0406}, |
| 10338 | {"de", 0x0c07}, |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10339 | {"en", 0x0000}, |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10340 | {"es", 0x2c0a}, |
| 10341 | {"et", 0x0425}, |
| 10342 | {"fr", 0x040c}, |
| 10343 | {"fr-ch", 0x100c}, |
| 10344 | {"hu", 0x040e}, |
| 10345 | {"it", 0x0410}, |
| 10346 | {"jp", 0x0411}, |
| 10347 | {"nl", 0x0413}, |
| 10348 | {"nn", 0x0414}, |
| 10349 | {"pl", 0x0415}, |
| 10350 | {"pt", 0x0816}, |
| 10351 | {"sl", 0x041b}, |
| 10352 | {"sv", 0x081d}, |
| 10353 | {"tr", 0x041f}, |
| 10354 | }; |
| 10355 | |
| 10356 | static int set_keyboard_lang_command(int command) |
| 10357 | { |
| 10358 | acpi_handle sskl_handle; |
| 10359 | int output; |
| 10360 | |
| 10361 | if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) { |
| 10362 | /* Platform doesn't support SSKL */ |
| 10363 | return -ENODEV; |
| 10364 | } |
| 10365 | |
| 10366 | if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command)) |
| 10367 | return -EIO; |
| 10368 | |
| 10369 | return 0; |
| 10370 | } |
| 10371 | |
| 10372 | static int get_keyboard_lang(int *output) |
| 10373 | { |
| 10374 | acpi_handle gskl_handle; |
| 10375 | int kbd_lang; |
| 10376 | |
| 10377 | if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) { |
| 10378 | /* Platform doesn't support GSKL */ |
| 10379 | return -ENODEV; |
| 10380 | } |
| 10381 | |
| 10382 | if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000)) |
| 10383 | return -EIO; |
| 10384 | |
Hans de Goede | 9e9c641 | 2021-01-25 21:52:58 +0100 | [diff] [blame] | 10385 | /* |
| 10386 | * METHOD_ERR gets returned on devices where there are no special (e.g. '=', |
| 10387 | * '(' and ')') keys which use layout dependent key-press emulation. |
| 10388 | */ |
| 10389 | if (kbd_lang & METHOD_ERR) |
| 10390 | return -ENODEV; |
| 10391 | |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10392 | *output = kbd_lang; |
| 10393 | |
| 10394 | return 0; |
| 10395 | } |
| 10396 | |
| 10397 | /* sysfs keyboard language entry */ |
| 10398 | static ssize_t keyboard_lang_show(struct device *dev, |
| 10399 | struct device_attribute *attr, |
| 10400 | char *buf) |
| 10401 | { |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10402 | int output, err, i, len = 0; |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10403 | |
| 10404 | err = get_keyboard_lang(&output); |
| 10405 | if (err) |
| 10406 | return err; |
| 10407 | |
| 10408 | for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) { |
| 10409 | if (i) |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10410 | len += sysfs_emit_at(buf, len, "%s", " "); |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10411 | |
| 10412 | if (output == keyboard_lang_data[i].lang_code) { |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10413 | len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str); |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10414 | } else { |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10415 | len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str); |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10416 | } |
| 10417 | } |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10418 | len += sysfs_emit_at(buf, len, "\n"); |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10419 | |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10420 | return len; |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10421 | } |
| 10422 | |
| 10423 | static ssize_t keyboard_lang_store(struct device *dev, |
| 10424 | struct device_attribute *attr, |
| 10425 | const char *buf, size_t count) |
| 10426 | { |
| 10427 | int err, i; |
| 10428 | bool lang_found = false; |
| 10429 | int lang_code = 0; |
| 10430 | |
| 10431 | for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) { |
| 10432 | if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) { |
| 10433 | lang_code = keyboard_lang_data[i].lang_code; |
| 10434 | lang_found = true; |
| 10435 | break; |
| 10436 | } |
| 10437 | } |
| 10438 | |
| 10439 | if (lang_found) { |
| 10440 | lang_code = lang_code | 1 << 24; |
| 10441 | |
| 10442 | /* Set language code */ |
| 10443 | err = set_keyboard_lang_command(lang_code); |
| 10444 | if (err) |
| 10445 | return err; |
| 10446 | } else { |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10447 | dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n"); |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10448 | return -EINVAL; |
| 10449 | } |
| 10450 | |
| 10451 | tpacpi_disclose_usertask(attr->attr.name, |
| 10452 | "keyboard language is set to %s\n", buf); |
| 10453 | |
| 10454 | sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang"); |
| 10455 | |
| 10456 | return count; |
| 10457 | } |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10458 | static DEVICE_ATTR_RW(keyboard_lang); |
| 10459 | |
| 10460 | static struct attribute *kbdlang_attributes[] = { |
| 10461 | &dev_attr_keyboard_lang.attr, |
| 10462 | NULL |
| 10463 | }; |
| 10464 | |
| 10465 | static const struct attribute_group kbdlang_attr_group = { |
| 10466 | .attrs = kbdlang_attributes, |
| 10467 | }; |
| 10468 | |
| 10469 | static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm) |
| 10470 | { |
| 10471 | int err, output; |
| 10472 | |
| 10473 | err = get_keyboard_lang(&output); |
| 10474 | /* |
| 10475 | * If support isn't available (ENODEV) then don't return an error |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10476 | * just don't create the sysfs group. |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10477 | */ |
| 10478 | if (err == -ENODEV) |
| 10479 | return 0; |
| 10480 | |
| 10481 | if (err) |
| 10482 | return err; |
| 10483 | |
| 10484 | /* Platform supports this feature - create the sysfs file */ |
Nitin Joshi | cfa75cc | 2021-02-02 09:32:10 +0900 | [diff] [blame] | 10485 | return sysfs_create_group(&tpacpi_pdev->dev.kobj, &kbdlang_attr_group); |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 10486 | } |
| 10487 | |
| 10488 | static void kbdlang_exit(void) |
| 10489 | { |
| 10490 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, &kbdlang_attr_group); |
| 10491 | } |
| 10492 | |
| 10493 | static struct ibm_struct kbdlang_driver_data = { |
| 10494 | .name = "kbdlang", |
| 10495 | .exit = kbdlang_exit, |
| 10496 | }; |
| 10497 | |
Nitin Joshi | 3feb52a | 2021-03-17 11:46:36 +0900 | [diff] [blame] | 10498 | /************************************************************************* |
| 10499 | * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN |
| 10500 | * and WLAN feature. |
| 10501 | */ |
| 10502 | #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000 |
| 10503 | #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4) |
| 10504 | #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8) |
| 10505 | static bool has_antennatype; |
| 10506 | static int wwan_antennatype; |
| 10507 | |
| 10508 | static int dprc_command(int command, int *output) |
| 10509 | { |
| 10510 | acpi_handle dprc_handle; |
| 10511 | |
| 10512 | if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) { |
| 10513 | /* Platform doesn't support DPRC */ |
| 10514 | return -ENODEV; |
| 10515 | } |
| 10516 | |
| 10517 | if (!acpi_evalf(dprc_handle, output, NULL, "dd", command)) |
| 10518 | return -EIO; |
| 10519 | |
| 10520 | /* |
| 10521 | * METHOD_ERR gets returned on devices where few commands are not supported |
| 10522 | * for example command to get WWAN Antenna type command is not supported on |
| 10523 | * some devices. |
| 10524 | */ |
| 10525 | if (*output & METHOD_ERR) |
| 10526 | return -ENODEV; |
| 10527 | |
| 10528 | return 0; |
| 10529 | } |
| 10530 | |
| 10531 | static int get_wwan_antenna(int *wwan_antennatype) |
| 10532 | { |
| 10533 | int output, err; |
| 10534 | |
| 10535 | /* Get current Antenna type */ |
| 10536 | err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output); |
| 10537 | if (err) |
| 10538 | return err; |
| 10539 | |
| 10540 | if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT) |
| 10541 | *wwan_antennatype = 1; |
| 10542 | else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT) |
| 10543 | *wwan_antennatype = 2; |
| 10544 | else |
| 10545 | return -ENODEV; |
| 10546 | |
| 10547 | return 0; |
| 10548 | } |
| 10549 | |
| 10550 | /* sysfs wwan antenna type entry */ |
| 10551 | static ssize_t wwan_antenna_type_show(struct device *dev, |
| 10552 | struct device_attribute *attr, |
| 10553 | char *buf) |
| 10554 | { |
| 10555 | switch (wwan_antennatype) { |
| 10556 | case 1: |
| 10557 | return sysfs_emit(buf, "type a\n"); |
| 10558 | case 2: |
| 10559 | return sysfs_emit(buf, "type b\n"); |
| 10560 | default: |
| 10561 | return -ENODATA; |
| 10562 | } |
| 10563 | } |
| 10564 | static DEVICE_ATTR_RO(wwan_antenna_type); |
| 10565 | |
| 10566 | static int tpacpi_dprc_init(struct ibm_init_struct *iibm) |
| 10567 | { |
| 10568 | int wwanantenna_err, err; |
| 10569 | |
| 10570 | wwanantenna_err = get_wwan_antenna(&wwan_antennatype); |
| 10571 | /* |
| 10572 | * If support isn't available (ENODEV) then quit, but don't |
| 10573 | * return an error. |
| 10574 | */ |
| 10575 | if (wwanantenna_err == -ENODEV) |
| 10576 | return 0; |
| 10577 | |
| 10578 | /* if there was an error return it */ |
| 10579 | if (wwanantenna_err && (wwanantenna_err != -ENODEV)) |
| 10580 | return wwanantenna_err; |
| 10581 | else if (!wwanantenna_err) |
| 10582 | has_antennatype = true; |
| 10583 | |
| 10584 | if (has_antennatype) { |
| 10585 | err = sysfs_create_file(&tpacpi_pdev->dev.kobj, &dev_attr_wwan_antenna_type.attr); |
| 10586 | if (err) |
| 10587 | return err; |
| 10588 | } |
| 10589 | return 0; |
| 10590 | } |
| 10591 | |
| 10592 | static void dprc_exit(void) |
| 10593 | { |
| 10594 | if (has_antennatype) |
| 10595 | sysfs_remove_file(&tpacpi_pdev->dev.kobj, &dev_attr_wwan_antenna_type.attr); |
| 10596 | } |
| 10597 | |
| 10598 | static struct ibm_struct dprc_driver_data = { |
| 10599 | .name = "dprc", |
| 10600 | .exit = dprc_exit, |
| 10601 | }; |
| 10602 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 10603 | /**************************************************************************** |
| 10604 | **************************************************************************** |
| 10605 | * |
| 10606 | * Infrastructure |
| 10607 | * |
| 10608 | **************************************************************************** |
| 10609 | ****************************************************************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10610 | |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 10611 | /* |
| 10612 | * HKEY event callout for other subdrivers go here |
| 10613 | * (yes, it is ugly, but it is quick, safe, and gets the job done |
| 10614 | */ |
| 10615 | static void tpacpi_driver_event(const unsigned int hkey_event) |
| 10616 | { |
Henrique de Moraes Holschuh | 347a268 | 2009-12-09 01:36:24 +0000 | [diff] [blame] | 10617 | if (ibm_backlight_device) { |
| 10618 | switch (hkey_event) { |
| 10619 | case TP_HKEY_EV_BRGHT_UP: |
| 10620 | case TP_HKEY_EV_BRGHT_DOWN: |
| 10621 | tpacpi_brightness_notify_change(); |
| 10622 | } |
| 10623 | } |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 10624 | if (alsa_card) { |
| 10625 | switch (hkey_event) { |
| 10626 | case TP_HKEY_EV_VOL_UP: |
| 10627 | case TP_HKEY_EV_VOL_DOWN: |
| 10628 | case TP_HKEY_EV_VOL_MUTE: |
| 10629 | volume_alsa_notify_change(); |
| 10630 | } |
| 10631 | } |
Hans de Goede | c685e20 | 2017-02-09 16:44:13 +0100 | [diff] [blame] | 10632 | if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) { |
| 10633 | enum led_brightness brightness; |
| 10634 | |
| 10635 | mutex_lock(&kbdlight_mutex); |
| 10636 | |
| 10637 | /* |
| 10638 | * Check the brightness actually changed, setting the brightness |
| 10639 | * through kbdlight_set_level() also triggers this event. |
| 10640 | */ |
| 10641 | brightness = kbdlight_sysfs_get(NULL); |
| 10642 | if (kbdlight_brightness != brightness) { |
| 10643 | kbdlight_brightness = brightness; |
| 10644 | led_classdev_notify_brightness_hw_changed( |
| 10645 | &tpacpi_led_kbdlight.led_classdev, brightness); |
| 10646 | } |
| 10647 | |
| 10648 | mutex_unlock(&kbdlight_mutex); |
| 10649 | } |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 10650 | |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10651 | if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) { |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 10652 | lapsensor_refresh(); |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10653 | /* If we are already accessing DYTC then skip dytc update */ |
| 10654 | if (!atomic_add_unless(&dytc_ignore_event, -1, 0)) |
| 10655 | dytc_profile_refresh(); |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 10656 | } |
Hans de Goede | f809891 | 2021-10-05 22:23:20 +0200 | [diff] [blame] | 10657 | |
| 10658 | if (lcdshadow_dev && hkey_event == TP_HKEY_EV_PRIVACYGUARD_TOGGLE) { |
| 10659 | enum drm_privacy_screen_status old_hw_state; |
| 10660 | bool changed; |
| 10661 | |
| 10662 | mutex_lock(&lcdshadow_dev->lock); |
| 10663 | old_hw_state = lcdshadow_dev->hw_state; |
| 10664 | lcdshadow_get_hw_state(lcdshadow_dev); |
| 10665 | changed = lcdshadow_dev->hw_state != old_hw_state; |
| 10666 | mutex_unlock(&lcdshadow_dev->lock); |
| 10667 | |
| 10668 | if (changed) |
| 10669 | drm_privacy_screen_call_notifier_chain(lcdshadow_dev); |
| 10670 | } |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 10671 | } |
| 10672 | |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 10673 | static void hotkey_driver_event(const unsigned int scancode) |
| 10674 | { |
Henrique de Moraes Holschuh | 67bcae6 | 2009-09-20 14:09:27 -0300 | [diff] [blame] | 10675 | tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode); |
Henrique de Moraes Holschuh | 8b468c0 | 2009-09-20 14:09:26 -0300 | [diff] [blame] | 10676 | } |
| 10677 | |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 10678 | /* --------------------------------------------------------------------- */ |
| 10679 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 10680 | /* /proc support */ |
Henrique de Moraes Holschuh | 94954cc6 | 2007-07-18 23:45:27 -0300 | [diff] [blame] | 10681 | static struct proc_dir_entry *proc_dir; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 10682 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 10683 | /* |
| 10684 | * Module and infrastructure proble, init and exit handling |
| 10685 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10686 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 10687 | static bool force_load; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 10688 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 10689 | #ifdef CONFIG_THINKPAD_ACPI_DEBUG |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 10690 | static const char * __init str_supported(int is_supported) |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 10691 | { |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 10692 | static char text_unsupported[] __initdata = "not supported"; |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 10693 | |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 10694 | return (is_supported) ? &text_unsupported[4] : &text_unsupported[0]; |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 10695 | } |
| 10696 | #endif /* CONFIG_THINKPAD_ACPI_DEBUG */ |
| 10697 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 10698 | static void ibm_exit(struct ibm_struct *ibm) |
| 10699 | { |
| 10700 | dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name); |
| 10701 | |
| 10702 | list_del_init(&ibm->all_drivers); |
| 10703 | |
| 10704 | if (ibm->flags.acpi_notify_installed) { |
| 10705 | dbg_printk(TPACPI_DBG_EXIT, |
| 10706 | "%s: acpi_remove_notify_handler\n", ibm->name); |
| 10707 | BUG_ON(!ibm->acpi); |
| 10708 | acpi_remove_notify_handler(*ibm->acpi->handle, |
| 10709 | ibm->acpi->type, |
| 10710 | dispatch_acpi_notify); |
| 10711 | ibm->flags.acpi_notify_installed = 0; |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 10712 | } |
| 10713 | |
| 10714 | if (ibm->flags.proc_created) { |
| 10715 | dbg_printk(TPACPI_DBG_EXIT, |
| 10716 | "%s: remove_proc_entry\n", ibm->name); |
| 10717 | remove_proc_entry(ibm->name, proc_dir); |
| 10718 | ibm->flags.proc_created = 0; |
| 10719 | } |
| 10720 | |
| 10721 | if (ibm->flags.acpi_driver_registered) { |
| 10722 | dbg_printk(TPACPI_DBG_EXIT, |
| 10723 | "%s: acpi_bus_unregister_driver\n", ibm->name); |
| 10724 | BUG_ON(!ibm->acpi); |
| 10725 | acpi_bus_unregister_driver(ibm->acpi->driver); |
| 10726 | kfree(ibm->acpi->driver); |
| 10727 | ibm->acpi->driver = NULL; |
| 10728 | ibm->flags.acpi_driver_registered = 0; |
| 10729 | } |
| 10730 | |
| 10731 | if (ibm->flags.init_called && ibm->exit) { |
| 10732 | ibm->exit(); |
| 10733 | ibm->flags.init_called = 0; |
| 10734 | } |
| 10735 | |
| 10736 | dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name); |
| 10737 | } |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 10738 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 10739 | static int __init ibm_init(struct ibm_init_struct *iibm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10740 | { |
| 10741 | int ret; |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 10742 | struct ibm_struct *ibm = iibm->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10743 | struct proc_dir_entry *entry; |
| 10744 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 10745 | BUG_ON(ibm == NULL); |
| 10746 | |
| 10747 | INIT_LIST_HEAD(&ibm->all_drivers); |
| 10748 | |
Henrique de Moraes Holschuh | 9264117 | 2007-04-21 11:08:35 -0300 | [diff] [blame] | 10749 | if (ibm->flags.experimental && !experimental) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10750 | return 0; |
| 10751 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 10752 | dbg_printk(TPACPI_DBG_INIT, |
| 10753 | "probing for %s\n", ibm->name); |
| 10754 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 10755 | if (iibm->init) { |
| 10756 | ret = iibm->init(iibm); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10757 | if (ret > 0) |
| 10758 | return 0; /* probe failed */ |
| 10759 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10760 | return ret; |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 10761 | |
Henrique de Moraes Holschuh | 9264117 | 2007-04-21 11:08:35 -0300 | [diff] [blame] | 10762 | ibm->flags.init_called = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10763 | } |
| 10764 | |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 10765 | if (ibm->acpi) { |
| 10766 | if (ibm->acpi->hid) { |
| 10767 | ret = register_tpacpi_subdriver(ibm); |
| 10768 | if (ret) |
| 10769 | goto err_out; |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10770 | } |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 10771 | |
| 10772 | if (ibm->acpi->notify) { |
| 10773 | ret = setup_acpi_notify(ibm); |
| 10774 | if (ret == -ENODEV) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 10775 | pr_notice("disabling subdriver %s\n", |
| 10776 | ibm->name); |
Henrique de Moraes Holschuh | 8d376cd | 2007-04-21 11:08:37 -0300 | [diff] [blame] | 10777 | ret = 0; |
| 10778 | goto err_out; |
| 10779 | } |
| 10780 | if (ret < 0) |
| 10781 | goto err_out; |
| 10782 | } |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10783 | } |
| 10784 | |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 10785 | dbg_printk(TPACPI_DBG_INIT, |
| 10786 | "%s installed\n", ibm->name); |
| 10787 | |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 10788 | if (ibm->read) { |
Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 10789 | umode_t mode = iibm->base_procfs_mode; |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 10790 | |
Henrique de Moraes Holschuh | b525c06 | 2010-02-25 22:22:22 -0300 | [diff] [blame] | 10791 | if (!mode) |
| 10792 | mode = S_IRUGO; |
Alexey Dobriyan | 887965e | 2009-12-15 21:51:12 -0200 | [diff] [blame] | 10793 | if (ibm->write) |
| 10794 | mode |= S_IWUSR; |
| 10795 | entry = proc_create_data(ibm->name, mode, proc_dir, |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 10796 | &dispatch_proc_ops, ibm); |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 10797 | if (!entry) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 10798 | pr_err("unable to create proc entry %s\n", ibm->name); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10799 | ret = -ENODEV; |
| 10800 | goto err_out; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 10801 | } |
Henrique de Moraes Holschuh | 9264117 | 2007-04-21 11:08:35 -0300 | [diff] [blame] | 10802 | ibm->flags.proc_created = 1; |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 10803 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10804 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 10805 | list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers); |
| 10806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10807 | return 0; |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10808 | |
| 10809 | err_out: |
Henrique de Moraes Holschuh | fe08bc4 | 2007-04-21 11:08:32 -0300 | [diff] [blame] | 10810 | dbg_printk(TPACPI_DBG_INIT, |
| 10811 | "%s: at error exit path with result %d\n", |
| 10812 | ibm->name, ret); |
| 10813 | |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10814 | ibm_exit(ibm); |
Jan van den Berg | 72a979f | 2014-09-17 00:01:08 +0200 | [diff] [blame] | 10815 | return (ret < 0) ? ret : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10816 | } |
| 10817 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 10818 | /* Probing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10819 | |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 10820 | static char __init tpacpi_parse_fw_id(const char * const s, |
| 10821 | u32 *model, u16 *release) |
Henrique de Moraes Holschuh | 050df10 | 2009-05-30 13:25:05 -0300 | [diff] [blame] | 10822 | { |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 10823 | int i; |
Henrique de Moraes Holschuh | 050df10 | 2009-05-30 13:25:05 -0300 | [diff] [blame] | 10824 | |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 10825 | if (!s || strlen(s) < 8) |
| 10826 | goto invalid; |
| 10827 | |
| 10828 | for (i = 0; i < 8; i++) |
| 10829 | if (!((s[i] >= '0' && s[i] <= '9') || |
| 10830 | (s[i] >= 'A' && s[i] <= 'Z'))) |
| 10831 | goto invalid; |
| 10832 | |
Adam Lee | 1b0eb5b | 2015-02-11 13:43:10 +0800 | [diff] [blame] | 10833 | /* |
| 10834 | * Most models: xxyTkkWW (#.##c) |
| 10835 | * Ancient 570/600 and -SL lacks (#.##c) |
| 10836 | */ |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 10837 | if (s[3] == 'T' || s[3] == 'N') { |
| 10838 | *model = TPID(s[0], s[1]); |
| 10839 | *release = TPVER(s[4], s[5]); |
| 10840 | return s[2]; |
Adam Lee | 1b0eb5b | 2015-02-11 13:43:10 +0800 | [diff] [blame] | 10841 | |
| 10842 | /* New models: xxxyTkkW (#.##c); T550 and some others */ |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 10843 | } else if (s[4] == 'T' || s[4] == 'N') { |
| 10844 | *model = TPID3(s[0], s[1], s[2]); |
| 10845 | *release = TPVER(s[5], s[6]); |
| 10846 | return s[3]; |
| 10847 | } |
| 10848 | |
| 10849 | invalid: |
| 10850 | return '\0'; |
Henrique de Moraes Holschuh | 050df10 | 2009-05-30 13:25:05 -0300 | [diff] [blame] | 10851 | } |
| 10852 | |
Jiaxun Yang | 9a4b33d | 2019-03-08 21:14:26 +0800 | [diff] [blame] | 10853 | static void find_new_ec_fwstr(const struct dmi_header *dm, void *private) |
| 10854 | { |
| 10855 | char *ec_fw_string = (char *) private; |
| 10856 | const char *dmi_data = (const char *)dm; |
| 10857 | /* |
| 10858 | * ThinkPad Embedded Controller Program Table on newer models |
| 10859 | * |
| 10860 | * Offset | Name | Width | Description |
| 10861 | * ---------------------------------------------------- |
| 10862 | * 0x00 | Type | BYTE | 0x8C |
| 10863 | * 0x01 | Length | BYTE | |
| 10864 | * 0x02 | Handle | WORD | Varies |
| 10865 | * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO" |
| 10866 | * 0x0A | OEM struct offset | BYTE | 0x0B |
| 10867 | * 0x0B | OEM struct number | BYTE | 0x07, for this structure |
| 10868 | * 0x0C | OEM struct revision | BYTE | 0x01, for this format |
| 10869 | * 0x0D | ECP version ID | STR ID | |
| 10870 | * 0x0E | ECP release date | STR ID | |
| 10871 | */ |
| 10872 | |
| 10873 | /* Return if data structure not match */ |
| 10874 | if (dm->type != 140 || dm->length < 0x0F || |
| 10875 | memcmp(dmi_data + 4, "LENOVO", 6) != 0 || |
| 10876 | dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 || |
| 10877 | dmi_data[0x0C] != 0x01) |
| 10878 | return; |
| 10879 | |
| 10880 | /* fwstr is the first 8byte string */ |
| 10881 | strncpy(ec_fw_string, dmi_data + 0x0F, 8); |
| 10882 | } |
| 10883 | |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 10884 | /* returns 0 - probe ok, or < 0 - probe error. |
| 10885 | * Probe ok doesn't mean thinkpad found. |
| 10886 | * On error, kfree() cleanup on tp->* is not performed, caller must do it */ |
| 10887 | static int __must_check __init get_thinkpad_model_data( |
| 10888 | struct thinkpad_id_data *tp) |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 10889 | { |
Jeff Garzik | 1855256 | 2007-10-03 15:15:40 -0400 | [diff] [blame] | 10890 | const struct dmi_device *dev = NULL; |
Jiaxun Yang | 9a4b33d | 2019-03-08 21:14:26 +0800 | [diff] [blame] | 10891 | char ec_fw_string[18] = {0}; |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 10892 | char const *s; |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 10893 | char t; |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 10894 | |
Henrique de Moraes Holschuh | d5a2f2f | 2007-07-18 23:45:42 -0300 | [diff] [blame] | 10895 | if (!tp) |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 10896 | return -EINVAL; |
Henrique de Moraes Holschuh | d5a2f2f | 2007-07-18 23:45:42 -0300 | [diff] [blame] | 10897 | |
| 10898 | memset(tp, 0, sizeof(*tp)); |
| 10899 | |
| 10900 | if (dmi_name_in_vendors("IBM")) |
| 10901 | tp->vendor = PCI_VENDOR_ID_IBM; |
| 10902 | else if (dmi_name_in_vendors("LENOVO")) |
| 10903 | tp->vendor = PCI_VENDOR_ID_LENOVO; |
| 10904 | else |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 10905 | return 0; |
Henrique de Moraes Holschuh | d5a2f2f | 2007-07-18 23:45:42 -0300 | [diff] [blame] | 10906 | |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 10907 | s = dmi_get_system_info(DMI_BIOS_VERSION); |
| 10908 | tp->bios_version_str = kstrdup(s, GFP_KERNEL); |
| 10909 | if (s && !tp->bios_version_str) |
| 10910 | return -ENOMEM; |
Henrique de Moraes Holschuh | 050df10 | 2009-05-30 13:25:05 -0300 | [diff] [blame] | 10911 | |
| 10912 | /* Really ancient ThinkPad 240X will fail this, which is fine */ |
Jouke Witteveen | 846a416 | 2018-07-11 11:44:41 +0200 | [diff] [blame] | 10913 | t = tpacpi_parse_fw_id(tp->bios_version_str, |
| 10914 | &tp->bios_model, &tp->bios_release); |
| 10915 | if (t != 'E' && t != 'C') |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 10916 | return 0; |
Henrique de Moraes Holschuh | 050df10 | 2009-05-30 13:25:05 -0300 | [diff] [blame] | 10917 | |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 10918 | /* |
| 10919 | * ThinkPad T23 or newer, A31 or newer, R50e or newer, |
| 10920 | * X32 or newer, all Z series; Some models must have an |
| 10921 | * up-to-date BIOS or they will not be detected. |
| 10922 | * |
Alexander A. Klimov | 19206a1 | 2020-07-13 21:06:28 +0200 | [diff] [blame] | 10923 | * See https://thinkwiki.org/wiki/List_of_DMI_IDs |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 10924 | */ |
| 10925 | while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) { |
Henrique de Moraes Holschuh | 49a13cd | 2006-11-24 11:47:13 -0200 | [diff] [blame] | 10926 | if (sscanf(dev->name, |
| 10927 | "IBM ThinkPad Embedded Controller -[%17c", |
| 10928 | ec_fw_string) == 1) { |
| 10929 | ec_fw_string[sizeof(ec_fw_string) - 1] = 0; |
| 10930 | ec_fw_string[strcspn(ec_fw_string, " ]")] = 0; |
Henrique de Moraes Holschuh | d5a2f2f | 2007-07-18 23:45:42 -0300 | [diff] [blame] | 10931 | break; |
Henrique de Moraes Holschuh | 49a13cd | 2006-11-24 11:47:13 -0200 | [diff] [blame] | 10932 | } |
Henrique de Moraes Holschuh | 60eb0b3 | 2006-11-24 11:47:08 -0200 | [diff] [blame] | 10933 | } |
Henrique de Moraes Holschuh | d5a2f2f | 2007-07-18 23:45:42 -0300 | [diff] [blame] | 10934 | |
Jiaxun Yang | 9a4b33d | 2019-03-08 21:14:26 +0800 | [diff] [blame] | 10935 | /* Newer ThinkPads have different EC program info table */ |
| 10936 | if (!ec_fw_string[0]) |
| 10937 | dmi_walk(find_new_ec_fwstr, &ec_fw_string); |
| 10938 | |
| 10939 | if (ec_fw_string[0]) { |
| 10940 | tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL); |
| 10941 | if (!tp->ec_version_str) |
| 10942 | return -ENOMEM; |
| 10943 | |
| 10944 | t = tpacpi_parse_fw_id(ec_fw_string, |
| 10945 | &tp->ec_model, &tp->ec_release); |
| 10946 | if (t != 'H') { |
| 10947 | pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n", |
| 10948 | ec_fw_string); |
| 10949 | pr_notice("please report this to %s\n", TPACPI_MAIL); |
| 10950 | } |
| 10951 | } |
| 10952 | |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 10953 | s = dmi_get_system_info(DMI_PRODUCT_VERSION); |
Rasmus Villemoes | 40f5c77 | 2014-10-13 15:54:52 -0700 | [diff] [blame] | 10954 | if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) { |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 10955 | tp->model_str = kstrdup(s, GFP_KERNEL); |
| 10956 | if (!tp->model_str) |
| 10957 | return -ENOMEM; |
Manoj Iyer | a4f46bb | 2012-08-06 18:15:37 -0500 | [diff] [blame] | 10958 | } else { |
| 10959 | s = dmi_get_system_info(DMI_BIOS_VENDOR); |
Rasmus Villemoes | 40f5c77 | 2014-10-13 15:54:52 -0700 | [diff] [blame] | 10960 | if (s && !(strncasecmp(s, "Lenovo", 6))) { |
Manoj Iyer | a4f46bb | 2012-08-06 18:15:37 -0500 | [diff] [blame] | 10961 | tp->model_str = kstrdup(s, GFP_KERNEL); |
| 10962 | if (!tp->model_str) |
| 10963 | return -ENOMEM; |
| 10964 | } |
Henrique de Moraes Holschuh | d5a2f2f | 2007-07-18 23:45:42 -0300 | [diff] [blame] | 10965 | } |
Henrique de Moraes Holschuh | 8c74adb | 2008-04-26 01:02:19 -0300 | [diff] [blame] | 10966 | |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 10967 | s = dmi_get_system_info(DMI_PRODUCT_NAME); |
| 10968 | tp->nummodel_str = kstrdup(s, GFP_KERNEL); |
| 10969 | if (s && !tp->nummodel_str) |
| 10970 | return -ENOMEM; |
| 10971 | |
| 10972 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10973 | } |
| 10974 | |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10975 | static int __init probe_for_thinkpad(void) |
| 10976 | { |
| 10977 | int is_thinkpad; |
| 10978 | |
| 10979 | if (acpi_disabled) |
| 10980 | return -ENODEV; |
| 10981 | |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 10982 | /* It would be dangerous to run the driver in this case */ |
| 10983 | if (!tpacpi_is_ibm() && !tpacpi_is_lenovo()) |
| 10984 | return -ENODEV; |
| 10985 | |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10986 | /* |
| 10987 | * Non-ancient models have better DMI tagging, but very old models |
Henrique de Moraes Holschuh | e675aba | 2009-09-12 15:22:13 -0300 | [diff] [blame] | 10988 | * don't. tpacpi_is_fw_known() is a cheat to help in that case. |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10989 | */ |
Henrique de Moraes Holschuh | e675aba | 2009-09-12 15:22:13 -0300 | [diff] [blame] | 10990 | is_thinkpad = (thinkpad_id.model_str != NULL) || |
| 10991 | (thinkpad_id.ec_model != 0) || |
| 10992 | tpacpi_is_fw_known(); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10993 | |
Henrique de Moraes Holschuh | 437e470 | 2010-05-16 19:45:43 -0300 | [diff] [blame] | 10994 | /* The EC handler is required */ |
| 10995 | tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10996 | if (!ec_handle) { |
| 10997 | if (is_thinkpad) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 10998 | pr_err("Not yet supported ThinkPad detected!\n"); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 10999 | return -ENODEV; |
| 11000 | } |
| 11001 | |
Henrique de Moraes Holschuh | 0dcef77 | 2007-04-21 11:08:34 -0300 | [diff] [blame] | 11002 | if (!is_thinkpad && !force_load) |
| 11003 | return -ENODEV; |
| 11004 | |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 11005 | return 0; |
| 11006 | } |
| 11007 | |
Henrique de Moraes Holschuh | 7a43f78 | 2010-05-16 19:45:31 -0300 | [diff] [blame] | 11008 | static void __init thinkpad_acpi_init_banner(void) |
| 11009 | { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11010 | pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION); |
| 11011 | pr_info("%s\n", TPACPI_URL); |
Henrique de Moraes Holschuh | 7a43f78 | 2010-05-16 19:45:31 -0300 | [diff] [blame] | 11012 | |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11013 | pr_info("ThinkPad BIOS %s, EC %s\n", |
Henrique de Moraes Holschuh | 7a43f78 | 2010-05-16 19:45:31 -0300 | [diff] [blame] | 11014 | (thinkpad_id.bios_version_str) ? |
| 11015 | thinkpad_id.bios_version_str : "unknown", |
| 11016 | (thinkpad_id.ec_version_str) ? |
| 11017 | thinkpad_id.ec_version_str : "unknown"); |
| 11018 | |
| 11019 | BUG_ON(!thinkpad_id.vendor); |
| 11020 | |
| 11021 | if (thinkpad_id.model_str) |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11022 | pr_info("%s %s, model %s\n", |
Henrique de Moraes Holschuh | 7a43f78 | 2010-05-16 19:45:31 -0300 | [diff] [blame] | 11023 | (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ? |
| 11024 | "IBM" : ((thinkpad_id.vendor == |
| 11025 | PCI_VENDOR_ID_LENOVO) ? |
| 11026 | "Lenovo" : "Unknown vendor"), |
| 11027 | thinkpad_id.model_str, |
| 11028 | (thinkpad_id.nummodel_str) ? |
| 11029 | thinkpad_id.nummodel_str : "unknown"); |
| 11030 | } |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 11031 | |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 11032 | /* Module init, exit, parameters */ |
| 11033 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11034 | static struct ibm_init_struct ibms_init[] __initdata = { |
| 11035 | { |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11036 | .data = &thinkpad_acpi_driver_data, |
| 11037 | }, |
| 11038 | { |
| 11039 | .init = hotkey_init, |
| 11040 | .data = &hotkey_driver_data, |
| 11041 | }, |
| 11042 | { |
| 11043 | .init = bluetooth_init, |
| 11044 | .data = &bluetooth_driver_data, |
| 11045 | }, |
| 11046 | { |
| 11047 | .init = wan_init, |
| 11048 | .data = &wan_driver_data, |
| 11049 | }, |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 11050 | { |
| 11051 | .init = uwb_init, |
| 11052 | .data = &uwb_driver_data, |
| 11053 | }, |
Henrique de Moraes Holschuh | d7c1d17 | 2008-02-16 02:17:54 -0200 | [diff] [blame] | 11054 | #ifdef CONFIG_THINKPAD_ACPI_VIDEO |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11055 | { |
| 11056 | .init = video_init, |
Henrique de Moraes Holschuh | b525c06 | 2010-02-25 22:22:22 -0300 | [diff] [blame] | 11057 | .base_procfs_mode = S_IRUSR, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11058 | .data = &video_driver_data, |
| 11059 | }, |
Henrique de Moraes Holschuh | d7c1d17 | 2008-02-16 02:17:54 -0200 | [diff] [blame] | 11060 | #endif |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11061 | { |
Pali Rohár | bb28f3d5 | 2015-12-30 23:27:41 +0100 | [diff] [blame] | 11062 | .init = kbdlight_init, |
| 11063 | .data = &kbdlight_driver_data, |
| 11064 | }, |
| 11065 | { |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11066 | .init = light_init, |
| 11067 | .data = &light_driver_data, |
| 11068 | }, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11069 | { |
| 11070 | .init = cmos_init, |
| 11071 | .data = &cmos_driver_data, |
| 11072 | }, |
| 11073 | { |
| 11074 | .init = led_init, |
| 11075 | .data = &led_driver_data, |
| 11076 | }, |
| 11077 | { |
| 11078 | .init = beep_init, |
| 11079 | .data = &beep_driver_data, |
| 11080 | }, |
| 11081 | { |
| 11082 | .init = thermal_init, |
| 11083 | .data = &thermal_driver_data, |
| 11084 | }, |
| 11085 | { |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11086 | .init = brightness_init, |
| 11087 | .data = &brightness_driver_data, |
| 11088 | }, |
| 11089 | { |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 11090 | .init = volume_init, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11091 | .data = &volume_driver_data, |
| 11092 | }, |
| 11093 | { |
| 11094 | .init = fan_init, |
| 11095 | .data = &fan_driver_data, |
| 11096 | }, |
David Henningsson | 420f973 | 2013-10-16 23:10:31 +0200 | [diff] [blame] | 11097 | { |
| 11098 | .init = mute_led_init, |
| 11099 | .data = &mute_led_driver_data, |
| 11100 | }, |
Ognjen Galic | 2801b96 | 2018-02-07 15:58:44 +0100 | [diff] [blame] | 11101 | { |
| 11102 | .init = tpacpi_battery_init, |
| 11103 | .data = &battery_driver_data, |
| 11104 | }, |
Alexander Schremmer | 110ea1d | 2019-08-22 13:48:33 +0200 | [diff] [blame] | 11105 | { |
| 11106 | .init = tpacpi_lcdshadow_init, |
| 11107 | .data = &lcdshadow_driver_data, |
| 11108 | }, |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 11109 | { |
Mark Pearson | 1ac0965 | 2020-11-24 13:11:54 -0500 | [diff] [blame] | 11110 | .init = tpacpi_proxsensor_init, |
| 11111 | .data = &proxsensor_driver_data, |
Mark Pearson | acf7f4a | 2020-07-02 21:23:53 -0400 | [diff] [blame] | 11112 | }, |
Mark Pearson | c3bfcd4 | 2021-01-11 11:22:37 -0500 | [diff] [blame] | 11113 | { |
| 11114 | .init = tpacpi_dytc_profile_init, |
| 11115 | .data = &dytc_profile_driver_data, |
| 11116 | }, |
Nitin Joshi | d7cbe27 | 2021-01-25 11:59:16 +0900 | [diff] [blame] | 11117 | { |
| 11118 | .init = tpacpi_kbdlang_init, |
| 11119 | .data = &kbdlang_driver_data, |
| 11120 | }, |
Nitin Joshi | 3feb52a | 2021-03-17 11:46:36 +0900 | [diff] [blame] | 11121 | { |
| 11122 | .init = tpacpi_dprc_init, |
| 11123 | .data = &dprc_driver_data, |
| 11124 | }, |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11125 | }; |
| 11126 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 11127 | static int __init set_ibm_param(const char *val, const struct kernel_param *kp) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 11128 | { |
| 11129 | unsigned int i; |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11130 | struct ibm_struct *ibm; |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 11131 | |
Henrique de Moraes Holschuh | 59f91ff | 2007-11-18 09:18:29 -0200 | [diff] [blame] | 11132 | if (!kp || !kp->name || !val) |
| 11133 | return -EINVAL; |
| 11134 | |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11135 | for (i = 0; i < ARRAY_SIZE(ibms_init); i++) { |
| 11136 | ibm = ibms_init[i].data; |
Henrique de Moraes Holschuh | 59f91ff | 2007-11-18 09:18:29 -0200 | [diff] [blame] | 11137 | WARN_ON(ibm == NULL); |
| 11138 | |
| 11139 | if (!ibm || !ibm->name) |
| 11140 | continue; |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11141 | |
| 11142 | if (strcmp(ibm->name, kp->name) == 0 && ibm->write) { |
Andy Shevchenko | be51bd4 | 2020-05-11 15:56:24 +0300 | [diff] [blame] | 11143 | if (strlen(val) > sizeof(ibms_init[i].param) - 1) |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 11144 | return -ENOSPC; |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11145 | strcpy(ibms_init[i].param, val); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 11146 | return 0; |
| 11147 | } |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11148 | } |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 11149 | |
| 11150 | return -EINVAL; |
| 11151 | } |
| 11152 | |
Henrique de Moraes Holschuh | b09c722 | 2009-12-09 01:36:27 +0000 | [diff] [blame] | 11153 | module_param(experimental, int, 0444); |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11154 | MODULE_PARM_DESC(experimental, |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 11155 | "Enables experimental features when non-zero"); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 11156 | |
Henrique de Moraes Holschuh | 132ce09 | 2007-04-21 11:08:30 -0300 | [diff] [blame] | 11157 | module_param_named(debug, dbg_level, uint, 0); |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11158 | MODULE_PARM_DESC(debug, "Sets debug level bit-mask"); |
Henrique de Moraes Holschuh | 132ce09 | 2007-04-21 11:08:30 -0300 | [diff] [blame] | 11159 | |
Henrique de Moraes Holschuh | b09c722 | 2009-12-09 01:36:27 +0000 | [diff] [blame] | 11160 | module_param(force_load, bool, 0444); |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11161 | MODULE_PARM_DESC(force_load, |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 11162 | "Attempts to load the driver even on a mis-identified ThinkPad when true"); |
Henrique de Moraes Holschuh | 0dcef77 | 2007-04-21 11:08:34 -0300 | [diff] [blame] | 11163 | |
Henrique de Moraes Holschuh | b09c722 | 2009-12-09 01:36:27 +0000 | [diff] [blame] | 11164 | module_param_named(fan_control, fan_control_allowed, bool, 0444); |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11165 | MODULE_PARM_DESC(fan_control, |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 11166 | "Enables setting fan parameters features when true"); |
Henrique de Moraes Holschuh | ecf2a80 | 2007-04-27 22:00:09 -0300 | [diff] [blame] | 11167 | |
Henrique de Moraes Holschuh | b09c722 | 2009-12-09 01:36:27 +0000 | [diff] [blame] | 11168 | module_param_named(brightness_mode, brightness_mode, uint, 0444); |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11169 | MODULE_PARM_DESC(brightness_mode, |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 11170 | "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM"); |
Henrique de Moraes Holschuh | 24d3b77 | 2007-07-18 23:45:43 -0300 | [diff] [blame] | 11171 | |
Henrique de Moraes Holschuh | b09c722 | 2009-12-09 01:36:27 +0000 | [diff] [blame] | 11172 | module_param(brightness_enable, uint, 0444); |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11173 | MODULE_PARM_DESC(brightness_enable, |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 11174 | "Enables backlight control when 1, disables when 0"); |
Henrique de Moraes Holschuh | 87cc537 | 2007-10-30 18:02:07 -0200 | [diff] [blame] | 11175 | |
Henrique de Moraes Holschuh | ff850c3 | 2009-12-26 22:52:15 -0200 | [diff] [blame] | 11176 | #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 11177 | module_param_named(volume_mode, volume_mode, uint, 0444); |
| 11178 | MODULE_PARM_DESC(volume_mode, |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 11179 | "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM"); |
Henrique de Moraes Holschuh | 329e4e1 | 2009-12-15 21:51:08 -0200 | [diff] [blame] | 11180 | |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 11181 | module_param_named(volume_capabilities, volume_capabilities, uint, 0444); |
| 11182 | MODULE_PARM_DESC(volume_capabilities, |
Colin Ian King | d33a7e5 | 2019-04-15 13:52:04 +0100 | [diff] [blame] | 11183 | "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only"); |
Henrique de Moraes Holschuh | a112cee | 2009-12-15 21:51:09 -0200 | [diff] [blame] | 11184 | |
Henrique de Moraes Holschuh | c7ac629 | 2009-12-15 21:51:10 -0200 | [diff] [blame] | 11185 | module_param_named(volume_control, volume_control_allowed, bool, 0444); |
| 11186 | MODULE_PARM_DESC(volume_control, |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 11187 | "Enables software override for the console audio control when true"); |
Henrique de Moraes Holschuh | c7ac629 | 2009-12-15 21:51:10 -0200 | [diff] [blame] | 11188 | |
Andy Lutomirski | 9a417ec | 2014-10-17 17:04:29 -0700 | [diff] [blame] | 11189 | module_param_named(software_mute, software_mute_requested, bool, 0444); |
| 11190 | MODULE_PARM_DESC(software_mute, |
| 11191 | "Request full software mute control"); |
| 11192 | |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 11193 | /* ALSA module API parameters */ |
| 11194 | module_param_named(index, alsa_index, int, 0444); |
| 11195 | MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer"); |
| 11196 | module_param_named(id, alsa_id, charp, 0444); |
| 11197 | MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer"); |
| 11198 | module_param_named(enable, alsa_enable, bool, 0444); |
| 11199 | MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer"); |
Henrique de Moraes Holschuh | ff850c3 | 2009-12-26 22:52:15 -0200 | [diff] [blame] | 11200 | #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */ |
Henrique de Moraes Holschuh | 0d204c3 | 2009-12-15 21:51:11 -0200 | [diff] [blame] | 11201 | |
Andy Shevchenko | 9b4bbbd | 2017-05-09 17:17:21 +0300 | [diff] [blame] | 11202 | /* The module parameter can't be read back, that's why 0 is used here */ |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 11203 | #define TPACPI_PARAM(feature) \ |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11204 | module_param_call(feature, set_ibm_param, NULL, NULL, 0); \ |
Andy Shevchenko | 5cac62a | 2017-05-09 17:17:20 +0300 | [diff] [blame] | 11205 | MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation") |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 11206 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 11207 | TPACPI_PARAM(hotkey); |
| 11208 | TPACPI_PARAM(bluetooth); |
| 11209 | TPACPI_PARAM(video); |
| 11210 | TPACPI_PARAM(light); |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 11211 | TPACPI_PARAM(cmos); |
| 11212 | TPACPI_PARAM(led); |
| 11213 | TPACPI_PARAM(beep); |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 11214 | TPACPI_PARAM(brightness); |
| 11215 | TPACPI_PARAM(volume); |
| 11216 | TPACPI_PARAM(fan); |
Henrique de Moraes Holschuh | 56b6aeb | 2007-03-23 17:33:57 -0300 | [diff] [blame] | 11217 | |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 11218 | #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES |
Henrique de Moraes Holschuh | b09c722 | 2009-12-09 01:36:27 +0000 | [diff] [blame] | 11219 | module_param(dbg_wlswemul, uint, 0444); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 11220 | MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation"); |
| 11221 | module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0); |
| 11222 | MODULE_PARM_DESC(wlsw_state, |
| 11223 | "Initial state of the emulated WLSW switch"); |
| 11224 | |
Henrique de Moraes Holschuh | b09c722 | 2009-12-09 01:36:27 +0000 | [diff] [blame] | 11225 | module_param(dbg_bluetoothemul, uint, 0444); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 11226 | MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation"); |
| 11227 | module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0); |
| 11228 | MODULE_PARM_DESC(bluetooth_state, |
| 11229 | "Initial state of the emulated bluetooth switch"); |
| 11230 | |
Henrique de Moraes Holschuh | b09c722 | 2009-12-09 01:36:27 +0000 | [diff] [blame] | 11231 | module_param(dbg_wwanemul, uint, 0444); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 11232 | MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation"); |
| 11233 | module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0); |
| 11234 | MODULE_PARM_DESC(wwan_state, |
| 11235 | "Initial state of the emulated WWAN switch"); |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 11236 | |
Henrique de Moraes Holschuh | b09c722 | 2009-12-09 01:36:27 +0000 | [diff] [blame] | 11237 | module_param(dbg_uwbemul, uint, 0444); |
Henrique de Moraes Holschuh | 0045c0a | 2009-01-11 03:01:03 -0200 | [diff] [blame] | 11238 | MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation"); |
| 11239 | module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0); |
| 11240 | MODULE_PARM_DESC(uwb_state, |
| 11241 | "Initial state of the emulated UWB switch"); |
Henrique de Moraes Holschuh | a73f309 | 2009-01-11 03:01:00 -0200 | [diff] [blame] | 11242 | #endif |
| 11243 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 11244 | static void thinkpad_acpi_module_exit(void) |
| 11245 | { |
| 11246 | struct ibm_struct *ibm, *itmp; |
| 11247 | |
| 11248 | tpacpi_lifecycle = TPACPI_LIFE_EXITING; |
| 11249 | |
| 11250 | list_for_each_entry_safe_reverse(ibm, itmp, |
| 11251 | &tpacpi_all_drivers, |
| 11252 | all_drivers) { |
| 11253 | ibm_exit(ibm); |
| 11254 | } |
| 11255 | |
| 11256 | dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n"); |
| 11257 | |
| 11258 | if (tpacpi_inputdev) { |
| 11259 | if (tp_features.input_device_registered) |
| 11260 | input_unregister_device(tpacpi_inputdev); |
| 11261 | else |
| 11262 | input_free_device(tpacpi_inputdev); |
Li Dongyang | 00d3959 | 2012-07-25 10:45:09 +1000 | [diff] [blame] | 11263 | kfree(hotkey_keycode_map); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 11264 | } |
| 11265 | |
| 11266 | if (tpacpi_hwmon) |
| 11267 | hwmon_device_unregister(tpacpi_hwmon); |
| 11268 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 11269 | if (tpacpi_sensors_pdev) |
| 11270 | platform_device_unregister(tpacpi_sensors_pdev); |
| 11271 | if (tpacpi_pdev) |
| 11272 | platform_device_unregister(tpacpi_pdev); |
| 11273 | |
| 11274 | if (tp_features.sensors_pdrv_attrs_registered) |
| 11275 | tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver); |
| 11276 | if (tp_features.platform_drv_attrs_registered) |
| 11277 | tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver); |
| 11278 | |
| 11279 | if (tp_features.sensors_pdrv_registered) |
| 11280 | platform_driver_unregister(&tpacpi_hwmon_pdriver); |
| 11281 | |
| 11282 | if (tp_features.platform_drv_registered) |
| 11283 | platform_driver_unregister(&tpacpi_pdriver); |
| 11284 | |
| 11285 | if (proc_dir) |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 11286 | remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 11287 | |
Henrique de Moraes Holschuh | e0e3c06 | 2008-04-26 01:02:28 -0300 | [diff] [blame] | 11288 | if (tpacpi_wq) |
| 11289 | destroy_workqueue(tpacpi_wq); |
| 11290 | |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 11291 | kfree(thinkpad_id.bios_version_str); |
| 11292 | kfree(thinkpad_id.ec_version_str); |
| 11293 | kfree(thinkpad_id.model_str); |
Li Dongyang | d2be15b | 2012-07-25 10:45:08 +1000 | [diff] [blame] | 11294 | kfree(thinkpad_id.nummodel_str); |
Henrique de Moraes Holschuh | b21a15f | 2008-01-08 13:02:45 -0200 | [diff] [blame] | 11295 | } |
| 11296 | |
Henrique de Moraes Holschuh | f74a27d | 2008-01-08 13:02:44 -0200 | [diff] [blame] | 11297 | |
Henrique de Moraes Holschuh | 1def711 | 2007-04-21 11:08:27 -0300 | [diff] [blame] | 11298 | static int __init thinkpad_acpi_module_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11299 | { |
| 11300 | int ret, i; |
| 11301 | |
Henrique de Moraes Holschuh | 8fef502 | 2007-09-23 11:39:02 -0300 | [diff] [blame] | 11302 | tpacpi_lifecycle = TPACPI_LIFE_INIT; |
| 11303 | |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 11304 | /* Driver-level probe */ |
Henrique de Moraes Holschuh | d5a2f2f | 2007-07-18 23:45:42 -0300 | [diff] [blame] | 11305 | |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 11306 | ret = get_thinkpad_model_data(&thinkpad_id); |
| 11307 | if (ret) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11308 | pr_err("unable to get DMI data: %d\n", ret); |
Henrique de Moraes Holschuh | bf20e74 | 2008-07-21 09:15:51 -0300 | [diff] [blame] | 11309 | thinkpad_acpi_module_exit(); |
| 11310 | return ret; |
| 11311 | } |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 11312 | ret = probe_for_thinkpad(); |
Henrique de Moraes Holschuh | d5a2f2f | 2007-07-18 23:45:42 -0300 | [diff] [blame] | 11313 | if (ret) { |
| 11314 | thinkpad_acpi_module_exit(); |
Henrique de Moraes Holschuh | 5fba344 | 2007-04-21 11:08:31 -0300 | [diff] [blame] | 11315 | return ret; |
Henrique de Moraes Holschuh | d5a2f2f | 2007-07-18 23:45:42 -0300 | [diff] [blame] | 11316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11317 | |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 11318 | /* Driver initialization */ |
Henrique de Moraes Holschuh | d5a2f2f | 2007-07-18 23:45:42 -0300 | [diff] [blame] | 11319 | |
Henrique de Moraes Holschuh | 7a43f78 | 2010-05-16 19:45:31 -0300 | [diff] [blame] | 11320 | thinkpad_acpi_init_banner(); |
| 11321 | tpacpi_check_outdated_fw(); |
| 11322 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 11323 | TPACPI_ACPIHANDLE_INIT(ecrd); |
| 11324 | TPACPI_ACPIHANDLE_INIT(ecwr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11325 | |
Henrique de Moraes Holschuh | e0e3c06 | 2008-04-26 01:02:28 -0300 | [diff] [blame] | 11326 | tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME); |
| 11327 | if (!tpacpi_wq) { |
| 11328 | thinkpad_acpi_module_exit(); |
| 11329 | return -ENOMEM; |
| 11330 | } |
| 11331 | |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 11332 | proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11333 | if (!proc_dir) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11334 | pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n"); |
Henrique de Moraes Holschuh | 1def711 | 2007-04-21 11:08:27 -0300 | [diff] [blame] | 11335 | thinkpad_acpi_module_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11336 | return -ENODEV; |
| 11337 | } |
Borislav Deianov | 78f81cc | 2005-08-17 00:00:00 -0400 | [diff] [blame] | 11338 | |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 11339 | ret = platform_driver_register(&tpacpi_pdriver); |
| 11340 | if (ret) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11341 | pr_err("unable to register main platform driver\n"); |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 11342 | thinkpad_acpi_module_exit(); |
| 11343 | return ret; |
| 11344 | } |
Henrique de Moraes Holschuh | ac36393 | 2007-07-27 17:04:40 -0300 | [diff] [blame] | 11345 | tp_features.platform_drv_registered = 1; |
| 11346 | |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 11347 | ret = platform_driver_register(&tpacpi_hwmon_pdriver); |
| 11348 | if (ret) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11349 | pr_err("unable to register hwmon platform driver\n"); |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 11350 | thinkpad_acpi_module_exit(); |
| 11351 | return ret; |
| 11352 | } |
| 11353 | tp_features.sensors_pdrv_registered = 1; |
| 11354 | |
Henrique de Moraes Holschuh | 176750d | 2007-04-24 11:48:13 -0300 | [diff] [blame] | 11355 | ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver); |
Henrique de Moraes Holschuh | 2369cc9 | 2007-09-23 11:39:07 -0300 | [diff] [blame] | 11356 | if (!ret) { |
| 11357 | tp_features.platform_drv_attrs_registered = 1; |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 11358 | ret = tpacpi_create_driver_attributes( |
| 11359 | &tpacpi_hwmon_pdriver.driver); |
Henrique de Moraes Holschuh | 2369cc9 | 2007-09-23 11:39:07 -0300 | [diff] [blame] | 11360 | } |
Henrique de Moraes Holschuh | 176750d | 2007-04-24 11:48:13 -0300 | [diff] [blame] | 11361 | if (ret) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11362 | pr_err("unable to create sysfs driver attributes\n"); |
Henrique de Moraes Holschuh | 176750d | 2007-04-24 11:48:13 -0300 | [diff] [blame] | 11363 | thinkpad_acpi_module_exit(); |
| 11364 | return ret; |
| 11365 | } |
Henrique de Moraes Holschuh | 2369cc9 | 2007-09-23 11:39:07 -0300 | [diff] [blame] | 11366 | tp_features.sensors_pdrv_attrs_registered = 1; |
Henrique de Moraes Holschuh | 176750d | 2007-04-24 11:48:13 -0300 | [diff] [blame] | 11367 | |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 11368 | |
| 11369 | /* Device initialization */ |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 11370 | tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1, |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 11371 | NULL, 0); |
| 11372 | if (IS_ERR(tpacpi_pdev)) { |
| 11373 | ret = PTR_ERR(tpacpi_pdev); |
| 11374 | tpacpi_pdev = NULL; |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11375 | pr_err("unable to register platform device\n"); |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 11376 | thinkpad_acpi_module_exit(); |
| 11377 | return ret; |
| 11378 | } |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 11379 | tpacpi_sensors_pdev = platform_device_register_simple( |
Henrique de Moraes Holschuh | 35ff8b9 | 2008-01-08 13:02:49 -0200 | [diff] [blame] | 11380 | TPACPI_HWMON_DRVR_NAME, |
| 11381 | -1, NULL, 0); |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 11382 | if (IS_ERR(tpacpi_sensors_pdev)) { |
| 11383 | ret = PTR_ERR(tpacpi_sensors_pdev); |
| 11384 | tpacpi_sensors_pdev = NULL; |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11385 | pr_err("unable to register hwmon platform device\n"); |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 11386 | thinkpad_acpi_module_exit(); |
| 11387 | return ret; |
| 11388 | } |
Henrique de Moraes Holschuh | 7fd4002 | 2007-09-25 06:38:03 -0300 | [diff] [blame] | 11389 | tp_features.sensors_pdev_attrs_registered = 1; |
Stanislav Fomichev | 6b99e35 | 2017-06-20 20:45:13 -0700 | [diff] [blame] | 11390 | tpacpi_hwmon = hwmon_device_register_with_groups( |
| 11391 | &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL); |
| 11392 | |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 11393 | if (IS_ERR(tpacpi_hwmon)) { |
| 11394 | ret = PTR_ERR(tpacpi_hwmon); |
| 11395 | tpacpi_hwmon = NULL; |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11396 | pr_err("unable to register hwmon device\n"); |
Henrique de Moraes Holschuh | 54ae150 | 2007-04-24 11:48:12 -0300 | [diff] [blame] | 11397 | thinkpad_acpi_module_exit(); |
| 11398 | return ret; |
| 11399 | } |
Henrique de Moraes Holschuh | 8523ed6 | 2007-09-23 11:39:01 -0300 | [diff] [blame] | 11400 | mutex_init(&tpacpi_inputdev_send_mutex); |
Henrique de Moraes Holschuh | 7f5d1cd | 2007-07-18 23:45:34 -0300 | [diff] [blame] | 11401 | tpacpi_inputdev = input_allocate_device(); |
| 11402 | if (!tpacpi_inputdev) { |
Henrique de Moraes Holschuh | 7f5d1cd | 2007-07-18 23:45:34 -0300 | [diff] [blame] | 11403 | thinkpad_acpi_module_exit(); |
| 11404 | return -ENOMEM; |
| 11405 | } else { |
| 11406 | /* Prepare input device, but don't register */ |
| 11407 | tpacpi_inputdev->name = "ThinkPad Extra Buttons"; |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 11408 | tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0"; |
Henrique de Moraes Holschuh | 7f5d1cd | 2007-07-18 23:45:34 -0300 | [diff] [blame] | 11409 | tpacpi_inputdev->id.bustype = BUS_HOST; |
Henrique de Moraes Holschuh | e28393c | 2010-05-16 19:45:23 -0300 | [diff] [blame] | 11410 | tpacpi_inputdev->id.vendor = thinkpad_id.vendor; |
Henrique de Moraes Holschuh | 7f5d1cd | 2007-07-18 23:45:34 -0300 | [diff] [blame] | 11411 | tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT; |
| 11412 | tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION; |
Henrique de Moraes Holschuh | d112ef9 | 2009-12-09 01:36:26 +0000 | [diff] [blame] | 11413 | tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev; |
Henrique de Moraes Holschuh | 7f5d1cd | 2007-07-18 23:45:34 -0300 | [diff] [blame] | 11414 | } |
Henrique de Moraes Holschuh | 7777583 | 2010-05-16 19:45:33 -0300 | [diff] [blame] | 11415 | |
| 11416 | /* Init subdriver dependencies */ |
| 11417 | tpacpi_detect_brightness_capabilities(); |
| 11418 | |
| 11419 | /* Init subdrivers */ |
Henrique de Moraes Holschuh | a5763f2 | 2007-04-21 11:08:33 -0300 | [diff] [blame] | 11420 | for (i = 0; i < ARRAY_SIZE(ibms_init); i++) { |
| 11421 | ret = ibm_init(&ibms_init[i]); |
| 11422 | if (ret >= 0 && *ibms_init[i].param) |
| 11423 | ret = ibms_init[i].data->write(ibms_init[i].param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11424 | if (ret < 0) { |
Henrique de Moraes Holschuh | 1def711 | 2007-04-21 11:08:27 -0300 | [diff] [blame] | 11425 | thinkpad_acpi_module_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11426 | return ret; |
| 11427 | } |
| 11428 | } |
Henrique de Moraes Holschuh | b589ea4 | 2010-02-25 21:28:58 -0300 | [diff] [blame] | 11429 | |
| 11430 | tpacpi_lifecycle = TPACPI_LIFE_RUNNING; |
| 11431 | |
Henrique de Moraes Holschuh | 7f5d1cd | 2007-07-18 23:45:34 -0300 | [diff] [blame] | 11432 | ret = input_register_device(tpacpi_inputdev); |
| 11433 | if (ret < 0) { |
Joe Perches | 0978e01 | 2011-04-04 10:06:25 -0700 | [diff] [blame] | 11434 | pr_err("unable to register input device\n"); |
Henrique de Moraes Holschuh | 7f5d1cd | 2007-07-18 23:45:34 -0300 | [diff] [blame] | 11435 | thinkpad_acpi_module_exit(); |
| 11436 | return ret; |
| 11437 | } else { |
| 11438 | tp_features.input_device_registered = 1; |
| 11439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11440 | |
| 11441 | return 0; |
| 11442 | } |
| 11443 | |
Henrique de Moraes Holschuh | 95e57ab | 2008-04-26 01:02:22 -0300 | [diff] [blame] | 11444 | MODULE_ALIAS(TPACPI_DRVR_SHORTNAME); |
| 11445 | |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11446 | /* |
Henrique de Moraes Holschuh | 922fe09 | 2009-04-14 02:44:13 +0000 | [diff] [blame] | 11447 | * This will autoload the driver in almost every ThinkPad |
| 11448 | * in widespread use. |
| 11449 | * |
| 11450 | * Only _VERY_ old models, like the 240, 240x and 570 lack |
| 11451 | * the HKEY event interface. |
| 11452 | */ |
| 11453 | MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids); |
| 11454 | |
| 11455 | /* |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11456 | * DMI matching for module autoloading |
| 11457 | * |
Alexander A. Klimov | 19206a1 | 2020-07-13 21:06:28 +0200 | [diff] [blame] | 11458 | * See https://thinkwiki.org/wiki/List_of_DMI_IDs |
| 11459 | * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11460 | * |
| 11461 | * Only models listed in thinkwiki will be supported, so add yours |
| 11462 | * if it is not there yet. |
| 11463 | */ |
| 11464 | #define IBM_BIOS_MODULE_ALIAS(__type) \ |
Mathieu Chouquet-Stringer | b36a50f | 2009-03-14 16:35:26 +0100 | [diff] [blame] | 11465 | MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*") |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11466 | |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11467 | /* Ancient thinkpad BIOSes have to be identified by |
| 11468 | * BIOS type or model number, and there are far less |
| 11469 | * BIOS types than model numbers... */ |
Henrique de Moraes Holschuh | 922fe09 | 2009-04-14 02:44:13 +0000 | [diff] [blame] | 11470 | IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */ |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11471 | |
Henrique de Moraes Holschuh | f68f53a | 2009-04-14 02:44:12 +0000 | [diff] [blame] | 11472 | MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>"); |
| 11473 | MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>"); |
Henrique de Moraes Holschuh | e0c7dfe | 2008-01-08 13:02:48 -0200 | [diff] [blame] | 11474 | MODULE_DESCRIPTION(TPACPI_DESC); |
| 11475 | MODULE_VERSION(TPACPI_VERSION); |
Henrique de Moraes Holschuh | f68080f | 2008-01-08 13:02:47 -0200 | [diff] [blame] | 11476 | MODULE_LICENSE("GPL"); |
| 11477 | |
Henrique de Moraes Holschuh | 1def711 | 2007-04-21 11:08:27 -0300 | [diff] [blame] | 11478 | module_init(thinkpad_acpi_module_init); |
| 11479 | module_exit(thinkpad_acpi_module_exit); |