blob: 91fab1a13a6d9d62fb4bb5f712d24d0bcaadd06d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
David Henningsson420f9732013-10-16 23:10:31 +020026#define TPACPI_VERSION "0.25"
Stanislav Fomichev6b99e352017-06-20 20:45:13 -070027#define TPACPI_SYSFS_VERSION 0x030000
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064#include <linux/nvram.h>
65#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020066#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020067#include <linux/sysfs.h>
68#include <linux/backlight.h>
69#include <linux/fb.h>
70#include <linux/platform_device.h>
71#include <linux/hwmon.h>
72#include <linux/hwmon-sysfs.h>
73#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030074#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030075#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020076#include <linux/dmi.h>
77#include <linux/jiffies.h>
78#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080079#include <linux/acpi.h>
80#include <linux/pci_ids.h>
81#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020082#include <sound/core.h>
83#include <sound/control.h>
84#include <sound/initval.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080085#include <linux/uaccess.h>
Hans de Goedeb33c6ce2015-06-16 16:28:10 +020086#include <acpi/video.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087
88/* ThinkPad CMOS commands */
89#define TP_CMOS_VOLUME_DOWN 0
90#define TP_CMOS_VOLUME_UP 1
91#define TP_CMOS_VOLUME_MUTE 2
92#define TP_CMOS_BRIGHTNESS_UP 4
93#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030094#define TP_CMOS_THINKLIGHT_ON 12
95#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020096
97/* NVRAM Addresses */
98enum tp_nvram_addr {
99 TP_NVRAM_ADDR_HK2 = 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
101 TP_NVRAM_ADDR_VIDEO = 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
103 TP_NVRAM_ADDR_MIXER = 0x60,
104};
105
106/* NVRAM bit masks */
107enum {
108 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
112 TP_NVRAM_MASK_THINKLIGHT = 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
117 TP_NVRAM_MASK_MUTE = 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME = 0,
121};
122
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300123/* Misc NVRAM-related */
124enum {
125 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
126};
127
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200128/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400129#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Hui Wanga3c42a42016-11-08 16:13:23 +0800131#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300132#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200133
134/* Input IDs */
135#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136#define TPACPI_HKEY_INPUT_VERSION 0x4101
137
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200138/* ACPI \WGSV commands */
139enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
144};
145
146/* TP_ACPI_WGSV_GET_STATE bits */
147enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
158};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200159
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300160/* HKEY events */
161enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
Hans de Goedec685e202017-02-09 16:44:13 +0100166 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300167 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
168 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
169 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170
171 /* Reasons for waking up from S3/S4 */
172 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
173 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
174 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
175 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
176 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
177 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178
179 /* Auto-sleep after eject request */
180 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
181 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182
183 /* Misc bay events */
184 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300185 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
186 or port replicator */
187 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
188 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300189
190 /* User-interface events */
191 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
192 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
193 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
194 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Lyudeb03f4d42016-11-11 15:15:03 -0500195 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
196 * enter/leave tablet mode
197 */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300198 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
201
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100205 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300206
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300207 /* Thermal events */
208 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
209 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
210 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
211 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
212 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
213
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100214 /* AC-related events */
215 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300216
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300217 /* Misc */
218 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
219};
220
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200221/****************************************************************************
222 * Main driver
223 */
224
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200225#define TPACPI_NAME "thinkpad"
226#define TPACPI_DESC "ThinkPad ACPI Extras"
227#define TPACPI_FILE TPACPI_NAME "_acpi"
228#define TPACPI_URL "http://ibm-acpi.sf.net/"
229#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200230
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200231#define TPACPI_PROC_DIR "ibm"
232#define TPACPI_ACPI_EVENT_PREFIX "ibm"
233#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300234#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200235#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200236
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300237#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300238#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300239
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200240#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200241
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000242/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200243#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000244#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200245#define TPACPI_DBG_INIT 0x0001
246#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000247#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000248#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000249#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000250#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200251#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200253#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
254#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
255#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200257
258/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200259 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200260 */
261
262struct ibm_struct;
263
264struct tp_acpi_drv_struct {
265 const struct acpi_device_id *hid;
266 struct acpi_driver *driver;
267
268 void (*notify) (struct ibm_struct *, u32);
269 acpi_handle *handle;
270 u32 type;
271 struct acpi_device *device;
272};
273
274struct ibm_struct {
275 char *name;
276
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200277 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200278 int (*write) (char *);
279 void (*exit) (void);
280 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200281 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200282 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200283
284 struct list_head all_drivers;
285
286 struct tp_acpi_drv_struct *acpi;
287
288 struct {
289 u8 acpi_driver_registered:1;
290 u8 acpi_notify_installed:1;
291 u8 proc_created:1;
292 u8 init_called:1;
293 u8 experimental:1;
294 } flags;
295};
296
297struct ibm_init_struct {
298 char param[32];
299
300 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400301 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200302 struct ibm_struct *data;
303};
304
305static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200306 u32 bluetooth:1;
307 u32 hotkey:1;
308 u32 hotkey_mask:1;
309 u32 hotkey_wlsw:1;
Lyudeb3180022016-11-11 15:15:02 -0500310 enum {
311 TP_HOTKEY_TABLET_NONE = 0,
312 TP_HOTKEY_TABLET_USES_MHKG,
Benjamin Bergdda3ec02017-09-15 15:20:49 +0200313 TP_HOTKEY_TABLET_USES_GMMS,
Lyudeb3180022016-11-11 15:15:02 -0500314 } hotkey_tablet;
Pali Rohárbb28f3d52015-12-30 23:27:41 +0100315 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200316 u32 light:1;
317 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300318 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300319 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200320 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200321 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200322 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300323 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300324 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200325 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200326 u32 input_device_registered:1;
327 u32 platform_drv_registered:1;
328 u32 platform_drv_attrs_registered:1;
329 u32 sensors_pdrv_registered:1;
330 u32 sensors_pdrv_attrs_registered:1;
331 u32 sensors_pdev_attrs_registered:1;
332 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100333 u32 has_adaptive_kbd:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200334} tp_features;
335
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300336static struct {
337 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200338 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300339} tp_warned;
340
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200341struct thinkpad_id_data {
342 unsigned int vendor; /* ThinkPad vendor:
343 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
344
345 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
346 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
347
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300348 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200349 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300350 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
351 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200352
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300353 char *model_str; /* ThinkPad T43 */
354 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200355};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200356static struct thinkpad_id_data thinkpad_id;
357
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300358static enum {
359 TPACPI_LIFE_INIT = 0,
360 TPACPI_LIFE_RUNNING,
361 TPACPI_LIFE_EXITING,
362} tpacpi_lifecycle;
363
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200364static int experimental;
365static u32 dbg_level;
366
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300367static struct workqueue_struct *tpacpi_wq;
368
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000369enum led_status_t {
370 TPACPI_LED_OFF = 0,
371 TPACPI_LED_ON,
372 TPACPI_LED_BLINK,
373};
374
Hans de Goede86ec0c22017-02-09 16:44:12 +0100375/* tpacpi LED class */
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300376struct tpacpi_led_classdev {
377 struct led_classdev led_classdev;
Adam Leeedf2d772013-06-08 16:51:15 +0800378 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300379};
380
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300381/* brightness level capabilities */
382static unsigned int bright_maxlvl; /* 0 = unknown */
383
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200384#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
385static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030386static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200387static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030388static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200389static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030390static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200391static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030392static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200393#endif
394
395
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000396/*************************************************************************
397 * Debugging helpers
398 */
399
Joe Perches0978e012011-04-04 10:06:25 -0700400#define dbg_printk(a_dbg_level, format, arg...) \
401do { \
402 if (dbg_level & (a_dbg_level)) \
403 printk(KERN_DEBUG pr_fmt("%s: " format), \
404 __func__, ##arg); \
405} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000406
407#ifdef CONFIG_THINKPAD_ACPI_DEBUG
408#define vdbg_printk dbg_printk
409static const char *str_supported(int is_supported);
410#else
Joe Perches0978e012011-04-04 10:06:25 -0700411static inline const char *str_supported(int is_supported) { return ""; }
412#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700413 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000414#endif
415
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000416static void tpacpi_log_usertask(const char * const what)
417{
Joe Perches0978e012011-04-04 10:06:25 -0700418 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
419 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000420}
421
Joe Perches0978e012011-04-04 10:06:25 -0700422#define tpacpi_disclose_usertask(what, format, arg...) \
423do { \
424 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
425 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
426 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
427 what, task_tgid_vnr(current), ## arg); \
428 } \
429} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000430
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300431/*
432 * Quirk handling helpers
433 *
434 * ThinkPad IDs and versions seen in the field so far
435 * are two-characters from the set [0-9A-Z], i.e. base 36.
436 *
437 * We use values well outside that range as specials.
438 */
439
440#define TPACPI_MATCH_ANY 0xffffU
441#define TPACPI_MATCH_UNKNOWN 0U
442
443/* TPID('1', 'Y') == 0x5931 */
444#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
445
446#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
447 { .vendor = PCI_VENDOR_ID_IBM, \
448 .bios = TPID(__id1, __id2), \
449 .ec = TPACPI_MATCH_ANY, \
450 .quirks = (__quirk) }
451
452#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
453 { .vendor = PCI_VENDOR_ID_LENOVO, \
454 .bios = TPID(__id1, __id2), \
455 .ec = TPACPI_MATCH_ANY, \
456 .quirks = (__quirk) }
457
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200458#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
459 { .vendor = PCI_VENDOR_ID_LENOVO, \
460 .bios = TPACPI_MATCH_ANY, \
461 .ec = TPID(__id1, __id2), \
462 .quirks = (__quirk) }
463
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300464struct tpacpi_quirk {
465 unsigned int vendor;
466 u16 bios;
467 u16 ec;
468 unsigned long quirks;
469};
470
471/**
472 * tpacpi_check_quirks() - search BIOS/EC version on a list
473 * @qlist: array of &struct tpacpi_quirk
474 * @qlist_size: number of elements in @qlist
475 *
476 * Iterates over a quirks list until one is found that matches the
477 * ThinkPad's vendor, BIOS and EC model.
478 *
479 * Returns 0 if nothing matches, otherwise returns the quirks field of
480 * the matching &struct tpacpi_quirk entry.
481 *
482 * The match criteria is: vendor, ec and bios much match.
483 */
484static unsigned long __init tpacpi_check_quirks(
485 const struct tpacpi_quirk *qlist,
486 unsigned int qlist_size)
487{
488 while (qlist_size) {
489 if ((qlist->vendor == thinkpad_id.vendor ||
490 qlist->vendor == TPACPI_MATCH_ANY) &&
491 (qlist->bios == thinkpad_id.bios_model ||
492 qlist->bios == TPACPI_MATCH_ANY) &&
493 (qlist->ec == thinkpad_id.ec_model ||
494 qlist->ec == TPACPI_MATCH_ANY))
495 return qlist->quirks;
496
497 qlist_size--;
498 qlist++;
499 }
500 return 0;
501}
502
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300503static inline bool __pure __init tpacpi_is_lenovo(void)
504{
505 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
506}
507
508static inline bool __pure __init tpacpi_is_ibm(void)
509{
510 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
511}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300512
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300513/****************************************************************************
514 ****************************************************************************
515 *
516 * ACPI Helpers and device model
517 *
518 ****************************************************************************
519 ****************************************************************************/
520
521/*************************************************************************
522 * ACPI basic handles
523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300525static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300526static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200528#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700530 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300531 &parent##_handle; \
532 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200534TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
535TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200537TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
538 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400539 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
540 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300541 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400542
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200543TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400544 "^HKEY", /* R30, R31 */
545 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300546 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400547
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300548/*************************************************************************
549 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200550 */
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700553 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400556 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200557 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400558 struct acpi_buffer result, *resultp;
559 union acpi_object out_obj;
560 acpi_status status;
561 va_list ap;
562 char res_type;
563 int success;
564 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700567 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
569 }
570
571 if (*fmt == 'q') {
572 quiet = 1;
573 fmt++;
574 } else
575 quiet = 0;
576
577 res_type = *(fmt++);
578
579 params.count = 0;
580 params.pointer = &in_objs[0];
581
582 va_start(ap, fmt);
583 while (*fmt) {
584 char c = *(fmt++);
585 switch (c) {
586 case 'd': /* int */
587 in_objs[params.count].integer.value = va_arg(ap, int);
588 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
589 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400590 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300592 pr_err("acpi_evalf() called with invalid format character '%c'\n",
593 c);
Jesper Juhl21365852010-12-24 19:56:28 +0100594 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 0;
596 }
597 }
598 va_end(ap);
599
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400600 if (res_type != 'v') {
601 result.length = sizeof(out_obj);
602 result.pointer = &out_obj;
603 resultp = &result;
604 } else
605 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400607 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400610 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300611 success = (status == AE_OK &&
612 out_obj.type == ACPI_TYPE_INTEGER);
613 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700614 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400616 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 success = status == AE_OK;
618 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400619 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300621 pr_err("acpi_evalf() called with invalid format character '%c'\n",
622 res_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624 }
625
626 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700627 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300628 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 return success;
631}
632
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200633static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300634{
635 int v;
636
637 if (ecrd_handle) {
638 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
639 return 0;
640 *p = v;
641 } else {
642 if (ec_read(i, p) < 0)
643 return 0;
644 }
645
646 return 1;
647}
648
649static int acpi_ec_write(int i, u8 v)
650{
651 if (ecwr_handle) {
652 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
653 return 0;
654 } else {
655 if (ec_write(i, v) < 0)
656 return 0;
657 }
658
659 return 1;
660}
661
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300662static int issue_thinkpad_cmos_command(int cmos_cmd)
663{
664 if (!cmos_handle)
665 return -ENXIO;
666
667 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
668 return -EIO;
669
670 return 0;
671}
672
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300673/*************************************************************************
674 * ACPI device model
675 */
676
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200677#define TPACPI_ACPIHANDLE_INIT(object) \
678 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300679 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200680
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300681static void __init drv_acpi_handle_init(const char *name,
682 acpi_handle *handle, const acpi_handle parent,
683 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300684{
685 int i;
686 acpi_status status;
687
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300688 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
689 name);
690
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300691 for (i = 0; i < num_paths; i++) {
692 status = acpi_get_handle(parent, paths[i], handle);
693 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300694 dbg_printk(TPACPI_DBG_INIT,
695 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300696 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300697 return;
698 }
699 }
700
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300701 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
702 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300703 *handle = NULL;
704}
705
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300706static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
707 u32 level, void *context, void **return_value)
708{
Aaron Lu46445b62013-10-11 21:27:46 +0800709 struct acpi_device *dev;
710 if (!strcmp(context, "video")) {
711 if (acpi_bus_get_device(handle, &dev))
712 return AE_OK;
713 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
714 return AE_OK;
715 }
716
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300717 *(acpi_handle *)return_value = handle;
718
719 return AE_CTRL_TERMINATE;
720}
721
722static void __init tpacpi_acpi_handle_locate(const char *name,
723 const char *hid,
724 acpi_handle *handle)
725{
726 acpi_status status;
727 acpi_handle device_found;
728
Aaron Lu46445b62013-10-11 21:27:46 +0800729 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300730 vdbg_printk(TPACPI_DBG_INIT,
731 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800732 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300733
734 memset(&device_found, 0, sizeof(device_found));
735 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
736 (void *)name, &device_found);
737
738 *handle = NULL;
739
740 if (ACPI_SUCCESS(status)) {
741 *handle = device_found;
742 dbg_printk(TPACPI_DBG_INIT,
743 "Found ACPI handle for %s\n", name);
744 } else {
745 vdbg_printk(TPACPI_DBG_INIT,
746 "Could not locate an ACPI handle for %s: %s\n",
747 name, acpi_format_exception(status));
748 }
749}
750
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300751static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300752{
753 struct ibm_struct *ibm = data;
754
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300755 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
756 return;
757
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300758 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300759 return;
760
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300761 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300762}
763
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300764static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300765{
766 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300767 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300768
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300769 BUG_ON(!ibm->acpi);
770
771 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300772 return 0;
773
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300774 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300775 "setting up ACPI notify for %s\n", ibm->name);
776
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300777 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
778 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700779 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300780 return -ENODEV;
781 }
782
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700783 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300784 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200785 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300786 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300787
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300788 status = acpi_install_notify_handler(*ibm->acpi->handle,
789 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300790 if (ACPI_FAILURE(status)) {
791 if (status == AE_ALREADY_EXISTS) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300792 pr_notice("another device driver is already handling %s events\n",
793 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300794 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700795 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300796 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300797 }
798 return -ENODEV;
799 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300800 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300801 return 0;
802}
803
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300804static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300805{
806 return 0;
807}
808
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300809static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300810{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300811 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300812
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300813 dbg_printk(TPACPI_DBG_INIT,
814 "registering %s as an ACPI driver\n", ibm->name);
815
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300816 BUG_ON(!ibm->acpi);
817
818 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
819 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700820 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300821 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300822 }
823
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200824 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300825 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200826
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300827 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300828
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300829 rc = acpi_bus_register_driver(ibm->acpi->driver);
830 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700831 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200832 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300833 kfree(ibm->acpi->driver);
834 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300835 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300836 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300837
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300838 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300839}
840
841
842/****************************************************************************
843 ****************************************************************************
844 *
845 * Procfs Helpers
846 *
847 ****************************************************************************
848 ****************************************************************************/
849
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200850static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300851{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200852 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300853
854 if (!ibm || !ibm->read)
855 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200856 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300857}
858
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200859static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300860{
Al Virod9dda782013-03-31 18:16:14 -0400861 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200862}
863
864static ssize_t dispatch_proc_write(struct file *file,
865 const char __user *userbuf,
866 size_t count, loff_t *pos)
867{
Al Virod9dda782013-03-31 18:16:14 -0400868 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300869 char *kernbuf;
870 int ret;
871
872 if (!ibm || !ibm->write)
873 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300874 if (count > PAGE_SIZE - 2)
875 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300876
877 kernbuf = kmalloc(count + 2, GFP_KERNEL);
878 if (!kernbuf)
879 return -ENOMEM;
880
881 if (copy_from_user(kernbuf, userbuf, count)) {
882 kfree(kernbuf);
883 return -EFAULT;
884 }
885
886 kernbuf[count] = 0;
887 strcat(kernbuf, ",");
888 ret = ibm->write(kernbuf);
889 if (ret == 0)
890 ret = count;
891
892 kfree(kernbuf);
893
894 return ret;
895}
896
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200897static const struct file_operations dispatch_proc_fops = {
898 .owner = THIS_MODULE,
899 .open = dispatch_proc_open,
900 .read = seq_read,
901 .llseek = seq_lseek,
902 .release = single_release,
903 .write = dispatch_proc_write,
904};
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906static char *next_cmd(char **cmds)
907{
908 char *start = *cmds;
909 char *end;
910
911 while ((end = strchr(start, ',')) && end == start)
912 start = end + 1;
913
914 if (!end)
915 return NULL;
916
917 *end = 0;
918 *cmds = end + 1;
919 return start;
920}
921
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300922
923/****************************************************************************
924 ****************************************************************************
925 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300926 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300927 *
928 ****************************************************************************
929 ****************************************************************************/
930
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300931static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300932static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700933static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300934static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300935static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200936static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300937
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200938#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200939static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200940{
941 struct ibm_struct *ibm, *itmp;
942
943 list_for_each_entry_safe(ibm, itmp,
944 &tpacpi_all_drivers,
945 all_drivers) {
946 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200947 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200948 }
949
950 return 0;
951}
952
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200953static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300954{
955 struct ibm_struct *ibm, *itmp;
956
957 list_for_each_entry_safe(ibm, itmp,
958 &tpacpi_all_drivers,
959 all_drivers) {
960 if (ibm->resume)
961 (ibm->resume)();
962 }
963
964 return 0;
965}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200966#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300967
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200968static SIMPLE_DEV_PM_OPS(tpacpi_pm,
969 tpacpi_suspend_handler, tpacpi_resume_handler);
970
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200971static void tpacpi_shutdown_handler(struct platform_device *pdev)
972{
973 struct ibm_struct *ibm, *itmp;
974
975 list_for_each_entry_safe(ibm, itmp,
976 &tpacpi_all_drivers,
977 all_drivers) {
978 if (ibm->shutdown)
979 (ibm->shutdown)();
980 }
981}
982
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300983static struct platform_driver tpacpi_pdriver = {
984 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200985 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200986 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300987 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200988 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300989};
990
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300991static struct platform_driver tpacpi_hwmon_pdriver = {
992 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200993 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300994 },
995};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300996
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300997/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300998 * sysfs support helpers
999 */
1000
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001001struct attribute_set {
1002 unsigned int members, max_members;
1003 struct attribute_group group;
1004};
1005
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001006struct attribute_set_obj {
1007 struct attribute_set s;
1008 struct attribute *a;
1009} __attribute__((packed));
1010
1011static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001012 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001013{
1014 struct attribute_set_obj *sobj;
1015
1016 if (max_members == 0)
1017 return NULL;
1018
1019 /* Allocates space for implicit NULL at the end too */
1020 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1021 max_members * sizeof(struct attribute *),
1022 GFP_KERNEL);
1023 if (!sobj)
1024 return NULL;
1025 sobj->s.max_members = max_members;
1026 sobj->s.group.attrs = &sobj->a;
1027 sobj->s.group.name = name;
1028
1029 return &sobj->s;
1030}
1031
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001032#define destroy_attr_set(_set) \
1033 kfree(_set);
1034
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001035/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001036static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001037{
1038 if (!s || !attr)
1039 return -EINVAL;
1040
1041 if (s->members >= s->max_members)
1042 return -ENOMEM;
1043
1044 s->group.attrs[s->members] = attr;
1045 s->members++;
1046
1047 return 0;
1048}
1049
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001050static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001051 struct attribute **attr,
1052 unsigned int count)
1053{
1054 int i, res;
1055
1056 for (i = 0; i < count; i++) {
1057 res = add_to_attr_set(s, attr[i]);
1058 if (res)
1059 return res;
1060 }
1061
1062 return 0;
1063}
1064
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001065static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001066{
1067 sysfs_remove_group(kobj, &s->group);
1068 destroy_attr_set(s);
1069}
1070
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001071#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1072 sysfs_create_group(_kobj, &_attr_set->group)
1073
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001074static int parse_strtoul(const char *buf,
1075 unsigned long max, unsigned long *value)
1076{
1077 char *endp;
1078
André Goddard Rosae7d28602009-12-14 18:01:06 -08001079 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1080 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001081 if (*endp || *value > max)
1082 return -EINVAL;
1083
1084 return 0;
1085}
1086
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001087static void tpacpi_disable_brightness_delay(void)
1088{
1089 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001090 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001091}
1092
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001093static void printk_deprecated_attribute(const char * const what,
1094 const char * const details)
1095{
1096 tpacpi_log_usertask("deprecated sysfs attribute");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001097 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001098 what, details);
1099}
1100
Johannes Berg19d337d2009-06-02 13:01:37 +02001101/*************************************************************************
1102 * rfkill and radio control support helpers
1103 */
1104
1105/*
1106 * ThinkPad-ACPI firmware handling model:
1107 *
1108 * WLSW (master wireless switch) is event-driven, and is common to all
1109 * firmware-controlled radios. It cannot be controlled, just monitored,
1110 * as expected. It overrides all radio state in firmware
1111 *
1112 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1113 * (TODO: verify how WLSW interacts with the returned radio state).
1114 *
1115 * The only time there are shadow radio state changes, is when
1116 * masked-off hotkeys are used.
1117 */
1118
1119/*
1120 * Internal driver API for radio state:
1121 *
1122 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1123 * bool: true means radio blocked (off)
1124 */
1125enum tpacpi_rfkill_state {
1126 TPACPI_RFK_RADIO_OFF = 0,
1127 TPACPI_RFK_RADIO_ON
1128};
1129
1130/* rfkill switches */
1131enum tpacpi_rfk_id {
1132 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1133 TPACPI_RFK_WWAN_SW_ID,
1134 TPACPI_RFK_UWB_SW_ID,
1135 TPACPI_RFK_SW_MAX
1136};
1137
1138static const char *tpacpi_rfkill_names[] = {
1139 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1140 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1141 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1142 [TPACPI_RFK_SW_MAX] = NULL
1143};
1144
1145/* ThinkPad-ACPI rfkill subdriver */
1146struct tpacpi_rfk {
1147 struct rfkill *rfkill;
1148 enum tpacpi_rfk_id id;
1149 const struct tpacpi_rfk_ops *ops;
1150};
1151
1152struct tpacpi_rfk_ops {
1153 /* firmware interface */
1154 int (*get_status)(void);
1155 int (*set_status)(const enum tpacpi_rfkill_state);
1156};
1157
1158static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1159
1160/* Query FW and update rfkill sw state for a given rfkill switch */
1161static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1162{
1163 int status;
1164
1165 if (!tp_rfk)
1166 return -ENODEV;
1167
1168 status = (tp_rfk->ops->get_status)();
1169 if (status < 0)
1170 return status;
1171
1172 rfkill_set_sw_state(tp_rfk->rfkill,
1173 (status == TPACPI_RFK_RADIO_OFF));
1174
1175 return status;
1176}
1177
1178/* Query FW and update rfkill sw state for all rfkill switches */
1179static void tpacpi_rfk_update_swstate_all(void)
1180{
1181 unsigned int i;
1182
1183 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1184 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1185}
1186
1187/*
1188 * Sync the HW-blocking state of all rfkill switches,
1189 * do notice it causes the rfkill core to schedule uevents
1190 */
1191static void tpacpi_rfk_update_hwblock_state(bool blocked)
1192{
1193 unsigned int i;
1194 struct tpacpi_rfk *tp_rfk;
1195
1196 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1197 tp_rfk = tpacpi_rfkill_switches[i];
1198 if (tp_rfk) {
1199 if (rfkill_set_hw_state(tp_rfk->rfkill,
1200 blocked)) {
1201 /* ignore -- we track sw block */
1202 }
1203 }
1204 }
1205}
1206
1207/* Call to get the WLSW state from the firmware */
1208static int hotkey_get_wlsw(void);
1209
1210/* Call to query WLSW state and update all rfkill switches */
1211static bool tpacpi_rfk_check_hwblock_state(void)
1212{
1213 int res = hotkey_get_wlsw();
1214 int hw_blocked;
1215
1216 /* When unknown or unsupported, we have to assume it is unblocked */
1217 if (res < 0)
1218 return false;
1219
1220 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1221 tpacpi_rfk_update_hwblock_state(hw_blocked);
1222
1223 return hw_blocked;
1224}
1225
1226static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1227{
1228 struct tpacpi_rfk *tp_rfk = data;
1229 int res;
1230
1231 dbg_printk(TPACPI_DBG_RFKILL,
1232 "request to change radio state to %s\n",
1233 blocked ? "blocked" : "unblocked");
1234
1235 /* try to set radio state */
1236 res = (tp_rfk->ops->set_status)(blocked ?
1237 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1238
1239 /* and update the rfkill core with whatever the FW really did */
1240 tpacpi_rfk_update_swstate(tp_rfk);
1241
1242 return (res < 0) ? res : 0;
1243}
1244
1245static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1246 .set_block = tpacpi_rfk_hook_set_block,
1247};
1248
1249static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1250 const struct tpacpi_rfk_ops *tp_rfkops,
1251 const enum rfkill_type rfktype,
1252 const char *name,
1253 const bool set_default)
1254{
1255 struct tpacpi_rfk *atp_rfk;
1256 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001257 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001258 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001259 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001260
1261 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1262
Johannes Berg19d337d2009-06-02 13:01:37 +02001263 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1264 if (atp_rfk)
1265 atp_rfk->rfkill = rfkill_alloc(name,
1266 &tpacpi_pdev->dev,
1267 rfktype,
1268 &tpacpi_rfk_rfkill_ops,
1269 atp_rfk);
1270 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001271 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001272 kfree(atp_rfk);
1273 return -ENOMEM;
1274 }
1275
1276 atp_rfk->id = id;
1277 atp_rfk->ops = tp_rfkops;
1278
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001279 sw_status = (tp_rfkops->get_status)();
1280 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001281 pr_err("failed to read initial state for %s, error %d\n",
1282 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001283 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001284 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001285 if (set_default) {
1286 /* try to keep the initial state, since we ask the
1287 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001288 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001289 }
1290 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001291 hw_state = tpacpi_rfk_check_hwblock_state();
1292 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001293
1294 res = rfkill_register(atp_rfk->rfkill);
1295 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001296 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001297 rfkill_destroy(atp_rfk->rfkill);
1298 kfree(atp_rfk);
1299 return res;
1300 }
1301
1302 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001303
Joe Perches0978e012011-04-04 10:06:25 -07001304 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001305 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001306 return 0;
1307}
1308
1309static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1310{
1311 struct tpacpi_rfk *tp_rfk;
1312
1313 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1314
1315 tp_rfk = tpacpi_rfkill_switches[id];
1316 if (tp_rfk) {
1317 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001318 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001319 tpacpi_rfkill_switches[id] = NULL;
1320 kfree(tp_rfk);
1321 }
1322}
1323
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001324static void printk_deprecated_rfkill_attribute(const char * const what)
1325{
1326 printk_deprecated_attribute(what,
1327 "Please switch to generic rfkill before year 2010");
1328}
1329
Johannes Berg19d337d2009-06-02 13:01:37 +02001330/* sysfs <radio> enable ------------------------------------------------ */
1331static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1332 struct device_attribute *attr,
1333 char *buf)
1334{
1335 int status;
1336
1337 printk_deprecated_rfkill_attribute(attr->attr.name);
1338
1339 /* This is in the ABI... */
1340 if (tpacpi_rfk_check_hwblock_state()) {
1341 status = TPACPI_RFK_RADIO_OFF;
1342 } else {
1343 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1344 if (status < 0)
1345 return status;
1346 }
1347
1348 return snprintf(buf, PAGE_SIZE, "%d\n",
1349 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1350}
1351
1352static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1353 struct device_attribute *attr,
1354 const char *buf, size_t count)
1355{
1356 unsigned long t;
1357 int res;
1358
1359 printk_deprecated_rfkill_attribute(attr->attr.name);
1360
1361 if (parse_strtoul(buf, 1, &t))
1362 return -EINVAL;
1363
1364 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1365
1366 /* This is in the ABI... */
1367 if (tpacpi_rfk_check_hwblock_state() && !!t)
1368 return -EPERM;
1369
1370 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1371 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1372 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1373
1374 return (res < 0) ? res : count;
1375}
1376
1377/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001378static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001379{
Johannes Berg19d337d2009-06-02 13:01:37 +02001380 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001381 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001382 else {
1383 int status;
1384
1385 /* This is in the ABI... */
1386 if (tpacpi_rfk_check_hwblock_state()) {
1387 status = TPACPI_RFK_RADIO_OFF;
1388 } else {
1389 status = tpacpi_rfk_update_swstate(
1390 tpacpi_rfkill_switches[id]);
1391 if (status < 0)
1392 return status;
1393 }
1394
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001395 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001396 (status == TPACPI_RFK_RADIO_ON) ?
1397 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001398 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001399 }
1400
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001401 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001402}
1403
1404static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1405{
1406 char *cmd;
1407 int status = -1;
1408 int res = 0;
1409
1410 if (id >= TPACPI_RFK_SW_MAX)
1411 return -ENODEV;
1412
1413 while ((cmd = next_cmd(&buf))) {
1414 if (strlencmp(cmd, "enable") == 0)
1415 status = TPACPI_RFK_RADIO_ON;
1416 else if (strlencmp(cmd, "disable") == 0)
1417 status = TPACPI_RFK_RADIO_OFF;
1418 else
1419 return -EINVAL;
1420 }
1421
1422 if (status != -1) {
1423 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1424 (status == TPACPI_RFK_RADIO_ON) ?
1425 "enable" : "disable",
1426 tpacpi_rfkill_names[id]);
1427 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1428 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1429 }
1430
1431 return res;
1432}
1433
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001434/*************************************************************************
1435 * thinkpad-acpi driver attributes
1436 */
1437
1438/* interface_version --------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001439static ssize_t interface_version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001440{
1441 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1442}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001443static DRIVER_ATTR_RO(interface_version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001444
1445/* debug_level --------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001446static ssize_t debug_level_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001447{
1448 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1449}
1450
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001451static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1452 size_t count)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001453{
1454 unsigned long t;
1455
1456 if (parse_strtoul(buf, 0xffff, &t))
1457 return -EINVAL;
1458
1459 dbg_level = t;
1460
1461 return count;
1462}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001463static DRIVER_ATTR_RW(debug_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001464
1465/* version ------------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001466static ssize_t version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001467{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001468 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1469 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001470}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001471static DRIVER_ATTR_RO(version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001472
1473/* --------------------------------------------------------------------- */
1474
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001475#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1476
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001477/* wlsw_emulstate ------------------------------------------------------ */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001478static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001479{
1480 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1481}
1482
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001483static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1484 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001485{
1486 unsigned long t;
1487
1488 if (parse_strtoul(buf, 1, &t))
1489 return -EINVAL;
1490
Johannes Berg19d337d2009-06-02 13:01:37 +02001491 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001492 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001493 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1494 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001495
1496 return count;
1497}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001498static DRIVER_ATTR_RW(wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001499
1500/* bluetooth_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001501static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001502{
1503 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1504}
1505
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001506static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1507 const char *buf, size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001508{
1509 unsigned long t;
1510
1511 if (parse_strtoul(buf, 1, &t))
1512 return -EINVAL;
1513
1514 tpacpi_bluetooth_emulstate = !!t;
1515
1516 return count;
1517}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001518static DRIVER_ATTR_RW(bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001519
1520/* wwan_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001521static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001522{
1523 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1524}
1525
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001526static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1527 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001528{
1529 unsigned long t;
1530
1531 if (parse_strtoul(buf, 1, &t))
1532 return -EINVAL;
1533
1534 tpacpi_wwan_emulstate = !!t;
1535
1536 return count;
1537}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001538static DRIVER_ATTR_RW(wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001539
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001540/* uwb_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001541static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001542{
1543 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1544}
1545
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001546static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1547 size_t count)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001548{
1549 unsigned long t;
1550
1551 if (parse_strtoul(buf, 1, &t))
1552 return -EINVAL;
1553
1554 tpacpi_uwb_emulstate = !!t;
1555
1556 return count;
1557}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001558static DRIVER_ATTR_RW(uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001559#endif
1560
1561/* --------------------------------------------------------------------- */
1562
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001563static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001564 &driver_attr_debug_level, &driver_attr_version,
1565 &driver_attr_interface_version,
1566};
1567
1568static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1569{
1570 int i, res;
1571
1572 i = 0;
1573 res = 0;
1574 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1575 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1576 i++;
1577 }
1578
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001579#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1580 if (!res && dbg_wlswemul)
1581 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1582 if (!res && dbg_bluetoothemul)
1583 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1584 if (!res && dbg_wwanemul)
1585 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001586 if (!res && dbg_uwbemul)
1587 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001588#endif
1589
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001590 return res;
1591}
1592
1593static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1594{
1595 int i;
1596
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001597 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001598 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001599
1600#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1601 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1602 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1603 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001604 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001605#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001606}
1607
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001608/*************************************************************************
1609 * Firmware Data
1610 */
1611
1612/*
1613 * Table of recommended minimum BIOS versions
1614 *
1615 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001616 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001617 * bugs and bad ACPI behaviour on older versions
1618 *
1619 * 2. BIOS or EC fw with known bugs that trigger on Linux
1620 *
1621 * 3. BIOS with known reduced functionality in older versions
1622 *
1623 * We recommend the latest BIOS and EC version.
1624 * We only support the latest BIOS and EC fw version as a rule.
1625 *
1626 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1627 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001628 *
1629 * WARNING: we use this table also to detect that the machine is
1630 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001631 */
1632
1633#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1634 { .vendor = (__v), \
1635 .bios = TPID(__id1, __id2), \
1636 .ec = TPACPI_MATCH_ANY, \
1637 .quirks = TPACPI_MATCH_ANY << 16 \
1638 | (__bv1) << 8 | (__bv2) }
1639
1640#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001641 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001642 { .vendor = (__v), \
1643 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001644 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001645 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1646 | (__bv1) << 8 | (__bv2) }
1647
1648#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1649 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1650
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001651/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001652#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1653 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001654 __bv1, __bv2, TPID(__id1, __id2), \
1655 __ev1, __ev2), \
1656 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1657 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1658 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001659
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001660/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001661#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1662 __eid1, __eid2, __ev1, __ev2) \
1663 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001664 __bv1, __bv2, TPID(__eid1, __eid2), \
1665 __ev1, __ev2), \
1666 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1667 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1668 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001669
1670#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1671 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1672
1673#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1674 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001675 __bv1, __bv2, TPID(__id1, __id2), \
1676 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001677
1678#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1679 __eid1, __eid2, __ev1, __ev2) \
1680 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001681 __bv1, __bv2, TPID(__eid1, __eid2), \
1682 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001683
1684static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1685 /* Numeric models ------------------ */
1686 /* FW MODEL BIOS VERS */
1687 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1688 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1689 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1690 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1691 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1692 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1693 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1694 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1695 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1696
1697 /* A-series ------------------------- */
1698 /* FW MODEL BIOS VERS EC VERS */
1699 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1700 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1701 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1702 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1703 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1704 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1705 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1706 TPV_QI0('1', '3', '2', '0'), /* A22m */
1707 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1708 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1709 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1710
1711 /* G-series ------------------------- */
1712 /* FW MODEL BIOS VERS */
1713 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1714 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1715
1716 /* R-series, T-series --------------- */
1717 /* FW MODEL BIOS VERS EC VERS */
1718 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1719 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1720 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1721 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1722 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1723 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1724 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1725 T40/p, T41/p, T42/p (1) */
1726 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1727 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1728 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1729 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1730
1731 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1732 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1733 TPV_QI0('1', '6', '3', '2'), /* T22 */
1734 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1735 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1736 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1737
1738 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1739 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001740 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001741
1742 /* BIOS FW BIOS VERS EC FW EC VERS */
1743 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1744 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1745
1746 /* X-series ------------------------- */
1747 /* FW MODEL BIOS VERS EC VERS */
1748 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1749 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1750 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1751 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1752 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1753 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1754 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1755
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001756 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1757 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001758
1759 /* (0) - older versions lack DMI EC fw string and functionality */
1760 /* (1) - older versions known to lack functionality */
1761};
1762
1763#undef TPV_QL1
1764#undef TPV_QL0
1765#undef TPV_QI2
1766#undef TPV_QI1
1767#undef TPV_QI0
1768#undef TPV_Q_X
1769#undef TPV_Q
1770
1771static void __init tpacpi_check_outdated_fw(void)
1772{
1773 unsigned long fwvers;
1774 u16 ec_version, bios_version;
1775
1776 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1777 ARRAY_SIZE(tpacpi_bios_version_qtable));
1778
1779 if (!fwvers)
1780 return;
1781
1782 bios_version = fwvers & 0xffffU;
1783 ec_version = (fwvers >> 16) & 0xffffU;
1784
1785 /* note that unknown versions are set to 0x0000 and we use that */
1786 if ((bios_version > thinkpad_id.bios_release) ||
1787 (ec_version > thinkpad_id.ec_release &&
1788 ec_version != TPACPI_MATCH_ANY)) {
1789 /*
1790 * The changelogs would let us track down the exact
1791 * reason, but it is just too much of a pain to track
1792 * it. We only list BIOSes that are either really
1793 * broken, or really stable to begin with, so it is
1794 * best if the user upgrades the firmware anyway.
1795 */
Joe Perches0978e012011-04-04 10:06:25 -07001796 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001797 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001798 }
1799}
1800
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001801static bool __init tpacpi_is_fw_known(void)
1802{
1803 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1804 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1805}
1806
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001807/****************************************************************************
1808 ****************************************************************************
1809 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001810 * Subdrivers
1811 *
1812 ****************************************************************************
1813 ****************************************************************************/
1814
1815/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001816 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001817 */
1818
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001819static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001821 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1822 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001826static struct ibm_struct thinkpad_acpi_driver_data = {
1827 .name = "driver",
1828 .read = thinkpad_acpi_driver_read,
1829};
1830
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001831/*************************************************************************
1832 * Hotkey subdriver
1833 */
1834
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001835/*
1836 * ThinkPad firmware event model
1837 *
1838 * The ThinkPad firmware has two main event interfaces: normal ACPI
1839 * notifications (which follow the ACPI standard), and a private event
1840 * interface.
1841 *
1842 * The private event interface also issues events for the hotkeys. As
1843 * the driver gained features, the event handling code ended up being
1844 * built around the hotkey subdriver. This will need to be refactored
1845 * to a more formal event API eventually.
1846 *
1847 * Some "hotkeys" are actually supposed to be used as event reports,
1848 * such as "brightness has changed", "volume has changed", depending on
1849 * the ThinkPad model and how the firmware is operating.
1850 *
1851 * Unlike other classes, hotkey-class events have mask/unmask control on
1852 * non-ancient firmware. However, how it behaves changes a lot with the
1853 * firmware model and version.
1854 */
1855
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001856enum { /* hot key scan codes (derived from ACPI DSDT) */
1857 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1858 TP_ACPI_HOTKEYSCAN_FNF2,
1859 TP_ACPI_HOTKEYSCAN_FNF3,
1860 TP_ACPI_HOTKEYSCAN_FNF4,
1861 TP_ACPI_HOTKEYSCAN_FNF5,
1862 TP_ACPI_HOTKEYSCAN_FNF6,
1863 TP_ACPI_HOTKEYSCAN_FNF7,
1864 TP_ACPI_HOTKEYSCAN_FNF8,
1865 TP_ACPI_HOTKEYSCAN_FNF9,
1866 TP_ACPI_HOTKEYSCAN_FNF10,
1867 TP_ACPI_HOTKEYSCAN_FNF11,
1868 TP_ACPI_HOTKEYSCAN_FNF12,
1869 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1870 TP_ACPI_HOTKEYSCAN_FNINSERT,
1871 TP_ACPI_HOTKEYSCAN_FNDELETE,
1872 TP_ACPI_HOTKEYSCAN_FNHOME,
1873 TP_ACPI_HOTKEYSCAN_FNEND,
1874 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1875 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1876 TP_ACPI_HOTKEYSCAN_FNSPACE,
1877 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1878 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1879 TP_ACPI_HOTKEYSCAN_MUTE,
1880 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001881 TP_ACPI_HOTKEYSCAN_UNK1,
1882 TP_ACPI_HOTKEYSCAN_UNK2,
1883 TP_ACPI_HOTKEYSCAN_UNK3,
1884 TP_ACPI_HOTKEYSCAN_UNK4,
1885 TP_ACPI_HOTKEYSCAN_UNK5,
1886 TP_ACPI_HOTKEYSCAN_UNK6,
1887 TP_ACPI_HOTKEYSCAN_UNK7,
1888 TP_ACPI_HOTKEYSCAN_UNK8,
1889
Christian Kellner149c8c72017-02-28 17:10:56 +01001890 /* Adaptive keyboard keycodes */
1891 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1892 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001893 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1894 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1895 TP_ACPI_HOTKEYSCAN_CLOUD,
1896 TP_ACPI_HOTKEYSCAN_UNK9,
1897 TP_ACPI_HOTKEYSCAN_VOICE,
1898 TP_ACPI_HOTKEYSCAN_UNK10,
1899 TP_ACPI_HOTKEYSCAN_GESTURES,
1900 TP_ACPI_HOTKEYSCAN_UNK11,
1901 TP_ACPI_HOTKEYSCAN_UNK12,
1902 TP_ACPI_HOTKEYSCAN_UNK13,
1903 TP_ACPI_HOTKEYSCAN_CONFIG,
1904 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1905 TP_ACPI_HOTKEYSCAN_RELOAD,
1906 TP_ACPI_HOTKEYSCAN_BACK,
1907 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1908 TP_ACPI_HOTKEYSCAN_MIC_UP,
1909 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1910 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1911 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1912
Christian Kellner696c65232017-02-28 17:10:57 +01001913 /* Lenovo extended keymap, starting at 0x1300 */
1914 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1915 /* first new observed key (star, favorites) is 0x1311 */
1916 TP_ACPI_HOTKEYSCAN_STAR = 69,
1917 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1918 TP_ACPI_HOTKEYSCAN_UNK25,
1919 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1920 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1921
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001922 /* Hotkey keymap size */
1923 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001924};
1925
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001926enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001927 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1928 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1929};
1930
1931enum { /* Positions of some of the keys in hotkey masks */
1932 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1933 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1934 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1935 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1936 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001937 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001938 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1939 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1940 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1941 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1942 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1943};
1944
1945enum { /* NVRAM to ACPI HKEY group map */
1946 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1947 TP_ACPI_HKEY_ZOOM_MASK |
1948 TP_ACPI_HKEY_DISPSWTCH_MASK |
1949 TP_ACPI_HKEY_HIBERNATE_MASK,
1950 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1951 TP_ACPI_HKEY_BRGHTDWN_MASK,
1952 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1953 TP_ACPI_HKEY_VOLDWN_MASK |
1954 TP_ACPI_HKEY_MUTE_MASK,
1955};
1956
1957#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1958struct tp_nvram_state {
1959 u16 thinkpad_toggle:1;
1960 u16 zoom_toggle:1;
1961 u16 display_toggle:1;
1962 u16 thinklight_toggle:1;
1963 u16 hibernate_toggle:1;
1964 u16 displayexp_toggle:1;
1965 u16 display_state:1;
1966 u16 brightness_toggle:1;
1967 u16 volume_toggle:1;
1968 u16 mute:1;
1969
1970 u8 brightness_level;
1971 u8 volume_level;
1972};
1973
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001974/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001975static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001976
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001977/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001978 * Acquire mutex to write poller control variables as an
1979 * atomic block.
1980 *
1981 * Increment hotkey_config_change when changing them if you
1982 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001983 *
1984 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1985 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001986static struct mutex hotkey_thread_data_mutex;
1987static unsigned int hotkey_config_change;
1988
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001989/*
1990 * hotkey poller control variables
1991 *
1992 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001993 *
1994 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1995 * should be used only when the changes need to be taken as
1996 * a block, OR when one needs to force the kthread to forget
1997 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001998 */
1999static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2000static unsigned int hotkey_poll_freq = 10; /* Hz */
2001
2002#define HOTKEY_CONFIG_CRITICAL_START \
2003 do { \
2004 mutex_lock(&hotkey_thread_data_mutex); \
2005 hotkey_config_change++; \
2006 } while (0);
2007#define HOTKEY_CONFIG_CRITICAL_END \
2008 mutex_unlock(&hotkey_thread_data_mutex);
2009
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002010#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2011
2012#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002013#define HOTKEY_CONFIG_CRITICAL_START
2014#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002015
2016#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2017
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002018static struct mutex hotkey_mutex;
2019
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002020static enum { /* Reasons for waking up */
2021 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2022 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2023 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2024} hotkey_wakeup_reason;
2025
2026static int hotkey_autosleep_ack;
2027
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002028static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2029static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002030static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002031static u32 hotkey_reserved_mask; /* events better left disabled */
2032static u32 hotkey_driver_mask; /* events needed by the driver */
2033static u32 hotkey_user_mask; /* events visible to userspace */
2034static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002035
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002036static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002037
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002038static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002039
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002040static void tpacpi_driver_event(const unsigned int hkey_event);
2041static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002042static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002043
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002044/* HKEY.MHKG() return bits */
2045#define TP_HOTKEY_TABLET_MASK (1 << 3)
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002046enum {
2047 TP_ACPI_MULTI_MODE_INVALID = 0,
2048 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2049 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2050 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2051 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2052 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2053 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2054 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2055};
2056
2057enum {
2058 /* The following modes are considered tablet mode for the purpose of
2059 * reporting the status to userspace. i.e. in all these modes it makes
2060 * sense to disable the laptop input devices such as touchpad and
2061 * keyboard.
2062 */
2063 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2064 TP_ACPI_MULTI_MODE_STAND |
2065 TP_ACPI_MULTI_MODE_TENT |
2066 TP_ACPI_MULTI_MODE_STAND_TENT,
2067};
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002068
Johannes Berg19d337d2009-06-02 13:01:37 +02002069static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002070{
Johannes Berg19d337d2009-06-02 13:01:37 +02002071 int status;
2072
2073 if (!tp_features.hotkey_wlsw)
2074 return -ENODEV;
2075
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002076#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002077 if (dbg_wlswemul)
2078 return (tpacpi_wlsw_emulstate) ?
2079 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002080#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002081
2082 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002083 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002084
2085 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002086}
2087
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002088static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2089{
2090 int type = (s >> 16) & 0xffff;
2091 int value = s & 0xffff;
2092 int mode = TP_ACPI_MULTI_MODE_INVALID;
2093 int valid_modes = 0;
2094
2095 if (has_tablet_mode)
2096 *has_tablet_mode = 0;
2097
2098 switch (type) {
2099 case 1:
2100 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2101 TP_ACPI_MULTI_MODE_TABLET |
2102 TP_ACPI_MULTI_MODE_STAND_TENT;
2103 break;
2104 case 2:
2105 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2106 TP_ACPI_MULTI_MODE_FLAT |
2107 TP_ACPI_MULTI_MODE_TABLET |
2108 TP_ACPI_MULTI_MODE_STAND |
2109 TP_ACPI_MULTI_MODE_TENT;
2110 break;
2111 case 3:
2112 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2113 TP_ACPI_MULTI_MODE_FLAT;
2114 break;
2115 case 4:
2116 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2117 TP_ACPI_MULTI_MODE_TABLET |
2118 TP_ACPI_MULTI_MODE_STAND |
2119 TP_ACPI_MULTI_MODE_TENT;
2120 break;
2121 case 5:
2122 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2123 TP_ACPI_MULTI_MODE_FLAT |
2124 TP_ACPI_MULTI_MODE_TABLET |
2125 TP_ACPI_MULTI_MODE_STAND |
2126 TP_ACPI_MULTI_MODE_TENT;
2127 break;
2128 default:
2129 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2130 type, value, TPACPI_MAIL);
2131 return 0;
2132 }
2133
2134 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2135 *has_tablet_mode = 1;
2136
2137 switch (value) {
2138 case 1:
2139 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2140 break;
2141 case 2:
2142 mode = TP_ACPI_MULTI_MODE_FLAT;
2143 break;
2144 case 3:
2145 mode = TP_ACPI_MULTI_MODE_TABLET;
2146 break;
2147 case 4:
2148 if (type == 1)
2149 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2150 else
2151 mode = TP_ACPI_MULTI_MODE_STAND;
2152 break;
2153 case 5:
2154 mode = TP_ACPI_MULTI_MODE_TENT;
2155 break;
2156 default:
2157 if (type == 5 && value == 0xffff) {
2158 pr_warn("Multi mode status is undetected, assuming laptop\n");
2159 return 0;
2160 }
2161 }
2162
2163 if (!(mode & valid_modes)) {
2164 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2165 value, type, TPACPI_MAIL);
2166 return 0;
2167 }
2168
2169 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2170}
2171
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002172static int hotkey_get_tablet_mode(int *status)
2173{
2174 int s;
2175
Lyudeb03f4d42016-11-11 15:15:03 -05002176 switch (tp_features.hotkey_tablet) {
2177 case TP_HOTKEY_TABLET_USES_MHKG:
2178 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2179 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002180
Lyudeb03f4d42016-11-11 15:15:03 -05002181 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2182 break;
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002183 case TP_HOTKEY_TABLET_USES_GMMS:
2184 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
Lyudeb03f4d42016-11-11 15:15:03 -05002185 return -EIO;
2186
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002187 *status = hotkey_gmms_get_tablet_mode(s, NULL);
Lyudeb03f4d42016-11-11 15:15:03 -05002188 break;
2189 default:
2190 break;
2191 }
2192
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002193 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002194}
2195
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002196/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002197 * Reads current event mask from firmware, and updates
2198 * hotkey_acpi_mask accordingly. Also resets any bits
2199 * from hotkey_user_mask that are unavailable to be
2200 * delivered (shadow requirement of the userspace ABI).
2201 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002202 * Call with hotkey_mutex held
2203 */
2204static int hotkey_mask_get(void)
2205{
2206 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002207 u32 m = 0;
2208
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002209 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002210 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002211
2212 hotkey_acpi_mask = m;
2213 } else {
2214 /* no mask support doesn't mean no event support... */
2215 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002216 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002217
2218 /* sync userspace-visible mask */
2219 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002220
2221 return 0;
2222}
2223
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002224static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002225{
2226 /* log only what the user can fix... */
2227 const u32 wantedmask = hotkey_driver_mask &
2228 ~(hotkey_acpi_mask | hotkey_source_mask) &
2229 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2230
2231 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002232 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002233}
2234
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002235/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002236 * Set the firmware mask when supported
2237 *
2238 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2239 *
2240 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2241 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002242 * Call with hotkey_mutex held
2243 */
2244static int hotkey_mask_set(u32 mask)
2245{
2246 int i;
2247 int rc = 0;
2248
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002249 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002250
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002251 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002252 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002253 if (!acpi_evalf(hkey_handle,
2254 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002255 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002256 rc = -EIO;
2257 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002258 }
2259 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002260 }
2261
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002262 /*
2263 * We *must* make an inconditional call to hotkey_mask_get to
2264 * refresh hotkey_acpi_mask and update hotkey_user_mask
2265 *
2266 * Take the opportunity to also log when we cannot _enable_
2267 * a given event.
2268 */
2269 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002270 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002271 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002272 }
2273
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002274 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2275 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002276
2277 return rc;
2278}
2279
2280/*
2281 * Sets hotkey_user_mask and tries to set the firmware mask
2282 *
2283 * Call with hotkey_mutex held
2284 */
2285static int hotkey_user_mask_set(const u32 mask)
2286{
2287 int rc;
2288
2289 /* Give people a chance to notice they are doing something that
2290 * is bound to go boom on their users sooner or later */
2291 if (!tp_warned.hotkey_mask_ff &&
2292 (mask == 0xffff || mask == 0xffffff ||
2293 mask == 0xffffffff)) {
2294 tp_warned.hotkey_mask_ff = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002295 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2296 mask);
2297 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002298 }
2299
2300 /* Try to enable what the user asked for, plus whatever we need.
2301 * this syncs everything but won't enable bits in hotkey_user_mask */
2302 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2303
2304 /* Enable the available bits in hotkey_user_mask */
2305 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2306
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002307 return rc;
2308}
2309
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002310/*
2311 * Sets the driver hotkey mask.
2312 *
2313 * Can be called even if the hotkey subdriver is inactive
2314 */
2315static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2316{
2317 int rc;
2318
2319 /* Do the right thing if hotkey_init has not been called yet */
2320 if (!tp_features.hotkey) {
2321 hotkey_driver_mask = mask;
2322 return 0;
2323 }
2324
2325 mutex_lock(&hotkey_mutex);
2326
2327 HOTKEY_CONFIG_CRITICAL_START
2328 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002329#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002330 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002331#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002332 HOTKEY_CONFIG_CRITICAL_END
2333
2334 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2335 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002336 hotkey_poll_setup(true);
2337
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002338 mutex_unlock(&hotkey_mutex);
2339
2340 return rc;
2341}
2342
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002343static int hotkey_status_get(int *status)
2344{
2345 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2346 return -EIO;
2347
2348 return 0;
2349}
2350
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002351static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002352{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002353 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002354 return -EIO;
2355
2356 return 0;
2357}
2358
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002359static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002360{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002361 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002362
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002363 if (tp_features.hotkey_tablet &&
2364 !hotkey_get_tablet_mode(&state)) {
2365 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002366
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002367 input_report_switch(tpacpi_inputdev,
2368 SW_TABLET_MODE, !!state);
2369 input_sync(tpacpi_inputdev);
2370
2371 mutex_unlock(&tpacpi_inputdev_send_mutex);
2372 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002373}
2374
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002375/* Do NOT call without validating scancode first */
2376static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002377{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002378 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002379
2380 if (keycode != KEY_RESERVED) {
2381 mutex_lock(&tpacpi_inputdev_send_mutex);
2382
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002383 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002384 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002385 input_sync(tpacpi_inputdev);
2386
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002387 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002388 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002389 input_sync(tpacpi_inputdev);
2390
2391 mutex_unlock(&tpacpi_inputdev_send_mutex);
2392 }
2393}
2394
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002395/* Do NOT call without validating scancode first */
2396static void tpacpi_input_send_key_masked(const unsigned int scancode)
2397{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002398 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002399 if (hotkey_user_mask & (1 << scancode))
2400 tpacpi_input_send_key(scancode);
2401}
2402
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002403#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2404static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2405
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002406/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002407static void tpacpi_hotkey_send_key(unsigned int scancode)
2408{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002409 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002410}
2411
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002412static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002413{
2414 u8 d;
2415
2416 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2417 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2418 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2419 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2420 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2421 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2422 }
Hans de Goedec685e202017-02-09 16:44:13 +01002423 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002424 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2425 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2426 }
2427 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2428 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2429 n->displayexp_toggle =
2430 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2431 }
2432 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2433 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2434 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2435 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2436 n->brightness_toggle =
2437 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2438 }
2439 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2440 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2441 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2442 >> TP_NVRAM_POS_LEVEL_VOLUME;
2443 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2444 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2445 }
2446}
2447
Behan Webstera4d44ba2014-02-12 21:58:46 +01002448#define TPACPI_COMPARE_KEY(__scancode, __member) \
2449do { \
2450 if ((event_mask & (1 << __scancode)) && \
2451 oldn->__member != newn->__member) \
2452 tpacpi_hotkey_send_key(__scancode); \
2453} while (0)
2454
2455#define TPACPI_MAY_SEND_KEY(__scancode) \
2456do { \
2457 if (event_mask & (1 << __scancode)) \
2458 tpacpi_hotkey_send_key(__scancode); \
2459} while (0)
2460
2461static void issue_volchange(const unsigned int oldvol,
2462 const unsigned int newvol,
2463 const u32 event_mask)
2464{
2465 unsigned int i = oldvol;
2466
2467 while (i > newvol) {
2468 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2469 i--;
2470 }
2471 while (i < newvol) {
2472 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2473 i++;
2474 }
2475}
2476
2477static void issue_brightnesschange(const unsigned int oldbrt,
2478 const unsigned int newbrt,
2479 const u32 event_mask)
2480{
2481 unsigned int i = oldbrt;
2482
2483 while (i > newbrt) {
2484 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2485 i--;
2486 }
2487 while (i < newbrt) {
2488 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2489 i++;
2490 }
2491}
2492
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002493static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2494 struct tp_nvram_state *newn,
2495 const u32 event_mask)
2496{
2497
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002498 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2499 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2500 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2501 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2502
2503 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2504
2505 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2506
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002507 /*
2508 * Handle volume
2509 *
2510 * This code is supposed to duplicate the IBM firmware behaviour:
2511 * - Pressing MUTE issues mute hotkey message, even when already mute
2512 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002513 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002514 * - The act of unmuting issues volume up/down notification,
2515 * depending which key was used to unmute
2516 *
2517 * We are constrained to what the NVRAM can tell us, which is not much
2518 * and certainly not enough if more than one volume hotkey was pressed
2519 * since the last poll cycle.
2520 *
2521 * Just to make our life interesting, some newer Lenovo ThinkPads have
2522 * bugs in the BIOS and may fail to update volume_toggle properly.
2523 */
2524 if (newn->mute) {
2525 /* muted */
2526 if (!oldn->mute ||
2527 oldn->volume_toggle != newn->volume_toggle ||
2528 oldn->volume_level != newn->volume_level) {
2529 /* recently muted, or repeated mute keypress, or
2530 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002531 issue_volchange(oldn->volume_level, newn->volume_level,
2532 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002533 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2534 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002535 } else {
2536 /* unmute */
2537 if (oldn->mute) {
2538 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002539 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002540 }
2541 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002542 issue_volchange(oldn->volume_level, newn->volume_level,
2543 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002544 } else if (oldn->volume_toggle != newn->volume_toggle) {
2545 /* repeated vol up/down keypress at end of scale ? */
2546 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002547 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002548 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2549 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002550 }
2551 }
2552
2553 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002554 if (oldn->brightness_level != newn->brightness_level) {
2555 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002556 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002557 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2558 /* repeated key presses that didn't change state */
2559 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002560 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002561 else if (newn->brightness_level >= bright_maxlvl
2562 && !tp_features.bright_unkfw)
2563 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002564 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002565
2566#undef TPACPI_COMPARE_KEY
2567#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002568}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002569
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002570/*
2571 * Polling driver
2572 *
2573 * We track all events in hotkey_source_mask all the time, since
2574 * most of them are edge-based. We only issue those requested by
2575 * hotkey_user_mask or hotkey_driver_mask, though.
2576 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002577static int hotkey_kthread(void *data)
2578{
2579 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002580 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002581 unsigned int si, so;
2582 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002583 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002584 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002585 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002586
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002587 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2588 goto exit;
2589
2590 set_freezable();
2591
2592 so = 0;
2593 si = 1;
2594 t = 0;
2595
2596 /* Initial state for compares */
2597 mutex_lock(&hotkey_thread_data_mutex);
2598 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002599 poll_mask = hotkey_source_mask;
2600 event_mask = hotkey_source_mask &
2601 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002602 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002603 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002604 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002605
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002606 while (!kthread_should_stop()) {
2607 if (t == 0) {
2608 if (likely(poll_freq))
2609 t = 1000/poll_freq;
2610 else
2611 t = 100; /* should never happen... */
2612 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002613 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002614 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002615 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002616
2617 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002618 continue;
2619
2620 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002621 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002622 /* forget old state on thaw or config change */
2623 si = so;
2624 t = 0;
2625 change_detector = hotkey_config_change;
2626 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002627 poll_mask = hotkey_source_mask;
2628 event_mask = hotkey_source_mask &
2629 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002630 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002631 mutex_unlock(&hotkey_thread_data_mutex);
2632
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002633 if (likely(poll_mask)) {
2634 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002635 if (likely(si != so)) {
2636 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002637 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002638 }
2639 }
2640
2641 so = si;
2642 si ^= 1;
2643 }
2644
2645exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002646 return 0;
2647}
2648
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002649/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002650static void hotkey_poll_stop_sync(void)
2651{
2652 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002653 kthread_stop(tpacpi_hotkey_task);
2654 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002655 }
2656}
2657
2658/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002659static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002660{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002661 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2662 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002663
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002664 if (hotkey_poll_freq > 0 &&
2665 (poll_driver_mask ||
2666 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002667 if (!tpacpi_hotkey_task) {
2668 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002669 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002670 if (IS_ERR(tpacpi_hotkey_task)) {
2671 tpacpi_hotkey_task = NULL;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002672 pr_err("could not create kernel thread for hotkey polling\n");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002673 }
2674 }
2675 } else {
2676 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002677 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002678 hotkey_poll_freq == 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002679 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
Joe Perches0978e012011-04-04 10:06:25 -07002680 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002681 }
2682 }
2683}
2684
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002685static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002686{
2687 mutex_lock(&hotkey_mutex);
2688 hotkey_poll_setup(may_warn);
2689 mutex_unlock(&hotkey_mutex);
2690}
2691
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002692/* call with hotkey_mutex held */
2693static void hotkey_poll_set_freq(unsigned int freq)
2694{
2695 if (!freq)
2696 hotkey_poll_stop_sync();
2697
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002698 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002699}
2700
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002701#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2702
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002703static void hotkey_poll_setup(const bool __unused)
2704{
2705}
2706
2707static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002708{
2709}
2710
2711#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2712
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002713static int hotkey_inputdev_open(struct input_dev *dev)
2714{
2715 switch (tpacpi_lifecycle) {
2716 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002717 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002718 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002719 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002720 case TPACPI_LIFE_EXITING:
2721 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002722 }
2723
2724 /* Should only happen if tpacpi_lifecycle is corrupt */
2725 BUG();
2726 return -EBUSY;
2727}
2728
2729static void hotkey_inputdev_close(struct input_dev *dev)
2730{
2731 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002732 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002733 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002734 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002735}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002736
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002737/* sysfs hotkey enable ------------------------------------------------- */
2738static ssize_t hotkey_enable_show(struct device *dev,
2739 struct device_attribute *attr,
2740 char *buf)
2741{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002742 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002743
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002744 printk_deprecated_attribute("hotkey_enable",
2745 "Hotkey reporting is always enabled");
2746
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002747 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002748 if (res)
2749 return res;
2750
2751 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2752}
2753
2754static ssize_t hotkey_enable_store(struct device *dev,
2755 struct device_attribute *attr,
2756 const char *buf, size_t count)
2757{
2758 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002759
2760 printk_deprecated_attribute("hotkey_enable",
2761 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002762
2763 if (parse_strtoul(buf, 1, &t))
2764 return -EINVAL;
2765
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002766 if (t == 0)
2767 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002768
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002769 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002770}
2771
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002772static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002773
2774/* sysfs hotkey mask --------------------------------------------------- */
2775static ssize_t hotkey_mask_show(struct device *dev,
2776 struct device_attribute *attr,
2777 char *buf)
2778{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002779 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002780}
2781
2782static ssize_t hotkey_mask_store(struct device *dev,
2783 struct device_attribute *attr,
2784 const char *buf, size_t count)
2785{
2786 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002787 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002788
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002789 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002790 return -EINVAL;
2791
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002792 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002793 return -ERESTARTSYS;
2794
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002795 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002796
2797#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002798 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002799#endif
2800
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002801 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002802
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002803 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2804
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002805 return (res) ? res : count;
2806}
2807
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002808static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002809
2810/* sysfs hotkey bios_enabled ------------------------------------------- */
2811static ssize_t hotkey_bios_enabled_show(struct device *dev,
2812 struct device_attribute *attr,
2813 char *buf)
2814{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002815 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002816}
2817
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002818static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002819
2820/* sysfs hotkey bios_mask ---------------------------------------------- */
2821static ssize_t hotkey_bios_mask_show(struct device *dev,
2822 struct device_attribute *attr,
2823 char *buf)
2824{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002825 printk_deprecated_attribute("hotkey_bios_mask",
2826 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002827 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002828}
2829
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002830static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002831
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002832/* sysfs hotkey all_mask ----------------------------------------------- */
2833static ssize_t hotkey_all_mask_show(struct device *dev,
2834 struct device_attribute *attr,
2835 char *buf)
2836{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002837 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2838 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002839}
2840
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002841static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002842
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002843/* sysfs hotkey all_mask ----------------------------------------------- */
2844static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2845 struct device_attribute *attr,
2846 char *buf)
2847{
2848 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2849 hotkey_adaptive_all_mask | hotkey_source_mask);
2850}
2851
2852static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2853
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002854/* sysfs hotkey recommended_mask --------------------------------------- */
2855static ssize_t hotkey_recommended_mask_show(struct device *dev,
2856 struct device_attribute *attr,
2857 char *buf)
2858{
2859 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002860 (hotkey_all_mask | hotkey_source_mask)
2861 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002862}
2863
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002864static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002865
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002866#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2867
2868/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2869static ssize_t hotkey_source_mask_show(struct device *dev,
2870 struct device_attribute *attr,
2871 char *buf)
2872{
2873 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2874}
2875
2876static ssize_t hotkey_source_mask_store(struct device *dev,
2877 struct device_attribute *attr,
2878 const char *buf, size_t count)
2879{
2880 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002881 u32 r_ev;
2882 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002883
2884 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2885 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2886 return -EINVAL;
2887
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002888 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002889 return -ERESTARTSYS;
2890
2891 HOTKEY_CONFIG_CRITICAL_START
2892 hotkey_source_mask = t;
2893 HOTKEY_CONFIG_CRITICAL_END
2894
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002895 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2896 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002897 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002898
2899 /* check if events needed by the driver got disabled */
2900 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2901 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002902
2903 mutex_unlock(&hotkey_mutex);
2904
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002905 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002906 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002907
2908 if (r_ev)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002909 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002910 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002911
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002912 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2913
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002914 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002915}
2916
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002917static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002918
2919/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2920static ssize_t hotkey_poll_freq_show(struct device *dev,
2921 struct device_attribute *attr,
2922 char *buf)
2923{
2924 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2925}
2926
2927static ssize_t hotkey_poll_freq_store(struct device *dev,
2928 struct device_attribute *attr,
2929 const char *buf, size_t count)
2930{
2931 unsigned long t;
2932
2933 if (parse_strtoul(buf, 25, &t))
2934 return -EINVAL;
2935
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002936 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002937 return -ERESTARTSYS;
2938
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002939 hotkey_poll_set_freq(t);
2940 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002941
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002942 mutex_unlock(&hotkey_mutex);
2943
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002944 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2945
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002946 return count;
2947}
2948
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002949static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002950
2951#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2952
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002953/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002954static ssize_t hotkey_radio_sw_show(struct device *dev,
2955 struct device_attribute *attr,
2956 char *buf)
2957{
Johannes Berg19d337d2009-06-02 13:01:37 +02002958 int res;
2959 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002960 if (res < 0)
2961 return res;
2962
Johannes Berg19d337d2009-06-02 13:01:37 +02002963 /* Opportunistic update */
2964 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2965
2966 return snprintf(buf, PAGE_SIZE, "%d\n",
2967 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002968}
2969
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002970static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002971
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002972static void hotkey_radio_sw_notify_change(void)
2973{
2974 if (tp_features.hotkey_wlsw)
2975 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2976 "hotkey_radio_sw");
2977}
2978
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002979/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2980static ssize_t hotkey_tablet_mode_show(struct device *dev,
2981 struct device_attribute *attr,
2982 char *buf)
2983{
2984 int res, s;
2985 res = hotkey_get_tablet_mode(&s);
2986 if (res < 0)
2987 return res;
2988
2989 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2990}
2991
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002992static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002993
2994static void hotkey_tablet_mode_notify_change(void)
2995{
2996 if (tp_features.hotkey_tablet)
2997 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2998 "hotkey_tablet_mode");
2999}
3000
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003001/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003002static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3003 struct device_attribute *attr,
3004 char *buf)
3005{
3006 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3007}
3008
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003009static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003010
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003011static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003012{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003013 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3014 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003015}
3016
3017/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003018static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3019 struct device_attribute *attr,
3020 char *buf)
3021{
3022 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3023}
3024
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003025static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3026 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003027
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003028static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003029{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003030 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3031 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003032}
3033
Bastien Nocerab790cee2015-03-02 14:45:27 +01003034/* sysfs adaptive kbd mode --------------------------------------------- */
3035
3036static int adaptive_keyboard_get_mode(void);
3037static int adaptive_keyboard_set_mode(int new_mode);
3038
3039enum ADAPTIVE_KEY_MODE {
3040 HOME_MODE,
3041 WEB_BROWSER_MODE,
3042 WEB_CONFERENCE_MODE,
3043 FUNCTION_MODE,
3044 LAYFLAT_MODE
3045};
3046
3047static ssize_t adaptive_kbd_mode_show(struct device *dev,
3048 struct device_attribute *attr,
3049 char *buf)
3050{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003051 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01003052
3053 current_mode = adaptive_keyboard_get_mode();
3054 if (current_mode < 0)
3055 return current_mode;
3056
3057 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3058}
3059
3060static ssize_t adaptive_kbd_mode_store(struct device *dev,
3061 struct device_attribute *attr,
3062 const char *buf, size_t count)
3063{
3064 unsigned long t;
3065 int res;
3066
3067 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3068 return -EINVAL;
3069
3070 res = adaptive_keyboard_set_mode(t);
3071 return (res < 0) ? res : count;
3072}
3073
3074static DEVICE_ATTR_RW(adaptive_kbd_mode);
3075
3076static struct attribute *adaptive_kbd_attributes[] = {
3077 &dev_attr_adaptive_kbd_mode.attr,
3078 NULL
3079};
3080
3081static const struct attribute_group adaptive_kbd_attr_group = {
3082 .attrs = adaptive_kbd_attributes,
3083};
3084
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003085/* --------------------------------------------------------------------- */
3086
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003087static struct attribute *hotkey_attributes[] __initdata = {
3088 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003089 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003090 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003091 &dev_attr_wakeup_reason.attr,
3092 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003093 &dev_attr_hotkey_mask.attr,
3094 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003095 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003096 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003097#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003098 &dev_attr_hotkey_source_mask.attr,
3099 &dev_attr_hotkey_poll_freq.attr,
3100#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003101};
3102
Johannes Berg19d337d2009-06-02 13:01:37 +02003103/*
3104 * Sync both the hw and sw blocking state of all switches
3105 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003106static void tpacpi_send_radiosw_update(void)
3107{
3108 int wlsw;
3109
Johannes Berg19d337d2009-06-02 13:01:37 +02003110 /*
3111 * We must sync all rfkill controllers *before* issuing any
3112 * rfkill input events, or we will race the rfkill core input
3113 * handler.
3114 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003115 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003116 * for the above.
3117 *
3118 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3119 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003120
Johannes Berg19d337d2009-06-02 13:01:37 +02003121 wlsw = hotkey_get_wlsw();
3122
3123 /* Sync hw blocking state first if it is hw-blocked */
3124 if (wlsw == TPACPI_RFK_RADIO_OFF)
3125 tpacpi_rfk_update_hwblock_state(true);
3126
3127 /* Sync sw blocking state */
3128 tpacpi_rfk_update_swstate_all();
3129
3130 /* Sync hw blocking state last if it is hw-unblocked */
3131 if (wlsw == TPACPI_RFK_RADIO_ON)
3132 tpacpi_rfk_update_hwblock_state(false);
3133
3134 /* Issue rfkill input event for WLSW switch */
3135 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003136 mutex_lock(&tpacpi_inputdev_send_mutex);
3137
3138 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003139 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003140 input_sync(tpacpi_inputdev);
3141
3142 mutex_unlock(&tpacpi_inputdev_send_mutex);
3143 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003144
3145 /*
3146 * this can be unconditional, as we will poll state again
3147 * if userspace uses the notify to read data
3148 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003149 hotkey_radio_sw_notify_change();
3150}
3151
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003152static void hotkey_exit(void)
3153{
3154#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003155 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003156 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003157 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003158#endif
3159
3160 if (hotkey_dev_attributes)
3161 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3162
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003163 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003164 "restoring original HKEY status and mask\n");
3165 /* yes, there is a bitwise or below, we want the
3166 * functions to be called even if one of them fail */
3167 if (((tp_features.hotkey_mask &&
3168 hotkey_mask_set(hotkey_orig_mask)) |
3169 hotkey_status_set(false)) != 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003170 pr_err("failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003171}
3172
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003173static void __init hotkey_unmap(const unsigned int scancode)
3174{
3175 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3176 clear_bit(hotkey_keycode_map[scancode],
3177 tpacpi_inputdev->keybit);
3178 hotkey_keycode_map[scancode] = KEY_RESERVED;
3179 }
3180}
3181
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003182/*
3183 * HKEY quirks:
3184 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3185 */
3186
3187#define TPACPI_HK_Q_INIMASK 0x0001
3188
3189static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3190 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3191 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3192 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3193 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3194 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3195 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3196 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3197 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3198 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3199 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3200 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3201 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3202 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3203 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3204 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3205 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3206 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3207 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3208 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3209};
3210
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003211typedef u16 tpacpi_keymap_entry_t;
3212typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003213
Lyudeb3180022016-11-11 15:15:02 -05003214static int hotkey_init_tablet_mode(void)
3215{
Darren Hartcb2bf252016-12-14 14:45:48 -08003216 int in_tablet_mode = 0, res;
3217 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003218
Benjamin Bergdda3ec02017-09-15 15:20:49 +02003219 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3220 int has_tablet_mode;
3221
3222 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3223 &has_tablet_mode);
3224 if (has_tablet_mode)
3225 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3226 type = "GMMS";
3227 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003228 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003229 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3230 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3231 type = "MHKG";
3232 }
3233
3234 if (!tp_features.hotkey_tablet)
3235 return 0;
3236
3237 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3238 type, in_tablet_mode ? "tablet" : "laptop");
3239
3240 res = add_to_attr_set(hotkey_dev_attributes,
3241 &dev_attr_hotkey_tablet_mode.attr);
3242 if (res)
3243 return -1;
3244
3245 return in_tablet_mode;
3246}
3247
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003248static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003249{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003250 /* Requirements for changing the default keymaps:
3251 *
3252 * 1. Many of the keys are mapped to KEY_RESERVED for very
3253 * good reasons. Do not change them unless you have deep
3254 * knowledge on the IBM and Lenovo ThinkPad firmware for
3255 * the various ThinkPad models. The driver behaves
3256 * differently for KEY_RESERVED: such keys have their
3257 * hot key mask *unset* in mask_recommended, and also
3258 * in the initial hot key mask programmed into the
3259 * firmware at driver load time, which means the firm-
3260 * ware may react very differently if you change them to
3261 * something else;
3262 *
3263 * 2. You must be subscribed to the linux-thinkpad and
3264 * ibm-acpi-devel mailing lists, and you should read the
3265 * list archives since 2007 if you want to change the
3266 * keymaps. This requirement exists so that you will
3267 * know the past history of problems with the thinkpad-
3268 * acpi driver keymaps, and also that you will be
3269 * listening to any bug reports;
3270 *
3271 * 3. Do not send thinkpad-acpi specific patches directly to
3272 * for merging, *ever*. Send them to the linux-acpi
3273 * mailinglist for comments. Merging is to be done only
3274 * through acpi-test and the ACPI maintainer.
3275 *
3276 * If the above is too much to ask, don't change the keymap.
3277 * Ask the thinkpad-acpi maintainer to do it, instead.
3278 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003279
3280 enum keymap_index {
3281 TPACPI_KEYMAP_IBM_GENERIC = 0,
3282 TPACPI_KEYMAP_LENOVO_GENERIC,
3283 };
3284
3285 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3286 /* Generic keymap for IBM ThinkPads */
3287 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003288 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003289 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003290 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3291 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003292
3293 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003294 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3295 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3296 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003297
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003298 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003299 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003300 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003301
3302 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003303 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003304
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003305 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3306 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003307
3308 /* Volume: firmware always react to it and reprograms
3309 * the built-in *extra* mixer. Never map it to control
3310 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003311 KEY_RESERVED, /* 0x14: VOLUME UP */
3312 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3313 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003314
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003315 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003316
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003317 /* (assignments unknown, please report if found) */
3318 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3319 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003320
3321 /* No assignments, only used for Adaptive keyboards. */
3322 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3323 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3324 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3325 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3326 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003327
3328 /* No assignment, used for newer Lenovo models */
3329 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3330 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3331 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3332 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3333 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3334 KEY_UNKNOWN, KEY_UNKNOWN
3335
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003336 },
3337
3338 /* Generic keymap for Lenovo ThinkPads */
3339 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003340 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3341 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003342 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003343 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003344
3345 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003346 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3347 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3348 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003349
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003350 /* These should be enabled --only-- when ACPI video
3351 * is disabled (i.e. in "vendor" mode), and are handled
3352 * in a special way by the init code */
3353 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3354 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003355
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003356 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003357
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003358 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3359 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003360
3361 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3362 * react to it and reprograms the built-in *extra* mixer.
3363 * Never map it to control another mixer by default.
3364 *
3365 * T60?, T61, R60?, R61: firmware and EC tries to send
3366 * these over the regular keyboard, so these are no-ops,
3367 * but there are still weird bugs re. MUTE, so do not
3368 * change unless you get test reports from all Lenovo
3369 * models. May cause the BIOS to interfere with the
3370 * HDA mixer.
3371 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003372 KEY_RESERVED, /* 0x14: VOLUME UP */
3373 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3374 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003375
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003376 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003377
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003378 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003379 KEY_UNKNOWN, KEY_UNKNOWN,
3380
3381 /*
3382 * The mic mute button only sends 0x1a. It does not
3383 * automatically mute the mic or change the mute light.
3384 */
3385 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3386
3387 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003388 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003389
3390 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003391 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003392
3393 /*
3394 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3395 * The first item in this list is the Mute button which is
3396 * emitted with 0x103 through
3397 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3398 * symbol is held.
3399 * We'll need to offset those by 0x20.
3400 */
3401 KEY_RESERVED, /* Mute held, 0x103 */
3402 KEY_BRIGHTNESS_MIN, /* Backlight off */
3403 KEY_RESERVED, /* Clipping tool */
3404 KEY_RESERVED, /* Cloud */
3405 KEY_RESERVED,
3406 KEY_VOICECOMMAND, /* Voice */
3407 KEY_RESERVED,
3408 KEY_RESERVED, /* Gestures */
3409 KEY_RESERVED,
3410 KEY_RESERVED,
3411 KEY_RESERVED,
3412 KEY_CONFIG, /* Settings */
3413 KEY_RESERVED, /* New tab */
3414 KEY_REFRESH, /* Reload */
3415 KEY_BACK, /* Back */
3416 KEY_RESERVED, /* Microphone down */
3417 KEY_RESERVED, /* Microphone up */
3418 KEY_RESERVED, /* Microphone cancellation */
3419 KEY_RESERVED, /* Camera mode */
3420 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003421
3422 /*
3423 * These are found in 2017 models (e.g. T470s, X270).
3424 * The lowest known value is 0x311, which according to
3425 * the manual should launch a user defined favorite
3426 * application.
3427 *
3428 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3429 * corresponding to 0x34.
3430 */
3431
3432 /* (assignments unknown, please report if found) */
3433 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3434 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3435 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3436 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3437 KEY_UNKNOWN,
3438
3439 KEY_FAVORITES, /* Favorite app, 0x311 */
3440 KEY_RESERVED, /* Clipping tool */
3441 KEY_RESERVED,
3442 KEY_BLUETOOTH, /* Bluetooth */
3443 KEY_KEYBOARD /* Keyboard, 0x315 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003444 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003445 };
3446
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003447 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3448 /* Generic maps (fallback) */
3449 {
3450 .vendor = PCI_VENDOR_ID_IBM,
3451 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3452 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3453 },
3454 {
3455 .vendor = PCI_VENDOR_ID_LENOVO,
3456 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3457 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3458 },
3459 };
3460
3461#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003462#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003463
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003464 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003465 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003466 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003467 bool radiosw_state = false;
3468 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003469
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003470 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003471 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003472
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003473 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3474 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003475
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003476 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003477 BUG_ON(tpacpi_inputdev->open != NULL ||
3478 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003479
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003480 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003481 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003482
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003483#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003484 mutex_init(&hotkey_thread_data_mutex);
3485#endif
3486
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003487 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003488 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003489
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003490 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3491 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003492 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003493
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003494 if (!tp_features.hotkey)
3495 return 1;
3496
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003497 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3498 ARRAY_SIZE(tpacpi_hotkey_qtable));
3499
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003500 tpacpi_disable_brightness_delay();
3501
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003502 /* MUST have enough space for all attributes to be added to
3503 * hotkey_dev_attributes */
3504 hotkey_dev_attributes = create_attr_set(
3505 ARRAY_SIZE(hotkey_attributes) + 2,
3506 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003507 if (!hotkey_dev_attributes)
3508 return -ENOMEM;
3509 res = add_many_to_attr_set(hotkey_dev_attributes,
3510 hotkey_attributes,
3511 ARRAY_SIZE(hotkey_attributes));
3512 if (res)
3513 goto err_exit;
3514
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003515 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003516 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3517 for HKEY interface version 0x100 */
3518 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003519 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3520 "firmware HKEY interface version: 0x%x\n",
3521 hkeyv);
3522
3523 switch (hkeyv >> 8) {
3524 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003525 /*
3526 * MHKV 0x100 in A31, R40, R40e,
3527 * T4x, X31, and later
3528 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003529
3530 /* Paranoia check AND init hotkey_all_mask */
3531 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3532 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003533 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003534 TPACPI_MAIL);
3535 /* Fallback: pre-init for FN+F3,F4,F12 */
3536 hotkey_all_mask = 0x080cU;
3537 } else {
3538 tp_features.hotkey_mask = 1;
3539 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003540 break;
3541
3542 case 2:
3543 /*
3544 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3545 */
3546
3547 /* Paranoia check AND init hotkey_all_mask */
3548 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3549 "MHKA", "dd", 1)) {
3550 pr_err("missing MHKA handler, please report this to %s\n",
3551 TPACPI_MAIL);
3552 /* Fallback: pre-init for FN+F3,F4,F12 */
3553 hotkey_all_mask = 0x080cU;
3554 } else {
3555 tp_features.hotkey_mask = 1;
3556 }
3557
3558 /*
3559 * Check if we have an adaptive keyboard, like on the
3560 * Lenovo Carbon X1 2014 (2nd Gen).
3561 */
3562 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3563 "MHKA", "dd", 2)) {
3564 if (hotkey_adaptive_all_mask != 0) {
3565 tp_features.has_adaptive_kbd = true;
3566 res = sysfs_create_group(
3567 &tpacpi_pdev->dev.kobj,
3568 &adaptive_kbd_attr_group);
3569 if (res)
3570 goto err_exit;
3571 }
3572 } else {
3573 tp_features.has_adaptive_kbd = false;
3574 hotkey_adaptive_all_mask = 0x0U;
3575 }
3576 break;
3577
3578 default:
3579 pr_err("unknown version of the HKEY interface: 0x%x\n",
3580 hkeyv);
3581 pr_err("please report this to %s\n", TPACPI_MAIL);
3582 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003583 }
3584 }
3585
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003586 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3587 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003588 str_supported(tp_features.hotkey_mask));
3589
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003590 /* Init hotkey_all_mask if not initialized yet */
3591 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3592 (quirks & TPACPI_HK_Q_INIMASK))
3593 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003594
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003595 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003596 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003597 /* hotkey_source_mask *must* be zero for
3598 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003599 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003600 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003601 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003602
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003603 hotkey_orig_mask = hotkey_acpi_mask;
3604 } else {
3605 hotkey_orig_mask = hotkey_all_mask;
3606 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003607 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003608
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003609#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3610 if (dbg_wlswemul) {
3611 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003612 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003613 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003614 } else
3615#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003616 /* Not all thinkpads have a hardware radio switch */
3617 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3618 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003619 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003620 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003621 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003622 }
3623 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003624 res = add_to_attr_set(hotkey_dev_attributes,
3625 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003626
Lyudeb3180022016-11-11 15:15:02 -05003627 res = hotkey_init_tablet_mode();
3628 if (res < 0)
3629 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003630
Lyudeb3180022016-11-11 15:15:02 -05003631 tabletsw_state = res;
3632
3633 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3634 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003635 if (res)
3636 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003637
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003638 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003639 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3640 GFP_KERNEL);
3641 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003642 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003643 res = -ENOMEM;
3644 goto err_exit;
3645 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003646
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003647 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3648 ARRAY_SIZE(tpacpi_keymap_qtable));
3649 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3650 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3651 "using keymap number %lu\n", keymap_id);
3652
3653 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3654 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003655
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003656 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003657 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3658 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3659 tpacpi_inputdev->keycode = hotkey_keycode_map;
3660 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3661 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003662 input_set_capability(tpacpi_inputdev, EV_KEY,
3663 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003664 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003665 if (i < sizeof(hotkey_reserved_mask)*8)
3666 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003667 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003668 }
3669
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003670 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003671 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003672 input_report_switch(tpacpi_inputdev,
3673 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003674 }
3675 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003676 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003677 input_report_switch(tpacpi_inputdev,
3678 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003679 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003680
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003681 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003682 * userspace. tpacpi_detect_brightness_capabilities() must have
3683 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003684 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003685 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3686 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003687
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003688 /* Disable brightness up/down on Lenovo thinkpads when
3689 * ACPI is handling them, otherwise it is plain impossible
3690 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003691 hotkey_reserved_mask |=
3692 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3693 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003694 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3695 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003696 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003697
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003698#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003699 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3700 & ~hotkey_all_mask
3701 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003702
3703 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3704 "hotkey source mask 0x%08x, polling freq %u\n",
3705 hotkey_source_mask, hotkey_poll_freq);
3706#endif
3707
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003708 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3709 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003710 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003711 if (res) {
3712 hotkey_exit();
3713 return res;
3714 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003715 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3716 | hotkey_driver_mask)
3717 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003718 if (res < 0 && res != -ENXIO) {
3719 hotkey_exit();
3720 return res;
3721 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003722 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3723 & ~hotkey_reserved_mask;
3724 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3725 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3726 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003727
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003728 tpacpi_inputdev->open = &hotkey_inputdev_open;
3729 tpacpi_inputdev->close = &hotkey_inputdev_close;
3730
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003731 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003732
3733 return 0;
3734
3735err_exit:
3736 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003737 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3738 &adaptive_kbd_attr_group);
3739
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003740 hotkey_dev_attributes = NULL;
3741
Jan van den Berg72a979f2014-09-17 00:01:08 +02003742 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003743}
3744
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003745/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3746 * mode, Web conference mode, Function mode and Lay-flat mode.
3747 * We support Home mode and Function mode currently.
3748 *
3749 * Will consider support rest of modes in future.
3750 *
3751 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003752static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003753 HOME_MODE,
3754/* WEB_BROWSER_MODE = 2,
3755 WEB_CONFERENCE_MODE = 3, */
3756 FUNCTION_MODE
3757};
3758
3759#define DFR_CHANGE_ROW 0x101
3760#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003761#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003762
3763/* press Fn key a while second, it will switch to Function Mode. Then
3764 * release Fn key, previous mode be restored.
3765 */
3766static bool adaptive_keyboard_mode_is_saved;
3767static int adaptive_keyboard_prev_mode;
3768
Bastien Noceraf74587f2015-03-02 14:45:22 +01003769static int adaptive_keyboard_get_mode(void)
3770{
3771 int mode = 0;
3772
3773 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3774 pr_err("Cannot read adaptive keyboard mode\n");
3775 return -EIO;
3776 }
3777
3778 return mode;
3779}
3780
3781static int adaptive_keyboard_set_mode(int new_mode)
3782{
3783 if (new_mode < 0 ||
3784 new_mode > LAYFLAT_MODE)
3785 return -EINVAL;
3786
3787 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3788 pr_err("Cannot set adaptive keyboard mode\n");
3789 return -EIO;
3790 }
3791
3792 return 0;
3793}
3794
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003795static int adaptive_keyboard_get_next_mode(int mode)
3796{
3797 size_t i;
3798 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3799
3800 for (i = 0; i <= max_mode; i++) {
3801 if (adaptive_keyboard_modes[i] == mode)
3802 break;
3803 }
3804
3805 if (i >= max_mode)
3806 i = 0;
3807 else
3808 i++;
3809
3810 return adaptive_keyboard_modes[i];
3811}
3812
3813static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3814{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003815 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003816 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003817 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003818
3819 switch (scancode) {
3820 case DFR_CHANGE_ROW:
3821 if (adaptive_keyboard_mode_is_saved) {
3822 new_mode = adaptive_keyboard_prev_mode;
3823 adaptive_keyboard_mode_is_saved = false;
3824 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003825 current_mode = adaptive_keyboard_get_mode();
3826 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003827 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003828 new_mode = adaptive_keyboard_get_next_mode(
3829 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003830 }
3831
Bastien Noceraf74587f2015-03-02 14:45:22 +01003832 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003833 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003834
3835 return true;
3836
3837 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003838 current_mode = adaptive_keyboard_get_mode();
3839 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003840 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003841
Bastien Noceraf74587f2015-03-02 14:45:22 +01003842 adaptive_keyboard_prev_mode = current_mode;
3843 adaptive_keyboard_mode_is_saved = true;
3844
3845 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3846 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003847 return true;
3848
3849 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003850 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003851 scancode >= FIRST_ADAPTIVE_KEY +
3852 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3853 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003854 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003855 scancode);
Bastien Nocera6a68d852015-03-02 14:45:31 +01003856 return false;
3857 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003858 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3859 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003860 if (keycode != KEY_RESERVED) {
3861 mutex_lock(&tpacpi_inputdev_send_mutex);
3862
3863 input_report_key(tpacpi_inputdev, keycode, 1);
3864 input_sync(tpacpi_inputdev);
3865
3866 input_report_key(tpacpi_inputdev, keycode, 0);
3867 input_sync(tpacpi_inputdev);
3868
3869 mutex_unlock(&tpacpi_inputdev_send_mutex);
3870 }
3871 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003872 }
3873}
3874
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003875static bool hotkey_notify_hotkey(const u32 hkey,
3876 bool *send_acpi_ev,
3877 bool *ignore_acpi_ev)
3878{
3879 /* 0x1000-0x1FFF: key presses */
3880 unsigned int scancode = hkey & 0xfff;
3881 *send_acpi_ev = true;
3882 *ignore_acpi_ev = false;
3883
Christian Kellner696c65232017-02-28 17:10:57 +01003884 /*
3885 * Original events are in the 0x10XX range, the adaptive keyboard
3886 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3887 * models, additional keys are emitted through 0x13XX.
3888 */
3889 switch ((hkey >> 8) & 0xf) {
3890 case 0:
3891 if (scancode > 0 &&
3892 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3893 /* HKEY event 0x1001 is scancode 0x00 */
3894 scancode--;
3895 if (!(hotkey_source_mask & (1 << scancode))) {
3896 tpacpi_input_send_key_masked(scancode);
3897 *send_acpi_ev = false;
3898 } else {
3899 *ignore_acpi_ev = true;
3900 }
3901 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003902 }
Christian Kellner696c65232017-02-28 17:10:57 +01003903 break;
3904
3905 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003906 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003907
3908 case 3:
3909 /* Extended keycodes start at 0x300 and our offset into the map
3910 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3911 * will be positive, but might not be in the correct range.
3912 */
3913 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3914 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3915 scancode < TPACPI_HOTKEY_MAP_LEN) {
3916 tpacpi_input_send_key(scancode);
3917 return true;
3918 }
3919 break;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003920 }
Christian Kellner696c65232017-02-28 17:10:57 +01003921
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003922 return false;
3923}
3924
3925static bool hotkey_notify_wakeup(const u32 hkey,
3926 bool *send_acpi_ev,
3927 bool *ignore_acpi_ev)
3928{
3929 /* 0x2000-0x2FFF: Wakeup reason */
3930 *send_acpi_ev = true;
3931 *ignore_acpi_ev = false;
3932
3933 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003934 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3935 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003936 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3937 *ignore_acpi_ev = true;
3938 break;
3939
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003940 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3941 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003942 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3943 *ignore_acpi_ev = true;
3944 break;
3945
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003946 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3947 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003948 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003949 /* how to auto-heal: */
3950 /* 2313: woke up from S3, go to S4/S5 */
3951 /* 2413: woke up from S4, go to S5 */
3952 break;
3953
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003954 default:
3955 return false;
3956 }
3957
3958 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003959 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003960 hotkey_wakeup_reason_notify_change();
3961 }
3962 return true;
3963}
3964
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003965static bool hotkey_notify_dockevent(const u32 hkey,
3966 bool *send_acpi_ev,
3967 bool *ignore_acpi_ev)
3968{
3969 /* 0x4000-0x4FFF: dock-related events */
3970 *send_acpi_ev = true;
3971 *ignore_acpi_ev = false;
3972
3973 switch (hkey) {
3974 case TP_HKEY_EV_UNDOCK_ACK:
3975 /* ACPI undock operation completed after wakeup */
3976 hotkey_autosleep_ack = 1;
3977 pr_info("undocked\n");
3978 hotkey_wakeup_hotunplug_complete_notify_change();
3979 return true;
3980
3981 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3982 pr_info("docked into hotplug port replicator\n");
3983 return true;
3984 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3985 pr_info("undocked from hotplug port replicator\n");
3986 return true;
3987
3988 default:
3989 return false;
3990 }
3991}
3992
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003993static bool hotkey_notify_usrevent(const u32 hkey,
3994 bool *send_acpi_ev,
3995 bool *ignore_acpi_ev)
3996{
3997 /* 0x5000-0x5FFF: human interface helpers */
3998 *send_acpi_ev = true;
3999 *ignore_acpi_ev = false;
4000
4001 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004002 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4003 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004004 return true;
4005
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004006 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4007 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004008 tpacpi_input_send_tabletsw();
4009 hotkey_tablet_mode_notify_change();
4010 *send_acpi_ev = false;
4011 return true;
4012
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004013 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4014 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4015 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03004016 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004017 *ignore_acpi_ev = true;
4018 return true;
4019
4020 default:
4021 return false;
4022 }
4023}
4024
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004025static void thermal_dump_all_sensors(void);
4026
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004027static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004028 bool *send_acpi_ev,
4029 bool *ignore_acpi_ev)
4030{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004031 bool known = true;
4032
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004033 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004034 *send_acpi_ev = true;
4035 *ignore_acpi_ev = false;
4036
4037 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004038 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07004039 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02004040 /* recommended action: do nothing, we don't have
4041 * Lenovo ATM information */
4042 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004043 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07004044 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004045 /* recommended action: warn user through gui */
4046 break;
4047 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07004048 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004049 /* recommended action: immediate sleep/hibernate */
4050 break;
4051 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004052 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004053 /* recommended action: warn user through gui, that */
4054 /* some internal component is too hot */
4055 break;
4056 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004057 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004058 /* recommended action: immediate sleep/hibernate */
4059 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01004060 case TP_HKEY_EV_AC_CHANGED:
4061 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4062 * AC status changed; can be triggered by plugging or
4063 * unplugging AC adapter, docking or undocking. */
4064
4065 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004066
4067 case TP_HKEY_EV_KEY_NUMLOCK:
4068 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01004069 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004070 /* key press events, we just ignore them as long as the EC
4071 * is still reporting them in the normal keyboard stream */
4072 *send_acpi_ev = false;
4073 *ignore_acpi_ev = true;
4074 return true;
4075
Lyudeb03f4d42016-11-11 15:15:03 -05004076 case TP_HKEY_EV_TABLET_CHANGED:
4077 tpacpi_input_send_tabletsw();
4078 hotkey_tablet_mode_notify_change();
4079 *send_acpi_ev = false;
4080 break;
4081
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004082 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004083 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004084 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004085 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004086
4087 thermal_dump_all_sensors();
4088
4089 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004090}
4091
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004092static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4093{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004094 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004095 bool send_acpi_ev;
4096 bool ignore_acpi_ev;
4097 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004098
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004099 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07004100 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004101 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004102 acpi_bus_generate_netlink_event(
4103 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004104 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004105 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004106 return;
4107 }
4108
4109 while (1) {
4110 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004111 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004112 return;
4113 }
4114
4115 if (hkey == 0) {
4116 /* queue empty */
4117 return;
4118 }
4119
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004120 send_acpi_ev = true;
4121 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004122
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004123 switch (hkey >> 12) {
4124 case 1:
4125 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004126 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4127 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004128 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004129 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004130 /* 0x2000-0x2FFF: Wakeup reason */
4131 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4132 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004133 break;
4134 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004135 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004136 switch (hkey) {
4137 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004138 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004139 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004140 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004141 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004142 break;
4143 case TP_HKEY_EV_OPTDRV_EJ:
4144 /* FIXME: kick libata if SATA link offline */
4145 known_ev = true;
4146 break;
4147 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004148 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004149 }
4150 break;
4151 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004152 /* 0x4000-0x4FFF: dock-related events */
4153 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4154 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004155 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004156 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004157 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004158 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4159 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004160 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004161 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004162 /* 0x6000-0x6FFF: thermal alarms/notices and
4163 * keyboard events */
4164 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004165 &ignore_acpi_ev);
4166 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004167 case 7:
4168 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004169 if (tp_features.hotkey_wlsw &&
4170 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004171 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004172 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004173 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004174 break;
4175 }
4176 /* fallthrough to default */
4177 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004178 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004179 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004180 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004181 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004182 pr_notice("please report the conditions when this event happened to %s\n",
4183 TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004184 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004185
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004186 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004187 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004188 acpi_bus_generate_netlink_event(
4189 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004190 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004191 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004192 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004193 }
4194}
4195
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004196static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004197{
4198 /* Do these on suspend, we get the events on early resume! */
4199 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4200 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004201
4202 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004203 if (tp_features.has_adaptive_kbd) {
4204 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4205 "GTRW", "dd", 0)) {
4206 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004207 }
4208 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004209}
4210
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004211static void hotkey_resume(void)
4212{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03004213 tpacpi_disable_brightness_delay();
4214
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004215 if (hotkey_status_set(true) < 0 ||
4216 hotkey_mask_set(hotkey_acpi_mask) < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004217 pr_err("error while attempting to reset the event firmware interface\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004218
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004219 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004220 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004221 hotkey_wakeup_reason_notify_change();
4222 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004223 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004224
4225 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004226 if (tp_features.has_adaptive_kbd) {
4227 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4228 adaptive_keyboard_prev_mode)) {
4229 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004230 }
4231 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004232}
4233
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004234/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004235static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004237 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004239 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004240 seq_printf(m, "status:\t\tnot supported\n");
4241 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004242 }
4243
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004244 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004245 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004246 res = hotkey_status_get(&status);
4247 if (!res)
4248 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004249 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004250 if (res)
4251 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004253 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004254 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004255 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4256 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004258 seq_printf(m, "mask:\t\tnot supported\n");
4259 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
4261
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263}
4264
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004265static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004266{
4267 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004268 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004269 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4270 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004271}
4272
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004273static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004275 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004276 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004279 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 return -ENODEV;
4281
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004282 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004283 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004284
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004285 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004286
4287 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 while ((cmd = next_cmd(&buf))) {
4289 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004290 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004292 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004293 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004295 mask = (hotkey_all_mask | hotkey_source_mask)
4296 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4298 /* mask set */
4299 } else if (sscanf(cmd, "%x", &mask) == 1) {
4300 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004301 } else {
4302 res = -EINVAL;
4303 goto errexit;
4304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004306
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004307 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004308 tpacpi_disclose_usertask("procfs hotkey",
4309 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004310 res = hotkey_user_mask_set(mask);
4311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004313errexit:
4314 mutex_unlock(&hotkey_mutex);
4315 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004316}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004318static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004319 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4320 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004321 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004322 {"", 0},
4323};
4324
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004325static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004326 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004327 .notify = hotkey_notify,
4328 .handle = &hkey_handle,
4329 .type = ACPI_DEVICE_NOTIFY,
4330};
4331
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004332static struct ibm_struct hotkey_driver_data = {
4333 .name = "hotkey",
4334 .read = hotkey_read,
4335 .write = hotkey_write,
4336 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004337 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004338 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004339 .acpi = &ibm_hotkey_acpidriver,
4340};
4341
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004342/*************************************************************************
4343 * Bluetooth subdriver
4344 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004346enum {
4347 /* ACPI GBDC/SBDC bits */
4348 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4349 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004350 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004351 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004352};
4353
4354enum {
4355 /* ACPI \BLTH commands */
4356 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4357 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4358 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4359 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4360 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004361};
4362
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004363#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4364
Johannes Berg19d337d2009-06-02 13:01:37 +02004365static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004366{
4367 int status;
4368
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004369#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4370 if (dbg_bluetoothemul)
4371 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004372 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004373#endif
4374
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004375 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4376 return -EIO;
4377
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004378 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004379 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004380}
4381
Johannes Berg19d337d2009-06-02 13:01:37 +02004382static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004383{
4384 int status;
4385
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004386 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004387 "will attempt to %s bluetooth\n",
4388 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004389
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004390#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4391 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004392 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004393 return 0;
4394 }
4395#endif
4396
Johannes Berg19d337d2009-06-02 13:01:37 +02004397 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004398 status = TP_ACPI_BLUETOOTH_RADIOSSW
4399 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4400 else
4401 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004402
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004403 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4404 return -EIO;
4405
4406 return 0;
4407}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004408
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004409/* sysfs bluetooth enable ---------------------------------------------- */
4410static ssize_t bluetooth_enable_show(struct device *dev,
4411 struct device_attribute *attr,
4412 char *buf)
4413{
Johannes Berg19d337d2009-06-02 13:01:37 +02004414 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4415 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004416}
4417
4418static ssize_t bluetooth_enable_store(struct device *dev,
4419 struct device_attribute *attr,
4420 const char *buf, size_t count)
4421{
Johannes Berg19d337d2009-06-02 13:01:37 +02004422 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4423 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004424}
4425
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004426static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004427
4428/* --------------------------------------------------------------------- */
4429
4430static struct attribute *bluetooth_attributes[] = {
4431 &dev_attr_bluetooth_enable.attr,
4432 NULL
4433};
4434
4435static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004436 .attrs = bluetooth_attributes,
4437};
4438
Johannes Berg19d337d2009-06-02 13:01:37 +02004439static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4440 .get_status = bluetooth_get_status,
4441 .set_status = bluetooth_set_status,
4442};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004443
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004444static void bluetooth_shutdown(void)
4445{
4446 /* Order firmware to save current state to NVRAM */
4447 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4448 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004449 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004450 else
4451 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004452 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004453}
4454
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004455static void bluetooth_exit(void)
4456{
4457 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4458 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004459
4460 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4461
4462 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004463}
4464
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004465static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004467 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004468 int status = 0;
4469
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004470 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4471 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004472
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004473 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004474
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004475 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4476 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004477 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004478 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004480 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4481 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004482 str_supported(tp_features.bluetooth),
4483 status);
4484
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004485#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4486 if (dbg_bluetoothemul) {
4487 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004488 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004489 } else
4490#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004491 if (tp_features.bluetooth &&
4492 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4493 /* no bluetooth hardware present in system */
4494 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004495 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004496 "bluetooth hardware not installed\n");
4497 }
4498
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004499 if (!tp_features.bluetooth)
4500 return 1;
4501
Johannes Berg19d337d2009-06-02 13:01:37 +02004502 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4503 &bluetooth_tprfk_ops,
4504 RFKILL_TYPE_BLUETOOTH,
4505 TPACPI_RFK_BLUETOOTH_SW_NAME,
4506 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004507 if (res)
4508 return res;
4509
Johannes Berg19d337d2009-06-02 13:01:37 +02004510 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4511 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004512 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004513 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004514 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004515 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004516
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518}
4519
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004520/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004521static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004523 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524}
4525
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004526static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
Johannes Berg19d337d2009-06-02 13:01:37 +02004528 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529}
4530
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004531static struct ibm_struct bluetooth_driver_data = {
4532 .name = "bluetooth",
4533 .read = bluetooth_read,
4534 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004535 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004536 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004537};
4538
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004539/*************************************************************************
4540 * Wan subdriver
4541 */
4542
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004543enum {
4544 /* ACPI GWAN/SWAN bits */
4545 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4546 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004547 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004548 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004549};
4550
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004551#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4552
Johannes Berg19d337d2009-06-02 13:01:37 +02004553static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004554{
4555 int status;
4556
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004557#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4558 if (dbg_wwanemul)
4559 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004560 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004561#endif
4562
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004563 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4564 return -EIO;
4565
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004566 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004567 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004568}
4569
Johannes Berg19d337d2009-06-02 13:01:37 +02004570static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004571{
4572 int status;
4573
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004574 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004575 "will attempt to %s wwan\n",
4576 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004577
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004578#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4579 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004580 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004581 return 0;
4582 }
4583#endif
4584
Johannes Berg19d337d2009-06-02 13:01:37 +02004585 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004586 status = TP_ACPI_WANCARD_RADIOSSW
4587 | TP_ACPI_WANCARD_RESUMECTRL;
4588 else
4589 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004590
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004591 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4592 return -EIO;
4593
4594 return 0;
4595}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004596
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004597/* sysfs wan enable ---------------------------------------------------- */
4598static ssize_t wan_enable_show(struct device *dev,
4599 struct device_attribute *attr,
4600 char *buf)
4601{
Johannes Berg19d337d2009-06-02 13:01:37 +02004602 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4603 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004604}
4605
4606static ssize_t wan_enable_store(struct device *dev,
4607 struct device_attribute *attr,
4608 const char *buf, size_t count)
4609{
Johannes Berg19d337d2009-06-02 13:01:37 +02004610 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4611 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004612}
4613
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004614static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4615 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004616
4617/* --------------------------------------------------------------------- */
4618
4619static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004620 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004621 NULL
4622};
4623
4624static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004625 .attrs = wan_attributes,
4626};
4627
Johannes Berg19d337d2009-06-02 13:01:37 +02004628static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4629 .get_status = wan_get_status,
4630 .set_status = wan_set_status,
4631};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004632
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004633static void wan_shutdown(void)
4634{
4635 /* Order firmware to save current state to NVRAM */
4636 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4637 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004638 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004639 else
4640 vdbg_printk(TPACPI_DBG_RFKILL,
4641 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004642}
4643
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004644static void wan_exit(void)
4645{
4646 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4647 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004648
4649 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4650
4651 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004652}
4653
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004654static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004655{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004656 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004657 int status = 0;
4658
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004659 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4660 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004661
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004662 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004663
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004664 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004665 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004666
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004667 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4668 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004669 str_supported(tp_features.wan),
4670 status);
4671
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004672#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4673 if (dbg_wwanemul) {
4674 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004675 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004676 } else
4677#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004678 if (tp_features.wan &&
4679 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4680 /* no wan hardware present in system */
4681 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004682 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004683 "wan hardware not installed\n");
4684 }
4685
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004686 if (!tp_features.wan)
4687 return 1;
4688
Johannes Berg19d337d2009-06-02 13:01:37 +02004689 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4690 &wan_tprfk_ops,
4691 RFKILL_TYPE_WWAN,
4692 TPACPI_RFK_WWAN_SW_NAME,
4693 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004694 if (res)
4695 return res;
4696
Johannes Berg19d337d2009-06-02 13:01:37 +02004697 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4698 &wan_attr_group);
4699
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004700 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004701 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004702 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004703 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004704
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004705 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004706}
4707
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004708/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004709static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004710{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004711 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004712}
4713
4714static int wan_write(char *buf)
4715{
Johannes Berg19d337d2009-06-02 13:01:37 +02004716 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004717}
4718
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004719static struct ibm_struct wan_driver_data = {
4720 .name = "wan",
4721 .read = wan_read,
4722 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004723 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004724 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004725};
4726
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004727/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004728 * UWB subdriver
4729 */
4730
4731enum {
4732 /* ACPI GUWB/SUWB bits */
4733 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4734 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4735};
4736
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004737#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4738
Johannes Berg19d337d2009-06-02 13:01:37 +02004739static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004740{
4741 int status;
4742
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004743#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4744 if (dbg_uwbemul)
4745 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004746 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004747#endif
4748
4749 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4750 return -EIO;
4751
4752 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004753 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004754}
4755
Johannes Berg19d337d2009-06-02 13:01:37 +02004756static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004757{
4758 int status;
4759
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004760 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004761 "will attempt to %s UWB\n",
4762 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004763
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004764#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4765 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004766 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004767 return 0;
4768 }
4769#endif
4770
Johannes Berg19d337d2009-06-02 13:01:37 +02004771 if (state == TPACPI_RFK_RADIO_ON)
4772 status = TP_ACPI_UWB_RADIOSSW;
4773 else
4774 status = 0;
4775
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004776 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4777 return -EIO;
4778
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004779 return 0;
4780}
4781
4782/* --------------------------------------------------------------------- */
4783
Johannes Berg19d337d2009-06-02 13:01:37 +02004784static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4785 .get_status = uwb_get_status,
4786 .set_status = uwb_set_status,
4787};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004788
4789static void uwb_exit(void)
4790{
Johannes Berg19d337d2009-06-02 13:01:37 +02004791 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004792}
4793
4794static int __init uwb_init(struct ibm_init_struct *iibm)
4795{
4796 int res;
4797 int status = 0;
4798
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004799 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4800 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004801
4802 TPACPI_ACPIHANDLE_INIT(hkey);
4803
4804 tp_features.uwb = hkey_handle &&
4805 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4806
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004807 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4808 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004809 str_supported(tp_features.uwb),
4810 status);
4811
4812#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4813 if (dbg_uwbemul) {
4814 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004815 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004816 } else
4817#endif
4818 if (tp_features.uwb &&
4819 !(status & TP_ACPI_UWB_HWPRESENT)) {
4820 /* no uwb hardware present in system */
4821 tp_features.uwb = 0;
4822 dbg_printk(TPACPI_DBG_INIT,
4823 "uwb hardware not installed\n");
4824 }
4825
4826 if (!tp_features.uwb)
4827 return 1;
4828
4829 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004830 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004831 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004832 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004833 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004834 return res;
4835}
4836
4837static struct ibm_struct uwb_driver_data = {
4838 .name = "uwb",
4839 .exit = uwb_exit,
4840 .flags.experimental = 1,
4841};
4842
4843/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004844 * Video subdriver
4845 */
4846
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004847#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4848
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004849enum video_access_mode {
4850 TPACPI_VIDEO_NONE = 0,
4851 TPACPI_VIDEO_570, /* 570 */
4852 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4853 TPACPI_VIDEO_NEW, /* all others */
4854};
4855
4856enum { /* video status flags, based on VIDEO_570 */
4857 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4858 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4859 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4860};
4861
4862enum { /* TPACPI_VIDEO_570 constants */
4863 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4864 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4865 * video_status_flags */
4866 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4867 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4868};
4869
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004870static enum video_access_mode video_supported;
4871static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004872
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004873static int video_autosw_get(void);
4874static int video_autosw_set(int enable);
4875
Joe Perches112a6ee2011-04-04 10:06:24 -07004876TPACPI_HANDLE(vid, root,
4877 "\\_SB.PCI.AGP.VGA", /* 570 */
4878 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4879 "\\_SB.PCI0.VID0", /* 770e */
4880 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4881 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4882 "\\_SB.PCI0.AGP.VID", /* all others */
4883 ); /* R30, R31 */
4884
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004885TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004886
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004887static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004889 int ivga;
4890
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004891 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4892
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004893 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004894 if (tpacpi_is_ibm())
4895 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004896
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004897 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4898 /* G41, assume IVGA doesn't change */
4899 vid_handle = vid2_handle;
4900
4901 if (!vid_handle)
4902 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004903 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004904 else if (tpacpi_is_ibm() &&
4905 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004906 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004907 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004908 else if (tpacpi_is_ibm() &&
4909 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004910 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004911 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004912 else
4913 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004914 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004916 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4917 str_supported(video_supported != TPACPI_VIDEO_NONE),
4918 video_supported);
4919
Jan van den Berg72a979f2014-09-17 00:01:08 +02004920 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921}
4922
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004923static void video_exit(void)
4924{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004925 dbg_printk(TPACPI_DBG_EXIT,
4926 "restoring original video autoswitch mode\n");
4927 if (video_autosw_set(video_orig_autosw))
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004928 pr_err("error while trying to restore original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004929}
4930
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004931static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
4933 int status = 0;
4934 int i;
4935
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004936 switch (video_supported) {
4937 case TPACPI_VIDEO_570:
4938 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4939 TP_ACPI_VIDEO_570_PHSCMD))
4940 return -EIO;
4941 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4942 break;
4943 case TPACPI_VIDEO_770:
4944 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4945 return -EIO;
4946 if (i)
4947 status |= TP_ACPI_VIDEO_S_LCD;
4948 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4949 return -EIO;
4950 if (i)
4951 status |= TP_ACPI_VIDEO_S_CRT;
4952 break;
4953 case TPACPI_VIDEO_NEW:
4954 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4955 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4956 return -EIO;
4957 if (i)
4958 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004960 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4961 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4962 return -EIO;
4963 if (i)
4964 status |= TP_ACPI_VIDEO_S_LCD;
4965 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4966 return -EIO;
4967 if (i)
4968 status |= TP_ACPI_VIDEO_S_DVI;
4969 break;
4970 default:
4971 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
4974 return status;
4975}
4976
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004977static int video_outputsw_set(int status)
4978{
4979 int autosw;
4980 int res = 0;
4981
4982 switch (video_supported) {
4983 case TPACPI_VIDEO_570:
4984 res = acpi_evalf(NULL, NULL,
4985 "\\_SB.PHS2", "vdd",
4986 TP_ACPI_VIDEO_570_PHS2CMD,
4987 status | TP_ACPI_VIDEO_570_PHS2SET);
4988 break;
4989 case TPACPI_VIDEO_770:
4990 autosw = video_autosw_get();
4991 if (autosw < 0)
4992 return autosw;
4993
4994 res = video_autosw_set(1);
4995 if (res)
4996 return res;
4997 res = acpi_evalf(vid_handle, NULL,
4998 "ASWT", "vdd", status * 0x100, 0);
4999 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005000 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005001 return -EIO;
5002 }
5003 break;
5004 case TPACPI_VIDEO_NEW:
5005 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005006 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005007 break;
5008 default:
5009 return -ENOSYS;
5010 }
5011
Jan van den Berg72a979f2014-09-17 00:01:08 +02005012 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005013}
5014
5015static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005017 int autosw = 0;
5018
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005019 switch (video_supported) {
5020 case TPACPI_VIDEO_570:
5021 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5022 return -EIO;
5023 break;
5024 case TPACPI_VIDEO_770:
5025 case TPACPI_VIDEO_NEW:
5026 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5027 return -EIO;
5028 break;
5029 default:
5030 return -ENOSYS;
5031 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005032
5033 return autosw & 1;
5034}
5035
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005036static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005037{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005038 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005039 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005040 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005041}
5042
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005043static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005044{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005045 int autosw = video_autosw_get();
5046 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005047
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005048 if (autosw < 0)
5049 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005050
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005051 switch (video_supported) {
5052 case TPACPI_VIDEO_570:
5053 res = video_autosw_set(1);
5054 if (res)
5055 return res;
5056 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5057 break;
5058 case TPACPI_VIDEO_770:
5059 case TPACPI_VIDEO_NEW:
5060 res = video_autosw_set(1);
5061 if (res)
5062 return res;
5063 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5064 break;
5065 default:
5066 return -ENOSYS;
5067 }
5068 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005069 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005070 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005071 }
5072
Jan van den Berg72a979f2014-09-17 00:01:08 +02005073 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005074}
5075
5076static int video_expand_toggle(void)
5077{
5078 switch (video_supported) {
5079 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005080 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005081 0 : -EIO;
5082 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005083 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005084 0 : -EIO;
5085 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005086 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005087 0 : -EIO;
5088 default:
5089 return -ENOSYS;
5090 }
5091 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005092}
5093
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005094static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005095{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005096 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005097
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005098 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005099 seq_printf(m, "status:\t\tnot supported\n");
5100 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005101 }
5102
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005103 /* Even reads can crash X.org, so... */
5104 if (!capable(CAP_SYS_ADMIN))
5105 return -EPERM;
5106
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005107 status = video_outputsw_get();
5108 if (status < 0)
5109 return status;
5110
5111 autosw = video_autosw_get();
5112 if (autosw < 0)
5113 return autosw;
5114
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005115 seq_printf(m, "status:\t\tsupported\n");
5116 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5117 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005118 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005119 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5120 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5121 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5122 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005123 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005124 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5125 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5126 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005127
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005128 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005129}
5130
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005131static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132{
5133 char *cmd;
5134 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005135 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005137 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005138 return -ENODEV;
5139
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005140 /* Even reads can crash X.org, let alone writes... */
5141 if (!capable(CAP_SYS_ADMIN))
5142 return -EPERM;
5143
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005144 enable = 0;
5145 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
5147 while ((cmd = next_cmd(&buf))) {
5148 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005149 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005151 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005153 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005155 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005156 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005157 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005158 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005159 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005160 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005161 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005163 res = video_autosw_set(1);
5164 if (res)
5165 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005167 res = video_autosw_set(0);
5168 if (res)
5169 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005171 res = video_outputsw_cycle();
5172 if (res)
5173 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005175 res = video_expand_toggle();
5176 if (res)
5177 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 } else
5179 return -EINVAL;
5180 }
5181
5182 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005183 status = video_outputsw_get();
5184 if (status < 0)
5185 return status;
5186 res = video_outputsw_set((status & ~disable) | enable);
5187 if (res)
5188 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 }
5190
5191 return 0;
5192}
5193
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005194static struct ibm_struct video_driver_data = {
5195 .name = "video",
5196 .read = video_read,
5197 .write = video_write,
5198 .exit = video_exit,
5199};
5200
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005201#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5202
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005203/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005204 * Keyboard backlight subdriver
5205 */
5206
Hans de Goedec685e202017-02-09 16:44:13 +01005207static enum led_brightness kbdlight_brightness;
5208static DEFINE_MUTEX(kbdlight_mutex);
5209
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005210static int kbdlight_set_level(int level)
5211{
Hans de Goedec685e202017-02-09 16:44:13 +01005212 int ret = 0;
5213
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005214 if (!hkey_handle)
5215 return -ENXIO;
5216
Hans de Goedec685e202017-02-09 16:44:13 +01005217 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005218
Hans de Goedec685e202017-02-09 16:44:13 +01005219 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5220 ret = -EIO;
5221 else
5222 kbdlight_brightness = level;
5223
5224 mutex_unlock(&kbdlight_mutex);
5225
5226 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005227}
5228
5229static int kbdlight_get_level(void)
5230{
5231 int status = 0;
5232
5233 if (!hkey_handle)
5234 return -ENXIO;
5235
5236 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5237 return -EIO;
5238
5239 if (status < 0)
5240 return status;
5241
5242 return status & 0x3;
5243}
5244
5245static bool kbdlight_is_supported(void)
5246{
5247 int status = 0;
5248
5249 if (!hkey_handle)
5250 return false;
5251
5252 if (!acpi_has_method(hkey_handle, "MLCG")) {
5253 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5254 return false;
5255 }
5256
5257 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5258 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5259 return false;
5260 }
5261
5262 if (status < 0) {
5263 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5264 return false;
5265 }
5266
5267 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5268 /*
5269 * Guessed test for keyboard backlight:
5270 *
5271 * Machines with backlight keyboard return:
5272 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5273 * b110100010010000000XX - ThinkPad x230
5274 * b010100000010000000XX - ThinkPad x240
5275 * b010100000010000000XX - ThinkPad W541
5276 * (XX is current backlight level)
5277 *
5278 * Machines without backlight keyboard return:
5279 * b10100001000000000000 - ThinkPad x230
5280 * b10110001000000000000 - ThinkPad E430
5281 * b00000000000000000000 - ThinkPad E450
5282 *
5283 * Candidate BITs for detection test (XOR):
5284 * b01000000001000000000
5285 * ^
5286 */
5287 return status & BIT(9);
5288}
5289
Hans de Goede86ec0c22017-02-09 16:44:12 +01005290static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005291 enum led_brightness brightness)
5292{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005293 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005294}
5295
5296static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5297{
5298 int level;
5299
5300 level = kbdlight_get_level();
5301 if (level < 0)
5302 return 0;
5303
5304 return level;
5305}
5306
5307static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5308 .led_classdev = {
5309 .name = "tpacpi::kbd_backlight",
5310 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005311 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005312 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005313 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005314 }
5315};
5316
5317static int __init kbdlight_init(struct ibm_init_struct *iibm)
5318{
5319 int rc;
5320
5321 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5322
5323 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005324
5325 if (!kbdlight_is_supported()) {
5326 tp_features.kbdlight = 0;
5327 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5328 return 1;
5329 }
5330
Hans de Goedec685e202017-02-09 16:44:13 +01005331 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005332 tp_features.kbdlight = 1;
5333
5334 rc = led_classdev_register(&tpacpi_pdev->dev,
5335 &tpacpi_led_kbdlight.led_classdev);
5336 if (rc < 0) {
5337 tp_features.kbdlight = 0;
5338 return rc;
5339 }
5340
Hans de Goedec685e202017-02-09 16:44:13 +01005341 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5342 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005343 return 0;
5344}
5345
5346static void kbdlight_exit(void)
5347{
5348 if (tp_features.kbdlight)
5349 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005350}
5351
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005352static int kbdlight_set_level_and_update(int level)
5353{
5354 int ret;
5355 struct led_classdev *led_cdev;
5356
5357 ret = kbdlight_set_level(level);
5358 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5359
5360 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5361 led_cdev->brightness = level;
5362
5363 return ret;
5364}
5365
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005366static int kbdlight_read(struct seq_file *m)
5367{
5368 int level;
5369
5370 if (!tp_features.kbdlight) {
5371 seq_printf(m, "status:\t\tnot supported\n");
5372 } else {
5373 level = kbdlight_get_level();
5374 if (level < 0)
5375 seq_printf(m, "status:\t\terror %d\n", level);
5376 else
5377 seq_printf(m, "status:\t\t%d\n", level);
5378 seq_printf(m, "commands:\t0, 1, 2\n");
5379 }
5380
5381 return 0;
5382}
5383
5384static int kbdlight_write(char *buf)
5385{
5386 char *cmd;
5387 int level = -1;
5388
5389 if (!tp_features.kbdlight)
5390 return -ENODEV;
5391
5392 while ((cmd = next_cmd(&buf))) {
5393 if (strlencmp(cmd, "0") == 0)
5394 level = 0;
5395 else if (strlencmp(cmd, "1") == 0)
5396 level = 1;
5397 else if (strlencmp(cmd, "2") == 0)
5398 level = 2;
5399 else
5400 return -EINVAL;
5401 }
5402
5403 if (level == -1)
5404 return -EINVAL;
5405
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005406 return kbdlight_set_level_and_update(level);
5407}
5408
5409static void kbdlight_suspend(void)
5410{
5411 struct led_classdev *led_cdev;
5412
5413 if (!tp_features.kbdlight)
5414 return;
5415
5416 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5417 led_update_brightness(led_cdev);
5418 led_classdev_suspend(led_cdev);
5419}
5420
5421static void kbdlight_resume(void)
5422{
5423 if (!tp_features.kbdlight)
5424 return;
5425
5426 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005427}
5428
5429static struct ibm_struct kbdlight_driver_data = {
5430 .name = "kbdlight",
5431 .read = kbdlight_read,
5432 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005433 .suspend = kbdlight_suspend,
5434 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005435 .exit = kbdlight_exit,
5436};
5437
5438/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005439 * Light (thinklight) subdriver
5440 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005442TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5443TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005444
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005445static int light_get_status(void)
5446{
5447 int status = 0;
5448
5449 if (tp_features.light_status) {
5450 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5451 return -EIO;
5452 return (!!status);
5453 }
5454
5455 return -ENXIO;
5456}
5457
5458static int light_set_status(int status)
5459{
5460 int rc;
5461
5462 if (tp_features.light) {
5463 if (cmos_handle) {
5464 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005465 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005466 TP_CMOS_THINKLIGHT_ON :
5467 TP_CMOS_THINKLIGHT_OFF);
5468 } else {
5469 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005470 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005471 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005472 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005473 }
5474
5475 return -ENXIO;
5476}
5477
Hans de Goede86ec0c22017-02-09 16:44:12 +01005478static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005479 enum led_brightness brightness)
5480{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005481 return light_set_status((brightness != LED_OFF) ?
5482 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005483}
5484
5485static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5486{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005487 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005488}
5489
5490static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5491 .led_classdev = {
5492 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005493 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005494 .brightness_get = &light_sysfs_get,
5495 }
5496};
5497
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005498static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005500 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005501
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005502 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5503
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005504 if (tpacpi_is_ibm()) {
5505 TPACPI_ACPIHANDLE_INIT(ledb);
5506 TPACPI_ACPIHANDLE_INIT(lght);
5507 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005508 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005509
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005510 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005511 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005512
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005513 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005514 /* light status not supported on
5515 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005516 tp_features.light_status =
5517 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005519 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5520 str_supported(tp_features.light),
5521 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005522
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005523 if (!tp_features.light)
5524 return 1;
5525
5526 rc = led_classdev_register(&tpacpi_pdev->dev,
5527 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005528
5529 if (rc < 0) {
5530 tp_features.light = 0;
5531 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005532 } else {
5533 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005534 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005535
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005536 return rc;
5537}
5538
5539static void light_exit(void)
5540{
5541 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542}
5543
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005544static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005546 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005548 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005549 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005550 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005551 seq_printf(m, "status:\t\tunknown\n");
5552 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005553 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005554 status = light_get_status();
5555 if (status < 0)
5556 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005557 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5558 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562}
5563
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005564static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005567 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005568
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005569 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005570 return -ENODEV;
5571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 while ((cmd = next_cmd(&buf))) {
5573 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005574 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005576 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 } else
5578 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 }
5580
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005581 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582}
5583
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005584static struct ibm_struct light_driver_data = {
5585 .name = "light",
5586 .read = light_read,
5587 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005588 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005589};
5590
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005591/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005592 * CMOS subdriver
5593 */
5594
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005595/* sysfs cmos_command -------------------------------------------------- */
5596static ssize_t cmos_command_store(struct device *dev,
5597 struct device_attribute *attr,
5598 const char *buf, size_t count)
5599{
5600 unsigned long cmos_cmd;
5601 int res;
5602
5603 if (parse_strtoul(buf, 21, &cmos_cmd))
5604 return -EINVAL;
5605
5606 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005607 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005608}
5609
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005610static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005611
5612/* --------------------------------------------------------------------- */
5613
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005614static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005615{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005616 int res;
5617
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005618 vdbg_printk(TPACPI_DBG_INIT,
5619 "initializing cmos commands subdriver\n");
5620
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005621 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005622
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005623 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5624 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005625
5626 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5627 if (res)
5628 return res;
5629
Jan van den Berg72a979f2014-09-17 00:01:08 +02005630 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005631}
5632
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005633static void cmos_exit(void)
5634{
5635 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5636}
5637
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005638static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005640 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5641 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005643 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005645 seq_printf(m, "status:\t\tsupported\n");
5646 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 }
5648
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650}
5651
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005652static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653{
5654 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005655 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
5657 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005658 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5659 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 /* cmos_cmd set */
5661 } else
5662 return -EINVAL;
5663
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005664 res = issue_thinkpad_cmos_command(cmos_cmd);
5665 if (res)
5666 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 }
5668
5669 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005670}
5671
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005672static struct ibm_struct cmos_driver_data = {
5673 .name = "cmos",
5674 .read = cmos_read,
5675 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005676 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005677};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005678
5679/*************************************************************************
5680 * LED subdriver
5681 */
5682
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005683enum led_access_mode {
5684 TPACPI_LED_NONE = 0,
5685 TPACPI_LED_570, /* 570 */
5686 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5687 TPACPI_LED_NEW, /* all others */
5688};
5689
5690enum { /* For TPACPI_LED_OLD */
5691 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5692 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5693 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5694};
5695
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005696static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005697
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005698static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005699
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005700#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005701static struct tpacpi_led_classdev *tpacpi_leds;
5702static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005703static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005704 /* there's a limit of 19 chars + NULL before 2.6.26 */
5705 "tpacpi::power",
5706 "tpacpi:orange:batt",
5707 "tpacpi:green:batt",
5708 "tpacpi::dock_active",
5709 "tpacpi::bay_active",
5710 "tpacpi::dock_batt",
5711 "tpacpi::unknown_led",
5712 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005713 "tpacpi::dock_status1",
5714 "tpacpi::dock_status2",
5715 "tpacpi::unknown_led2",
5716 "tpacpi::unknown_led3",
5717 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005718};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005719#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005720
5721static inline bool tpacpi_is_led_restricted(const unsigned int led)
5722{
5723#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5724 return false;
5725#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005726 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005727#endif
5728}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005729
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005730static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005731{
5732 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005733 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005734
5735 switch (led_supported) {
5736 case TPACPI_LED_570:
5737 if (!acpi_evalf(ec_handle,
5738 &status, "GLED", "dd", 1 << led))
5739 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005740 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005741 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005742 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005743 TPACPI_LED_ON :
5744 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005745 tpacpi_led_state_cache[led] = led_s;
5746 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005747 default:
5748 return -ENXIO;
5749 }
5750
5751 /* not reached */
5752}
5753
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005754static int led_set_status(const unsigned int led,
5755 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005756{
5757 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005758 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5759 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005760
5761 int rc = 0;
5762
5763 switch (led_supported) {
5764 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005765 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005766 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005767 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005768 if (unlikely(tpacpi_is_led_restricted(led)))
5769 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005770 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5771 (1 << led), led_sled_arg1[ledstatus]))
5772 rc = -EIO;
5773 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005774 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005775 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005776 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005777 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005778 if (unlikely(tpacpi_is_led_restricted(led)))
5779 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005780 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5781 if (rc >= 0)
5782 rc = ec_write(TPACPI_LED_EC_HLBL,
5783 (ledstatus == TPACPI_LED_BLINK) << led);
5784 if (rc >= 0)
5785 rc = ec_write(TPACPI_LED_EC_HLCL,
5786 (ledstatus != TPACPI_LED_OFF) << led);
5787 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005788 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005789 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005790 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5791 return -EINVAL;
5792 if (unlikely(tpacpi_is_led_restricted(led)))
5793 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005794 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5795 led, led_led_arg1[ledstatus]))
5796 rc = -EIO;
5797 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005798 default:
5799 rc = -ENXIO;
5800 }
5801
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005802 if (!rc)
5803 tpacpi_led_state_cache[led] = ledstatus;
5804
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005805 return rc;
5806}
5807
Hans de Goede86ec0c22017-02-09 16:44:12 +01005808static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005809 enum led_brightness brightness)
5810{
5811 struct tpacpi_led_classdev *data = container_of(led_cdev,
5812 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005813 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005814
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005815 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005816 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005817 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005818 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005819 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005820 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005821
Hans de Goede86ec0c22017-02-09 16:44:12 +01005822 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005823}
5824
5825static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5826 unsigned long *delay_on, unsigned long *delay_off)
5827{
5828 struct tpacpi_led_classdev *data = container_of(led_cdev,
5829 struct tpacpi_led_classdev, led_classdev);
5830
5831 /* Can we choose the flash rate? */
5832 if (*delay_on == 0 && *delay_off == 0) {
5833 /* yes. set them to the hardware blink rate (1 Hz) */
5834 *delay_on = 500; /* ms */
5835 *delay_off = 500; /* ms */
5836 } else if ((*delay_on != 500) || (*delay_off != 500))
5837 return -EINVAL;
5838
Hans de Goede86ec0c22017-02-09 16:44:12 +01005839 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005840}
5841
5842static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5843{
5844 int rc;
5845
5846 struct tpacpi_led_classdev *data = container_of(led_cdev,
5847 struct tpacpi_led_classdev, led_classdev);
5848
5849 rc = led_get_status(data->led);
5850
5851 if (rc == TPACPI_LED_OFF || rc < 0)
5852 rc = LED_OFF; /* no error handling in led class :( */
5853 else
5854 rc = LED_FULL;
5855
5856 return rc;
5857}
5858
5859static void led_exit(void)
5860{
5861 unsigned int i;
5862
5863 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5864 if (tpacpi_leds[i].led_classdev.name)
5865 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5866 }
5867
5868 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005869}
5870
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005871static int __init tpacpi_init_led(unsigned int led)
5872{
5873 int rc;
5874
5875 tpacpi_leds[led].led = led;
5876
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005877 /* LEDs with no name don't get registered */
5878 if (!tpacpi_led_names[led])
5879 return 0;
5880
Hans de Goede86ec0c22017-02-09 16:44:12 +01005881 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005882 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5883 if (led_supported == TPACPI_LED_570)
5884 tpacpi_leds[led].led_classdev.brightness_get =
5885 &led_sysfs_get;
5886
5887 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5888
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005889 rc = led_classdev_register(&tpacpi_pdev->dev,
5890 &tpacpi_leds[led].led_classdev);
5891 if (rc < 0)
5892 tpacpi_leds[led].led_classdev.name = NULL;
5893
5894 return rc;
5895}
5896
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005897static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5898 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5899 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5900 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5901
5902 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5903 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5904 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5905 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5906 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5907 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5908 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5909 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5910
5911 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5912 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5913 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5914 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5915 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5916
5917 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5918 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5919 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5920 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5921
5922 /* (1) - may have excess leds enabled on MSB */
5923
5924 /* Defaults (order matters, keep last, don't reorder!) */
5925 { /* Lenovo */
5926 .vendor = PCI_VENDOR_ID_LENOVO,
5927 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5928 .quirks = 0x1fffU,
5929 },
5930 { /* IBM ThinkPads with no EC version string */
5931 .vendor = PCI_VENDOR_ID_IBM,
5932 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5933 .quirks = 0x00ffU,
5934 },
5935 { /* IBM ThinkPads with EC version string */
5936 .vendor = PCI_VENDOR_ID_IBM,
5937 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5938 .quirks = 0x00bfU,
5939 },
5940};
5941
5942#undef TPACPI_LEDQ_IBM
5943#undef TPACPI_LEDQ_LNV
5944
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005945static enum led_access_mode __init led_init_detect_mode(void)
5946{
5947 acpi_status status;
5948
5949 if (tpacpi_is_ibm()) {
5950 /* 570 */
5951 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5952 if (ACPI_SUCCESS(status))
5953 return TPACPI_LED_570;
5954
5955 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5956 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5957 if (ACPI_SUCCESS(status))
5958 return TPACPI_LED_OLD;
5959 }
5960
5961 /* most others */
5962 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5963 if (ACPI_SUCCESS(status))
5964 return TPACPI_LED_NEW;
5965
5966 /* R30, R31, and unknown firmwares */
5967 led_handle = NULL;
5968 return TPACPI_LED_NONE;
5969}
5970
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005971static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005972{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005973 unsigned int i;
5974 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005975 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005976
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005977 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5978
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005979 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005980
Adam Leefcb44e12013-06-07 16:20:08 +08005981 if (led_supported != TPACPI_LED_NONE) {
5982 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5983 ARRAY_SIZE(led_useful_qtable));
5984
5985 if (!useful_leds) {
5986 led_handle = NULL;
5987 led_supported = TPACPI_LED_NONE;
5988 }
5989 }
5990
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005991 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5992 str_supported(led_supported), led_supported);
5993
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005994 if (led_supported == TPACPI_LED_NONE)
5995 return 1;
5996
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005997 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5998 GFP_KERNEL);
5999 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07006000 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006001 return -ENOMEM;
6002 }
6003
6004 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08006005 tpacpi_leds[i].led = -1;
6006
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006007 if (!tpacpi_is_led_restricted(i) &&
6008 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006009 rc = tpacpi_init_led(i);
6010 if (rc < 0) {
6011 led_exit();
6012 return rc;
6013 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006014 }
6015 }
6016
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006017#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006018 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006019#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006020 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006021}
6022
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006023#define str_led_status(s) \
6024 ((s) == TPACPI_LED_OFF ? "off" : \
6025 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006026
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006027static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006029 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006030 seq_printf(m, "status:\t\tnot supported\n");
6031 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006032 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006033 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006034
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006035 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006036 /* 570 */
6037 int i, status;
6038 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006039 status = led_get_status(i);
6040 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006041 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006042 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006043 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006044 }
6045 }
6046
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006047 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050}
6051
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006052static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053{
6054 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006055 int led, rc;
6056 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006057
6058 if (!led_supported)
6059 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
6061 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08006062 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 return -EINVAL;
6064
Adam Leeedf2d772013-06-08 16:51:15 +08006065 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6066 tpacpi_leds[led].led < 0)
6067 return -ENODEV;
6068
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006069 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006070 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006072 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006073 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006074 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006075 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006076 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006077 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006078
6079 rc = led_set_status(led, s);
6080 if (rc < 0)
6081 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 }
6083
6084 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006085}
6086
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006087static struct ibm_struct led_driver_data = {
6088 .name = "led",
6089 .read = led_read,
6090 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006091 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006092};
6093
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006094/*************************************************************************
6095 * Beep subdriver
6096 */
6097
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006098TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006099
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006100#define TPACPI_BEEP_Q1 0x0001
6101
6102static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6103 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6104 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6105};
6106
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006107static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006108{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006109 unsigned long quirks;
6110
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006111 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6112
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006113 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006114
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006115 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6116 str_supported(beep_handle != NULL));
6117
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006118 quirks = tpacpi_check_quirks(beep_quirk_table,
6119 ARRAY_SIZE(beep_quirk_table));
6120
6121 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6122
Jan van den Berg72a979f2014-09-17 00:01:08 +02006123 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006124}
6125
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006126static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006128 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006129 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006130 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006131 seq_printf(m, "status:\t\tsupported\n");
6132 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136}
6137
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006138static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139{
6140 char *cmd;
6141 int beep_cmd;
6142
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006143 if (!beep_handle)
6144 return -ENODEV;
6145
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006147 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6148 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 /* beep_cmd set */
6150 } else
6151 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006152 if (tp_features.beep_needs_two_args) {
6153 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6154 beep_cmd, 0))
6155 return -EIO;
6156 } else {
6157 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6158 beep_cmd))
6159 return -EIO;
6160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 }
6162
6163 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006164}
6165
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006166static struct ibm_struct beep_driver_data = {
6167 .name = "beep",
6168 .read = beep_read,
6169 .write = beep_write,
6170};
6171
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006172/*************************************************************************
6173 * Thermal subdriver
6174 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006175
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006176enum thermal_access_mode {
6177 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6178 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6179 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6180 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6181 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6182};
6183
6184enum { /* TPACPI_THERMAL_TPEC_* */
6185 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6186 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6187 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006188
6189 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006190};
6191
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006192
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006193#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6194struct ibm_thermal_sensors_struct {
6195 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6196};
6197
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006198static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006199
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006200/* idx is zero-based */
6201static int thermal_get_sensor(int idx, s32 *value)
6202{
6203 int t;
6204 s8 tmp;
6205 char tmpi[5];
6206
6207 t = TP_EC_THERMAL_TMP0;
6208
6209 switch (thermal_read_mode) {
6210#if TPACPI_MAX_THERMAL_SENSORS >= 16
6211 case TPACPI_THERMAL_TPEC_16:
6212 if (idx >= 8 && idx <= 15) {
6213 t = TP_EC_THERMAL_TMP8;
6214 idx -= 8;
6215 }
6216 /* fallthrough */
6217#endif
6218 case TPACPI_THERMAL_TPEC_8:
6219 if (idx <= 7) {
6220 if (!acpi_ec_read(t + idx, &tmp))
6221 return -EIO;
6222 *value = tmp * 1000;
6223 return 0;
6224 }
6225 break;
6226
6227 case TPACPI_THERMAL_ACPI_UPDT:
6228 if (idx <= 7) {
6229 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6230 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6231 return -EIO;
6232 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6233 return -EIO;
6234 *value = (t - 2732) * 100;
6235 return 0;
6236 }
6237 break;
6238
6239 case TPACPI_THERMAL_ACPI_TMP07:
6240 if (idx <= 7) {
6241 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6242 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6243 return -EIO;
6244 if (t > 127 || t < -127)
6245 t = TP_EC_THERMAL_TMP_NA;
6246 *value = t * 1000;
6247 return 0;
6248 }
6249 break;
6250
6251 case TPACPI_THERMAL_NONE:
6252 default:
6253 return -ENOSYS;
6254 }
6255
6256 return -EINVAL;
6257}
6258
6259static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6260{
6261 int res, i;
6262 int n;
6263
6264 n = 8;
6265 i = 0;
6266
6267 if (!s)
6268 return -EINVAL;
6269
6270 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6271 n = 16;
6272
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006273 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006274 res = thermal_get_sensor(i, &s->temp[i]);
6275 if (res)
6276 return res;
6277 }
6278
6279 return n;
6280}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006281
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006282static void thermal_dump_all_sensors(void)
6283{
6284 int n, i;
6285 struct ibm_thermal_sensors_struct t;
6286
6287 n = thermal_get_sensors(&t);
6288 if (n <= 0)
6289 return;
6290
Joe Perches0978e012011-04-04 10:06:25 -07006291 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006292
6293 for (i = 0; i < n; i++) {
6294 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006295 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006296 else
Joe Perches0978e012011-04-04 10:06:25 -07006297 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006298 }
6299
Joe Perches0978e012011-04-04 10:06:25 -07006300 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006301}
6302
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006303/* sysfs temp##_input -------------------------------------------------- */
6304
6305static ssize_t thermal_temp_input_show(struct device *dev,
6306 struct device_attribute *attr,
6307 char *buf)
6308{
6309 struct sensor_device_attribute *sensor_attr =
6310 to_sensor_dev_attr(attr);
6311 int idx = sensor_attr->index;
6312 s32 value;
6313 int res;
6314
6315 res = thermal_get_sensor(idx, &value);
6316 if (res)
6317 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006318 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006319 return -ENXIO;
6320
6321 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6322}
6323
6324#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006325 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6326 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006327
6328static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6329 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6330 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6331 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6332 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6333 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6334 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6335 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6336 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6337 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6338 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6339 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6340 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6341 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6342 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6343 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6344 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6345};
6346
6347#define THERMAL_ATTRS(X) \
6348 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6349
6350static struct attribute *thermal_temp_input_attr[] = {
6351 THERMAL_ATTRS(8),
6352 THERMAL_ATTRS(9),
6353 THERMAL_ATTRS(10),
6354 THERMAL_ATTRS(11),
6355 THERMAL_ATTRS(12),
6356 THERMAL_ATTRS(13),
6357 THERMAL_ATTRS(14),
6358 THERMAL_ATTRS(15),
6359 THERMAL_ATTRS(0),
6360 THERMAL_ATTRS(1),
6361 THERMAL_ATTRS(2),
6362 THERMAL_ATTRS(3),
6363 THERMAL_ATTRS(4),
6364 THERMAL_ATTRS(5),
6365 THERMAL_ATTRS(6),
6366 THERMAL_ATTRS(7),
6367 NULL
6368};
6369
6370static const struct attribute_group thermal_temp_input16_group = {
6371 .attrs = thermal_temp_input_attr
6372};
6373
6374static const struct attribute_group thermal_temp_input8_group = {
6375 .attrs = &thermal_temp_input_attr[8]
6376};
6377
6378#undef THERMAL_SENSOR_ATTR_TEMP
6379#undef THERMAL_ATTRS
6380
6381/* --------------------------------------------------------------------- */
6382
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006383static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006384{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006385 u8 t, ta1, ta2;
6386 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006387 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006388 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006389
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006390 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6391
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006392 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006393
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006394 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006395 /*
6396 * Direct EC access mode: sensors at registers
6397 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6398 * non-implemented, thermal sensors return 0x80 when
6399 * not available
6400 */
6401
6402 ta1 = ta2 = 0;
6403 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006404 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006405 ta1 |= t;
6406 } else {
6407 ta1 = 0;
6408 break;
6409 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006410 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006411 ta2 |= t;
6412 } else {
6413 ta1 = 0;
6414 break;
6415 }
6416 }
6417 if (ta1 == 0) {
6418 /* This is sheer paranoia, but we handle it anyway */
6419 if (acpi_tmp7) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006420 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006421 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006422 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006423 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006424 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006425 }
6426 } else {
6427 thermal_read_mode =
6428 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006429 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006430 }
6431 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006432 if (tpacpi_is_ibm() &&
6433 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006434 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006435 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006436 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006437 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006438 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006439 }
6440 } else {
6441 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006442 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006443 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006444
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006445 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6446 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6447 thermal_read_mode);
6448
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006449 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006450 case TPACPI_THERMAL_TPEC_16:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006451 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006452 &thermal_temp_input16_group);
6453 if (res)
6454 return res;
6455 break;
6456 case TPACPI_THERMAL_TPEC_8:
6457 case TPACPI_THERMAL_ACPI_TMP07:
6458 case TPACPI_THERMAL_ACPI_UPDT:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006459 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006460 &thermal_temp_input8_group);
6461 if (res)
6462 return res;
6463 break;
6464 case TPACPI_THERMAL_NONE:
6465 default:
6466 return 1;
6467 }
6468
6469 return 0;
6470}
6471
6472static void thermal_exit(void)
6473{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006474 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006475 case TPACPI_THERMAL_TPEC_16:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006476 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006477 &thermal_temp_input16_group);
6478 break;
6479 case TPACPI_THERMAL_TPEC_8:
6480 case TPACPI_THERMAL_ACPI_TMP07:
6481 case TPACPI_THERMAL_ACPI_UPDT:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006482 sysfs_remove_group(&tpacpi_hwmon->kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006483 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006484 break;
6485 case TPACPI_THERMAL_NONE:
6486 default:
6487 break;
6488 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006489}
6490
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006491static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006492{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006493 int n, i;
6494 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006495
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006496 n = thermal_get_sensors(&t);
6497 if (unlikely(n < 0))
6498 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006499
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006500 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006501
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006502 if (n > 0) {
6503 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006504 seq_printf(m, "%d ", t.temp[i] / 1000);
6505 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006506 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006507 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006508
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006509 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006510}
6511
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006512static struct ibm_struct thermal_driver_data = {
6513 .name = "thermal",
6514 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006515 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006516};
6517
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006518/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006519 * Backlight/brightness subdriver
6520 */
Holger Macht8acb0252006-10-20 14:30:28 -07006521
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006522#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6523
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006524/*
6525 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6526 * CMOS NVRAM byte 0x5E, bits 0-3.
6527 *
6528 * EC HBRV (0x31) has the following layout
6529 * Bit 7: unknown function
6530 * Bit 6: unknown function
6531 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6532 * Bit 4: must be set to zero to avoid problems
6533 * Bit 3-0: backlight brightness level
6534 *
6535 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006536 *
6537 * WARNING: The X61 has been verified to use HBRV for something else, so
6538 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6539 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006540 */
6541
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006542enum {
6543 TP_EC_BACKLIGHT = 0x31,
6544
6545 /* TP_EC_BACKLIGHT bitmasks */
6546 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6547 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6548 TP_EC_BACKLIGHT_MAPSW = 0x20,
6549};
6550
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006551enum tpacpi_brightness_access_mode {
6552 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6553 TPACPI_BRGHT_MODE_EC, /* EC control */
6554 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6555 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6556 TPACPI_BRGHT_MODE_MAX
6557};
6558
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006559static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006560
6561static enum tpacpi_brightness_access_mode brightness_mode =
6562 TPACPI_BRGHT_MODE_MAX;
6563
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006564static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6565
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006566static struct mutex brightness_mutex;
6567
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006568/* NVRAM brightness access,
6569 * call with brightness_mutex held! */
6570static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006571{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006572 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006573
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006574 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6575 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6576 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006577 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006578
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006579 return lnvram;
6580}
6581
6582static void tpacpi_brightness_checkpoint_nvram(void)
6583{
6584 u8 lec = 0;
6585 u8 b_nvram;
6586
6587 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6588 return;
6589
6590 vdbg_printk(TPACPI_DBG_BRGHT,
6591 "trying to checkpoint backlight level to NVRAM...\n");
6592
6593 if (mutex_lock_killable(&brightness_mutex) < 0)
6594 return;
6595
6596 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6597 goto unlock;
6598 lec &= TP_EC_BACKLIGHT_LVLMSK;
6599 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6600
6601 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6602 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6603 /* NVRAM needs update */
6604 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6605 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6606 b_nvram |= lec;
6607 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6608 dbg_printk(TPACPI_DBG_BRGHT,
6609 "updated NVRAM backlight level to %u (0x%02x)\n",
6610 (unsigned int) lec, (unsigned int) b_nvram);
6611 } else
6612 vdbg_printk(TPACPI_DBG_BRGHT,
6613 "NVRAM backlight level already is %u (0x%02x)\n",
6614 (unsigned int) lec, (unsigned int) b_nvram);
6615
6616unlock:
6617 mutex_unlock(&brightness_mutex);
6618}
6619
6620
6621/* call with brightness_mutex held! */
6622static int tpacpi_brightness_get_raw(int *status)
6623{
6624 u8 lec = 0;
6625
6626 switch (brightness_mode) {
6627 case TPACPI_BRGHT_MODE_UCMS_STEP:
6628 *status = tpacpi_brightness_nvram_get();
6629 return 0;
6630 case TPACPI_BRGHT_MODE_EC:
6631 case TPACPI_BRGHT_MODE_ECNVRAM:
6632 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006633 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006634 *status = lec;
6635 return 0;
6636 default:
6637 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006638 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006639}
6640
6641/* call with brightness_mutex held! */
6642/* do NOT call with illegal backlight level value */
6643static int tpacpi_brightness_set_ec(unsigned int value)
6644{
6645 u8 lec = 0;
6646
6647 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6648 return -EIO;
6649
6650 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6651 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6652 (value & TP_EC_BACKLIGHT_LVLMSK))))
6653 return -EIO;
6654
6655 return 0;
6656}
6657
6658/* call with brightness_mutex held! */
6659static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6660{
6661 int cmos_cmd, inc;
6662 unsigned int current_value, i;
6663
6664 current_value = tpacpi_brightness_nvram_get();
6665
6666 if (value == current_value)
6667 return 0;
6668
6669 cmos_cmd = (value > current_value) ?
6670 TP_CMOS_BRIGHTNESS_UP :
6671 TP_CMOS_BRIGHTNESS_DOWN;
6672 inc = (value > current_value) ? 1 : -1;
6673
6674 for (i = current_value; i != value; i += inc)
6675 if (issue_thinkpad_cmos_command(cmos_cmd))
6676 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006677
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006678 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006679}
6680
6681/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006682static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006683{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006684 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006685
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006686 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006687 return -EINVAL;
6688
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006689 vdbg_printk(TPACPI_DBG_BRGHT,
6690 "set backlight level to %d\n", value);
6691
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006692 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006693 if (res < 0)
6694 return res;
6695
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006696 switch (brightness_mode) {
6697 case TPACPI_BRGHT_MODE_EC:
6698 case TPACPI_BRGHT_MODE_ECNVRAM:
6699 res = tpacpi_brightness_set_ec(value);
6700 break;
6701 case TPACPI_BRGHT_MODE_UCMS_STEP:
6702 res = tpacpi_brightness_set_ucmsstep(value);
6703 break;
6704 default:
6705 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006706 }
6707
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006708 mutex_unlock(&brightness_mutex);
6709 return res;
6710}
6711
6712/* sysfs backlight class ----------------------------------------------- */
6713
6714static int brightness_update_status(struct backlight_device *bd)
6715{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006716 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006717 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6718 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006719 bd->props.brightness : 0;
6720
6721 dbg_printk(TPACPI_DBG_BRGHT,
6722 "backlight: attempt to set level to %d\n",
6723 level);
6724
6725 /* it is the backlight class's job (caller) to handle
6726 * EINTR and other errors properly */
6727 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006728}
Holger Macht8acb0252006-10-20 14:30:28 -07006729
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006730static int brightness_get(struct backlight_device *bd)
6731{
6732 int status, res;
6733
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006734 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006735 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006736 return 0;
6737
6738 res = tpacpi_brightness_get_raw(&status);
6739
6740 mutex_unlock(&brightness_mutex);
6741
6742 if (res < 0)
6743 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006744
6745 return status & TP_EC_BACKLIGHT_LVLMSK;
6746}
6747
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006748static void tpacpi_brightness_notify_change(void)
6749{
6750 backlight_force_update(ibm_backlight_device,
6751 BACKLIGHT_UPDATE_HOTKEY);
6752}
6753
Lionel Debrouxacc24722010-11-16 14:14:02 +01006754static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006755 .get_brightness = brightness_get,
6756 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006757};
6758
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006759/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006760
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006761/*
6762 * Call _BCL method of video device. On some ThinkPads this will
6763 * switch the firmware to the ACPI brightness control mode.
6764 */
6765
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006766static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6767{
6768 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6769 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006770 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006771 int rc;
6772
Aaron Lu46445b62013-10-11 21:27:46 +08006773 if (acpi_bus_get_device(handle, &device))
6774 return 0;
6775
6776 rc = 0;
6777 list_for_each_entry(child, &device->children, node) {
6778 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6779 NULL, &buffer);
6780 if (ACPI_FAILURE(status))
6781 continue;
6782
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006783 obj = (union acpi_object *)buffer.pointer;
6784 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006785 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006786 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006787 rc = 0;
6788 } else {
6789 rc = obj->package.count;
6790 }
Aaron Lu46445b62013-10-11 21:27:46 +08006791 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006792 }
6793
6794 kfree(buffer.pointer);
6795 return rc;
6796}
6797
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006798
6799/*
6800 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6801 */
6802static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6803{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006804 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006805 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006806
Aaron Lu46445b62013-10-11 21:27:46 +08006807 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006808 if (video_device)
6809 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006810
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006811 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006812
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006813 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006814}
6815
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006816/*
6817 * These are only useful for models that have only one possibility
6818 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6819 * these quirks.
6820 */
6821#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6822#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6823#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6824
6825static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6826 /* Models with ATI GPUs known to require ECNVRAM mode */
6827 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6828
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006829 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006830 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006831 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006832 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006833 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6834
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006835 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006836 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006837 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6838 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006839
6840 /* Models with Intel GMA900 */
6841 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6842 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6843 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6844};
6845
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006846/*
6847 * Returns < 0 for error, otherwise sets tp_features.bright_*
6848 * and bright_maxlvl.
6849 */
6850static void __init tpacpi_detect_brightness_capabilities(void)
6851{
6852 unsigned int b;
6853
6854 vdbg_printk(TPACPI_DBG_INIT,
6855 "detecting firmware brightness interface capabilities\n");
6856
6857 /* we could run a quirks check here (same table used by
6858 * brightness_init) if needed */
6859
6860 /*
6861 * We always attempt to detect acpi support, so as to switch
6862 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6863 * going to publish a backlight interface
6864 */
6865 b = tpacpi_check_std_acpi_brightness_support();
6866 switch (b) {
6867 case 16:
6868 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006869 break;
6870 case 8:
6871 case 0:
6872 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006873 break;
6874 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006875 tp_features.bright_unkfw = 1;
6876 bright_maxlvl = b - 1;
6877 }
Eric Curtin15c75622016-01-30 16:55:59 +00006878 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006879}
6880
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006881static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006882{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006883 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006884 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006885 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006886
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006887 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6888
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006889 mutex_init(&brightness_mutex);
6890
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006891 quirks = tpacpi_check_quirks(brightness_quirk_table,
6892 ARRAY_SIZE(brightness_quirk_table));
6893
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006894 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006895
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006896 /* if it is unknown, we don't handle it: it wouldn't be safe */
6897 if (tp_features.bright_unkfw)
6898 return 1;
6899
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006900 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006901 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006902 "brightness support disabled by module parameter\n");
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006903 return 1;
6904 }
6905
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006906 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006907 if (brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006908 pr_info("Standard ACPI backlight interface available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006909 return 1;
6910 } else if (brightness_enable == 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006911 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006912 return 1;
6913 }
6914 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006915 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006916 }
6917
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006918 /*
6919 * Check for module parameter bogosity, note that we
6920 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6921 * able to detect "unspecified"
6922 */
6923 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006924 return -EINVAL;
6925
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006926 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6927 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6928 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006929 if (quirks & TPACPI_BRGHT_Q_EC)
6930 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6931 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006932 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6933
6934 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006935 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006936 brightness_mode);
6937 }
6938
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006939 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006940 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006941 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6942 brightness_mode == TPACPI_BRGHT_MODE_EC))
6943 return -EINVAL;
6944
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006945 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006946 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006947
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006948 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006949 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006950 props.max_brightness = bright_maxlvl;
6951 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006952 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6953 NULL, NULL,
6954 &ibm_backlight_data,
6955 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006956 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006957 int rc = PTR_ERR(ibm_backlight_device);
6958 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006959 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006960 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006961 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006962 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6963 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006964
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006965 if (quirks & TPACPI_BRGHT_Q_ASK) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006966 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6967 brightness_mode);
6968 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6969 TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006970 }
6971
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006972 /* Added by mistake in early 2007. Probably useless, but it could
6973 * be working around some unknown firmware problem where the value
6974 * read at startup doesn't match the real hardware state... so leave
6975 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006976 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006977
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006978 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006979 "brightness: registering brightness hotkeys as change notification\n");
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006980 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6981 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006982 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006983 return 0;
6984}
6985
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006986static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006987{
6988 tpacpi_brightness_checkpoint_nvram();
6989}
6990
6991static void brightness_shutdown(void)
6992{
6993 tpacpi_brightness_checkpoint_nvram();
6994}
6995
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006996static void brightness_exit(void)
6997{
6998 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006999 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007000 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007001 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007002 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007003
7004 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007005}
7006
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007007static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007008{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007009 int level;
7010
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007011 level = brightness_get(NULL);
7012 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007013 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007014 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007015 seq_printf(m, "level:\t\t%d\n", level);
7016 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007017 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7018 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007019 }
7020
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007021 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007022}
7023
7024static int brightness_write(char *buf)
7025{
7026 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007027 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007028 char *cmd;
7029
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007030 level = brightness_get(NULL);
7031 if (level < 0)
7032 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007033
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007034 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007035 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007036 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007037 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007038 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007039 if (level > 0)
7040 level--;
7041 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007042 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007043 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007044 } else
7045 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007046 }
7047
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007048 tpacpi_disclose_usertask("procfs brightness",
7049 "set level to %d\n", level);
7050
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007051 /*
7052 * Now we know what the final level should be, so we try to set it.
7053 * Doing it this way makes the syscall restartable in case of EINTR
7054 */
7055 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007056 if (!rc && ibm_backlight_device)
7057 backlight_force_update(ibm_backlight_device,
7058 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02007059 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007060}
7061
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007062static struct ibm_struct brightness_driver_data = {
7063 .name = "brightness",
7064 .read = brightness_read,
7065 .write = brightness_write,
7066 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007067 .suspend = brightness_suspend,
7068 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007069};
7070
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007071/*************************************************************************
7072 * Volume subdriver
7073 */
7074
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007075/*
7076 * IBM ThinkPads have a simple volume controller with MUTE gating.
7077 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7078 *
7079 * Since the *61 series (and probably also the later *60 series), Lenovo
7080 * ThinkPads only implement the MUTE gate.
7081 *
7082 * EC register 0x30
7083 * Bit 6: MUTE (1 mutes sound)
7084 * Bit 3-0: Volume
7085 * Other bits should be zero as far as we know.
7086 *
7087 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7088 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7089 * such as bit 7 which is used to detect repeated presses of MUTE,
7090 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007091 *
7092 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7093 * in response to user input. Unfortunately, this rarely works well.
7094 * The laptop changes the state of its internal MUTE gate and, on some
7095 * models, sends KEY_MUTE, causing any user code that responds to the
7096 * mute button to get confused. The hardware MUTE gate is also
7097 * unnecessary, since user code can handle the mute button without
7098 * kernel or EC help.
7099 *
7100 * To avoid confusing userspace, we simply disable all EC-based mute
7101 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007102 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007103
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007104#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7105
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007106#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7107#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7108#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7109
Takashi Iwaicab66612013-10-24 16:06:32 +02007110#if SNDRV_CARDS <= 32
7111#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7112#else
7113#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7114#endif
7115static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007116static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307117static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007118
7119struct tpacpi_alsa_data {
7120 struct snd_card *card;
7121 struct snd_ctl_elem_id *ctl_mute_id;
7122 struct snd_ctl_elem_id *ctl_vol_id;
7123};
7124
7125static struct snd_card *alsa_card;
7126
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007127enum {
7128 TP_EC_AUDIO = 0x30,
7129
7130 /* TP_EC_AUDIO bits */
7131 TP_EC_AUDIO_MUTESW = 6,
7132
7133 /* TP_EC_AUDIO bitmasks */
7134 TP_EC_AUDIO_LVL_MSK = 0x0F,
7135 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7136
7137 /* Maximum volume */
7138 TP_EC_VOLUME_MAX = 14,
7139};
7140
7141enum tpacpi_volume_access_mode {
7142 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7143 TPACPI_VOL_MODE_EC, /* Pure EC control */
7144 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7145 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7146 TPACPI_VOL_MODE_MAX
7147};
7148
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007149enum tpacpi_volume_capabilities {
7150 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7151 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7152 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7153 TPACPI_VOL_CAP_MAX
7154};
7155
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007156enum tpacpi_mute_btn_mode {
7157 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7158 /* We don't know what mode 1 is. */
7159 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7160 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7161};
7162
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007163static enum tpacpi_volume_access_mode volume_mode =
7164 TPACPI_VOL_MODE_MAX;
7165
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007166static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307167static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007168static bool software_mute_requested = true;
7169static bool software_mute_active;
7170static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007171
7172/*
7173 * Used to syncronize writers to TP_EC_AUDIO and
7174 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7175 */
7176static struct mutex volume_mutex;
7177
7178static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007179{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007180 u8 lec = 0;
7181 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007182 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007183
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007184 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7185 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007186 if (!volume_control_allowed)
7187 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007188 if (software_mute_active)
7189 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007190
7191 vdbg_printk(TPACPI_DBG_MIXER,
7192 "trying to checkpoint mixer state to NVRAM...\n");
7193
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007194 if (tp_features.mixer_no_level_control)
7195 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7196 else
7197 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7198
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007199 if (mutex_lock_killable(&volume_mutex) < 0)
7200 return;
7201
7202 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7203 goto unlock;
7204 lec &= ec_mask;
7205 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7206
7207 if (lec != (b_nvram & ec_mask)) {
7208 /* NVRAM needs update */
7209 b_nvram &= ~ec_mask;
7210 b_nvram |= lec;
7211 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7212 dbg_printk(TPACPI_DBG_MIXER,
7213 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7214 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007215 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007216 vdbg_printk(TPACPI_DBG_MIXER,
7217 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7218 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007219 }
7220
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007221unlock:
7222 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007223}
7224
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007225static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007226{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007227 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007228
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007229 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7230 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007231
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007232 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007233
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007234 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007235
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007236 return 0;
7237}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007238
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007239static int volume_get_status(u8 *status)
7240{
7241 return volume_get_status_ec(status);
7242}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007243
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007244static int volume_set_status_ec(const u8 status)
7245{
7246 if (!acpi_ec_write(TP_EC_AUDIO, status))
7247 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007248
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007249 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7250
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007251 /*
7252 * On X200s, and possibly on others, it can take a while for
7253 * reads to become correct.
7254 */
7255 msleep(1);
7256
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007257 return 0;
7258}
7259
7260static int volume_set_status(const u8 status)
7261{
7262 return volume_set_status_ec(status);
7263}
7264
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007265/* returns < 0 on error, 0 on no change, 1 on change */
7266static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007267{
7268 int rc;
7269 u8 s, n;
7270
7271 if (mutex_lock_killable(&volume_mutex) < 0)
7272 return -EINTR;
7273
7274 rc = volume_get_status_ec(&s);
7275 if (rc)
7276 goto unlock;
7277
7278 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7279 s & ~TP_EC_AUDIO_MUTESW_MSK;
7280
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007281 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007282 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007283 if (!rc)
7284 rc = 1;
7285 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007286
7287unlock:
7288 mutex_unlock(&volume_mutex);
7289 return rc;
7290}
7291
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007292static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007293{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007294 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007295 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007296 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007297}
7298
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007299static int volume_set_mute(const bool mute)
7300{
7301 int rc;
7302
7303 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7304 (mute) ? "" : "un");
7305
7306 rc = __volume_set_mute_ec(mute);
7307 return (rc < 0) ? rc : 0;
7308}
7309
7310/* returns < 0 on error, 0 on no change, 1 on change */
7311static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007312{
7313 int rc;
7314 u8 s, n;
7315
7316 if (vol > TP_EC_VOLUME_MAX)
7317 return -EINVAL;
7318
7319 if (mutex_lock_killable(&volume_mutex) < 0)
7320 return -EINTR;
7321
7322 rc = volume_get_status_ec(&s);
7323 if (rc)
7324 goto unlock;
7325
7326 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7327
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007328 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007329 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007330 if (!rc)
7331 rc = 1;
7332 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007333
7334unlock:
7335 mutex_unlock(&volume_mutex);
7336 return rc;
7337}
7338
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007339static int volume_set_software_mute(bool startup)
7340{
7341 int result;
7342
7343 if (!tpacpi_is_lenovo())
7344 return -ENODEV;
7345
7346 if (startup) {
7347 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7348 "HAUM", "qd"))
7349 return -EIO;
7350
7351 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7352 "Initial HAUM setting was %d\n",
7353 software_mute_orig_mode);
7354 }
7355
7356 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7357 (int)TP_EC_MUTE_BTN_NONE))
7358 return -EIO;
7359
7360 if (result != TP_EC_MUTE_BTN_NONE)
7361 pr_warn("Unexpected SAUM result %d\n",
7362 result);
7363
7364 /*
7365 * In software mute mode, the standard codec controls take
7366 * precendence, so we unmute the ThinkPad HW switch at
7367 * startup. Just on case there are SAUM-capable ThinkPads
7368 * with level controls, set max HW volume as well.
7369 */
7370 if (tp_features.mixer_no_level_control)
7371 result = volume_set_mute(false);
7372 else
7373 result = volume_set_status(TP_EC_VOLUME_MAX);
7374
7375 if (result != 0)
7376 pr_warn("Failed to unmute the HW mute switch\n");
7377
7378 return 0;
7379}
7380
7381static void volume_exit_software_mute(void)
7382{
7383 int r;
7384
7385 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7386 || r != software_mute_orig_mode)
7387 pr_warn("Failed to restore mute mode\n");
7388}
7389
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007390static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007391{
7392 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007393 "ALSA: trying to set volume level to %hu\n", vol);
7394 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007395}
7396
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007397static void volume_alsa_notify_change(void)
7398{
7399 struct tpacpi_alsa_data *d;
7400
7401 if (alsa_card && alsa_card->private_data) {
7402 d = alsa_card->private_data;
7403 if (d->ctl_mute_id)
7404 snd_ctl_notify(alsa_card,
7405 SNDRV_CTL_EVENT_MASK_VALUE,
7406 d->ctl_mute_id);
7407 if (d->ctl_vol_id)
7408 snd_ctl_notify(alsa_card,
7409 SNDRV_CTL_EVENT_MASK_VALUE,
7410 d->ctl_vol_id);
7411 }
7412}
7413
7414static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7415 struct snd_ctl_elem_info *uinfo)
7416{
7417 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7418 uinfo->count = 1;
7419 uinfo->value.integer.min = 0;
7420 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7421 return 0;
7422}
7423
7424static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7425 struct snd_ctl_elem_value *ucontrol)
7426{
7427 u8 s;
7428 int rc;
7429
7430 rc = volume_get_status(&s);
7431 if (rc < 0)
7432 return rc;
7433
7434 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7435 return 0;
7436}
7437
7438static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7439 struct snd_ctl_elem_value *ucontrol)
7440{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007441 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7442 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007443 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007444}
7445
7446#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7447
7448static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7449 struct snd_ctl_elem_value *ucontrol)
7450{
7451 u8 s;
7452 int rc;
7453
7454 rc = volume_get_status(&s);
7455 if (rc < 0)
7456 return rc;
7457
7458 ucontrol->value.integer.value[0] =
7459 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7460 return 0;
7461}
7462
7463static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7464 struct snd_ctl_elem_value *ucontrol)
7465{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007466 tpacpi_disclose_usertask("ALSA", "%smute\n",
7467 ucontrol->value.integer.value[0] ?
7468 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007469 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007470}
7471
Mathias Krausec3aa4722014-07-16 19:43:17 +02007472static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007473 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7474 .name = "Console Playback Volume",
7475 .index = 0,
7476 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7477 .info = volume_alsa_vol_info,
7478 .get = volume_alsa_vol_get,
7479};
7480
Mathias Krausec3aa4722014-07-16 19:43:17 +02007481static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007482 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7483 .name = "Console Playback Switch",
7484 .index = 0,
7485 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7486 .info = volume_alsa_mute_info,
7487 .get = volume_alsa_mute_get,
7488};
7489
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007490static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007491{
7492 tpacpi_volume_checkpoint_nvram();
7493}
7494
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007495static void volume_resume(void)
7496{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007497 if (software_mute_active) {
7498 if (volume_set_software_mute(false) < 0)
7499 pr_warn("Failed to restore software mute\n");
7500 } else {
7501 volume_alsa_notify_change();
7502 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007503}
7504
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007505static void volume_shutdown(void)
7506{
7507 tpacpi_volume_checkpoint_nvram();
7508}
7509
7510static void volume_exit(void)
7511{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007512 if (alsa_card) {
7513 snd_card_free(alsa_card);
7514 alsa_card = NULL;
7515 }
7516
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007517 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007518
7519 if (software_mute_active)
7520 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007521}
7522
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007523static int __init volume_create_alsa_mixer(void)
7524{
7525 struct snd_card *card;
7526 struct tpacpi_alsa_data *data;
7527 struct snd_kcontrol *ctl_vol;
7528 struct snd_kcontrol *ctl_mute;
7529 int rc;
7530
Takashi Iwai89235e52014-01-29 15:01:27 +01007531 rc = snd_card_new(&tpacpi_pdev->dev,
7532 alsa_index, alsa_id, THIS_MODULE,
7533 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007534 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007535 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007536 return 1;
7537 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007538
7539 BUG_ON(!card->private_data);
7540 data = card->private_data;
7541 data->card = card;
7542
7543 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7544 sizeof(card->driver));
7545 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7546 sizeof(card->shortname));
7547 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7548 (thinkpad_id.ec_version_str) ?
7549 thinkpad_id.ec_version_str : "(unknown)");
7550 snprintf(card->longname, sizeof(card->longname),
7551 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7552 (thinkpad_id.ec_version_str) ?
7553 thinkpad_id.ec_version_str : "unknown");
7554
7555 if (volume_control_allowed) {
7556 volume_alsa_control_vol.put = volume_alsa_vol_put;
7557 volume_alsa_control_vol.access =
7558 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7559
7560 volume_alsa_control_mute.put = volume_alsa_mute_put;
7561 volume_alsa_control_mute.access =
7562 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7563 }
7564
7565 if (!tp_features.mixer_no_level_control) {
7566 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7567 rc = snd_ctl_add(card, ctl_vol);
7568 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007569 pr_err("Failed to create ALSA volume control: %d\n",
7570 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007571 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007572 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007573 data->ctl_vol_id = &ctl_vol->id;
7574 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007575
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007576 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7577 rc = snd_ctl_add(card, ctl_mute);
7578 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007579 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007580 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007581 }
7582 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007583
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007584 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007585 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007586 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007587 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007588 }
7589
7590 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007591 return 0;
7592
7593err_exit:
7594 snd_card_free(card);
7595 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007596}
7597
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007598#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7599#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7600
7601static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7602 /* Whitelist volume level on all IBM by default */
7603 { .vendor = PCI_VENDOR_ID_IBM,
7604 .bios = TPACPI_MATCH_ANY,
7605 .ec = TPACPI_MATCH_ANY,
7606 .quirks = TPACPI_VOL_Q_LEVEL },
7607
7608 /* Lenovo models with volume control (needs confirmation) */
7609 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7610 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7611 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7612 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7613 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7614 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7615 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7616
7617 /* Whitelist mute-only on all Lenovo by default */
7618 { .vendor = PCI_VENDOR_ID_LENOVO,
7619 .bios = TPACPI_MATCH_ANY,
7620 .ec = TPACPI_MATCH_ANY,
7621 .quirks = TPACPI_VOL_Q_MUTEONLY }
7622};
7623
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007624static int __init volume_init(struct ibm_init_struct *iibm)
7625{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007626 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007627 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007628
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007629 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7630
7631 mutex_init(&volume_mutex);
7632
7633 /*
7634 * Check for module parameter bogosity, note that we
7635 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7636 * able to detect "unspecified"
7637 */
7638 if (volume_mode > TPACPI_VOL_MODE_MAX)
7639 return -EINVAL;
7640
7641 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007642 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7643 TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007644 return 1;
7645 }
7646
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007647 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7648 return -EINVAL;
7649
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007650 /*
7651 * The ALSA mixer is our primary interface.
7652 * When disabled, don't install the subdriver at all
7653 */
7654 if (!alsa_enable) {
7655 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007656 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007657 return 1;
7658 }
7659
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007660 quirks = tpacpi_check_quirks(volume_quirk_table,
7661 ARRAY_SIZE(volume_quirk_table));
7662
7663 switch (volume_capabilities) {
7664 case TPACPI_VOL_CAP_AUTO:
7665 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7666 tp_features.mixer_no_level_control = 1;
7667 else if (quirks & TPACPI_VOL_Q_LEVEL)
7668 tp_features.mixer_no_level_control = 0;
7669 else
7670 return 1; /* no mixer */
7671 break;
7672 case TPACPI_VOL_CAP_VOLMUTE:
7673 tp_features.mixer_no_level_control = 0;
7674 break;
7675 case TPACPI_VOL_CAP_MUTEONLY:
7676 tp_features.mixer_no_level_control = 1;
7677 break;
7678 default:
7679 return 1;
7680 }
7681
7682 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7683 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7684 "using user-supplied volume_capabilities=%d\n",
7685 volume_capabilities);
7686
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007687 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7688 volume_mode == TPACPI_VOL_MODE_MAX) {
7689 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7690
7691 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7692 "driver auto-selected volume_mode=%d\n",
7693 volume_mode);
7694 } else {
7695 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7696 "using user-supplied volume_mode=%d\n",
7697 volume_mode);
7698 }
7699
7700 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007701 "mute is supported, volume control is %s\n",
7702 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007703
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007704 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7705 software_mute_active = true;
7706 } else {
7707 rc = volume_create_alsa_mixer();
7708 if (rc) {
7709 pr_err("Could not create the ALSA mixer interface\n");
7710 return rc;
7711 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007712
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007713 pr_info("Console audio control enabled, mode: %s\n",
7714 (volume_control_allowed) ?
7715 "override (read/write)" :
7716 "monitor (read only)");
7717 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007718
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007719 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7720 "registering volume hotkeys as change notification\n");
7721 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7722 | TP_ACPI_HKEY_VOLUP_MASK
7723 | TP_ACPI_HKEY_VOLDWN_MASK
7724 | TP_ACPI_HKEY_MUTE_MASK);
7725
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007726 return 0;
7727}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007728
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007729static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007730{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007731 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007732
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007733 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007734 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007735 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007736 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007737 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007738 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007739 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007740 status & TP_EC_AUDIO_LVL_MSK);
7741
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007742 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007743 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007744
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007745 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007746 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007747 if (!tp_features.mixer_no_level_control) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007748 seq_printf(m, "commands:\tup, down\n");
7749 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7750 TP_EC_VOLUME_MAX);
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007751 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007752 }
7753 }
7754
7755 return 0;
7756}
7757
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007758static int volume_write(char *buf)
7759{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007760 u8 s;
7761 u8 new_level, new_mute;
7762 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007763 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007764 int rc;
7765
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007766 /*
7767 * We do allow volume control at driver startup, so that the
7768 * user can set initial state through the volume=... parameter hack.
7769 */
7770 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7771 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7772 tp_warned.volume_ctrl_forbidden = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007773 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7774 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007775 }
7776 return -EPERM;
7777 }
7778
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007779 rc = volume_get_status(&s);
7780 if (rc < 0)
7781 return rc;
7782
7783 new_level = s & TP_EC_AUDIO_LVL_MSK;
7784 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007785
7786 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007787 if (!tp_features.mixer_no_level_control) {
7788 if (strlencmp(cmd, "up") == 0) {
7789 if (new_mute)
7790 new_mute = 0;
7791 else if (new_level < TP_EC_VOLUME_MAX)
7792 new_level++;
7793 continue;
7794 } else if (strlencmp(cmd, "down") == 0) {
7795 if (new_mute)
7796 new_mute = 0;
7797 else if (new_level > 0)
7798 new_level--;
7799 continue;
7800 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7801 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7802 new_level = l;
7803 continue;
7804 }
7805 }
7806 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007807 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007808 else if (strlencmp(cmd, "unmute") == 0)
7809 new_mute = 0;
7810 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007811 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007812 }
7813
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007814 if (tp_features.mixer_no_level_control) {
7815 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7816 new_mute ? "" : "un");
7817 rc = volume_set_mute(!!new_mute);
7818 } else {
7819 tpacpi_disclose_usertask("procfs volume",
7820 "%smute and set level to %d\n",
7821 new_mute ? "" : "un", new_level);
7822 rc = volume_set_status(new_mute | new_level);
7823 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007824 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007825
7826 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007827}
7828
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007829static struct ibm_struct volume_driver_data = {
7830 .name = "volume",
7831 .read = volume_read,
7832 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007833 .exit = volume_exit,
7834 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007835 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007836 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007837};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007838
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007839#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7840
7841#define alsa_card NULL
7842
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007843static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007844{
7845}
7846
7847static int __init volume_init(struct ibm_init_struct *iibm)
7848{
Joe Perches0978e012011-04-04 10:06:25 -07007849 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007850
7851 return 1;
7852}
7853
7854static struct ibm_struct volume_driver_data = {
7855 .name = "volume",
7856};
7857
7858#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7859
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007860/*************************************************************************
7861 * Fan subdriver
7862 */
7863
7864/*
7865 * FAN ACCESS MODES
7866 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007867 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007868 * ACPI GFAN method: returns fan level
7869 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007870 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007871 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007872 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007873 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007874 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7875 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007876 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7877 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007878 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007879 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007880 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7881 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007882 *
7883 * Fan speed changes of any sort (including those caused by the
7884 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007885 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007886 * limited.
7887 *
7888 * Reading is not available if GFAN exists.
7889 * Writing is not available if SFAN exists.
7890 *
7891 * Bits
7892 * 7 automatic mode engaged;
7893 * (default operation mode of the ThinkPad)
7894 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007895 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007896 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007897 * the tachometer while the fan controller ramps up
7898 * the speed (which can take up to a few *minutes*).
7899 * Speeds up fan to 100% duty-cycle, which is far above
7900 * the standard RPM levels. It is not impossible that
7901 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007902 * 5-3 unused in some models. Extra bits for fan level
7903 * in others, but still useless as all values above
7904 * 7 map to the same speed as level 7 in these models.
7905 * 2-0 fan level (0..7 usually)
7906 * 0x00 = stop
7907 * 0x07 = max (set when temperatures critical)
7908 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007909 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007910 *
7911 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007912 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007913 * does so, its initial value is meaningless (0x07).
7914 *
7915 * For firmware bugs, refer to:
7916 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7917 *
7918 * ----
7919 *
7920 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7921 * Main fan tachometer reading (in RPM)
7922 *
7923 * This register is present on all ThinkPads with a new-style EC, and
7924 * it is known not to be present on the A21m/e, and T22, as there is
7925 * something else in offset 0x84 according to the ACPI DSDT. Other
7926 * ThinkPads from this same time period (and earlier) probably lack the
7927 * tachometer as well.
7928 *
Nick Andrew877d0312009-01-26 11:06:57 +01007929 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007930 * was never fixed by IBM to report the EC firmware version string
7931 * probably support the tachometer (like the early X models), so
7932 * detecting it is quite hard. We need more data to know for sure.
7933 *
7934 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7935 * might result.
7936 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007937 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7938 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007939 *
7940 * For firmware bugs, refer to:
7941 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7942 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007943 * ----
7944 *
7945 * ThinkPad EC register 0x31 bit 0 (only on select models)
7946 *
7947 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7948 * show the speed of the main fan. When bit 0 of EC register 0x31
7949 * is one, the tachometer registers show the speed of the auxiliary
7950 * fan.
7951 *
7952 * Fan control seems to affect both fans, regardless of the state
7953 * of this bit.
7954 *
7955 * So far, only the firmware for the X60/X61 non-tablet versions
7956 * seem to support this (firmware TP-7M).
7957 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007958 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007959 * ThinkPad X31, X40, X41. Not available in the X60.
7960 *
7961 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7962 * high speed. ACPI DSDT seems to map these three speeds to levels
7963 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7964 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7965 *
7966 * The speeds are stored on handles
7967 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7968 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007969 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007970 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7971 *
7972 * ACPI DSDT switches which set is in use depending on various
7973 * factors.
7974 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007975 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007976 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7977 * but the ACPI tables just mention level 7.
7978 */
7979
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007980enum { /* Fan control constants */
7981 fan_status_offset = 0x2f, /* EC register 0x2f */
7982 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7983 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007984 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7985 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007986
7987 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7988 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7989
7990 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7991};
7992
7993enum fan_status_access_mode {
7994 TPACPI_FAN_NONE = 0, /* No fan status or control */
7995 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7996 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7997};
7998
7999enum fan_control_access_mode {
8000 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8001 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8002 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8003 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8004};
8005
8006enum fan_control_commands {
8007 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8008 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8009 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8010 * and also watchdog cmd */
8011};
8012
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308013static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008014
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008015static enum fan_status_access_mode fan_status_access_mode;
8016static enum fan_control_access_mode fan_control_access_mode;
8017static enum fan_control_commands fan_control_commands;
8018
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008019static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008020static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008021static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008022static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008023
8024static struct mutex fan_mutex;
8025
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008026static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05008027static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008028
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008029TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8030TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008031 "\\FSPD", /* 600e/x, 770e, 770x */
8032 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008033TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008034 "JFNS", /* 770x-JL */
8035 ); /* all others */
8036
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008037/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008038 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8039 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8040 * be in auto mode (0x80).
8041 *
8042 * This is corrected by any write to HFSP either by the driver, or
8043 * by the firmware.
8044 *
8045 * We assume 0x07 really means auto mode while this quirk is active,
8046 * as this is far more likely than the ThinkPad being in level 7,
8047 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008048 *
8049 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8050 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008051 */
8052
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008053static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008054{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008055 if (fan_control_initial_status == 0x07) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008056 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008057 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008058 }
8059}
8060
8061static void fan_quirk1_handle(u8 *fan_status)
8062{
8063 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8064 if (*fan_status != fan_control_initial_status) {
8065 /* something changed the HFSP regisnter since
8066 * driver init time, so it is not undefined
8067 * anymore */
8068 tp_features.fan_ctrl_status_undef = 0;
8069 } else {
8070 /* Return most likely status. In fact, it
8071 * might be the only possible status */
8072 *fan_status = TP_EC_FAN_AUTO;
8073 }
8074 }
8075}
8076
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008077/* Select main fan on X60/X61, NOOP on others */
8078static bool fan_select_fan1(void)
8079{
8080 if (tp_features.second_fan) {
8081 u8 val;
8082
8083 if (ec_read(fan_select_offset, &val) < 0)
8084 return false;
8085 val &= 0xFEU;
8086 if (ec_write(fan_select_offset, val) < 0)
8087 return false;
8088 }
8089 return true;
8090}
8091
8092/* Select secondary fan on X60/X61 */
8093static bool fan_select_fan2(void)
8094{
8095 u8 val;
8096
8097 if (!tp_features.second_fan)
8098 return false;
8099
8100 if (ec_read(fan_select_offset, &val) < 0)
8101 return false;
8102 val |= 0x01U;
8103 if (ec_write(fan_select_offset, val) < 0)
8104 return false;
8105
8106 return true;
8107}
8108
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008109/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008110 * Call with fan_mutex held
8111 */
8112static void fan_update_desired_level(u8 status)
8113{
8114 if ((status &
8115 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8116 if (status > 7)
8117 fan_control_desired_level = 7;
8118 else
8119 fan_control_desired_level = status;
8120 }
8121}
8122
8123static int fan_get_status(u8 *status)
8124{
8125 u8 s;
8126
8127 /* TODO:
8128 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8129
8130 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008131 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008132 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008133 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008134
Dan Carpentereceeb432012-09-01 12:54:07 -07008135 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008136 return -EIO;
8137
8138 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008139 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008140
8141 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008142 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008143 case TPACPI_FAN_RD_TPEC:
8144 /* all except 570, 600e/x, 770e, 770x */
8145 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8146 return -EIO;
8147
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008148 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008149 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008150 fan_quirk1_handle(status);
8151 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008152
8153 break;
8154
8155 default:
8156 return -ENXIO;
8157 }
8158
8159 return 0;
8160}
8161
8162static int fan_get_status_safe(u8 *status)
8163{
8164 int rc;
8165 u8 s;
8166
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008167 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008168 return -ERESTARTSYS;
8169 rc = fan_get_status(&s);
8170 if (!rc)
8171 fan_update_desired_level(s);
8172 mutex_unlock(&fan_mutex);
8173
Dan Carpentera7718362016-04-15 17:46:34 +03008174 if (rc)
8175 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008176 if (status)
8177 *status = s;
8178
Dan Carpentera7718362016-04-15 17:46:34 +03008179 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008180}
8181
8182static int fan_get_speed(unsigned int *speed)
8183{
8184 u8 hi, lo;
8185
8186 switch (fan_status_access_mode) {
8187 case TPACPI_FAN_RD_TPEC:
8188 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008189 if (unlikely(!fan_select_fan1()))
8190 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008191 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8192 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8193 return -EIO;
8194
8195 if (likely(speed))
8196 *speed = (hi << 8) | lo;
8197
8198 break;
8199
8200 default:
8201 return -ENXIO;
8202 }
8203
8204 return 0;
8205}
8206
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008207static int fan2_get_speed(unsigned int *speed)
8208{
8209 u8 hi, lo;
8210 bool rc;
8211
8212 switch (fan_status_access_mode) {
8213 case TPACPI_FAN_RD_TPEC:
8214 /* all except 570, 600e/x, 770e, 770x */
8215 if (unlikely(!fan_select_fan2()))
8216 return -EIO;
8217 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8218 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8219 fan_select_fan1(); /* play it safe */
8220 if (rc)
8221 return -EIO;
8222
8223 if (likely(speed))
8224 *speed = (hi << 8) | lo;
8225
8226 break;
8227
8228 default:
8229 return -ENXIO;
8230 }
8231
8232 return 0;
8233}
8234
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008235static int fan_set_level(int level)
8236{
8237 if (!fan_control_allowed)
8238 return -EPERM;
8239
8240 switch (fan_control_access_mode) {
8241 case TPACPI_FAN_WR_ACPI_SFAN:
8242 if (level >= 0 && level <= 7) {
8243 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8244 return -EIO;
8245 } else
8246 return -EINVAL;
8247 break;
8248
8249 case TPACPI_FAN_WR_ACPI_FANS:
8250 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008251 if (!(level & TP_EC_FAN_AUTO) &&
8252 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008253 ((level < 0) || (level > 7)))
8254 return -EINVAL;
8255
8256 /* safety net should the EC not support AUTO
8257 * or FULLSPEED mode bits and just ignore them */
8258 if (level & TP_EC_FAN_FULLSPEED)
8259 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008260 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008261 level |= 4; /* safety min speed 4 */
8262
8263 if (!acpi_ec_write(fan_status_offset, level))
8264 return -EIO;
8265 else
8266 tp_features.fan_ctrl_status_undef = 0;
8267 break;
8268
8269 default:
8270 return -ENXIO;
8271 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008272
8273 vdbg_printk(TPACPI_DBG_FAN,
8274 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008275 return 0;
8276}
8277
8278static int fan_set_level_safe(int level)
8279{
8280 int rc;
8281
8282 if (!fan_control_allowed)
8283 return -EPERM;
8284
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008285 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008286 return -ERESTARTSYS;
8287
8288 if (level == TPACPI_FAN_LAST_LEVEL)
8289 level = fan_control_desired_level;
8290
8291 rc = fan_set_level(level);
8292 if (!rc)
8293 fan_update_desired_level(level);
8294
8295 mutex_unlock(&fan_mutex);
8296 return rc;
8297}
8298
8299static int fan_set_enable(void)
8300{
8301 u8 s;
8302 int rc;
8303
8304 if (!fan_control_allowed)
8305 return -EPERM;
8306
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008307 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008308 return -ERESTARTSYS;
8309
8310 switch (fan_control_access_mode) {
8311 case TPACPI_FAN_WR_ACPI_FANS:
8312 case TPACPI_FAN_WR_TPEC:
8313 rc = fan_get_status(&s);
8314 if (rc < 0)
8315 break;
8316
8317 /* Don't go out of emergency fan mode */
8318 if (s != 7) {
8319 s &= 0x07;
8320 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8321 }
8322
8323 if (!acpi_ec_write(fan_status_offset, s))
8324 rc = -EIO;
8325 else {
8326 tp_features.fan_ctrl_status_undef = 0;
8327 rc = 0;
8328 }
8329 break;
8330
8331 case TPACPI_FAN_WR_ACPI_SFAN:
8332 rc = fan_get_status(&s);
8333 if (rc < 0)
8334 break;
8335
8336 s &= 0x07;
8337
8338 /* Set fan to at least level 4 */
8339 s |= 4;
8340
8341 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008342 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008343 else
8344 rc = 0;
8345 break;
8346
8347 default:
8348 rc = -ENXIO;
8349 }
8350
8351 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008352
8353 if (!rc)
8354 vdbg_printk(TPACPI_DBG_FAN,
8355 "fan control: set fan control register to 0x%02x\n",
8356 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008357 return rc;
8358}
8359
8360static int fan_set_disable(void)
8361{
8362 int rc;
8363
8364 if (!fan_control_allowed)
8365 return -EPERM;
8366
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008367 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008368 return -ERESTARTSYS;
8369
8370 rc = 0;
8371 switch (fan_control_access_mode) {
8372 case TPACPI_FAN_WR_ACPI_FANS:
8373 case TPACPI_FAN_WR_TPEC:
8374 if (!acpi_ec_write(fan_status_offset, 0x00))
8375 rc = -EIO;
8376 else {
8377 fan_control_desired_level = 0;
8378 tp_features.fan_ctrl_status_undef = 0;
8379 }
8380 break;
8381
8382 case TPACPI_FAN_WR_ACPI_SFAN:
8383 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8384 rc = -EIO;
8385 else
8386 fan_control_desired_level = 0;
8387 break;
8388
8389 default:
8390 rc = -ENXIO;
8391 }
8392
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008393 if (!rc)
8394 vdbg_printk(TPACPI_DBG_FAN,
8395 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008396
8397 mutex_unlock(&fan_mutex);
8398 return rc;
8399}
8400
8401static int fan_set_speed(int speed)
8402{
8403 int rc;
8404
8405 if (!fan_control_allowed)
8406 return -EPERM;
8407
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008408 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008409 return -ERESTARTSYS;
8410
8411 rc = 0;
8412 switch (fan_control_access_mode) {
8413 case TPACPI_FAN_WR_ACPI_FANS:
8414 if (speed >= 0 && speed <= 65535) {
8415 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8416 speed, speed, speed))
8417 rc = -EIO;
8418 } else
8419 rc = -EINVAL;
8420 break;
8421
8422 default:
8423 rc = -ENXIO;
8424 }
8425
8426 mutex_unlock(&fan_mutex);
8427 return rc;
8428}
8429
8430static void fan_watchdog_reset(void)
8431{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008432 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8433 return;
8434
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008435 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008436 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8437 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8438 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8439 else
8440 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008441}
8442
8443static void fan_watchdog_fire(struct work_struct *ignored)
8444{
8445 int rc;
8446
8447 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8448 return;
8449
Joe Perches0978e012011-04-04 10:06:25 -07008450 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008451 rc = fan_set_enable();
8452 if (rc < 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008453 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8454 rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008455 /* reschedule for later */
8456 fan_watchdog_reset();
8457 }
8458}
8459
8460/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008461 * SYSFS fan layout: hwmon compatible (device)
8462 *
8463 * pwm*_enable:
8464 * 0: "disengaged" mode
8465 * 1: manual mode
8466 * 2: native EC "auto" mode (recommended, hardware default)
8467 *
8468 * pwm*: set speed in manual mode, ignored otherwise.
8469 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8470 * interpolation.
8471 *
8472 * fan*_input: tachometer reading, RPM
8473 *
8474 *
8475 * SYSFS fan layout: extensions
8476 *
8477 * fan_watchdog (driver):
8478 * fan watchdog interval in seconds, 0 disables (default), max 120
8479 */
8480
8481/* sysfs fan pwm1_enable ----------------------------------------------- */
8482static ssize_t fan_pwm1_enable_show(struct device *dev,
8483 struct device_attribute *attr,
8484 char *buf)
8485{
8486 int res, mode;
8487 u8 status;
8488
8489 res = fan_get_status_safe(&status);
8490 if (res)
8491 return res;
8492
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008493 if (status & TP_EC_FAN_FULLSPEED) {
8494 mode = 0;
8495 } else if (status & TP_EC_FAN_AUTO) {
8496 mode = 2;
8497 } else
8498 mode = 1;
8499
8500 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8501}
8502
8503static ssize_t fan_pwm1_enable_store(struct device *dev,
8504 struct device_attribute *attr,
8505 const char *buf, size_t count)
8506{
8507 unsigned long t;
8508 int res, level;
8509
8510 if (parse_strtoul(buf, 2, &t))
8511 return -EINVAL;
8512
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008513 tpacpi_disclose_usertask("hwmon pwm1_enable",
8514 "set fan mode to %lu\n", t);
8515
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008516 switch (t) {
8517 case 0:
8518 level = TP_EC_FAN_FULLSPEED;
8519 break;
8520 case 1:
8521 level = TPACPI_FAN_LAST_LEVEL;
8522 break;
8523 case 2:
8524 level = TP_EC_FAN_AUTO;
8525 break;
8526 case 3:
8527 /* reserved for software-controlled auto mode */
8528 return -ENOSYS;
8529 default:
8530 return -EINVAL;
8531 }
8532
8533 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008534 if (res == -ENXIO)
8535 return -EINVAL;
8536 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008537 return res;
8538
8539 fan_watchdog_reset();
8540
8541 return count;
8542}
8543
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008544static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8545 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008546
8547/* sysfs fan pwm1 ------------------------------------------------------ */
8548static ssize_t fan_pwm1_show(struct device *dev,
8549 struct device_attribute *attr,
8550 char *buf)
8551{
8552 int res;
8553 u8 status;
8554
8555 res = fan_get_status_safe(&status);
8556 if (res)
8557 return res;
8558
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008559 if ((status &
8560 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8561 status = fan_control_desired_level;
8562
8563 if (status > 7)
8564 status = 7;
8565
8566 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8567}
8568
8569static ssize_t fan_pwm1_store(struct device *dev,
8570 struct device_attribute *attr,
8571 const char *buf, size_t count)
8572{
8573 unsigned long s;
8574 int rc;
8575 u8 status, newlevel;
8576
8577 if (parse_strtoul(buf, 255, &s))
8578 return -EINVAL;
8579
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008580 tpacpi_disclose_usertask("hwmon pwm1",
8581 "set fan speed to %lu\n", s);
8582
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008583 /* scale down from 0-255 to 0-7 */
8584 newlevel = (s >> 5) & 0x07;
8585
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008586 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008587 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008588
8589 rc = fan_get_status(&status);
8590 if (!rc && (status &
8591 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8592 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008593 if (rc == -ENXIO)
8594 rc = -EINVAL;
8595 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008596 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008597 fan_watchdog_reset();
8598 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008599 }
8600
8601 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008602 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008603}
8604
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008605static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008606
8607/* sysfs fan fan1_input ------------------------------------------------ */
8608static ssize_t fan_fan1_input_show(struct device *dev,
8609 struct device_attribute *attr,
8610 char *buf)
8611{
8612 int res;
8613 unsigned int speed;
8614
8615 res = fan_get_speed(&speed);
8616 if (res < 0)
8617 return res;
8618
8619 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8620}
8621
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008622static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008623
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008624/* sysfs fan fan2_input ------------------------------------------------ */
8625static ssize_t fan_fan2_input_show(struct device *dev,
8626 struct device_attribute *attr,
8627 char *buf)
8628{
8629 int res;
8630 unsigned int speed;
8631
8632 res = fan2_get_speed(&speed);
8633 if (res < 0)
8634 return res;
8635
8636 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8637}
8638
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008639static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008640
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008641/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008642static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008643{
8644 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8645}
8646
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008647static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8648 size_t count)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008649{
8650 unsigned long t;
8651
8652 if (parse_strtoul(buf, 120, &t))
8653 return -EINVAL;
8654
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008655 if (!fan_control_allowed)
8656 return -EPERM;
8657
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008658 fan_watchdog_maxinterval = t;
8659 fan_watchdog_reset();
8660
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008661 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8662
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008663 return count;
8664}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008665static DRIVER_ATTR_RW(fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008666
8667/* --------------------------------------------------------------------- */
8668static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008669 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8670 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008671 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008672 NULL
8673};
8674
8675static const struct attribute_group fan_attr_group = {
8676 .attrs = fan_attributes,
8677};
8678
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008679#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8680#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008681
8682#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8683 { .vendor = PCI_VENDOR_ID_IBM, \
8684 .bios = TPACPI_MATCH_ANY, \
8685 .ec = TPID(__id1, __id2), \
8686 .quirks = __quirks }
8687
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008688#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8689 { .vendor = PCI_VENDOR_ID_LENOVO, \
8690 .bios = TPACPI_MATCH_ANY, \
8691 .ec = TPID(__id1, __id2), \
8692 .quirks = __quirks }
8693
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008694static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8695 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8696 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8697 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8698 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008699 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008700};
8701
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008702#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008703#undef TPACPI_FAN_QI
8704
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008705static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008706{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008707 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008708 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008709
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008710 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8711 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008712
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008713 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008714 fan_status_access_mode = TPACPI_FAN_NONE;
8715 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008716 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008717 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008718 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008719 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008720 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008721
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008722 if (tpacpi_is_ibm()) {
8723 TPACPI_ACPIHANDLE_INIT(fans);
8724 TPACPI_ACPIHANDLE_INIT(gfan);
8725 TPACPI_ACPIHANDLE_INIT(sfan);
8726 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008727
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008728 quirks = tpacpi_check_quirks(fan_quirk_table,
8729 ARRAY_SIZE(fan_quirk_table));
8730
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008731 if (gfan_handle) {
8732 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008733 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008734 } else {
8735 /* all other ThinkPads: note that even old-style
8736 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008737 if (likely(acpi_ec_read(fan_status_offset,
8738 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008739 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008740 if (quirks & TPACPI_FAN_Q1)
8741 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008742 if (quirks & TPACPI_FAN_2FAN) {
8743 tp_features.second_fan = 1;
8744 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8745 "secondary fan support enabled\n");
8746 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008747 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008748 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008749 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008750 }
8751 }
8752
8753 if (sfan_handle) {
8754 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008755 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008756 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008757 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008758 } else {
8759 if (!gfan_handle) {
8760 /* gfan without sfan means no fan control */
8761 /* all other models implement TP EC 0x2f control */
8762
8763 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008764 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008765 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008766 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008767 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008768 TPACPI_FAN_CMD_SPEED |
8769 TPACPI_FAN_CMD_LEVEL |
8770 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008771 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008772 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008773 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008774 TPACPI_FAN_CMD_LEVEL |
8775 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008776 }
8777 }
8778 }
8779
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008780 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8781 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008782 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8783 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8784 fan_status_access_mode, fan_control_access_mode);
8785
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008786 /* fan control master switch */
8787 if (!fan_control_allowed) {
8788 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8789 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008790 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008791 "fan control features disabled by parameter\n");
8792 }
8793
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008794 /* update fan_control_desired_level */
8795 if (fan_status_access_mode != TPACPI_FAN_NONE)
8796 fan_get_status_safe(NULL);
8797
8798 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8799 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008800 if (tp_features.second_fan) {
8801 /* attach second fan tachometer */
8802 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008803 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008804 }
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008805 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008806 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008807 if (rc < 0)
8808 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008809
8810 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8811 &driver_attr_fan_watchdog);
8812 if (rc < 0) {
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008813 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008814 &fan_attr_group);
8815 return rc;
8816 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008817 return 0;
8818 } else
8819 return 1;
8820}
8821
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008822static void fan_exit(void)
8823{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008824 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008825 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008826
8827 /* FIXME: can we really do this unconditionally? */
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008828 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008829 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8830 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008831
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008832 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008833 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008834}
8835
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008836static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008837{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008838 int rc;
8839
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008840 if (!fan_control_allowed)
8841 return;
8842
8843 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008844 fan_control_resume_level = 0;
8845 rc = fan_get_status_safe(&fan_control_resume_level);
8846 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008847 pr_notice("failed to read fan level for later restore during resume: %d\n",
8848 rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008849
8850 /* if it is undefined, don't attempt to restore it.
8851 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008852 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008853 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008854}
8855
8856static void fan_resume(void)
8857{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008858 u8 current_level = 7;
8859 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008860 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008861
8862 /* DSDT *always* updates status on resume */
8863 tp_features.fan_ctrl_status_undef = 0;
8864
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008865 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008866 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008867 (fan_get_status_safe(&current_level) < 0))
8868 return;
8869
8870 switch (fan_control_access_mode) {
8871 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008872 /* never decrease fan level */
8873 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008874 break;
8875 case TPACPI_FAN_WR_ACPI_FANS:
8876 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008877 /* never decrease fan level, scale is:
8878 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8879 *
8880 * We expect the firmware to set either 7 or AUTO, but we
8881 * handle FULLSPEED out of paranoia.
8882 *
8883 * So, we can safely only restore FULLSPEED or 7, anything
8884 * else could slow the fan. Restoring AUTO is useless, at
8885 * best that's exactly what the DSDT already set (it is the
8886 * slower it uses).
8887 *
8888 * Always keep in mind that the DSDT *will* have set the
8889 * fans to what the vendor supposes is the best level. We
8890 * muck with it only to speed the fan up.
8891 */
8892 if (fan_control_resume_level != 7 &&
8893 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8894 return;
8895 else
8896 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8897 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008898 break;
8899 default:
8900 return;
8901 }
8902 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008903 pr_notice("restoring fan level to 0x%02x\n",
8904 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008905 rc = fan_set_level_safe(fan_control_resume_level);
8906 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008907 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008908 }
8909}
8910
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008911static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008912{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008913 int rc;
8914 u8 status;
8915 unsigned int speed = 0;
8916
8917 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008918 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008919 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008920 rc = fan_get_status_safe(&status);
8921 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008922 return rc;
8923
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008924 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008925 "level:\t\t%d\n",
8926 (status != 0) ? "enabled" : "disabled", status);
8927 break;
8928
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008929 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008930 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008931 rc = fan_get_status_safe(&status);
8932 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008933 return rc;
8934
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008935 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008936 (status != 0) ? "enabled" : "disabled");
8937
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008938 rc = fan_get_speed(&speed);
8939 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008940 return rc;
8941
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008942 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008943
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008944 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008945 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008946 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008947 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008948 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008949 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008950 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008951 break;
8952
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008953 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008954 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008955 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008956 }
8957
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008958 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008959 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008960
8961 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008962 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008963 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008964 break;
8965
8966 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008967 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008968 break;
8969 }
8970 }
8971
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008972 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008973 seq_printf(m, "commands:\tenable, disable\n"
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008974 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008975
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008976 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008977 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008978
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008979 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008980}
8981
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008982static int fan_write_cmd_level(const char *cmd, int *rc)
8983{
8984 int level;
8985
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008986 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008987 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008988 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008989 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008990 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008991 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008992 return 0;
8993
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008994 *rc = fan_set_level_safe(level);
8995 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008996 pr_err("level command accepted for unsupported access mode %d\n",
8997 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008998 else if (!*rc)
8999 tpacpi_disclose_usertask("procfs fan",
9000 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009001
9002 return 1;
9003}
9004
9005static int fan_write_cmd_enable(const char *cmd, int *rc)
9006{
9007 if (strlencmp(cmd, "enable") != 0)
9008 return 0;
9009
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009010 *rc = fan_set_enable();
9011 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009012 pr_err("enable command accepted for unsupported access mode %d\n",
9013 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009014 else if (!*rc)
9015 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009016
9017 return 1;
9018}
9019
9020static int fan_write_cmd_disable(const char *cmd, int *rc)
9021{
9022 if (strlencmp(cmd, "disable") != 0)
9023 return 0;
9024
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009025 *rc = fan_set_disable();
9026 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009027 pr_err("disable command accepted for unsupported access mode %d\n",
9028 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009029 else if (!*rc)
9030 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009031
9032 return 1;
9033}
9034
9035static int fan_write_cmd_speed(const char *cmd, int *rc)
9036{
9037 int speed;
9038
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02009039 /* TODO:
9040 * Support speed <low> <medium> <high> ? */
9041
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009042 if (sscanf(cmd, "speed %d", &speed) != 1)
9043 return 0;
9044
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009045 *rc = fan_set_speed(speed);
9046 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009047 pr_err("speed command accepted for unsupported access mode %d\n",
9048 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009049 else if (!*rc)
9050 tpacpi_disclose_usertask("procfs fan",
9051 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009052
9053 return 1;
9054}
9055
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009056static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9057{
9058 int interval;
9059
9060 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9061 return 0;
9062
9063 if (interval < 0 || interval > 120)
9064 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009065 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009066 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009067 tpacpi_disclose_usertask("procfs fan",
9068 "set watchdog timer to %d\n",
9069 interval);
9070 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009071
9072 return 1;
9073}
9074
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009075static int fan_write(char *buf)
9076{
9077 char *cmd;
9078 int rc = 0;
9079
9080 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009081 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009082 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009083 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009084 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009085 fan_write_cmd_disable(cmd, &rc) ||
9086 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009087 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009088 fan_write_cmd_speed(cmd, &rc))
9089 )
9090 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009091 else if (!rc)
9092 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009093 }
9094
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009095 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009096}
9097
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009098static struct ibm_struct fan_driver_data = {
9099 .name = "fan",
9100 .read = fan_read,
9101 .write = fan_write,
9102 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009103 .suspend = fan_suspend,
9104 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009105};
9106
David Henningsson420f9732013-10-16 23:10:31 +02009107/*************************************************************************
9108 * Mute LED subdriver
9109 */
9110
9111
9112struct tp_led_table {
9113 acpi_string name;
9114 int on_value;
9115 int off_value;
9116 int state;
9117};
9118
9119static struct tp_led_table led_tables[] = {
9120 [TPACPI_LED_MUTE] = {
9121 .name = "SSMS",
9122 .on_value = 1,
9123 .off_value = 0,
9124 },
9125 [TPACPI_LED_MICMUTE] = {
9126 .name = "MMTS",
9127 .on_value = 2,
9128 .off_value = 0,
9129 },
9130};
9131
9132static int mute_led_on_off(struct tp_led_table *t, bool state)
9133{
9134 acpi_handle temp;
9135 int output;
9136
Axel Lin3526eca2016-09-19 09:33:51 +08009137 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009138 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9139 return -EIO;
9140 }
9141
9142 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9143 state ? t->on_value : t->off_value))
9144 return -EIO;
9145
9146 t->state = state;
9147 return state;
9148}
9149
9150int tpacpi_led_set(int whichled, bool on)
9151{
9152 struct tp_led_table *t;
9153
9154 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9155 return -EINVAL;
9156
9157 t = &led_tables[whichled];
9158 if (t->state < 0 || t->state == on)
9159 return t->state;
9160 return mute_led_on_off(t, on);
9161}
9162EXPORT_SYMBOL_GPL(tpacpi_led_set);
9163
9164static int mute_led_init(struct ibm_init_struct *iibm)
9165{
9166 acpi_handle temp;
9167 int i;
9168
9169 for (i = 0; i < TPACPI_LED_MAX; i++) {
9170 struct tp_led_table *t = &led_tables[i];
9171 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9172 mute_led_on_off(t, false);
9173 else
9174 t->state = -ENODEV;
9175 }
9176 return 0;
9177}
9178
9179static void mute_led_exit(void)
9180{
9181 int i;
9182
9183 for (i = 0; i < TPACPI_LED_MAX; i++)
9184 tpacpi_led_set(i, false);
9185}
9186
Takashi Iwai119f4492014-02-12 16:32:45 +01009187static void mute_led_resume(void)
9188{
9189 int i;
9190
9191 for (i = 0; i < TPACPI_LED_MAX; i++) {
9192 struct tp_led_table *t = &led_tables[i];
9193 if (t->state >= 0)
9194 mute_led_on_off(t, t->state);
9195 }
9196}
9197
David Henningsson420f9732013-10-16 23:10:31 +02009198static struct ibm_struct mute_led_driver_data = {
9199 .name = "mute_led",
9200 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009201 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009202};
9203
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009204/****************************************************************************
9205 ****************************************************************************
9206 *
9207 * Infrastructure
9208 *
9209 ****************************************************************************
9210 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009211
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009212/*
9213 * HKEY event callout for other subdrivers go here
9214 * (yes, it is ugly, but it is quick, safe, and gets the job done
9215 */
9216static void tpacpi_driver_event(const unsigned int hkey_event)
9217{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009218 if (ibm_backlight_device) {
9219 switch (hkey_event) {
9220 case TP_HKEY_EV_BRGHT_UP:
9221 case TP_HKEY_EV_BRGHT_DOWN:
9222 tpacpi_brightness_notify_change();
9223 }
9224 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009225 if (alsa_card) {
9226 switch (hkey_event) {
9227 case TP_HKEY_EV_VOL_UP:
9228 case TP_HKEY_EV_VOL_DOWN:
9229 case TP_HKEY_EV_VOL_MUTE:
9230 volume_alsa_notify_change();
9231 }
9232 }
Hans de Goedec685e202017-02-09 16:44:13 +01009233 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9234 enum led_brightness brightness;
9235
9236 mutex_lock(&kbdlight_mutex);
9237
9238 /*
9239 * Check the brightness actually changed, setting the brightness
9240 * through kbdlight_set_level() also triggers this event.
9241 */
9242 brightness = kbdlight_sysfs_get(NULL);
9243 if (kbdlight_brightness != brightness) {
9244 kbdlight_brightness = brightness;
9245 led_classdev_notify_brightness_hw_changed(
9246 &tpacpi_led_kbdlight.led_classdev, brightness);
9247 }
9248
9249 mutex_unlock(&kbdlight_mutex);
9250 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009251}
9252
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009253static void hotkey_driver_event(const unsigned int scancode)
9254{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009255 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009256}
9257
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009258/* --------------------------------------------------------------------- */
9259
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009260/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009261static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009262
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009263/*
9264 * Module and infrastructure proble, init and exit handling
9265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309267static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009268
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009269#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009270static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009271{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009272 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009273
Jan van den Berg72a979f2014-09-17 00:01:08 +02009274 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009275}
9276#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9277
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009278static void ibm_exit(struct ibm_struct *ibm)
9279{
9280 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9281
9282 list_del_init(&ibm->all_drivers);
9283
9284 if (ibm->flags.acpi_notify_installed) {
9285 dbg_printk(TPACPI_DBG_EXIT,
9286 "%s: acpi_remove_notify_handler\n", ibm->name);
9287 BUG_ON(!ibm->acpi);
9288 acpi_remove_notify_handler(*ibm->acpi->handle,
9289 ibm->acpi->type,
9290 dispatch_acpi_notify);
9291 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009292 }
9293
9294 if (ibm->flags.proc_created) {
9295 dbg_printk(TPACPI_DBG_EXIT,
9296 "%s: remove_proc_entry\n", ibm->name);
9297 remove_proc_entry(ibm->name, proc_dir);
9298 ibm->flags.proc_created = 0;
9299 }
9300
9301 if (ibm->flags.acpi_driver_registered) {
9302 dbg_printk(TPACPI_DBG_EXIT,
9303 "%s: acpi_bus_unregister_driver\n", ibm->name);
9304 BUG_ON(!ibm->acpi);
9305 acpi_bus_unregister_driver(ibm->acpi->driver);
9306 kfree(ibm->acpi->driver);
9307 ibm->acpi->driver = NULL;
9308 ibm->flags.acpi_driver_registered = 0;
9309 }
9310
9311 if (ibm->flags.init_called && ibm->exit) {
9312 ibm->exit();
9313 ibm->flags.init_called = 0;
9314 }
9315
9316 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9317}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009318
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009319static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320{
9321 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009322 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323 struct proc_dir_entry *entry;
9324
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009325 BUG_ON(ibm == NULL);
9326
9327 INIT_LIST_HEAD(&ibm->all_drivers);
9328
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009329 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 return 0;
9331
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009332 dbg_printk(TPACPI_DBG_INIT,
9333 "probing for %s\n", ibm->name);
9334
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009335 if (iibm->init) {
9336 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009337 if (ret > 0)
9338 return 0; /* probe failed */
9339 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009340 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009341
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009342 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343 }
9344
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009345 if (ibm->acpi) {
9346 if (ibm->acpi->hid) {
9347 ret = register_tpacpi_subdriver(ibm);
9348 if (ret)
9349 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009350 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009351
9352 if (ibm->acpi->notify) {
9353 ret = setup_acpi_notify(ibm);
9354 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009355 pr_notice("disabling subdriver %s\n",
9356 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009357 ret = 0;
9358 goto err_out;
9359 }
9360 if (ret < 0)
9361 goto err_out;
9362 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009363 }
9364
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009365 dbg_printk(TPACPI_DBG_INIT,
9366 "%s installed\n", ibm->name);
9367
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009368 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009369 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009370
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009371 if (!mode)
9372 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009373 if (ibm->write)
9374 mode |= S_IWUSR;
9375 entry = proc_create_data(ibm->name, mode, proc_dir,
9376 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009377 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009378 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009379 ret = -ENODEV;
9380 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009381 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009382 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009385 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9386
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009388
9389err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009390 dbg_printk(TPACPI_DBG_INIT,
9391 "%s: at error exit path with result %d\n",
9392 ibm->name, ret);
9393
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009394 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009395 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396}
9397
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009398/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009400static bool __pure __init tpacpi_is_fw_digit(const char c)
9401{
9402 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9403}
9404
Jan van den Berg72a979f2014-09-17 00:01:08 +02009405static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009406 const char t)
9407{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009408 /*
9409 * Most models: xxyTkkWW (#.##c)
9410 * Ancient 570/600 and -SL lacks (#.##c)
9411 */
9412 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009413 tpacpi_is_fw_digit(s[0]) &&
9414 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009415 s[2] == t &&
9416 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009417 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009418 tpacpi_is_fw_digit(s[5]))
9419 return true;
9420
9421 /* New models: xxxyTkkW (#.##c); T550 and some others */
9422 return s && strlen(s) >= 8 &&
9423 tpacpi_is_fw_digit(s[0]) &&
9424 tpacpi_is_fw_digit(s[1]) &&
9425 tpacpi_is_fw_digit(s[2]) &&
9426 s[3] == t &&
9427 (s[4] == 'T' || s[4] == 'N') &&
9428 tpacpi_is_fw_digit(s[5]) &&
9429 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009430}
9431
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009432/* returns 0 - probe ok, or < 0 - probe error.
9433 * Probe ok doesn't mean thinkpad found.
9434 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9435static int __must_check __init get_thinkpad_model_data(
9436 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009437{
Jeff Garzik18552562007-10-03 15:15:40 -04009438 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009439 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009440 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009441
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009442 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009443 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009444
9445 memset(tp, 0, sizeof(*tp));
9446
9447 if (dmi_name_in_vendors("IBM"))
9448 tp->vendor = PCI_VENDOR_ID_IBM;
9449 else if (dmi_name_in_vendors("LENOVO"))
9450 tp->vendor = PCI_VENDOR_ID_LENOVO;
9451 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009452 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009453
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009454 s = dmi_get_system_info(DMI_BIOS_VERSION);
9455 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9456 if (s && !tp->bios_version_str)
9457 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009458
9459 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009460 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9461 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009462 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009463
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009464 tp->bios_model = tp->bios_version_str[0]
9465 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009466 tp->bios_release = (tp->bios_version_str[4] << 8)
9467 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009468
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009469 /*
9470 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9471 * X32 or newer, all Z series; Some models must have an
9472 * up-to-date BIOS or they will not be detected.
9473 *
9474 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9475 */
9476 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009477 if (sscanf(dev->name,
9478 "IBM ThinkPad Embedded Controller -[%17c",
9479 ec_fw_string) == 1) {
9480 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9481 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009482
9483 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009484 if (!tp->ec_version_str)
9485 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009486
9487 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9488 tp->ec_model = ec_fw_string[0]
9489 | (ec_fw_string[1] << 8);
9490 tp->ec_release = (ec_fw_string[4] << 8)
9491 | ec_fw_string[5];
9492 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009493 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
Joe Perches0978e012011-04-04 10:06:25 -07009494 ec_fw_string);
9495 pr_notice("please report this to %s\n",
9496 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009497 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009498 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009499 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009500 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009501
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009502 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009503 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009504 tp->model_str = kstrdup(s, GFP_KERNEL);
9505 if (!tp->model_str)
9506 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009507 } else {
9508 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009509 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009510 tp->model_str = kstrdup(s, GFP_KERNEL);
9511 if (!tp->model_str)
9512 return -ENOMEM;
9513 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009514 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009515
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009516 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9517 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9518 if (s && !tp->nummodel_str)
9519 return -ENOMEM;
9520
9521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009522}
9523
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009524static int __init probe_for_thinkpad(void)
9525{
9526 int is_thinkpad;
9527
9528 if (acpi_disabled)
9529 return -ENODEV;
9530
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009531 /* It would be dangerous to run the driver in this case */
9532 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9533 return -ENODEV;
9534
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009535 /*
9536 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009537 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009538 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009539 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9540 (thinkpad_id.ec_model != 0) ||
9541 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009542
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009543 /* The EC handler is required */
9544 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009545 if (!ec_handle) {
9546 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009547 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009548 return -ENODEV;
9549 }
9550
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009551 if (!is_thinkpad && !force_load)
9552 return -ENODEV;
9553
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009554 return 0;
9555}
9556
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009557static void __init thinkpad_acpi_init_banner(void)
9558{
Joe Perches0978e012011-04-04 10:06:25 -07009559 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9560 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009561
Joe Perches0978e012011-04-04 10:06:25 -07009562 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009563 (thinkpad_id.bios_version_str) ?
9564 thinkpad_id.bios_version_str : "unknown",
9565 (thinkpad_id.ec_version_str) ?
9566 thinkpad_id.ec_version_str : "unknown");
9567
9568 BUG_ON(!thinkpad_id.vendor);
9569
9570 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009571 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009572 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9573 "IBM" : ((thinkpad_id.vendor ==
9574 PCI_VENDOR_ID_LENOVO) ?
9575 "Lenovo" : "Unknown vendor"),
9576 thinkpad_id.model_str,
9577 (thinkpad_id.nummodel_str) ?
9578 thinkpad_id.nummodel_str : "unknown");
9579}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009580
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009581/* Module init, exit, parameters */
9582
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009583static struct ibm_init_struct ibms_init[] __initdata = {
9584 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009585 .data = &thinkpad_acpi_driver_data,
9586 },
9587 {
9588 .init = hotkey_init,
9589 .data = &hotkey_driver_data,
9590 },
9591 {
9592 .init = bluetooth_init,
9593 .data = &bluetooth_driver_data,
9594 },
9595 {
9596 .init = wan_init,
9597 .data = &wan_driver_data,
9598 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009599 {
9600 .init = uwb_init,
9601 .data = &uwb_driver_data,
9602 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009603#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009604 {
9605 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009606 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009607 .data = &video_driver_data,
9608 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009609#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009610 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +01009611 .init = kbdlight_init,
9612 .data = &kbdlight_driver_data,
9613 },
9614 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009615 .init = light_init,
9616 .data = &light_driver_data,
9617 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009618 {
9619 .init = cmos_init,
9620 .data = &cmos_driver_data,
9621 },
9622 {
9623 .init = led_init,
9624 .data = &led_driver_data,
9625 },
9626 {
9627 .init = beep_init,
9628 .data = &beep_driver_data,
9629 },
9630 {
9631 .init = thermal_init,
9632 .data = &thermal_driver_data,
9633 },
9634 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009635 .init = brightness_init,
9636 .data = &brightness_driver_data,
9637 },
9638 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009639 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009640 .data = &volume_driver_data,
9641 },
9642 {
9643 .init = fan_init,
9644 .data = &fan_driver_data,
9645 },
David Henningsson420f9732013-10-16 23:10:31 +02009646 {
9647 .init = mute_led_init,
9648 .data = &mute_led_driver_data,
9649 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009650};
9651
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009652static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9653{
9654 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009655 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009656
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009657 if (!kp || !kp->name || !val)
9658 return -EINVAL;
9659
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009660 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9661 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009662 WARN_ON(ibm == NULL);
9663
9664 if (!ibm || !ibm->name)
9665 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009666
9667 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9668 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009669 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009670 strcpy(ibms_init[i].param, val);
9671 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009672 return 0;
9673 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009674 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009675
9676 return -EINVAL;
9677}
9678
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009679module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009680MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009681 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009682
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009683module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009684MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009685
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009686module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009687MODULE_PARM_DESC(force_load,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009688 "Attempts to load the driver even on a mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009689
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009690module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009691MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009692 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009693
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009694module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009695MODULE_PARM_DESC(brightness_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009696 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009697
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009698module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009699MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009700 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009701
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009702#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009703module_param_named(volume_mode, volume_mode, uint, 0444);
9704MODULE_PARM_DESC(volume_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009705 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009706
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009707module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9708MODULE_PARM_DESC(volume_capabilities,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009709 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009710
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009711module_param_named(volume_control, volume_control_allowed, bool, 0444);
9712MODULE_PARM_DESC(volume_control,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009713 "Enables software override for the console audio control when true");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009714
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07009715module_param_named(software_mute, software_mute_requested, bool, 0444);
9716MODULE_PARM_DESC(software_mute,
9717 "Request full software mute control");
9718
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009719/* ALSA module API parameters */
9720module_param_named(index, alsa_index, int, 0444);
9721MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9722module_param_named(id, alsa_id, charp, 0444);
9723MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9724module_param_named(enable, alsa_enable, bool, 0444);
9725MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009726#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009727
Andy Shevchenko9b4bbbd2017-05-09 17:17:21 +03009728/* The module parameter can't be read back, that's why 0 is used here */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009729#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009730 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009731 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009732
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009733TPACPI_PARAM(hotkey);
9734TPACPI_PARAM(bluetooth);
9735TPACPI_PARAM(video);
9736TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009737TPACPI_PARAM(cmos);
9738TPACPI_PARAM(led);
9739TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009740TPACPI_PARAM(brightness);
9741TPACPI_PARAM(volume);
9742TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009743
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009744#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009745module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009746MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9747module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9748MODULE_PARM_DESC(wlsw_state,
9749 "Initial state of the emulated WLSW switch");
9750
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009751module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009752MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9753module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9754MODULE_PARM_DESC(bluetooth_state,
9755 "Initial state of the emulated bluetooth switch");
9756
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009757module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009758MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9759module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9760MODULE_PARM_DESC(wwan_state,
9761 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009762
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009763module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009764MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9765module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9766MODULE_PARM_DESC(uwb_state,
9767 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009768#endif
9769
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009770static void thinkpad_acpi_module_exit(void)
9771{
9772 struct ibm_struct *ibm, *itmp;
9773
9774 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9775
9776 list_for_each_entry_safe_reverse(ibm, itmp,
9777 &tpacpi_all_drivers,
9778 all_drivers) {
9779 ibm_exit(ibm);
9780 }
9781
9782 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9783
9784 if (tpacpi_inputdev) {
9785 if (tp_features.input_device_registered)
9786 input_unregister_device(tpacpi_inputdev);
9787 else
9788 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009789 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009790 }
9791
9792 if (tpacpi_hwmon)
9793 hwmon_device_unregister(tpacpi_hwmon);
9794
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009795 if (tpacpi_sensors_pdev)
9796 platform_device_unregister(tpacpi_sensors_pdev);
9797 if (tpacpi_pdev)
9798 platform_device_unregister(tpacpi_pdev);
9799
9800 if (tp_features.sensors_pdrv_attrs_registered)
9801 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9802 if (tp_features.platform_drv_attrs_registered)
9803 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9804
9805 if (tp_features.sensors_pdrv_registered)
9806 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9807
9808 if (tp_features.platform_drv_registered)
9809 platform_driver_unregister(&tpacpi_pdriver);
9810
9811 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009812 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009813
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009814 if (tpacpi_wq)
9815 destroy_workqueue(tpacpi_wq);
9816
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009817 kfree(thinkpad_id.bios_version_str);
9818 kfree(thinkpad_id.ec_version_str);
9819 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009820 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009821}
9822
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009823
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009824static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825{
9826 int ret, i;
9827
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009828 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9829
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009830 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009831
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009832 ret = get_thinkpad_model_data(&thinkpad_id);
9833 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009834 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009835 thinkpad_acpi_module_exit();
9836 return ret;
9837 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009838 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009839 if (ret) {
9840 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009841 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009844 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009845
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009846 thinkpad_acpi_init_banner();
9847 tpacpi_check_outdated_fw();
9848
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009849 TPACPI_ACPIHANDLE_INIT(ecrd);
9850 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009852 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9853 if (!tpacpi_wq) {
9854 thinkpad_acpi_module_exit();
9855 return -ENOMEM;
9856 }
9857
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009858 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009859 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009860 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009861 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009862 return -ENODEV;
9863 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009864
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009865 ret = platform_driver_register(&tpacpi_pdriver);
9866 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009867 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009868 thinkpad_acpi_module_exit();
9869 return ret;
9870 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009871 tp_features.platform_drv_registered = 1;
9872
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009873 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9874 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009875 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009876 thinkpad_acpi_module_exit();
9877 return ret;
9878 }
9879 tp_features.sensors_pdrv_registered = 1;
9880
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009881 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009882 if (!ret) {
9883 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009884 ret = tpacpi_create_driver_attributes(
9885 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009886 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009887 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009888 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009889 thinkpad_acpi_module_exit();
9890 return ret;
9891 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009892 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009893
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009894
9895 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009896 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009897 NULL, 0);
9898 if (IS_ERR(tpacpi_pdev)) {
9899 ret = PTR_ERR(tpacpi_pdev);
9900 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009901 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009902 thinkpad_acpi_module_exit();
9903 return ret;
9904 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009905 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009906 TPACPI_HWMON_DRVR_NAME,
9907 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009908 if (IS_ERR(tpacpi_sensors_pdev)) {
9909 ret = PTR_ERR(tpacpi_sensors_pdev);
9910 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009911 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009912 thinkpad_acpi_module_exit();
9913 return ret;
9914 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009915 tp_features.sensors_pdev_attrs_registered = 1;
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07009916 tpacpi_hwmon = hwmon_device_register_with_groups(
9917 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
9918
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009919 if (IS_ERR(tpacpi_hwmon)) {
9920 ret = PTR_ERR(tpacpi_hwmon);
9921 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009922 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009923 thinkpad_acpi_module_exit();
9924 return ret;
9925 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009926 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009927 tpacpi_inputdev = input_allocate_device();
9928 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009929 thinkpad_acpi_module_exit();
9930 return -ENOMEM;
9931 } else {
9932 /* Prepare input device, but don't register */
9933 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009934 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009935 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009936 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009937 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9938 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009939 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009940 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009941
9942 /* Init subdriver dependencies */
9943 tpacpi_detect_brightness_capabilities();
9944
9945 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009946 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9947 ret = ibm_init(&ibms_init[i]);
9948 if (ret >= 0 && *ibms_init[i].param)
9949 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009951 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009952 return ret;
9953 }
9954 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009955
9956 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9957
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009958 ret = input_register_device(tpacpi_inputdev);
9959 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009960 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009961 thinkpad_acpi_module_exit();
9962 return ret;
9963 } else {
9964 tp_features.input_device_registered = 1;
9965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009966
9967 return 0;
9968}
9969
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009970MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9971
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009972/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009973 * This will autoload the driver in almost every ThinkPad
9974 * in widespread use.
9975 *
9976 * Only _VERY_ old models, like the 240, 240x and 570 lack
9977 * the HKEY event interface.
9978 */
9979MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9980
9981/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009982 * DMI matching for module autoloading
9983 *
9984 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9985 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9986 *
9987 * Only models listed in thinkwiki will be supported, so add yours
9988 * if it is not there yet.
9989 */
9990#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009991 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009992
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009993/* Ancient thinkpad BIOSes have to be identified by
9994 * BIOS type or model number, and there are far less
9995 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009996IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009997
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009998MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9999MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010000MODULE_DESCRIPTION(TPACPI_DESC);
10001MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010002MODULE_LICENSE("GPL");
10003
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010004module_init(thinkpad_acpi_module_init);
10005module_exit(thinkpad_acpi_module_exit);